Add no-die-with-parent combinator
This commit is contained in:
parent
8f61c14085
commit
053e2c1364
9 changed files with 131 additions and 12 deletions
|
|
@ -39,6 +39,7 @@ let
|
||||||
jail-to-host-channel = ./combinators/jail-to-host-channel.nix;
|
jail-to-host-channel = ./combinators/jail-to-host-channel.nix;
|
||||||
mount-cwd = ./combinators/mount-cwd.nix;
|
mount-cwd = ./combinators/mount-cwd.nix;
|
||||||
network = ./combinators/network.nix;
|
network = ./combinators/network.nix;
|
||||||
|
no-die-with-parent = ./combinators/no-die-with-parent.nix;
|
||||||
no-new-session = ./combinators/no-new-session.nix;
|
no-new-session = ./combinators/no-new-session.nix;
|
||||||
noescape = ./combinators/noescape.nix;
|
noescape = ./combinators/noescape.nix;
|
||||||
notifications = ./combinators/notifications.nix;
|
notifications = ./combinators/notifications.nix;
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,6 @@ in
|
||||||
* Sets up a fake dev at `/dev` (Using `--dev /dev` bwrap args)
|
* Sets up a fake dev at `/dev` (Using `--dev /dev` bwrap args)
|
||||||
* Sets up a tmpfs at `/tmp` (Using `--tmpfs /tmp` bwrap args)
|
* Sets up a tmpfs at `/tmp` (Using `--tmpfs /tmp` bwrap args)
|
||||||
* Sets up a tmpfs at your home (Using `--tmpfs ~` bwrap args)
|
* Sets up a tmpfs at your home (Using `--tmpfs ~` bwrap args)
|
||||||
* Ensures all processes in the jail are killed when the jail exits
|
|
||||||
(Using `--die-with-parent` bwrap arg)
|
|
||||||
* Adds coreutils to the package deps (`add-pkg-deps [ pkgs.coreutils ]`)
|
* Adds coreutils to the package deps (`add-pkg-deps [ pkgs.coreutils ]`)
|
||||||
* Binds `/bin/sh` from `pkgs.bash` and adds `/bin` to `$PATH`
|
* Binds `/bin/sh` from `pkgs.bash` and adds `/bin` to `$PATH`
|
||||||
(`ro-bind "''${pkgs.bash}/bin/sh" "/bin/sh"`)
|
(`ro-bind "''${pkgs.bash}/bin/sh" "/bin/sh"`)
|
||||||
|
|
@ -45,7 +43,6 @@ in
|
||||||
(unsafe-add-raw-args "--dev /dev")
|
(unsafe-add-raw-args "--dev /dev")
|
||||||
(unsafe-add-raw-args "--tmpfs /tmp")
|
(unsafe-add-raw-args "--tmpfs /tmp")
|
||||||
(unsafe-add-raw-args "--tmpfs ~")
|
(unsafe-add-raw-args "--tmpfs ~")
|
||||||
(unsafe-add-raw-args "--die-with-parent")
|
|
||||||
(ro-bind "${pkgs.bash}/bin/sh" "/bin/sh")
|
(ro-bind "${pkgs.bash}/bin/sh" "/bin/sh")
|
||||||
(add-path "/bin")
|
(add-path "/bin")
|
||||||
(helpers.pushState "additionalRuntimeClosures" pkgs.bash)
|
(helpers.pushState "additionalRuntimeClosures" pkgs.bash)
|
||||||
|
|
|
||||||
15
lib/combinators/no-die-with-parent.nix
Normal file
15
lib/combinators/no-die-with-parent.nix
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
{ ... }:
|
||||||
|
{
|
||||||
|
sig = "Permission";
|
||||||
|
doc = ''
|
||||||
|
Disables `--die-with-parent`
|
||||||
|
|
||||||
|
By default, jail-nix includes the `--die-with-parent` bwrap flag which
|
||||||
|
kills all processes within this jail when the parent process dies. Use this
|
||||||
|
combinator if you don't want this behavior.
|
||||||
|
|
||||||
|
This may be useful if the software you are jailing forks a daemon and then
|
||||||
|
exits.
|
||||||
|
'';
|
||||||
|
impl = state: state // { dieWithParent = false; };
|
||||||
|
}
|
||||||
|
|
@ -36,6 +36,7 @@ let
|
||||||
argv = "\"$@\"";
|
argv = "\"$@\"";
|
||||||
runtime = "";
|
runtime = "";
|
||||||
newSession = true;
|
newSession = true;
|
||||||
|
dieWithParent = true;
|
||||||
hostname = "jail";
|
hostname = "jail";
|
||||||
env = { };
|
env = { };
|
||||||
namespaces = { };
|
namespaces = { };
|
||||||
|
|
@ -75,6 +76,7 @@ let
|
||||||
)
|
)
|
||||||
(s: if builtins.length s.path > 0 then set-env "PATH" (lib.concatStringsSep ":" s.path) s else s)
|
(s: if builtins.length s.path > 0 then set-env "PATH" (lib.concatStringsSep ":" s.path) s else s)
|
||||||
(s: if s.newSession then unsafe-add-raw-args "--new-session" s else s)
|
(s: if s.newSession then unsafe-add-raw-args "--new-session" s else s)
|
||||||
|
(s: if s.dieWithParent then unsafe-add-raw-args "--die-with-parent" s else s)
|
||||||
(
|
(
|
||||||
s:
|
s:
|
||||||
lib.foldr (
|
lib.foldr (
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ test-suite spec
|
||||||
ImportQualifiedPost
|
ImportQualifiedPost
|
||||||
LambdaCase
|
LambdaCase
|
||||||
NamedFieldPuns
|
NamedFieldPuns
|
||||||
|
NumericUnderscores
|
||||||
OverloadedStrings
|
OverloadedStrings
|
||||||
QuasiQuotes
|
QuasiQuotes
|
||||||
ScopedTypeVariables
|
ScopedTypeVariables
|
||||||
|
|
@ -51,4 +52,5 @@ test-suite spec
|
||||||
, string-conversions
|
, string-conversions
|
||||||
, temporary
|
, temporary
|
||||||
, text
|
, text
|
||||||
|
, unix
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ tests:
|
||||||
- string-conversions
|
- string-conversions
|
||||||
- temporary
|
- temporary
|
||||||
- text
|
- text
|
||||||
|
- unix
|
||||||
|
|
||||||
default-extensions:
|
default-extensions:
|
||||||
- DerivingStrategies
|
- DerivingStrategies
|
||||||
|
|
@ -28,6 +29,7 @@ default-extensions:
|
||||||
- ImportQualifiedPost
|
- ImportQualifiedPost
|
||||||
- LambdaCase
|
- LambdaCase
|
||||||
- NamedFieldPuns
|
- NamedFieldPuns
|
||||||
|
- NumericUnderscores
|
||||||
- OverloadedStrings
|
- OverloadedStrings
|
||||||
- QuasiQuotes
|
- QuasiQuotes
|
||||||
- ScopedTypeVariables
|
- ScopedTypeVariables
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
module CombinatorsSpec (spec) where
|
module CombinatorsSpec (spec) where
|
||||||
|
|
||||||
|
import Control.Concurrent (threadDelay)
|
||||||
|
import Control.Monad
|
||||||
import Data.List.Extra
|
import Data.List.Extra
|
||||||
|
import Data.Maybe (fromJust)
|
||||||
import Data.String.Interpolate.Util
|
import Data.String.Interpolate.Util
|
||||||
import System.Directory.Extra (createDirectoryIfMissing, doesFileExist)
|
import System.Directory.Extra (createDirectoryIfMissing, doesFileExist, doesPathExist)
|
||||||
|
import System.Posix (getProcessStatus, sigKILL, signalProcess)
|
||||||
|
import System.Process qualified as Process
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import TestPrelude
|
import TestPrelude
|
||||||
|
|
||||||
|
|
@ -111,6 +116,60 @@ spec = parallel $ inTestM $ do
|
||||||
|]
|
|]
|
||||||
liftIO $ (read out :: Int) `shouldSatisfy` (< 100)
|
liftIO $ (read out :: Int) `shouldSatisfy` (< 100)
|
||||||
|
|
||||||
|
describe "no-die-with-parent" $ do
|
||||||
|
let waitForChildPid :: Process.Pid -> TestM Process.Pid
|
||||||
|
waitForChildPid pid = do
|
||||||
|
children <- liftIO $ readFile ("/proc" </> show pid </> "task" </> show pid </> "children")
|
||||||
|
if children /= ""
|
||||||
|
then pure $ read children
|
||||||
|
else do
|
||||||
|
liftIO $ threadDelay 20_000
|
||||||
|
waitForChildPid pid
|
||||||
|
|
||||||
|
let pidExists :: Process.Pid -> TestM Bool
|
||||||
|
pidExists = liftIO . doesPathExist . ("/proc" </>) . show
|
||||||
|
|
||||||
|
let killProcess :: Process.Pid -> Bool -> TestM ()
|
||||||
|
killProcess pid waitForProcessToExit = liftIO $ do
|
||||||
|
signalProcess sigKILL pid
|
||||||
|
when waitForProcessToExit $ void $ getProcessStatus True False pid
|
||||||
|
|
||||||
|
it "kills all child processes when not specifying no-die-with-parent" $ do
|
||||||
|
(_, _, _, ph) <-
|
||||||
|
spawnNixDrv
|
||||||
|
[i|
|
||||||
|
let script = sh "nohup sleep inf";
|
||||||
|
in jail "test" script null
|
||||||
|
|]
|
||||||
|
outerBwrapPid <- liftIO $ fromJust <$> Process.getPid ph
|
||||||
|
innerBwrapPid <- waitForChildPid outerBwrapPid
|
||||||
|
bashPid <- waitForChildPid innerBwrapPid
|
||||||
|
sleepPid <- waitForChildPid bashPid
|
||||||
|
killProcess outerBwrapPid True
|
||||||
|
pidExists outerBwrapPid `shouldEventuallyReturn` False
|
||||||
|
pidExists innerBwrapPid `shouldEventuallyReturn` False
|
||||||
|
pidExists bashPid `shouldEventuallyReturn` False
|
||||||
|
pidExists sleepPid `shouldEventuallyReturn` False
|
||||||
|
|
||||||
|
it "does not kill child processes when specifying no-die-with-parent" $ do
|
||||||
|
(_, _, _, ph) <-
|
||||||
|
spawnNixDrv
|
||||||
|
[i|
|
||||||
|
let script = sh "nohup sleep inf";
|
||||||
|
in jail "test" script (c: [ c.no-die-with-parent ])
|
||||||
|
|]
|
||||||
|
outerBwrapPid <- liftIO $ fromJust <$> Process.getPid ph
|
||||||
|
innerBwrapPid <- waitForChildPid outerBwrapPid
|
||||||
|
bashPid <- waitForChildPid innerBwrapPid
|
||||||
|
sleepPid <- waitForChildPid bashPid
|
||||||
|
killProcess outerBwrapPid True
|
||||||
|
pidExists outerBwrapPid `shouldEventuallyReturn` False
|
||||||
|
liftIO $ threadDelay 500_000
|
||||||
|
pidExists innerBwrapPid `shouldReturnTestM` True
|
||||||
|
pidExists bashPid `shouldReturnTestM` True
|
||||||
|
pidExists sleepPid `shouldReturnTestM` True
|
||||||
|
killProcess sleepPid False
|
||||||
|
|
||||||
describe "open-urls-in-browser" $ do
|
describe "open-urls-in-browser" $ do
|
||||||
it "exposes a $BROWSER in the jail that calls $BROWSER outside of the jail" $ do
|
it "exposes a $BROWSER in the jail that calls $BROWSER outside of the jail" $ do
|
||||||
tmpDir <- getTestDir
|
tmpDir <- getTestDir
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,7 @@ spec = parallel $ inTestM $ do
|
||||||
in
|
in
|
||||||
jail' "test" "some-entrypoint" []
|
jail' "test" "some-entrypoint" []
|
||||||
|]
|
|]
|
||||||
`shouldOutput` "--unshare-user --unshare-ipc --unshare-pid --unshare-net --unshare-uts --unshare-cgroup --new-session -- some-entrypoint"
|
`shouldOutput` "--unshare-user --unshare-ipc --unshare-pid --unshare-net --unshare-uts --unshare-cgroup --new-session --die-with-parent -- some-entrypoint"
|
||||||
|
|
||||||
describe "sandboxed nix store" $ do
|
describe "sandboxed nix store" $ do
|
||||||
let checkRuntimeDepIsExposed :: Bool -> String -> TestM ()
|
let checkRuntimeDepIsExposed :: Bool -> String -> TestM ()
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,19 @@ module TestPrelude
|
||||||
inTestM,
|
inTestM,
|
||||||
liftIO,
|
liftIO,
|
||||||
runNixDrv,
|
runNixDrv,
|
||||||
shouldOutput,
|
spawnNixDrv,
|
||||||
toNixString,
|
toNixString,
|
||||||
void,
|
void,
|
||||||
withEnv,
|
withEnv,
|
||||||
lifted,
|
lifted,
|
||||||
|
-- expectations
|
||||||
|
shouldEventuallyReturn,
|
||||||
|
shouldOutput,
|
||||||
|
shouldReturnTestM,
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
|
import Control.Concurrent (threadDelay)
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Control.Monad.Extra (mapMaybeM)
|
import Control.Monad.Extra (mapMaybeM)
|
||||||
import Control.Monad.IO.Class (liftIO)
|
import Control.Monad.IO.Class (liftIO)
|
||||||
|
|
@ -29,9 +34,11 @@ import Data.Map qualified as Map
|
||||||
import Data.Maybe (fromJust)
|
import Data.Maybe (fromJust)
|
||||||
import Data.String.Conversions
|
import Data.String.Conversions
|
||||||
import Data.String.Interpolate (i)
|
import Data.String.Interpolate (i)
|
||||||
|
import GHC.IO.Handle (Handle)
|
||||||
import System.Environment (lookupEnv)
|
import System.Environment (lookupEnv)
|
||||||
import System.FilePath ((</>))
|
import System.FilePath ((</>))
|
||||||
import System.IO.Temp (withSystemTempDirectory)
|
import System.IO.Temp (withSystemTempDirectory)
|
||||||
|
import System.Process (CreateProcess, ProcessHandle, createProcess)
|
||||||
import System.Process qualified as Process
|
import System.Process qualified as Process
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Core.Spec qualified
|
import Test.Hspec.Core.Spec qualified
|
||||||
|
|
@ -55,6 +62,9 @@ lifted io action = do
|
||||||
ctx <- ask
|
ctx <- ask
|
||||||
liftIO $ io $ flip runReaderT ctx . testMReaderT . action
|
liftIO $ io $ flip runReaderT ctx . testMReaderT . action
|
||||||
|
|
||||||
|
lifted' :: (IO x -> IO y) -> TestM x -> TestM y
|
||||||
|
lifted' fn action = lifted (fn . ($ ())) (const action)
|
||||||
|
|
||||||
instance Example (TestM ()) where
|
instance Example (TestM ()) where
|
||||||
type Arg (TestM ()) = Context
|
type Arg (TestM ()) = Context
|
||||||
evaluateExample test _ hook _ = do
|
evaluateExample test _ hook _ = do
|
||||||
|
|
@ -94,15 +104,26 @@ toNixString s =
|
||||||
withEnv :: String -> String -> TestM () -> TestM ()
|
withEnv :: String -> String -> TestM () -> TestM ()
|
||||||
withEnv name value = local $ \ctx -> ctx {env = Map.insert name value (env ctx)}
|
withEnv name value = local $ \ctx -> ctx {env = Map.insert name value (env ctx)}
|
||||||
|
|
||||||
runNixDrv :: String -> TestM String
|
mkNixDrvProccess :: String -> TestM CreateProcess
|
||||||
runNixDrv nixDrvExpr = do
|
mkNixDrvProccess nixDrvExpr = do
|
||||||
(exe :: String, drvPath :: FilePath) <-
|
(exe :: String, drvPath :: FilePath) <-
|
||||||
evalNixExpr
|
evalNixExpr
|
||||||
[i| let d = #{nixDrvExpr}; in [(lib.getExe d) d.drvPath] |]
|
[i| let d = #{nixDrvExpr}; in [(lib.getExe d) d.drvPath] |]
|
||||||
void $ buildNixDrvPath drvPath
|
void $ buildNixDrvPath drvPath
|
||||||
env <- asks env
|
env <- asks env
|
||||||
let p = (Process.proc exe []) {Process.env = Just $ Map.toList env}
|
pure $
|
||||||
liftIO $ Process.readCreateProcess p ""
|
(Process.proc exe [])
|
||||||
|
{ Process.env = Just $ Map.toList env,
|
||||||
|
Process.std_in = Process.CreatePipe,
|
||||||
|
Process.std_out = Process.CreatePipe,
|
||||||
|
Process.std_err = Process.CreatePipe
|
||||||
|
}
|
||||||
|
|
||||||
|
runNixDrv :: String -> TestM String
|
||||||
|
runNixDrv = liftIO . flip Process.readCreateProcess "" <=< mkNixDrvProccess
|
||||||
|
|
||||||
|
spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
|
||||||
|
spawnNixDrv = liftIO . createProcess <=< mkNixDrvProccess
|
||||||
|
|
||||||
buildNixDrvPath :: FilePath -> TestM FilePath
|
buildNixDrvPath :: FilePath -> TestM FilePath
|
||||||
buildNixDrvPath drvPath = do
|
buildNixDrvPath drvPath = do
|
||||||
|
|
@ -142,7 +163,27 @@ evalNixExpr nixExpr = do
|
||||||
]
|
]
|
||||||
""
|
""
|
||||||
|
|
||||||
shouldOutput :: String -> String -> TestM ()
|
shouldOutput :: (HasCallStack) => String -> String -> TestM ()
|
||||||
shouldOutput nixDrvExpr expectedOutput = do
|
shouldOutput nixDrvExpr expectedOutput = do
|
||||||
out <- runNixDrv nixDrvExpr
|
out <- runNixDrv nixDrvExpr
|
||||||
liftIO $ out `shouldBe` expectedOutput
|
liftIO $ out `shouldBe` expectedOutput
|
||||||
|
|
||||||
|
shouldReturnTestM :: (HasCallStack, Show a, Eq a) => TestM a -> a -> TestM ()
|
||||||
|
shouldReturnTestM action expected = lifted' (`shouldReturn` expected) action
|
||||||
|
|
||||||
|
shouldEventuallyReturn :: (HasCallStack, Eq a, Show a) => TestM a -> a -> TestM ()
|
||||||
|
shouldEventuallyReturn action expected = go 0
|
||||||
|
where
|
||||||
|
delay = 20_000
|
||||||
|
maxRetries = 5_000_000 `div` delay
|
||||||
|
go :: Int -> TestM ()
|
||||||
|
go retryCount = do
|
||||||
|
result <- action
|
||||||
|
if result == expected
|
||||||
|
then pure ()
|
||||||
|
else do
|
||||||
|
if retryCount > maxRetries
|
||||||
|
then liftIO $ result `shouldBe` expected
|
||||||
|
else do
|
||||||
|
liftIO $ threadDelay delay
|
||||||
|
go $ retryCount + 1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue