FreeBSD read and readdir

152 views
Skip to first unread message

Josh Bleecher Snyder

unread,
Mar 5, 2021, 7:24:16 PM3/5/21
to golang-dev
golang.org/cl/296391 adds a new filesystem test: Calling read on a directory or readdir on a file should fail. This test fails for the package os implementation on FreeBSD. I don’t see anything obviously different between FreeBSD and the other posixes, on which this test passes.

I know nothing about FreeBSD. Is this intended behavior? If not, is the bug in package os or in the OS? If the latter, should we work around it package os?

I’m emailing here because I don’t know who the Go FreeBSD experts are.

Thanks,
Josh

Ian Lance Taylor

unread,
Mar 5, 2021, 7:38:14 PM3/5/21
to Josh Bleecher Snyder, golang-dev
I'll note that the FreeBSD read(2) man page says "Directories may only
be read directly if the filesystem supports it and the
security.bsd.allow_read_dir sysctl MIB is set to a non-zero value."
POSIX says that read should return EISDIR if "The fildes argument
refers to a directory and the implementation does not allow the
directory to be read using read() or pread()". So it seems that POSIX
permits calling read on a directory, and FreeBSD permits that at least
in some cases.

So it's not clear to me that that part of CL 296391 is correct.

Ian

Josh Bleecher Snyder

unread,
Mar 5, 2021, 7:55:10 PM3/5/21
to Ian Lance Taylor, golang-dev
That’s insofar as io/fs should match posix.

Package os is supposed to be mostly platform-independent. I’d say io/fs even more so. I don’t know what read of a directory could mean in a platform-independent way.

So I guess I’d lean towards documenting that it isn’t allowed and then fixing it, either in os.DirFS or in os.File.Read.

-josh




Ian Lance Taylor

unread,
Mar 5, 2021, 8:11:43 PM3/5/21
to Josh Bleecher Snyder, golang-dev
I don't know how to do that without adding extra fstat system calls
somewhere. Is it really worth it?

Ian

Josh Bleecher Snyder

unread,
Mar 5, 2021, 8:36:47 PM3/5/21
to Ian Lance Taylor, golang-dev
Hmm. I suppose not. Although we could restrict the extra syscall to os.DirFS on FreeBSD.

-josh 

Bakul Shah

unread,
Mar 5, 2021, 10:26:32 PM3/5/21
to Ian Lance Taylor, Josh Bleecher Snyder, golang-dev
The security.bsd.allow_read_dir sysctl was added FreeSD-current on
June 4, 2020. It was merged into the stable/12 branch on Jul 8 so it
is not in -12.1 or earlier FreeBSD releases. On UFS filesystem you could
always read a directory until this sysctl came along. As the FreeBSD
commit log says,

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.

[Speaking just for myself] I'd be okay with just documenting the behavior.
Reply all
Reply to author
Forward
0 new messages