ff6046
From ecc4a34067ed7c03b9ee710aa5e5976fded48c2a Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Fri, 18 Jan 2019 20:13:55 +0100
ff6046
Subject: [PATCH] nss: unportect errno before writing to NSS' *errnop
ff6046
ff6046
Fixes: #11321
ff6046
(cherry picked from commit cdccd29f39cd20cb2a8b71e50445eb839f076331)
ff6046
ff6046
Resolves: #1691691
ff6046
---
ff6046
 src/nss-myhostname/nss-myhostname.c | 13 +++++++++++++
ff6046
 src/nss-mymachines/nss-mymachines.c | 13 +++++++++++++
ff6046
 src/nss-resolve/nss-resolve.c       |  8 ++++++++
ff6046
 src/nss-systemd/nss-systemd.c       | 10 ++++++++++
ff6046
 4 files changed, 44 insertions(+)
ff6046
ff6046
diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c
ff6046
index 5abc0c91bf..e491351dee 100644
ff6046
--- a/src/nss-myhostname/nss-myhostname.c
ff6046
+++ b/src/nss-myhostname/nss-myhostname.c
ff6046
@@ -74,6 +74,7 @@ enum nss_status _nss_myhostname_gethostbyname4_r(
ff6046
         } else {
ff6046
                 hn = gethostname_malloc();
ff6046
                 if (!hn) {
ff6046
+                        UNPROTECT_ERRNO;
ff6046
                         *errnop = ENOMEM;
ff6046
                         *h_errnop = NO_RECOVERY;
ff6046
                         return NSS_STATUS_TRYAGAIN;
ff6046
@@ -96,6 +97,7 @@ enum nss_status _nss_myhostname_gethostbyname4_r(
ff6046
         l = strlen(canonical);
ff6046
         ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * (n_addresses > 0 ? n_addresses : 2);
ff6046
         if (buflen < ms) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 *h_errnop = NETDB_INTERNAL;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
@@ -186,6 +188,8 @@ static enum nss_status fill_in_hostent(
ff6046
         assert(errnop);
ff6046
         assert(h_errnop);
ff6046
 
ff6046
+        PROTECT_ERRNO;
ff6046
+
ff6046
         alen = FAMILY_ADDRESS_SIZE(af);
ff6046
 
ff6046
         for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
ff6046
@@ -202,6 +206,7 @@ static enum nss_status fill_in_hostent(
ff6046
                 (c > 0 ? c+1 : 2) * sizeof(char*);
ff6046
 
ff6046
         if (buflen < ms) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 *h_errnop = NETDB_INTERNAL;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
@@ -321,6 +326,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r(
ff6046
                 af = AF_INET;
ff6046
 
ff6046
         if (!IN_SET(af, AF_INET, AF_INET6)) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = EAFNOSUPPORT;
ff6046
                 *h_errnop = NO_DATA;
ff6046
                 return NSS_STATUS_UNAVAIL;
ff6046
@@ -343,6 +349,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r(
ff6046
         } else {
ff6046
                 hn = gethostname_malloc();
ff6046
                 if (!hn) {
ff6046
+                        UNPROTECT_ERRNO;
ff6046
                         *errnop = ENOMEM;
ff6046
                         *h_errnop = NO_RECOVERY;
ff6046
                         return NSS_STATUS_TRYAGAIN;
ff6046
@@ -362,6 +369,8 @@ enum nss_status _nss_myhostname_gethostbyname3_r(
ff6046
                 local_address_ipv4 = LOCALADDRESS_IPV4;
ff6046
         }
ff6046
 
ff6046
+        UNPROTECT_ERRNO;
ff6046
+
ff6046
         return fill_in_hostent(
ff6046
                         canonical, additional,
ff6046
                         af,
ff6046
@@ -401,12 +410,14 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r(
ff6046
         assert(h_errnop);
ff6046
 
ff6046
         if (!IN_SET(af, AF_INET, AF_INET6)) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = EAFNOSUPPORT;
ff6046
                 *h_errnop = NO_DATA;
ff6046
                 return NSS_STATUS_UNAVAIL;
ff6046
         }
ff6046
 
ff6046
         if (len != FAMILY_ADDRESS_SIZE(af)) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = EINVAL;
ff6046
                 *h_errnop = NO_RECOVERY;
ff6046
                 return NSS_STATUS_UNAVAIL;
ff6046
@@ -461,6 +472,7 @@ found:
ff6046
         if (!canonical || additional_from_hostname) {
ff6046
                 hn = gethostname_malloc();
ff6046
                 if (!hn) {
ff6046
+                        UNPROTECT_ERRNO;
ff6046
                         *errnop = ENOMEM;
ff6046
                         *h_errnop = NO_RECOVERY;
ff6046
                         return NSS_STATUS_TRYAGAIN;
ff6046
@@ -472,6 +484,7 @@ found:
ff6046
                         additional = hn;
ff6046
         }
ff6046
 
ff6046
+        UNPROTECT_ERRNO;
ff6046
         return fill_in_hostent(
ff6046
                         canonical, additional,
ff6046
                         af,
ff6046
diff --git a/src/nss-mymachines/nss-mymachines.c b/src/nss-mymachines/nss-mymachines.c
ff6046
index 9b81cd9ad1..af9f0bf41f 100644
ff6046
--- a/src/nss-mymachines/nss-mymachines.c
ff6046
+++ b/src/nss-mymachines/nss-mymachines.c
ff6046
@@ -134,6 +134,7 @@ enum nss_status _nss_mymachines_gethostbyname4_r(
ff6046
         l = strlen(name);
ff6046
         ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
ff6046
         if (buflen < ms) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 *h_errnop = NETDB_INTERNAL;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
@@ -208,6 +209,7 @@ enum nss_status _nss_mymachines_gethostbyname4_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         *h_errnop = NO_DATA;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
@@ -289,6 +291,7 @@ enum nss_status _nss_mymachines_gethostbyname3_r(
ff6046
         ms = ALIGN(l+1) + c * ALIGN(alen) + (c+2) * sizeof(char*);
ff6046
 
ff6046
         if (buflen < ms) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 *h_errnop = NETDB_INTERNAL;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
@@ -372,6 +375,7 @@ enum nss_status _nss_mymachines_gethostbyname3_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         *h_errnop = NO_DATA;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
@@ -455,6 +459,7 @@ enum nss_status _nss_mymachines_getpwnam_r(
ff6046
 
ff6046
         l = strlen(name);
ff6046
         if (buflen < l+1) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -472,6 +477,7 @@ enum nss_status _nss_mymachines_getpwnam_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
@@ -530,6 +536,7 @@ enum nss_status _nss_mymachines_getpwuid_r(
ff6046
                 return NSS_STATUS_NOTFOUND;
ff6046
 
ff6046
         if (snprintf(buffer, buflen, "vu-%s-" UID_FMT, machine, (uid_t) mapped) >= (int) buflen) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -545,6 +552,7 @@ enum nss_status _nss_mymachines_getpwuid_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
@@ -623,6 +631,7 @@ enum nss_status _nss_mymachines_getgrnam_r(
ff6046
 
ff6046
         l = sizeof(char*) + strlen(name) + 1;
ff6046
         if (buflen < l) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -638,6 +647,7 @@ enum nss_status _nss_mymachines_getgrnam_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
@@ -696,12 +706,14 @@ enum nss_status _nss_mymachines_getgrgid_r(
ff6046
                 return NSS_STATUS_NOTFOUND;
ff6046
 
ff6046
         if (buflen < sizeof(char*) + 1) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
 
ff6046
         memzero(buffer, sizeof(char*));
ff6046
         if (snprintf(buffer + sizeof(char*), buflen - sizeof(char*), "vg-%s-" GID_FMT, machine, (gid_t) mapped) >= (int) buflen) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -714,6 +726,7 @@ enum nss_status _nss_mymachines_getgrgid_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c
ff6046
index b2bb698ded..145dbdd60c 100644
ff6046
--- a/src/nss-resolve/nss-resolve.c
ff6046
+++ b/src/nss-resolve/nss-resolve.c
ff6046
@@ -167,6 +167,7 @@ enum nss_status _nss_resolve_gethostbyname4_r(
ff6046
         l = strlen(canonical);
ff6046
         ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
ff6046
         if (buflen < ms) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 *h_errnop = NETDB_INTERNAL;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
@@ -248,6 +249,7 @@ enum nss_status _nss_resolve_gethostbyname4_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         *h_errnop = NO_RECOVERY;
ff6046
         return ret;
ff6046
@@ -340,6 +342,7 @@ enum nss_status _nss_resolve_gethostbyname3_r(
ff6046
         ms = ALIGN(l+1) + c * ALIGN(alen) + (c+2) * sizeof(char*);
ff6046
 
ff6046
         if (buflen < ms) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 *h_errnop = NETDB_INTERNAL;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
@@ -431,6 +434,7 @@ enum nss_status _nss_resolve_gethostbyname3_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         *h_errnop = NO_RECOVERY;
ff6046
         return ret;
ff6046
@@ -468,12 +472,14 @@ enum nss_status _nss_resolve_gethostbyaddr2_r(
ff6046
         assert(h_errnop);
ff6046
 
ff6046
         if (!IN_SET(af, AF_INET, AF_INET6)) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = EAFNOSUPPORT;
ff6046
                 *h_errnop = NO_DATA;
ff6046
                 return NSS_STATUS_UNAVAIL;
ff6046
         }
ff6046
 
ff6046
         if (len != FAMILY_ADDRESS_SIZE(af)) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = EINVAL;
ff6046
                 *h_errnop = NO_RECOVERY;
ff6046
                 return NSS_STATUS_UNAVAIL;
ff6046
@@ -547,6 +553,7 @@ enum nss_status _nss_resolve_gethostbyaddr2_r(
ff6046
               c * sizeof(char*);        /* pointers to aliases, plus trailing NULL */
ff6046
 
ff6046
         if (buflen < ms) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 *h_errnop = NETDB_INTERNAL;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
@@ -607,6 +614,7 @@ enum nss_status _nss_resolve_gethostbyaddr2_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         *h_errnop = NO_RECOVERY;
ff6046
         return ret;
ff6046
diff --git a/src/nss-systemd/nss-systemd.c b/src/nss-systemd/nss-systemd.c
ff6046
index f554828d49..f8db27ae27 100644
ff6046
--- a/src/nss-systemd/nss-systemd.c
ff6046
+++ b/src/nss-systemd/nss-systemd.c
ff6046
@@ -210,6 +210,7 @@ enum nss_status _nss_systemd_getpwnam_r(
ff6046
 
ff6046
         l = strlen(name);
ff6046
         if (buflen < l+1) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -227,6 +228,7 @@ enum nss_status _nss_systemd_getpwnam_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
@@ -310,6 +312,7 @@ enum nss_status _nss_systemd_getpwuid_r(
ff6046
 
ff6046
         l = strlen(translated) + 1;
ff6046
         if (buflen < l) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -327,6 +330,7 @@ enum nss_status _nss_systemd_getpwuid_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
@@ -408,6 +412,7 @@ enum nss_status _nss_systemd_getgrnam_r(
ff6046
 
ff6046
         l = sizeof(char*) + strlen(name) + 1;
ff6046
         if (buflen < l) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -423,6 +428,7 @@ enum nss_status _nss_systemd_getgrnam_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
@@ -506,6 +512,7 @@ enum nss_status _nss_systemd_getgrgid_r(
ff6046
 
ff6046
         l = sizeof(char*) + strlen(translated) + 1;
ff6046
         if (buflen < l) {
ff6046
+                UNPROTECT_ERRNO;
ff6046
                 *errnop = ERANGE;
ff6046
                 return NSS_STATUS_TRYAGAIN;
ff6046
         }
ff6046
@@ -521,6 +528,7 @@ enum nss_status _nss_systemd_getgrgid_r(
ff6046
         return NSS_STATUS_SUCCESS;
ff6046
 
ff6046
 fail:
ff6046
+        UNPROTECT_ERRNO;
ff6046
         *errnop = -r;
ff6046
         return NSS_STATUS_UNAVAIL;
ff6046
 }
ff6046
@@ -740,6 +748,7 @@ enum nss_status _nss_systemd_getpwent_r(struct passwd *result, char *buffer, siz
ff6046
         LIST_FOREACH(entries, p, getpwent_data.position) {
ff6046
                 len = strlen(p->name) + 1;
ff6046
                 if (buflen < len) {
ff6046
+                        UNPROTECT_ERRNO;
ff6046
                         *errnop = ERANGE;
ff6046
                         ret = NSS_STATUS_TRYAGAIN;
ff6046
                         goto finalize;
ff6046
@@ -791,6 +800,7 @@ enum nss_status _nss_systemd_getgrent_r(struct group *result, char *buffer, size
ff6046
         LIST_FOREACH(entries, p, getgrent_data.position) {
ff6046
                 len = sizeof(char*) + strlen(p->name) + 1;
ff6046
                 if (buflen < len) {
ff6046
+                        UNPROTECT_ERRNO;
ff6046
                         *errnop = ERANGE;
ff6046
                         ret = NSS_STATUS_TRYAGAIN;
ff6046
                         goto finalize;