Page MenuHomeFreeBSD

D62026.1795657172.diff
No OneTemporary

Authored By
Unknown
Size
38 KB
Referenced Files
None
Subscribers
None

D62026.1795657172.diff

diff --git a/usr.sbin/Makefile b/usr.sbin/Makefile
--- a/usr.sbin/Makefile
+++ b/usr.sbin/Makefile
@@ -22,6 +22,7 @@
devinfo \
diskinfo \
dumpcis \
+ enjail \
etcupdate \
extattr \
extattrctl \
diff --git a/usr.sbin/enjail/Makefile b/usr.sbin/enjail/Makefile
new file mode 100644
--- /dev/null
+++ b/usr.sbin/enjail/Makefile
@@ -0,0 +1,11 @@
+.include <src.opts.mk>
+
+PACKAGE= rc
+SCRIPTS= enjail
+
+MAN= enjail.8
+
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/enjail/enjail b/usr.sbin/enjail/enjail
new file mode 100755
--- /dev/null
+++ b/usr.sbin/enjail/enjail
@@ -0,0 +1,1133 @@
+#!/bin/sh
+############################################################ LICENSE
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Devin Teske <dteske@FreeBSD.org>
+#
+############################################################ IDENT(1)
+#
+# $Title: Run a command in a temporary sandbox jail $
+# $Copyright: 2026 Devin Teske. All rights reserved. $
+#
+############################################################ INFORMATION
+#
+# Mount a directory read-only as a jail's / and run a command there.
+# A missing command runs the user's shell with -i.
+# -c leaves that sandbox mounted and prints its name.
+# -d removes a sandbox -c created.
+# Host paths (-r, and those -e derives) are mapped at the same path.
+# -w punches an rw,noexec hole through the root for one file or socket.
+# -W does the same without noexec.
+#
+############################################################ CONFIGURATION
+
+#
+# Default tmpfs size. -s replaces it
+#
+TMPFS_SIZE=16m
+
+#
+# Persistent sandbox records. -c writes one; -d reads it.
+#
+STATE_DIR=/var/run/enjail
+
+############################################################ GLOBALS
+
+pgm="${0##*/}" # Program basename
+
+#
+# Global exit status
+#
+SUCCESS=0
+FAILURE=1
+
+#
+# Command-line flags
+#
+ALLOW_CHFLAGS= # -F
+ALLOW_NET= # -N
+ALLOW_RAW= # -R
+ALLOW_SUID= # -I
+ALLOW_SYSV= # -S
+CREATE= # -c
+DESTROY= # -d name
+ELF_CMDS= # -e command | -e path
+HUSER= # -u user
+JUSER= # -U user
+SIZE_SET= # -s was given
+SIZE=$TMPFS_SIZE # -s size
+SKIP_DEVFS= # -D
+SKIP_TMPFS= # -T
+WANT_ELF= # -e was given
+
+#
+# Arguments
+#
+DIR=
+JAIL_ARGS=
+JNAME=
+
+#
+# Miscellaneous
+#
+ELF_MAP_LOADER=
+NL="
+" # END-QUOTE
+RO_PATHS=
+SCRATCH=
+W_PATHS=
+WX_PATHS=
+
+############################################################ FUNCTIONS
+
+# quietly $cmd ...
+#
+# Run a command with stdout and stderr discarded. Status is preserved.
+#
+quietly()
+{
+ "$@" > /dev/null 2>&1
+}
+
+# warn [$fmt [$args ...]]
+#
+# Print a message on stderr, prefixed with the program name.
+#
+warn()
+{
+ local fmt="$1"
+
+ [ "$fmt" ] || return $SUCCESS
+ shift 1 # fmt
+ printf "%s: $fmt\n" "$pgm" "$@" >&2
+}
+
+# die [$fmt [$args ...]]
+#
+# Optionally print a message, then exit with failure.
+#
+die()
+{
+ warn "$@"
+ exit $FAILURE
+}
+
+# usage [$fmt [$args ...]]
+#
+# Print a syntax statement and exit. An optional message comes first.
+#
+usage()
+{
+ local optfmt="\t%-12s %s\n"
+
+ [ $# -gt 0 ] && warn "$@"
+
+ exec >&2
+ printf "Usage: %s [-DFINRST] %s\n" "$pgm" \
+ "[-e command]... [-r hostpath]... [-s size] \\"
+ printf " %*s %s\n" "${#pgm}" "" \
+ "[{-u | -U} user] [{-w | -W} jailpath]... dir [command ...]"
+ printf "Usage: %s [-DFINRST] -c %s\n" "$pgm" \
+ "[-e command]... [-r hostpath]... [-s size] \\"
+ printf " %*s %s\n" "${#pgm}" "" \
+ "[{-u | -U} user] [{-w | -W} jailpath]... dir"
+ printf "Usage: %s -d name\n" "$pgm"
+ printf "Options:\n"
+ printf "$optfmt" "-c" "Create a persistent sandbox."
+ printf "$optfmt" "-d name" "Destroy a persistent sandbox."
+ printf "$optfmt" "-D" "Do not mount devfs."
+ printf "$optfmt" "-e command" "Map a host ELF and its libraries."
+ printf "$optfmt" "-F" "Allow file flags (securelevel 0)."
+ printf "$optfmt" "-h" "Print this usage and exit."
+ printf "$optfmt" "-I" "Allow setuid execution on nullfs."
+ printf "$optfmt" "-N" "Inherit IPv4 and IPv6."
+ printf "$optfmt" "-R" "Allow raw sockets."
+ printf "$optfmt" "-r hostpath" "Map a host path read-only."
+ printf "$optfmt" "-S" "Allow System V IPC."
+ printf "$optfmt" "-s size" "tmpfs size (default 16m)."
+ printf "$optfmt" "-T" "Do not mount tmpfs."
+ printf "$optfmt" "-U user" "Run as user in the jail password database."
+ printf "$optfmt" "-u user" "Run as user in the host password database."
+ printf "$optfmt" "-W jailpath" \
+ "Writable and executable file or socket."
+ printf "$optfmt" "-w jailpath" "Writable file or socket (noexec)."
+ die
+}
+
+# enjail_fstab_vis $string $var_to_set
+#
+# Encode \ as \\, space as \s, and tab as \t for an fstab(5) field.
+# jail(8) splits the mount line before strunvis(3).
+#
+enjail_fstab_vis()
+{
+ local __start="$1" __var_to_set="$2"
+ local __ch __string=
+
+ while [ "$__start" ]; do
+ __ch=${__start%"${__start#?}"}
+ __start=${__start#?}
+ case "$__ch" in
+ '\') __string="$__string\\\\" ;;
+ " ") __string="$__string\\s" ;;
+ $'\t') __string="$__string\\t" ;;
+ *) __string="$__string$__ch"
+ esac
+ done
+ setvar "$__var_to_set" "$__string"
+}
+
+# enjail_check_ws $path
+#
+# Space and tab can be vis-encoded. Any other whitespace cannot.
+#
+enjail_check_ws()
+{
+ local path="$1"
+ local vis
+
+ vis="$path"
+ case "$path" in
+ *" "*|*$'\t'*) enjail_fstab_vis "$path" vis
+ esac
+ case "$vis" in
+ *[[:space:]]*) die "%s: unsupported whitespace in pathname" "$path"
+ esac
+}
+
+# enjail_check_path $path
+#
+# Refuse whitespace jail(8) cannot keep in one field, and a path
+# readlink -f does not print unchanged. A missing path returns success
+# so the caller can say why.
+#
+enjail_check_path()
+{
+ local path="$1"
+ local real
+
+ enjail_check_ws "$path"
+ real=$( readlink -f "$path" ) || [ "$real" ] || return $SUCCESS
+ [ "$real" = "$path" ] || die "%s: resolves to %s" "$path" "$real"
+}
+
+# enjail_require_abs $path
+#
+# Die unless $path is absolute.
+#
+enjail_require_abs()
+{
+ local path="$1"
+
+ case "$path" in
+ /*) ;;
+ *) die "%s: not an absolute pathname" "$path"
+ esac
+}
+
+# enjail_is_inside $path $root
+#
+# Success when $path is $root or a path under it.
+#
+enjail_is_inside()
+{
+ local path="$1" root="$2"
+ local prefix
+
+ [ "$path" = "$root" ] && return $SUCCESS
+ prefix="${root%/}/"
+ case "$path/" in
+ "$prefix"*) return $SUCCESS ;;
+ esac
+ return $FAILURE
+}
+
+# enjail_umount $mountpoint
+#
+# Unmount when the path is a mount point.
+#
+enjail_umount()
+{
+ local mp="$1"
+
+ quietly mountpoint -q "$mp" || return $SUCCESS
+ umount "$mp"
+}
+
+# enjail_add_ro $hostpath
+#
+# Append a host path to the read-only list, once.
+#
+enjail_add_ro()
+{
+ local path="$1"
+
+ case "$NL$RO_PATHS$NL" in
+ *"$NL$path$NL"*) return $SUCCESS ;;
+ esac
+ RO_PATHS="$RO_PATHS$NL$path"
+}
+
+# enjail_same_type $host $injail
+#
+# Both paths must be a directory, a regular file, or a socket, and the
+# same kind. nullfs requires that.
+#
+enjail_same_type()
+{
+ local host="$1" injail="$2"
+
+ if [ -d "$host" ]; then
+ [ -d "$injail" ] ||
+ die "%s: not a directory in the jail root" "$host"
+ return $SUCCESS
+ fi
+ if [ -f "$host" ]; then
+ [ -f "$injail" ] ||
+ die "%s: not a regular file in the jail root" "$host"
+ return $SUCCESS
+ fi
+ if [ -S "$host" ]; then
+ [ -S "$injail" ] ||
+ die "%s: not a socket in the jail root" "$host"
+ return $SUCCESS
+ fi
+ die "%s: not a directory, file, or socket" "$host"
+}
+
+# enjail_elf_type $elf
+#
+# Print EXEC or DYN. Empty for any other ELF type.
+#
+enjail_elf_type()
+{
+ readelf -h "$1" | awk '
+ $1 == "Type:" {
+ if ($2 == "EXEC" || $2 == "DYN")
+ print $2
+ exit
+ }
+ '
+}
+
+# enjail_interp $elf
+#
+# Print the PT_INTERP path, or nothing when the file is static.
+# NB: -W is for cross-platform compatibilty with GNU readelf(1)
+#
+enjail_interp()
+{
+ readelf -Wl "$1" | awk '
+ /Requesting program interpreter:/ {
+ sub(/^.*interpreter: /, "")
+ sub(/\]$/, "")
+ print
+ exit
+ }
+ '
+}
+
+# enjail_ldd $elf
+#
+# Print absolute library paths ldd(1) resolved. A missing library is
+# a failure. A static file does not reach here.
+#
+enjail_ldd()
+{
+ local elf="$1"
+
+ ldd "$elf" 2>&1 | awk '
+ /=>/ {
+ p = ""
+ for (i = 1; i <= NF; i++) {
+ if ($i == "=>") {
+ p = $(i + 1)
+ break
+ }
+ }
+ if (p == "not") exit 2
+ if (p ~ "^/") print p
+ }
+ ' || die "%s: library not found" "$elf"
+}
+
+# enjail_elf_paths $command
+#
+# Resolve a host command and print the ELF. No PT_INTERP means the
+# file alone. Otherwise add its interpreter and ldd(1) libraries.
+#
+enjail_elf_paths()
+{
+ local cmd="$1"
+ local path interp kind
+
+ case "$cmd" in
+ */*) path="$cmd" ;;
+ *) path=$( command -v "$cmd" ) || die "%s: not found" "$cmd"
+ esac
+ case "$path" in
+ /*) ;;
+ *) die "%s: not found" "$cmd"
+ esac
+ [ -f "$path" ] || die "%s: not a regular file" "$path"
+ quietly readelf -h "$path" || die "%s: not an ELF file" "$path"
+ kind=$( enjail_elf_type "$path" )
+ [ "$kind" ] || die "%s: not ET_EXEC or ET_DYN" "$path"
+ printf '%s\n' "$path"
+ interp=$( enjail_interp "$path" )
+ if [ "$interp" ]; then
+ ELF_MAP_LOADER=1
+ printf '%s\n' "$interp"
+ enjail_ldd "$path"
+ fi
+}
+
+# enjail_overlay_loader
+#
+# Cover the jail's libmap and hints when a dynamic -e file is mapped.
+#
+enjail_overlay_loader()
+{
+ local extra injail
+
+ for extra in /etc/libmap.conf /var/run/ld-elf.so.hints; do
+ injail="${DIR%/}$extra"
+ [ -e "$injail" ] || continue
+ [ -e "$extra" ] || die "%s: not on the host" "$extra"
+ enjail_add_ro "$extra"
+ done
+}
+
+# enjail_expand_elf
+#
+# Turn each -e command into read-only host paths
+#
+enjail_expand_elf()
+{
+ local cmd list path
+ local oldifs noglob=
+
+ [ "$WANT_ELF" ] || return $SUCCESS
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ for cmd in $ELF_CMDS; do
+ list=$( enjail_elf_paths "$cmd" ) || exit
+ for path in $list; do
+ enjail_add_ro "$path"
+ done
+ done
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+ [ "$ELF_MAP_LOADER" ] && enjail_overlay_loader
+}
+
+# enjail_check_ro
+#
+# Each read-only host path must sit outside the jail root.
+# A regular file may be missing from the jail when its parent
+# directory is there. Anything else must already exist and match.
+#
+enjail_check_ro()
+{
+ local path injail parent pdir
+ local oldifs noglob=
+
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ for path in $RO_PATHS; do
+ enjail_require_abs "$path"
+ if enjail_is_inside "$path" "$DIR"; then
+ die "%s: inside the jail root" "$path"
+ fi
+ enjail_check_path "$path"
+ injail="${DIR%/}$path"
+ if [ -f "$path" ]; then
+ case "$path" in
+ /*/*) parent="${path%/*}" ;;
+ /*) parent=/ ;;
+ esac
+ if [ "$parent" = / ]; then
+ pdir="${DIR%/}"
+ [ "$pdir" ] || pdir=/
+ else
+ pdir="${DIR%/}$parent"
+ fi
+ enjail_check_path "$pdir"
+ [ -d "$pdir" ] ||
+ die "%s: not a directory" "$parent"
+ if [ -e "$injail" ]; then
+ enjail_check_path "$injail"
+ enjail_same_type "$path" "$injail"
+ fi
+ else
+ enjail_check_path "$injail"
+ [ -e "$injail" ] ||
+ die "%s: not present in the jail root" \
+ "$path"
+ enjail_same_type "$path" "$injail"
+ fi
+ done
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+}
+
+# enjail_check_w $list
+#
+# Each in-jail path must be a regular file or a socket under the root
+#
+enjail_check_w()
+{
+ local list="$1"
+ local path src
+ local oldifs noglob=
+
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ for path in $list; do
+ enjail_require_abs "$path"
+ src="${DIR%/}$path"
+ enjail_check_path "$src"
+ [ -f "$src" -o -S "$src" ] ||
+ die "%s: not a regular file or socket" "$path"
+ done
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+}
+
+# enjail_arg $argument
+#
+# Append one jail(8) parameter. The value must not contain a newline
+#
+enjail_arg()
+{
+ JAIL_ARGS="$JAIL_ARGS$NL$1"
+}
+
+# enjail_mount $source $mountpoint $opts
+#
+# Append one nullfs mount= parameter. Space and tab in a path are
+# vis-encoded so jail(8) keeps the field whole.
+#
+enjail_mount()
+{
+ local source="$1" mnt="$2" opts="$3"
+ local spec vis_mnt
+
+ if [ "$ALLOW_SUID" ]; then
+ opts="$opts,suid"
+ else
+ opts="$opts,nosuid"
+ fi
+ enjail_fstab_vis "$source" spec
+ enjail_fstab_vis "$mnt" vis_mnt
+ enjail_arg "mount=$spec $vis_mnt nullfs $opts"
+}
+
+# enjail_mount_list $list $opts $where
+#
+# $where is host (the path is the source) or jail (the source is the
+# same path under the jail root). The mount point is the scratch path.
+#
+enjail_mount_list()
+{
+ local list="$1" opts="$2" where="$3"
+ local path spec_path use_opts
+ local oldifs noglob=
+
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ for path in $list; do
+ if [ "$where" = host ]; then
+ spec_path="$path"
+ else
+ spec_path="${DIR%/}$path"
+ fi
+ use_opts=$opts
+ if [ "$where" = host ] && [ -f "$path" ]; then
+ use_opts=ro,ephemeral
+ fi
+ enjail_mount "$spec_path" "$SCRATCH$path" "$use_opts"
+ done
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+}
+
+# enjail_umount_rev $list
+#
+# Unmount scratch copies of the listed paths, last mounted first.
+# Failure is remembered; the rest are still unmounted.
+#
+enjail_umount_rev()
+{
+ local list="$1"
+ local path rev failed
+ local oldifs noglob=
+
+ failed=
+ rev=
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ for path in $list; do
+ rev="$path$NL$rev"
+ done
+ for path in $rev; do
+ enjail_umount "$SCRATCH$path" || failed=1
+ done
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+ [ ! "$failed" ]
+}
+
+# enjail_have_inet6
+#
+# Success when the kernel publishes kern.features.inet6.
+#
+enjail_have_inet6()
+{
+ quietly sysctl -N kern.features.inet6
+}
+
+# enjail_ip $mode
+#
+# Pass ip4=$mode. Pass ip6=$mode when the kernel has IPv6.
+#
+enjail_ip()
+{
+ enjail_arg "ip4=$1"
+ enjail_have_inet6 && enjail_arg "ip6=$1"
+}
+
+# enjail_prepare
+#
+# Fill JAIL_ARGS with the sandbox mounts and restrictions.
+#
+enjail_prepare()
+{
+ local mnt tmpfs_mnt
+
+ enjail_arg "path=$SCRATCH"
+ enjail_arg "host.hostname=enjail"
+ enjail_mount "$DIR" "$SCRATCH" ro
+ enjail_mount_list "$RO_PATHS" ro host
+ enjail_mount_list "$W_PATHS" "rw,noexec" jail
+ enjail_mount_list "$WX_PATHS" rw jail
+ if [ ! "$SKIP_TMPFS" ]; then
+ enjail_fstab_vis "$SCRATCH/tmp" mnt
+ tmpfs_mnt="mount=tmpfs $mnt tmpfs"
+ tmpfs_mnt="$tmpfs_mnt rw,noexec,mode=1777,size=$SIZE"
+ enjail_arg "$tmpfs_mnt"
+ fi
+ if [ "$ALLOW_NET" ]; then
+ enjail_ip inherit
+ else
+ enjail_ip disable
+ fi
+ if [ "$ALLOW_CHFLAGS" ]; then
+ enjail_arg "securelevel=0"
+ enjail_arg "allow.chflags"
+ else
+ enjail_arg "securelevel=3"
+ enjail_arg "allow.nochflags"
+ fi
+ if [ "$ALLOW_RAW" ]; then
+ enjail_arg "allow.raw_sockets"
+ else
+ enjail_arg "allow.noraw_sockets"
+ fi
+ if [ ! "$ALLOW_SYSV" ]; then
+ enjail_arg "sysvmsg=disable"
+ enjail_arg "sysvsem=disable"
+ enjail_arg "sysvshm=disable"
+ fi
+ enjail_arg "enforce_statfs=2"
+ enjail_arg "children.max=0"
+ enjail_arg "allow.nomount"
+ enjail_arg "allow.noquotas"
+ enjail_arg "allow.noread_msgbuf"
+ enjail_arg "allow.nounprivileged_proc_debug"
+ [ "$SKIP_DEVFS" ] || enjail_arg "mount.devfs"
+ enjail_arg "exec.clean"
+ if [ "$HUSER" ]; then
+ enjail_arg "exec.system_jail_user"
+ enjail_arg "exec.jail_user=$HUSER"
+ elif [ "$JUSER" ]; then
+ enjail_arg "exec.jail_user=$JUSER"
+ fi
+}
+
+# enjail_do_jail $word ...
+#
+# Run jail -c with JAIL_ARGS and any extra words.
+#
+enjail_do_jail()
+{
+ local oldifs status noglob=
+
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ env - /usr/sbin/jail -c $JAIL_ARGS "$@"
+ status=$?
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+ return $status
+}
+
+# enjail_cleanup
+#
+# Unmount the scratch stack and remove the scratch directory.
+# The directory is removed only when every unmount succeeds.
+#
+enjail_cleanup()
+{
+ local clean
+
+ clean=$SUCCESS
+ [ "$SKIP_DEVFS" ] ||
+ enjail_umount "$SCRATCH/dev" || clean=$FAILURE
+ [ "$SKIP_TMPFS" ] ||
+ enjail_umount "$SCRATCH/tmp" || clean=$FAILURE
+ enjail_umount_rev "$WX_PATHS" || clean=$FAILURE
+ enjail_umount_rev "$W_PATHS" || clean=$FAILURE
+ enjail_umount_rev "$RO_PATHS" || clean=$FAILURE
+ enjail_umount "$SCRATCH" || clean=$FAILURE
+ if [ "$clean" -eq $SUCCESS ]; then
+ rmdir "$SCRATCH" || clean=$FAILURE
+ fi
+ [ "$clean" -eq $SUCCESS ] ||
+ warn "%s: scratch directory left mounted" "$SCRATCH"
+ return $clean
+}
+
+# enjail_run $command ...
+#
+# Mount, run the command with our stdin, then unmount
+#
+enjail_run()
+{
+ local status clean
+
+ enjail_prepare
+ enjail_arg "nopersist"
+ enjail_do_jail command "$@"
+ status=$?
+ enjail_cleanup
+ clean=$?
+ [ "$status" -eq $SUCCESS ] || return $status
+ return $clean
+}
+
+# enjail_new_name
+#
+# Set JNAME to enjail_<pid>_<8 hex chars>. Jail names cannot contain
+# a dot.
+#
+enjail_new_name()
+{
+ local hex= byte raw
+
+ raw=$( od -An -N4 -tx1 /dev/urandom ) || {
+ warn "cannot read /dev/urandom"
+ return $FAILURE
+ }
+ for byte in $raw; do
+ case "$byte" in
+ [0-9a-f][0-9a-f]) hex="$hex$byte" ;;
+ *) warn "cannot read /dev/urandom"
+ return $FAILURE
+ esac
+ done
+ case "$hex" in
+ [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])
+ ;;
+ *) warn "cannot read /dev/urandom"
+ return $FAILURE
+ esac
+ JNAME="enjail_$$_$hex"
+}
+
+# enjail_state_dir
+#
+# Create the record directory. A symlink is refused.
+#
+enjail_state_dir()
+{
+ if [ -L "$STATE_DIR" ]; then
+ warn "%s: is a symbolic link" "$STATE_DIR"
+ return $FAILURE
+ fi
+ if [ -d "$STATE_DIR" ]; then
+ return $SUCCESS
+ fi
+ if [ -e "$STATE_DIR" ]; then
+ warn "%s: not a directory" "$STATE_DIR"
+ return $FAILURE
+ fi
+ mkdir -m 0755 "$STATE_DIR" || return $FAILURE
+}
+
+# enjail_state_emit $list
+#
+# Print $SCRATCH plus each path, last mounted first.
+#
+enjail_state_emit()
+{
+ local list="$1"
+ local path rev
+ local oldifs noglob=
+
+ rev=
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ for path in $list; do
+ [ "$path" ] || continue
+ rev="$SCRATCH$path$NL$rev"
+ done
+ for path in $rev; do
+ [ "$path" ] || continue
+ printf '%s\n' "$path"
+ done
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+}
+
+# enjail_state_write $file
+#
+# Record the scratch directory and the unmount list. The first line
+# is removed with rmdir. The rest are unmounted in the order written.
+#
+enjail_state_write()
+{
+ local file="$1"
+ local mask tmp status
+
+ enjail_state_dir || return $FAILURE
+ if [ -e "$file" ] || [ -L "$file" ]; then
+ warn "%s: already exists" "$file"
+ return $FAILURE
+ fi
+ tmp="$file.new"
+ mask=$( umask )
+ umask 077
+ {
+ printf '%s\n' "$SCRATCH"
+ [ "$SKIP_DEVFS" ] || printf '%s\n' "$SCRATCH/dev"
+ [ "$SKIP_TMPFS" ] || printf '%s\n' "$SCRATCH/tmp"
+ enjail_state_emit "$WX_PATHS"
+ enjail_state_emit "$W_PATHS"
+ enjail_state_emit "$RO_PATHS"
+ printf '%s\n' "$SCRATCH"
+ } > "$tmp"
+ status=$?
+ umask "$mask"
+ if [ "$status" -ne $SUCCESS ]; then
+ rm -f "$tmp"
+ return $FAILURE
+ fi
+ chmod 0600 "$tmp" || {
+ rm -f "$tmp"
+ return $FAILURE
+ }
+ mv "$tmp" "$file" || {
+ rm -f "$tmp"
+ return $FAILURE
+ }
+ return $SUCCESS
+}
+
+# enjail_create
+#
+# Mount a persistent sandbox, record it, and print its name.
+#
+enjail_create()
+{
+ enjail_prepare
+ if ! enjail_new_name; then
+ rmdir "$SCRATCH" || warn "%s: not removed" "$SCRATCH"
+ return $FAILURE
+ fi
+ enjail_arg "name=$JNAME"
+ enjail_arg "persist"
+ if ! enjail_do_jail; then
+ enjail_cleanup
+ return $FAILURE
+ fi
+ if ! enjail_state_write "$STATE_DIR/$JNAME"; then
+ jail -r "$JNAME" || exit
+ enjail_cleanup
+ return $FAILURE
+ fi
+ printf '%s\n' "$JNAME"
+ return $SUCCESS
+}
+
+# enjail_name_ok $name
+#
+# Success when $name is enjail_<pid>_<8 hex chars>.
+#
+enjail_name_ok()
+{
+ local name="$1"
+ local pid hex
+
+ pid=${name#enjail_}
+ pid=${pid%%_*}
+ hex=${name##*_}
+ case "$pid" in
+ ""|*[!0-9]*) return $FAILURE ;;
+ esac
+ case "$hex" in
+ [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])
+ ;;
+ *) return $FAILURE
+ esac
+ [ "$name" = "enjail_${pid}_$hex" ]
+}
+
+# enjail_path_under $path $root
+#
+# Success when $path is $root or a path under it, with no . or ..
+# component.
+#
+enjail_path_under()
+{
+ local path="$1" root="$2"
+ local rest part
+
+ enjail_is_inside "$path" "$root" || return $FAILURE
+ rest=${path#/}
+ while [ "$rest" ]; do
+ part=${rest%%/*}
+ case "$part" in
+ .|..) return $FAILURE ;;
+ esac
+ [ "$part" = "$rest" ] && break
+ rest=${rest#*/}
+ done
+ return $SUCCESS
+}
+
+# enjail_state_ok $file
+#
+# The record is a root-owned mode 0600 regular file, not a symlink.
+#
+enjail_state_ok()
+{
+ local file="$1"
+ local st uid mode
+
+ if [ -L "$file" ]; then
+ die "%s: is a symbolic link" "$file"
+ fi
+ [ -f "$file" ] || die "%s: not found" "$file"
+ st=$( stat -f '%u %Lp' "$file" ) || die "%s: not found" "$file"
+ uid=${st%% *}
+ mode=${st#* }
+ [ "$uid" = 0 ] || die "%s: not owned by root" "$file"
+ case "$mode" in
+ 600|0600) ;;
+ *) die "%s: mode is not 0600" "$file"
+ esac
+}
+
+# enjail_destroy $name
+#
+# Remove a sandbox created by -c. A busy jail is left mounted.
+#
+enjail_destroy()
+{
+ local name="$1"
+ local file scratch line path mounts
+ local clean oldifs noglob=
+
+ enjail_name_ok "$name" || die "%s: not an enjail name" "$name"
+ file="$STATE_DIR/$name"
+ enjail_state_ok "$file"
+ scratch=
+ mounts=
+ while IFS= read -r line; do
+ enjail_require_abs "$line"
+ if [ ! "$scratch" ]; then
+ scratch=$line
+ [ "$scratch" = / ] &&
+ die "%s: not a scratch directory" "$scratch"
+ enjail_path_under "$scratch" "$scratch" ||
+ die "%s: not a scratch directory" "$scratch"
+ continue
+ fi
+ enjail_path_under "$line" "$scratch" ||
+ die "%s: not under %s" "$line" "$scratch"
+ mounts="$mounts$NL$line"
+ done < "$file"
+ [ "$scratch" ] || die "%s: empty" "$file"
+ if quietly jls -j "$name" jid; then
+ jail -r "$name" || exit
+ fi
+ clean=$SUCCESS
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+ oldifs=$IFS
+ set -f
+ IFS=$NL
+ for path in $mounts; do
+ [ "$path" ] || continue
+ enjail_umount "$path" || clean=$FAILURE
+ done
+ IFS=$oldifs
+ [ "$noglob" ] || set +f
+ if [ "$clean" -eq $SUCCESS ] && [ -d "$scratch" ]; then
+ rmdir "$scratch" || clean=$FAILURE
+ elif [ "$clean" -eq $SUCCESS ] && [ -e "$scratch" ]; then
+ warn "%s: not a directory" "$scratch"
+ clean=$FAILURE
+ fi
+ if [ "$clean" -eq $SUCCESS ]; then
+ rm -f "$file" || clean=$FAILURE
+ else
+ warn "%s: scratch directory left mounted" "$scratch"
+ fi
+ return $clean
+}
+
+############################################################ MAIN
+
+SIZE=$TMPFS_SIZE
+while getopts cd:De:FhINr:Rs:STU:u:W:w: flag; do
+ case "$flag" in
+ c) CREATE=1 ;;
+ d) enjail_check_ws "$OPTARG"
+ DESTROY="$OPTARG" ;;
+ D) SKIP_DEVFS=1 ;;
+ e) enjail_check_ws "$OPTARG"
+ WANT_ELF=1
+ ELF_CMDS="$ELF_CMDS$NL$OPTARG" ;;
+ F) ALLOW_CHFLAGS=1 ;;
+ h) usage ;;
+ I) ALLOW_SUID=1 ;;
+ N) ALLOW_NET=1 ;;
+ R) ALLOW_RAW=1 ;;
+ r) enjail_check_ws "$OPTARG"
+ RO_PATHS="$RO_PATHS$NL$OPTARG" ;;
+ s) SIZE="$OPTARG" SIZE_SET=1 ;;
+ S) ALLOW_SYSV=1 ;;
+ T) SKIP_TMPFS=1 ;;
+ U) JUSER="$OPTARG" ;;
+ u) HUSER="$OPTARG" ;;
+ W) enjail_check_ws "$OPTARG"
+ WX_PATHS="$WX_PATHS$NL$OPTARG" ;;
+ w) enjail_check_ws "$OPTARG"
+ W_PATHS="$W_PATHS$NL$OPTARG" ;;
+ *) usage
+ esac
+done
+shift $(( $OPTIND - 1 ))
+
+if [ "$DESTROY" ]; then
+ [ "$CREATE" ] && die "-c and -d are mutually exclusive"
+ [ "$ALLOW_NET$ALLOW_SYSV$WANT_ELF$SIZE_SET" ] &&
+ die "-d does not take other options"
+ [ "$ALLOW_CHFLAGS$ALLOW_SUID$ALLOW_RAW" ] &&
+ die "-d does not take other options"
+ [ "$SKIP_DEVFS$SKIP_TMPFS$HUSER$JUSER" ] &&
+ die "-d does not take other options"
+ [ "$RO_PATHS$W_PATHS$WX_PATHS" ] &&
+ die "-d does not take other options"
+ [ $# -eq 0 ] || die "-d does not take a jail root"
+ enjail_destroy "$DESTROY"
+ exit
+fi
+
+[ "$ALLOW_RAW" ] && [ ! "$ALLOW_NET" ] &&
+ die "-R requires -N"
+[ "$SIZE_SET" -a "$SKIP_TMPFS" ] &&
+ die "-s and -T are mutually exclusive"
+[ "$HUSER" -a "$JUSER" ] &&
+ die "-u and -U are mutually exclusive"
+case "$SIZE" in
+*[!0-9KkMmGgTt%]*) die "%s: invalid tmpfs size" "$SIZE" ;;
+*[0-9]*) ;;
+*) die "%s: invalid tmpfs size" "$SIZE"
+esac
+
+[ $# -ge 1 ] || usage "no jail root"
+DIR=$1
+shift # DIR
+if [ "$CREATE" ]; then
+ [ $# -eq 0 ] || die "-c does not take a command"
+elif [ $# -eq 0 ]; then
+ shell=${SHELL:-/bin/sh}
+ set -- "$shell" -i
+fi
+
+enjail_require_abs "$DIR"
+while :; do
+ case "$DIR" in
+ /) break ;;
+ */) DIR="${DIR%/}" ;;
+ *) break
+ esac
+done
+enjail_check_path "$DIR"
+[ -d "$DIR" ] || die "%s: not a directory" "$DIR"
+
+enjail_expand_elf
+enjail_check_ro
+enjail_check_w "$W_PATHS"
+enjail_check_w "$WX_PATHS"
+
+if [ ! "$SKIP_TMPFS" ]; then
+ enjail_check_path "${DIR%/}/tmp"
+ [ -d "${DIR%/}/tmp" ] ||
+ die "%s: not a directory" "${DIR%/}/tmp"
+fi
+if [ ! "$SKIP_DEVFS" ]; then
+ enjail_check_path "${DIR%/}/dev"
+ [ -d "${DIR%/}/dev" ] ||
+ die "%s: not a directory" "${DIR%/}/dev"
+fi
+
+SCRATCH=$( mktemp -d -t enjail ) || die "cannot create scratch directory"
+if enjail_is_inside "$SCRATCH" "$DIR"; then
+ rmdir "$SCRATCH" || warn "%s: not removed" "$SCRATCH"
+ die "%s: contains the scratch directory" "$DIR"
+fi
+
+if [ "$CREATE" ]; then
+ enjail_create
+else
+ enjail_run "$@"
+fi
+exit
+
+################################################################################
+# END
+################################################################################
diff --git a/usr.sbin/enjail/enjail.8 b/usr.sbin/enjail/enjail.8
new file mode 100644
--- /dev/null
+++ b/usr.sbin/enjail/enjail.8
@@ -0,0 +1,385 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2026 Devin Teske
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.Dd November 24, 2026
+.Dt ENJAIL 8
+.Os
+.Sh NAME
+.Nm enjail
+.Nd run a command in a temporary sandbox jail
+.Sh SYNOPSIS
+.Nm
+.Op Fl DFINRST
+.Oo
+.Fl e Ar command
+.Oc Ns ...
+.Oo
+.Fl r Ar hostpath
+.Oc Ns ...
+.Op Fl s Ar size
+.Oo
+.Bro Fl u | Fl U Brc
+.Ar user
+.Oc
+.Oo
+.Bro Fl w | Fl W Brc
+.Ar jailpath
+.Oc Ns ...
+.Ar dir
+.Op Ar command ...
+.Nm
+.Op Fl DFINRST
+.Fl c
+.Oo
+.Fl e Ar command
+.Oc Ns ...
+.Oo
+.Fl r Ar hostpath
+.Oc Ns ...
+.Op Fl s Ar size
+.Oo
+.Bro Fl u | Fl U Brc
+.Ar user
+.Oc
+.Oo
+.Bro Fl w | Fl W Brc
+.Ar jailpath
+.Oc Ns ...
+.Ar dir
+.Nm
+.Fl d Ar name
+.Sh DESCRIPTION
+.Nm
+mounts
+.Ar dir
+read-only with
+.Xr nullfs 4
+on a temporary directory and runs
+.Ar command
+there.
+That directory is the jail's
+.Pa / .
+If
+.Ar command
+is not specified then the user's shell is used.
+The shell is
+.Ev SHELL ,
+or
+.Pa /bin/sh
+when
+.Ev SHELL
+is unset, and it is invoked with
+.Ql -i .
+That pathname must exist in the jail.
+IPv4 is passed
+.Ql ip4=disable
+unless
+.Fl N
+is given, which passes
+.Ql ip4=inherit .
+IPv6 is passed the same way when
+.Xr sysctl 8
+.Va kern.features.inet6
+is present.
+System V IPC is disabled unless
+.Fl S
+is given.
+Every sandbox is passed
+.Ql enforce_statfs=2 ,
+.Ql children.max=0 ,
+.Ql allow.nomount ,
+.Ql allow.noquotas ,
+.Ql allow.noread_msgbuf ,
+and
+.Ql allow.nounprivileged_proc_debug .
+The command's standard input is the standard input of
+.Nm .
+When the command exits,
+.Nm
+unmounts what
+.Xr jail 8
+left behind and removes the temporary directory.
+.Fl c
+creates the same sandbox, prints its name, and leaves it mounted
+for a later
+.Xr jexec 8
+or
+.Ql "sysrc -j" .
+.Fl d
+removes a sandbox that
+.Nm
+created.
+The jail root is not modified.
+A command is still required to exist in the jail for the one-shot form.
+.Pp
+A path is refused when
+.Xr readlink 1
+.Fl f
+does not print it unchanged.
+Space and tab in a path are encoded for
+.Xr jail 8 ;
+any other whitespace is refused.
+.Pp
+The options are as follows:
+.Bl -tag -width ".Fl W Ar jailpath"
+.It Fl c
+Create the sandbox and print its name on standard output.
+No command is run, and the jail stays mounted.
+The name is
+.Ql enjail_
+followed by the process id, an underscore, and eight hexadecimal
+digits.
+Remove it later with
+.Fl d .
+.Fl u
+and
+.Fl U
+still set
+.Va exec.jail_user .
+A later
+.Xr jexec 8
+chooses its own user.
+.It Fl d Ar name
+Remove a sandbox created by
+.Fl c .
+.Ar name
+must be a name
+.Nm
+generated.
+The recorded mounts are unmounted and the temporary directory is
+removed.
+.Ar dir
+is not modified.
+If a command is still running in the jail, the jail is left mounted
+so
+.Fl d
+can be tried again.
+.It Fl D
+Do not mount
+.Xr devfs 8 .
+Without
+.Fl D ,
+.Ar dir
+must contain
+.Pa /dev
+as a directory.
+.It Fl e Ar command
+Find
+.Ar command
+on the host
+.Ev PATH ,
+or use it as a path when it contains a slash.
+It must be an ELF executable
+.Pq Dv ET_EXEC
+or shared object
+.Pq Dv ET_DYN .
+The file is mounted read-only at the same absolute path inside the jail.
+When the file has an interpreter, that interpreter and every library
+.Xr ldd 1
+lists are mounted the same way.
+A file with no interpreter is static.
+.Nm
+mounts that file and does not run
+.Xr ldd 1 .
+When a dynamic file is mapped and
+.Ar dir
+has
+.Pa /etc/libmap.conf
+or
+.Pa /var/run/ld-elf.so.hints ,
+the host's copy of that file is mounted over it.
+The command to run is still the operand after
+.Ar dir .
+Repeatable.
+.Pp
+A regular file may be missing from
+.Ar dir
+when its parent directory already exists.
+The name is not created in
+.Ar dir .
+A directory or a socket must already exist and be the same type.
+A hints or libmap entry can still name some other file in the jail.
+.It Fl F
+Set
+.Va securelevel
+to 0 and pass
+.Ql allow.chflags .
+Without
+.Fl F ,
+.Va securelevel
+is 3 and
+.Ql allow.nochflags
+is passed.
+A jail is never given a lower securelevel than the host.
+.It Fl I
+Pass
+.Ql suid
+on each
+.Xr nullfs 4
+mount.
+Without
+.Fl I ,
+each of those mounts is
+.Ql nosuid .
+.It Fl N
+Pass
+.Ql ip4=inherit .
+Without
+.Fl N ,
+.Ql ip4=disable
+is passed.
+When
+.Va kern.features.inet6
+is present, the same value is passed for IPv6.
+.It Fl R
+Pass
+.Ql allow.raw_sockets .
+Without
+.Fl R ,
+.Ql allow.noraw_sockets
+is passed.
+Requires
+.Fl N .
+.It Fl r Ar hostpath
+Mount a host directory, regular file, or socket read-only at the same
+absolute path inside the jail.
+The path must not be
+.Ar dir
+or a path under it.
+A regular file may be missing from
+.Ar dir
+when its parent directory already exists.
+The name is not created in
+.Ar dir .
+A directory or a socket must already exist and be the same type.
+Repeatable.
+.It Fl S
+Allow System V shared memory, semaphores, and message queues.
+.It Fl s Ar size
+Size of the
+.Xr tmpfs 4
+mounted on
+.Pa /tmp .
+The default is
+.Cm 16m .
+The syntax is the
+.Cm size
+option of
+.Xr tmpfs 4 .
+.It Fl T
+Do not mount
+.Xr tmpfs 4 .
+Without
+.Fl T ,
+.Ar dir
+must contain
+.Pa /tmp
+as a directory.
+Cannot be combined with
+.Fl s .
+.It Fl U Ar user
+Run
+.Ar command
+as
+.Ar user
+from the jail password database
+.Pq Ql exec.jail_user .
+Cannot be combined with
+.Fl u .
+.It Fl u Ar user
+Run
+.Ar command
+as
+.Ar user
+from the host password database
+.Pq Ql exec.system_jail_user .
+Cannot be combined with
+.Fl U .
+.It Fl W Ar jailpath
+Mount the regular file or socket
+.Ar jailpath ,
+an absolute path inside
+.Ar dir ,
+read-write over the read-only tree.
+This mount is executable.
+Repeatable.
+.It Fl w Ar jailpath
+Like
+.Fl W ,
+with
+.Dv noexec .
+Repeatable.
+.El
+.Pp
+.Pa /tmp ,
+when mounted, is mode 1777,
+.Dv noexec ,
+and limited to
+.Fl s .
+.Sh EXIT STATUS
+.Ex -std
+.Pp
+The status is the command's status when every unmount succeeds.
+A command that succeeds and leaves a mount behind exits non-zero.
+.Fl c
+exits zero when the jail is up and the name was printed.
+Failure prints nothing on standard output.
+.Sh SEE ALSO
+.Xr nullfs 4 ,
+.Xr tmpfs 4 ,
+.Xr jail 8 ,
+.Xr jexec 8 ,
+.Xr mount_nullfs 8 ,
+.Xr sysctl 8 ,
+.Xr sysrc 8
+.Sh DEPENDENCIES
+The following standard commands are required by
+.Nm :
+.Pp
+.Xr awk 1 ,
+.Xr env 1 ,
+.Xr jail 8 ,
+.Xr jls 8 ,
+.Xr ldd 1 ,
+.Xr mktemp 1 ,
+.Xr mountpoint 1 ,
+.Xr od 1 ,
+.Xr readelf 1 ,
+.Xr readlink 1 ,
+.Xr rmdir 1 ,
+.Xr sh 1 ,
+.Xr stat 1 ,
+.Xr sysctl 8 ,
+and
+.Xr umount 8 .
+.Sh HISTORY
+The
+.Nm
+utility first appeared in
+.Fx 16.0 .
+.Sh AUTHORS
+.An Devin Teske Aq Mt dteske@FreeBSD.org
diff --git a/usr.sbin/enjail/tests/Makefile b/usr.sbin/enjail/tests/Makefile
new file mode 100644
--- /dev/null
+++ b/usr.sbin/enjail/tests/Makefile
@@ -0,0 +1,3 @@
+ATF_TESTS_SH+= enjail_test
+
+.include <bsd.test.mk>
diff --git a/usr.sbin/enjail/tests/enjail_test.sh b/usr.sbin/enjail/tests/enjail_test.sh
new file mode 100644
--- /dev/null
+++ b/usr.sbin/enjail/tests/enjail_test.sh
@@ -0,0 +1,237 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Devin Teske <dteske@FreeBSD.org>
+#
+
+atf_test_case s_and_T
+s_and_T_head()
+{
+ atf_set "descr" "-s and -T are mutually exclusive"
+}
+s_and_T_body()
+{
+ atf_check -s exit:1 -e match:"mutually exclusive" \
+ enjail -s 1m -T / /bin/sh
+ atf_check -s exit:1 -e match:"mutually exclusive" \
+ enjail -T -s 16m / /bin/sh
+}
+
+atf_test_case w_directory
+w_directory_head()
+{
+ atf_set "descr" "-w rejects a directory"
+}
+w_directory_body()
+{
+ atf_check mkdir -p root/etc root/tmp root/dev
+ atf_check -s exit:1 -e match:"not a regular file" \
+ enjail -w /etc "$PWD/root" /bin/sh
+}
+
+atf_test_case r_inside
+r_inside_head()
+{
+ atf_set "descr" "-r rejects a path inside the jail root"
+}
+r_inside_body()
+{
+ atf_check mkdir -p root/usr
+ atf_check -s exit:1 -e match:"inside the jail root" \
+ enjail -r "$PWD/root/usr" "$PWD/root" /bin/sh
+}
+
+atf_test_case relative_dir
+relative_dir_head()
+{
+ atf_set "descr" "the jail root must be absolute"
+}
+relative_dir_body()
+{
+ atf_check -s exit:1 -e match:"absolute pathname" \
+ enjail relative /bin/sh
+}
+
+atf_test_case r_missing_file
+r_missing_file_head()
+{
+ atf_set "descr" "-r of a missing regular file is not refused"
+}
+r_missing_file_body()
+{
+ atf_check mkdir -p root/bin root/tmp root/dev
+ atf_check -s exit:1 -e not-match:"not present" \
+ enjail -T -D -r /bin/sh "$PWD/root" /bin/true
+}
+
+atf_test_case r_missing_parent
+r_missing_parent_head()
+{
+ atf_set "descr" "-r refuses a regular file whose parent is missing"
+}
+r_missing_parent_body()
+{
+ atf_check mkdir -p root
+ atf_check -s exit:1 -e match:"not a directory" \
+ enjail -T -D -r /bin/sh "$PWD/root" /bin/sh
+}
+
+atf_test_case c_with_command
+c_with_command_head()
+{
+ atf_set "descr" "-c refuses a command"
+}
+c_with_command_body()
+{
+ atf_check -s exit:1 -e match:"does not take a command" \
+ enjail -c / /bin/sh
+}
+
+atf_test_case c_and_d
+c_and_d_head()
+{
+ atf_set "descr" "-c and -d are mutually exclusive"
+}
+c_and_d_body()
+{
+ atf_check -s exit:1 -e match:"mutually exclusive" \
+ enjail -c -d enjail_1_aaaaaaaa /
+}
+
+atf_test_case d_bad_name
+d_bad_name_head()
+{
+ atf_set "descr" "-d refuses a name it did not generate"
+}
+d_bad_name_body()
+{
+ atf_check -s exit:1 -e match:"not an enjail name" \
+ enjail -d other
+}
+
+atf_test_case d_extra
+d_extra_head()
+{
+ atf_set "descr" "-d does not take a jail root"
+}
+d_extra_body()
+{
+ atf_check -s exit:1 -e match:"does not take a jail root" \
+ enjail -d enjail_1_aaaaaaaa /tmp
+}
+
+atf_test_case e_static
+e_static_head()
+{
+ atf_set "descr" "-e does not run ldd on a static executable"
+}
+e_static_body()
+{
+ atf_check mkdir -p bin root/rescue
+ printf '%s\n' '#!/bin/sh' 'touch ldd.called' 'exit 1' > bin/ldd
+ atf_check chmod 755 bin/ldd
+ atf_check -s exit:1 \
+ -e not-match:"not an ELF" \
+ -e not-match:"not ET_EXEC" \
+ -e not-match:"library not found" \
+ env PATH="$PWD/bin:$PATH" \
+ enjail -T -D -e /rescue/rescue "$PWD/root" /rescue/rescue
+ atf_check -s exit:1 test -f ldd.called
+}
+
+atf_test_case e_dynamic_ldd
+e_dynamic_ldd_head()
+{
+ atf_set "descr" "-e still runs ldd on a dynamic executable"
+}
+e_dynamic_ldd_body()
+{
+ atf_check mkdir -p bin root/bin root/libexec
+ printf '%s\n' '#!/bin/sh' 'touch ldd.called' 'exit 0' > bin/ldd
+ atf_check chmod 755 bin/ldd
+ atf_check -s exit:1 \
+ env PATH="$PWD/bin:$PATH" \
+ enjail -T -D -e /bin/sh "$PWD/root" /bin/sh
+ atf_check test -f ldd.called
+}
+
+atf_test_case R_requires_N
+R_requires_N_head()
+{
+ atf_set "descr" "-R requires -N"
+}
+R_requires_N_body()
+{
+ atf_check -s exit:1 -e match:"requires -N" \
+ enjail -R /
+}
+
+atf_test_case R_with_N
+R_with_N_head()
+{
+ atf_set "descr" "-R with -N is accepted"
+}
+R_with_N_body()
+{
+ atf_check -s exit:1 -e match:"no jail root" \
+ enjail -R -N
+}
+
+atf_test_case d_with_F
+d_with_F_head()
+{
+ atf_set "descr" "-d does not take -F"
+}
+d_with_F_body()
+{
+ atf_check -s exit:1 -e match:"does not take other options" \
+ enjail -F -d enjail_1_aaaaaaaa
+}
+
+atf_test_case inet6_absent
+inet6_absent_head()
+{
+ atf_set "descr" "a missing kern.features.inet6 is not fatal"
+}
+inet6_absent_body()
+{
+ atf_check mkdir -p bin
+ printf '%s\n' '#!/bin/sh' 'touch sysctl.called' 'exit 1' > bin/sysctl
+ atf_check chmod 755 bin/sysctl
+ atf_check -s exit:1 -e match:"initgroups" \
+ env PATH="$PWD/bin:$PATH" enjail -T -D /usr /bin/true
+ atf_check test -f sysctl.called
+}
+
+atf_test_case r_missing_dir
+r_missing_dir_head()
+{
+ atf_set "descr" "-r still refuses a missing directory"
+}
+r_missing_dir_body()
+{
+ atf_check mkdir -p root/tmp root/dev
+ atf_check -s exit:1 -e match:"not present" \
+ enjail -T -D -r /usr "$PWD/root" /bin/sh
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case s_and_T
+ atf_add_test_case w_directory
+ atf_add_test_case r_inside
+ atf_add_test_case relative_dir
+ atf_add_test_case r_missing_file
+ atf_add_test_case r_missing_parent
+ atf_add_test_case e_static
+ atf_add_test_case e_dynamic_ldd
+ atf_add_test_case R_requires_N
+ atf_add_test_case R_with_N
+ atf_add_test_case d_with_F
+ atf_add_test_case inet6_absent
+ atf_add_test_case r_missing_dir
+ atf_add_test_case c_with_command
+ atf_add_test_case c_and_d
+ atf_add_test_case d_bad_name
+ atf_add_test_case d_extra
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 26, 1:39 AM (21 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42001182
Default Alt Text
D62026.1795657172.diff (38 KB)

Event Timeline