about summary refs log tree commit diff
path: root/third_party/nix/tests/setuid.nix
blob: 77e83c8d6c2c1300a0439eccb5ce01e37e7acabe (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Verify that Linux builds cannot create setuid or setgid binaries.

{ nixpkgs, system, nix }:

with import (nixpkgs + "/nixos/lib/testing.nix") { inherit system; };

makeTest {

  machine =
    { config, lib, pkgs, ... }:
    { virtualisation.writableStore = true;
      nix.package = nix;
      nix.binaryCaches = [ ];
      nix.nixPath = [ "nixpkgs=${lib.cleanSource pkgs.path}" ];
      virtualisation.pathsInNixDB = [ pkgs.stdenv pkgs.pkgsi686Linux.stdenv ];
    };

  testScript = { nodes }:
    ''
      startAll;

      # Copying to /tmp should succeed.
      $machine->succeed('nix-build --no-sandbox -E \'(with import <nixpkgs> {}; runCommand "foo" {} "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 555 ]]');

      $machine->succeed("rm /tmp/id");

      # Creating a setuid binary should fail.
      $machine->fail('nix-build --no-sandbox -E \'(with import <nixpkgs> {}; runCommand "foo" {} "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
        chmod 4755 /tmp/id
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 555 ]]');

      $machine->succeed("rm /tmp/id");

      # Creating a setgid binary should fail.
      $machine->fail('nix-build --no-sandbox -E \'(with import <nixpkgs> {}; runCommand "foo" {} "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
        chmod 2755 /tmp/id
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 555 ]]');

      $machine->succeed("rm /tmp/id");

      # The checks should also work on 32-bit binaries.
      $machine->fail('nix-build --no-sandbox -E \'(with import <nixpkgs> { system = "i686-linux"; }; runCommand "foo" {} "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
        chmod 2755 /tmp/id
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 555 ]]');

      $machine->succeed("rm /tmp/id");

      # The tests above use fchmodat(). Test chmod() as well.
      $machine->succeed('nix-build --no-sandbox -E \'(with import <nixpkgs> {}; runCommand "foo" { buildInputs = [ perl ]; } "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
        perl -e \"chmod 0666, qw(/tmp/id) or die\"
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 666 ]]');

      $machine->succeed("rm /tmp/id");

      $machine->fail('nix-build --no-sandbox -E \'(with import <nixpkgs> {}; runCommand "foo" { buildInputs = [ perl ]; } "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
        perl -e \"chmod 04755, qw(/tmp/id) or die\"
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 555 ]]');

      $machine->succeed("rm /tmp/id");

      # And test fchmod().
      $machine->succeed('nix-build --no-sandbox -E \'(with import <nixpkgs> {}; runCommand "foo" { buildInputs = [ perl ]; } "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
        perl -e \"my \\\$x; open \\\$x, qw(/tmp/id); chmod 01750, \\\$x or die\"
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 1750 ]]');

      $machine->succeed("rm /tmp/id");

      $machine->fail('nix-build --no-sandbox -E \'(with import <nixpkgs> {}; runCommand "foo" { buildInputs = [ perl ]; } "
        mkdir -p $out
        cp ${pkgs.coreutils}/bin/id /tmp/id
        perl -e \"my \\\$x; open \\\$x, qw(/tmp/id); chmod 04777, \\\$x or die\"
      ")\' ');

      $machine->succeed('[[ $(stat -c %a /tmp/id) = 555 ]]');

      $machine->succeed("rm /tmp/id");
    '';

}