Skip to contentSkip to conversation

hfs: Validate CNIDs in hfs_read_inode

PATCH Superseded George Anthony Vernon 4 participants 35 messages Oct 3, 2025 – Feb 27

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hfs: Validate CNIDs in hfs_read_inode

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

I-love.SAKURA.ne.jp/T/

---
 fs/hfs/inode.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
fs/hfs/inode.c +34 -0
index 9cd449913dc8..6f893011492a 100644
@@ -321,6 +321,30 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record read from disk
+ */
+static bool is_valid_cnid(unsigned long cnid, s8 type)
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+
+	switch (cnid) {
+	case HFS_POR_CNID:
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+	case HFS_BAD_CNID:
+	case HFS_EXCH_CNID:
+		return type == HFS_CDR_FIL;
+	default:
+		return false;
+	}
+}
+
 /*
  * hfs_read_inode
  */
@@ -359,6 +383,11 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		}
 
 		inode->i_ino = be32_to_cpu(rec->file.FlNum);
+		if (!is_valid_cnid(inode->i_ino, HFS_CDR_FIL)) {
+			pr_warn("rejected cnid %lu\n", inode->i_ino);
+			make_bad_inode(inode);
+			break;
+		}
 		inode->i_mode = S_IRUGO | S_IXUGO;
 		if (!(rec->file.Flags & HFS_FIL_LOCK))
 			inode->i_mode |= S_IWUGO;
@@ -372,6 +401,11 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		break;
 	case HFS_CDR_DIR:
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
+		if (!is_valid_cnid(inode->i_ino, HFS_CDR_DIR)) {
+			pr_warn("rejected cnid %lu\n", inode->i_ino);
+			make_bad_inode(inode);
+			break;
+		}
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;
 		inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);
Signature · 1 line
2.50.1
#1 lore raw
Quoted 14 lines · George Anthony Vernon

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

Quoted 5 lines

I-love.SAKURA.ne.jp/T/

Let's pay respect to previous efforts. I am suggesting to add this line:

Are you OK with it?

Quoted 10 lines
---
 fs/hfs/inode.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
fs/hfs/inode.c +0 -0
index 9cd449913dc8..6f893011492a 100644

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 329

Quoted 9 lines
fs/hfs/inode.c +6 -0
@@ -321,6 +321,30 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record read from disk
+ */
+static bool is_valid_cnid(unsigned long cnid, s8 type)

I think we can declare like this:

static inline
bool is_valid_cnid(unsigned long cnid, s8 type)

Why cnid has unsigned long type? The u32 is pretty enough.

Why type has signed type (s8)? We don't expect negative values here. Let's use u8 type.

Quoted 5 lines
fs/hfs/inode.c +5 -0
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+
+	switch (cnid) {

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	case HFS_POR_CNID:
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+	case HFS_BAD_CNID:
+	case HFS_EXCH_CNID:
+		return type == HFS_CDR_FIL;
+	default:
+		return false;

We can simply have default that is doing nothing:

default:

    /* continue logic */
    break;

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 345

Quoted 1 line
fs/hfs/inode.c +1 -0
+	}

I believe that it will be better to return false by default here (after switch).

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 387

Quoted 11 lines
fs/hfs/inode.c +4 -0
+}
+
 /*
  * hfs_read_inode
  */
@@ -359,6 +383,11 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		}
 
 		inode->i_ino = be32_to_cpu(rec->file.FlNum);
+		if (!is_valid_cnid(inode->i_ino, HFS_CDR_FIL)) {
+			pr_warn("rejected cnid %lu\n", inode->i_ino);

The syzbot reported the issue on specially corrupted volume. So, probably, it will be better to mentioned that "volume is probably corrupted" and to advice to run FSCK tool.

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 388

Quoted 1 line
fs/hfs/inode.c +1 -0
+			make_bad_inode(inode);

We already have make_bad_inode(inode) under default case of switch. Let's jump there without replicating this call multiple times. It makes the code more complicated.

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 405

Quoted 11 lines
fs/hfs/inode.c +4 -0
+			break;
+		}
 		inode->i_mode = S_IRUGO | S_IXUGO;
 		if (!(rec->file.Flags & HFS_FIL_LOCK))
 			inode->i_mode |= S_IWUGO;
@@ -372,6 +401,11 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		break;
 	case HFS_CDR_DIR:
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
+		if (!is_valid_cnid(inode->i_ino, HFS_CDR_DIR)) {
+			pr_warn("rejected cnid %lu\n", inode->i_ino);

Ditto.

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 406

Quoted 1 line
fs/hfs/inode.c +1 -0
+			make_bad_inode(inode);

Ditto.

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 411

Quoted 5 lines
fs/hfs/inode.c +2 -0
+			break;
+		}
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;
 		inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);

We have practically the same check for the case of hfs_write_inode():

int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)

{
	struct inode *main_inode = inode;
	struct hfs_find_data fd;
	hfs_cat_rec rec;
	int res;

	hfs_dbg("ino %lu\n", inode->i_ino);
	res = hfs_ext_write_extent(inode);
	if (res)
		return res;

	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
		switch (inode->i_ino) {
		case HFS_ROOT_CNID:
			break;
		case HFS_EXT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
			return 0;
		case HFS_CAT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
			return 0;
		default:
			BUG();
			return -EIO;

I think we need to select something one here. :) I believe we need to remove BUG() and return -EIO, finally. What do you think?

		}
	}

<skipped>
}

What's about to use your check here too?

Mostly, I like your approach but the patch needs some polishing yet. ;)

Thanks,
Slava.

#2 lore raw
Quoted 5 lines · Viacheslav Dubeyko

Let's pay respect to previous efforts. I am suggesting to add this line:

Are you OK with it?

I agree with paying respect to Tetsuo. The kernel docs indicate that the SoB tag isn't used like that. Would the Suggested-by: tag be more appropriate?

Quoted 6 lines

I think we can declare like this:

static inline
bool is_valid_cnid(unsigned long cnid, s8 type)

Why cnid has unsigned long type? The u32 is pretty enough.

Because struct inode's inode number is an unsigned long.

Quoted 3 lines

Why type has signed type (s8)? We don't expect negative values here. Let's use u8 type.

Because the type field of struct hfs_cat_rec is an s8. Is there anything to gain by casting the s8 to a u8?

Quoted 19 lines
Quoted 6 lines
fs/hfs/inode.c +5 -0
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+
+	switch (cnid) {

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	case HFS_POR_CNID:
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+	case HFS_BAD_CNID:
+	case HFS_EXCH_CNID:
+		return type == HFS_CDR_FIL;
+	default:
+		return false;

We can simply have default that is doing nothing:

Quoted 7 lines

default:

    /* continue logic */
    break;

Review

Quoted 1 line
(quoted diff) +1 -0
+	}

I believe that it will be better to return false by default here (after switch).

We can do that, but why would it be better, is it an optimisation? We don't have any logic to continue.

Quoted 34 lines

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 411

Quoted 5 lines
fs/hfs/inode.c +2 -0
+			break;
+		}
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;
 		inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);

We have practically the same check for the case of hfs_write_inode():

int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)

{
	struct inode *main_inode = inode;
	struct hfs_find_data fd;
	hfs_cat_rec rec;
	int res;

	hfs_dbg("ino %lu\n", inode->i_ino);
	res = hfs_ext_write_extent(inode);
	if (res)
		return res;

	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
		switch (inode->i_ino) {
		case HFS_ROOT_CNID:
			break;
		case HFS_EXT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
			return 0;
		case HFS_CAT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
			return 0;
		default:
			BUG();
			return -EIO;
Quoted 2 lines

I think we need to select something one here. :) I believe we need to remove BUG() and return -EIO, finally. What do you think?

I think that with validation of inodes in hfs_read_inode this code path should no longer be reachable by poking the kernel interface from userspace. If it is ever reached, it means kernel logic is broken, so it should be treated as a bug.

Quoted 8 lines
		}
	}

<skipped>
}

What's about to use your check here too?

Let's do that, I'll include it in V2.

Quoted 5 lines

Mostly, I like your approach but the patch needs some polishing yet. ;)

Thanks,
Slava.

Thank you for taking the time to give detailed feedback, I really appreciate it.

George

#3 lore raw
Quoted 4 lines · George Anthony Vernon
Quoted 3 lines · Viacheslav Dubeyko

Let's pay respect to previous efforts. I am suggesting to add this line:

Quoted 8 lines
Quoted 3 lines

Are you OK with it?

I agree with paying respect to Tetsuo. The kernel docs indicate that the SoB tag
isn't used like that. Would the Suggested-by: tag be more appropriate?

Frankly speaking, I don't see how Suggested-by is applicable here. :) My point was that if you mentioned the previous discussion, then it means that you read it. And it sounds to me that your patch is following to the points are discussed there. So, your code is inevitably based on the code is shared during that discussion. This is why I suggested the Signed-off-by. But if you think that it's not correct logic for you, then I am completely OK. :)

Quoted 7 lines
Quoted 6 lines

I think we can declare like this:

static inline
bool is_valid_cnid(unsigned long cnid, s8 type)

Why cnid has unsigned long type? The u32 is pretty enough.

Because struct inode's inode number is an unsigned long.

The Catalog Node ID (CNID) is identification number of item in Catalog File of HFS/HFS+ file system. And it hasn't direct relation with inode number. The Technical Note TN1150 [1] define it as:

The catalog node ID is defined by the CatalogNodeID data type.

typedef UInt32 HFSCatalogNodeID;

The hfs.h declares CNID as __be32 always. Also, hfsplus_raw.h defines CNID as: typedef __be32 hfsplus_cnid;.

So, it cannot be bigger than 32 bits. But unsigned long could be bigger than unsigned int. Potentially, unsigned long could be 64 bits on some platforms.

Quoted 8 lines
Quoted 4 lines

Why type has signed type (s8)? We don't expect negative values here. Let's use
u8 type.

Because the type field of struct hfs_cat_rec is an s8. Is there anything to gain
by casting the s8 to a u8?

I am not completely sure that s8 was correct declaration type in struct hfs_cat_rec and other ones. But if we will use s8 as input parameter, then we could have soon another syzbot report about crash because this framework has generated negative values as input parameter. And I would like to avoid such situation by using u8 data type. Especially, because, negative values don't make sense for type of object.

Quoted 23 lines
Quoted 23 lines
Quoted 6 lines
fs/hfs/inode.c +5 -0
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+
+	switch (cnid) {

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	case HFS_POR_CNID:
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+	case HFS_BAD_CNID:
+	case HFS_EXCH_CNID:
+		return type == HFS_CDR_FIL;
+	default:
+		return false;

We can simply have default that is doing nothing:

default:
    /* continue logic */
    break;

Quoted 7 lines
Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	}

I believe that it will be better to return false by default here (after switch).

We can do that, but why would it be better, is it an optimisation? We don't have
any logic to continue.

We have this function flow:

bool is_valid_cnid()
{
if (condition)

      return <something>;

switch () {
case 1:

      return something;

}
}

Some compilers can treat this like function should return value but has no return by default. And it could generate warnings. So, this is why I suggested to have return at the end of function by default.

Quoted 44 lines
Quoted 35 lines
Quoted 7 lines
(quoted diff) +2 -0
+			break;
+		}

  inode->i_size = be16_to_cpu(rec->dir.Val) + 2; HFS_I(inode)->fs_blocks = 0; inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb-

Quoted 1 line

s_dir_umask);

We have practically the same check for the case of hfs_write_inode():

int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)

{
	struct inode *main_inode = inode;
	struct hfs_find_data fd;
	hfs_cat_rec rec;
	int res;

	hfs_dbg("ino %lu\n", inode->i_ino);
	res = hfs_ext_write_extent(inode);
	if (res)
		return res;

	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
		switch (inode->i_ino) {
		case HFS_ROOT_CNID:
			break;
		case HFS_EXT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)-
Quoted 1 line

ext_tree);

			return 0;
		case HFS_CAT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)-
Quoted 1 line

cat_tree);

Quoted 8 lines
			return 0;
		default:
			BUG();
			return -EIO;

I think we need to select something one here. :) I believe we need to remove
BUG() and return -EIO, finally. What do you think?

Quoted 7 lines

I think that with validation of inodes in hfs_read_inode this code path should
no longer be reachable by poking the kernel interface from userspace. If it is
ever reached, it means kernel logic is broken, so it should be treated as a bug.

We already have multiple syzbot reports with kernel crashes for likewise BUG() statements in HFS/HFS+ code. From one point of view, it is better to return error instead of crashing kernel. From another point of view, the 'return -EIO' is never called because we have BUG() before. So, these two statements together don't make sense. This is why I am suggesting to rework this code.

Thanks,
Slava.

Quoted 15 lines
Quoted 8 lines
		}
	}

<skipped>
}

What's about to use your check here too?

Let's do that, I'll include it in V2.

Quoted 4 lines

Mostly, I like your approach but the patch needs some polishing yet. ;)

Quoted 7 lines
Quoted 2 lines

Thanks,
Slava.

Thank you for taking the time to give detailed feedback, I really appreciate it.

George

[1]
https://dubeyko.com/development/FileSystems/HFSPLUS/tn1150.html#CatalogFile

#4 lore raw

I found this patch. Please CC: me when posting V2.

Quoted 5 lines · Viacheslav Dubeyko
Quoted 4 lines · George Anthony Vernon
Quoted 3 lines · Viacheslav Dubeyko

Let's pay respect to previous efforts. I am suggesting to add this line:

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Are you OK with it?

I agree with paying respect to Tetsuo. The kernel docs indicate that the SoB tag
isn't used like that. Would the Suggested-by: tag be more appropriate?

I'm not suggesting this change. Therefore, Cc: might match.

But I don't like

line, for syzbot only tested one cnid which was embedded in the reproducer. My modified reproducer which tests all range still hits BUG() when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is HFS_POR_CNID. That is why I push
https://lkml.kernel.org/r/[email protected]
as a fix for this problem (and you can propose this patch as a further sanity check). Unless

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 335

Quoted 7 lines
fs/hfs/inode.c +6 -0
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+
+	switch (cnid) {
+	case HFS_POR_CNID:

we disable HFS_POR_CNID case (which I guess it is wrong to do so), we shall hit BUG() in hfs_write_inode().

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 9 lines
fs/hfs/inode.c +9 -0
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+	case HFS_BAD_CNID:
+	case HFS_EXCH_CNID:
+		return type == HFS_CDR_FIL;
+	default:
+		return false;

int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)

{
	struct inode *main_inode = inode;
	struct hfs_find_data fd;
	hfs_cat_rec rec;
	int res;

	hfs_dbg("ino %lu\n", inode->i_ino);
	res = hfs_ext_write_extent(inode);
	if (res)
		return res;

	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
		switch (inode->i_ino) {
		case HFS_ROOT_CNID:
			break;
		case HFS_EXT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)-
Quoted 1 line

ext_tree);

			return 0;
		case HFS_CAT_CNID:
			hfs_btree_write(HFS_SB(inode->i_sb)-
Quoted 1 line

cat_tree);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
			return 0;
		default:
			BUG();
			return -EIO;

I think we need to select something one here. :) I believe we need to remove
BUG() and return -EIO, finally. What do you think?

I think that removing this BUG() now is wrong.
Without my patch, the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) can be HFS_POR_CNID or greater than HFS_FIRSTUSER_CNID, which I think is a logical error in the filesystem image.

Quoted 7 lines
Quoted 7 lines

I think that with validation of inodes in hfs_read_inode this code path should
no longer be reachable by poking the kernel interface from userspace. If it is
ever reached, it means kernel logic is broken, so it should be treated as a bug.

Your patch is incomplete. Please also apply my patch.

#5 lore raw

Hi Tetsuo,

Quoted 1 line · Tetsuo Handa

I found this patch. Please CC: me when posting V2.

Sorry I forgot to CC you last time :)

Quoted 1 line

I'm not suggesting this change. Therefore, Cc: might match.

Sure, I have added a CC tag for you in V2 which I'm currently testing.

Quoted 24 lines

further sanity check). Unless

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 335

Quoted 7 lines
fs/hfs/inode.c +6 -0
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+
+	switch (cnid) {
+	case HFS_POR_CNID:

we disable HFS_POR_CNID case (which I guess it is wrong to do so), we shall hit BUG() in hfs_write_inode().

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 9 lines
fs/hfs/inode.c +9 -0
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+	case HFS_BAD_CNID:
+	case HFS_EXCH_CNID:
+		return type == HFS_CDR_FIL;
+	default:
+		return false;
Quoted 1 line

I think that removing this BUG() now is wrong.

I think HFS_POR_CNID case should be disallowed. There is no real underlying file with that CNID. If we ever found a record with that CNID it would mean the filesystem image was broken, and if we ever try to write a record with that CNID, it means we screwed up.

Quoted 7 lines

Without my patch, the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) can be HFS_POR_CNID or greater than HFS_FIRSTUSER_CNID, which I think is a logical error in the filesystem image.

Your patch is incomplete. Please also apply my patch.

I agree your check is good to catch root inode's i_ino > 15 (is this reachable?) and I'd like to add it. Would you be happy if I make a 2-part patch series with your patch second, keeping your sign-off on it?

Thanks,

George

#6 lore raw
Quoted 4 lines · George Anthony Vernon

I think HFS_POR_CNID case should be disallowed. There is no real underlying file with that CNID. If we ever found a record with that CNID it would mean the filesystem image was broken, and if we ever try to write a record with that CNID, it means we screwed up.

Hmm, your interpretation does not match what Viacheslav Dubeyko interpreted

hfs_read_inode() can be called for the root directory and parent of the root cases. So, HFS_POR_CNID and HFS_ROOT_CNID are legitimate values.

at https://lkml.kernel.org/r/[email protected] .

But if HFS_POR_CNID is not allowed, you can inline is_valid_cnid() for HFS_CDR_DIR case like https://lkml.kernel.org/r/[email protected] .

Quoted 3 lines

I agree your check is good to catch root inode's i_ino > 15 (is this reachable?) and I'd like to add it. Would you be happy if I make a 2-part patch series with your patch second, keeping your sign-off on it?

OK.

#7 lore raw

v2 cover hfs: Validate CNIDs read from filesystem

Changes from V1->V2:
- is_valid_cnid prototype now takes u32 and u8 types
- Add fsck advice in dmesg
- Replace make_bad_inode calls in hfs_read_inode with gotos
- Reuse same check in hfs_write_inode
- Disallow HFS_POR_CNID, HFS_BAD_CNID, and HFS_EXCH_CNID
- Add Tetsuo's patch to mine and make it a patch series

This patch series contains two patches which work together to prevent bad catalog records from being read. Previously it was possible for a malformed HFS filesystem image to reach a BUG() in writeback. I propose to fix this by verifying that CNIDs are allowed ones when constructing an inode in hfs_read_inode(). Tetsuo Handa's additional check in hfs_fill_super makes sure the root inode's CNID is correct, handling an edge case where the records' directory CNID > 15.

I think we should continue returning BUG() in hfs_write_inode() because it is a filesystem implementation error if we ever allow an inode to be constructed from a bad CNID. Now we properly guard our reads in hfs_read_inode(), records with bad CNIDs should not get so far as to initialise inodes which are queued for writeback.

I'm suggesting to disallow HFS_POR_CNID because there is no 'real' record with that CNID or corresponding file, it doesn't make sense to present an inode for it to the VFS when it exists only as a dummy reference in our internal btree. Similarly I'm suggesting to disallow HFS_BAD_CNID and HFS_EXCH_CNID because not only are they for internal use, but we also do not implement bad blocks or exchange file functionality in the Linux driver.

Thank you to Slava and Tetsuo for the feedback on V1.

George Anthony Vernon (2):
hfs: Validate CNIDs in hfs_read_inode
hfs: Update sanity check of the root record

 fs/hfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++-----------
 fs/hfs/super.c |  2 +-
 2 files changed, 54 insertions(+), 15 deletions(-)
Signature · 1 line
2.50.1
#8 lore raw

PATCH v2 1/2 hfs: Validate CNIDs in hfs_read_inode

In reply to Tetsuo Handa

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

I-love.SAKURA.ne.jp/T/

---
 fs/hfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 14 deletions(-)
fs/hfs/inode.c +53 -14
index 9cd449913dc8..bc346693941d 100644
@@ -321,6 +321,38 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record
+ */
+static inline
+bool is_valid_cnid(u32 cnid, u8 type)
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+
+	switch (cnid) {
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+		return type == HFS_CDR_FIL;
+	case HFS_POR_CNID:
+		/* No valid record with this CNID */
+		break;
+	case HFS_BAD_CNID:
+	case HFS_EXCH_CNID:
+		/* Not implemented */
+		break;
+	default:
+		/* Invalid reserved CNID */
+		break;
+	}
+
+	return false;
+}
+
 /*
  * hfs_read_inode
  */
@@ -350,6 +382,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 	rec = idata->rec;
 	switch (rec->type) {
 	case HFS_CDR_FIL:
+		if (!is_valid_cnid(rec->file.FlNum, HFS_CDR_FIL))
+			goto make_bad_inode;
 		if (!HFS_IS_RSRC(inode)) {
 			hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
 					    rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
@@ -371,6 +405,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_mapping->a_ops = &hfs_aops;
 		break;
 	case HFS_CDR_DIR:
+		if (!is_valid_cnid(rec->dir.DirID, HFS_CDR_DIR))
+			goto make_bad_inode;
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;
@@ -380,8 +416,12 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_op = &hfs_dir_inode_operations;
 		inode->i_fop = &hfs_dir_operations;
 		break;
+	make_bad_inode:
+		pr_warn("rejected cnid %lu. Volume is probably corrupted, try performing fsck.\n", inode->i_ino);
+		fallthrough;
 	default:
 		make_bad_inode(inode);
+		break;
 	}
 	return 0;
 }
@@ -441,20 +481,19 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
 	if (res)
 		return res;
 
-	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
-		switch (inode->i_ino) {
-		case HFS_ROOT_CNID:
-			break;
-		case HFS_EXT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
-			return 0;
-		case HFS_CAT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
-			return 0;
-		default:
-			BUG();
-			return -EIO;
-		}
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))
+		BUG();
+
+	switch (inode->i_ino) {
+	case HFS_EXT_CNID:
+		hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
+		return 0;
+	case HFS_CAT_CNID:
+		hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
+		return 0;
+	default:
+		break;
 	}
 
 	if (HFS_IS_RSRC(inode))
Signature · 1 line
2.50.1
#9 lore raw
Quoted 5 lines · George Anthony Vernon

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

The TN1150 technical note describes HFS+ file system and it needs to take into account the difference between HFS and HFS+. So, it is not completely correct for the case of HFS to follow to the TN1150 technical note as it is.

Quoted 39 lines

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

I-love.SAKURA.ne.jp/T/

---
 fs/hfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 14 deletions(-)
fs/hfs/inode.c +11 -0
index 9cd449913dc8..bc346693941d 100644
@@ -321,6 +321,38 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record
+ */
+static inline
+bool is_valid_cnid(u32 cnid, u8 type)
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	switch (cnid) {
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+		return type == HFS_CDR_FIL;
+	case HFS_POR_CNID:
+		/* No valid record with this CNID */
+		break;
+	case HFS_BAD_CNID:

HFS is ancient file system that was needed to work with floppy disks. And bad sectors management was regular task and responsibility of HFS for the case of floppy disks (HDD was also not very reliable at that times). So, HFS implements the bad block management. It means that, potentially, Linux kernel could need to mount a file system volume that created by ancient Mac OS.

I don't think that it's correct management of HFS_BAD_CNID. We must to expect to have such CNID for the case of HFS.

Quoted 33 lines
fs/hfs/inode.c +15 -0
+	case HFS_EXCH_CNID:
+		/* Not implemented */
+		break;
+	default:
+		/* Invalid reserved CNID */
+		break;
+	}
+
+	return false;
+}
+
 /*
  * hfs_read_inode
  */
@@ -350,6 +382,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 	rec = idata->rec;
 	switch (rec->type) {
 	case HFS_CDR_FIL:
+		if (!is_valid_cnid(rec->file.FlNum, HFS_CDR_FIL))
+			goto make_bad_inode;
 		if (!HFS_IS_RSRC(inode)) {
 			hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
 					    rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
@@ -371,6 +405,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_mapping->a_ops = &hfs_aops;
 		break;
 	case HFS_CDR_DIR:
+		if (!is_valid_cnid(rec->dir.DirID, HFS_CDR_DIR))
+			goto make_bad_inode;
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 420

Quoted 5 lines
fs/hfs/inode.c +2 -0
@@ -380,8 +416,12 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_op = &hfs_dir_inode_operations;
 		inode->i_fop = &hfs_dir_operations;
 		break;
+	make_bad_inode:
+		pr_warn("rejected cnid %lu. Volume is probably corrupted, try performing fsck.\n", inode->i_ino);

The "invalid cnid" could sound more relevant than "rejected cnid" for my taste.

The whole message is too long. What's about to have two messages here?

pr_warn("invalid cnid %lu\n", inode->i_ino); pr_warn("Volume is probably corrupted, try performing fsck.\n");

Quoted 17 lines
fs/hfs/inode.c +2 -6
+		fallthrough;
 	default:
 		make_bad_inode(inode);
+		break;
 	}
 	return 0;
 }
@@ -441,20 +481,19 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
 	if (res)
 		return res;
 
-	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
-		switch (inode->i_ino) {
-		case HFS_ROOT_CNID:
-			break;
-		case HFS_EXT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 485

Quoted 10 lines
fs/hfs/inode.c +2 -8
-			return 0;
-		case HFS_CAT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
-			return 0;
-		default:
-			BUG();
-			return -EIO;
-		}
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))

What's about to introduce static inline function or local variable for S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL? I don't like this two line implementation.

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 1 line
fs/hfs/inode.c +1 -0
+		BUG();

I am completely against of leaving BUG() here. Several fixes of syzbot issues were the exchanging BUG() on returning error code. I don't want to investigate the another syzbot issue that will involve this BUG() here. Let's return error code here.

Usually, it makes sense to have BUG() for debug mode and to return error code for the case of release mode. But we don't have the debug mode for HFS code.

Thanks,
Slava.

Quoted 13 lines
fs/hfs/inode.c +10 -0
+
+	switch (inode->i_ino) {
+	case HFS_EXT_CNID:
+		hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
+		return 0;
+	case HFS_CAT_CNID:
+		hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
+		return 0;
+	default:
+		break;
 	}
 
 	if (HFS_IS_RSRC(inode))
#10 lore raw
Quoted 10 lines · Viacheslav Dubeyko
Quoted 5 lines · George Anthony Vernon

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

The TN1150 technical note describes HFS+ file system and it needs to take into account the difference between HFS and HFS+. So, it is not completely correct for the case of HFS to follow to the TN1150 technical note as it is.

I've checked Inside Macintosh: Files Chapter 2 page 70 to make sure HFS is the same (CNIDs 1 - 5 are assigned, and all of 1-15 are reserved). I will add this to the commit message for V3.

Quoted 56 lines
Quoted 39 lines

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

I-love.SAKURA.ne.jp/T/

---
 fs/hfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 14 deletions(-)
fs/hfs/inode.c +11 -0
index 9cd449913dc8..bc346693941d 100644
@@ -321,6 +321,38 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record
+ */
+static inline
+bool is_valid_cnid(u32 cnid, u8 type)
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	switch (cnid) {
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+		return type == HFS_CDR_FIL;
+	case HFS_POR_CNID:
+		/* No valid record with this CNID */
+		break;
+	case HFS_BAD_CNID:

HFS is ancient file system that was needed to work with floppy disks. And bad sectors management was regular task and responsibility of HFS for the case of floppy disks (HDD was also not very reliable at that times). So, HFS implements the bad block management. It means that, potentially, Linux kernel could need to mount a file system volume that created by ancient Mac OS.

Quoted 3 lines

I don't think that it's correct management of HFS_BAD_CNID. We must to expect to have such CNID for the case of HFS.

HFS_BAD_CNID is reserved for internal use of the filesystem implementation. Since we never intend to use it, there is no correct logical path that we should ever construct an inode with CNID 5. It does not correspond to a record that the user can open, as it is a special CNID used only for extent records used to mark blocks as allocated so they are not used, a behaviour which we do not implement in the Linux HFS or HFS+ drivers. Disallowing this CNID will not prevent correctly formed filesystems from being mounted. I also don't think that presenting an internal record-keeping structure to the VFS would make sense or would be consistent with other filesystems.

Quoted 41 lines
Quoted 33 lines
fs/hfs/inode.c +15 -0
+	case HFS_EXCH_CNID:
+		/* Not implemented */
+		break;
+	default:
+		/* Invalid reserved CNID */
+		break;
+	}
+
+	return false;
+}
+
 /*
  * hfs_read_inode
  */
@@ -350,6 +382,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 	rec = idata->rec;
 	switch (rec->type) {
 	case HFS_CDR_FIL:
+		if (!is_valid_cnid(rec->file.FlNum, HFS_CDR_FIL))
+			goto make_bad_inode;
 		if (!HFS_IS_RSRC(inode)) {
 			hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
 					    rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
@@ -371,6 +405,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_mapping->a_ops = &hfs_aops;
 		break;
 	case HFS_CDR_DIR:
+		if (!is_valid_cnid(rec->dir.DirID, HFS_CDR_DIR))
+			goto make_bad_inode;
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 420

Quoted 5 lines
fs/hfs/inode.c +2 -0
@@ -380,8 +416,12 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_op = &hfs_dir_inode_operations;
 		inode->i_fop = &hfs_dir_operations;
 		break;
+	make_bad_inode:
+		pr_warn("rejected cnid %lu. Volume is probably corrupted, try performing fsck.\n", inode->i_ino);

The "invalid cnid" could sound more relevant than "rejected cnid" for my taste.

Quoted 5 lines

The whole message is too long. What's about to have two messages here?

pr_warn("invalid cnid %lu\n", inode->i_ino); pr_warn("Volume is probably corrupted, try performing fsck.\n");

Good improvement!

Quoted 24 lines
Quoted 24 lines
fs/hfs/inode.c +2 -12
+		fallthrough;
 	default:
 		make_bad_inode(inode);
+		break;
 	}
 	return 0;
 }
@@ -441,20 +481,19 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
 	if (res)
 		return res;
 
-	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
-		switch (inode->i_ino) {
-		case HFS_ROOT_CNID:
-			break;
-		case HFS_EXT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
-			return 0;
-		case HFS_CAT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
-			return 0;
-		default:
-			BUG();
Quoted 8 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 485

Quoted 4 lines
fs/hfs/inode.c +2 -2
-			return -EIO;
-		}
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))

What's about to introduce static inline function or local variable for S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL? I don't like this two line implementation.

Okay, I will rewrite this.

Quoted 10 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 2 lines
fs/hfs/inode.c +1 -0
+		BUG();

I am completely against of leaving BUG() here. Several fixes of syzbot issues were the exchanging BUG() on returning error code. I don't want to investigate the another syzbot issue that will involve this BUG() here. Let's return error code here.

Usually, it makes sense to have BUG() for debug mode and to return error code for the case of release mode. But we don't have the debug mode for HFS code.

I prefer BUG() because I think it is a serious bug that we should not allow for a bad inode to be written. I am willing to take responsibility for investigating further issues if they appear as a result of this. Of course, the final say on BUG() or -EIO is yours as the maintainer.

Quoted 4 lines

Thanks,
Slava.

Many thanks for your time and efforts,

George

#11 lore raw
Quoted 76 lines · George Anthony Vernon
Quoted 10 lines · Viacheslav Dubeyko
Quoted 5 lines · George Anthony Vernon

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

The TN1150 technical note describes HFS+ file system and it needs to take into account the difference between HFS and HFS+. So, it is not completely correct for the case of HFS to follow to the TN1150 technical note as it is.

I've checked Inside Macintosh: Files Chapter 2 page 70 to make sure HFS is the same (CNIDs 1 - 5 are assigned, and all of 1-15 are reserved). I will add this to the commit message for V3.

Quoted 56 lines
Quoted 39 lines

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

I-love.SAKURA.ne.jp/T/

---
 fs/hfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 14 deletions(-)
fs/hfs/inode.c +11 -0
index 9cd449913dc8..bc346693941d 100644
@@ -321,6 +321,38 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record
+ */
+static inline
+bool is_valid_cnid(u32 cnid, u8 type)
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	switch (cnid) {
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+		return type == HFS_CDR_FIL;
+	case HFS_POR_CNID:
+		/* No valid record with this CNID */
+		break;
+	case HFS_BAD_CNID:

HFS is ancient file system that was needed to work with floppy disks. And bad sectors management was regular task and responsibility of HFS for the case of floppy disks (HDD was also not very reliable at that times). So, HFS implements the bad block management. It means that, potentially, Linux kernel could need to mount a file system volume that created by ancient Mac OS.

Quoted 3 lines

I don't think that it's correct management of HFS_BAD_CNID. We must to expect to have such CNID for the case of HFS.

Quoted 11 lines

HFS_BAD_CNID is reserved for internal use of the filesystem implementation. Since we never intend to use it, there is no correct logical path that we should ever construct an inode with CNID 5. It does not correspond to a record that the user can open, as it is a special CNID used only for extent records used to mark blocks as allocated so they are not used, a behaviour which we do not implement in the Linux HFS or HFS+ drivers. Disallowing this CNID will not prevent correctly formed filesystems from being mounted. I also don't think that presenting an internal record-keeping structure to the VFS would make sense or would be consistent with other filesystems.

Yes, we don't want to use it on Linux kernel side. But, potentially, the file for bad block management could or was been created/used on Mac OS side. And if anyone tries to mount the HFS volume with the bad block file, then we will refuse to mount it on the Linux kernel side. This is my main worry here. But it is not very probable situation, by the way.

Quoted 90 lines
Quoted 41 lines
Quoted 33 lines
fs/hfs/inode.c +15 -0
+	case HFS_EXCH_CNID:
+		/* Not implemented */
+		break;
+	default:
+		/* Invalid reserved CNID */
+		break;
+	}
+
+	return false;
+}
+
 /*
  * hfs_read_inode
  */
@@ -350,6 +382,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 	rec = idata->rec;
 	switch (rec->type) {
 	case HFS_CDR_FIL:
+		if (!is_valid_cnid(rec->file.FlNum, HFS_CDR_FIL))
+			goto make_bad_inode;
 		if (!HFS_IS_RSRC(inode)) {
 			hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
 					    rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
@@ -371,6 +405,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_mapping->a_ops = &hfs_aops;
 		break;
 	case HFS_CDR_DIR:
+		if (!is_valid_cnid(rec->dir.DirID, HFS_CDR_DIR))
+			goto make_bad_inode;
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 420

Quoted 5 lines
fs/hfs/inode.c +2 -0
@@ -380,8 +416,12 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_op = &hfs_dir_inode_operations;
 		inode->i_fop = &hfs_dir_operations;
 		break;
+	make_bad_inode:
+		pr_warn("rejected cnid %lu. Volume is probably corrupted, try performing fsck.\n", inode->i_ino);

The "invalid cnid" could sound more relevant than "rejected cnid" for my taste.

Quoted 5 lines

The whole message is too long. What's about to have two messages here?

pr_warn("invalid cnid %lu\n", inode->i_ino); pr_warn("Volume is probably corrupted, try performing fsck.\n");

Good improvement!

Quoted 24 lines
Quoted 24 lines
fs/hfs/inode.c +2 -12
+		fallthrough;
 	default:
 		make_bad_inode(inode);
+		break;
 	}
 	return 0;
 }
@@ -441,20 +481,19 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
 	if (res)
 		return res;
 
-	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
-		switch (inode->i_ino) {
-		case HFS_ROOT_CNID:
-			break;
-		case HFS_EXT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
-			return 0;
-		case HFS_CAT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
-			return 0;
-		default:
-			BUG();
Quoted 8 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 485

Quoted 4 lines
fs/hfs/inode.c +2 -2
-			return -EIO;
-		}
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))

What's about to introduce static inline function or local variable for S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL? I don't like this two line implementation.

Okay, I will rewrite this.

Quoted 8 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 2 lines
fs/hfs/inode.c +1 -0
+		BUG();

I am completely against of leaving BUG() here. Several fixes of syzbot issues were the exchanging BUG() on returning error code. I don't want to investigate the another syzbot issue that will involve this BUG() here. Let's return error code here.

Quoted 9 lines
Quoted 2 lines

Usually, it makes sense to have BUG() for debug mode and to return error code for the case of release mode. But we don't have the debug mode for HFS code.

I prefer BUG() because I think it is a serious bug that we should not allow for a bad inode to be written. I am willing to take responsibility for investigating further issues if they appear as a result of this. Of course, the final say on BUG() or -EIO is yours as the maintainer.

Quoted 1 line

The hfs_write_inode() will return error code and it is also bug case. But we will not crash the kernel in such case. Why would you still like to crash the kernel? :)

I see your point that we should not be here because we must create the bad inode in hfs_read_inode() for the case of corrupted Catalog File's records. Let me sleep on it. :)

Thanks,
Slava.

#12 lore raw
Quoted 89 lines · Viacheslav Dubeyko
Quoted 76 lines · George Anthony Vernon
Quoted 10 lines · Viacheslav Dubeyko
Quoted 5 lines · George Anthony Vernon

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

The TN1150 technical note describes HFS+ file system and it needs to take into account the difference between HFS and HFS+. So, it is not completely correct for the case of HFS to follow to the TN1150 technical note as it is.

I've checked Inside Macintosh: Files Chapter 2 page 70 to make sure HFS is the same (CNIDs 1 - 5 are assigned, and all of 1-15 are reserved). I will add this to the commit message for V3.

Quoted 56 lines
Quoted 39 lines

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

I-love.SAKURA.ne.jp/T/

---
 fs/hfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 14 deletions(-)
fs/hfs/inode.c +11 -0
index 9cd449913dc8..bc346693941d 100644
@@ -321,6 +321,38 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record
+ */
+static inline
+bool is_valid_cnid(u32 cnid, u8 type)
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	switch (cnid) {
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+		return type == HFS_CDR_FIL;
+	case HFS_POR_CNID:
+		/* No valid record with this CNID */
+		break;
+	case HFS_BAD_CNID:

HFS is ancient file system that was needed to work with floppy disks. And bad sectors management was regular task and responsibility of HFS for the case of floppy disks (HDD was also not very reliable at that times). So, HFS implements the bad block management. It means that, potentially, Linux kernel could need to mount a file system volume that created by ancient Mac OS.

Quoted 3 lines

I don't think that it's correct management of HFS_BAD_CNID. We must to expect to have such CNID for the case of HFS.

Quoted 11 lines

HFS_BAD_CNID is reserved for internal use of the filesystem implementation. Since we never intend to use it, there is no correct logical path that we should ever construct an inode with CNID 5. It does not correspond to a record that the user can open, as it is a special CNID used only for extent records used to mark blocks as allocated so they are not used, a behaviour which we do not implement in the Linux HFS or HFS+ drivers. Disallowing this CNID will not prevent correctly formed filesystems from being mounted. I also don't think that presenting an internal record-keeping structure to the VFS would make sense or would be consistent with other filesystems.

Quoted 6 lines

Yes, we don't want to use it on Linux kernel side. But, potentially, the file for bad block management could or was been created/used on Mac OS side. And if anyone tries to mount the HFS volume with the bad block file, then we will refuse to mount it on the Linux kernel side. This is my main worry here. But it is not very probable situation, by the way.

The mount operation only requires reading the root inode, so the proposed change does not mean we would refuse to mount an HFS volume.

It is okay if HFS_BAD_CNID already exists in the btree, we simply won't ever try to read or write it from disk.

Quoted 100 lines
Quoted 90 lines
Quoted 41 lines
Quoted 33 lines
fs/hfs/inode.c +15 -0
+	case HFS_EXCH_CNID:
+		/* Not implemented */
+		break;
+	default:
+		/* Invalid reserved CNID */
+		break;
+	}
+
+	return false;
+}
+
 /*
  * hfs_read_inode
  */
@@ -350,6 +382,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 	rec = idata->rec;
 	switch (rec->type) {
 	case HFS_CDR_FIL:
+		if (!is_valid_cnid(rec->file.FlNum, HFS_CDR_FIL))
+			goto make_bad_inode;
 		if (!HFS_IS_RSRC(inode)) {
 			hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
 					    rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
@@ -371,6 +405,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_mapping->a_ops = &hfs_aops;
 		break;
 	case HFS_CDR_DIR:
+		if (!is_valid_cnid(rec->dir.DirID, HFS_CDR_DIR))
+			goto make_bad_inode;
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 420

Quoted 5 lines
fs/hfs/inode.c +2 -0
@@ -380,8 +416,12 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_op = &hfs_dir_inode_operations;
 		inode->i_fop = &hfs_dir_operations;
 		break;
+	make_bad_inode:
+		pr_warn("rejected cnid %lu. Volume is probably corrupted, try performing fsck.\n", inode->i_ino);

The "invalid cnid" could sound more relevant than "rejected cnid" for my taste.

Quoted 5 lines

The whole message is too long. What's about to have two messages here?

pr_warn("invalid cnid %lu\n", inode->i_ino); pr_warn("Volume is probably corrupted, try performing fsck.\n");

Good improvement!

Quoted 24 lines
Quoted 24 lines
fs/hfs/inode.c +2 -12
+		fallthrough;
 	default:
 		make_bad_inode(inode);
+		break;
 	}
 	return 0;
 }
@@ -441,20 +481,19 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
 	if (res)
 		return res;
 
-	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
-		switch (inode->i_ino) {
-		case HFS_ROOT_CNID:
-			break;
-		case HFS_EXT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
-			return 0;
-		case HFS_CAT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
-			return 0;
-		default:
-			BUG();
Quoted 8 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 485

Quoted 4 lines
fs/hfs/inode.c +2 -2
-			return -EIO;
-		}
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))

What's about to introduce static inline function or local variable for S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL? I don't like this two line implementation.

Okay, I will rewrite this.

Quoted 8 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 2 lines
fs/hfs/inode.c +1 -0
+		BUG();

I am completely against of leaving BUG() here. Several fixes of syzbot issues were the exchanging BUG() on returning error code. I don't want to investigate the another syzbot issue that will involve this BUG() here. Let's return error code here.

Quoted 9 lines
Quoted 2 lines

Usually, it makes sense to have BUG() for debug mode and to return error code for the case of release mode. But we don't have the debug mode for HFS code.

I prefer BUG() because I think it is a serious bug that we should not allow for a bad inode to be written. I am willing to take responsibility for investigating further issues if they appear as a result of this. Of course, the final say on BUG() or -EIO is yours as the maintainer.

Quoted 1 line
Quoted 3 lines

The hfs_write_inode() will return error code and it is also bug case. But we will not crash the kernel in such case. Why would you still like to crash the kernel? :)

I would like the kernel to crash so that a bug report is made and I can fix it, rather than something go wrong quietly. My logic about that could be wrong and I'm happy to return error code as you prefer.

Quoted 7 lines

I see your point that we should not be here because we must create the bad inode in hfs_read_inode() for the case of corrupted Catalog File's records. Let me sleep on it. :)

Thanks,
Slava.

Thanks,

George

#13 lore raw
Quoted 96 lines · George Anthony Vernon
Quoted 89 lines · Viacheslav Dubeyko
Quoted 76 lines · George Anthony Vernon
Quoted 10 lines · Viacheslav Dubeyko
Quoted 5 lines · George Anthony Vernon

hfs_read_inode previously did not validate CNIDs read from disk, thereby allowing inodes to be constructed with disallowed CNIDs and placed on the dirty list, eventually hitting a bug on writeback.

Validate reserved CNIDs according to Apple technical note TN1150.

The TN1150 technical note describes HFS+ file system and it needs to take into account the difference between HFS and HFS+. So, it is not completely correct for the case of HFS to follow to the TN1150 technical note as it is.

I've checked Inside Macintosh: Files Chapter 2 page 70 to make sure HFS is the same (CNIDs 1 - 5 are assigned, and all of 1-15 are reserved). I will add this to the commit message for V3.

Quoted 56 lines
Quoted 39 lines

This issue was discussed at length on LKML previously, the discussion is linked below.

Syzbot tested this patch on mainline and the bug did not replicate. This patch was regression tested by issuing various system calls on a mounted HFS filesystem and validating that file creation, deletion, reads and writes all work.

I-love.SAKURA.ne.jp/T/

---
 fs/hfs/inode.c | 67 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 53 insertions(+), 14 deletions(-)
fs/hfs/inode.c +11 -0
index 9cd449913dc8..bc346693941d 100644
@@ -321,6 +321,38 @@ static int hfs_test_inode(struct inode *inode, void *data)
 	}
 }
 
+/*
+ * is_valid_cnid
+ *
+ * Validate the CNID of a catalog record
+ */
+static inline
+bool is_valid_cnid(u32 cnid, u8 type)
+{
+	if (likely(cnid >= HFS_FIRSTUSER_CNID))
+		return true;
+

fs/hfs/inode.c static int hfs_test_inode(struct inode *inode, void *data) · line 344

Quoted 10 lines
fs/hfs/inode.c +10 -0
+	switch (cnid) {
+	case HFS_ROOT_CNID:
+		return type == HFS_CDR_DIR;
+	case HFS_EXT_CNID:
+	case HFS_CAT_CNID:
+		return type == HFS_CDR_FIL;
+	case HFS_POR_CNID:
+		/* No valid record with this CNID */
+		break;
+	case HFS_BAD_CNID:

HFS is ancient file system that was needed to work with floppy disks. And bad sectors management was regular task and responsibility of HFS for the case of floppy disks (HDD was also not very reliable at that times). So, HFS implements the bad block management. It means that, potentially, Linux kernel could need to mount a file system volume that created by ancient Mac OS.

Quoted 3 lines

I don't think that it's correct management of HFS_BAD_CNID. We must to expect to have such CNID for the case of HFS.

Quoted 11 lines

HFS_BAD_CNID is reserved for internal use of the filesystem implementation. Since we never intend to use it, there is no correct logical path that we should ever construct an inode with CNID 5. It does not correspond to a record that the user can open, as it is a special CNID used only for extent records used to mark blocks as allocated so they are not used, a behaviour which we do not implement in the Linux HFS or HFS+ drivers. Disallowing this CNID will not prevent correctly formed filesystems from being mounted. I also don't think that presenting an internal record-keeping structure to the VFS would make sense or would be consistent with other filesystems.

Quoted 6 lines

Yes, we don't want to use it on Linux kernel side. But, potentially, the file for bad block management could or was been created/used on Mac OS side. And if anyone tries to mount the HFS volume with the bad block file, then we will refuse to mount it on the Linux kernel side. This is my main worry here. But it is not very probable situation, by the way.

Quoted 5 lines

The mount operation only requires reading the root inode, so the proposed change does not mean we would refuse to mount an HFS volume.

It is okay if HFS_BAD_CNID already exists in the btree, we simply won't ever try to read or write it from disk.

The function is_valid_cnid() doesn't limit the context of it using. Logically, HFS_BAD_CNID is valid CNID because the record with such CNID could present in Catalog File. And if anybody tries to use is_valid_cnid() for checking the Catalog File's record (for example, during traversing the Catalog File's content), then this record will be treated like corrupted with current implementation. However, it will be not correct conclusion. I am trying to have logically correct function here that can be used in any reasonable context.

Quoted 100 lines
Quoted 100 lines
Quoted 99 lines
Quoted 41 lines
Quoted 33 lines
fs/hfs/inode.c +15 -0
+	case HFS_EXCH_CNID:
+		/* Not implemented */
+		break;
+	default:
+		/* Invalid reserved CNID */
+		break;
+	}
+
+	return false;
+}
+
 /*
  * hfs_read_inode
  */
@@ -350,6 +382,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 	rec = idata->rec;
 	switch (rec->type) {
 	case HFS_CDR_FIL:
+		if (!is_valid_cnid(rec->file.FlNum, HFS_CDR_FIL))
+			goto make_bad_inode;
 		if (!HFS_IS_RSRC(inode)) {
 			hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
 					    rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
@@ -371,6 +405,8 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_mapping->a_ops = &hfs_aops;
 		break;
 	case HFS_CDR_DIR:
+		if (!is_valid_cnid(rec->dir.DirID, HFS_CDR_DIR))
+			goto make_bad_inode;
 		inode->i_ino = be32_to_cpu(rec->dir.DirID);
 		inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
 		HFS_I(inode)->fs_blocks = 0;

fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 420

Quoted 5 lines
fs/hfs/inode.c +2 -0
@@ -380,8 +416,12 @@ static int hfs_read_inode(struct inode *inode, void *data)
 		inode->i_op = &hfs_dir_inode_operations;
 		inode->i_fop = &hfs_dir_operations;
 		break;
+	make_bad_inode:
+		pr_warn("rejected cnid %lu. Volume is probably corrupted, try performing fsck.\n", inode->i_ino);

The "invalid cnid" could sound more relevant than "rejected cnid" for my taste.

Quoted 5 lines

The whole message is too long. What's about to have two messages here?

pr_warn("invalid cnid %lu\n", inode->i_ino); pr_warn("Volume is probably corrupted, try performing fsck.\n");

Good improvement!

Quoted 24 lines
Quoted 24 lines
fs/hfs/inode.c +2 -12
+		fallthrough;
 	default:
 		make_bad_inode(inode);
+		break;
 	}
 	return 0;
 }
@@ -441,20 +481,19 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
 	if (res)
 		return res;
 
-	if (inode->i_ino < HFS_FIRSTUSER_CNID) {
-		switch (inode->i_ino) {
-		case HFS_ROOT_CNID:
-			break;
-		case HFS_EXT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
-			return 0;
-		case HFS_CAT_CNID:
-			hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
-			return 0;
-		default:
-			BUG();
Quoted 8 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 485

Quoted 4 lines
fs/hfs/inode.c +2 -2
-			return -EIO;
-		}
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))

What's about to introduce static inline function or local variable for S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL? I don't like this two line implementation.

Okay, I will rewrite this.

Quoted 10 lines

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 2 lines
fs/hfs/inode.c +1 -0
+		BUG();

I am completely against of leaving BUG() here. Several fixes of syzbot issues were the exchanging BUG() on returning error code. I don't want to investigate the another syzbot issue that will involve this BUG() here. Let's return error code here.

Usually, it makes sense to have BUG() for debug mode and to return error code for the case of release mode. But we don't have the debug mode for HFS code.

I prefer BUG() because I think it is a serious bug that we should not allow for a bad inode to be written. I am willing to take responsibility for investigating further issues if they appear as a result of this. Of course, the final say on BUG() or -EIO is yours as the maintainer.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

The hfs_write_inode() will return error code and it is also bug case. But we will not crash the kernel in such case. Why would you still like to crash the kernel? :)

I would like the kernel to crash so that a bug report is made and I can fix it, rather than something go wrong quietly. My logic about that could be wrong and I'm happy to return error code as you prefer.

The hfs_write_inode() will return error code and it means that request fails. Finally, kernel code will complain about it. You can add pr_err()/pr_warn()/pr_c rit() message if you like to be more informative here. If we follow to your approach, then all error codes in hfs_write_inode() needs to be converted into BUG_ON(). But it sounds really weird to me. So, if you have error messages in system log, then you will have the bug report, anyway.

Thanks,
Slava.

Quoted 11 lines
Quoted 7 lines

I see your point that we should not be here because we must create the bad inode in hfs_read_inode() for the case of corrupted Catalog File's records. Let me sleep on it. :)

Thanks,
Slava.

Thanks,

George

#14 lore raw
In reply to George Anthony Vernon

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 3 lines · George Anthony Vernon
fs/hfs/inode.c +3 -0
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))
+		BUG();

Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel? If no, this check is racy because make_bad_inode() makes S_ISDIR(inode->i_mode) == false.

#15 lore raw
Quoted 8 lines · Tetsuo Handa

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 3 lines · George Anthony Vernon
fs/hfs/inode.c +3 -0
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))
+		BUG();

Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel? If no, this check is racy because make_bad_inode() makes S_ISDIR(inode->i_mode) == false.

Any inode should be completely created before any hfs_write_inode() call can happen. So, I don't see how hfs_write_inode() and make_bad_inode() could run in parallel.

But, maybe, I am not completely right that we need to call is_bad_inode() in hfs_write_inode() of checking that it's bad inode. For example, ubifs is doing it in the ubifs_write_inode() [1]. NILFS2 is doing it in nilfs_dirty_inode() [2]. And majority of file systems call is_bad_inode() in evict_inode() methods.

Thanks,
Slava.

[1] https://elixir.bootlin.com/linux/v6.18-rc5/source/fs/ubifs/super.c#L299
[2] https://elixir.bootlin.com/linux/v6.18-rc5/source/fs/nilfs2/inode.c#L1087

#16 lore raw
Quoted 10 lines · Viacheslav Dubeyko
Quoted 8 lines · Tetsuo Handa

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 3 lines · George Anthony Vernon
fs/hfs/inode.c +3 -0
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))
+		BUG();

Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel? If no, this check is racy because make_bad_inode() makes S_ISDIR(inode->i_mode) == false.

Quoted 4 lines

Any inode should be completely created before any hfs_write_inode() call can happen. So, I don't see how hfs_write_inode() and make_bad_inode() could run in parallel.

Could we not read the same inode a second time, during the execution of hfs_write_inode()?

Then I believe we could hit make_bad_inode() in hfs_read_inode() once we had already entered hfs_write_inode(), and so test a cnid against the wrong i_mode.

Thanks,

George

#17 lore raw
Quoted 16 lines · George Anthony Vernon
Quoted 10 lines · Viacheslav Dubeyko
Quoted 8 lines · Tetsuo Handa

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 3 lines · George Anthony Vernon
fs/hfs/inode.c +3 -0
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))
+		BUG();

Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel? If no, this check is racy because make_bad_inode() makes S_ISDIR(inode->i_mode) == false.

Quoted 4 lines

Any inode should be completely created before any hfs_write_inode() call can happen. So, I don't see how hfs_write_inode() and make_bad_inode() could run in parallel.

Quoted 8 lines

Could we not read the same inode a second time, during the execution of hfs_write_inode()?

Then I believe we could hit make_bad_inode() in hfs_read_inode() once we had already entered hfs_write_inode(), and so test a cnid against the wrong i_mode.

Maybe, I am missing something in your point. But your explanation sounds slightly strange to me.

At first, VFS code tries to find inode object in the cache of already created inodes. So, if we created inode, then it should be in inode cache. It means that we don't need to execute any read operations for such inode object. So, we can call hfs_write_inode() only if inode object completely created and it is in inode cache.

Potentially, it is possible to imagine situation that two or more threads try to access the same inode ID in parallel while inode object is not created yet. But, usually, newly created inode is locked until the end of creation operation. So, only one thread will call hfs_read_inode() and others will get inode object from the cache.

So, hfs_write_inode() could happen if we took the inode from inode cache. And this inode could be good or bad object. But I still don't see how hfs_read_inode() and hfs_write_inode() can be called in parallel for the same inode object.

Thanks,
Slava.

#18 lore raw
In reply to George Anthony Vernon
Quoted 16 lines · George Anthony Vernon
Quoted 10 lines · Viacheslav Dubeyko
Quoted 8 lines · Tetsuo Handa

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 3 lines · George Anthony Vernon
fs/hfs/inode.c +3 -0
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))
+		BUG();

Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel? If no, this check is racy because make_bad_inode() makes S_ISDIR(inode->i_mode) == false.

Quoted 4 lines

Any inode should be completely created before any hfs_write_inode() call can happen. So, I don't see how hfs_write_inode() and make_bad_inode() could run in parallel.

Quoted 7 lines

Could we not read the same inode a second time, during the execution of hfs_write_inode()?

Then I believe we could hit make_bad_inode() in hfs_read_inode() once we had already entered hfs_write_inode(), and so test a cnid against the wrong i_mode.

My "Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel?" question does not assume "make_bad_inode() for HFS is called from only hfs_read_inode()".

write_inode() already checks for !is_bad_inode(inode) before calling filesystem's write_inode callback
( https://elixir.bootlin.com/linux/v6.18-rc7/source/fs/fs-writeback.c#L1558 ).

If the reason for "ubifs is doing it in the ubifs_write_inode()" ( https://elixir.bootlin.com/linux/v6.18-rc7/source/fs/ubifs/super.c#L299 ) is that make_bad_inode() could be called at any moment, it is not safe for HFS to depend on "inode->i_mode does not change during hfs_write_inode()".

#19 lore raw
Quoted 20 lines · Tetsuo Handa
Quoted 19 lines · George Anthony Vernon
Quoted 10 lines · Viacheslav Dubeyko
Quoted 8 lines · Tetsuo Handa

fs/hfs/inode.c int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) · line 486

Quoted 3 lines · George Anthony Vernon
fs/hfs/inode.c +3 -0
+	if (!is_valid_cnid(inode->i_ino,
+			   S_ISDIR(inode->i_mode) ? HFS_CDR_DIR : HFS_CDR_FIL))
+		BUG();

Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel? If no, this check is racy because make_bad_inode() makes S_ISDIR(inode->i_mode) == false.

Quoted 4 lines

Any inode should be completely created before any hfs_write_inode() call can happen. So, I don't see how hfs_write_inode() and make_bad_inode() could run in parallel.

Could we not read the same inode a second time, during the execution of hfs_write_inode()?

Quoted 7 lines
Quoted 4 lines

Then I believe we could hit make_bad_inode() in hfs_read_inode() once we had already entered hfs_write_inode(), and so test a cnid against the wrong i_mode.

My "Is it guaranteed that hfs_write_inode() and make_bad_inode() never run in parallel?" question does not assume "make_bad_inode() for HFS is called from only hfs_read_inode()".

In a different thread, it turned out that make_bad_inode() must not be called once the inode is fully visible
( https://lkml.kernel.org/r/wqkxevwtev5p77czk2com5zvbbwcpxxeucrt7zbgjciqxjyivx@c7624klburuh ), requiring "make_bad_inode() for HFS is called from only hfs_read_inode()" assumption being true.

When can we expect next version of this patch?

Quoted 10 lines

write_inode() already checks for !is_bad_inode(inode) before calling filesystem's write_inode callback
( https://elixir.bootlin.com/linux/v6.18-rc7/source/fs/fs-writeback.c#L1558 ).

If the reason for "ubifs is doing it in the ubifs_write_inode()" ( https://elixir.bootlin.com/linux/v6.18-rc7/source/fs/ubifs/super.c#L299 ) is that make_bad_inode() could be called at any moment, it is not safe for HFS to depend on "inode->i_mode does not change during hfs_write_inode()".

#20 lore raw
Quoted 1 line · Tetsuo Handa

When can we expect next version of this patch?

I'm testing https://lkml.kernel.org/r/[email protected]
in linux-next.git tree since next-20260202, and syzbot did not find problems; ready to send to linux.git tree if we need more time before getting next version of your patch.

#21 lore raw

PATCH v6 1/1 hfs: update sanity check of the root record

syzbot is reporting that BUG() in hfs_write_inode() fires when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
Viacheslav Dubeyko and George Anthony Vernon are trying to fix this problem in hfs_read_inode(), but no new patch is proposed for three months ( https://lkml.kernel.org/r/[email protected] ) . This problem is "one of top crashers which is wasting syzbot resources" and "a very low-hanging fruit which can be trivially avoided". I already tested this patch using linux-next tree for two weeks, and syzbot did not find problems. Therefore, while what they would propose might partially overwrap with my proposal, let's make it possible to utilize syzbot resources for finding other bugs.

 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +1 -1
index 97546d6b41f4..c283fc9c5e88 100644
@@ -361,7 +361,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))
 			res = -EIO;
 	}
 	if (res)
Signature · 1 line
2.53.0
#22 lore raw

Re: [PATCH v6] hfs: update sanity check of the root record

Quoted 11 lines · Tetsuo Handa

syzbot is reporting that BUG() in hfs_write_inode() fires when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
Viacheslav Dubeyko and George Anthony Vernon are trying to fix this problem

If you would like to finish George Anthony Vernon's work, then you are welcome. We have multiple issues in HFS/HFS+ code base. And I don't see any priority in this issue. So, I don't see the point to accept not the fix but a workaround. I am not working on this issue right now. My current priority is HFS/HFS+ xfstests/fstests issues.

Thanks,
Slava.

Quoted 25 lines

in hfs_read_inode(), but no new patch is proposed for three months
( https://urldefense.proofpoint.com/v2/url?u=https-3A__lkml.kernel.org_r_20251104014738.131872-2D3-2Dcontact-40gvernon.com&d=DwICaQ&c=BSDicqBQBDjDI9RkVyTcHQ&r=q5bIm4AXMzc8NJu1_RGmnQ2fMWKq4Y4RAkElvUgSs00&m=q42kdxHiu3lKc6X4JayE4Pr7ImiI0vuvKj9hUsjKifFFSIrHatEHr77NAXC7wPkb&s=5i9RKH-nHB2eIXqkCmivOMSGscBJ2MRkOerYl6Bl9zM&e= ) . This problem is "one of top crashers which is wasting syzbot resources" and
"a very low-hanging fruit which can be trivially avoided". I already tested
this patch using linux-next tree for two weeks, and syzbot did not find
problems. Therefore, while what they would propose might partially overwrap
with my proposal, let's make it possible to utilize syzbot resources for
finding other bugs.

 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +1 -1
index 97546d6b41f4..c283fc9c5e88 100644
@@ -361,7 +361,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))
 			res = -EIO;
 	}
 	if (res)
#23 lore raw
In reply to Tetsuo Handa
Quoted 7 lines · Tetsuo Handa
Quoted 1 line · Tetsuo Handa

When can we expect next version of this patch?

I'm testing https://lkml.kernel.org/r/[email protected]
in linux-next.git tree since next-20260202, and syzbot did not find problems; ready to send to linux.git tree if we need more time before getting next version of your patch.

Thanks for your patience Tetsuo, I've been focussed on work for the past few months following a change of employer. I will review the most recent feedback this weekend and respin the patch.

I appreciate your feedback on previous versions of the patch. It will be good to get this bug finally closed.

Many thanks,

George

#24 lore raw

PATCH v2 2/2 hfs: Update sanity check of the root record

In reply to Tetsuo Handa

syzbot is reporting that BUG() in hfs_write_inode() fires upon unmount operation when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +1 -1
index 47f50fa555a4..a7dd20f2d743 100644
@@ -358,7 +358,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))
 			res = -EIO;
 	}
 	if (res)
Signature · 1 line
2.50.1
#25 lore raw

Re: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 19 lines · George Anthony Vernon

syzbot is reporting that BUG() in hfs_write_inode() fires upon unmount operation when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +0 -0
index 47f50fa555a4..a7dd20f2d743 100644

fs/hfs/super.c static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 361

Quoted 5 lines
fs/hfs/super.c +1 -1
@@ -358,7 +358,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))

This check is completely unnecessary. Because, we have hfs_iget() then [1]:

	res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
	if (res)
		goto bail_no_root;
	res = hfs_cat_find_brec(sb, HFS_ROOT_CNID, &fd);
	if (!res) {
		if (fd.entrylength != sizeof(rec.dir)) {
			res =  -EIO;
			goto bail_hfs_find;
		}
		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
		if (rec.type != HFS_CDR_DIR)
			res = -EIO;
	}
	if (res)
		goto bail_hfs_find;
	res = -EINVAL;
	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode)
		goto bail_no_root;

The hfs_iget() calls iget5_locked() [2]:

struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec)

{
	struct hfs_iget_data data = { key, rec };
	struct inode *inode;
	u32 cnid;

	switch (rec->type) {
	case HFS_CDR_DIR:
		cnid = be32_to_cpu(rec->dir.DirID);
		break;
	case HFS_CDR_FIL:
		cnid = be32_to_cpu(rec->file.FlNum);
		break;
	default:
		return NULL;
	}
	inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data);
	if (inode && (inode->i_state & I_NEW))
		unlock_new_inode(inode);
	return inode;
}

And iget5_locked() calls hfs_read_inode(). And hfs_read_inode() will call is_valid_cnid() after applying your patch. So, is_valid_cnid() in hfs_read_inode() can completely manage the issue. This is why we don't need in this modification after your first patch.

But I think we need to check that root_inode is not bad inode afterwards:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode))
		goto bail_no_root;

Thanks,
Slava.

fs/hfs/super.c static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 364

Quoted 3 lines
fs/hfs/super.c +0 -0
 			res = -EIO;
 	}
 	if (res)

[1] https://elixir.bootlin.com/linux/v6.18-rc4/source/fs/hfs/super.c#L367
[2] https://elixir.bootlin.com/linux/v6.18-rc4/source/fs/hfs/inode.c#L414

#26 lore raw

Re: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 28 lines · Viacheslav Dubeyko
Quoted 19 lines · George Anthony Vernon

syzbot is reporting that BUG() in hfs_write_inode() fires upon unmount operation when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +0 -0
index 47f50fa555a4..a7dd20f2d743 100644

fs/hfs/super.c static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 361

Quoted 5 lines
fs/hfs/super.c +1 -1
@@ -358,7 +358,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))

This check is completely unnecessary. Because, we have hfs_iget() then [1]:

Quoted 7 lines

The hfs_iget() calls iget5_locked() [2]:

And iget5_locked() calls hfs_read_inode(). And hfs_read_inode() will call is_valid_cnid() after applying your patch. So, is_valid_cnid() in hfs_read_inode() can completely manage the issue. This is why we don't need in this modification after your first patch.

I think Tetsuo's concern is that a directory catalog record with cnid > 15 might be returned as a result of hfs_bnode_read, which is_valid_cnid() would not protect against. I've satisfied myself that hfs_bnode_read() in hfs_fill_super() will populate hfs_find_data fd correctly and crash out if it failed to find a record with root CNID so this path is unreachable and there is no need for the second patch.

Quoted 6 lines

But I think we need to check that root_inode is not bad inode afterwards:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode))
		goto bail_no_root;

Agreed, I see hfs_read_inode might return a bad inode. Thanks for catching this. I noticed also that it returns an int but the return value holds no meaning; it is always zero.

Quoted 3 lines

Thanks,
Slava.

Many thanks again,

George

#27 lore raw

RE: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 37 lines · George Anthony Vernon
Quoted 28 lines · Viacheslav Dubeyko
Quoted 19 lines · George Anthony Vernon

syzbot is reporting that BUG() in hfs_write_inode() fires upon unmount operation when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +0 -0
index 47f50fa555a4..a7dd20f2d743 100644

fs/hfs/super.c static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 361

Quoted 5 lines
fs/hfs/super.c +1 -1
@@ -358,7 +358,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))

This check is completely unnecessary. Because, we have hfs_iget() then [1]:

Quoted 7 lines

The hfs_iget() calls iget5_locked() [2]:

And iget5_locked() calls hfs_read_inode(). And hfs_read_inode() will call is_valid_cnid() after applying your patch. So, is_valid_cnid() in hfs_read_inode() can completely manage the issue. This is why we don't need in this modification after your first patch.

Quoted 7 lines

I think Tetsuo's concern is that a directory catalog record with cnid > 15 might be returned as a result of hfs_bnode_read, which is_valid_cnid() would not protect against. I've satisfied myself that hfs_bnode_read() in hfs_fill_super() will populate hfs_find_data fd correctly and crash out if it failed to find a record with root CNID so this path is unreachable and there is no need for the second patch.

Technically speaking, we can adopt this check to be completely sure that nothing will be wrong during the mount operation. But I believe that is_valid_cnid() should be good enough to manage this. Potential argument could be that the check of rec.dir.DirID could be faster operation than to call hfs_iget(). But mount is rare and not very fast operation, anyway. And if we fail to mount, then the speed of mount operation is not very important.

Quoted 12 lines
Quoted 6 lines

But I think we need to check that root_inode is not bad inode afterwards:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode))
		goto bail_no_root;

Agreed, I see hfs_read_inode might return a bad inode. Thanks for catching this. I noticed also that it returns an int but the return value holds no meaning; it is always zero.

I've realized that hfs_write_inode() doesn't check that inode is bad like other file systems do. Probably, we need to have this check too.

Thanks,
Slava.

#28 lore raw

Re: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 46 lines · Viacheslav Dubeyko
Quoted 37 lines · George Anthony Vernon
Quoted 28 lines · Viacheslav Dubeyko
Quoted 19 lines · George Anthony Vernon

syzbot is reporting that BUG() in hfs_write_inode() fires upon unmount operation when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +0 -0
index 47f50fa555a4..a7dd20f2d743 100644

fs/hfs/super.c static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 361

Quoted 5 lines
fs/hfs/super.c +1 -1
@@ -358,7 +358,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))

This check is completely unnecessary. Because, we have hfs_iget() then [1]:

Quoted 7 lines

The hfs_iget() calls iget5_locked() [2]:

And iget5_locked() calls hfs_read_inode(). And hfs_read_inode() will call is_valid_cnid() after applying your patch. So, is_valid_cnid() in hfs_read_inode() can completely manage the issue. This is why we don't need in this modification after your first patch.

Quoted 7 lines

I think Tetsuo's concern is that a directory catalog record with cnid > 15 might be returned as a result of hfs_bnode_read, which is_valid_cnid() would not protect against. I've satisfied myself that hfs_bnode_read() in hfs_fill_super() will populate hfs_find_data fd correctly and crash out if it failed to find a record with root CNID so this path is unreachable and there is no need for the second patch.

Quoted 6 lines

Technically speaking, we can adopt this check to be completely sure that nothing will be wrong during the mount operation. But I believe that is_valid_cnid() should be good enough to manage this. Potential argument could be that the check of rec.dir.DirID could be faster operation than to call hfs_iget(). But mount is rare and not very fast operation, anyway. And if we fail to mount, then the speed of mount operation is not very important.

Agreed we're not worried about speed that the mount operation can reach fail case. The check would have value if the bnode populated in hfs_find_data fd by hfs_cat_find_brec() is bad. That would be very defensive, I'm not sure it's necessary.

Maybe is_valid_cnid() should be is_valid_catalog_cnid(), since that is what it is actually testing for at the interface with the VFS. Would you agree?

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

But I think we need to check that root_inode is not bad inode afterwards:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode))
		goto bail_no_root;
Quoted 8 lines
Quoted 5 lines

Agreed, I see hfs_read_inode might return a bad inode. Thanks for catching this. I noticed also that it returns an int but the return value holds no meaning; it is always zero.

I've realized that hfs_write_inode() doesn't check that inode is bad like other file systems do. Probably, we need to have this check too.

Good point, and similarly with HFS+. I'll take a look.

Thanks,

George

#29 lore raw

RE: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 54 lines · George Anthony Vernon
Quoted 46 lines · Viacheslav Dubeyko
Quoted 37 lines · George Anthony Vernon
Quoted 28 lines · Viacheslav Dubeyko
Quoted 19 lines · George Anthony Vernon

syzbot is reporting that BUG() in hfs_write_inode() fires upon unmount operation when the inode number of the record retrieved as a result of hfs_cat_find_brec(HFS_ROOT_CNID) is not HFS_ROOT_CNID, for commit b905bafdea21 ("hfs: Sanity check the root record") checked the record size and the record type but did not check the inode number.

---
 fs/hfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/hfs/super.c +0 -0
index 47f50fa555a4..a7dd20f2d743 100644

fs/hfs/super.c static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 361

Quoted 5 lines
fs/hfs/super.c +1 -1
@@ -358,7 +358,7 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto bail_hfs_find;
 		}
 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
-		if (rec.type != HFS_CDR_DIR)
+		if (rec.type != HFS_CDR_DIR || rec.dir.DirID != cpu_to_be32(HFS_ROOT_CNID))

This check is completely unnecessary. Because, we have hfs_iget() then [1]:

Quoted 7 lines

The hfs_iget() calls iget5_locked() [2]:

And iget5_locked() calls hfs_read_inode(). And hfs_read_inode() will call is_valid_cnid() after applying your patch. So, is_valid_cnid() in hfs_read_inode() can completely manage the issue. This is why we don't need in this modification after your first patch.

Quoted 7 lines

I think Tetsuo's concern is that a directory catalog record with cnid > 15 might be returned as a result of hfs_bnode_read, which is_valid_cnid() would not protect against. I've satisfied myself that hfs_bnode_read() in hfs_fill_super() will populate hfs_find_data fd correctly and crash out if it failed to find a record with root CNID so this path is unreachable and there is no need for the second patch.

Quoted 6 lines

Technically speaking, we can adopt this check to be completely sure that nothing will be wrong during the mount operation. But I believe that is_valid_cnid() should be good enough to manage this. Potential argument could be that the check of rec.dir.DirID could be faster operation than to call hfs_iget(). But mount is rare and not very fast operation, anyway. And if we fail to mount, then the speed of mount operation is not very important.

Quoted 9 lines

Agreed we're not worried about speed that the mount operation can reach fail case. The check would have value if the bnode populated in hfs_find_data fd by hfs_cat_find_brec() is bad. That would be very defensive, I'm not sure it's necessary.

Maybe is_valid_cnid() should be is_valid_catalog_cnid(), since that is what it is actually testing for at the interface with the VFS. Would you agree?

CNID is abbreviation of Catalog Node ID. So, is_valid_catalog_cnid() will sound like Catalog Catalog Node ID. :)

Thanks,
Slava.

Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

But I think we need to check that root_inode is not bad inode afterwards:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode))
		goto bail_no_root;
Quoted 8 lines
Quoted 5 lines

Agreed, I see hfs_read_inode might return a bad inode. Thanks for catching this. I noticed also that it returns an int but the return value holds no meaning; it is always zero.

I've realized that hfs_write_inode() doesn't check that inode is bad like other file systems do. Probably, we need to have this check too.

Good point, and similarly with HFS+. I'll take a look.

Thanks,

George

#30 lore raw

Re: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 6 lines · Viacheslav Dubeyko
Quoted 4 lines · George Anthony Vernon

Maybe is_valid_cnid() should be is_valid_catalog_cnid(), since that is what it is actually testing for at the interface with the VFS. Would you agree?

Quoted 6 lines

CNID is abbreviation of Catalog Node ID. So, is_valid_catalog_cnid() will sound like Catalog Catalog Node ID. :)

Thanks,
Slava.

Indeed, I think my thinking got muddled up late at night :)

Thanks,

George

#31 lore raw

Re: [PATCH v2 2/2] hfs: Update sanity check of the root record

In reply to George Anthony Vernon
Quoted 11 lines · George Anthony Vernon
Quoted 6 lines

Technically speaking, we can adopt this check to be completely sure that nothing will be wrong during the mount operation. But I believe that is_valid_cnid() should be good enough to manage this. Potential argument could be that the check of rec.dir.DirID could be faster operation than to call hfs_iget(). But mount is rare and not very fast operation, anyway. And if we fail to mount, then the speed of mount operation is not very important.

Agreed we're not worried about speed that the mount operation can reach fail case. The check would have value if the bnode populated in hfs_find_data fd by hfs_cat_find_brec() is bad. That would be very defensive, I'm not sure it's necessary.

With my patch, mount() syscall fails with EIO unless rec.dir.DirID == 2. Without my patch, mount() syscall succeeds and EIO is later returned when trying to read the root directory of the mounted filesystem.

This is not a problem of speed. Fuzzing unreadable root directory is useless. There is no point with making mount() syscall succeed.

#32 lore raw

RE: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 8 lines · Tetsuo Handa
Quoted 7 lines · George Anthony Vernon
Quoted 6 lines

Technically speaking, we can adopt this check to be completely sure that nothing will be wrong during the mount operation. But I believe that is_valid_cnid() should be good enough to manage this. Potential argument could be that the check of rec.dir.DirID could be faster operation than to call hfs_iget(). But mount is rare and not very fast operation, anyway. And if we fail to mount, then the speed of mount operation is not very important.

Quoted 9 lines
Quoted 4 lines

Agreed we're not worried about speed that the mount operation can reach fail case. The check would have value if the bnode populated in hfs_find_data fd by hfs_cat_find_brec() is bad. That would be very defensive, I'm not sure it's necessary.

With my patch, mount() syscall fails with EIO unless rec.dir.DirID == 2. Without my patch, mount() syscall succeeds and EIO is later returned when trying to read the root directory of the mounted filesystem.

The file system is mounted only if hfs_fill_super() created root node and return 0 [1]. However, if hfs_iget() return bad inode [2] and we will call is_bad_inode() here [3]:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode)) <-- call will be here
		goto bail_no_root;

then, mount will fail. So, no successful mount will happen because is_valid_cnid() will manage the check in hfs_read_inode().

Thanks,
Slava.

Quoted 2 lines

This is not a problem of speed. Fuzzing unreadable root directory is useless. There is no point with making mount() syscall succeed.

[1] https://elixir.bootlin.com/linux/v6.18-rc5/source/fs/hfs/super.c#L379
[2] https://elixir.bootlin.com/linux/v6.18-rc5/source/fs/hfs/super.c#L367
[3] https://elixir.bootlin.com/linux/v6.18-rc5/source/fs/hfs/super.c#L369

#33 lore raw

Re: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 11 lines · Viacheslav Dubeyko

The file system is mounted only if hfs_fill_super() created root node and return 0 [1]. However, if hfs_iget() return bad inode [2] and we will call is_bad_inode() here [3]:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode)) <-- call will be here
		goto bail_no_root;

then, mount will fail. So, no successful mount will happen because is_valid_cnid() will manage the check in hfs_read_inode().

Do you admit that mounting (and optionally fuzzing on) a bad inode (an inode which was subjected to make_bad_inode()) is useless?

Adding is_bad_inode() check without corresponding iput() in error path causes an inode leak bug. Also, error code will differ (my patch returns -EIO while your approach will return -EINVAL).

Honestly speaking, I don't like use of make_bad_inode(). make_bad_inode() might change file type. Also, I worry that make_bad_inode() causes a subtle race bug like https://syzkaller.appspot.com/bug?extid=b7c3ba8cdc2f6cf83c21 which has not come to a conclusion.

Why can't we remove make_bad_inode() usage from hfs_read_inode() and return non-0 value (so that inode_insert5() will return NULL and iget5_locked() will call destroy_inode() and return NULL) when hfs_read_inode() encountered an invalid entry?

#34 lore raw

RE: [PATCH v2 2/2] hfs: Update sanity check of the root record

Quoted 10 lines · Tetsuo Handa
Quoted 9 lines · Viacheslav Dubeyko

The file system is mounted only if hfs_fill_super() created root node and return 0 [1]. However, if hfs_iget() return bad inode [2] and we will call is_bad_inode() here [3]:

	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
	hfs_find_exit(&fd);
	if (!root_inode || is_bad_inode(root_inode)) <-- call will be here
		goto bail_no_root;
Quoted 6 lines
Quoted 2 lines

then, mount will fail. So, no successful mount will happen because is_valid_cnid() will manage the check in hfs_read_inode().

Do you admit that mounting (and optionally fuzzing on) a bad inode (an inode which was subjected to make_bad_inode()) is useless?

Mount will fail in the case of bad root inode. I don't see any issue here.

Quoted 4 lines

Adding is_bad_inode() check without corresponding iput() in error path causes an inode leak bug. Also, error code will differ (my patch returns -EIO while your approach will return -EINVAL).

I don't see any problem to add iput(root_inode) as part of failure management in hfs_fill_super(). And we can return any proper error code for the case of bad root inode.

Quoted 3 lines

Honestly speaking, I don't like use of make_bad_inode(). make_bad_inode() might change file type.

If Catalog File's entry is corrupted, then we cannot assume which particular file type is correct one. And nobody will operate by bad inode. So, it doesn't matter which type bad inode has.

Quoted 4 lines

Also, I worry that make_bad_inode() causes a subtle race bug
like https://syzkaller.appspot.com/bug?extid=b7c3ba8cdc2f6cf83c21 which has not
come to a conclusion.

Frankly speaking, I don't see how this issue is related to the HFS mount operation.

Quoted 3 lines

Why can't we remove make_bad_inode() usage from hfs_read_inode() and return non-0 value (so that inode_insert5() will return NULL and iget5_locked() will call destroy_inode() and return NULL) when hfs_read_inode() encountered an invalid entry?

Bunch of other file systems use the make_bad_inode() and in fill_super() call too. I don't see what is wrong with calling make_bad_inode().

Thanks,
Slava.