2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
|
|
|
|
inherit (config.security) wrapperDir wrappers;
|
|
|
|
|
|
|
|
parentWrapperDir = dirOf wrapperDir;
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
# This is security-sensitive code, and glibc vulns happen from time to time.
|
|
|
|
# musl is security-focused and generally more minimal, so it's a better choice here.
|
|
|
|
# The dynamic linker is still a fairly complex piece of code, and the wrappers are
|
|
|
|
# quite small, so linking it statically is more appropriate.
|
|
|
|
securityWrapper = sourceProg : pkgs.pkgsStatic.callPackage ./wrapper.nix {
|
|
|
|
inherit sourceProg;
|
|
|
|
|
|
|
|
# glibc definitions of insecure environment variables
|
|
|
|
#
|
|
|
|
# We extract the single header file we need into its own derivation,
|
|
|
|
# so that we don't have to pull full glibc sources to build wrappers.
|
|
|
|
#
|
|
|
|
# They're taken from pkgs.glibc so that we don't have to keep as close
|
|
|
|
# an eye on glibc changes. Not every relevant variable is in this header,
|
|
|
|
# so we maintain a slightly stricter list in wrapper.c itself as well.
|
|
|
|
unsecvars = lib.overrideDerivation (pkgs.srcOnly pkgs.glibc)
|
|
|
|
({ name, ... }: {
|
|
|
|
name = "${name}-unsecvars";
|
|
|
|
installPhase = ''
|
|
|
|
mkdir $out
|
|
|
|
cp sysdeps/generic/unsecvars.h $out
|
|
|
|
'';
|
|
|
|
});
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
fileModeType =
|
|
|
|
let
|
|
|
|
# taken from the chmod(1) man page
|
|
|
|
symbolic = "[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+";
|
|
|
|
numeric = "[-+=]?[0-7]{0,4}";
|
|
|
|
mode = "((${symbolic})(,${symbolic})*)|(${numeric})";
|
|
|
|
in
|
|
|
|
lib.types.strMatching mode
|
|
|
|
// { description = "file mode string"; };
|
|
|
|
|
|
|
|
wrapperType = lib.types.submodule ({ name, config, ... }: {
|
|
|
|
options.source = lib.mkOption
|
|
|
|
{ type = lib.types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The absolute path to the program to be wrapped.";
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
options.program = lib.mkOption
|
|
|
|
{ type = with lib.types; nullOr str;
|
|
|
|
default = name;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-09-22 15:38:15 +00:00
|
|
|
The name of the wrapper program. Defaults to the attribute name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.owner = lib.mkOption
|
|
|
|
{ type = lib.types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The owner of the wrapper program.";
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
options.group = lib.mkOption
|
|
|
|
{ type = lib.types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The group of the wrapper program.";
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
options.permissions = lib.mkOption
|
|
|
|
{ type = fileModeType;
|
|
|
|
default = "u+rx,g+x,o+x";
|
|
|
|
example = "a+rx";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-09-22 15:38:15 +00:00
|
|
|
The permissions of the wrapper program. The format is that of a
|
2022-08-12 12:06:08 +00:00
|
|
|
symbolic or numeric file mode understood by {command}`chmod`.
|
2021-09-22 15:38:15 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.capabilities = lib.mkOption
|
|
|
|
{ type = lib.types.commas;
|
|
|
|
default = "";
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-09-11 15:47:08 +00:00
|
|
|
A comma-separated list of capability clauses to be given to the
|
|
|
|
wrapper program. The format for capability clauses is described in the
|
|
|
|
“TEXTUAL REPRESENTATION” section of the {manpage}`cap_from_text(3)`
|
|
|
|
manual page. For a list of capabilities supported by the system, check
|
|
|
|
the {manpage}`capabilities(7)` manual page.
|
2021-09-22 15:38:15 +00:00
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
::: {.note}
|
|
|
|
`cap_setpcap`, which is required for the wrapper
|
|
|
|
program to be able to raise caps into the Ambient set is NOT raised
|
|
|
|
to the Ambient set so that the real program cannot modify its own
|
|
|
|
capabilities!! This may be too restrictive for cases in which the
|
|
|
|
real program needs cap_setpcap but it at least leans on the side
|
|
|
|
security paranoid vs. too relaxed.
|
|
|
|
:::
|
2021-09-22 15:38:15 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.setuid = lib.mkOption
|
|
|
|
{ type = lib.types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether to add the setuid bit the wrapper program.";
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
options.setgid = lib.mkOption
|
|
|
|
{ type = lib.types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether to add the setgid bit the wrapper program.";
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
###### Activation script for the setcap wrappers
|
|
|
|
mkSetcapProgram =
|
|
|
|
{ program
|
|
|
|
, capabilities
|
|
|
|
, source
|
2021-09-22 15:38:15 +00:00
|
|
|
, owner
|
|
|
|
, group
|
|
|
|
, permissions
|
2020-04-24 23:36:52 +00:00
|
|
|
, ...
|
|
|
|
}:
|
|
|
|
''
|
2023-10-09 19:29:22 +00:00
|
|
|
cp ${securityWrapper source}/bin/security-wrapper "$wrapperDir/${program}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Prevent races
|
2021-09-22 15:38:15 +00:00
|
|
|
chmod 0000 "$wrapperDir/${program}"
|
2022-05-18 14:49:53 +00:00
|
|
|
chown ${owner}:${group} "$wrapperDir/${program}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Set desired capabilities on the file plus cap_setpcap so
|
|
|
|
# the wrapper program can elevate the capabilities set on
|
|
|
|
# its file into the Ambient set.
|
2021-09-22 15:38:15 +00:00
|
|
|
${pkgs.libcap.out}/bin/setcap "cap_setpcap,${capabilities}" "$wrapperDir/${program}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Set the executable bit
|
2021-09-22 15:38:15 +00:00
|
|
|
chmod ${permissions} "$wrapperDir/${program}"
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
###### Activation script for the setuid wrappers
|
|
|
|
mkSetuidProgram =
|
|
|
|
{ program
|
|
|
|
, source
|
2021-09-22 15:38:15 +00:00
|
|
|
, owner
|
|
|
|
, group
|
|
|
|
, setuid
|
|
|
|
, setgid
|
|
|
|
, permissions
|
2020-04-24 23:36:52 +00:00
|
|
|
, ...
|
|
|
|
}:
|
|
|
|
''
|
2023-10-09 19:29:22 +00:00
|
|
|
cp ${securityWrapper source}/bin/security-wrapper "$wrapperDir/${program}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Prevent races
|
2021-09-22 15:38:15 +00:00
|
|
|
chmod 0000 "$wrapperDir/${program}"
|
2022-05-18 14:49:53 +00:00
|
|
|
chown ${owner}:${group} "$wrapperDir/${program}"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
chmod "u${if setuid then "+" else "-"}s,g${if setgid then "+" else "-"}s,${permissions}" "$wrapperDir/${program}"
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
mkWrappedPrograms =
|
|
|
|
builtins.map
|
2021-09-22 15:38:15 +00:00
|
|
|
(opts:
|
|
|
|
if opts.capabilities != ""
|
|
|
|
then mkSetcapProgram opts
|
|
|
|
else mkSetuidProgram opts
|
|
|
|
) (lib.attrValues wrappers);
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [ "security" "setuidOwners" ] "Use security.wrappers instead")
|
|
|
|
(lib.mkRemovedOptionModule [ "security" "setuidPrograms" ] "Use security.wrappers instead")
|
|
|
|
];
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
security.wrappers = lib.mkOption {
|
2021-09-22 15:38:15 +00:00
|
|
|
type = lib.types.attrsOf wrapperType;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = {};
|
2021-10-06 13:57:05 +00:00
|
|
|
example = lib.literalExpression
|
2020-04-24 23:36:52 +00:00
|
|
|
''
|
2021-09-22 15:38:15 +00:00
|
|
|
{
|
|
|
|
# a setuid root program
|
|
|
|
doas =
|
|
|
|
{ setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
source = "''${pkgs.doas}/bin/doas";
|
|
|
|
};
|
|
|
|
|
|
|
|
# a setgid program
|
|
|
|
locate =
|
|
|
|
{ setgid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "mlocate";
|
|
|
|
source = "''${pkgs.locate}/bin/locate";
|
|
|
|
};
|
|
|
|
|
|
|
|
# a program with the CAP_NET_RAW capability
|
|
|
|
ping =
|
|
|
|
{ owner = "root";
|
|
|
|
group = "root";
|
|
|
|
capabilities = "cap_net_raw+ep";
|
|
|
|
source = "''${pkgs.iputils.out}/bin/ping";
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-09-22 15:38:15 +00:00
|
|
|
This option effectively allows adding setuid/setgid bits, capabilities,
|
|
|
|
changing file ownership and permissions of a program without directly
|
|
|
|
modifying it. This works by creating a wrapper program under the
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`security.wrapperDir` directory, which is then added to
|
|
|
|
the shell `PATH`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
security.wrapperDirSize = lib.mkOption {
|
|
|
|
default = "50%";
|
|
|
|
example = "10G";
|
|
|
|
type = lib.types.str;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-08-21 13:32:41 +00:00
|
|
|
Size limit for the /run/wrappers tmpfs. Look at mount(8), tmpfs size option,
|
|
|
|
for the accepted syntax. WARNING: don't set to less than 64MB.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
security.wrapperDir = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
|
|
|
default = "/run/wrappers/bin";
|
|
|
|
internal = true;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
This option defines the path to the wrapper programs. It
|
2022-12-28 21:21:41 +00:00
|
|
|
should not be overridden.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = {
|
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
assertions = lib.mapAttrsToList
|
|
|
|
(name: opts:
|
|
|
|
{ assertion = opts.setuid || opts.setgid -> opts.capabilities == "";
|
|
|
|
message = ''
|
|
|
|
The security.wrappers.${name} wrapper is not valid:
|
|
|
|
setuid/setgid and capabilities are mutually exclusive.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
) wrappers;
|
|
|
|
|
|
|
|
security.wrappers =
|
|
|
|
let
|
|
|
|
mkSetuidRoot = source:
|
|
|
|
{ setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
inherit source;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{ # These are mount related wrappers that require the +s permission.
|
|
|
|
fusermount = mkSetuidRoot "${pkgs.fuse}/bin/fusermount";
|
|
|
|
fusermount3 = mkSetuidRoot "${pkgs.fuse3}/bin/fusermount3";
|
|
|
|
mount = mkSetuidRoot "${lib.getBin pkgs.util-linux}/bin/mount";
|
|
|
|
umount = mkSetuidRoot "${lib.getBin pkgs.util-linux}/bin/umount";
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
boot.specialFileSystems.${parentWrapperDir} = {
|
|
|
|
fsType = "tmpfs";
|
2022-08-21 13:32:41 +00:00
|
|
|
options = [ "nodev" "mode=755" "size=${config.security.wrapperDirSize}" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Make sure our wrapperDir exports to the PATH env variable when
|
|
|
|
# initializing the shell
|
|
|
|
environment.extraInit = ''
|
|
|
|
# Wrappers override other bin directories.
|
|
|
|
export PATH="${wrapperDir}:$PATH"
|
|
|
|
'';
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
security.apparmor.includes = lib.mapAttrs' (wrapName: wrap: lib.nameValuePair
|
|
|
|
"nixos/security.wrappers/${wrapName}" ''
|
|
|
|
include "${pkgs.apparmorRulesFromClosure { name="security.wrappers.${wrapName}"; } [
|
|
|
|
(securityWrapper wrap.source)
|
2021-05-20 23:08:51 +00:00
|
|
|
]}"
|
2023-10-09 19:29:22 +00:00
|
|
|
mrpx ${wrap.source},
|
|
|
|
'') wrappers;
|
2021-05-20 23:08:51 +00:00
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
systemd.services.suid-sgid-wrappers = {
|
|
|
|
description = "Create SUID/SGID Wrappers";
|
|
|
|
wantedBy = [ "sysinit.target" ];
|
|
|
|
before = [ "sysinit.target" ];
|
|
|
|
unitConfig.DefaultDependencies = false;
|
|
|
|
unitConfig.RequiresMountsFor = [ "/nix/store" "/run/wrappers" ];
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script = ''
|
|
|
|
chmod 755 "${parentWrapperDir}"
|
|
|
|
|
|
|
|
# We want to place the tmpdirs for the wrappers to the parent dir.
|
|
|
|
wrapperDir=$(mktemp --directory --tmpdir="${parentWrapperDir}" wrappers.XXXXXXXXXX)
|
|
|
|
chmod a+rx "$wrapperDir"
|
|
|
|
|
|
|
|
${lib.concatStringsSep "\n" mkWrappedPrograms}
|
|
|
|
|
|
|
|
if [ -L ${wrapperDir} ]; then
|
|
|
|
# Atomically replace the symlink
|
|
|
|
# See https://axialcorps.com/2013/07/03/atomically-replacing-files-and-directories/
|
|
|
|
old=$(readlink -f ${wrapperDir})
|
|
|
|
if [ -e "${wrapperDir}-tmp" ]; then
|
|
|
|
rm --force --recursive "${wrapperDir}-tmp"
|
2020-04-24 23:36:52 +00:00
|
|
|
fi
|
2023-11-16 04:20:00 +00:00
|
|
|
ln --symbolic --force --no-dereference "$wrapperDir" "${wrapperDir}-tmp"
|
|
|
|
mv --no-target-directory "${wrapperDir}-tmp" "${wrapperDir}"
|
|
|
|
rm --force --recursive "$old"
|
|
|
|
else
|
|
|
|
# For initial setup
|
|
|
|
ln --symbolic "$wrapperDir" "${wrapperDir}"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
2021-09-22 15:38:15 +00:00
|
|
|
|
|
|
|
###### wrappers consistency checks
|
2023-05-24 13:37:59 +00:00
|
|
|
system.checks = lib.singleton (pkgs.runCommandLocal
|
2021-09-22 15:38:15 +00:00
|
|
|
"ensure-all-wrappers-paths-exist" { }
|
|
|
|
''
|
|
|
|
# make sure we produce output
|
|
|
|
mkdir -p $out
|
|
|
|
|
|
|
|
echo -n "Checking that Nix store paths of all wrapped programs exist... "
|
|
|
|
|
|
|
|
declare -A wrappers
|
|
|
|
${lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v:
|
|
|
|
"wrappers['${n}']='${v.source}'") wrappers)}
|
|
|
|
|
|
|
|
for name in "''${!wrappers[@]}"; do
|
|
|
|
path="''${wrappers[$name]}"
|
|
|
|
if [[ "$path" =~ /nix/store ]] && [ ! -e "$path" ]; then
|
|
|
|
test -t 1 && echo -ne '\033[1;31m'
|
|
|
|
echo "FAIL"
|
|
|
|
echo "The path $path does not exist!"
|
|
|
|
echo 'Please, check the value of `security.wrappers."'$name'".source`.'
|
|
|
|
test -t 1 && echo -ne '\033[0m'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "OK"
|
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|