From: Eric Biggers <ebiggers@kernel.org>
To: "Darrick J. Wong" <djwong@kernel.org>
Cc: Zorro Lang <zlang@redhat.com>, David Sterba <dsterba@suse.cz>,
fstests@vger.kernel.org
Subject: Re: Dangerous commands (was:[ANNOUNCE] fstests: for-next branch updated to v2024.02.04)
Date: Sun, 25 Feb 2024 09:45:27 -0800 [thread overview]
Message-ID: <20240225174527.GB1128@sol.localdomain> (raw)
In-Reply-To: <20240225170304.GO6188@frogsfrogsfrogs>
On Sun, Feb 25, 2024 at 09:03:04AM -0800, Darrick J. Wong wrote:
> On Sun, Feb 25, 2024 at 08:51:28AM -0800, Eric Biggers wrote:
> > On Sun, Feb 25, 2024 at 11:16:16PM +0800, Zorro Lang wrote:
> > > 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,
> > > > 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?
> > >
> > > Hi David,
> > >
> > > Thanks for taking care about it :)
> > >
> > > >
> > > > For example:
> > > >
> > > > _rm_tmp() {
> > > > rm -rf -- $tmp
> > >
> > > It's "$tmp.*"
> > >
> > > May I ask what problem does the "--" hope to avoid? If the "$tmp" is empty,
> > > "rm -rf" and "rm -rf --"" looks like both doing nothing. So what kind
> > > of situation does the "--" hope to fix?
> > >
> > > The root problem in above [1] is about "${FOO}*". If someone does "rm -rf ${FOO}*"
> > > in its custom _cleanup_xxxxx function, then it's dangerous if "$FOO" is empty.
> > >
> > > I thought some ways to avoid that:
> > > 1) Try to avoid doing rm -rf ${FOO}*, if not necessary.
> > > 2) Must checks [ -n "$FOO" ] before doing any rm -rf ${FOO}*
> > > 3) Someone's custom _cleanup_xxxxx better to be called before default _cleanup
> > > does "cd /".
> > > 4) Think about bringing in someone "Static program analysis" tool about bash
> > > script, but I don't know if there're someone good, feel free to give me
> > > suggestions.
> >
> > "--" prevents the following arguments from being interpreted as options if they
> > begin with "-". That's a good practice, but it doesn't help with ${FOO} being
> > empty. To cause the script to exit if ${FOO} is empty, it can be written as
> > ${FOO:?}. Alternatively, 'set -u' can be used.
>
> I said that four days ago. Did nobody receive that reply?
>
> https://lore.kernel.org/fstests/20240225165128.GA1128@sol.localdomain/T/#m0efd851c5a1fb0dbe418f4aff818d20f4355638b
>
You didn't mention the :? option, and I thought that would be worth mentioning.
Of course ideally -u would be used everywhere, as you said.
- Eric
next prev parent reply other threads:[~2024-02-25 17:45 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
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 [this message]
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=20240225174527.GB1128@sol.localdomain \
--to=ebiggers@kernel.org \
--cc=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.