Skip to content

Commit aa3e6fa

Browse files
authored
Merge pull request #325133 from jpds/nixos-prometheus-hardening
nixos/prometheus: systemd hardening for alertmanager/pushgateway
2 parents bfbc945 + 008ea18 commit aa3e6fa

File tree

5 files changed

+106
-7
lines changed

5 files changed

+106
-7
lines changed

nixos/modules/services/monitoring/prometheus/alertmanager-webhook-logger.nix

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ in
3232
${escapeShellArgs cfg.extraFlags}
3333
'';
3434

35+
CapabilityBoundingSet = [ "" ];
36+
DeviceAllow = [ "" ];
3537
DynamicUser = true;
3638
NoNewPrivileges = true;
3739

40+
MemoryDenyWriteExecute = true;
41+
42+
LockPersonality = true;
43+
3844
ProtectProc = "invisible";
3945
ProtectSystem = "strict";
4046
ProtectHome = "tmpfs";
@@ -43,14 +49,19 @@ in
4349
PrivateDevices = true;
4450
PrivateIPC = true;
4551

52+
ProcSubset = "pid";
53+
4654
ProtectHostname = true;
4755
ProtectClock = true;
4856
ProtectKernelTunables = true;
4957
ProtectKernelModules = true;
5058
ProtectKernelLogs = true;
5159
ProtectControlGroups = true;
5260

61+
Restart = "on-failure";
62+
5363
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
64+
RestrictNamespaces = true;
5465
RestrictRealtime = true;
5566
RestrictSUIDSGID = true;
5667

nixos/modules/services/monitoring/prometheus/alertmanager.nix

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,57 @@ in {
181181
-i "${alertmanagerYml}"
182182
'';
183183
serviceConfig = {
184-
Restart = "always";
185-
StateDirectory = "alertmanager";
186-
DynamicUser = true; # implies PrivateTmp
187-
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
188-
WorkingDirectory = "/tmp";
189184
ExecStart = "${cfg.package}/bin/alertmanager" +
190185
optionalString (length cmdlineArgs != 0) (" \\\n " +
191186
concatStringsSep " \\\n " cmdlineArgs);
192187
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
188+
189+
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
190+
191+
CapabilityBoundingSet = [ "" ];
192+
DeviceAllow = [ "" ];
193+
DynamicUser = true;
194+
NoNewPrivileges = true;
195+
196+
MemoryDenyWriteExecute = true;
197+
198+
LockPersonality = true;
199+
200+
ProtectProc = "invisible";
201+
ProtectSystem = "strict";
202+
ProtectHome = "tmpfs";
203+
204+
PrivateTmp = true;
205+
PrivateDevices = true;
206+
PrivateIPC = true;
207+
208+
ProcSubset = "pid";
209+
210+
ProtectHostname = true;
211+
ProtectClock = true;
212+
ProtectKernelTunables = true;
213+
ProtectKernelModules = true;
214+
ProtectKernelLogs = true;
215+
ProtectControlGroups = true;
216+
217+
Restart = "always";
218+
219+
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_NETLINK" ];
220+
RestrictNamespaces = true;
221+
RestrictRealtime = true;
222+
RestrictSUIDSGID = true;
223+
224+
StateDirectory = "alertmanager";
225+
SystemCallFilter = [
226+
"@system-service"
227+
"~@cpu-emulation"
228+
"~@privileged"
229+
"~@reboot"
230+
"~@setuid"
231+
"~@swap"
232+
];
233+
234+
WorkingDirectory = "/tmp";
193235
};
194236
};
195237
})

nixos/modules/services/monitoring/prometheus/pushgateway.nix

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,52 @@ in {
147147
wantedBy = [ "multi-user.target" ];
148148
after = [ "network.target" ];
149149
serviceConfig = {
150-
Restart = "always";
151-
DynamicUser = true;
152150
ExecStart = "${cfg.package}/bin/pushgateway" +
153151
optionalString (length cmdlineArgs != 0) (" \\\n " +
154152
concatStringsSep " \\\n " cmdlineArgs);
153+
154+
CapabilityBoundingSet = [ "" ];
155+
DeviceAllow = [ "" ];
156+
DynamicUser = true;
157+
NoNewPrivileges = true;
158+
159+
MemoryDenyWriteExecute = true;
160+
161+
LockPersonality = true;
162+
163+
ProtectProc = "invisible";
164+
ProtectSystem = "strict";
165+
ProtectHome = "tmpfs";
166+
167+
PrivateTmp = true;
168+
PrivateDevices = true;
169+
PrivateIPC = true;
170+
171+
ProcSubset = "pid";
172+
173+
ProtectHostname = true;
174+
ProtectClock = true;
175+
ProtectKernelTunables = true;
176+
ProtectKernelModules = true;
177+
ProtectKernelLogs = true;
178+
ProtectControlGroups = true;
179+
180+
Restart = "always";
181+
182+
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
183+
RestrictNamespaces = true;
184+
RestrictRealtime = true;
185+
RestrictSUIDSGID = true;
186+
155187
StateDirectory = if cfg.persistMetrics then cfg.stateDir else null;
188+
SystemCallFilter = [
189+
"@system-service"
190+
"~@cpu-emulation"
191+
"~@privileged"
192+
"~@reboot"
193+
"~@setuid"
194+
"~@swap"
195+
];
156196
};
157197
};
158198
};

nixos/tests/prometheus/alertmanager.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,9 @@ import ../make-test-python.nix ({ lib, pkgs, ... }:
144144
logger.wait_until_succeeds(
145145
"journalctl -o cat -u alertmanager-webhook-logger.service | grep '\"alertname\":\"InstanceDown\"'"
146146
)
147+
148+
logger.log(logger.succeed("systemd-analyze security alertmanager-webhook-logger.service | grep -v '✓'"))
149+
150+
alertmanager.log(alertmanager.succeed("systemd-analyze security alertmanager.service | grep -v '✓'"))
147151
'';
148152
})

nixos/tests/prometheus/pushgateway.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,7 @@ import ../make-test-python.nix ({ lib, pkgs, ... }:
9090
"curl -sf 'http://127.0.0.1:9090/api/v1/query?query=absent(some_metric)' | "
9191
+ "jq '.data.result[0].value[1]' | grep '\"1\"'"
9292
)
93+
94+
pushgateway.log(pushgateway.succeed("systemd-analyze security pushgateway.service | grep -v '✓'"))
9395
'';
9496
})

0 commit comments

Comments
 (0)