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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
{ config, lib, pkgs, ... }:
let
inherit (config.security) permissionsWrapperDir;
cfg = config.security.permissionsWrappers;
setcapWrappers = import ./setcap-wrapper-drv.nix { };
setuidWrappers = import ./setuid-wrapper-drv.nix { };
###### Activation script for the setcap wrappers
configureSetcapWrapper =
{ program
, capabilities
, source ? null
, owner ? "nobody"
, group ? "nogroup"
, setcap ? false
}:
''
cp ${setcapWrappers}/bin/${program}.wrapper ${permissionsWrapperDir}/${program}
# Prevent races
chmod 0000 ${permissionsWrapperDir}/${program}
chown ${owner}.${group} ${permissionsWrapperDir}/${program}
# 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.
#
# Only set the capabilities though if we're being told to
# do so.
${
if setcap then
''
${pkgs.libcap.out}/bin/setcap "cap_setpcap,${capabilities}" ${permissionsWrapperDir}/${program}
''
else ""
}
# Set the executable bit
chmod u+rx,g+x,o+x ${permissionsWrapperDir}/${program}
'';
###### Activation script for the setuid wrappers
setuidPrograms =
(map (x: { program = x; owner = "root"; group = "root"; setuid = true; })
config.security.setuidPrograms)
++ config.security.setuidOwners;
makeSetuidWrapper =
{ program
, source ? null
, owner ? "nobody"
, group ? "nogroup"
, setuid ? false
, setgid ? false
, permissions ? "u+rx,g+x,o+x"
}:
''
cp ${setuidWrappers}/bin/${program}.wrapper ${permissionsWrapperDir}/${program}
# Prevent races
chmod 0000 ${permissionsWrapperDir}/${program}
chown ${owner}.${group} ${permissionsWrapperDir}/${program}
chmod "u${if setuid then "+" else "-"}s,g${if setgid then "+" else "-"}s,${permissions}" ${permissionsWrapperDir}/${program}
'';
in
{
###### interface
options = {
security.permissionsWrappers.setcap = mkOption {
type = types.listOf types.attrs;
default = [];
example =
[ { program = "ping";
source = "${pkgs.iputils.out}/bin/ping"
owner = "nobody";
group = "nogroup";
setcap = true;
capabilities = "cap_net_raw+ep";
}
];
description = ''
This option sets capabilities on a wrapper program that
propagates those capabilities down to the wrapped, real
program.
The `program` attribute is the name of the program to be
wrapped. If no `source` attribute is provided, specifying the
absolute path to the program, then the program will be
searched for in the path environment variable.
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.
The attribute `setcap` defaults to false and it will create a
wrapper program but never set the capability set on it. This
is done so that you can remove a capability sent entirely from
a wrapper program without also needing to go change any
absolute paths that may be directly referencing the wrapper
program.
'';
};
security.permissionsWrappers.setuid = mkOption {
type = types.listOf types.attrs;
default = [];
example =
[ { program = "sendmail";
source = "${pkgs.sendmail.bin}/bin/sendmail";
owner = "nobody";
group = "postdrop";
setuid = false;
setgid = true;
permissions = "u+rx,g+x,o+x";
}
];
description = ''
This option allows the ownership and permissions on the setuid
wrappers for specific programs to be overridden from the
default (setuid root, but not setgid root).
'';
};
security.permissionsWrapperDir = mkOption {
type = types.path;
default = "/var/permissions-wrappers";
internal = true;
description = ''
This option defines the path to the permissions wrappers. It
should not be overriden.
'';
};
};
###### implementation
config = {
# Make sure our setcap-wrapper dir exports to the PATH env
# variable when initializing the shell
environment.extraInit = ''
# The permissions wrappers override other bin directories.
export PATH="${config.security.permissionsWrapperDir}:$PATH"
'';
###### setcap activation script
system.activationScripts.setcap =
stringAfter [ "users" ]
''
# Look in the system path and in the default profile for
# programs to be wrapped.
PERMISSIONS_WRAPPER_PATH=${config.system.path}/bin:${config.system.path}/sbin
# When a program is removed from the security.permissionsWrappers.setcap
# list we have to remove all of the previous program wrappers
# and re-build them minus the wrapper for the program removed,
# hence the rm here in the activation script.
rm -f ${permissionsWrapperDir}/*
# Concatenate the generated shell slices to configure
# wrappers for each program needing specialized capabilities.
${concatMapStrings configureSetcapWrapper cfg.setcap}
'';
###### setuid activation script
system.activationScripts.setuid =
stringAfter [ "users" ]
''
# Look in the system path and in the default profile for
# programs to be wrapped.
PERMISSIONS_WRAPPER_PATH=${config.system.path}/bin:${config.system.path}/sbin
# When a program is removed from the security.permissionsWrappers.setcap
# list we have to remove all of the previous program wrappers
# and re-build them minus the wrapper for the program removed,
# hence the rm here in the activation script.
rm -f ${permissionsWrapperDir}/*
# Concatenate the generated shell slices to configure
# wrappers for each program needing specialized capabilities.
${concatMapStrings configureSetuidWrapper cfg.setuid}
'';
};
}
|