2017-05-29 12:19:11 +00:00
|
|
|
# Verify that Linux builds cannot create setuid or setgid binaries.
|
|
|
|
|
2019-10-04 08:45:33 +00:00
|
|
|
{ nixpkgs, system, overlay }:
|
2017-05-29 12:19:11 +00:00
|
|
|
|
2020-10-17 20:08:18 +00:00
|
|
|
with import (nixpkgs + "/nixos/lib/testing-python.nix") {
|
2019-10-04 08:45:33 +00:00
|
|
|
inherit system;
|
|
|
|
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
|
|
|
|
};
|
2017-05-29 12:19:11 +00:00
|
|
|
|
|
|
|
makeTest {
|
|
|
|
|
|
|
|
machine =
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
{ virtualisation.writableStore = true;
|
2019-11-05 10:12:25 +00:00
|
|
|
nix.binaryCaches = lib.mkForce [ ];
|
2017-05-29 12:19:11 +00:00
|
|
|
nix.nixPath = [ "nixpkgs=${lib.cleanSource pkgs.path}" ];
|
|
|
|
virtualisation.pathsInNixDB = [ pkgs.stdenv pkgs.pkgsi686Linux.stdenv ];
|
|
|
|
};
|
|
|
|
|
2020-10-17 20:08:18 +00:00
|
|
|
testScript = { nodes }: ''
|
|
|
|
# fmt: off
|
|
|
|
start_all()
|
|
|
|
|
|
|
|
# Copying to /tmp should succeed.
|
|
|
|
machine.succeed(r"""
|
|
|
|
nix-build --no-sandbox -E '(with import <nixpkgs> {}; runCommand "foo" {} "
|
|
|
|
mkdir -p $out
|
|
|
|
cp ${pkgs.coreutils}/bin/id /tmp/id
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 555 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
|
|
|
|
# Creating a setuid binary should fail.
|
|
|
|
machine.fail(r"""
|
|
|
|
nix-build --no-sandbox -E '(with import <nixpkgs> {}; runCommand "foo" {} "
|
|
|
|
mkdir -p $out
|
|
|
|
cp ${pkgs.coreutils}/bin/id /tmp/id
|
|
|
|
chmod 4755 /tmp/id
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 555 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
|
|
|
|
# Creating a setgid binary should fail.
|
|
|
|
machine.fail(r"""
|
|
|
|
nix-build --no-sandbox -E '(with import <nixpkgs> {}; runCommand "foo" {} "
|
|
|
|
mkdir -p $out
|
|
|
|
cp ${pkgs.coreutils}/bin/id /tmp/id
|
|
|
|
chmod 2755 /tmp/id
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 555 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
|
|
|
|
# The checks should also work on 32-bit binaries.
|
|
|
|
machine.fail(r"""
|
|
|
|
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
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 555 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
|
|
|
|
# The tests above use fchmodat(). Test chmod() as well.
|
|
|
|
machine.succeed(r"""
|
|
|
|
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\"
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 666 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
|
|
|
|
machine.fail(r"""
|
|
|
|
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\"
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 555 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
|
|
|
|
# And test fchmod().
|
|
|
|
machine.succeed(r"""
|
|
|
|
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\"
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 1750 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
|
|
|
|
machine.fail(r"""
|
|
|
|
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\"
|
|
|
|
")'
|
|
|
|
""".strip())
|
|
|
|
|
|
|
|
machine.succeed('[[ $(stat -c %a /tmp/id) = 555 ]]')
|
|
|
|
|
|
|
|
machine.succeed("rm /tmp/id")
|
|
|
|
'';
|
2017-05-29 12:19:11 +00:00
|
|
|
}
|