Re: [PATCH v4] hfs: Validate CNIDs in hfs_read_inode

From: Tetsuo Handa

Date: Fri Mar 13 2026 - 07:04:41 EST


On 2026/03/13 8:13, Viacheslav Dubeyko wrote:
>> And even after both changes are applied, my patch still makes sense
>> because mount() operation still succeeds for cnid >= 16. :-)
>
> I don't follow how it could happen. Please, take a look here [1]:

Do you remember that you said

"Why do not localize the all checks in hfs_read_inode()?"

in https://lkml.kernel.org/r/5498a57ea660b5366ef213acd554aba55a5804d1.camel@xxxxxxx ?

We came to a patch that validates and calls make_bad_inode() for only 0 <= cnid <= 15 range.
We have never come to a patch that calls make_bad_inode() for cnid >= 16. That is why my
patch is needed; my patch rejects mount() operation if the inode number of the record
retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID.

Anyway, seeing is believing. Try testing with the attached reproducer. This reproducer
tries to mount a crafted HFS image on /mnt . Prepare /dev/loop0 in unused state, and
then run the below command line. You will see that mount() succeeds for cnid == 2 and
cnid >= 16.

# for cnid in $(seq 0 20); do unshare -m ./repro $cnid; done 2>/dev/null
cnid=0 mount=-1 (22)
cnid=1 mount=-1 (22)
cnid=2 mount=0 (0)
cnid=3 mount=-1 (20)
cnid=4 mount=-1 (20)
cnid=5 mount=-1 (22)
cnid=6 mount=-1 (22)
cnid=7 mount=-1 (22)
cnid=8 mount=-1 (22)
cnid=9 mount=-1 (22)
cnid=10 mount=-1 (22)
cnid=11 mount=-1 (22)
cnid=12 mount=-1 (22)
cnid=13 mount=-1 (22)
cnid=14 mount=-1 (22)
cnid=15 mount=-1 (22)
cnid=16 mount=0 (0)
cnid=17 mount=0 (0)
cnid=18 mount=0 (0)
cnid=19 mount=0 (0)
cnid=20 mount=0 (0)
// https://syzkaller.appspot.com/bug?id=ee595bf9e099fff0610828e37bbbcdb7a2933f58
// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include <linux/loop.h>

#ifndef __NR_memfd_create
#define __NR_memfd_create 319
#endif

static unsigned int cnid;

//% This code is derived from puff.{c,h}, found in the zlib development. The
//% original files come with the following copyright notice:

//% Copyright (C) 2002-2013 Mark Adler, all rights reserved
//% version 2.3, 21 Jan 2013
//% This software is provided 'as-is', without any express or implied
//% warranty. In no event will the author be held liable for any damages
//% arising from the use of this software.
//% Permission is granted to anyone to use this software for any purpose,
//% including commercial applications, and to alter it and redistribute it
//% freely, subject to the following restrictions:
//% 1. The origin of this software must not be misrepresented; you must not
//% claim that you wrote the original software. If you use this software
//% in a product, an acknowledgment in the product documentation would be
//% appreciated but is not required.
//% 2. Altered source versions must be plainly marked as such, and must not be
//% misrepresented as being the original software.
//% 3. This notice may not be removed or altered from any source distribution.
//% Mark Adler madler@xxxxxxxxxxxxxxxxxx

//% BEGIN CODE DERIVED FROM puff.{c,h}

#define MAXBITS 15
#define MAXLCODES 286
#define MAXDCODES 30
#define MAXCODES (MAXLCODES + MAXDCODES)
#define FIXLCODES 288

struct puff_state {
unsigned char* out;
unsigned long outlen;
unsigned long outcnt;
const unsigned char* in;
unsigned long inlen;
unsigned long incnt;
int bitbuf;
int bitcnt;
jmp_buf env;
};
static int puff_bits(struct puff_state* s, int need)
{
long val = s->bitbuf;
while (s->bitcnt < need) {
if (s->incnt == s->inlen)
longjmp(s->env, 1);
val |= (long)(s->in[s->incnt++]) << s->bitcnt;
s->bitcnt += 8;
}
s->bitbuf = (int)(val >> need);
s->bitcnt -= need;
return (int)(val & ((1L << need) - 1));
}
static int puff_stored(struct puff_state* s)
{
s->bitbuf = 0;
s->bitcnt = 0;
if (s->incnt + 4 > s->inlen)
return 2;
unsigned len = s->in[s->incnt++];
len |= s->in[s->incnt++] << 8;
if (s->in[s->incnt++] != (~len & 0xff) ||
s->in[s->incnt++] != ((~len >> 8) & 0xff))
return -2;
if (s->incnt + len > s->inlen)
return 2;
if (s->outcnt + len > s->outlen)
return 1;
for (; len--; s->outcnt++, s->incnt++) {
if (s->in[s->incnt])
s->out[s->outcnt] = s->in[s->incnt];
}
return 0;
}
struct puff_huffman {
short* count;
short* symbol;
};
static int puff_decode(struct puff_state* s, const struct puff_huffman* h)
{
int first = 0;
int index = 0;
int bitbuf = s->bitbuf;
int left = s->bitcnt;
int code = first = index = 0;
int len = 1;
short* next = h->count + 1;
while (1) {
while (left--) {
code |= bitbuf & 1;
bitbuf >>= 1;
int count = *next++;
if (code - count < first) {
s->bitbuf = bitbuf;
s->bitcnt = (s->bitcnt - len) & 7;
return h->symbol[index + (code - first)];
}
index += count;
first += count;
first <<= 1;
code <<= 1;
len++;
}
left = (MAXBITS + 1) - len;
if (left == 0)
break;
if (s->incnt == s->inlen)
longjmp(s->env, 1);
bitbuf = s->in[s->incnt++];
if (left > 8)
left = 8;
}
return -10;
}
static int puff_construct(struct puff_huffman* h, const short* length, int n)
{
int len;
for (len = 0; len <= MAXBITS; len++)
h->count[len] = 0;
int symbol;
for (symbol = 0; symbol < n; symbol++)
(h->count[length[symbol]])++;
if (h->count[0] == n)
return 0;
int left = 1;
for (len = 1; len <= MAXBITS; len++) {
left <<= 1;
left -= h->count[len];
if (left < 0)
return left;
}
short offs[MAXBITS + 1];
offs[1] = 0;
for (len = 1; len < MAXBITS; len++)
offs[len + 1] = offs[len] + h->count[len];
for (symbol = 0; symbol < n; symbol++)
if (length[symbol] != 0)
h->symbol[offs[length[symbol]]++] = symbol;
return left;
}
static int puff_codes(struct puff_state* s, const struct puff_huffman* lencode,
const struct puff_huffman* distcode)
{
static const short lens[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
67, 83, 99, 115, 131, 163, 195, 227, 258};
static const short lext[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
static const short dists[30] = {
1, 2, 3, 4, 5, 7, 9, 13, 17, 25,
33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
static const short dext[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
int symbol;
do {
symbol = puff_decode(s, lencode);
if (symbol < 0)
return symbol;
if (symbol < 256) {
if (s->outcnt == s->outlen)
return 1;
if (symbol)
s->out[s->outcnt] = symbol;
s->outcnt++;
} else if (symbol > 256) {
symbol -= 257;
if (symbol >= 29)
return -10;
int len = lens[symbol] + puff_bits(s, lext[symbol]);
symbol = puff_decode(s, distcode);
if (symbol < 0)
return symbol;
unsigned dist = dists[symbol] + puff_bits(s, dext[symbol]);
if (dist > s->outcnt)
return -11;
if (s->outcnt + len > s->outlen)
return 1;
while (len--) {
if (dist <= s->outcnt && s->out[s->outcnt - dist])
s->out[s->outcnt] = s->out[s->outcnt - dist];
s->outcnt++;
}
}
} while (symbol != 256);
return 0;
}
static int puff_fixed(struct puff_state* s)
{
static int virgin = 1;
static short lencnt[MAXBITS + 1], lensym[FIXLCODES];
static short distcnt[MAXBITS + 1], distsym[MAXDCODES];
static struct puff_huffman lencode, distcode;
if (virgin) {
lencode.count = lencnt;
lencode.symbol = lensym;
distcode.count = distcnt;
distcode.symbol = distsym;
short lengths[FIXLCODES];
int symbol;
for (symbol = 0; symbol < 144; symbol++)
lengths[symbol] = 8;
for (; symbol < 256; symbol++)
lengths[symbol] = 9;
for (; symbol < 280; symbol++)
lengths[symbol] = 7;
for (; symbol < FIXLCODES; symbol++)
lengths[symbol] = 8;
puff_construct(&lencode, lengths, FIXLCODES);
for (symbol = 0; symbol < MAXDCODES; symbol++)
lengths[symbol] = 5;
puff_construct(&distcode, lengths, MAXDCODES);
virgin = 0;
}
return puff_codes(s, &lencode, &distcode);
}
static int puff_dynamic(struct puff_state* s)
{
static const short order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
11, 4, 12, 3, 13, 2, 14, 1, 15};
int nlen = puff_bits(s, 5) + 257;
int ndist = puff_bits(s, 5) + 1;
int ncode = puff_bits(s, 4) + 4;
if (nlen > MAXLCODES || ndist > MAXDCODES)
return -3;
short lengths[MAXCODES];
int index;
for (index = 0; index < ncode; index++)
lengths[order[index]] = puff_bits(s, 3);
for (; index < 19; index++)
lengths[order[index]] = 0;
short lencnt[MAXBITS + 1], lensym[MAXLCODES];
struct puff_huffman lencode = {lencnt, lensym};
int err = puff_construct(&lencode, lengths, 19);
if (err != 0)
return -4;
index = 0;
while (index < nlen + ndist) {
int symbol;
int len;
symbol = puff_decode(s, &lencode);
if (symbol < 0)
return symbol;
if (symbol < 16)
lengths[index++] = symbol;
else {
len = 0;
if (symbol == 16) {
if (index == 0)
return -5;
len = lengths[index - 1];
symbol = 3 + puff_bits(s, 2);
} else if (symbol == 17)
symbol = 3 + puff_bits(s, 3);
else
symbol = 11 + puff_bits(s, 7);
if (index + symbol > nlen + ndist)
return -6;
while (symbol--)
lengths[index++] = len;
}
}
if (lengths[256] == 0)
return -9;
err = puff_construct(&lencode, lengths, nlen);
if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1]))
return -7;
short distcnt[MAXBITS + 1], distsym[MAXDCODES];
struct puff_huffman distcode = {distcnt, distsym};
err = puff_construct(&distcode, lengths + nlen, ndist);
if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1]))
return -8;
return puff_codes(s, &lencode, &distcode);
}
static int puff(unsigned char* dest, unsigned long* destlen,
const unsigned char* source, unsigned long sourcelen)
{
struct puff_state s = {
.out = dest,
.outlen = *destlen,
.outcnt = 0,
.in = source,
.inlen = sourcelen,
.incnt = 0,
.bitbuf = 0,
.bitcnt = 0,
};
int err;
if (setjmp(s.env) != 0)
err = 2;
else {
int last;
do {
last = puff_bits(&s, 1);
int type = puff_bits(&s, 2);
err = type == 0 ? puff_stored(&s)
: (type == 1 ? puff_fixed(&s)
: (type == 2 ? puff_dynamic(&s) : -1));
if (err != 0)
break;
} while (!last);
}
*destlen = s.outcnt;
return err;
}

//% END CODE DERIVED FROM puff.{c,h}

#define ZLIB_HEADER_WIDTH 2

static int puff_zlib_to_file(const unsigned char* source,
unsigned long sourcelen, int dest_fd)
{
if (sourcelen < ZLIB_HEADER_WIDTH)
return 0;
source += ZLIB_HEADER_WIDTH;
sourcelen -= ZLIB_HEADER_WIDTH;
const unsigned long max_destlen = 132 << 20;
void* ret = mmap(0, max_destlen, PROT_WRITE | PROT_READ,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (ret == MAP_FAILED)
return -1;
unsigned char* dest = (unsigned char*)ret;
unsigned long destlen = max_destlen;
int err = puff(dest, &destlen, source, sourcelen);
if (err) {
munmap(dest, max_destlen);
errno = -err;
return -1;
}
if (write(dest_fd, dest, destlen) != (ssize_t)destlen) {
munmap(dest, max_destlen);
return -1;
}
return munmap(dest, max_destlen);
}

static void syz_mount_image(volatile long fsarg, volatile long dir,
volatile long flags, volatile long optsarg,
volatile long change_dir,
volatile unsigned long size, volatile long image)
{
unsigned char* data = (unsigned char*)image;
int err = 0, loopfd = -1;
int memfd = syscall(__NR_memfd_create, "syzkaller", 0);
if (memfd == -1) {
err = errno;
goto error;
}
if (puff_zlib_to_file(data, size, memfd)) {
err = errno;
goto error_close_memfd;
}
loopfd = open("/dev/loop0", O_RDWR);
if (loopfd == -1) {
err = errno;
goto error_close_memfd;
}
if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
if (errno != EBUSY) {
err = errno;
goto error_close_loop;
}
ioctl(loopfd, LOOP_CLR_FD, 0);
usleep(1000);
if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
err = errno;
goto error_close_loop;
}
}
pwrite(memfd, &cnid, sizeof(cnid), 4096 + 548);
close(memfd);
errno = 0;
err = mount("/dev/loop0", "/mnt", "hfs", 0, NULL);
printf("cnid=%d mount=%d (%d)\n", cnid / 0x1000000, err, errno);
fflush(stdout);
exit(0);

error_close_loop:
close(loopfd);
error_close_memfd:
close(memfd);
error:
fprintf(stderr, "error %d. Retrying...\n", err);
}

static void execute_one(void)
{
memcpy((void*)0x200000002c80, "hfs\000", 4);
memcpy((void*)0x2000000003c0, "./file1\000", 8);
*(uint8_t*)0x200000000000 = 0;
sprintf((char*)0x200000000001, "0x%016llx", (long long)-1);
memcpy(
(void*)0x200000000100,
"\x78\x9c\xec\xdd\xbf\x6e\xd3\x5c\x18\xc7\xf1\xdf\x71\xd2\x36\xef\x4b\x55"
"\xdc\x3f\x08\x09\x31\x15\x2a\x31\xa1\xb6\x0c\x20\x96\x4a\xa8\x77\xc0\xc2"
"\x84\x28\x4d\x90\xaa\x5a\x45\x82\x22\xd1\x4e\x81\x19\x71\x01\xec\xdc\x02"
"\x17\xc0\xc8\x54\x31\x23\xb1\x31\x31\x30\x16\x96\xa0\x73\x7c\x42\xec\x34"
"\x8e\x93\x28\xa9\x93\xf4\xfb\x91\x12\x25\xf6\x79\xec\xe7\xd8\xc7\x3d\x7e"
"\x52\xb5\x11\x80\x0b\xeb\xc1\xf6\xf7\x8f\x77\x7e\xd8\x87\x91\x4a\x2a\x49"
"\xba\x2f\x05\x92\x2a\x52\x59\xd2\x15\x5d\xad\xbc\x3a\x38\xdc\x3b\x8c\x6a"
"\xd5\x6e\x1b\x2a\xb9\x08\xfb\x30\xa5\xac\x36\xbb\x07\xb5\x4e\x8b\x6d\x9c"
"\xdb\x97\x17\xda\x77\x65\xcd\x27\x97\x61\x34\x2a\xdf\x54\x2f\x3a\x07\x14"
"\xcf\x5d\xfd\x1d\x04\xd2\x9c\xbf\x3a\xdd\xfa\xca\xb9\x67\x36\x1a\x17\x7d"
"\xd0\x9b\x53\x9d\xea\xb5\x16\x8a\xce\x03\x00\x50\x2c\x3f\xff\x07\x7e\x9e"
"\x9f\x8f\x17\x29\x08\xa4\x35\x3f\xed\xa7\xe7\xff\xc9\x9e\x40\xcd\xaf\x86"
"\x53\x74\x1e\x23\xf3\x39\x67\x7d\x62\xfe\x77\x55\x56\xc3\xd8\xf3\x7b\xd9"
"\xad\x6a\xd5\x7b\x76\x08\xe8\xae\x0f\x71\x55\xa2\xf4\xa7\xef\x83\x36\xab"
"\x78\x64\xa5\x6e\x30\x4d\x5e\x55\xe9\x72\x09\xfe\x7b\xb6\x17\xd5\x6e\xef"
"\x3e\x8f\xaa\x81\xde\x6a\xcb\x9b\x69\x35\x5b\x71\xcf\xd5\x78\xe8\x36\x35"
"\x6b\x5a\xfb\xfa\xcd\xd9\x4d\xaf\xc6\x1d\xf3\x99\xe5\x4a\x6e\xad\x3f\x97"
"\x5c\x1f\x66\x6c\x1f\x36\x83\x93\x56\xfe\x89\x26\xcb\xc3\xdd\x63\x3e\xf3"
"\xc5\x9c\x98\xc7\x26\xd4\x07\x55\xff\xdd\xff\x95\x1b\xc6\x1e\x0c\x77\x3c"
"\xc2\xb6\x33\x15\xe7\xbf\x9e\xbd\x45\xd7\xcb\x59\xb9\x56\xc9\xb3\x94\x68"
"\xb2\xe8\x76\x72\x2d\x7d\xc4\xbb\xf6\xb2\xa4\x8c\x8a\x44\xcd\xf3\xb6\xa8"
"\xf4\x07\x04\x61\x5e\x9e\x2e\x6a\xa9\x2d\x2a\xee\xdd\x46\x4e\xd4\x72\xc7"
"\xa8\xcd\x9c\xa8\x95\xf6\xa8\xd6\x68\xce\x8e\x1c\x35\xf3\xde\x3c\x32\xab"
"\xfa\xa9\x4f\xda\x4e\xdc\xff\x07\xf6\x68\xaf\xa9\x97\x2b\xd3\xb6\x71\x2d"
"\xfd\xc8\xe8\xda\x9f\xb2\x6b\x19\xba\xf9\xc4\x5f\x75\xf5\xeb\x1d\x5b\x06"
"\x83\xf6\x08\x03\x78\xa7\xa7\xba\xa7\x85\x97\x47\xc7\xfb\x3b\x51\x54\x7b"
"\x31\xb5\x2f\xec\x95\x38\x06\x69\x1c\x1d\xef\xff\x6e\x8c\x45\x1a\x3b\x51"
"\xd4\x1c\x04\xe3\x92\xcf\xd4\xbe\xb0\x07\xb9\x90\xbd\x37\xe7\x9d\xc1\xb7"
"\x53\xd8\x4f\x26\x9c\xa3\xd6\x49\xef\x33\x90\xdf\xcd\x4c\x0b\x7b\xdf\x65"
"\xe2\xfa\x2f\x51\xaf\xac\xbb\x9b\x35\xfb\x14\xa6\xef\xd3\xe7\x92\xb1\xb9"
"\xb5\x60\x62\x8b\x1b\x19\xb5\xc1\x92\x7b\xfe\x3f\xbb\x82\x4b\x31\x52\x5d"
"\x8d\xcc\xca\xa0\xad\xe6\xea\xbc\x47\x57\x73\xdd\xb8\x25\xdd\xec\x65\x8f"
"\xb1\xd0\xe7\x39\x7e\xb6\x06\x09\x32\xdb\xfa\xaa\x27\x7c\xfe\x0f\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x30\x69\x86\xf7\x27\x07"
"\x15\x65\xad\x2a\xba\x8f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x4c\xba\x51\x7c\xff\xaf\xff\xef\xf0\xe6\x4c\x9b\x5e"
"\xbe\xff\xf7\xa1\xe2\x77\x65\xcd\x6b\x66\x18\x3d\x04\x90\xe5\x6f\x00\x00"
"\x00\xff\xff\x7b\x54\x80\x16",
673);
syz_mount_image(/*fs=*/0x200000002c80, /*dir=*/0x2000000003c0,
/*flags=MS_REC|MS_NOATIME|MS_DIRSYNC|0x200*/ 0x4680,
/*opts=*/0x200000000000, /*chdir=*/0xfd, /*size=*/0x2a1,
/*img=*/0x200000000100);
}

int main(int argc, char *argv[])
{
if (argc == 2)
cnid = (atoi(argv[1]) & 255) * 0x1000000;
syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
/*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
/*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
/*fd=*/(intptr_t)-1, /*offset=*/0ul);
while (1) {
execute_one();
sleep(1);
}
return 0;
}