Fix: runas: less-than-zero comparison of an unsigned value
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 6 Aug 2021 13:40:20 +0000 (09:40 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 6 Aug 2021 15:43:11 +0000 (11:43 -0400)
Fixes two defects found by Coverity related to unsigned integers being
treated as signed.

Reported by Coverity:
    CID 1461333:  Control flow issues  (NO_EFFECT)
    This less-than-zero comparison of an unsigned value is never true. "buf_size < 0UL".

    CID 1461332:  Integer handling issues  (NEGATIVE_RETURNS)
    "buf_size" is passed to a parameter that cannot be negative.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: Id6d4a71960f2ef34f14c05e66ef5d934b7a3e524

src/common/runas.c

index ab43423b1a3fd86ff798d45fb825a4b6e2392dba..f714ba59daf3d3d4af0b2ae78eaf9d82001b873d 100644 (file)
@@ -907,14 +907,15 @@ static int get_user_infos_from_uid(
 {
        int ret;
        char *buf = NULL;
-       size_t buf_size;
+       long raw_get_pw_buf_size;
+       size_t get_pw_buf_size;
        struct passwd pwd;
        struct passwd *result = NULL;
 
        /* Fetch the max size for the temporary buffer. */
        errno = 0;
-       buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
-       if (buf_size < 0) {
+       raw_get_pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+       if (raw_get_pw_buf_size < 0) {
                if (errno != 0) {
                        PERROR("Failed to query _SC_GETPW_R_SIZE_MAX");
                        goto error;
@@ -923,16 +924,18 @@ static int get_user_infos_from_uid(
                /* Limit is indeterminate. */
                WARN("Failed to query _SC_GETPW_R_SIZE_MAX as it is "
                        "indeterminate; falling back to default buffer size");
-               buf_size = GETPW_BUFFER_FALLBACK_SIZE;
+               raw_get_pw_buf_size = GETPW_BUFFER_FALLBACK_SIZE;
        }
 
-       buf = zmalloc(buf_size);
+       get_pw_buf_size = (size_t) raw_get_pw_buf_size;
+
+       buf = zmalloc(get_pw_buf_size);
        if (buf == NULL) {
                PERROR("Failed to allocate buffer to get password file entries");
                goto error;
        }
 
-       ret = getpwuid_r(uid, &pwd, buf, buf_size, &result);
+       ret = getpwuid_r(uid, &pwd, buf, get_pw_buf_size, &result);
        if (ret < 0) {
                PERROR("Failed to get user information for user:  uid = %d",
                                (int) uid);
This page took 0.02843 seconds and 5 git commands to generate.