1 { config, lib, pkgs, ... }:
4 inherit (config.security) wrapperDir wrappers;
6 parentWrapperDir = dirOf wrapperDir;
8 # This is security-sensitive code, and glibc vulns happen from time to time.
9 # musl is security-focused and generally more minimal, so it's a better choice here.
10 # The dynamic linker is still a fairly complex piece of code, and the wrappers are
11 # quite small, so linking it statically is more appropriate.
12 securityWrapper = sourceProg : pkgs.pkgsStatic.callPackage ./wrapper.nix {
15 # glibc definitions of insecure environment variables
17 # We extract the single header file we need into its own derivation,
18 # so that we don't have to pull full glibc sources to build wrappers.
20 # They're taken from pkgs.glibc so that we don't have to keep as close
21 # an eye on glibc changes. Not every relevant variable is in this header,
22 # so we maintain a slightly stricter list in wrapper.c itself as well.
23 unsecvars = lib.overrideDerivation (pkgs.srcOnly pkgs.glibc)
25 name = "${name}-unsecvars";
28 cp sysdeps/generic/unsecvars.h $out
35 # taken from the chmod(1) man page
36 symbolic = "[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+";
37 numeric = "[-+=]?[0-7]{0,4}";
38 mode = "((${symbolic})(,${symbolic})*)|(${numeric})";
40 lib.types.strMatching mode
41 // { description = "file mode string"; };
43 wrapperType = lib.types.submodule ({ name, config, ... }: {
44 options.source = lib.mkOption
45 { type = lib.types.path;
46 description = "The absolute path to the program to be wrapped.";
48 options.program = lib.mkOption
49 { type = with lib.types; nullOr str;
52 The name of the wrapper program. Defaults to the attribute name.
55 options.owner = lib.mkOption
56 { type = lib.types.str;
57 description = "The owner of the wrapper program.";
59 options.group = lib.mkOption
60 { type = lib.types.str;
61 description = "The group of the wrapper program.";
63 options.permissions = lib.mkOption
64 { type = fileModeType;
65 default = "u+rx,g+x,o+x";
68 The permissions of the wrapper program. The format is that of a
69 symbolic or numeric file mode understood by {command}`chmod`.
72 options.capabilities = lib.mkOption
73 { type = lib.types.commas;
76 A comma-separated list of capability clauses to be given to the
77 wrapper program. The format for capability clauses is described in the
78 “TEXTUAL REPRESENTATION” section of the {manpage}`cap_from_text(3)`
79 manual page. For a list of capabilities supported by the system, check
80 the {manpage}`capabilities(7)` manual page.
83 `cap_setpcap`, which is required for the wrapper
84 program to be able to raise caps into the Ambient set is NOT raised
85 to the Ambient set so that the real program cannot modify its own
86 capabilities!! This may be too restrictive for cases in which the
87 real program needs cap_setpcap but it at least leans on the side
88 security paranoid vs. too relaxed.
92 options.setuid = lib.mkOption
93 { type = lib.types.bool;
95 description = "Whether to add the setuid bit the wrapper program.";
97 options.setgid = lib.mkOption
98 { type = lib.types.bool;
100 description = "Whether to add the setgid bit the wrapper program.";
104 ###### Activation script for the setcap wrappers
115 cp ${securityWrapper source}/bin/security-wrapper "$wrapperDir/${program}"
118 chmod 0000 "$wrapperDir/${program}"
119 chown ${owner}:${group} "$wrapperDir/${program}"
121 # Set desired capabilities on the file plus cap_setpcap so
122 # the wrapper program can elevate the capabilities set on
123 # its file into the Ambient set.
124 ${pkgs.libcap.out}/bin/setcap "cap_setpcap,${capabilities}" "$wrapperDir/${program}"
126 # Set the executable bit
127 chmod ${permissions} "$wrapperDir/${program}"
130 ###### Activation script for the setuid wrappers
142 cp ${securityWrapper source}/bin/security-wrapper "$wrapperDir/${program}"
145 chmod 0000 "$wrapperDir/${program}"
146 chown ${owner}:${group} "$wrapperDir/${program}"
148 chmod "u${if setuid then "+" else "-"}s,g${if setgid then "+" else "-"}s,${permissions}" "$wrapperDir/${program}"
154 if opts.capabilities != ""
155 then mkSetcapProgram opts
156 else mkSetuidProgram opts
157 ) (lib.attrValues wrappers);
161 (lib.mkRemovedOptionModule [ "security" "setuidOwners" ] "Use security.wrappers instead")
162 (lib.mkRemovedOptionModule [ "security" "setuidPrograms" ] "Use security.wrappers instead")
168 security.wrappers = lib.mkOption {
169 type = lib.types.attrsOf wrapperType;
171 example = lib.literalExpression
174 # a setuid root program
179 source = "''${pkgs.doas}/bin/doas";
187 source = "''${pkgs.locate}/bin/locate";
190 # a program with the CAP_NET_RAW capability
194 capabilities = "cap_net_raw+ep";
195 source = "''${pkgs.iputils.out}/bin/ping";
200 This option effectively allows adding setuid/setgid bits, capabilities,
201 changing file ownership and permissions of a program without directly
202 modifying it. This works by creating a wrapper program under the
203 {option}`security.wrapperDir` directory, which is then added to
208 security.wrapperDirSize = lib.mkOption {
211 type = lib.types.str;
213 Size limit for the /run/wrappers tmpfs. Look at mount(8), tmpfs size option,
214 for the accepted syntax. WARNING: don't set to less than 64MB.
218 security.wrapperDir = lib.mkOption {
219 type = lib.types.path;
220 default = "/run/wrappers/bin";
223 This option defines the path to the wrapper programs. It
224 should not be overridden.
229 ###### implementation
232 assertions = lib.mapAttrsToList
234 { assertion = opts.setuid || opts.setgid -> opts.capabilities == "";
236 The security.wrappers.${name} wrapper is not valid:
237 setuid/setgid and capabilities are mutually exclusive.
244 mkSetuidRoot = source:
251 { # These are mount related wrappers that require the +s permission.
252 fusermount = mkSetuidRoot "${pkgs.fuse}/bin/fusermount";
253 fusermount3 = mkSetuidRoot "${pkgs.fuse3}/bin/fusermount3";
254 mount = mkSetuidRoot "${lib.getBin pkgs.util-linux}/bin/mount";
255 umount = mkSetuidRoot "${lib.getBin pkgs.util-linux}/bin/umount";
258 # Make sure our wrapperDir exports to the PATH env variable when
259 # initializing the shell
260 environment.extraInit = ''
261 # Wrappers override other bin directories.
262 export PATH="${wrapperDir}:$PATH"
265 security.apparmor.includes = lib.mapAttrs' (wrapName: wrap: lib.nameValuePair
266 "nixos/security.wrappers/${wrapName}" ''
267 include "${pkgs.apparmorRulesFromClosure { name="security.wrappers.${wrapName}"; } [
268 (securityWrapper wrap.source)
274 where = parentWrapperDir;
277 options = lib.concatStringsSep "," ([
280 "size=${config.security.wrapperDirSize}"
284 systemd.services.suid-sgid-wrappers = {
285 description = "Create SUID/SGID Wrappers";
286 wantedBy = [ "sysinit.target" ];
287 before = [ "sysinit.target" "shutdown.target" ];
288 conflicts = [ "shutdown.target" ];
289 after = [ "systemd-sysusers.service" ];
290 unitConfig.DefaultDependencies = false;
291 unitConfig.RequiresMountsFor = [ "/nix/store" "/run/wrappers" ];
292 serviceConfig.Type = "oneshot";
294 chmod 755 "${parentWrapperDir}"
296 # We want to place the tmpdirs for the wrappers to the parent dir.
297 wrapperDir=$(mktemp --directory --tmpdir="${parentWrapperDir}" wrappers.XXXXXXXXXX)
298 chmod a+rx "$wrapperDir"
300 ${lib.concatStringsSep "\n" mkWrappedPrograms}
302 if [ -L ${wrapperDir} ]; then
303 # Atomically replace the symlink
304 # See https://axialcorps.com/2013/07/03/atomically-replacing-files-and-directories/
305 old=$(readlink -f ${wrapperDir})
306 if [ -e "${wrapperDir}-tmp" ]; then
307 rm --force --recursive "${wrapperDir}-tmp"
309 ln --symbolic --force --no-dereference "$wrapperDir" "${wrapperDir}-tmp"
310 mv --no-target-directory "${wrapperDir}-tmp" "${wrapperDir}"
311 rm --force --recursive "$old"
314 ln --symbolic "$wrapperDir" "${wrapperDir}"
319 ###### wrappers consistency checks
320 system.checks = lib.singleton (pkgs.runCommandLocal
321 "ensure-all-wrappers-paths-exist" { }
323 # make sure we produce output
326 echo -n "Checking that Nix store paths of all wrapped programs exist... "
329 ${lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v:
330 "wrappers['${n}']='${v.source}'") wrappers)}
332 for name in "''${!wrappers[@]}"; do
333 path="''${wrappers[$name]}"
334 if [[ "$path" =~ /nix/store ]] && [ ! -e "$path" ]; then
335 test -t 1 && echo -ne '\033[1;31m'
337 echo "The path $path does not exist!"
338 echo 'Please, check the value of `security.wrappers."'$name'".source`.'
339 test -t 1 && echo -ne '\033[0m'