Subject: [PATCH] main/nfs-utils: Fix `_nss_name_to_gid()`

From: Jack O'Sullivan <[email-masked]>
Date: Tue, 24 Sep 2019 10:25:02 +0000

`sysconf(_SC_GETGR_R_SIZE_MAX)` returns -1 on musl. A patch exists to
work around this, but it is incomplete (`_nss_name_to_gid()` is not
included in `musl-svcgssd-sysconf.patch`.
---
 main/nfs-utils/APKBUILD                   |  4 +-
 main/nfs-utils/musl-svcgssd-sysconf.patch | 45 +++++++++++++++++------
 2 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/main/nfs-utils/APKBUILD b/main/nfs-utils/APKBUILD
index f1abf58f2b..f0b2fb6f9c 100644
--- a/main/nfs-utils/APKBUILD
+++ b/main/nfs-utils/APKBUILD
@@ -2,7 +2,7 @@
 # Maintainer: Natanael Copa <[email-masked]>
 pkgname=nfs-utils
 pkgver=2.4.1
-pkgrel=0
+pkgrel=1
 pkgdesc="kernel-mode NFS"
 url="http://linux-nfs.org"
 arch="all"
@@ -122,7 +122,7 @@ sha512sums="5f1e301de0bd75a12ff501e20784fe316553c162872fac08ccdc91d33abae91037e2
 674ecf2c4bc8e9364ddd0f34cc03c96674753494cbc5a5d157bd70ed4342ff90356c3e85c544510648dbe90cb43b7fd83ba50653bddffc4b3b5550367b6d0b8e  nfs-utils-mtab-sym.patch
 99609058351733dc9d02bd90156ded96bb04924b7c00c07be485e06f60d5fb77d1dbc4deca7d9fb88c5bb8fe96c483c5ec5a8fac26ca61fd351304def79b057d  musl-configure_ac.patch
 94d7ba23164660f1da9298494dff75c57f5a300cb32b2922bc2226fcdaded7eaaa0c50a59a145ac7c75639d177558b5f5594fb1f03a50f60f4c577c93b135748  musl-getservbyport.patch
-8499b0d129a86d56736720b6bd2caee042f9ad9a3504ba88d4ccc5f29fa55bc63ed015f84b5dcf958f1b05907e8cdb2e76fd6d56aad3a17c839aee564573e2e0  musl-svcgssd-sysconf.patch
+52eeade44753f2002bf99d58ad4982086aab74ef8b14de46be547f23508197f58a6ff529145f96de7f031ac0bb7779b648d05fd981cdd91556dd13d068dfe57b  musl-svcgssd-sysconf.patch
 9647d8f4a64a95a7abf5e26f040cf5567670e2194ce307a971a36eaae7fe490af5a494e7b380a48c88e309f4c25777d73d6c140b224aeed829fbc46b1d0baa64  limits.patch
 f7feb79cfcab0478affb640d1e5ad059757c88d51cc790fd54cde2fd7ed2e3cfd8f7f4c2de993d99da03e8ce3bdfb2750a4cb997b850fe33d0ef76d9b91c9018  nfs.initd
 89259b9f0878658d48792b5b2f42b43c966ed098dba1fecf9e07fb0de4aab37ad67655ea8dbcc2361ddab2b5013b2de35a03048a513aaeedf790e4b416a35a54  nfsmount.initd
diff --git a/main/nfs-utils/musl-svcgssd-sysconf.patch b/main/nfs-utils/musl-svcgssd-sysconf.patch
index 7e658013b8..ec280ccaa8 100644
--- a/main/nfs-utils/musl-svcgssd-sysconf.patch
+++ b/main/nfs-utils/musl-svcgssd-sysconf.patch
@@ -1,6 +1,6 @@
 --- a/support/nfsidmap/libnfsidmap.c
 +++ b/support/nfsidmap/libnfsidmap.c
-@@ -430,11 +430,17 @@
+@@ -432,11 +432,17 @@ int nfs4_init_name_mapping(char *conffil
  
  	nobody_user = conf_get_str("Mapping", "Nobody-User");
  	if (nobody_user) {
@@ -19,7 +19,7 @@
  		buf = malloc(sizeof(*buf) + buflen);
  		if (buf) {
  			err = getpwnam_r(nobody_user, buf, ((char *)buf) + sizeof(*buf), buflen, &pw);
-@@ -451,10 +457,16 @@
+@@ -453,11 +459,17 @@ int nfs4_init_name_mapping(char *conffil
  
  	nobody_group = conf_get_str("Mapping", "Nobody-Group");
  	if (nobody_group) {
@@ -29,17 +29,18 @@
  		struct group *buf;
  		struct group *gr = NULL;
  		int err;
-+
+ 
 +		/*sysconf can return -1 when _SC_GETGR_R_SIZE_MAX is not defined, like on musl systems, if cast to size_t this will lead
 +		  to an integer overflow, which leads to a buffer overflow and crashes svcgssd */
 +		if (scbuflen > 0)
 +			buflen = (size_t)scbuflen;
- 
++
  		buf = malloc(sizeof(*buf) + buflen);
  		if (buf) {
+ 			err = getgrnam_r(nobody_group, buf, ((char *)buf) + sizeof(*buf), buflen, &gr);
 --- a/support/nfsidmap/static.c
 +++ b/support/nfsidmap/static.c
-@@ -98,10 +98,14 @@
+@@ -98,10 +98,14 @@ static struct passwd *static_getpwnam(co
  {
  	struct passwd *pw;
  	struct pwbuf *buf;
@@ -55,7 +56,7 @@
  	buf = malloc(sizeof(*buf) + buflen);
  	if (!buf) {
  		err = ENOMEM;
-@@ -149,9 +153,13 @@
+@@ -149,10 +153,14 @@ static struct group *static_getgrnam(con
  {
  	struct group *gr;
  	struct grbuf *buf;
@@ -64,15 +65,16 @@
 +	size_t buflen = 1024;
  	char *localgroup;
  	int err;
-+
+ 
 +	if (scbuflen > 0)
 +		buflen = (size_t)scbuflen;
- 
++
  	buf = malloc(sizeof(*buf) + buflen);
  	if (!buf) {
+ 		err = ENOMEM;
 --- a/support/nfsidmap/nss.c
 +++ b/support/nfsidmap/nss.c
-@@ -91,9 +91,13 @@
+@@ -91,9 +91,13 @@ static int nss_uid_to_name(uid_t uid, ch
  	struct passwd *pw = NULL;
  	struct passwd pwbuf;
  	char *buf;
@@ -87,7 +89,7 @@
  	buf = malloc(buflen);
  	if (!buf)
  		goto out;
-@@ -119,9 +123,13 @@
+@@ -119,9 +123,13 @@ static int nss_gid_to_name(gid_t gid, ch
  	struct group *gr = NULL;
  	struct group grbuf;
  	char *buf;
@@ -102,7 +104,7 @@
  	if (domain == NULL)
  		domain = get_default_domain();
  
-@@ -192,12 +200,13 @@
+@@ -192,12 +200,13 @@ static struct passwd *nss_getpwnam(const
  {
  	struct passwd *pw;
  	struct pwbuf *buf;
@@ -119,3 +121,24 @@
  
  	buf = malloc(sizeof(*buf) + buflen);
  	if (buf == NULL)
+@@ -301,7 +310,8 @@ static int _nss_name_to_gid(char *name,
+ 	struct group *gr = NULL;
+ 	struct group grbuf;
+ 	char *buf, *domain;
+-	size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
++	long scbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
++	size_t buflen = 1024;
+ 	int err = -EINVAL;
+ 	char *localname = NULL;
+ 	char *ref_name = NULL;
+@@ -327,8 +337,8 @@ static int _nss_name_to_gid(char *name,
+ 	}
+ 
+ 	err = -ENOMEM;
+-	if (buflen > UINT_MAX)
+-		goto out_name;
++	if (scbuflen > 0)
++		buflen = (size_t)scbuflen;
+ 
+ 	do {
+ 		buf = malloc(buflen);
-- 
2.22.0