Skip to content

Medium: get_userid() Memory Management Issues #5

@smooge

Description

@smooge

Priority: MEDIUM
File: Src/checkX.c
Function: get_userid()
Discovered: During security pattern testing with test_checkx_security_patterns.c

Description: Multiple memory management and buffer safety issues in get_userid() function.

Reproduction Steps:

  1. Call get_userid(NULL) - function allocates 12 bytes with malloc()
  2. Function doesn't provide clear ownership of allocated memory
  3. Call get_userid() with small buffer causes strcpy() without bounds checking
  4. Multiple calls create potential memory leaks without clear free() responsibility

Impact: Memory leaks during normal operation, potential buffer overflow from strcpy() without bounds checking.

Proposed Fix: Implement comprehensive memory safety:

char* get_userid(char *outname) {
    struct passwd *pwtemp = NULL;

    if ((pwtemp = getpwuid(getuid())) == NULL) {
        if (outname != NULL) {
            /* Use safe string copy with bounds checking */
            outname[0] = '\0';
        }
        return NULL;
    }

    if (outname == NULL) {
        /* Clear documentation: caller must free() */
        if ((outname = malloc(strlen(pwtemp->pw_name) + 1)) == NULL) {
            fprintf(stderr, "Memory allocation failure\n");
            return NULL;
        }
    }

    /* Use safe string copy */
    strncpy(outname, pwtemp->pw_name, 11);
    outname[11] = '\0';  /* Ensure null termination */
    return outname;
}

Original Bug ID: BUG-005

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmodernizationIssues related to C code modernization

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions