In a private mail it was noticed that tar(1) doesn't like if you remove
the directory out from under it.

$ mkdir -p /tmp/x && cd $_ && rm -rf $_ && tar -C / -f /nonexistent
(null): Can't open current working directory.: No such file or directory

While that's not really a supported configuration, we can make
fw_update(8) behave a little nicer.

First we `cd /` before running tar, which avoids the ability to remove
the cwd from under it.  This makes a normal fw_update and the "-d" flag
work ask expected.

Through some more testing, we obviously can't support -F in the case
when we don't have a cwd, but the output in that case is a bit ugly due
to the way we use subshells and a background ftp process:

$ mkdir -p /tmp/x && cd $_ && rm -rf $_ && doas fw_update -F ogx
/usr/sbin/fw_update: cannot create ./SHA256.sig: No such file or directory
Cannot fetch http://firmware.openbsd.org/firmware/snapshots/SHA256.sig
/usr/sbin/fw_update: cannot create ./SHA256.sig: No such file or directory
fw_update: downloaded none; kept none

With this change it stops after the first two lines.  This part is a bit
racy as the directory could be removed at any point during the download
and other errors will continue to be ugly, so maybe best to ignore that
problem.

Comments, obvious bugs, OK?


Index: usr.sbin/fw_update/fw_update.sh
===================================================================
RCS file: /cvs/src/usr.sbin/fw_update/fw_update.sh,v
retrieving revision 1.44
diff -u -p -r1.44 fw_update.sh
--- usr.sbin/fw_update/fw_update.sh     12 Dec 2022 02:30:51 -0000      1.44
+++ usr.sbin/fw_update/fw_update.sh     17 Mar 2023 00:08:45 -0000
@@ -143,7 +143,7 @@ check_cfile() {
 fetch_cfile() {
        if "$DOWNLOAD"; then
                set +o noclobber # we want to get the latest CFILE
-               fetch "$CFILE" || return 1
+               fetch "$CFILE" || exit 2
                set -o noclobber
                ! signify -qVep "$FWPUB_KEY" -x "$CFILE" -m "$CFILE" &&
                    echo "Signature check of SHA256.sig failed" >&2 &&
@@ -277,10 +277,12 @@ add_firmware () {
                2|3) _flags=-Vm ;;
        esac
 
-       ftp -N "${0##/}" -D "$_m" "$_flags" -o- "file:${1}" |
+       ftp -N "${0##/}" -D "$_m" "$_flags" -o- "file:${1}" | (
+               cd / # tar does not like if cwd is nonexistent
                tar -s ",^\+,${FWPKGTMP}/+," \
                    -s ",^firmware,${DESTDIR}/etc/firmware," \
                    -C / -zxphf - "+*" "firmware/*"
+       )
 
        _pkgname="$( sed -n '/^@name /{s///p;q;}' "${FWPKGTMP}/+CONTENTS" )"
        if [ ! "$_pkgname" ]; then
@@ -514,7 +516,10 @@ if [ "${devices[*]:-}" ]; then
 
                verify_existing=true
                if [ "$f" = "$d" ]; then
-                       f=$( firmware_filename "$d" ) || continue
+                       f=$( firmware_filename "$d" ) || {
+                           [ $? = 1 ] || exit 1
+                           continue
+                       }
                        if [ ! "$f" ]; then
                                if "$INSTALL" && unregister_firmware "$d"; then
                                        unregister="$unregister,$d"

Reply via email to