Skip to content

Commit 08df5ec

Browse files
authored
Merge pull request #1701 from giuseppe/set-home-unknown-id
utils: set_home_env returns negative value on errors
2 parents a197cd6 + 3adcc2c commit 08df5ec

File tree

3 files changed

+72
-56
lines changed

3 files changed

+72
-56
lines changed

src/libcrun/utils.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ set_home_env (uid_t id)
14631463

14641464
error:
14651465
/* Let callers handle the error if the user was not found. */
1466-
return ret;
1466+
return ret ? -errno : 0;
14671467
}
14681468

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

tests/test_cwd.py

+55-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,63 @@ def test_cwd():
2626
if "/var" not in out:
2727
return -1
2828
return 0
29-
29+
30+
def test_cwd_relative():
31+
conf = base_config()
32+
conf['process']['args'] = ['./init', 'echo', 'hello']
33+
conf['process']['cwd'] = "/sbin"
34+
add_all_namespaces(conf)
35+
try:
36+
out, _ = run_and_get_output(conf)
37+
if "hello" not in str(out):
38+
return -1
39+
except Exception as e:
40+
return -1
41+
return 0
42+
43+
def test_cwd_relative_subdir():
44+
conf = base_config()
45+
conf['process']['args'] = ['sbin/init', 'echo', 'hello']
46+
conf['process']['cwd'] = "/"
47+
add_all_namespaces(conf)
48+
try:
49+
out, _ = run_and_get_output(conf)
50+
if "hello" not in str(out):
51+
return -1
52+
except:
53+
return -1
54+
return 0
55+
56+
def test_cwd_not_exist():
57+
conf = base_config()
58+
conf['process']['args'] = ['/init', 'true']
59+
conf['process']['cwd'] = "/doesnotexist"
60+
add_all_namespaces(conf)
61+
try:
62+
run_and_get_output(conf)
63+
except:
64+
return -1
65+
return 0
66+
67+
def test_cwd_absolute():
68+
conf = base_config()
69+
conf['process']['args'] = ['/init', 'echo', 'hello']
70+
conf['process']['cwd'] = "/sbin"
71+
add_all_namespaces(conf)
72+
try:
73+
out, _ = run_and_get_output(conf)
74+
if "hello" not in str(out):
75+
return -1
76+
except:
77+
return -1
78+
return 0
79+
3080
all_tests = {
3181
"cwd" : test_cwd,
82+
"cwd-relative": test_cwd_relative,
83+
"cwd-relative-subdir": test_cwd_relative_subdir,
84+
"cwd-absolute": test_cwd_absolute,
85+
"cwd-not-exist" : test_cwd_not_exist,
3286
}
3387

3488
if __name__ == "__main__":

tests/test_start.py

+16-54
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,6 @@
2424
import json
2525
from tests_utils import *
2626

27-
def test_cwd_relative():
28-
conf = base_config()
29-
conf['process']['args'] = ['./init', 'echo', 'hello']
30-
conf['process']['cwd'] = "/sbin"
31-
add_all_namespaces(conf)
32-
try:
33-
out, _ = run_and_get_output(conf)
34-
if "hello" not in str(out):
35-
return -1
36-
except Exception as e:
37-
return -1
38-
return 0
39-
40-
def test_cwd_relative_subdir():
41-
conf = base_config()
42-
conf['process']['args'] = ['sbin/init', 'echo', 'hello']
43-
conf['process']['cwd'] = "/"
44-
add_all_namespaces(conf)
45-
try:
46-
out, _ = run_and_get_output(conf)
47-
if "hello" not in str(out):
48-
return -1
49-
except:
50-
return -1
51-
return 0
52-
53-
def test_cwd_not_exist():
54-
conf = base_config()
55-
conf['process']['args'] = ['/init', 'true']
56-
conf['process']['cwd'] = "/doesnotexist"
57-
add_all_namespaces(conf)
58-
try:
59-
run_and_get_output(conf)
60-
except:
61-
return -1
62-
return 0
63-
64-
def test_cwd_absolute():
65-
conf = base_config()
66-
conf['process']['args'] = ['/init', 'echo', 'hello']
67-
conf['process']['cwd'] = "/sbin"
68-
add_all_namespaces(conf)
69-
try:
70-
out, _ = run_and_get_output(conf)
71-
if "hello" not in str(out):
72-
return -1
73-
except:
74-
return -1
75-
return 0
76-
7727
def test_not_allowed_ipc_sysctl():
7828
if is_rootless():
7929
return 77
@@ -556,6 +506,21 @@ def test_invalid_id():
556506
return -1
557507
return 0
558508

509+
def test_home_unknown_id():
510+
if is_rootless():
511+
return 77
512+
513+
conf = base_config()
514+
conf['process']['args'] = ['/init', 'printenv', "HOME"]
515+
conf['process']['user']['uid'] = 101010
516+
conf['process']['user']['gid'] = 101010
517+
add_all_namespaces(conf)
518+
out, _ = run_and_get_output(conf)
519+
if out != "/":
520+
sys.stderr.write("expected: `/`, got output: `%s`\n" % out)
521+
return -1
522+
return 0
523+
559524
all_tests = {
560525
"start" : test_start,
561526
"start-override-config" : test_start_override_config,
@@ -565,10 +530,6 @@ def test_invalid_id():
565530
"sd-notify-env" : test_sd_notify_env,
566531
"sd-notify-proxy": test_sd_notify_proxy,
567532
"listen_pid_env": test_listen_pid_env,
568-
"cwd-relative": test_cwd_relative,
569-
"cwd-relative-subdir": test_cwd_relative_subdir,
570-
"cwd-absolute": test_cwd_absolute,
571-
"cwd-not-exist" : test_cwd_not_exist,
572533
"empty-home": test_empty_home,
573534
"delete-in-created-state": test_delete_in_created_state,
574535
"run-rootless-netns-with-userns" : test_run_rootless_netns_with_userns,
@@ -579,6 +540,7 @@ def test_invalid_id():
579540
"ioprio": test_ioprio,
580541
"run-keep": test_run_keep,
581542
"invalid-id": test_invalid_id,
543+
"home-unknown-id": test_home_unknown_id,
582544
}
583545

584546
if __name__ == "__main__":

0 commit comments

Comments
 (0)