workcell-0.1.2: src/Enter.hs
-- SPDX-License-Identifier: LGPL-2.1-or-later
module Enter (enterSession) where
import Control.Exception (bracket)
import Control.Monad (unless)
import qualified Data.ByteString.Char8 as BS
import SimpleCmd (needProgram)
import System.Directory (findExecutable)
import System.Exit (exitWith)
import System.FilePath ((</>))
import System.IO (hIsTerminalDevice, stdin, stdout)
import System.Posix.Files (readSymbolicLink)
import System.Posix.IO (closeFd)
import System.Posix.Process (getProcessID)
import System.Process (CreateProcess(..), proc)
import Child (waitChild, withChild)
import Sandbox (NetworkMode(..))
import Session (Session(..), SessionDetails(..), findSession, validSession)
import Util (invalid, ownerNamespace, procPath, shellQuote, withDescriptors)
enterSession :: Bool -> Bool -> [String] -> IO ()
enterSession dry root idcmd = do
let (ident,command) =
case idcmd of
[] -> (Nothing,[])
(h:t) -> (Just h,t)
record <- findSession ident
interactive <- (&&) <$> hIsTerminalDevice stdin <*> hIsTerminalDevice stdout
entryCommand <-
let cmd = if null command then ["/bin/bash"] else command in
if interactive then terminalCommand cmd else pure cmd
let pid = sessionPID record
details = sessionDetails record
namespaces = ["mnt"] ++ ["pid" | not $ sessionSharedProcesses details] ++
["net" | sessionNetwork details /= HostNetwork]
flag "mnt" = "--mount"
flag name = "--" ++ name
-- Preserve the mapped host credentials in either mode: setup-namespace
-- UID/GID 0 already maps to the invoking account, and setgroups is denied
-- in unprivileged user namespaces.
tailArgs = ["--target", show pid, "--env", "--preserve-credentials"]
withDescriptors [procPath pid ("ns" </> name) | name <- namespaces] $ \fds ->
case fds of
mount:_ -> bracket (ownerNamespace mount) closeFd $ \owner -> do
supervisor <- getProcessID
let reference fd = "/proc/" ++ show supervisor ++ "/fd/" ++ show fd
ownerUser <- readSymbolicLink $ reference owner
targetUser <- readSymbolicLink $ procPath pid "ns/user"
-- Mount/network entry requires their owning user namespace. Bubblewrap
-- may then use a child user namespace to restore the invoking UID/GID.
-- Join that final namespace only after entering the other namespaces.
finalCommand <-
if root || ownerUser == targetUser
then pure entryCommand
else do
insidePID <-
if sessionSharedProcesses details
then pure (show pid)
else do
status <- lines . BS.unpack <$> BS.readFile (procPath pid "status")
case [inside | line <- status, "NSpid:":ids <- [words line], inside:_ <- [reverse ids]] of
[inside] -> pure inside
_ -> invalid "cannot determine session PID inside its namespace"
nsenter <- findExecutable "nsenter" >>= maybe (invalid "nsenter is required for entry") pure
pure $ [nsenter, "--user=/proc/" ++ insidePID ++ "/ns/user", "--preserve-credentials", "--"] ++ entryCommand
if dry
then putStrLn $ unwords $ map shellQuote $
["nsenter", "--user=" ++ procPath pid "ns/user"] ++
["--user-parent" | ownerUser /= targetUser] ++
[flag name ++ "=" ++ procPath pid ("ns" </> name) | name <- namespaces] ++
tailArgs ++ ["--root", "--wd", "--"] ++ finalCommand
else do
needProgram "nsenter"
withDescriptors [procPath pid "root", procPath pid "cwd"] $ \directories -> do
valid <- validSession record
unless valid $ invalid "session exited while preparing entry"
-- Explicit descriptors avoid nsenter's mixed pidfd/FD join ordering.
let args = ["--user=" ++ reference owner] ++
zipWith (\name fd -> flag name ++ "=" ++ reference fd) namespaces fds ++
tailArgs ++ zipWith (\name fd -> name ++ "=" ++ reference fd)
["--root", "--wd"] directories ++ ["--"] ++ finalCommand
-- nsenter opens the supervisor's descriptors before joining. Its
-- command inherits none of our namespace/root directory handles.
result <- withChild ((proc "nsenter" args)
{delegate_ctlc = True, close_fds = True}) waitChild
exitWith result
_ -> invalid "missing mount namespace descriptor"
-- The caller's terminal belongs to another devpts instance. Allocate the new
-- terminal after nsenter has joined the session, and relay input/output to it.
terminalCommand :: [String] -> IO [String]
terminalCommand command = do
forward <- findExecutable "systemd-pty-forward"
case forward of
Just path -> pure $ [path, "--quiet", "--"] ++ command
Nothing -> do
script <- findExecutable "script"
case script of
Just path -> pure [path, "--quiet", "--return", "--command",
"exec " ++ unwords (map shellQuote command), "/dev/null"]
Nothing -> invalid "interactive entry requires systemd-pty-forward or script (from util-linux)"