about summary refs log tree commit diff
path: root/nixos/tests/wrappers.nix
blob: 08c1ad0b6b999e967d2fad40be5c85179c5eca7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import ./make-test-python.nix ({ pkgs, ... }:
let
  userUid = 1000;
  usersGid = 100;
  busybox = pkgs : pkgs.busybox.override {
    # Without this, the busybox binary drops euid to ruid for most applets, including id.
    # See https://bugs.busybox.net/show_bug.cgi?id=15101
    extraConfig = "CONFIG_FEATURE_SUID n";
  };
in
{
  name = "wrappers";

  nodes.machine = { config, pkgs, ... }: {
    ids.gids.users = usersGid;

    users.users = {
      regular = {
        uid = userUid;
        isNormalUser = true;
      };
    };

    security.wrappers = {
      suidRoot = {
        owner = "root";
        group = "root";
        setuid = true;
        source = "${busybox pkgs}/bin/busybox";
        program = "suid_root_busybox";
      };
      sgidRoot = {
        owner = "root";
        group = "root";
        setgid = true;
        source = "${busybox pkgs}/bin/busybox";
        program = "sgid_root_busybox";
      };
      withChown = {
        owner = "root";
        group = "root";
        source = "${pkgs.libcap}/bin/capsh";
        program = "capsh_with_chown";
        capabilities = "cap_chown+ep";
      };
    };
  };

  testScript =
    ''
      def cmd_as_regular(cmd):
        return "su -l regular -c '{0}'".format(cmd)

      def test_as_regular(cmd, expected):
        out = machine.succeed(cmd_as_regular(cmd)).strip()
        assert out == expected, "Expected {0} to output {1}, but got {2}".format(cmd, expected, out)

      test_as_regular('${busybox pkgs}/bin/busybox id -u', '${toString userUid}')
      test_as_regular('${busybox pkgs}/bin/busybox id -ru', '${toString userUid}')
      test_as_regular('${busybox pkgs}/bin/busybox id -g', '${toString usersGid}')
      test_as_regular('${busybox pkgs}/bin/busybox id -rg', '${toString usersGid}')

      test_as_regular('/run/wrappers/bin/suid_root_busybox id -u', '0')
      test_as_regular('/run/wrappers/bin/suid_root_busybox id -ru', '${toString userUid}')
      test_as_regular('/run/wrappers/bin/suid_root_busybox id -g', '${toString usersGid}')
      test_as_regular('/run/wrappers/bin/suid_root_busybox id -rg', '${toString usersGid}')

      test_as_regular('/run/wrappers/bin/sgid_root_busybox id -u', '${toString userUid}')
      test_as_regular('/run/wrappers/bin/sgid_root_busybox id -ru', '${toString userUid}')
      test_as_regular('/run/wrappers/bin/sgid_root_busybox id -g', '0')
      test_as_regular('/run/wrappers/bin/sgid_root_busybox id -rg', '${toString usersGid}')

      # We are only testing the permitted set, because it's easiest to look at with capsh.
      machine.fail(cmd_as_regular('${pkgs.libcap}/bin/capsh --has-p=CAP_CHOWN'))
      machine.fail(cmd_as_regular('${pkgs.libcap}/bin/capsh --has-p=CAP_SYS_ADMIN'))
      machine.succeed(cmd_as_regular('/run/wrappers/bin/capsh_with_chown --has-p=CAP_CHOWN'))
      machine.fail(cmd_as_regular('/run/wrappers/bin/capsh_with_chown --has-p=CAP_SYS_ADMIN'))
    '';
})