Commit Graph

77 Commits

Author SHA1 Message Date
Conrad Meyer
900c4ed3ca rm(1): Formalize non-functional status of -P flag
-P was introduced in 4.4BSD-Lite2 around 1994.  It overwrote file contents
with a pass of 0xff, 0x00, then 0xff, in a low effort attempt to "really
delete" files.

It has no user-visible effect; at the end of the day, the file is unlinked via
the filesystem.  Furthermore, the utility of overwriting files with patterned
data is extremely limited due to caveats at every layer of the stack[0] and
therefore mostly futile.  At the least, three passes is likely wasteful on
modern hardware[1].  It could also be seen as a violation of the "Unix
Philosophy" to do one thing per tiny, composable program.

Since 1994, FreeBSD has left it alone; OpenBSD replaced it with a single
pass of arc4random(3) output in 2012[2]; and NetBSD implemented partial, but
explicitly incomplete support for U.S. DoD 5220.22-M, "National Industrial
Security Program Operating Manual" in 2004[3].

NetBSD's enhanced comment above rm_overwrite makes a strong case for removing
the flag entirely:

> This is an expensive way to keep people from recovering files from your
> non-snapshotted FFS filesystems using fsdb(8).  Really.  No more.
>
> It is impossible to actually conform to the exact procedure given in
> [NISPOM] if one is overwriting a file, not an entire disk, because the
> procedure requires examination and comparison of the disk's defect lists.
> Any program that claims to securely erase *files* while conforming to the
> standard, then, is not correct.
>
> Furthermore, the presence of track caches, disk and controller write
> caches, and so forth make it extremely difficult to ensure that data have
> actually been written to the disk, particularly when one tries to repeatedly
> overwrite the same sectors in quick succession.  We call fsync(), but
> controllers with nonvolatile cache, as well as IDE disks that just plain lie
> about the stable storage of data, will defeat this.
>
> [NISPOM] requires physical media destruction, rather than any technique of
> the sort attempted here, for secret data.

As a first step towards evental removal, make it a placebo.  It's not like
it was serving any security function.  It is not defined in or mentioned by
POSIX.

If you are security conscious and need to erase your files, use a
woodchipper.  At a minimum, the entire disk needs to be overwritten, not
just one file.

[0]: https://www.ru.nl/publish/pages/909282/draft-paper.pdf
[1]: https://commons.erau.edu/cgi/viewcontent.cgi?article=1131&context=jdfsl
[2]: https://github.com/openbsd/src/commit/7c5c57ba81b5fe8ff2d4899ff643af18c
[3]: https://github.com/NetBSD/src/commit/fdf0a7a25e59af958fca1e2159921562cd

Reviewed by:	markj, Daniel O'Connor <darius AT dons.net.au> (previous version)
Differential Revision:	https://reviews.freebsd.org/D17906
2018-11-10 20:26:55 +00:00
Ed Maste
19b4f0dca0 Fix unlink(1) for files starting with -
Restore the original behavior of unlink(1), passing the provided filename
directly to unlink(2), handling the first argument being "--" correctly.

This fixes "unlink -foo", broken in r97533.

PR:		228448
Submitted by:	Brennan Vincent <brennan@umanwizard.com> (original version)
Submitted by:	Yuri Pankov
Reported by:	Brennan Vincent <brennan@umanwizard.com>
Reviewed by:	emaste, kevans, vangyzen, 0mp
Approved by:	re (delphij)
Differential Revision: https://reviews.freebsd.org/D17132
2018-09-12 19:41:16 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Enji Cooper
6f74a1c952 Fix cosmetic nit when printing out "override $mode" and "$owner/$group ..."
The wrong index was being checked for == ' ' in the resulting stringified
mode from strmode(3) -- it should have been the 11th value, not the 10th.

MFC after:	3 days
PR:		76711
Submitted by:	Vasil Dimov <vd@datamax.bg>
2017-11-05 21:43:26 +00:00
Warner Losh
fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Baptiste Daroussin
8e5c71e2d6 Protecting against rm -rf / is now POSIXLY_CORRECT per posix 1003.1
edition 2013. No need anymore to disable the protection if one set
the POXILY_CORRECT environment variable.

Reviewed by:	imp
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D4092
2015-11-07 02:18:19 +00:00
Xin LI
b81cd10773 Respect locale settings.
MFC after:	2 weeks
2015-08-28 00:49:30 +00:00
Warner Losh
413a368c90 rm -rf can fail sometimes with an error from fts_read. Make it honor
fflag to ignore fts_read errors, but stop deleting from that directory
because no further progress can be made.

When building a kernel with a high -j value on a high core count
machine, during the cleanobj phase we can wind up doing multiple rm
-rf at the same time for modules that have subdirectories. This
exposed this race (sometimes) as fts_read can return an error if the
directory is removed by another rm -rf. Since the intent of the -f
flag was to ignore errors, even if this was a bug in fts_read, we
should ignore the error like we've been instructed to do.
2014-07-07 23:21:20 +00:00
Eitan Adler
4c99904b9a Add two more 'static' qualifiers 2013-04-26 17:56:35 +00:00
Eitan Adler
9d6d3f96d0 Take some improvements from DragonFlyBSD:
- add const where appropriate
	- add static where appropriate
	- fix a whitespace issues

Reviewed by:	brooks
Obtained from:	DragonFlyBSD
MFC After:	1 week
2013-04-26 17:45:40 +00:00
Eitan Adler
d4319e7433 Add -x option to avoid crossing mount points when removing a hierarchy.
Discussed on:	-hackers
Inspired by:	DragonflyBSD
MFC After:	1 week
2013-04-26 17:45:37 +00:00
Matthew D Fleming
6db1a7f11e Fix bin/ build with a 64-bit ino_t.
Original code by:	Gleb Kurtsou
2012-09-27 23:31:12 +00:00
Xin LI
930e323894 Polish previous revision: if the fts_* routines have lstat()'ed the
directory entry then use the struct stat from that instead of doing
it again, and skip the rm_overwrite() call if fts_read() indicated
that the entry couldn't be a regular file.

Obtained from:	OpenBSD
MFC after:	1 week
2012-06-20 21:10:38 +00:00
Kevin Lo
4ec1405b39 Fix potential symlink race condition in "rm -P" by adding a check
that the file we have opened is the one we expected.  Also open in
non-blocking mode to avoid a potential hang with FIFOs.

Obtained from:	NetBSD via OpenBSD
2012-06-20 02:21:53 +00:00
Ed Schouten
f9d4afb439 Put some static keywords in the source code.
For these simple utilities, it doesn't harm to make all global variables
static. In fact, this allows the compiler to perform better forms of
optimisation and analysis.
2011-10-31 08:59:17 +00:00
Jilles Tjoelker
0cf90cd1d3 bin: Prefer strrchr() to rindex().
This removes the last index/rindex usage from /bin.
2011-03-15 22:22:11 +00:00
Ulrich Spörlein
a3800f8f0e rm(1): clarify that -P works only when blocks are updated in-place
Suggested by:	pjd, ivoras, arundel
2010-10-08 15:20:20 +00:00
Jilles Tjoelker
6911f596a1 rm, find -delete: fix removing symlinks with uchg/uappnd set.
Formerly, this tried to clear the flags on the symlink's target
instead of the symlink itself.

As before, this only happens for root or for the unlink(1) variant of rm.

PR:		bin/111226 (part of)
Submitted by:	Martin Kammerhofer
Approved by:	ed (mentor)
MFC after:	3 weeks
2009-05-30 10:42:19 +00:00
Warner Losh
b5260db685 Implement ^T support for rm: now it will report the next file it
removes when you hit ^T.  This is similar to what's done for cp.  The
signal handler and type definitions for "info" were borrowed directly
from cp.
2009-04-29 18:08:18 +00:00
Xin LI
86da4a5eea Correct a security issue introduced in previous commit:
instead of removing the file and issue a warning about
the removal, do not do any operation at all in case -P
is specified when the dinode has hard links.

With -f and -P specified together, we assume that the
user wants rm to overwrite the contents of the file
and remove it (destroy the contents of file but leave
its hard links as is).

The reason of doing it this way is that, in case where
a hard link is created by a malicious user (currently
this is permitted even if the user has no access to the
file).  Losing the link can potentially mean that the
actual owner would lose control completely to the user
who wants to obtain access in a future day.

Discussed with:	Peter Jermey
2006-10-31 02:22:36 +00:00
Xin LI
0b6f55b77c Be more reasonable when overwrite mode is specified while there
is hard links.  Overwritting when links > 1 would cause data
loss, which is usually undesired.

Inspired by:	discussion on -hackers@
Suggested by:	elessar at bsdforen de
Obtained from:	OpenBSD
2006-10-30 03:32:09 +00:00
Maxim Konovalov
da29c6560b o Backout rev. 1.55. Don't waste cpu cycles for bzero(), do not
call chflags() for whiteouted files.

Prodded by:	ru
2006-10-18 13:16:06 +00:00
Maxim Konovalov
cf1db7b34f o Zero out struct stat before usage. lstat(2) can fail and
leave garbage there which will break -W code path.

PR:		bin/84569
Submitted by:	Igor
MFC after:	2 weeks
2006-10-18 08:22:33 +00:00
Maxim Konovalov
089418247a o Be pedantic and do fts_close() when done.
PR:		bin/95292
Submitted by:	Charles Hardin
Obtained from:	NetBSD via OpenBSD, PR
2006-04-15 09:26:23 +00:00
Doug Barton
a5f6295013 Handle the case where the -P flag is specified for a read-only file
earlier, and more gracefully. Previously, this combination would be
ignored early in the code where permissions are tested and fail later
with a very unhelpful "permission denied" error.

Instead, test for this flag in the same block that generates the
"override?" messages for read-only files, but instead of trying
to guess what the user has in mind, generate an error and exit.

Update the man page to reflect this new behavior.

Not objected to by:	freebsd-hackers@
2005-09-29 20:40:29 +00:00
Jordan K. Hubbard
de3abdfaf4 UNIX conformance: If -r -f on non-existent directory, don't emit error. 2004-11-13 04:07:01 +00:00
Xin LI
24c0f7385b Add -I, an option that asks for confirmation once if recursively
removing directories or if more than 3 files are listed in the
command line.

This feature is intended to provide a safe net but not being too
annoying like having "rm -i" for every deleting operations, and
is generally good for both newbies and power users, preventing
them from being so easily run into ``rm -rf /'', ``rm -rf *''
and so forth.

Originally implemented by Matthew Dillon for DragonFly, plus
some improvements done by various DragonFly contributors.

Approved by:	murray (mentor; the original dillon's version)
Discussed with:	des
Obtained from:	DragonFly's bin/rm/
		rm.c rev. 1.4 - 1.8
		rm.1 rev. 1.3 - 1.4
MFC After:	1 month
2004-10-28 08:25:30 +00:00
Dag-Erling Smørgrav
3f91ab9262 The previous commit added code to rm(1) to warn about and remove any
occurrences of "/" in the argument list.  This corresponds to Enhancement
Request Number 5 in the Austin Group TC2 Aardvark's XCU Defects Report
(<URL:http://www.opengroup.org/austin/aardvark/finaltext/xcubug.txt>).
Further discussion is available in the Austin Group mailing list archives
(<URL:http://www.opengroup.org/austin/mailarchives/>, "Defect in XCU rm")
and for Austin Group members, in the Austin Group Interpretations archive
(<URL:http://www.opengroup.org/austin/interps/>, AI-019)

This commit makes that check conditional on !POSIXLY_CORRECT, since it
is not strictly correct according to the current version of the standard
(but is expected to be correct according to the next version, and has
already been adopted by Solaris).
2004-10-04 19:24:28 +00:00
Dag-Erling Smørgrav
68ef5f71b0 Find out how flame-proof my underwear really is. 2004-10-04 11:26:01 +00:00
Dag-Erling Smørgrav
c335b1ecdb Whitespace cleanup. 2004-10-04 11:14:12 +00:00
Mark Murray
6195fb4102 Remove clause 3 from the UCB licenses.
OK'ed by:	imp, core
2004-04-06 20:06:54 +00:00
Jun Kuriyama
b800f53d91 o Fix a style bug and poor wording in comment.
o When fts_read() cannot stat the file, it can't be unlinked.  At
  that case, don't display error message when -f flag is used.

Obtained from:	bde
PR:		kern/16815, bin/35842
Reported by:	kuriyama, Aleksandr A. Babaylov <.@babolo.ru>
2004-01-01 10:26:43 +00:00
Guido van Rooij
f3761deee3 When the P flag is set (i.e. Overwrite regular files before deleting them),
do only unlink the file if we could indeed overwrite the file.
Old behaviour: rm -P /tmp/foo (foo mode 0444) would NOT overwrite foo,
but still delete it (with a warning: rm: foo: Permission denied)
New behaviour: Just the EPERM warning, but no deletion

Reviewed by:	bde
2003-11-10 09:40:18 +00:00
Bruce Evans
8f1f433834 1. Fixed leakage of a file descriptor for every non-fatal failure in
rm_overwrite() (for rm -P).

2. Print the file name in the error message for (fatal) malloc() failures
   in rm_overwrite().  I first thought that malloc() failures should be
   non-fatal since they don't prevent proceeding the the next file, but
   making them non-fatal would normally give too much output for rm -Pr
   on a large tree in the unlikely event that even one occurs, since the
   malloc()ed amounts are usually the same.  Just print the file name since
   the malloc()ed amounts are not always the same and it doesn't hurt to
   know where rm was when it quit.

Submitted by:	guido ((1) and original version of (2))
2003-11-08 09:55:16 +00:00
David E. O'Brien
09a80d4867 Quiet warnings about copyright[]. 2003-05-01 16:58:57 +00:00
Tom Rhodes
7d971bbf29 s/filesystem/file system/ as discussed on -developers 2002-08-21 17:32:44 +00:00
Tim J. Robbins
90833c99de Complain if more than one file argument is given to unlink(1) like we did
before I made unlink use getopt().
2002-07-12 07:20:20 +00:00
Matthew Dillon
5ad9e45f96 err() is documented as allowing NULL for the format string but GCC isn't
happy about it any more so change the usage to make buildworld work again.
2002-07-10 20:44:55 +00:00
David E. O'Brien
2749b14129 Consistently use FBSDID 2002-06-30 05:15:05 +00:00
Tim J. Robbins
e9393a92a6 Reject options, handle "--" correctly in unlink(1). 2002-05-30 01:05:15 +00:00
Tom Rhodes
ebd4324457 Consistancy check s/file system/filesystem/
Reviewed by:	brian
2002-05-16 01:57:20 +00:00
Warner Losh
fc69394f0a Move user_from_uid to pwd.h
Move group_from_gid to grp.h
Remove from stdlib.h
Make the prototypes match the code
Fix rm and mv to include new files.

NetBSD has these defined in those files, and others too that I've not
done.

Approved by: terminal room kabal
Reviewed by: jhb, phk
2002-02-14 01:59:47 +00:00
Warner Losh
46251dde8f o __P has been reoved
o Old-style K&R declarations have been converted to new C89 style
o register has been removed
o prototype for main() has been removed (gcc3 makes it an error)
o int main(int argc, char *argv[]) is the preferred main definition.
o Attempt to not break style(9) conformance for declarations more than
  they already are.
2002-02-02 06:48:10 +00:00
Luigi Rizzo
69393d0646 Add prototypes for main() so that these programs compile with -Werror
(which somehow now seems to be the default for compiling -current).
This error popped up while doing a PicoBSD cross-compile on a 4.3-ish system,
it may well be that there are other apps which have similar problems,
but I did not spot them as they are not included in my picobsd config.

Whether adding prototypes for main() is the correct solution or not
I have no idea, a request to -current on the matter went basically
unanswered. Those who have better ideas are welcome to back this out
and replace it with the correct fix.
2001-12-14 16:22:41 +00:00
Ruslan Ermilov
754e501c71 Removed wrong cast for fts_open()'s third argument. 2001-06-13 15:01:25 +00:00
David E. O'Brien
b6f80a8e3d Display pathname of item being rm'ed.
Submitted by:	Peter Pentchev <roam@orbitel.bg>
2000-12-20 08:31:26 +00:00
Josef Karthauser
141d77b8cb Switch over to using the new fflagstostr and strtofflags library calls. 2000-06-17 14:19:33 +00:00
Jeroen Ruigrok van der Werven
69757cf969 Remove unused #include. 2000-05-01 18:34:36 +00:00
Josef Karthauser
418d67b0d9 Revert part of the last commit, remove {g|s}etflags from the libc
interface, and statically link them to the programs using them.
These functions, upon reflection and discussion, are too generically
named for a library interface with such specific functionality.
Also the api that they use, whilst ok for private use, isn't good
enough for a libc function.

Additionally there were complications with the build/install-world
process.  It depends heavily upon xinstall, which got broken by
the change in api, and caused bootstrap problems and general mayhem.

There is work in progress to address future problems that may be
caused by changes in install-chain tools, and better names for
{g|s}etflags can be derived when some future program requires them.
For now the code has been left in src/lib/libc/gen (it started off
in src/bin/ls).

It's important to provide library functions for manipulating file
flag strings if we ever want this interface to be adopted outside
of the source tree, but now isn't necessarily the right moment
with 4.0-release just around the corner.

Approved:	jkh
2000-02-05 18:42:36 +00:00
Josef Karthauser
18c0eeddf7 Historically file flags (schg, uschg, etc) have been converted from
string to u_long and back using two functions, flags_to_string and
string_to_flags, which co-existed with 'ls'.  As time has progressed
more and more other tools have used these private functions to
manipulate the file flags.

Recently I moved these functions from /usr/src/bin/ls to libutil,
but after some discussion with bde it's been decided that they
really ought to go in libc.

There are two already existing libc functions for manipulating file
modes:  setmode and getmode.  In keeping with these flags_to_string
has been renamed getflags and string_to_flags to setflags.

The manual page could probably be improved upon ;)
2000-01-27 21:17:01 +00:00