From: "Darrick J. Wong" <djwong@kernel.org>
To: David Sterba <dsterba@suse.cz>
Cc: fstests@vger.kernel.org, zlang@redhat.com
Subject: Re: Dangerous commands (was:[ANNOUNCE] fstests: for-next branch updated to v2024.02.04)
Date: Wed, 21 Feb 2024 08:13:58 -0800 [thread overview]
Message-ID: <20240221161358.GM6188@frogsfrogsfrogs> (raw)
In-Reply-To: <20240221140951.GJ355@suse.cz>
On Wed, Feb 21, 2024 at 03:09:51PM +0100, David Sterba wrote:
> Hi,
>
> reading [1] and how late it was found that effectively a "rm -rf /" can
> happen makes me worried about what I can expect from fstests after git
> pull. Many people contribute and the number for custom _cleanup()
> functions with unquoted 'rm' commands is just asking for more problems.
>
> [1] https://lore.kernel.org/all/20240205060016.7fgiyafbnrvf5chj@dell-per750-06-vm-08.rhts.eng.pek2.redhat.com/
>
> Unquoted arguments in shell scripts is IMO a big anti-pattern,
That wasn't even the problem there. I temporarily forgot my shellfu and
did the equivalent of:
<never set FUBAR>
rm -rf "$FUBAR"*
The shell helpfully expands the never-set FUBAR to an empty string, then
globs the star to everything in the current directory. rm has
safeguards to prevent you from "rm -rf /" but that offers no protection
from what happens above.
The argument was quoted "properly", but what I think you're really
asking for is "set -u":
#!/bin/bash -u
unset FUBAR
echo -- rm -f "$FUBAR"*
$ /tmp/a.sh
/tmp/a.sh: line 8: FUBAR: unbound variable
Unfortunately you can't just set that unconditionally for all of fstests
and call it a day because now you have to test every line of shellcode
to make sure nothing breaks on unset variables that would have been fine
otherwise. That turns into a mess of:
test -n "$FUBAR" && rm -f "$FUBAR"*
Oh but then there's the other bad shell habit of setting variables to
the empty string -- sometimes you really want that empty string, other
times authors seem to have used that in place of unset. We've gotten
away with that bit of bad hygiene for ages, likely due to -u being a
nondefault option.
> unfortunately present everywhere in xfstests since the beginning.
> Rewriting all scripts would be quite a lot of work, could you at least
> provide safe versions of the cleanup helpers?
I worried about this a long time ago, and tried running shellcheck on
the entire codebase. Thousands of error messages about sloppy quoting
later I gave up. Later I turned that into a patch in djwong-wtf that
runs it only on the files changed by the head commit.
It **didn't notice** the cleanup error! So that wouldn't have saved me.
Long term we ought to rewrite fstests in any language that isn't as much
of a foot gun. Or someone starts a project to set -e -u and deals with
the massive treewide change that's going to be.
> For example:
>
> _rm_tmp() {
> rm -rf -- $tmp
Um, isn't this /also/ an unquoted variable?
I guess one could make safe(r) wrappers so at least rm won't get mixed
up by dashes at the start of filenames:
_rm_files() {
rm -f -- "$@"
}
_rm_dirtree() {
rm -r -- "$@"
}
But then you do
moopath="foo bar"
_rm_files $moopath
and we've lost the battle yet again.
(On the plus side, that's four separate "f*king bash" invocations in one
email! :))
--D
> }
>
> and used as
>
> _cleanup() {
> _rm_tmp
> }
>
> or at least mandate the "--" separator and quoting arguments in new code
> and gradually fix the existing code.
>
> I can send patches at least for btrfs and generic as this affects me but
> first I'd like to know that this will become standard coding style
> requirement in fstests.
>
next prev parent reply other threads:[~2024-02-21 16:13 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-21 14:09 Dangerous commands (was:[ANNOUNCE] fstests: for-next branch updated to v2024.02.04) David Sterba
2024-02-21 16:13 ` Darrick J. Wong [this message]
2024-02-27 3:40 ` Zorro Lang
2024-02-29 18:44 ` David Sterba
2024-02-29 20:05 ` Eric Biggers
2024-02-23 3:53 ` Dave Chinner
2024-02-25 15:37 ` Zorro Lang
2024-02-29 19:19 ` David Sterba
2024-02-25 15:16 ` Zorro Lang
2024-02-25 16:51 ` Eric Biggers
2024-02-25 17:03 ` Darrick J. Wong
2024-02-25 17:45 ` Eric Biggers
2024-02-26 2:56 ` Zorro Lang
2024-02-26 18:18 ` Darrick J. Wong
2024-02-26 18:56 ` Darrick J. Wong
2024-02-27 5:18 ` Eric Biggers
2024-02-26 2:25 ` Zorro Lang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240221161358.GM6188@frogsfrogsfrogs \
--to=djwong@kernel.org \
--cc=dsterba@suse.cz \
--cc=fstests@vger.kernel.org \
--cc=zlang@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox