From a1678269f93d40f334798fb166956733f528d70d Mon Sep 17 00:00:00 2001 From: Joachim Fasting Date: Sun, 30 Apr 2017 14:41:56 +0200 Subject: [PATCH] nixos/hardened profile: disable user namespaces at runtime --- nixos/modules/profiles/hardened.nix | 12 ++++++++++++ nixos/tests/hardened.nix | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/nixos/modules/profiles/hardened.nix b/nixos/modules/profiles/hardened.nix index c7f80fe47aac..8bde2e4f4984 100644 --- a/nixos/modules/profiles/hardened.nix +++ b/nixos/modules/profiles/hardened.nix @@ -47,4 +47,16 @@ with lib; # ... or at least apply some hardening to it boot.kernel.sysctl."net.core.bpf_jit_harden" = mkDefault true; + + # A recurring problem with user namespaces is that there are + # still code paths where the kernel's permission checking logic + # fails to account for namespacing, instead permitting a + # namespaced process to act outside the namespace with the + # same privileges as it would have inside it. This is particularly + # bad in the common case of running as root within the namespace. + # + # Setting the number of allowed userns to 0 effectively disables + # the feature at runtime. Attempting to create a user namespace + # with unshare will then fail with "no space left on device". + boot.kernel.sysctl."user.max_user_namespaces" = mkDefault 0; } diff --git a/nixos/tests/hardened.nix b/nixos/tests/hardened.nix index 389d7ed7ffaa..1d9a9043e03a 100644 --- a/nixos/tests/hardened.nix +++ b/nixos/tests/hardened.nix @@ -27,5 +27,10 @@ import ./make-test.nix ({ pkgs, ...} : { # note: this better a be module we normally wouldn't load ... $machine->fail("modprobe dccp"); }; + + # Test userns + subtest "userns", sub { + $machine->fail("unshare --user"); + }; ''; })