Add jail-to-host-channel combinator
This commit is contained in:
parent
7dc202a5a7
commit
f5cc61ef63
2 changed files with 94 additions and 0 deletions
|
|
@ -1042,6 +1042,86 @@ in rec {
|
||||||
;
|
;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
jail-to-host-channel = {
|
||||||
|
sig = "String -> String -> Combinator";
|
||||||
|
doc = ''
|
||||||
|
Allows programs in the jail to execute and pass messages to a
|
||||||
|
specific handler that runs outside of the jail.
|
||||||
|
|
||||||
|
The first parameter specifies a name of a program that is exposed
|
||||||
|
to the jail that, when called, sends its first argument to the
|
||||||
|
script passed to the second parameter. The script runs *outside*
|
||||||
|
of the jail. Any stdout generated by the script is relayed back
|
||||||
|
as stdout from the program in the jail.
|
||||||
|
|
||||||
|
Current limitations:
|
||||||
|
|
||||||
|
* Only a single argument is supported. For more arguments you
|
||||||
|
will need to use an encoding like JSON.
|
||||||
|
* Only stdout is relayed back to the jail, stderr will be
|
||||||
|
visible in your terminal but the jail won't be able to read
|
||||||
|
it.
|
||||||
|
* The first parameter must be a valid POSIX variable name.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```nix
|
||||||
|
jail-to-host-channel "getHostFileSize" ${"''"}
|
||||||
|
# This runs *outside* of the jail
|
||||||
|
if [ -f "$1" ]; then
|
||||||
|
wc -c < "$1"
|
||||||
|
else
|
||||||
|
echo "$1 is not a file on the host"
|
||||||
|
fi
|
||||||
|
${"''"}
|
||||||
|
```
|
||||||
|
|
||||||
|
This exposes a program inside the jail called `getHostFileSize`
|
||||||
|
that returns the size of the file passed in without needing to
|
||||||
|
give the jail read access to any files.
|
||||||
|
'';
|
||||||
|
__functor = _:
|
||||||
|
jailTxName: hostRxHandler:
|
||||||
|
assert pkgs.lib.isValidPosixName jailTxName;
|
||||||
|
let varPrefix = "J2H_CHAN_${pkgs.lib.toUpper jailTxName}"; in
|
||||||
|
include-once "jail-to-host-channel-${jailTxName}"
|
||||||
|
(compose [
|
||||||
|
(add-runtime ''
|
||||||
|
# Set up jail-to-host channel "${jailTxName}"
|
||||||
|
${varPrefix}_TMP=$(mktemp -d)
|
||||||
|
mkfifo "''$${varPrefix}_TMP/fifo"
|
||||||
|
(
|
||||||
|
while true; do
|
||||||
|
MSG=$(<"''$${varPrefix}_TMP/fifo")
|
||||||
|
${pkgs.lib.getExe (pkgs.writeShellApplication {
|
||||||
|
name = "${jailTxName}-host-rx-handler";
|
||||||
|
text = hostRxHandler;
|
||||||
|
})} "$MSG" > "''$${varPrefix}_TMP/fifo" || break
|
||||||
|
done
|
||||||
|
) &
|
||||||
|
${varPrefix}_RX_PID=$!
|
||||||
|
'')
|
||||||
|
(add-cleanup ''
|
||||||
|
kill "''$${varPrefix}_RX_PID"
|
||||||
|
rm -rf "''$${varPrefix}_TMP"
|
||||||
|
'')
|
||||||
|
(ro-bind (noescape "\"\$${varPrefix}_TMP/fifo\"") "/run/j2hc-${jailTxName}")
|
||||||
|
(add-pkg-deps [(pkgs.writeShellApplication {
|
||||||
|
name = jailTxName;
|
||||||
|
runtimeInputs = [ pkgs.util-linux ];
|
||||||
|
text = ''
|
||||||
|
flock /run/j2hc.lock ${pkgs.lib.getExe (pkgs.writeShellApplication {
|
||||||
|
name = "${jailTxName}-locked";
|
||||||
|
text = ''
|
||||||
|
echo "$1" > /run/j2hc-${jailTxName}
|
||||||
|
echo "$(< /run/j2hc-${jailTxName})"
|
||||||
|
'';
|
||||||
|
})} "$1"
|
||||||
|
'';
|
||||||
|
})])
|
||||||
|
])
|
||||||
|
;
|
||||||
|
};
|
||||||
|
|
||||||
############################################
|
############################################
|
||||||
# deprecated
|
# deprecated
|
||||||
persisthome = {
|
persisthome = {
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,20 @@ spec = parallel $ inTestM $ do
|
||||||
|]
|
|]
|
||||||
`shouldOutput` "secret\n"
|
`shouldOutput` "secret\n"
|
||||||
|
|
||||||
|
describe "jail-to-host-channel" $ do
|
||||||
|
it "calls a script on the outside of the jail with an argument and returns its stdout" $ do
|
||||||
|
tmpDir <- getTestDir
|
||||||
|
liftIO $ writeFile (tmpDir </> "secret") "hunter2"
|
||||||
|
[i|
|
||||||
|
jail "test" (sh ''getsha1 "#{tmpDir </> "secret"}"'') (c: [
|
||||||
|
(c.jail-to-host-channel "getsha1" ''
|
||||||
|
echo "this runs outside of the jail"
|
||||||
|
sha1sum < "$1"
|
||||||
|
'')
|
||||||
|
])
|
||||||
|
|]
|
||||||
|
`shouldOutput` "this runs outside of the jail\nf3bbbd66a63d4bf1747940578ec3d0103530e21d -\n"
|
||||||
|
|
||||||
describe "network" $ do
|
describe "network" $ do
|
||||||
it "grants network access to the jailed program" $ do
|
it "grants network access to the jailed program" $ do
|
||||||
out <-
|
out <-
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue