Page MenuHomeFreeBSD

D61418.1795641246.diff
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

D61418.1795641246.diff

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -836,6 +836,8 @@
..
fusefs
..
+ msdosfs
+ ..
pjdfstest
..
tarfs
diff --git a/sys/fs/msdosfs/msdosfs_conv.c b/sys/fs/msdosfs/msdosfs_conv.c
--- a/sys/fs/msdosfs/msdosfs_conv.c
+++ b/sys/fs/msdosfs/msdosfs_conv.c
@@ -1137,17 +1137,15 @@
return (ENAMETOOLONG);
}
+ if (id * WIN_CHARS + count + nbp->nb_len > sizeof(nbp->nb_buf))
+ return (ENAMETOOLONG);
+
/*
* Shift suffix upwards or downwards by the amount length differs
* from WIN_CHARS.
*/
- if (count != WIN_CHARS && nbp->nb_len != 0) {
- if ((id * WIN_CHARS + count + nbp->nb_len) >
- sizeof(nbp->nb_buf))
- return (ENAMETOOLONG);
-
+ if (count != WIN_CHARS && nbp->nb_len != 0)
memmove(slot + count, slot + WIN_CHARS, nbp->nb_len);
- }
/* Copy in the substring to its slot and update length so far. */
memcpy(slot, name, count);
diff --git a/tests/sys/fs/Makefile b/tests/sys/fs/Makefile
--- a/tests/sys/fs/Makefile
+++ b/tests/sys/fs/Makefile
@@ -14,6 +14,7 @@
.if ${COMPILER_FEATURES:Mc++14} && ${MK_GOOGLETEST} != "no"
TESTS_SUBDIRS+= fusefs
.endif
+TESTS_SUBDIRS+= msdosfs
TESTS_SUBDIRS+= pjdfstest
TESTS_SUBDIRS+= tarfs
TESTS_SUBDIRS+= tmpfs
diff --git a/tests/sys/fs/msdosfs/Makefile b/tests/sys/fs/msdosfs/Makefile
new file mode 100644
--- /dev/null
+++ b/tests/sys/fs/msdosfs/Makefile
@@ -0,0 +1,7 @@
+PACKAGE= tests
+
+TESTSDIR= ${TESTSBASE}/sys/fs/msdosfs
+
+ATF_TESTS_SH+= mbnambuf_test
+
+.include <bsd.test.mk>
diff --git a/tests/sys/fs/msdosfs/mbnambuf_test.sh b/tests/sys/fs/msdosfs/mbnambuf_test.sh
new file mode 100755
--- /dev/null
+++ b/tests/sys/fs/msdosfs/mbnambuf_test.sh
@@ -0,0 +1,120 @@
+#!/bin/sh
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Osvaldo Janeri Filho <ojanerif@amd.com>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.
+#
+# Sponsored by: Advanced Micro Devices, Inc.
+
+#
+# Regression test for the mbnambuf_write() bounds check (D61418).
+#
+# A malformed LFN winentry with weCnt = WIN_LAST | 20 produces slot
+# id 19, offset 19 * 13 = 247 in the 256-byte nb_buf. Without the
+# bounds check the memcpy writes past the end of the buffer.
+
+atf_test_case lfn_oob_slot cleanup
+lfn_oob_slot_head()
+{
+ atf_set "descr" "mbnambuf_write: malformed LFN with high slot id"
+ atf_set "require.user" "root"
+ atf_set "timeout" "60"
+}
+
+lfn_oob_slot_body()
+{
+ IMG=$(pwd)/lfn_oob.img
+
+ # Create a 4 MB FAT12 image.
+ truncate -s 4m "$IMG" ||
+ atf_fail "cannot create image file"
+ md=$(mdconfig -a -t vnode -f "$IMG") ||
+ atf_fail "mdconfig failed"
+ echo "$md" > md_unit
+
+ atf_check -s exit:0 -o ignore \
+ newfs_msdos -F 12 -h 16 -u 63 /dev/${md}
+
+ # Mount, create a reference file, unmount.
+ mkdir -p mnt
+ atf_check -s exit:0 mount -t msdosfs /dev/${md} mnt
+ atf_check -s exit:0 touch mnt/GOODFILE
+ atf_check -s exit:0 umount mnt
+ mdconfig -d -u ${md#md}
+
+ # Patch the image file directly.
+ # BPB: bps=512 rsvd=2 nfats=2 spf=3 -> rootoff = (2+6)*512 = 4096
+ rootoff=4096
+
+ # Find GOODFILE entry (first byte 'G' = 0x47).
+ off=$rootoff
+ slot=0
+ while [ $slot -lt 64 ]; do
+ b=$(dd if="$IMG" bs=1 skip=$off count=1 2>/dev/null |
+ od -A n -t x1 | tr -d ' ')
+ if [ "$b" = "47" ]; then break; fi
+ off=$((off + 32))
+ slot=$((slot + 1))
+ done
+ [ "$b" = "47" ] ||
+ atf_fail "GOODFILE entry not found in root directory"
+
+ # Checksum of "BAAD TXT".
+ s=0
+ for byte in 66 65 65 68 32 32 32 32 84 88 84; do
+ s=$(( ((s >> 1) + ((s & 1) << 7) + byte) & 0xFF ))
+ done
+ chk=$s
+
+ # Malformed LFN: weCnt = 0x54 (WIN_LAST|20), id=19, slot=247.
+ lfn_off=$((off + 32))
+ printf '\124\115\000\101\000\114\000\106\000\117\000\017\000' \
+ > lfn_raw
+ printf "\\$(printf '%03o' $chk)" >> lfn_raw
+ printf '\122\000\115\000\105\000\104\000\114\000\106\000' \
+ >> lfn_raw
+ printf '\000\000\116\000\041\000' >> lfn_raw
+ dd if=lfn_raw of="$IMG" bs=1 seek=$lfn_off conv=notrunc \
+ 2>/dev/null
+
+ # Short name entry "BAAD TXT".
+ short_off=$((lfn_off + 32))
+ printf 'BAAD TXT\040' > short_raw
+ dd if=/dev/zero bs=1 count=20 >> short_raw 2>/dev/null
+ dd if=short_raw of="$IMG" bs=1 seek=$short_off conv=notrunc \
+ 2>/dev/null
+
+ # Mount read-only and readdir. Must not panic.
+ md=$(mdconfig -a -t vnode -f "$IMG")
+ echo "$md" > md_unit
+ atf_check -s exit:0 mount -t msdosfs -o ro /dev/${md} mnt
+ atf_check -s exit:0 -o ignore ls mnt
+ atf_check -s exit:0 test -f mnt/GOODFILE
+}
+
+lfn_oob_slot_cleanup()
+{
+ umount mnt 2>/dev/null || true
+ if [ -f md_unit ]; then
+ mdconfig -d -u $(sed 's/^md//' md_unit) 2>/dev/null || true
+ fi
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case lfn_oob_slot
+}

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 25, 9:14 PM (18 h, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41961572
Default Alt Text
D61418.1795641246.diff (5 KB)

Event Timeline