Make combinators available at jail.combinators
Instead of requiring the third argument to be a function that passes in combinators, this exposes all combinators on `jail.combinators` and allows the third argument to just be a list (or even null!). This may be more ergonomic in some contexts (like nixpkgs overlays). This isn't a breaking change, the third argument can still be a function and will receive the combinators if it is.
This commit is contained in:
parent
2f6e7c3078
commit
c5ba72dc5a
4 changed files with 110 additions and 98 deletions
|
|
@ -22,7 +22,7 @@ list](https://lists.sr.ht/~alexdavid/general).
|
||||||
ls -la $HOME
|
ls -la $HOME
|
||||||
'';
|
'';
|
||||||
in {
|
in {
|
||||||
packages.x86_64-linux.jailed = jail "my-jail" untrusted-package (combinators: with combinators; [
|
packages.x86_64-linux.jailed = jail "my-jail" untrusted-package (with jail.combinators; [
|
||||||
# See combinators section below for more functions that can go here
|
# See combinators section below for more functions that can go here
|
||||||
|
|
||||||
# Give program access to the network
|
# Give program access to the network
|
||||||
|
|
|
||||||
12
flake.nix
12
flake.nix
|
|
@ -1,16 +1,10 @@
|
||||||
{
|
{
|
||||||
outputs = _: {
|
outputs = _: {
|
||||||
lib = let
|
lib = let
|
||||||
runJail = import ./lib.nix;
|
jailLib = import ./lib.nix;
|
||||||
in {
|
in {
|
||||||
init = pkgs: name: exe: applyCombinators: runJail {
|
init = pkgs: jailLib { inherit pkgs; };
|
||||||
inherit pkgs name exe applyCombinators;
|
extend = jailLib;
|
||||||
};
|
|
||||||
|
|
||||||
extend = { pkgs, additionalCombinators }: name: exe: applyCombinators: runJail {
|
|
||||||
inherit pkgs name exe;
|
|
||||||
applyCombinators = combinators: applyCombinators (combinators // additionalCombinators combinators);
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
179
lib.nix
179
lib.nix
|
|
@ -1,93 +1,110 @@
|
||||||
{ pkgs, name, exe, applyCombinators }: let
|
{ pkgs
|
||||||
|
, additionalCombinators ? _: {}
|
||||||
|
}: let
|
||||||
inherit (pkgs) lib;
|
inherit (pkgs) lib;
|
||||||
|
|
||||||
helpers = rec {
|
helpers = rec {
|
||||||
dataDir = "~/.local/share/jail.nix";
|
dataDir = "~/.local/share/jail.nix";
|
||||||
dataDirSubPath = subPath: "${dataDir}/${combinators.escape subPath}";
|
dataDirSubPath = subPath: "${dataDir}/${builtinCombinators.escape subPath}";
|
||||||
};
|
};
|
||||||
|
|
||||||
combinators = import ./combinators.nix { inherit pkgs lib helpers; };
|
builtinCombinators = import ./combinators.nix { inherit pkgs lib helpers; };
|
||||||
|
|
||||||
initial-state = {
|
allCombinators = builtinCombinators // additionalCombinators builtinCombinators;
|
||||||
name = name;
|
|
||||||
cmd = "${lib.getExe pkgs.bubblewrap}";
|
|
||||||
entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe;
|
|
||||||
path = "${pkgs.coreutils}/bin";
|
|
||||||
argv = "\"$@\"";
|
|
||||||
runtime = "";
|
|
||||||
new-session = true;
|
|
||||||
hostname = "jail";
|
|
||||||
env = {};
|
|
||||||
namespaces = {};
|
|
||||||
included-once = []; # See include-once combinator
|
|
||||||
cleanup = []; # See cleanup combinator
|
|
||||||
};
|
|
||||||
in lib.pipe initial-state (
|
|
||||||
# apply pre-user combinators
|
|
||||||
(with combinators; [
|
|
||||||
(unsafe-add-raw-args "--proc /proc")
|
|
||||||
(unsafe-add-raw-args "--dev /dev")
|
|
||||||
(unsafe-add-raw-args "--tmpfs /tmp")
|
|
||||||
(unsafe-add-raw-args "--tmpfs ~")
|
|
||||||
(unsafe-add-raw-args "--clearenv")
|
|
||||||
(unsafe-add-raw-args "--die-with-parent")
|
|
||||||
(readonly "/nix/store")
|
|
||||||
(readonly "/bin/sh")
|
|
||||||
(fwd-env "LANG")
|
|
||||||
(fwd-env "HOME")
|
|
||||||
(fwd-env "TERM")
|
|
||||||
|
|
||||||
(add-runtime ''
|
in {
|
||||||
if [ ! -e ${helpers.dataDirSubPath "passwd"} ] || [ ! -e ${helpers.dataDirSubPath "group"} ]; then
|
combinators = allCombinators;
|
||||||
mkdir -p ${helpers.dataDir}
|
|
||||||
echo "root:x:0:0:System administrator:/root:$(which nologin)" > ${helpers.dataDirSubPath "passwd"}
|
|
||||||
echo "$(id -un):x:$(id -u):$(id -g)::$HOME:$(which nologin)" >> ${helpers.dataDirSubPath "passwd"}
|
|
||||||
echo "root:x:0:" > ${helpers.dataDirSubPath "group"}
|
|
||||||
echo "$(id -gn):x:$(id -g):" >> ${helpers.dataDirSubPath "group"}
|
|
||||||
fi
|
|
||||||
'')
|
|
||||||
(ro-bind (noescape (helpers.dataDirSubPath "passwd")) "/etc/passwd")
|
|
||||||
(ro-bind (noescape (helpers.dataDirSubPath "group")) "/etc/group")
|
|
||||||
])
|
|
||||||
|
|
||||||
# apply user combinators
|
__functor = _: name: exe: combinatorsToApply: let
|
||||||
++ applyCombinators combinators
|
initialState = {
|
||||||
|
name = name;
|
||||||
|
cmd = "${lib.getExe pkgs.bubblewrap}";
|
||||||
|
entry = if builtins.typeOf exe == "string" then lib.escapeShellArg exe else lib.getExe exe;
|
||||||
|
path = "${pkgs.coreutils}/bin";
|
||||||
|
argv = "\"$@\"";
|
||||||
|
runtime = "";
|
||||||
|
new-session = true;
|
||||||
|
hostname = "jail";
|
||||||
|
env = {};
|
||||||
|
namespaces = {};
|
||||||
|
included-once = []; # See include-once combinator
|
||||||
|
cleanup = []; # See cleanup combinator
|
||||||
|
};
|
||||||
|
|
||||||
# apply post-user combinators
|
userCombinators =
|
||||||
++ (with combinators; [
|
let t = builtins.typeOf combinatorsToApply; in
|
||||||
(s:
|
if t == "lambda" then combinatorsToApply allCombinators else
|
||||||
# See `--unshare-*` in BWRAP(1)
|
if t == "list" then combinatorsToApply else
|
||||||
lib.pipe ["user" "ipc" "pid" "net" "uts" "cgroup"] [
|
if t == "null" then []
|
||||||
(lib.filter (ns: !(s.namespaces.${ns} or false)))
|
else throw "Unknown combinator type ${t}. Must be a function, list, or null";
|
||||||
(map (ns: "--unshare-${ns}"))
|
|
||||||
(builtins.concatStringsSep " ")
|
|
||||||
unsafe-add-raw-args
|
|
||||||
] s
|
|
||||||
)
|
|
||||||
(s: set-env "PATH" s.path s)
|
|
||||||
(s: if s.new-session then unsafe-add-raw-args "--new-session" s else s)
|
|
||||||
(s: lib.foldr (envVar:
|
|
||||||
assert pkgs.lib.isValidPosixName envVar;
|
|
||||||
unsafe-add-raw-args "--setenv ${envVar} ${s.env.${envVar}}"
|
|
||||||
) s (builtins.attrNames s.env))
|
|
||||||
])
|
|
||||||
|
|
||||||
# build jailed app from state
|
in lib.pipe initialState (
|
||||||
++ [
|
# apply pre-user combinators
|
||||||
(state: ''
|
(with builtinCombinators; [
|
||||||
RUNTIME_ARGS=()
|
(unsafe-add-raw-args "--proc /proc")
|
||||||
${if builtins.length state.cleanup > 0 then ''
|
(unsafe-add-raw-args "--dev /dev")
|
||||||
function cleanup {
|
(unsafe-add-raw-args "--tmpfs /tmp")
|
||||||
${lib.concatStringsSep "\n" state.cleanup}
|
(unsafe-add-raw-args "--tmpfs ~")
|
||||||
}
|
(unsafe-add-raw-args "--clearenv")
|
||||||
trap cleanup EXIT
|
(unsafe-add-raw-args "--die-with-parent")
|
||||||
'' else ""}
|
(readonly "/nix/store")
|
||||||
${state.runtime}
|
(readonly "/bin/sh")
|
||||||
${if builtins.length state.cleanup > 0 then "" else "exec "}${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${state.entry} ${state.argv}
|
(fwd-env "LANG")
|
||||||
'')
|
(fwd-env "HOME")
|
||||||
(text: pkgs.writeShellApplication { inherit name text; })
|
(fwd-env "TERM")
|
||||||
|
|
||||||
# forward man pages
|
(add-runtime ''
|
||||||
(jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed)
|
if [ ! -e ${helpers.dataDirSubPath "passwd"} ] || [ ! -e ${helpers.dataDirSubPath "group"} ]; then
|
||||||
]
|
mkdir -p ${helpers.dataDir}
|
||||||
)
|
echo "root:x:0:0:System administrator:/root:$(which nologin)" > ${helpers.dataDirSubPath "passwd"}
|
||||||
|
echo "$(id -un):x:$(id -u):$(id -g)::$HOME:$(which nologin)" >> ${helpers.dataDirSubPath "passwd"}
|
||||||
|
echo "root:x:0:" > ${helpers.dataDirSubPath "group"}
|
||||||
|
echo "$(id -gn):x:$(id -g):" >> ${helpers.dataDirSubPath "group"}
|
||||||
|
fi
|
||||||
|
'')
|
||||||
|
(ro-bind (noescape (helpers.dataDirSubPath "passwd")) "/etc/passwd")
|
||||||
|
(ro-bind (noescape (helpers.dataDirSubPath "group")) "/etc/group")
|
||||||
|
])
|
||||||
|
|
||||||
|
# apply user combinators
|
||||||
|
++ userCombinators
|
||||||
|
|
||||||
|
# apply post-user combinators
|
||||||
|
++ (with builtinCombinators; [
|
||||||
|
(s:
|
||||||
|
# See `--unshare-*` in BWRAP(1)
|
||||||
|
lib.pipe ["user" "ipc" "pid" "net" "uts" "cgroup"] [
|
||||||
|
(lib.filter (ns: !(s.namespaces.${ns} or false)))
|
||||||
|
(map (ns: "--unshare-${ns}"))
|
||||||
|
(builtins.concatStringsSep " ")
|
||||||
|
unsafe-add-raw-args
|
||||||
|
] s
|
||||||
|
)
|
||||||
|
(s: set-env "PATH" s.path s)
|
||||||
|
(s: if s.new-session then unsafe-add-raw-args "--new-session" s else s)
|
||||||
|
(s: lib.foldr (envVar:
|
||||||
|
assert pkgs.lib.isValidPosixName envVar;
|
||||||
|
unsafe-add-raw-args "--setenv ${envVar} ${s.env.${envVar}}"
|
||||||
|
) s (builtins.attrNames s.env))
|
||||||
|
])
|
||||||
|
|
||||||
|
# build jailed app from state
|
||||||
|
++ [
|
||||||
|
(state: ''
|
||||||
|
RUNTIME_ARGS=()
|
||||||
|
${if builtins.length state.cleanup > 0 then ''
|
||||||
|
function cleanup {
|
||||||
|
${lib.concatStringsSep "\n" state.cleanup}
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
'' else ""}
|
||||||
|
${state.runtime}
|
||||||
|
${if builtins.length state.cleanup > 0 then "" else "exec "}${state.cmd} "''${RUNTIME_ARGS[@]}" -- ${state.entry} ${state.argv}
|
||||||
|
'')
|
||||||
|
(text: pkgs.writeShellApplication { inherit name text; })
|
||||||
|
|
||||||
|
# forward man pages
|
||||||
|
(jailed: if exe ? man then jailed // { inherit (exe) man; } else jailed)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,11 +70,12 @@ following positional arguments:
|
||||||
be a derivation like `pkgs.hello`, or a string like
|
be a derivation like `pkgs.hello`, or a string like
|
||||||
`"${pkgs.hello}/bin/hello"`. If a derivation is passed it will call
|
`"${pkgs.hello}/bin/hello"`. If a derivation is passed it will call
|
||||||
`nixpkgs.lib.getExe` on it to get the entrypoint.
|
`nixpkgs.lib.getExe` on it to get the entrypoint.
|
||||||
* **combinators**: (Function returning a list of **Combinators**) - The third
|
* **combinators**: (List of **Combinators**) - The third argument allows you to
|
||||||
argument allows you to control the permissions of the jail. By default, the
|
control the permissions of the jail. By default, the wrapped program runs
|
||||||
wrapped program runs with very few permissions and to get it to even work you
|
with very few permissions and to get it to even work you will need to define
|
||||||
will need to define what you want it to have access to here. For convenience,
|
what you want it to have access to here. For convenience, if this argument is
|
||||||
this is a function that passes all the available combinators.
|
a function, jail.combinators will be passed in, and the return value will be
|
||||||
|
used.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|
@ -97,7 +98,7 @@ your Nixos configurations:
|
||||||
# Now all modules have `jail` passed into them:
|
# Now all modules have `jail` passed into them:
|
||||||
({ jail, ... }: {
|
({ jail, ... }: {
|
||||||
environment.systemPackages = [
|
environment.systemPackages = [
|
||||||
(jail "jailed-hello" pkgs.hello (c: []))
|
(jail "jailed-hello" pkgs.hello [])
|
||||||
];
|
];
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
|
@ -118,7 +119,7 @@ your Nixos configurations:
|
||||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||||
jail = jail-nix.lib.init pkgs;
|
jail = jail-nix.lib.init pkgs;
|
||||||
in {
|
in {
|
||||||
my-jailed-package = jail "jailed-hello" pkgs.hello (c: []);
|
my-jailed-package = jail "jailed-hello" pkgs.hello [];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue