Skip to content

Commit

Permalink
Merge pull request #1633 from kwilczynski/fix/handle-mssing-user-when…
Browse files Browse the repository at this point in the history
…-setting-HOME-environment

utils: return error from set_home_env() if the user was not found
  • Loading branch information
giuseppe authored Jan 8, 2025
2 parents 0b63b44 + 25efd10 commit fb1010a
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/libcrun/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ set_home_env (uid_t id)
cleanup_free char *buf = NULL;
long buf_size;
cleanup_file FILE *stream = NULL;
int ret = -1;

buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
if (buf_size < 0)
Expand All @@ -1457,26 +1458,17 @@ set_home_env (uid_t id)

stream = fopen ("/etc/passwd", "re");
if (stream == NULL)
{
if (errno == ENOENT)
goto exit;

return -1;
}
goto error;

for (;;)
{
int ret;
struct passwd *ret_pw = NULL;

ret = fgetpwent_r (stream, &pwd, buf, buf_size, &ret_pw);
if (UNLIKELY (ret != 0))
{
if (errno == ENOENT)
return 0;

if (errno != ERANGE)
return ret;
goto error;

buf_size *= 2;
buf = xrealloc (buf, buf_size);
Expand All @@ -1490,10 +1482,9 @@ set_home_env (uid_t id)
}
}

exit:
/* If the user was not found, set it to something reasonable. */
setenv ("HOME", "/", 1);
return 0;
error:
/* Let callers handle the error if the user was not found. */
return ret;
}

/*if subuid or subgid exist, take the first range for the user */
Expand Down

0 comments on commit fb1010a

Please sign in to comment.