Skip to contentSkip to conversation

nfsidmap: fallback if sysconf(_SC_GET(PW|GR)_R_SIZE_MAX) doesn't exist

PATCH New Jan Tatje 1 participant 1 message May 8, 2024

Subsystems (2)
lore
Share & tools
lore

Jan Tatje

author
#0 lore raw

PATCH 1/1 nfsidmap: fallback if sysconf(_SC_GET(PW|GR)_R_SIZE_MAX) doesn't exist

On musl-libc systems _SC_GET(PW|GR)_R_SIZE_MAX does not exist. If sysconf returns -1 start with a sane value and call get(pw|gr)(nam|uid)_r repeatedly until it no longer returns ERANGE.

---
 support/nfsidmap/gums.c        | 22 +++++++++++--
 support/nfsidmap/libnfsidmap.c | 29 +++++++++++++++--
 support/nfsidmap/nss.c         | 59 +++++++++++++++++++++++++++-------
 support/nfsidmap/regex.c       | 54 +++++++++++++++++++++++++++----
 support/nfsidmap/static.c      | 22 +++++++++++--
 5 files changed, 159 insertions(+), 27 deletions(-)
support/nfsidmap/gums.c +19 -3
index 1d6eb318..caffc679 100644
@@ -475,13 +475,25 @@ static int translate_to_uid(char *local_uid, uid_t *uid, uid_t *gid)
 	int ret = -1;
 	struct passwd *pw = NULL;
 	struct pwbuf *buf = NULL;
-	size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+	long scbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+	size_t buflen = 1024;
+
+	if (scbuflen > 0)
+		buflen = (size_t)scbuflen;
 
 	buf = malloc(sizeof(*buf) + buflen);
 	if (buf == NULL)
 		goto out;
 
-	ret = getpwnam_r(local_uid, &buf->pwbuf, buf->buf, buflen, &pw);
+	while ((ret = getpwnam_r(local_uid, &buf->pwbuf, buf->buf, buflen, &pw)) == ERANGE) {
+		buflen = buflen * 2;
+		struct pwbuf *nbuf = realloc(buf, sizeof(*buf) + buflen);
+		if (nbuf == NULL) {
+			ret = ENOMEM;
+			goto out;
+		}
+		buf = nbuf;
+	}
 	if (pw == NULL) {
 		IDMAP_LOG(0, ("getpwnam: name %s not found\n", local_uid));
 		goto out;
@@ -501,9 +513,13 @@ static int translate_to_gid(char *local_gid, uid_t *gid)
 	struct group *gr = NULL;
 	struct group grbuf;
 	char *buf = NULL;
-	size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+	long scbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+	size_t buflen = 1024;
 	int ret = -1;
 
+	if (scbuflen > 0)
+		buflen = (size_t)scbuflen;
+
 	do {
 		buf = malloc(buflen);
 		if (buf == NULL)
support/nfsidmap/libnfsidmap.c +26 -3
index f8c36480..14cafc3d 100644
@@ -457,14 +457,26 @@ int nfs4_init_name_mapping(char *conffile)
 
 	nobody_user = conf_get_str("Mapping", "Nobody-User");
 	if (nobody_user) {
-		size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+		long scbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+		size_t buflen = 1024;
 		struct passwd *buf;
 		struct passwd *pw = NULL;
 		int err;
 
+		if (scbuflen > 0)
+			buflen = (size_t)scbuflen;
+
 		buf = malloc(sizeof(*buf) + buflen);
 		if (buf) {
 			err = getpwnam_r(nobody_user, buf, ((char *)buf) + sizeof(*buf), buflen, &pw);
+			while ((err = getpwnam_r(nobody_user, buf, ((char *)buf) + sizeof(*buf), buflen, &pw)) == ERANGE) {
+				buflen = buflen * 2;
+				struct passwd* nbuf = realloc(buf, sizeof(*buf) + buflen);
+				if (nbuf == NULL) {
+					break;
+				}
+				buf = nbuf;
+			}
 			if (err == 0 && pw != NULL)
 				nobody_uid = pw->pw_uid;
 			else
@@ -478,14 +490,25 @@ int nfs4_init_name_mapping(char *conffile)
 
 	nobody_group = conf_get_str("Mapping", "Nobody-Group");
 	if (nobody_group) {
-		size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+		long scbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+		size_t buflen = 1024;
 		struct group *buf;
 		struct group *gr = NULL;
 		int err;
 
+		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);
+			while ((err = getgrnam_r(nobody_group, buf, ((char *)buf) + sizeof(*buf), buflen, &gr)) == ERANGE) {
+				buflen = buflen * 2;
+				struct group *nbuf = realloc(buf, sizeof(*buf) + buflen);
+				if (nbuf == NULL) {
+					break;
+				}
+				buf = nbuf;
+			}
 			if (err == 0 && gr != NULL)
 				nobody_gid = gr->gr_gid;
 			else
support/nfsidmap/nss.c +48 -11

117 lines not on this page · raw message

support/nfsidmap/regex.c +47 -7

112 lines not on this page · raw message

support/nfsidmap/static.c +19 -3
index 8ac4a398..0bb1728d 100644
@@ -98,10 +98,14 @@ static struct passwd *static_getpwnam(const char *name,
 {
 	struct passwd *pw;
 	struct pwbuf *buf;
-	size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+	long scbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+	size_t buflen = 1024;
 	char *localname;
 	int err;
 
+	if (scbuflen > 0)
+		buflen = (size_t)scbuflen;
+
 	buf = malloc(sizeof(*buf) + buflen);
 	if (!buf) {
 		err = ENOMEM;
@@ -149,10 +153,14 @@ static struct group *static_getgrnam(const char *name,
 {
 	struct group *gr;
 	struct grbuf *buf;
-	size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+	long scbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+	size_t buflen = 1024;
 	char *localgroup;
 	int err;
 
+	if (scbuflen > 0)
+		buflen = (size_t)scbuflen;
+
 	buf = malloc(sizeof(*buf) + buflen);
 	if (!buf) {
 		err = ENOMEM;
@@ -166,7 +174,15 @@ static struct group *static_getgrnam(const char *name,
 	}
 
 again:
-	err = getgrnam_r(localgroup, &buf->grbuf, buf->buf, buflen, &gr);
+	while ((err = getgrnam_r(localgroup, &buf->grbuf, buf->buf, buflen, &gr)) == ERANGE) {
+		buflen = buflen * 2;
+		struct grbuf *nbuf = realloc(buf, sizeof(*buf) + buflen);
+		if (nbuf == NULL) {
+			err = ENOMEM;
+			goto err_free_buf;
+		}
+		buf = nbuf;
+	}
 
 	if (err == EINTR)
 		goto again;
Signature · 1 line
2.45.0