Page MenuHomeFreeBSD

D61938.1795510813.diff
No OneTemporary

Authored By
Unknown
Size
21 KB
Referenced Files
None
Subscribers
None

D61938.1795510813.diff

diff --git a/share/man/man4/nullfs.4 b/share/man/man4/nullfs.4
--- a/share/man/man4/nullfs.4
+++ b/share/man/man4/nullfs.4
@@ -22,7 +22,7 @@
.\" (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 March 15, 2022
+.Dd November 22, 2026
.Dt NULLFS 4
.Os
.Sh NAME
@@ -47,6 +47,13 @@
driver will permit the
.Fx
kernel to mount a loopback file system sub-tree.
+A regular file or a socket may be mounted again over a
+.Nm
+vnode of that same object, so a read-write mount of one file can sit
+on a read-only
+.Nm
+of a tree that contains the file.
+A second mount of the same directory is rejected.
.Sh EXAMPLES
To mount a
.Nm
diff --git a/sys/fs/nullfs/null_vfsops.c b/sys/fs/nullfs/null_vfsops.c
--- a/sys/fs/nullfs/null_vfsops.c
+++ b/sys/fs/nullfs/null_vfsops.c
@@ -161,10 +161,17 @@
/*
* Check multi null mount to avoid `lock against myself' panic.
+ * A second nullfs of the same directory deadlocks on the shared
+ * lower vnode lock. A regular file or a socket may be mounted
+ * again, so one object can be read-write on a read-only nullfs
+ * of that same object. null_lock() nests that second hold.
*/
if (null_is_nullfs_vnode(mp->mnt_vnodecovered)) {
nn = VTONULL(mp->mnt_vnodecovered);
- if (nn == NULL || lowerrootvp == nn->null_lowervp) {
+ if (nn == NULL ||
+ (lowerrootvp == nn->null_lowervp &&
+ lowerrootvp->v_type != VREG &&
+ lowerrootvp->v_type != VSOCK)) {
NULLFSDEBUG("nullfs_mount: multi null mount?\n");
vput(lowerrootvp);
return (EDEADLK);
diff --git a/sys/fs/nullfs/null_vnops.c b/sys/fs/nullfs/null_vnops.c
--- a/sys/fs/nullfs/null_vnops.c
+++ b/sys/fs/nullfs/null_vnops.c
@@ -857,7 +857,16 @@
return (vop_stdlock(ap));
VNPASS(lvp->v_holdcnt > 0, lvp);
- error = VOP_LOCK(lvp, ap->a_flags);
+ /*
+ * Stacking a file or socket nullfs on a nullfs vnode of that
+ * same object locks the lower vnode twice on one thread. Nest
+ * that hold. Leave ap->a_flags alone; the doomed path below
+ * still reads it.
+ */
+ flags = ap->a_flags;
+ if (VOP_ISLOCKED(lvp) != 0)
+ flags |= LK_CANRECURSE;
+ error = VOP_LOCK(lvp, flags);
/*
* We might have slept to get the lock and someone might have
* clean our vnode already, switching vnode lock from one in
diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@
#
# Version information
#
-SYSRC_VERSION="9.4 Nov-22,2026"
+SYSRC_VERSION="9.5 Nov-22,2026"
#
# Options
@@ -29,6 +29,7 @@
IGNORE_UNKNOWNS= # -i
HUSER= # -u user
JAIL= # -j jail
+JROOT= # -J dir
JUSER= # -U user
LIST_CONFS= # -l
LIST_SERVICE_CONFS= # -L
@@ -129,8 +130,10 @@
"Print this message to stderr and exit."
f_err "$optfmt" "-i" \
"Ignore unknown variables."
+ f_err "$optfmt" "-J dir" \
+ "Operate within a temporary sandbox jail with path \`dir'."
f_err "$optfmt" "-j jail" \
- "The jid or name of the jail to operate within."
+ "The jid or name of the running jail to operate within."
f_err "$optfmt" "-L" \
"List all configuration files including rc.conf.d entries."
f_err "$optfmt" "-l" \
@@ -177,10 +180,10 @@
# jail_depend
#
# Dump dependencies such as include files to stdout to be piped-into sh(1)
-# running via jexec(8)/chroot(8). As a security measure, this prevents existing
-# library files from being loaded in the jail. This also relaxes the
-# requirement to have these files in every jail before sysrc can be used on
-# said jail.
+# running via jexec(8), chroot(8), or jail(8) -c. As a security measure, this
+# prevents existing library files from being loaded in the jail. This also
+# relaxes the requirement to have these files in every jail before sysrc can
+# be used on said jail.
#
jail_depend()
{
@@ -190,6 +193,212 @@
cat $SYSRC_SHARE/sysrc.subr
}
+# jail_sandbox_check_path $path
+#
+# Refuse a path that readlink -f does not print unchanged. A missing
+# final component returns success so the caller can say why. Space and
+# tab are allowed; any other whitespace is refused.
+#
+jail_sandbox_check_path()
+{
+ local path="$1"
+ local real vis
+
+ vis="$path"
+ case "$path" in
+ *" "*|*" "*) f_fstab_vis "$path" vis
+ esac
+ case "$vis" in
+ *[[:space:]]*) die "%s: unsupported whitespace in pathname" "$path"
+ esac
+ real=$( readlink -f "$path" ) || [ "$real" ] || return $SUCCESS
+ [ "$real" = "$path" ] ||
+ die "%s: resolves to %s" "$path" "$real"
+}
+
+# jail_sandbox_umount $mountpoint
+#
+# Unmount $mountpoint when it is mounted.
+#
+jail_sandbox_umount()
+{
+ local mp="$1"
+
+ mountpoint -q "$mp" || return $SUCCESS
+ umount "$mp"
+}
+
+# jail_sandbox_newdir
+#
+# Create the scratch directory and refuse one inside $JROOT.
+#
+jail_sandbox_newdir()
+{
+ local dir
+
+ dir=$( mktemp -d -t sysrc.J ) ||
+ die "cannot create scratch directory"
+ case "$dir/" in
+ "$JROOT"/*)
+ rmdir "$dir" || warn "%s: not removed" "$dir"
+ die "%s: contains the scratch directory" "$JROOT"
+ ;;
+ esac
+ printf "%s\n" "$dir"
+}
+
+# jail_sandbox_run
+#
+# Mount $JROOT read-only on $jpath and run this script with $runargs.
+# Each path in $files is mounted read-write. Unmount before returning.
+#
+jail_sandbox_run()
+{
+ local clean=$SUCCESS status=$SUCCESS
+ local file mnt spec
+
+ f_fstab_vis "$JROOT" spec
+ f_fstab_vis "$jpath" mnt
+ set -- path="$jpath" \
+ host.hostname=sysrc \
+ mount="$spec $mnt nullfs ro"
+ set -f # noglob
+ for file in $files; do
+ f_fstab_vis "${JROOT%/}$file" spec
+ f_fstab_vis "$jpath$file" mnt
+ set -- "$@" mount="$spec $mnt nullfs rw"
+ done
+ set +f # glob
+ if [ "$files" ]; then
+ f_fstab_vis "$jpath/tmp" mnt
+ file="mount=tmpfs $mnt tmpfs rw,noexec,mode=1777,size=1m"
+ set -- "$@" "$file"
+ fi
+ set -- "$@" \
+ ip4=disable ip6=disable \
+ sysvmsg=disable sysvsem=disable sysvshm=disable \
+ mount.devfs exec.clean nopersist
+ if [ "$HUSER" ]; then
+ set -- "$@" exec.system_jail_user "exec.jail_user=$HUSER"
+ elif [ "$JUSER" ]; then
+ set -- "$@" "exec.jail_user=$JUSER"
+ fi
+ set -- "$@" command=/bin/sh
+
+ (
+ echo "RC_DEFAULTS='$( f_shell_escape "$RC_DEFAULTS" )'"
+ echo set -- $runargs
+ jail_depend
+ cat "$0"
+ ) | env - /usr/sbin/jail -c "$@"
+ status=$?
+
+ if [ "$files" ]; then
+ jail_sandbox_umount "$jpath/tmp" || clean=$FAILURE
+ fi
+ jail_sandbox_umount "$jpath/dev" || clean=$FAILURE
+ set -f # noglob
+ for file in $files; do
+ jail_sandbox_umount "$jpath$file" || clean=$FAILURE
+ done
+ set +f # glob
+ jail_sandbox_umount "$jpath" || clean=$FAILURE
+ if [ "$clean" -eq $SUCCESS ]; then
+ rmdir "$jpath" || clean=$FAILURE
+ fi
+ [ "$clean" -ne $SUCCESS ] &&
+ warn "%s: scratch directory left mounted" "$jpath"
+ [ "$status" -eq $SUCCESS ] || return $status
+ return $clean
+}
+
+# jail_sandbox
+#
+# Sandbox jail on $JROOT. The tree is read-only. A write with no -f learns
+# the jail's own rc files from a read-only pass (-El, or -Els with -s), then
+# mounts those files read-write. -f mounts the given files and skips that
+# pass. jail -c does not unmount, so we do that ourselves afterward.
+#
+# Requires $args to be set, which is then passed to jail_sandbox_run().
+#
+jail_sandbox()
+{
+ local _files files item jpath runargs svc
+ local needwrite=
+ local noglob=
+
+ case "$JROOT" in
+ /*) ;;
+ *) die "-J requires an absolute pathname"
+ esac
+ while :; do
+ case "$JROOT" in
+ /) break ;;
+ */) JROOT="${JROOT%/}" ;;
+ *) break
+ esac
+ done
+
+ jail_sandbox_check_path "$JROOT"
+ [ -d "$JROOT" ] || die "%s: not a directory" "$JROOT"
+ jail_sandbox_check_path "${JROOT%/}/tmp"
+ [ -d "${JROOT%/}/tmp" ] ||
+ die "%s: not a directory" "${JROOT%/}/tmp"
+ jail_sandbox_check_path "${JROOT%/}/dev"
+ [ -d "${JROOT%/}/dev" ] ||
+ die "%s: not a directory" "${JROOT%/}/dev"
+
+ #
+ # A query can read the tree. A write has to learn the jail's files
+ # before anything is mounted read-write (unless -f is given, in which
+ # case we are told what to make read-write).
+ #
+ if [ ! "$LIST_CONFS$LIST_SERVICE_CONFS" -a ! "$CHECK_ONLY" ] &&
+ [ ! "$DESCRIBE" ]; then
+ [ "$DELETE" ] && needwrite=1
+ for item in "$@"; do
+ case "$item" in
+ *=*) needwrite=1 ;;
+ esac
+ done
+ fi
+
+ case "$-" in
+ *f*) noglob=1 ;;
+ esac
+
+ if [ "${RC_CONFS+set}" ]; then
+ files="$RC_CONFS"
+ elif [ "$needwrite" ]; then
+ runargs="-E -l"
+ if [ "$SERVICE" ]; then
+ f_shell_escape "$SERVICE" svc
+ runargs="$runargs -s '$svc'"
+ fi
+ jpath=$( jail_sandbox_newdir ) || return
+ files=$( jail_sandbox_run ) || return
+ [ "$files" ] ||
+ die "%s: no rc.conf to mount read-write" "$JROOT"
+ f_shell_escape "$files" _files
+ args="$args -f '$_files'"
+ fi
+ set -f # noglob
+ for f in $files; do
+ case "$f" in
+ /*) ;;
+ *) die "%s: not an absolute pathname" "$f"
+ esac
+ jail_sandbox_check_path "${JROOT%/}$f"
+ [ -f "${JROOT%/}$f" ] ||
+ die "%s: not a regular file" "${JROOT%/}$f"
+ done
+ [ "$noglob" ] || set +f # glob
+
+ jpath=$( jail_sandbox_newdir ) || return
+ runargs="$args"
+ jail_sandbox_run
+}
+
# escape_required $string
#
# Returns success if $string contains characters unsafe if not escaped. These
@@ -260,7 +469,7 @@
#
# Process command-line flags
#
-while getopts AacDdEeFf:hij:LlNnqR:rs:U:u:vXx flag; do
+while getopts AacDdEeFf:hiJ:j:LlNnqR:rs:U:u:vXx flag; do
case "$flag" in
A) SHOW_ALL=2 ;;
a) SHOW_ALL=${SHOW_ALL:-1} ;;
@@ -273,6 +482,9 @@
f) DEFAULT= RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG" ;;
h) usage ;; # NOTREACHED
i) IGNORE_UNKNOWNS=1 ;;
+ J) [ "$OPTARG" ] ||
+ die "Missing or null argument to \`-J' flag"
+ JROOT="$OPTARG" ;;
j) [ "$OPTARG" ] ||
die "Missing or null argument to \`-j' flag"
JAIL="$OPTARG" ;;
@@ -326,11 +538,15 @@
fi
[ "$JAIL" -a "$ROOTDIR" ] &&
usage "-j and -R are mutually exclusive" # NOTREACHED
+[ "$JROOT" -a "$JAIL" ] &&
+ usage "-J and -j are mutually exclusive" # NOTREACHED
+[ "$JROOT" -a "$ROOTDIR" ] &&
+ usage "-J and -R are mutually exclusive" # NOTREACHED
#
# Process `-L' flag
#
-if [ "$LIST_SERVICE_CONFS" ]; then
+if [ "$LIST_SERVICE_CONFS" -a ! "$JROOT" ]; then
list=
#
@@ -405,20 +621,23 @@
#
# Validate arguments
#
-for name in "$@"; do
- # NB: shell expansion syntax removed first
- name="${name%%:[+=-]*}"
- name="${name%%.=*}" # string append
- name="${name%%[%#+=-]*}"
- [ "$name" = "${name#*[!$VALID_VARNAME_CHARS]}" ] ||
- die "%s: name contains characters not allowed in shell" "$name"
-done
-unset name
+if [ ! "$LIST_CONFS$LIST_SERVICE_CONFS" ]; then
+ msg="%s: name contains characters not allowed in shell"
+ for name in "$@"; do
+ # NB: shell expansion syntax removed first
+ name="${name%%:[+=-]*}"
+ name="${name%%.=*}" # string append
+ name="${name%%[%#+=-]*}"
+ [ "$name" = "${name#*[!$VALID_VARNAME_CHARS]}" ] ||
+ die "$msg" "$name"
+ done
+ unset name msg
+fi
#
# Process `-s name' argument
#
-if [ "$SERVICE" -a ! "${RC_CONFS+set}" ]; then
+if [ "$SERVICE" -a ! "${RC_CONFS+set}$JROOT" ]; then
if f_sysrc_service_configs "$SERVICE" RC_CONFS; then
rc_conf_files=$( f_sysrc_get rc_conf_files )
RC_CONFS="$rc_conf_files${RC_CONFS:+ }$RC_CONFS"
@@ -431,7 +650,7 @@
#
# Process `-E' option flag
#
-if [ "$EXISTING_ONLY" ]; then
+if [ "$EXISTING_ONLY" -a ! "$JROOT" ]; then
#
# To get f_sysrc_*() to ignore missing rc_conf_files, we have to use
# RC_CONFS to override the unpreened value. If RC_CONFS already has a
@@ -455,7 +674,7 @@
#
# Process `-l' option flag
#
-if [ "$LIST_CONFS" ]; then
+if [ "$LIST_CONFS" -a ! "$JROOT" ]; then
[ $# -eq 0 ] || usage "-l does not take names" # NOTREACHED
if [ "$DEFAULT" ]; then
list="$RC_DEFAULTS"
@@ -475,30 +694,34 @@
#
# [More] Sanity checks (e.g., "sysrc --")
#
-[ $# -eq 0 -a ! "$SHOW_ALL" ] && usage "no names provided" # NOTREACHED
-
-#
-# Taint-check all rc.conf(5) files
-#
-errmsg="Exiting due to previous syntax errors"
-if [ "${RC_CONFS+set}" ]; then
- ( set -f # noglob
- for i in $RC_CONFS; do
- [ -e "$i" ] || continue
- /bin/sh -n "$i" || exit $FAILURE
- done
- exit $SUCCESS
- ) || die "$errmsg"
-else
- /bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
- ( . "$RC_DEFAULTS"
- set -f # noglob
- for i in $rc_conf_files; do
- [ -e "$i" ] || continue
- /bin/sh -n "$i" || exit $FAILURE
- done
- exit $SUCCESS
- ) || die "$errmsg"
+[ $# -eq 0 -a ! "$SHOW_ALL$LIST_CONFS" ] &&
+ [ ! "$LIST_SERVICE_CONFS" ] &&
+ usage "no names provided" # NOTREACHED
+
+#
+# Taint-check all rc.conf(5) files. -J checks the jail's files.
+#
+if [ ! "$JROOT" ]; then
+ errmsg="Exiting due to previous syntax errors"
+ if [ "${RC_CONFS+set}" ]; then
+ ( set -f # noglob
+ for i in $RC_CONFS; do
+ [ -e "$i" ] || continue
+ /bin/sh -n "$i" || exit $FAILURE
+ done
+ exit $SUCCESS
+ ) || die "$errmsg"
+ else
+ /bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
+ ( . "$RC_DEFAULTS"
+ set -f # noglob
+ for i in $rc_conf_files; do
+ [ -e "$i" ] || continue
+ /bin/sh -n "$i" || exit $FAILURE
+ done
+ exit $SUCCESS
+ ) || die "$errmsg"
+ fi
fi
#
@@ -537,18 +760,18 @@
fi
#
-# Process `-j jail' and `-R dir' command-line options
+# Process `-j jail', `-J dir', and `-R dir' command-line options
#
if [ "$HUSER" -a "$JUSER" ]; then
die "-u and -U are mutually exclusive"
fi
-if [ "$HUSER" -a ! "$JAIL" -a ! "$ROOTDIR" ]; then
- die "-u requires \`-j jail' or \`-R dir'"
+if [ "$HUSER" -a ! "$JAIL$ROOTDIR$JROOT" ]; then
+ die "-u requires \`-j jail', \`-J dir', or \`-R dir'"
fi
-if [ "$JUSER" -a ! "$JAIL" -a ! "$ROOTDIR" ]; then
- die "-U requires \`-j jail'"
+if [ "$JUSER" -a ! "$JAIL$ROOTDIR$JROOT" ]; then
+ die "-U requires \`-j jail' or \`-J dir'"
fi
-if [ "$JAIL" -o "$ROOTDIR" ]; then
+if [ "$JAIL$ROOTDIR$JROOT" ]; then
#
# Reconstruct the arguments that we want to carry-over
# NB: Order matters
@@ -589,6 +812,15 @@
done
unset arg
+ #
+ # -J is exclusive with -j and -R. The inner script must not see
+ # -J, -j, -R, -u, or -U.
+ #
+ if [ "$JROOT" ]; then
+ jail_sandbox
+ exit
+ fi
+
#
# If both are supplied, `-j jail' supercedes `-R dir'
#
diff --git a/usr.sbin/sysrc/sysrc.8 b/usr.sbin/sysrc/sysrc.8
--- a/usr.sbin/sysrc/sysrc.8
+++ b/usr.sbin/sysrc/sysrc.8
@@ -35,7 +35,7 @@
.Nm
.Op Fl cDdEeFhiNnqrvx
.Op Fl f Ar file
-.Op Fl j Ar jail | Fl R Ar dir
+.Op Fl j Ar jail | Fl J Ar dir | Fl R Ar dir
.Op Fl s Ar name
.Op Fl u Ar user | Fl U Ar user
.Ar name Ns Op Ns Oo +|-|. Oc Ns = Ns Ar value
@@ -43,7 +43,7 @@
.Nm
.Op Fl cDdEeFhiNnqrvx
.Op Fl f Ar file
-.Op Fl j Ar jail | Fl R Ar dir
+.Op Fl j Ar jail | Fl J Ar dir | Fl R Ar dir
.Op Fl s Ar name
.Op Fl u Ar user | Fl U Ar user
.Fl A | a
@@ -119,6 +119,53 @@
Print a full usage statement to stderr and exit.
.It Fl i
Ignore unknown variables.
+.It Fl J Ar dir
+Operate within a temporary sandbox jail with path
+.Ar dir .
+The tree is mounted read-only with
+.Xr nullfs 4
+on a temporary directory, each file to be edited is mounted
+read-write, and
+.Pa /tmp
+is a one-megabyte
+.Xr tmpfs 4
+with mode 1777 and
+.Dv noexec .
+Network and System V IPC are disabled.
+The directory stays read-only, so the file is updated in place.
+Without
+.Fl f ,
+a write learns the file list from a read-only pass over the jail
+.Pq Ql -El ,
+or
+.Pq Ql -Els
+with
+.Fl s ,
+so
+.Pa /etc/defaults/vendor.conf
+and the jail's own rc.d scripts are what choose the files.
+With
+.Fl f ,
+only the named files are mounted and that pass is skipped.
+The jail is removed when
+.Nm
+exits, and this process unmounts what
+.Xr jail 8
+left behind.
+.Ar dir
+must contain
+.Pa /tmp
+and
+.Pa /dev
+as directories, and the rc file must already exist.
+A path is refused when
+.Xr readlink 1
+.Fl f
+does not print it unchanged.
+Cannot be combined with
+.Fl j
+or
+.Fl R .
.It Fl j Ar jail
The
.Ar jid
@@ -184,9 +231,11 @@
Run inside the jail as
.Ar user .
The name is looked up in the jail password database after attach
-.Pq Xr jexec 8 Fl U .
+.Pq Xr jexec 8 Fl U , or Ql exec.jail_user .
Requires
-.Fl j ,
+.Fl j
+or
+.Fl J ,
or
.Fl R
pointing at a running jail.
@@ -202,8 +251,13 @@
.Xr chroot 8
as
.Fl u .
+With
+.Fl J ,
+the name is looked up in the host password database
+.Pq Ql exec.system_jail_user .
Requires
-.Fl j
+.Fl j ,
+.Fl J ,
or
.Fl R .
Cannot be combined with
@@ -499,10 +553,13 @@
.Xr chroot 8 ,
.Xr env 1 ,
.Xr grep 1 ,
+.Xr jail 8 ,
.Xr jexec 8 ,
.Xr jls 8 ,
.Xr mktemp 1 ,
+.Xr mountpoint 1 ,
.Xr mv 1 ,
+.Xr readlink 1 ,
.Xr rm 1 ,
.Xr sh 1 ,
and
diff --git a/usr.sbin/sysrc/sysrc.subr b/usr.sbin/sysrc/sysrc.subr
--- a/usr.sbin/sysrc/sysrc.subr
+++ b/usr.sbin/sysrc/sysrc.subr
@@ -184,6 +184,34 @@
}
fi
+# f_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). A \ in the path is
+# encoded, so a \s already there stays a backslash and an s.
+#
+f_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" ;;
+ ' ') __string="$__string\\t" ;;
+ *) __string="$__string$__ch"
+ esac
+ done
+ if [ "$__var_to_set" ]; then
+ setvar "$__var_to_set" "$__string"
+ else
+ echo "$__string"
+ fi
+}
+
# f_clean_env [--except $varname ...]
#
# Unset all environment variables in the current scope. An optional list of
@@ -1169,6 +1197,37 @@
exit retval
}
'
+
+# f_sysrc_replace $tmpfile $file
+#
+# Move $tmpfile onto $file. mv(1) unlinks a destination that lives on
+# another device, and a mounted file cannot be unlinked. Write through
+# that vnode instead.
+#
+f_sysrc_replace()
+{
+ local funcname=f_sysrc_replace
+ local tmpfile="$1" file="$2"
+ local _file _dir dev dirdev dir
+
+ f_shell_escape "$file" _file
+ dir="${file%/*}"
+ [ "$dir" ] || dir=/
+ f_shell_escape "$dir/." _dir
+ f_eval_catch -dk dev $funcname stat \
+ "stat -f '%%d' '%s'" "$_file" || dev=
+ f_eval_catch -dk dirdev $funcname stat \
+ "stat -f '%%d' '%s'" "$_dir" || dirdev=
+ if [ "$dev" -a "$dirdev" -a "$dev" != "$dirdev" ]; then
+ f_eval_catch -de $funcname cat \
+ "cat '%s' > '%s'" "$tmpfile" "$_file" ||
+ return $FAILURE
+ rm -f "$tmpfile"
+ return $SUCCESS
+ fi
+ f_eval_catch -de $funcname mv "mv '%s' '%s'" "$tmpfile" "$_file"
+}
+
f_sysrc_set()
{
local funcname=f_sysrc_set
@@ -1302,7 +1361,7 @@
#
# Finally, move the temporary file into place.
#
- f_eval_catch -de $funcname mv "mv '%s' '%s'" "$tmpfile" "$_file"
+ f_sysrc_replace "$tmpfile" "$file"
}
# f_sysrc_delete $varname
@@ -1415,9 +1474,7 @@
#
# Finally, move the temporary file into place.
#
- if ! f_eval_catch -de $funcname mv \
- "mv '%s' '%s'" "$tmpfile" "$_file"
- then
+ if ! f_sysrc_replace "$tmpfile" "$file"; then
[ "$noglob" ] || set +f
return $FAILURE
fi
diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh
--- a/usr.sbin/sysrc/tests/sysrc_test.sh
+++ b/usr.sbin/sysrc/tests/sysrc_test.sh
@@ -766,6 +766,83 @@
sysrc_jail_cleanup
}
+atf_test_case J_flag_refuse
+J_flag_refuse_head()
+{
+ atf_set "descr" "-J rejects a bad jail root before it mounts"
+}
+J_flag_refuse_body()
+{
+ atf_check -s exit:1 -e match:"mutually exclusive" \
+ sysrc -J /nonexistent -j 0 foo
+ atf_check -s exit:1 -e match:"mutually exclusive" \
+ sysrc -J /nonexistent -R / foo
+ atf_check -s exit:1 -e match:"absolute pathname" \
+ sysrc -J relative foo
+ atf_check -s exit:1 -e match:"resolves to" \
+ sysrc -J /tmp/../etc foo
+
+ atf_check mkdir real
+ atf_check ln -s real link
+ atf_check -s exit:1 -e match:"resolves to" \
+ sysrc -J "$PWD/link" foo
+ atf_check -s exit:1 -e match:"resolves to" \
+ sysrc -u root -J "$PWD/link" foo
+
+ atf_check mkdir -p tree/tmp tree/dev tree/etc
+ printf '%s\n' 'test_enable="NO"' > tree/etc/other
+ atf_check ln -s other tree/etc/rc.conf
+ atf_check -s exit:1 -e match:"resolves to" \
+ sysrc -J "$PWD/tree" -f /etc/rc.conf foo
+ atf_check -o inline:'test_enable="NO"\n' cat tree/etc/other
+}
+
+atf_test_case J_flag cleanup
+J_flag_head()
+{
+ atf_set "descr" "-J edits rc.conf through a one-shot jail"
+ atf_set "require.user" "root"
+}
+J_flag_body()
+{
+ local root="$PWD/root"
+
+ # A nested jail cannot be created here
+ [ "$( sysctl -n security.jail.jailed )" = 0 ] ||
+ atf_skip "cannot create a jail from inside a jail"
+ sysrc_mkroot "$root" sh awk grep mktemp mv chmod chown stat cat rm
+ atf_check mkdir -p "$root/dev" "$root/etc/defaults" "$root/root"
+ atf_check cp /etc/defaults/rc.conf "$root/etc/defaults/rc.conf"
+ atf_check cp /etc/login.conf "$root/etc/login.conf"
+ printf '%s\n' 'root:*:0:0::0:0:Charlie &:/root:/bin/sh' \
+ > "$root/etc/master.passwd"
+ atf_check chmod 600 "$root/etc/master.passwd"
+ printf '%s\n' 'wheel:*:0:root' > "$root/etc/group"
+ atf_check pwd_mkdb -d "$root/etc" "$root/etc/master.passwd"
+ printf '%s\n' 'test_enable="NO"' > "$root/etc/rc.conf"
+
+ atf_check -o inline:"test_enable: NO -> YES\n" \
+ sysrc -J "$root" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="YES"\n' cat "$root/etc/rc.conf"
+
+ # No -f: the read-only pass must learn /etc/rc.conf from this tree
+ printf '%s\n' 'test_enable="NO"' > "$root/etc/rc.conf"
+ atf_check -o inline:"test_enable: NO -> YES\n" \
+ sysrc -J "$root" test_enable=YES
+ atf_check -o inline:'test_enable="YES"\n' cat "$root/etc/rc.conf"
+}
+J_flag_cleanup()
+{
+ local root="$PWD/root" spec file rest
+
+ [ -d "$root" ] || return 0
+ mount -p | while read spec file rest; do
+ case "$spec" in
+ "$root"|"$root"/*) umount "$file" ;;
+ esac
+ done
+}
+
atf_init_test_cases()
{
atf_add_test_case check_variable
@@ -788,4 +865,6 @@
atf_add_test_case u_flag_chroot
atf_add_test_case u_flag_jail
atf_add_test_case U_flag_jail
+ atf_add_test_case J_flag_refuse
+ atf_add_test_case J_flag
}

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 24, 9:00 AM (23 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41951297
Default Alt Text
D61938.1795510813.diff (21 KB)

Event Timeline