2017-01-29 04:48:03 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
|
2017-02-14 21:20:27 +00:00
|
|
|
inherit (config.security) wrapperDir wrappers;
|
2017-01-29 07:27:11 +00:00
|
|
|
|
2017-02-18 17:06:09 +00:00
|
|
|
parentWrapperDir = dirOf wrapperDir;
|
|
|
|
|
2021-01-14 07:24:27 +00:00
|
|
|
securityWrapper = pkgs.callPackage ./wrapper.nix {
|
|
|
|
inherit parentWrapperDir;
|
2017-02-14 00:03:06 +00:00
|
|
|
};
|
2017-01-29 04:48:03 +00:00
|
|
|
|
2021-06-09 00:41:26 +01: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;
|
|
|
|
description = "The absolute path to the program to be wrapped.";
|
|
|
|
};
|
|
|
|
options.program = lib.mkOption
|
|
|
|
{ type = with lib.types; nullOr str;
|
|
|
|
default = name;
|
|
|
|
description = ''
|
|
|
|
The name of the wrapper program. Defaults to the attribute name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.owner = lib.mkOption
|
|
|
|
{ type = lib.types.str;
|
2021-06-09 18:59:39 +01:00
|
|
|
description = "The owner of the wrapper program.";
|
2021-06-09 00:41:26 +01:00
|
|
|
};
|
|
|
|
options.group = lib.mkOption
|
|
|
|
{ type = lib.types.str;
|
2021-06-09 18:59:39 +01:00
|
|
|
description = "The group of the wrapper program.";
|
2021-06-09 00:41:26 +01:00
|
|
|
};
|
|
|
|
options.permissions = lib.mkOption
|
2021-06-09 18:59:39 +01:00
|
|
|
{ type = fileModeType;
|
|
|
|
default = "u+rx,g+x,o+x";
|
|
|
|
example = "a+rx";
|
2021-06-09 00:41:26 +01:00
|
|
|
description = ''
|
|
|
|
The permissions of the wrapper program. The format is that of a
|
|
|
|
symbolic or numeric file mode understood by <command>chmod</command>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.capabilities = lib.mkOption
|
|
|
|
{ type = lib.types.commas;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
A comma-separated list of capabilities to be given to the wrapper
|
|
|
|
program. For capabilities supported by the system check the
|
|
|
|
<citerefentry>
|
|
|
|
<refentrytitle>capabilities</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum>
|
|
|
|
</citerefentry>
|
|
|
|
manual page.
|
|
|
|
|
|
|
|
<note><para>
|
|
|
|
<literal>cap_setpcap</literal>, 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.
|
|
|
|
</para></note>
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.setuid = lib.mkOption
|
|
|
|
{ type = lib.types.bool;
|
2021-09-12 17:53:48 +01:00
|
|
|
default = false;
|
2021-06-09 00:41:26 +01:00
|
|
|
description = "Whether to add the setuid bit the wrapper program.";
|
|
|
|
};
|
|
|
|
options.setgid = lib.mkOption
|
|
|
|
{ type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Whether to add the setgid bit the wrapper program.";
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-01-29 04:48:03 +00:00
|
|
|
###### Activation script for the setcap wrappers
|
|
|
|
mkSetcapProgram =
|
|
|
|
{ program
|
|
|
|
, capabilities
|
2017-02-14 00:28:13 +00:00
|
|
|
, source
|
2021-06-09 00:41:26 +01:00
|
|
|
, owner
|
|
|
|
, group
|
|
|
|
, permissions
|
2017-01-29 07:20:02 +00:00
|
|
|
, ...
|
2017-01-30 18:26:56 +00:00
|
|
|
}:
|
2017-01-29 07:07:12 +00:00
|
|
|
assert (lib.versionAtLeast (lib.getVersion config.boot.kernelPackages.kernel) "4.3");
|
2017-02-14 00:03:06 +00:00
|
|
|
''
|
2021-09-12 15:14:40 +01:00
|
|
|
cp ${securityWrapper}/bin/security-wrapper "$wrapperDir/${program}"
|
|
|
|
echo -n "${source}" > "$wrapperDir/${program}.real"
|
2017-01-29 04:48:03 +00:00
|
|
|
|
|
|
|
# Prevent races
|
2021-09-12 15:14:40 +01:00
|
|
|
chmod 0000 "$wrapperDir/${program}"
|
|
|
|
chown ${owner}.${group} "$wrapperDir/${program}"
|
2017-01-29 04:48:03 +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-12 15:14:40 +01:00
|
|
|
${pkgs.libcap.out}/bin/setcap "cap_setpcap,${capabilities}" "$wrapperDir/${program}"
|
2017-01-29 04:48:03 +00:00
|
|
|
|
|
|
|
# Set the executable bit
|
2021-09-12 15:14:40 +01:00
|
|
|
chmod ${permissions} "$wrapperDir/${program}"
|
2017-01-29 04:48:03 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
###### Activation script for the setuid wrappers
|
|
|
|
mkSetuidProgram =
|
|
|
|
{ program
|
2017-02-14 00:28:13 +00:00
|
|
|
, source
|
2021-06-09 00:41:26 +01:00
|
|
|
, owner
|
|
|
|
, group
|
|
|
|
, setuid
|
|
|
|
, setgid
|
|
|
|
, permissions
|
2017-01-29 07:20:02 +00:00
|
|
|
, ...
|
2017-01-30 18:26:56 +00:00
|
|
|
}:
|
2017-02-14 00:03:06 +00:00
|
|
|
''
|
2021-09-12 15:14:40 +01:00
|
|
|
cp ${securityWrapper}/bin/security-wrapper "$wrapperDir/${program}"
|
|
|
|
echo -n "${source}" > "$wrapperDir/${program}.real"
|
2017-01-29 04:48:03 +00:00
|
|
|
|
|
|
|
# Prevent races
|
2021-09-12 15:14:40 +01:00
|
|
|
chmod 0000 "$wrapperDir/${program}"
|
|
|
|
chown ${owner}.${group} "$wrapperDir/${program}"
|
2017-01-29 04:48:03 +00:00
|
|
|
|
2021-09-12 15:14:40 +01:00
|
|
|
chmod "u${if setuid then "+" else "-"}s,g${if setgid then "+" else "-"}s,${permissions}" "$wrapperDir/${program}"
|
2017-01-29 04:48:03 +00:00
|
|
|
'';
|
2017-01-29 07:27:11 +00:00
|
|
|
|
|
|
|
mkWrappedPrograms =
|
|
|
|
builtins.map
|
2021-06-09 00:41:26 +01:00
|
|
|
(opts:
|
|
|
|
if opts.capabilities != ""
|
2021-06-09 18:59:39 +01:00
|
|
|
then mkSetcapProgram opts
|
|
|
|
else mkSetuidProgram opts
|
2021-06-09 00:41:26 +01:00
|
|
|
) (lib.attrValues wrappers);
|
2017-01-29 04:48:03 +00:00
|
|
|
in
|
|
|
|
{
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [ "security" "setuidOwners" ] "Use security.wrappers instead")
|
|
|
|
(lib.mkRemovedOptionModule [ "security" "setuidPrograms" ] "Use security.wrappers instead")
|
|
|
|
];
|
2017-01-29 04:48:03 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
security.wrappers = lib.mkOption {
|
2021-06-09 00:41:26 +01:00
|
|
|
type = lib.types.attrsOf wrapperType;
|
2017-01-29 04:48:03 +00:00
|
|
|
default = {};
|
2021-10-03 17:06:03 +01:00
|
|
|
example = lib.literalExpression
|
2017-02-15 08:07:16 +00:00
|
|
|
''
|
2021-09-12 17:53:48 +01: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";
|
|
|
|
};
|
2017-02-15 08:07:16 +00:00
|
|
|
}
|
|
|
|
'';
|
2017-01-29 04:48:03 +00:00
|
|
|
description = ''
|
2021-06-09 00:41:26 +01: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
|
|
|
|
<option>security.wrapperDir</option> directory, which is then added to
|
|
|
|
the shell <literal>PATH</literal>.
|
2017-01-29 04:48:03 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
security.wrapperDir = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2017-01-29 10:11:01 +00:00
|
|
|
default = "/run/wrappers/bin";
|
2017-01-29 04:48:03 +00:00
|
|
|
internal = true;
|
|
|
|
description = ''
|
|
|
|
This option defines the path to the wrapper programs. It
|
|
|
|
should not be overriden.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = {
|
2017-01-29 23:10:32 +00:00
|
|
|
|
2021-06-09 00:41:26 +01: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;
|
|
|
|
|
2021-09-12 17:53:48 +01:00
|
|
|
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";
|
|
|
|
};
|
2017-01-29 23:10:32 +00:00
|
|
|
|
2017-02-18 17:06:09 +00:00
|
|
|
boot.specialFileSystems.${parentWrapperDir} = {
|
|
|
|
fsType = "tmpfs";
|
2020-09-28 18:43:54 +01:00
|
|
|
options = [ "nodev" "mode=755" ];
|
2017-02-18 17:06:09 +00:00
|
|
|
};
|
|
|
|
|
2017-01-29 07:07:12 +00:00
|
|
|
# Make sure our wrapperDir exports to the PATH env variable when
|
|
|
|
# initializing the shell
|
2017-01-29 04:48:03 +00:00
|
|
|
environment.extraInit = ''
|
2017-01-29 07:07:12 +00:00
|
|
|
# Wrappers override other bin directories.
|
2017-01-29 04:48:03 +00:00
|
|
|
export PATH="${wrapperDir}:$PATH"
|
|
|
|
'';
|
|
|
|
|
2020-10-18 14:36:24 +01:00
|
|
|
security.apparmor.includes."nixos/security.wrappers" = ''
|
|
|
|
include "${pkgs.apparmorRulesFromClosure { name="security.wrappers"; } [
|
|
|
|
securityWrapper
|
|
|
|
pkgs.stdenv.cc.cc
|
|
|
|
pkgs.stdenv.cc.libc
|
|
|
|
]}"
|
|
|
|
'';
|
|
|
|
|
2021-06-10 13:57:52 +01:00
|
|
|
###### wrappers activation script
|
2017-01-29 04:48:03 +00:00
|
|
|
system.activationScripts.wrappers =
|
2017-06-25 20:42:07 +01:00
|
|
|
lib.stringAfter [ "specialfs" "users" ]
|
2017-01-29 04:48:03 +00:00
|
|
|
''
|
2020-09-28 18:43:54 +01:00
|
|
|
chmod 755 "${parentWrapperDir}"
|
|
|
|
|
2017-02-18 17:06:09 +00:00
|
|
|
# We want to place the tmpdirs for the wrappers to the parent dir.
|
|
|
|
wrapperDir=$(mktemp --directory --tmpdir="${parentWrapperDir}" wrappers.XXXXXXXXXX)
|
2021-09-12 15:14:40 +01:00
|
|
|
chmod a+rx "$wrapperDir"
|
2017-01-29 04:48:03 +00:00
|
|
|
|
2017-01-29 07:07:12 +00:00
|
|
|
${lib.concatStringsSep "\n" mkWrappedPrograms}
|
2017-01-29 10:11:01 +00:00
|
|
|
|
|
|
|
if [ -L ${wrapperDir} ]; then
|
|
|
|
# Atomically replace the symlink
|
|
|
|
# See https://axialcorps.com/2013/07/03/atomically-replacing-files-and-directories/
|
2017-01-29 11:05:30 +00:00
|
|
|
old=$(readlink -f ${wrapperDir})
|
2021-09-12 15:14:40 +01:00
|
|
|
if [ -e "${wrapperDir}-tmp" ]; then
|
|
|
|
rm --force --recursive "${wrapperDir}-tmp"
|
2020-09-28 18:43:54 +01:00
|
|
|
fi
|
2021-09-12 15:14:40 +01:00
|
|
|
ln --symbolic --force --no-dereference "$wrapperDir" "${wrapperDir}-tmp"
|
|
|
|
mv --no-target-directory "${wrapperDir}-tmp" "${wrapperDir}"
|
|
|
|
rm --force --recursive "$old"
|
2017-01-29 10:11:01 +00:00
|
|
|
else
|
|
|
|
# For initial setup
|
2021-09-12 15:14:40 +01:00
|
|
|
ln --symbolic "$wrapperDir" "${wrapperDir}"
|
2017-01-29 10:11:01 +00:00
|
|
|
fi
|
2017-01-29 04:48:03 +00:00
|
|
|
'';
|
2021-06-10 13:57:52 +01:00
|
|
|
|
|
|
|
###### wrappers consistency checks
|
|
|
|
system.extraDependencies = lib.singleton (pkgs.runCommandLocal
|
|
|
|
"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"
|
|
|
|
'');
|
2017-01-29 04:48:03 +00:00
|
|
|
};
|
|
|
|
}
|