Skip to content

Commit 045fbc3

Browse files
committed
nixos/tmpfiles: properly escape argument option
The systemd.tmpfiles.settings.<name>.<path>.<type>.argument option may contain arbitrary strings. This could allow intentional or unintentional introduction of new configuration lines. The argument field cannot be quoted, C‐style \xNN escape sequences are however permitted. By escaping whitespace and newline characters, the issue can be mitigated.
1 parent b179a7a commit 045fbc3

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

nixos/modules/system/boot/systemd/tmpfiles.nix

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ let
1818
inherit elemType placeholder;
1919
};
2020

21+
escapeArgument = lib.strings.escapeC [
22+
"\t"
23+
"\n"
24+
"\r"
25+
" "
26+
"\\"
27+
];
28+
2129
settingsOption = {
2230
description = ''
2331
Declare systemd-tmpfiles rules to create, delete, and clean up volatile
@@ -126,7 +134,7 @@ let
126134

127135
# generates a single entry for a tmpfiles.d rule
128136
settingsEntryToRule = path: entry: ''
129-
'${entry.type}' '${path}' '${entry.mode}' '${entry.user}' '${entry.group}' '${entry.age}' ${entry.argument}
137+
'${entry.type}' '${path}' '${entry.mode}' '${entry.user}' '${entry.group}' '${entry.age}' ${escapeArgument entry.argument}
130138
'';
131139

132140
# generates a list of tmpfiles.d rules from the attrs (paths) under tmpfiles.settings.<name>
@@ -199,7 +207,25 @@ in
199207
"boot.initrd.systemd.storePaths will lead to errors in the future."
200208
"Found these problematic files: ${lib.concatStringsSep ", " paths}"
201209
]
202-
);
210+
)
211+
++ (lib.flatten (
212+
lib.mapAttrsToList (
213+
name: paths:
214+
lib.mapAttrsToList (
215+
path: entries:
216+
lib.mapAttrsToList (
217+
type': entry:
218+
lib.optional (lib.match ''.*\\([nrt]|x[0-9A-Fa-f]{2}).*'' entry.argument != null) (
219+
lib.concatStringsSep " " [
220+
"The argument option of ${name}.${type'}.${path} appears to"
221+
"contain escape sequences, which will be escaped again."
222+
"Unescape them if this is not intended: \"${entry.argument}\""
223+
]
224+
)
225+
) entries
226+
) paths
227+
) cfg.settings
228+
));
203229

204230
systemd.additionalUpstreamSystemUnits = [
205231
"systemd-tmpfiles-clean.service"

0 commit comments

Comments
 (0)