Issue #3482 has been reported by tuxillo.

----------------------------------------
Bug #3482: libc: a damaged hash(3) database page makes dbm and dbopen write and 
read out of bounds
http://bugs.dragonflybsd.org/issues/3482

* Author: tuxillo
* Status: New
* Priority: Normal
* Target version: 6.6
* Start date: 2026-11-26
----------------------------------------
As of master efcec8993c, the hash(3) code (lib/libc/db/hash) follows the 
offsets stored in a page it reads from a database file without checking them, 
so a damaged page makes it write and read outside the page buffer, or loop 
forever. dsynth hit this on a 6.4 build host: its checksum database, 
ports_crc.db (ndbm), had been damaged by an interrupted run, one page's OFFSET 
field read 0xfcb4, and storing a 16-byte record in that bucket wrote it about 
63 KiB past the 4 KiB page buffer, into dsynth's heap. A later run died with 
SIGBUS.

putpair() (hash_page.c) stores a pair at OFFSET minus its size with no bounds 
check, the lookups and hash_seq() take key and data sizes from neighbouring 
offsets, and __get_page() only checks the size of the pread(). With an entry 
count larger than the page, hash_access() reads the zeroed free area as 
overflow links and loops forever in newbuf(); in a database of the other byte 
order, __get_page() first byte-swaps past the page. FreeBSD's recent hardening 
(7f5f07b139a5) checks the header, not page contents, so it doesn't cover this.

The attached patch checks each page __get_page() reads from disk before it is 
used: the entry table and the FREESPACE and OFFSET fields fit, the data area 
starts after them, key/data pairs lie inside the page in order, and a big 
key/data page's offsets point inside it. The entry count is checked before the 
table is byte-swapped. A bad page fails with EFTYPE, also set in hashp->error 
so that dbm_error() reports it. It also fixes __get_buf(): when __get_page() 
failed, the buffer stayed in the pool with the page's address, still linked 
from the previous overflow page, so the next access used the unread page and 
evicting it read its entries.

TESTING: DragonFly has no ASan, so the attached vfhtest builds libc's hash 
sources with an allocator that puts each block right before 128 KiB of 
PROT_NONE, and any access past a page buffer faults where it happens. On a 
5000-key ndbm database with every bucket page damaged in one of five ways 
(OFFSET, entry count, FREESPACE, a key offset past the page, two key offsets 
swapped), master wrote or read out of bounds or hung on store or walk, and 
likewise on a big-endian database; with the patch every case fails with EFTYPE. 
With the installed libc and no special allocator, the OFFSET case reports 
success after writing into the heap. Random stress runs (big keys and data, 
deletes and re-inserts, page sizes 512, 1024 and 4096, both byte orders) and 
walks of /etc/pwd.db, /etc/login.conf.db and /usr/share/misc/termcap.db give 
identical results with and without the patch, also with the patch built into 
libc.so. hash_page.c and hash_buf.c are the same on DragonFly_RELEASE_6_4, so 
the patch applies there.

To run it: tar xf vfhtest.tar.gz, then sh build.sh /usr/src vfhtest (any source 
tree with lib/libc) and sh run.sh ./vfhtest. The second run of build.sh on a 
patched tree gives the "after" binary.

How dsynth's database got damaged is a separate dsynth issue: libc writes dirty 
pages back as they leave its cache but the header only in dbm_close(), and 
dsynth keeps the database open for the whole run, so a killed run leaves new 
pages under the old header.

---Files--------------------------------
0001-libc-reject-damaged-hash-3-pages-instead-of-followin.patch (5.74 KB)
vfhtest.tar.gz (6.75 KB)


-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://bugs.dragonflybsd.org/my/account

Reply via email to