Hi,
found it while copying lots of audiobooks to player devices for library
loan.
I'll append the llm puke below and at least can confirm that the suggested
patch fixes it on a custom kernel.
Subject: msdosfs: readdir(3) returns the 8.3 name instead of the long name
when a getdents(2) buffer fills exactly
>Synopsis: msdosfs_readdir() discards pending Win95 long name state
when
the caller's buffer runs out, returning the 8.3 short name
for
one file per getdents(2) call boundary
>Category: kernel
>Environment:
System : OpenBSD X.Y
Details : OpenBSD X.Y-current (GENERIC.MP) #N: ...
Architecture: OpenBSD.amd64
Machine : amd64
>Description:
In sys/msdosfs/msdosfs_vnops.c, msdosfs_readdir()'s inner loop over
directory entries is bounded by "on + n", where
n = min(pmp->pm_bpcluster - on, uio->uio_resid);
n counts *output* bytes (uio_resid) but is used to bound *directory*
bytes, and it is not a multiple of sizeof(struct direntry), so the bound
rounds up to cover one more 32-byte on-disk entry than the output budget
actually allows.
The loop has no uio_resid termination check -- it only tests for space
when it is about to emit a dirent -- while Win95 long name slots, deleted
entries and volume labels advance offset without consuming any uio_resid.
So when the last dirent emitted exhausts uio_resid exactly, the loop runs
one more iteration. If that entry is the WIN_LAST slot of the next long
name, win2unixfn() latches the checksum, offset advances past it, the
outer loop then exits on uio_resid == 0, and out: writes uio_offset
without consulting wlast. The wlast rewind added for the related "dirent
does not fit" case is only reachable from the branch at the short entry,
so it does not cover this exit path.
The next getdents(2) therefore resumes on the *second* long name slot
with chksum == -1. win2unixfn() sees a slot without WIN_LAST, hits
} else if (chksum != wep->weChksum)
chksum = -1;
and returns -1, so at the short entry chksum != winChksum(dentp->deName)
and the entry is rendered by dos2unixfn(): readdir(3) reports the 8.3
name.
msdosfs_lookup() is unaffected -- it scans whole clusters with no user
buffer involved and can never be truncated mid-group -- so stat(2) on the
long name still succeeds while the name is missing from the directory
listing. It is one file, not two: both names resolve to the same denode.
This is not specific to the odd media it was found on. Sector sizes are
normalised to DEV_BSIZE at mount time (msdosfs_vfsops.c), and pcbmap()
returns blsize == pm_bpcluster for directories, so the sector size only
matters in that it sets the cluster size, hence st_blksize (= va_blocksize
= pm_bpcluster), hence the getdents(2) buffer libc picks in opendir.c:
dirp->dd_len = ((int)sb.st_blksize * 4 + pageoffset) & ~pageoffset;
With a smaller buffer the defect is much worse: modelling the loop with a
96-byte getdents(2) buffer over a 600-entry directory yields 145 wrong
names and 144 duplicated entries.
>How-To-Repeat:
Originally seen on a USB stick whose controller reports 1024-byte logical
sectors (FAT32 by newfs_msdos: BytesPerSec=1024, SecPerClus=4,
Reserved=16, NumFATs=2, FATSz32=8011, RootClus=2), mounted -o long.
In an empty directory:
for i in $(seq 1 300); do
n=$(printf 'dam12681_%04d' $i)
touch $n.mp3 $n.smil
done
ls -1 | grep '~' # prints DAM1~171.MP3
ls -1 | wc -l # 600
stat dam12681_0171.mp3 # succeeds
Always exactly that file. It is the 341st name; st_blksize is 4096, so
the libc buffer is 16384 = 32 (".") + 32 ("..") + 340 * 48, and it is the
first entry of the second getdents(2) call.
Reproduced on ordinary 512-byte-sector media via vnd(4), which makes for
a cheap regression test (512-byte clusters move the failure to the 85th
file):
dd if=/dev/zero of=/tmp/fat.img bs=1m count=8
vnconfig vnd0 /tmp/fat.img
newfs_msdos -F 16 -S 512 -c 1 /dev/rvnd0c
mount_msdos /dev/vnd0c /mnt
mkdir /mnt/t && cd /mnt/t # not the root dir: FAT16 has only
# 512 slots there, i.e. 170 long
names
for i in $(jot 300 1); do touch $(printf 'f%012d.mp3' $i); done
ls -l | grep '~'
gives
-rw-r--r-- 1 root wheel 0 Nov 23 12:09 F0000~85.MP3
F0000~85.MP3 is the 8.3 form of f000000000085.mp3: uniqdosname() hands
out generation numbers in creation order, so with the names numbered from
1 the generation and the name agree. "ls -1 | wc -l" is 300,
"ls -1 | grep -c f000000000085.mp3" is 0, and stat(1) on either name
returns the same inode -- it is one file whose long name is not listed.
Here st_blksize is 512, so the libc buffer is 4096 = 32 + 32 + 84 * 48:
84 names are emitted, and the 85th is again the first entry of the second
getdents(2) call. Note the test only reproduces in a *fresh* directory.
>Fix:
Stop the loop as soon as the caller's buffer is full, before consuming
any further entry. offset is incremented in the for() increment, so at
the top of the body it still refers to dentp and out: records the correct
uio_offset; the next call then restarts at the group's first slot.
Rewinding to wlast at out: would also fix this case, but it would fire on
the SLOT_EMPTY and diff <= 0 exits too, where an orphaned trailing long
name group would make getdents(2) return 0 bytes indefinitely rather than
ending the directory. Rounding n down to a multiple of sizeof(struct
direntry) can yield n == 0 and loop forever.
Tested on amd64 with a kernel carrying this diff: the vnd(4) case above
lists all 300 long names, with nothing falling back to 8.3. Separately
modelling the loop over buffer sizes 32..32768, cluster sizes 512..32768,
name lengths 17..40 and 4..1500 files shows no wrong, missing or
duplicated names either. The existing wlast rewind is still required,
and still correct, for the "uio_resid nonzero but smaller than d_reclen"
case.
Index: sys/msdosfs/msdosfs_vnops.c
===================================================================
--- sys/msdosfs/msdosfs_vnops.c
+++ sys/msdosfs/msdosfs_vnops.c
@@ -1538,6 +1538,21 @@
dentp, prev, crnt, dentp->deName[0],
dentp->deAttributes);
#endif
/*
+ * Stop as soon as the caller's buffer is full.
+ * Entries that produce no output, in particular
+ * the Win95 long name slots handled below, do
+ * not consume uio_resid, so this loop can run on
+ * past the point where the last dirent was handed
+ * out. Were we to swallow the leading slots of a
+ * long name here, uio_offset would be left in the
+ * middle of the group and the next call would
+ * report the short name instead.
+ */
+ if (uio->uio_resid == 0) {
+ brelse(bp);
+ goto out;
+ }
+ /*
* If this is an unused entry, we can stop.
*/
if (dentp->deName[0] == SLOT_EMPTY) {