freebsd-dev/bin/sh
Kyle Evans dcef4f65ae vfs: add restrictions to read(2) of a directory [1/2]
Historically, we've allowed read() of a directory and some filesystems will
accommodate (e.g. ufs/ffs, msdosfs). From the history department staffed by
Warner: <<EOF

pdp-7 unix seemed to allow reading directories, but they were weird, special
things there so I'm unsure (my pdp-7 assembler sucks).

1st Edition's sources are lost, mostly. The kernel allows it. The
reconstructed sources from 2nd or 3rd edition read it though.

V6 to V7 changed the filesystem format, and should have been a warning, but
reading directories weren't materially changed.

4.1b BSD introduced readdir because of UFS. UFS broke all directory reading
programs in 1983. ls, du, find, etc all had to be rewritten. readdir() and
friends were introduced here.

SysVr3 picked up readdir() in 1987 for the AT&T fork of Unix. SysVr4 updated
all the directory reading programs in 1988 because different filesystem
types were introduced.

In the 90s, these interfaces became completely ubiquitous as PDP-11s running
V7 faded from view and all the folks that initially started on V7 upgraded
to SysV. Linux never supported this (though I've not done the software
archeology to check) because it has always had a pathological diversity of
filesystems.
EOF

Disallowing read(2) on a directory has the side-effect of masking
application bugs from relying on other implementation's behavior
(e.g. Linux) of rejecting these with EISDIR across the board, but allowing
it has been a vector for at least one stack disclosure bug in the past[0].

By POSIX, this is implementation-defined whether read() handles directories
or not. Popular implementations have chosen to reject them, and this seems
sensible: the data you're reading from a directory is not structured in some
unified way across filesystem implementations like with readdir(2), so it is
impossible for applications to portably rely on this.

With this patch, we will reject most read(2) of a dirfd with EISDIR. Users
that know what they're doing can conscientiously set
bsd.security.allow_read_dir=1 to allow read(2) of directories, as it has
proven useful for debugging or recovery. A future commit will further limit
the sysctl to allow only the system root to read(2) directories, to make it
at least relatively safe to leave on for longer periods of time.

While we're adding logic pertaining to directory vnodes to vn_io_fault, an
additional assertion has also been added to ensure that we're not reaching
vn_io_fault with any write request on a directory vnode. Such request would
be a logical error in the kernel, and must be debugged rather than allowing
it to potentially silently error out.

Commented out shell aliases have been placed in root's chsrc/shrc to promote
awareness that grep may become noisy after this change, depending on your
usage.

A tentative MFC plan has been put together to try and make it as trivial as
possible to identify issues and collect reports; note that this will be
strongly re-evaluated. Tentatively, I will MFC this knob with the default as
it is in HEAD to improve our odds of actually getting reports. The future
priv(9) to further restrict the sysctl WILL NOT BE MERGED BACK, so the knob
will be a faithful reversion on stable/12. We will go into the merge
acknowledging that the sysctl default may be flipped back to restore
historical behavior at *any* point if it's warranted.

[0] https://www.freebsd.org/security/advisories/FreeBSD-SA-19:10.ufs.asc

PR:		246412
Reviewed by:	mckusick, kib, emaste, jilles, cy, phk, imp (all previous)
Reviewed by:	rgrimes (latest version)
MFC after:	1 month (note the MFC plan mentioned above)
Relnotes:	absolutely, but will amend previous RELNOTES entry
Differential Revision:	https://reviews.freebsd.org/D24596
2020-06-04 18:09:55 +00:00
..
bltin General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
funcs Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
tests sh: Allow more scripts without #! 2020-05-30 16:00:49 +00:00
alias.c sh: Fix use after free when resetting an in-use alias. 2017-04-16 22:10:02 +00:00
alias.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
arith_yacc.c sh: Restore $((x)) error checking after fix for $((-9223372036854775808)) 2019-02-10 22:23:05 +00:00
arith_yacc.h sh: Restore $((x)) error checking after fix for $((-9223372036854775808)) 2019-02-10 22:23:05 +00:00
arith_yylex.c sh: Restore $((x)) error checking after fix for $((-9223372036854775808)) 2019-02-10 22:23:05 +00:00
arith.h sh: Remove unused function declaration for arith_lex_reset(). 2017-03-18 20:41:07 +00:00
builtins.def sh: Remove remnants to compile out fc completely 2020-04-22 21:45:43 +00:00
cd.c sh: Fix rare memory leak with SIGINT 2020-01-01 12:06:37 +00:00
cd.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
dot.profile Install .shrc for root, and set PS1 for the toor account. 2019-01-24 23:34:51 +00:00
dot.shrc vfs: add restrictions to read(2) of a directory [1/2] 2020-06-04 18:09:55 +00:00
error.c sh: Unify EXERROR and EXEXEC 2018-11-09 14:58:24 +00:00
error.h sh: Unify EXERROR and EXEXEC 2018-11-09 14:58:24 +00:00
eval.c sh: Unify EXERROR and EXEXEC 2018-11-09 14:58:24 +00:00
eval.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
exec.c sh: Allow more scripts without #! 2020-05-30 16:00:49 +00:00
exec.h sh: Use exitstatus instead of exerrno to pass EXEXEC status 2018-10-27 20:17:57 +00:00
expand.c sh: Fix ${param?} default error message 2018-11-28 20:03:53 +00:00
expand.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
histedit.c Fix build with NO_HISTORY set 2020-04-21 00:37:55 +00:00
input.c sh: remove duplicate el definition 2020-03-28 17:02:32 +00:00
input.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
jobs.c sh: Fix double INTON with vfork 2020-05-16 16:29:23 +00:00
jobs.h sh: Move various structs from jobs.h to jobs.c 2018-01-01 22:31:52 +00:00
mail.c sh: Don't use padvance() for MAIL/MAILPATH 2018-07-15 09:14:30 +00:00
mail.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
main.c sh: Unify EXERROR and EXEXEC 2018-11-09 14:58:24 +00:00
main.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
Makefile Bump WARNS for sh(1). 2020-04-01 15:12:51 +00:00
Makefile.depend DIRDEPS_BUILD: Update dependencies. 2017-10-31 00:07:04 +00:00
memalloc.c sh: Assert INTOFF rather than applying it in ck* 2020-04-28 20:34:27 +00:00
memalloc.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
miscbltin.c sh: Remove a comment that was obsoleted by r358152 2020-05-22 14:46:23 +00:00
mkbuiltins sh: Remove remnants to compile out fc completely 2020-04-22 21:45:43 +00:00
mknodes.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
mksyntax.c sh: Split CNL syntax category to avoid a check on state[level].syntax 2018-05-21 21:52:48 +00:00
mktokens Don't hardcode /usr/bin as the path for mktemp in build tools 2018-02-06 15:41:35 +00:00
myhistedit.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
mystring.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
mystring.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
nodes.c.pat Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
nodetypes Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
options.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
options.h sh: Add set -o pipefail 2019-02-24 21:05:13 +00:00
output.c sh: Send normal output from bind builtin to stdout 2019-02-19 21:27:30 +00:00
output.h sh: Send normal output from bind builtin to stdout 2019-02-19 21:27:30 +00:00
parser.c Make sh(1) support \u in PS1. This removes one fork/exec on interactive 2019-01-24 11:59:46 +00:00
parser.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
profile Don't mess with BLOCKSIZE in shell startup files - it's set by login.conf(5); 2019-01-20 22:08:49 +00:00
redir.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
redir.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
sh.1 sh: Add set -o pipefail 2019-02-24 21:05:13 +00:00
shell.h sh: Restore $((x)) error checking after fix for $((-9223372036854775808)) 2019-02-10 22:23:05 +00:00
show.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
show.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
TOUR sh: Unify EXERROR and EXEXEC 2018-11-09 14:58:24 +00:00
trap.c General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
trap.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00
var.c sh: Do not place exported but unset variables into the environment 2019-01-03 20:22:35 +00:00
var.h General further adoption of SPDX licensing ID tags. 2017-11-20 19:49:47 +00:00