Skip to content

Commit

Permalink
utils: set HOME to root if the user not found
Browse files Browse the repository at this point in the history
if there is no HOME env variable set and the user is not found, then
set the home directory to '/'.

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Feb 16, 2021
1 parent d611a40 commit a6eb8a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/libcrun/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,8 @@ set_home_env (uid_t id)
if (stream == NULL)
{
if (errno == ENOENT)
return 0;
goto exit;

return -1;
}

Expand Down Expand Up @@ -1186,6 +1187,10 @@ set_home_env (uid_t id)
return 0;
}
}

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

Expand Down
13 changes: 13 additions & 0 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ def notify_server():
pass
return 0

def test_empty_home():
conf = base_config()
conf['process']['args'] = ['/sbin/init', 'printenv', 'HOME']
add_all_namespaces(conf)
try:
out, _ = run_and_get_output(conf)
if "/" not in str(out):
return -1
except Exception as e:
return -1
return 0

all_tests = {
"start" : test_start,
"start-override-config" : test_start_override_config,
Expand All @@ -228,6 +240,7 @@ def notify_server():
"test-cwd-relative": test_cwd_relative,
"test-cwd-relative-subdir": test_cwd_relative_subdir,
"test-cwd-absolute": test_cwd_absolute,
"empty-home": test_empty_home,
}

if __name__ == "__main__":
Expand Down

0 comments on commit a6eb8a8

Please sign in to comment.