Bug 287451 - [/bin/ls] readdir() errors delivered via fts_children() are silently discarded
Summary: [/bin/ls] readdir() errors delivered via fts_children() are silently discarded
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Kyle Evans
URL: https://reviews.freebsd.org/D51056
Keywords:
Depends on:
Blocks:
 
Reported: 2025-06-11 15:32 UTC by Maxim Suhanov
Modified: 2026-04-12 13:57 UTC (History)
3 users (show)

See Also:
kevans: mfc-stable15+
kevans: mfc-stable14+
kevans: mfc-stable13-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Maxim Suhanov 2025-06-11 15:32:51 UTC
Hello.

# Problem

When running the 'ls' command against an unreadable directory, its exit code is zero, and no read error is printed.

Example:

root@testhost:~ # mount | grep -F /mnt
/dev/da1p1.eli on /mnt (ufs, local, soft-updates)
root@testhost:~ # ls /mnt
.snap	test	test2
root@testhost:~ # ls /mnt/test2
1.txt
root@testhost:~ # ls /mnt/test
root@testhost:~ # echo $?
0
root@testhost:~ # truss ls -l /mnt/test/ 2>&1 | tail
fstat(4,{ mode=drwxr-xr-x ,inode=98304,size=26624,blksize=32768 }) = 0 (0x0)
fchdir(0x4)					 = 0 (0x0)
getdirentries(4,0x5743bd843000,4096,0x5743bd840028) ERR#97 'Integrity check failed'
close(4)					 = 0 (0x0)
fchdir(0x3)					 = 0 (0x0)
fstat(1,{ mode=p--------- ,inode=708,size=0,blksize=4096 }) = 0 (0x0)
total 0
write(1,"total 0\n",8)				 = 8 (0x8)
exit(0x0)					
process exit, rval = 0
root@testhost:~ # 

Compare this to the 'find' command (which reports the error properly):

root@testhost:~ # find /mnt/
/mnt/
/mnt/.snap
/mnt/test
find: /mnt/test: Integrity check failed
/mnt/test2
/mnt/test2/1.txt
root@testhost:~ # echo $?
1
root@testhost:~ # 

# Root cause

The bug is in 'bin/ls/ls.c':

static void
traverse(int argc, char *argv[], int options)
{
	FTS *ftsp;
	FTSENT *p, *chp;
	int ch_options;

	if ((ftsp =
	    fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL)
		err(1, "fts_open");

	[...]

	chp = fts_children(ftsp, 0);
	if (chp != NULL)
		display(NULL, chp, options);
	if (f_listdir) {
		fts_close(ftsp);
		return;
	}

	[...]

	while (errno = 0, (p = fts_read(ftsp)) != NULL)
		switch (p->fts_info) {
		case FTS_DC:
			[...]
			break;
		case FTS_DNR:
		case FTS_ERR:
			[...]
			break;
		case FTS_D:
			if (p->fts_level != FTS_ROOTLEVEL &&
			    p->fts_name[0] == '.' && !f_listdot)
				break;

			[...]

			chp = fts_children(ftsp, ch_options); // BUG!
			display(p, chp, options);

			[...]
			break;
		default:
			break;
		}
	if (errno) // 'errno' is 0 after the loop!
		err(1, "fts_read");
	fts_close(ftsp);
}

The second call to 'fts_children()' returns NULL and 'errno' is set to a non-zero value, but it's not checked in the code.

Subsequent calls (i.e., to 'fts_read()' in the while loop) reset the 'errno' value, so it becomes zero after the loop.
Comment 1 commit-hook freebsd_committer freebsd_triage 2026-02-11 19:57:22 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7bf81e39d83087dc7f984077b5eed5a48df794d4

commit 7bf81e39d83087dc7f984077b5eed5a48df794d4
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-02-11 19:55:55 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-02-11 19:56:37 +0000

    ls: check fts_children() for errors that may not surface otherwise

    In particular, if one simply does a non-recursive `ls` on a directory
    that is not accessible, there are some classes of errors that may cause
    it to fail that wouldn't be surfaced unless we do an fts_read() that
    will recurse into the inaccessible directory.  Catch those kinds of
    errors here since we cannot expect to an FTS_ERR/FTS_DNR entry to follow
    up on them.

    PR:             287451
    Reviewed by:    kib
    Discusssed with:        des
    Differential Revision:  https://reviews.freebsd.org/D51056

 bin/ls/ls.c              | 17 +++++++++++++++++
 bin/ls/tests/ls_tests.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
Comment 2 commit-hook freebsd_committer freebsd_triage 2026-04-12 03:17:01 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7bb6c194c9695377a643b26b59be4af4852c8227

commit 7bb6c194c9695377a643b26b59be4af4852c8227
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-02-11 19:55:55 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-04-12 03:15:15 +0000

    ls: check fts_children() for errors that may not surface otherwise

    In particular, if one simply does a non-recursive `ls` on a directory
    that is not accessible, there are some classes of errors that may cause
    it to fail that wouldn't be surfaced unless we do an fts_read() that
    will recurse into the inaccessible directory.  Catch those kinds of
    errors here since we cannot expect to an FTS_ERR/FTS_DNR entry to follow
    up on them.

    PR:             287451
    Reviewed by:    kib
    Discusssed with:        des

    (cherry picked from commit 7bf81e39d83087dc7f984077b5eed5a48df794d4)

 bin/ls/ls.c              | 17 +++++++++++++++++
 bin/ls/tests/ls_tests.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
Comment 3 commit-hook freebsd_committer freebsd_triage 2026-04-12 13:45:25 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=6c92918fa159e974322c5e09ac6cffa9d807b0f2

commit 6c92918fa159e974322c5e09ac6cffa9d807b0f2
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-02-11 19:55:55 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-04-12 13:43:45 +0000

    ls: check fts_children() for errors that may not surface otherwise

    In particular, if one simply does a non-recursive `ls` on a directory
    that is not accessible, there are some classes of errors that may cause
    it to fail that wouldn't be surfaced unless we do an fts_read() that
    will recurse into the inaccessible directory.  Catch those kinds of
    errors here since we cannot expect to an FTS_ERR/FTS_DNR entry to follow
    up on them.

    PR:             287451
    Reviewed by:    kib
    Discusssed with:        des

    (cherry picked from commit 7bf81e39d83087dc7f984077b5eed5a48df794d4)

 bin/ls/ls.c              | 17 +++++++++++++++++
 bin/ls/tests/ls_tests.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)