From a6eb8a83917543c6f862fef5b9875371b0f74095 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 16 Feb 2021 14:17:01 +0100 Subject: [PATCH] utils: set HOME to root if the user not found 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 --- src/libcrun/utils.c | 7 ++++++- tests/test_start.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/libcrun/utils.c b/src/libcrun/utils.c index 1143161456..9399136cec 100644 --- a/src/libcrun/utils.c +++ b/src/libcrun/utils.c @@ -1157,7 +1157,8 @@ set_home_env (uid_t id) if (stream == NULL) { if (errno == ENOENT) - return 0; + goto exit; + return -1; } @@ -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; } diff --git a/tests/test_start.py b/tests/test_start.py index 5ead50d42f..684366c922 100755 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -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, @@ -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__":