(Redirecting this to ports@)

Could you folks test this patch against dovecot from -stable?  I only
did compile testing on -current.  I don't know how the allocator(s)
handle failures nor how would i_realloc handle pwbuf_size ==
old_pwbuf_size, but this looks safe.


$OpenBSD$

Hack: we avoid the actual ERANGE error case by always providing a large
enough buffer.

--- src/lib/ipwd.c.orig Tue Nov 19 21:36:30 2013
+++ src/lib/ipwd.c      Sun Jun 22 04:31:43 2014
@@ -15,24 +15,26 @@ static size_t pwbuf_size, grbuf_size;
 static void pw_init(void)
 {
        size_t old_pwbuf_size = pwbuf_size;
+       size_t new_pwbuf_size;
 
-       if (pwbuf == NULL || errno == ERANGE) {
-               pwbuf_size = nearest_power(old_pwbuf_size + 1);
-               if (pwbuf_size < PWBUF_MIN_SIZE)
-                       pwbuf_size = PWBUF_MIN_SIZE;
-               pwbuf = i_realloc(pwbuf, old_pwbuf_size, pwbuf_size);
+       if (pwbuf == NULL) {
+               new_pwbuf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+               pwbuf = i_realloc(pwbuf, old_pwbuf_size, new_pwbuf_size);
+               if (pwbuf != NULL)
+                       pwbuf_size = new_pwbuf_size;
        }
 }
 
 static void gr_init(void)
 {
        size_t old_grbuf_size = grbuf_size;
+       size_t new_grbuf_size;
 
-       if (grbuf == NULL || errno == ERANGE) {
-               grbuf_size = nearest_power(old_grbuf_size + 1);
-               if (grbuf_size < PWBUF_MIN_SIZE)
-                       grbuf_size = PWBUF_MIN_SIZE;
-               grbuf = i_realloc(grbuf, old_grbuf_size, grbuf_size);
+       if (grbuf == NULL) {
+               new_grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
+               grbuf = i_realloc(grbuf, old_grbuf_size, new_grbuf_size);
+               if (grbuf != NULL)
+                       grbuf_size = new_grbuf_size;
        }
 }
 
@@ -50,11 +52,6 @@ int i_getpwnam(const char *name, struct passwd *pwd_r)
        do {
                pw_init();
                errno = getpwnam_r(name, pwd_r, pwbuf, pwbuf_size, &result);
-#ifdef __OpenBSD__
-               /* OpenBSD returns 1 for all errors, assume it's ERANGE */
-               if (errno == 1)
-                       errno = ERANGE;
-#endif
        } while (errno == ERANGE);
        if (result != NULL)
                return 1;
@@ -73,11 +70,6 @@ int i_getpwuid(uid_t uid, struct passwd *pwd_r)
        do {
                pw_init();
                errno = getpwuid_r(uid, pwd_r, pwbuf, pwbuf_size, &result);
-#ifdef __OpenBSD__
-               /* OpenBSD returns 1 for all errors, assume it's ERANGE */
-               if (errno == 1)
-                       errno = ERANGE;
-#endif
        } while (errno == ERANGE);
        if (result != NULL)
                return 1;


-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to