From a9c75e76895641510338124331fc837cefac1872 Mon Sep 17 00:00:00 2001 From: r-vdp Date: Thu, 8 May 2025 12:13:50 +0200 Subject: [PATCH 1/3] make-initrd-ng: fix file permissions We want to strip the write bit from files after we copied them. XOR is not the right operator for this, since if the bit is 0 in both the actual permissions and the mask, then the result will be a 1. So in practice, we were assigning write permissions for group and others to all files and we were only stripping the write permissions of the owner (since the owner had write permissions, and so the result of the XOR is 0). The correct thing to do is to AND with the maximum permissions that we want to maintain (which is the inverse of what we want to strip), so that only those bits are preserved and the others are always set to 0. --- pkgs/build-support/kernel/make-initrd-ng/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/kernel/make-initrd-ng/src/main.rs b/pkgs/build-support/kernel/make-initrd-ng/src/main.rs index 64c114f2d9fa..149c5393e35a 100644 --- a/pkgs/build-support/kernel/make-initrd-ng/src/main.rs +++ b/pkgs/build-support/kernel/make-initrd-ng/src/main.rs @@ -212,7 +212,7 @@ fn copy_file< } // Remove writable permissions - permissions.set_mode(permissions.mode() ^ 0o222); + permissions.set_mode(permissions.mode() & 0o555); fs::set_permissions(&target, permissions) .wrap_err_with(|| format!("failed to remove writable permissions for {:?}", target))?; }; From 01b589a7f89d204518ebfc09e07d6161e7739617 Mon Sep 17 00:00:00 2001 From: r-vdp Date: Thu, 8 May 2025 12:32:46 +0200 Subject: [PATCH 2/3] systemd-initrd: add test to ensure that the permissions on the systemd generators are correct --- nixos/tests/systemd-initrd-simple.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nixos/tests/systemd-initrd-simple.nix b/nixos/tests/systemd-initrd-simple.nix index 7a379404bbc2..646eb9e0ac2c 100644 --- a/nixos/tests/systemd-initrd-simple.nix +++ b/nixos/tests/systemd-initrd-simple.nix @@ -50,6 +50,9 @@ import ./make-test-python.nix ( newAvail = machine.succeed("df --output=avail / | sed 1d") assert int(oldAvail) < int(newAvail), "File system did not grow" + + with subtest("no warnings from systemd about write permissions"): + machine.fail("journalctl -b 0 | grep 'is marked world-writable, which is a security risk as it is executed with privileges'") ''; } ) From ed70f0089d3b02540fd89ae4bb18e51c88db1ac2 Mon Sep 17 00:00:00 2001 From: r-vdp Date: Thu, 8 May 2025 12:37:39 +0200 Subject: [PATCH 3/3] systemd-initrd: migrate test to runTest and add comment for syntax highlighting of test script --- nixos/tests/all-tests.nix | 2 +- nixos/tests/systemd-initrd-simple.nix | 27 +++++++++++++-------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index b5c46f2fb16a..22058134ecb2 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -1296,7 +1296,7 @@ in systemd-initrd-luks-unl0kr = handleTest ./systemd-initrd-luks-unl0kr.nix { }; systemd-initrd-modprobe = handleTest ./systemd-initrd-modprobe.nix { }; systemd-initrd-shutdown = handleTest ./systemd-shutdown.nix { systemdStage1 = true; }; - systemd-initrd-simple = handleTest ./systemd-initrd-simple.nix { }; + systemd-initrd-simple = runTest ./systemd-initrd-simple.nix; systemd-initrd-swraid = handleTest ./systemd-initrd-swraid.nix { }; systemd-initrd-vconsole = handleTest ./systemd-initrd-vconsole.nix { }; systemd-initrd-networkd = handleTest ./systemd-initrd-networkd.nix { }; diff --git a/nixos/tests/systemd-initrd-simple.nix b/nixos/tests/systemd-initrd-simple.nix index 646eb9e0ac2c..191a23abb2e4 100644 --- a/nixos/tests/systemd-initrd-simple.nix +++ b/nixos/tests/systemd-initrd-simple.nix @@ -1,17 +1,17 @@ -import ./make-test-python.nix ( - { lib, pkgs, ... }: - { - name = "systemd-initrd-simple"; +{ + name = "systemd-initrd-simple"; - nodes.machine = - { pkgs, ... }: - { - testing.initrdBackdoor = true; - boot.initrd.systemd.enable = true; - virtualisation.fileSystems."/".autoResize = true; - }; + nodes.machine = + { pkgs, ... }: + { + testing.initrdBackdoor = true; + boot.initrd.systemd.enable = true; + virtualisation.fileSystems."/".autoResize = true; + }; - testScript = '' + testScript = + # python + '' import subprocess with subtest("testing initrd backdoor"): @@ -54,5 +54,4 @@ import ./make-test-python.nix ( with subtest("no warnings from systemd about write permissions"): machine.fail("journalctl -b 0 | grep 'is marked world-writable, which is a security risk as it is executed with privileges'") ''; - } -) +}