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