diff mbox

[5/5] genhomedircon: sysconf can return -1 without failure

Message ID 20180517051117.48454-6-jason@perfinion.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Jason Zaman May 17, 2018, 5:11 a.m. UTC
from getpwnam_r(3): "The call sysconf(_SC_GETPW_R_SIZE_MAX) returns
either -1, without changing errno, or an initial suggested size for buf.
(If this size is too small, the call fails with ERANGE, in which case
the caller can retry with a larger buffer.)"

The same can happen for _SC_GETGR_R_SIZE_MAX. 1024 appears to be a good
fallback but may need revisiting in the future.

This triggered an error on musl libc but could happen other places too.

Signed-off-by: Jason Zaman <jason@perfinion.com>
---
 libsemanage/src/genhomedircon.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c
index d09d82ff..3e61b510 100644
--- a/libsemanage/src/genhomedircon.c
+++ b/libsemanage/src/genhomedircon.c
@@ -972,9 +972,13 @@  static int add_user(genhomedircon_settings_t * s,
 	char uid[11];
 	char gid[11];
 
+	errno = 0;
 	/* Allocate space for the getpwnam_r buffer */
 	rbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
-	if (rbuflen <= 0)
+	if (rbuflen == -1 && errno == 0)
+		/* sysconf returning -1 with no errno means indeterminate size */
+		rbuflen = 1024;
+	else if (rbuflen <= 0)
 		goto cleanup;
 	rbuf = malloc(rbuflen);
 	if (rbuf == NULL)
@@ -1057,8 +1061,12 @@  static int get_group_users(genhomedircon_settings_t * s,
 	struct group grstorage, *group = NULL;
 	struct passwd *pw = NULL;
 
+	errno = 0;
 	grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
-	if (grbuflen <= 0)
+	if (grbuflen == -1 && errno == 0)
+		/* sysconf returning -1 with no errno means indeterminate size */
+		grbuflen = 1024;
+	else if (grbuflen <= 0)
 		goto cleanup;
 	grbuf = malloc(grbuflen);
 	if (grbuf == NULL)