git: 0b1de275ebc4 - main - sysrc: Accept values that contain a newline

From: Devin Teske <dteske_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 21:51:32 UTC
The branch main has been updated by dteske:

URL: https://cgit.FreeBSD.org/src/commit/?id=0b1de275ebc43cdb51f2c9c477339de810635fff

commit 0b1de275ebc43cdb51f2c9c477339de810635fff
Author:     Devin Teske <dteske@FreeBSD.org>
AuthorDate: 2026-11-25 21:47:08 +0000
Commit:     Devin Teske <dteske@FreeBSD.org>
CommitDate: 2026-11-25 21:47:38 +0000

    sysrc: Accept values that contain a newline
    
    awk -v rejects a newline in the value, and the old writer treated that
    failure as an empty edit. The temporary file then held only the new
    assignment, so the rest of the configuration was lost.
    
    Pass the value in the environment and replace the last assignment by
    joining that line through end of file. A quoted newline stays with the
    value, and the text after it stays after the closer. If awk exits
    above 1, leave the file alone. sh -n still refuses a value that would
    break the quotes.
    
    PR:             283729
    MFC after:      2 weeks
    Reviewed by:    kfv
    Differential Revision:  https://reviews.freebsd.org/D61743
---
 usr.sbin/sysrc/sysrc               |  2 +-
 usr.sbin/sysrc/sysrc.8             |  5 +--
 usr.sbin/sysrc/sysrc.subr          | 81 +++++++++++++++++++++++++-------------
 usr.sbin/sysrc/tests/sysrc_test.sh | 67 +++++++++++++++++++++++++++++++
 4 files changed, 123 insertions(+), 32 deletions(-)

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index 986f726a7435..d79bb4952c40 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@ pgm="${0##*/}" # Program basename
 #
 # Version information
 #
-SYSRC_VERSION="9.2 Nov-18,2026"
+SYSRC_VERSION="9.3 Nov-18,2026"
 
 #
 # Options
diff --git a/usr.sbin/sysrc/sysrc.8 b/usr.sbin/sysrc/sysrc.8
index 40f60c6ff077..9067ec55a6a2 100644
--- a/usr.sbin/sysrc/sysrc.8
+++ b/usr.sbin/sysrc/sysrc.8
@@ -475,9 +475,8 @@ The following standard commands are required by
 .Xr mv 1 ,
 .Xr rm 1 ,
 .Xr sh 1 ,
-.Xr stat 1 ,
 and
-.Xr tail 1 .
+.Xr stat 1 .
 .Sh FILES
 .Bl -tag -width ".Pa /etc/defaults/rc.conf" -compact
 .It Pa /etc/defaults/rc.conf
@@ -542,11 +541,11 @@ Return
 if $cloned_interfaces is set:
 .Dl sysrc cloned_interfaces+"alternate"
 .Sh SEE ALSO
+.Xr rc.conf 5 ,
 .Xr jail 8 ,
 .Xr jexec 8 ,
 .Xr jls 8 ,
 .Xr rc 8 ,
-.Xr rc.conf 5 ,
 .Xr rc.subr 8 ,
 .Xr sysctl 8
 .Sh HISTORY
diff --git a/usr.sbin/sysrc/sysrc.subr b/usr.sbin/sysrc/sysrc.subr
index 9ee396f96086..c8c47b2a1e85 100644
--- a/usr.sbin/sysrc/sysrc.subr
+++ b/usr.sbin/sysrc/sysrc.subr
@@ -30,6 +30,7 @@ FAILURE=1
 msg_cannot_create_permission_denied="%s: cannot create %s: Permission denied"
 msg_permission_denied="%s: %s: Permission denied"
 msg_previous_syntax_errors="%s: Not overwriting \`%s' due to previous syntax errors"
+msg_awk_failure="%s: Not overwriting \`%s' due to awk(1) failure"
 
 #
 # Valid characters that can appear in an sh(1) variable name
@@ -1093,61 +1094,80 @@ f_sysrc_desc()
 f_sysrc_set_awk='
 # Variables that should be defined on the invocation line:
 # 	-v varname="varname"
-# 	-v new_value="new_value"
+# The new value is ENVIRON["SYSRC_NV"], not -v. awk -v rejects a newline.
 #
 BEGIN {
 	regex = "^[[:space:]]*"varname"="
-	found = retval = 0
+	new_value = ENVIRON["SYSRC_NV"]
+	retval = 0
 }
-{
-	# If already found... just spew
-	if ( found ) { print; next }
+{ lines[NR] = $0 }
+END {
+	last = 0
+	for (i = 1; i <= NR; i++)
+		if ( match(lines[i], regex) ) last = i
+	if ( !last ) {
+		for (i = 1; i <= NR; i++) print lines[i]
+		exit 0
+	}
+	for (i = 1; i < last; i++) print lines[i]
 
-	# Does this line match an assignment to our variable?
-	if ( ! match($0, regex) ) { print; next }
+	# From the last assignment through EOF, so a quoted newline stays
+	# with the value and the remainder of the file stays after it.
+	text = lines[last]
+	for (i = last + 1; i <= NR; i++)
+		text = text "\n" lines[i]
 
-	# Save important match information
-	found = 1
+	match(text, regex)
 	matchlen = RSTART + RLENGTH - 1
-
-	# Store the value text for later munging
-	value = substr($0, matchlen + 1, length($0) - matchlen)
-
-	# Store the first character of the value
+	value = substr(text, matchlen + 1, length(text) - matchlen)
 	t1 = t2 = substr(value, 0, 1)
 
 	# Assignment w/ back-ticks, expression, or misc.
 	# We ignore these since we did not generate them
 	#
-	if ( t1 ~ /[`$\\]/ ) { retval = 1; print; next }
+	if ( t1 ~ /[`$\\]/ ) { retval = 1; print text }
 
 	# Assignment w/ single-quoted value
 	else if ( t1 == "'\''" ) {
 		sub(/^'\''[^'\'']*/, "", value)
 		if ( length(value) == 0 ) t2 = ""
 		sub(/^'\''/, "", value)
+		printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \
+			t1, new_value, t2, value
 	}
 
 	# Assignment w/ double-quoted value
+	# NB: \\ does not escape the closing quote
 	else if ( t1 == "\"" ) {
-		sub(/^"(.*\\\\+")*[^"]*/, "", value)
+		sub(/^"([^"\\]|\\.)*/, "", value)
 		if ( length(value) == 0 ) t2 = ""
 		sub(/^"/, "", value)
+		printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \
+			t1, new_value, t2, value
 	}
 
 	# Assignment w/ non-quoted value
 	else if ( t1 ~ /[^[:space:];]/ ) {
 		t1 = t2 = "\""
 		sub(/^[^[:space:]]*/, "", value)
+		printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \
+			t1, new_value, t2, value
 	}
 
 	# Null-assignment
-	else if ( t1 ~ /[[:space:];]/ ) { t1 = t2 = "\"" }
+	else if ( t1 ~ /[[:space:];]/ ) {
+		t1 = t2 = "\""
+		printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \
+			t1, new_value, t2, value
+	}
+
+	else
+		printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \
+			t1, new_value, t2, value
 
-	printf "%s%c%s%c%s\n", substr($0, 0, matchlen), \
-		t1, new_value, t2, value
+	exit retval
 }
-END { exit retval }
 '
 f_sysrc_set()
 {
@@ -1248,21 +1268,26 @@ f_sysrc_set()
 	#
 	# Operate on the matching file, replacing only the last occurrence.
 	#
-	# Use awk to ensure LF at end of each line, else files without ending
-	# LF will trigger a bug in `tail -r' where last two lines are joined.
+	# A newline in the value is data. awk -v cannot carry one, so the
+	# value is passed in the environment. Physical lines are not reversed;
+	# a newline inside quotes belongs to the value.
 	#
 	local new_contents retval
-	new_contents=$( awk 1 "$file" 2> /dev/null | tail -r )
-	new_contents=$( echo "$new_contents" | awk -v varname="$varname" \
-		-v new_value="$new_value" "$f_sysrc_set_awk" )
+	new_contents=$( SYSRC_NV="$new_value" awk -v varname="$varname" \
+		"$f_sysrc_set_awk" "$file" )
 	retval=$?
+	if [ "$retval" -gt 1 ]; then
+		f_err "$msg_awk_failure\n" "$pgm" "$file"
+		rm -f "$tmpfile"
+		return $FAILURE
+	fi
 
 	#
 	# Write the temporary file contents.
 	#
-	echo "$new_contents" | tail -r > "$tmpfile" || return $FAILURE
-	if [ $retval -ne $SUCCESS ]; then
-		echo "$varname=\"$new_value\"" >> "$tmpfile"
+	printf '%s\n' "$new_contents" > "$tmpfile" || return $FAILURE
+	if [ "$retval" -ne "$SUCCESS" ]; then
+		printf '%s="%s"\n' "$varname" "$new_value" >> "$tmpfile"
 	fi
 
 	#
diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh
index 3e051be9ed80..7a5a6f90b85a 100644
--- a/usr.sbin/sysrc/tests/sysrc_test.sh
+++ b/usr.sbin/sysrc/tests/sysrc_test.sh
@@ -506,10 +506,77 @@ strappend_body()
 	    sysrc -f "${SYSRC_CONF}" 'cron_flags+=-J 15'
 }
 
+atf_test_case set_variable_newline
+set_variable_newline_head()
+{
+	atf_set "descr" "Assignment values may contain a newline"
+}
+set_variable_newline_body()
+{
+	local val
+
+	printf '%s\n' 'test_enable="NO"' 'others_enable="YES"' > "${SYSRC_CONF}"
+	val=$(printf 'YES\nNOX')
+	val="${val%X}"
+
+	atf_check -o inline:"test_enable: NO -> YES
+NO
+" sysrc -f "${SYSRC_CONF}" "test_enable=$val"
+
+	printf '%s\n' 'test_enable="YES' 'NO"' 'others_enable="YES"' > expected
+	atf_check -o file:expected cat "${SYSRC_CONF}"
+
+	atf_check -o inline:"YES
+NO
+" sysrc -f "${SYSRC_CONF}" -nr test_enable
+
+	# A new variable is appended. The neighbor stays.
+	printf '%s\n' 'keep=1' > "${SYSRC_CONF}"
+	atf_check -o inline:"fresh:  -> YES
+NO
+" sysrc -f "${SYSRC_CONF}" "fresh=$val"
+	printf '%s\n' 'keep=1' 'fresh="YES' 'NO"' > expected
+	atf_check -o file:expected cat "${SYSRC_CONF}"
+
+	# A quote that would break the string does not replace the file
+	printf '%s\n' 'test_enable="YES' 'NO"' 'others_enable="YES"' \
+	    > "${SYSRC_CONF}"
+	cp "${SYSRC_CONF}" saved
+	val=$(printf 'YE"S\nNOX')
+	val="${val%X}"
+	atf_check -s exit:1 -e match:"syntax errors" \
+	    sysrc -f "${SYSRC_CONF}" "test_enable=$val"
+	atf_check -o file:saved cat "${SYSRC_CONF}"
+}
+
+atf_test_case set_variable_backslash
+set_variable_backslash_head()
+{
+	atf_set "descr" "A value ending in a backslash can be replaced"
+}
+set_variable_backslash_body()
+{
+	# "C:\\" stores the value C:\ and the closer is not escaped
+	printf '%s\n' 'bar="C:\\"' 'keep=1' > "${SYSRC_CONF}"
+	atf_check -o inline:'bar: C:\ -> NEW\n' \
+	    sysrc -f "${SYSRC_CONF}" bar=NEW
+	printf '%s\n' 'bar="NEW"' 'keep=1' > expected
+	atf_check -o file:expected cat "${SYSRC_CONF}"
+
+	# A later value ending in a backslash belongs to that later name
+	printf '%s\n' 'aa="old"' 'bb="C:\\"' > "${SYSRC_CONF}"
+	atf_check -o inline:'aa: old -> NEW\n' \
+	    sysrc -f "${SYSRC_CONF}" aa=NEW
+	printf '%s\n' 'aa="NEW"' 'bb="C:\\"' > expected
+	atf_check -o file:expected cat "${SYSRC_CONF}"
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case check_variable
 	atf_add_test_case set_variable
+	atf_add_test_case set_variable_newline
+	atf_add_test_case set_variable_backslash
 	atf_add_test_case remove_variable_x_flag
 	atf_add_test_case append_list
 	atf_add_test_case strappend