1{ pkgs, ... }:
2let
3 userUid = 1000;
4 usersGid = 100;
5 busybox =
6 pkgs:
7 pkgs.busybox.override {
8 # Without this, the busybox binary drops euid to ruid for most applets, including id.
9 # See https://bugs.busybox.net/show_bug.cgi?id=15101
10 extraConfig = "CONFIG_FEATURE_SUID n";
11 };
12in
13{
14 name = "wrappers";
15
16 nodes.machine =
17 { config, pkgs, ... }:
18 {
19 ids.gids.users = usersGid;
20
21 users.users = {
22 regular = {
23 uid = userUid;
24 isNormalUser = true;
25 };
26 };
27
28 security.apparmor.enable = true;
29
30 security.wrappers = {
31 disabled = {
32 enable = false;
33 owner = "root";
34 group = "root";
35 setuid = true;
36 source = "${busybox pkgs}/bin/busybox";
37 program = "disabled_busybox";
38 };
39 suidRoot = {
40 owner = "root";
41 group = "root";
42 setuid = true;
43 source = "${busybox pkgs}/bin/busybox";
44 program = "suid_root_busybox";
45 };
46 sgidRoot = {
47 owner = "root";
48 group = "root";
49 setgid = true;
50 source = "${busybox pkgs}/bin/busybox";
51 program = "sgid_root_busybox";
52 };
53 withChown = {
54 owner = "root";
55 group = "root";
56 source = "${pkgs.libcap}/bin/capsh";
57 program = "capsh_with_chown";
58 capabilities = "cap_chown+ep";
59 };
60 };
61 };
62
63 testScript = ''
64 def cmd_as_regular(cmd):
65 return "su -l regular -c '{0}'".format(cmd)
66
67 def test_as_regular(cmd, expected):
68 out = machine.succeed(cmd_as_regular(cmd)).strip()
69 assert out == expected, "Expected {0} to output {1}, but got {2}".format(cmd, expected, out)
70
71 def test_as_regular_in_userns_mapped_as_root(cmd, expected):
72 out = machine.succeed(f"su -l regular -c '${pkgs.util-linux}/bin/unshare -rm {cmd}'").strip()
73 assert out == expected, "Expected {0} to output {1}, but got {2}".format(cmd, expected, out)
74
75 test_as_regular('${busybox pkgs}/bin/busybox id -u', '${toString userUid}')
76 test_as_regular('${busybox pkgs}/bin/busybox id -ru', '${toString userUid}')
77 test_as_regular('${busybox pkgs}/bin/busybox id -g', '${toString usersGid}')
78 test_as_regular('${busybox pkgs}/bin/busybox id -rg', '${toString usersGid}')
79
80 test_as_regular('/run/wrappers/bin/suid_root_busybox id -u', '0')
81 test_as_regular('/run/wrappers/bin/suid_root_busybox id -ru', '${toString userUid}')
82 test_as_regular('/run/wrappers/bin/suid_root_busybox id -g', '${toString usersGid}')
83 test_as_regular('/run/wrappers/bin/suid_root_busybox id -rg', '${toString usersGid}')
84
85 test_as_regular('/run/wrappers/bin/sgid_root_busybox id -u', '${toString userUid}')
86 test_as_regular('/run/wrappers/bin/sgid_root_busybox id -ru', '${toString userUid}')
87 test_as_regular('/run/wrappers/bin/sgid_root_busybox id -g', '0')
88 test_as_regular('/run/wrappers/bin/sgid_root_busybox id -rg', '${toString usersGid}')
89
90 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/suid_root_busybox id -u', '0')
91 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/suid_root_busybox id -ru', '0')
92 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/suid_root_busybox id -g', '0')
93 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/suid_root_busybox id -rg', '0')
94
95 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/sgid_root_busybox id -u', '0')
96 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/sgid_root_busybox id -ru', '0')
97 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/sgid_root_busybox id -g', '0')
98 test_as_regular_in_userns_mapped_as_root('/run/wrappers/bin/sgid_root_busybox id -rg', '0')
99
100 # Test that in nonewprivs environment the wrappers simply exec their target.
101 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/suid_root_busybox id -u', '${toString userUid}')
102 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/suid_root_busybox id -ru', '${toString userUid}')
103 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/suid_root_busybox id -g', '${toString usersGid}')
104 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/suid_root_busybox id -rg', '${toString usersGid}')
105
106 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/sgid_root_busybox id -u', '${toString userUid}')
107 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/sgid_root_busybox id -ru', '${toString userUid}')
108 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/sgid_root_busybox id -g', '${toString usersGid}')
109 test_as_regular('${pkgs.util-linux}/bin/setpriv --no-new-privs /run/wrappers/bin/sgid_root_busybox id -rg', '${toString usersGid}')
110
111 # We are only testing the permitted set, because it's easiest to look at with capsh.
112 machine.fail(cmd_as_regular('${pkgs.libcap}/bin/capsh --has-p=CAP_CHOWN'))
113 machine.fail(cmd_as_regular('${pkgs.libcap}/bin/capsh --has-p=CAP_SYS_ADMIN'))
114 machine.succeed(cmd_as_regular('/run/wrappers/bin/capsh_with_chown --has-p=CAP_CHOWN'))
115 machine.fail(cmd_as_regular('/run/wrappers/bin/capsh_with_chown --has-p=CAP_SYS_ADMIN'))
116
117 # Test that the only user of apparmor policy includes generated by
118 # wrappers works. Ideally this'd be located in a test for the module that
119 # actually makes the apparmor policy for ping, but there's no convenient
120 # test for that one.
121 machine.succeed("ping -c 1 127.0.0.1")
122
123 # Test that the disabled wrapper is not present.
124 machine.fail("test -e /run/wrappers/bin/disabled_busybox")
125 '';
126}