diff mbox series

[09/11] libsemanage: support huge passwd entries

Message ID 20231219160943.334370-9-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit a1fa1c8ad794
Delegated to: Petr Lautrbach
Headers show
Series [01/11] libselinux/man: mention errno for regex compilation failure | expand

Commit Message

Christian Göttsche Dec. 19, 2023, 4:09 p.m. UTC
getpwnam_r(3) might return ERANGE in case the supplied buffer was too
short for the passwd entry.  Retry with a bigger buffer.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsemanage/src/genhomedircon.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c
index 66585987..ecb34fc7 100644
--- a/libsemanage/src/genhomedircon.c
+++ b/libsemanage/src/genhomedircon.c
@@ -985,9 +985,6 @@  static int add_user(genhomedircon_settings_t * s,
 		rbuflen = 1024;
 	else if (rbuflen <= 0)
 		goto cleanup;
-	rbuf = malloc(rbuflen);
-	if (rbuf == NULL)
-		goto cleanup;
 
 	if (user) {
 		prefix = semanage_user_get_prefix(user);
@@ -1005,7 +1002,17 @@  static int add_user(genhomedircon_settings_t * s,
 		homedir_role = prefix;
 	}
 
+retry:
+	rbuf = malloc(rbuflen);
+	if (rbuf == NULL)
+		goto cleanup;
+
 	retval = getpwnam_r(name, &pwstorage, rbuf, rbuflen, &pwent);
+	if (retval == ERANGE && rbuflen < LONG_MAX / 2) {
+		free(rbuf);
+		rbuflen *= 2;
+		goto retry;
+	}
 	if (retval != 0 || pwent == NULL) {
 		if (retval != 0 && retval != ENOENT) {
 			goto cleanup;