Skip to contentSkip to conversation

hfsplus: bound btree keys by the tree's max_key_len in hfs_bnode_read_key()

PATCH Superseded ozgun kultekin 2 participants 12 messages Nov 14 – Nov 19

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 hfsplus: bound btree keys by the tree's max_key_len in hfs_bnode_read_key()

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key writes up to 516 attacker-controlled bytes into kmalloc-32. The error path has the same problem: it clears 518 bytes of the 24-byte buffer.

hfs_brec_insert() reaches this with an unvalidated key: when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node. A read-write mount that extends the catalog (creating the private data directory), or an append to a file whose extents spill into the extents tree, is enough; no other file operation is needed.

The 518-byte bound was added by commit bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key"); it covers the catalog and attributes buffers but not the extents buffer. Bound the key length by the tree's own maximum, which btree.c already validates per tree, and only clear the bytes the buffer actually has.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key")

---

fs/hfsplus/bnode.c +7 -2
index 3a1a1fa..f414020 100644
@@ -84,8 +84,13 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 	else
 		key_len = tree->max_key_len + 2;
 
-	if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfsplus_btree_key));
+	/*
+	 * The key buffers come from hfs_find_init() and hold
+	 * tree->max_key_len + 2 bytes each, which is far less than
+	 * sizeof(hfsplus_btree_key) for the extents tree.
+	 */
+	if (key_len > tree->max_key_len + 2 || key_len < 1) {
+		memset(key, 0, tree->max_key_len + 2);
 		pr_err("hfsplus: Invalid key length: %u\n", key_len);
 		return;
 	}

--
2.47.0

#1 lore raw

PATCH v2 2/2 hfs: bound btree keys by the tree's max_key_len in hfs_bnode_read_key()

The classic HFS variant of the same bug: hfs_bnode_read_key() accepts an on-disk key length up to sizeof(hfs_btree_key), while the key buffers from hfs_find_init() hold tree->max_key_len + 2 bytes (18 bytes in total for the extents tree). Bound the length by the tree's maximum and clear only the buffer's own size on error.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key")

---

fs/hfs/bnode.c +3 -2
index fcb5b9c..f678eed 100644
@@ -83,8 +83,9 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 	else
 		key_len = tree->max_key_len + 1;
 
-	if (key_len > sizeof(hfs_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfs_btree_key));
+	/* hfs_find_init() sizes each key buffer as tree->max_key_len + 2 */
+	if (key_len > tree->max_key_len + 1 || key_len < 1) {
+		memset(key, 0, tree->max_key_len + 2);
 		pr_err("hfs: Invalid key length: %u\n", key_len);
 		return;
 	}

--
2.47.0

#2 lore raw

Re: [PATCH 2/2] hfs: bound btree keys by the tree's max_key_len in hfs_bnode_read_key()

Quoted 33 lines · ozgun kultekin

The classic HFS variant of the same bug: hfs_bnode_read_key() accepts an on-disk key length up to sizeof(hfs_btree_key), while the key buffers
from hfs_find_init() hold tree->max_key_len + 2 bytes (18 bytes in total
for the extents tree). Bound the length by the tree's maximum and clear
only the buffer's own size on error.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in

hfs_bnode_read_key")

Closes:
https://lore.kernel.org/all/[email protected]/

---

fs/hfs/bnode.c +0 -0
index fcb5b9c..f678eed 100644
@@ -83,8 +83,9 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
 void *key, u32 off)

  else
  key_len = tree->max_key_len + 1;

fs/hfs/bnode.c +1 -2
-	if (key_len > sizeof(hfs_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfs_btree_key));
+	/* hfs_find_init() sizes each key buffer as tree-
Quoted 1 line

max_key_len + 2 */

fs/hfs/bnode.c +2 -0
+	if (key_len > tree->max_key_len + 1 || key_len < 1) {
+		memset(key, 0, tree->max_key_len + 2);
Quoted 5 lines

  pr_err("hfs: Invalid key length: %u\n", key_len); return;
  }
--
2.47.0

Please, explain the issue more clearly.

Thanks,
Slava.

#3 lore raw
In reply to ozgun kultekin
Quoted 56 lines · ozgun kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks
it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key
writes up to 516 attacker-controlled bytes into kmalloc-32. The error path has the same problem: it clears 518 bytes of the 24-byte buffer.

hfs_brec_insert() reaches this with an unvalidated key: when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and
after a split it reads the first key of the new node. A read-write mount
that extends the catalog (creating the private data directory), or an append to a file whose extents spill into the extents tree, is enough;
no other file operation is needed.

The 518-byte bound was added by commit bb5e07cb9277 ("hfs/hfsplus:
fix
slab-out-of-bounds in hfs_bnode_read_key"); it covers the catalog and attributes buffers but not the extents buffer. Bound the key length by
the tree's own maximum, which btree.c already validates per tree, and only clear the bytes the buffer actually has.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in

hfs_bnode_read_key")

Closes:
https://lore.kernel.org/all/[email protected]/

---

fs/hfsplus/bnode.c +0 -0
index 3a1a1fa..f414020 100644
@@ -84,8 +84,13 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
 void *key, u32 off)

  else
  key_len = tree->max_key_len + 2;

fs/hfsplus/bnode.c +7 -2
-	if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfsplus_btree_key));
+	/*
+	 * The key buffers come from hfs_find_init() and hold
+	 * tree->max_key_len + 2 bytes each, which is far less than
+	 * sizeof(hfsplus_btree_key) for the extents tree.
+	 */
+	if (key_len > tree->max_key_len + 2 || key_len < 1) {
+		memset(key, 0, tree->max_key_len + 2);
Quoted 6 lines

  pr_err("hfsplus: Invalid key length: %u\n", key_len);
  return;
  }
--
2.47.0

I don't quite follow to the fix. What issue are you trying to fix? Could you please explain it more clearly?

Thanks,
Slava.

#4 lore raw

Hi Slava,

Sorry, the commit message was too dense. Here is the issue step by step.

hfs_find_init() allocates one buffer for the search key and the found key:

ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL); fd->search_key = ptr;
fd->key = ptr + tree->max_key_len + 2;

For the extents tree max_key_len is 10, so the allocation is 24 bytes and each key buffer is 12 bytes.

hfs_bnode_read_key() takes the key length from disk (for leaf records of the extents tree) and only checks it against sizeof(hfsplus_btree_key), which is 518:

if (key_len > sizeof(hfsplus_btree_key) || key_len < 1)

So an extents leaf record with key length 514 passes the check, and hfs_bnode_read() copies 516 bytes into the 24-byte allocation. The check from bb5e07cb9277 protects the catalog tree, where the buffers are big enough, but not the extents tree.

It is reached from hfs_brec_insert(), which calls hfs_bnode_read_key() on a key that was never validated (record 0 of the leaf when the tree has no root yet, or the first key of a new node after a split). Mounting the crafted image read-write is enough: hfsplus_fill_super() creates the private data directory, which extends the catalog file and inserts an extents record.

KASAN on Fedora 7.3.3-300.fc45.x86_64+debug, mount only:

BUG: KASAN: slab-out-of-bounds in hfsplus_bnode_read+0x39/0x360 [hfsplus]
Write of size 516 at addr ff11000003a0e740 by task pool-1/1720
Call Trace:
 __asan_memset+0x27/0x50
 hfsplus_bnode_read+0x39/0x360 [hfsplus]
 hfsplus_bnode_read_key+0x12b/0x230 [hfsplus]
 hfsplus_brec_insert+0x554/0xcc0 [hfsplus]
 __hfsplus_ext_write_extent+0x4fd/0x750 [hfsplus]
 hfsplus_ext_read_extent+0x7a7/0xaa0 [hfsplus]
 hfsplus_file_extend+0x862/0x12a0 [hfsplus]
 hfsplus_bmap_reserve+0x2e1/0x4c0 [hfsplus]
 hfsplus_fill_super+0x135a/0x1910 [hfsplus]
Allocated by task 1720:
 __kmalloc_noprof+0x353/0x9d0
 hfsplus_find_init+0x18c/0x400 [hfsplus]
 hfsplus_fill_super+0x135a/0x1910 [hfsplus]

The buggy address is located 0 bytes inside of allocated 24-byte region [ff11000003a0e740, ff11000003a0e758)

The error path has the same problem: memset(key, 0, sizeof(hfsplus_btree_key)) clears 518 bytes of the same buffer.

The fix checks key_len against tree->max_key_len + 2, the real size of each key buffer, and clears only that much on error. max_key_len is already validated per tree in hfs_btree_open(), so valid keys still pass, including the 518-byte catalog keys. With the patch the image is refused with "hfsplus: Invalid key length: 516" and there is no KASAN report. Normal HFS+ volumes still work (create, append into the extents tree, rename, delete, xattrs) and fsck.hfsplus reports them clean.

Patch 2/2 is the same bug in classic hfs: there the extents key buffers are 18 bytes in total, and the check is against sizeof(hfs_btree_key).

I can send the image generator, or a v2 with this text in the commit message if that helps.

Thanks,
ozgun

Quoted 70 lines · Viacheslav Dubeyko
Quoted 60 lines · ozgun kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks
it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key
writes up to 516 attacker-controlled bytes into kmalloc-32. The error path has the same problem: it clears 518 bytes of the 24-byte buffer.

hfs_brec_insert() reaches this with an unvalidated key: when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and
after a split it reads the first key of the new node. A read-write mount
that extends the catalog (creating the private data directory), or an append to a file whose extents spill into the extents tree, is enough;
no other file operation is needed.

The 518-byte bound was added by commit bb5e07cb9277 ("hfs/hfsplus:
fix
slab-out-of-bounds in hfs_bnode_read_key"); it covers the catalog and attributes buffers but not the extents buffer. Bound the key length by
the tree's own maximum, which btree.c already validates per tree, and only clear the bytes the buffer actually has.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in

hfs_bnode_read_key")

Closes:
https://lore.kernel.org/all/[email protected]/

---

fs/hfsplus/bnode.c +7 -2
index 3a1a1fa..f414020 100644
@@ -84,8 +84,13 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
 void *key, u32 off)
      else
              key_len = tree->max_key_len + 2;
 
-     if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
-             memset(key, 0, sizeof(hfsplus_btree_key));
+     /*
+      * The key buffers come from hfs_find_init() and hold
+      * tree->max_key_len + 2 bytes each, which is far less than
+      * sizeof(hfsplus_btree_key) for the extents tree.
+      */
+     if (key_len > tree->max_key_len + 2 || key_len < 1) {
+             memset(key, 0, tree->max_key_len + 2);
              pr_err("hfsplus: Invalid key length: %u\n",
 key_len);
              return;
      }
Quoted 2 lines

--
2.47.0

I don't quite follow to the fix. What issue are you trying to fix? Could you please explain it more clearly?

Thanks,
Slava.

#5 lore raw
Quoted 71 lines · Özgün Kültekin

Hi Slava,

Sorry, the commit message was too dense. Here is the issue step by step.

hfs_find_init() allocates one buffer for the search key and the found key:

 ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL); fd->search_key = ptr;
 fd->key = ptr + tree->max_key_len + 2;

For the extents tree max_key_len is 10, so the allocation is 24 bytes and each key buffer is 12 bytes.

hfs_bnode_read_key() takes the key length from disk (for leaf records of the extents tree) and only checks it against sizeof(hfsplus_btree_key), which is 518:

 if (key_len > sizeof(hfsplus_btree_key) || key_len < 1)

So an extents leaf record with key length 514 passes the check, and hfs_bnode_read() copies 516 bytes into the 24-byte allocation. The check from bb5e07cb9277 protects the catalog tree, where the buffers are big enough, but not the extents tree.

It is reached from hfs_brec_insert(), which calls hfs_bnode_read_key()
on a key that was never validated (record 0 of the leaf when the tree has no root yet, or the first key of a new node after a split). Mounting the crafted image read-write is enough: hfsplus_fill_super() creates the private data directory, which extends the catalog file and
inserts an extents record.

KASAN on Fedora 7.3.3-300.fc45.x86_64+debug, mount only:

BUG: KASAN: slab-out-of-bounds in hfsplus_bnode_read+0x39/0x360
[hfsplus]
Write of size 516 at addr ff11000003a0e740 by task pool-1/1720
Call Trace:
 __asan_memset+0x27/0x50
 hfsplus_bnode_read+0x39/0x360 [hfsplus]
 hfsplus_bnode_read_key+0x12b/0x230 [hfsplus]
 hfsplus_brec_insert+0x554/0xcc0 [hfsplus]
 __hfsplus_ext_write_extent+0x4fd/0x750 [hfsplus]
 hfsplus_ext_read_extent+0x7a7/0xaa0 [hfsplus]
 hfsplus_file_extend+0x862/0x12a0 [hfsplus]
 hfsplus_bmap_reserve+0x2e1/0x4c0 [hfsplus]
 hfsplus_fill_super+0x135a/0x1910 [hfsplus]
Allocated by task 1720:
 __kmalloc_noprof+0x353/0x9d0
 hfsplus_find_init+0x18c/0x400 [hfsplus]
 hfsplus_fill_super+0x135a/0x1910 [hfsplus]

The buggy address is located 0 bytes inside of allocated 24-byte region [ff11000003a0e740, ff11000003a0e758)

The error path has the same problem: memset(key, 0, sizeof(hfsplus_btree_key)) clears 518 bytes of the same buffer.

The fix checks key_len against tree->max_key_len + 2, the real size of
each key buffer, and clears only that much on error. max_key_len is already validated per tree in hfs_btree_open(), so valid keys still pass, including the 518-byte catalog keys. With the patch the image is
refused with "hfsplus: Invalid key length: 516" and there is no KASAN report. Normal HFS+ volumes still work (create, append into the extents tree, rename, delete, xattrs) and fsck.hfsplus reports them clean.

Quoted 7 lines

Patch 2/2 is the same bug in classic hfs: there the extents key buffers are 18 bytes in total, and the check is against sizeof(hfs_btree_key).

I can send the image generator, or a v2 with this text in the commit message if that helps.

I think my answer for another your patch is applicable here too.

Thanks,
Slava.

#6 lore raw

v2 cover hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key()

In reply to ozgun kultekin

hfs_bnode_read_key() checks the on-disk key length against the size of the key union, but the buffers from hfs_find_init() are only max_key_len + 2 bytes each. A crafted extents leaf key overflows them.

Changes since v1:
- hfs_bnode_read_key() takes the size of the destination buffer instead of assuming it, as Viacheslav suggested.
- hfs_find_init() stores that size in struct hfs_find_data as key_size and allocates the two key buffers from it, replacing "tree->max_key_len * 2 + 4".
- Every caller passes fd->key_size. On a too long key the function clears key_size bytes and logs the error, as before.
- Added Suggested-by: Viacheslav Dubeyko.

Tested on 7.4-rc3 with KASAN in QEMU: the crafted images are refused with "Invalid key length: 516" (the unpatched module corrupts memory); an image made by mkfs.hfsplus survives create, rename, delete, xattrs and appends that spill into the extents tree, and fsck.hfsplus reports it clean. hfs is compile-tested only.

v1: https://lore.kernel.org/all/[email protected]/

ozgun kultekin (2):
hfsplus: pass the key buffer size to hfs_bnode_read_key()
hfs: pass the key buffer size to hfs_bnode_read_key()

 fs/hfs/bfind.c          | 6 ++++--
 fs/hfs/bnode.c          | 7 ++++---
 fs/hfs/brec.c           | 9 +++++----
 fs/hfs/btree.h          | 5 ++++-
 fs/hfsplus/bfind.c      | 6 ++++--
 fs/hfsplus/bnode.c      | 7 ++++---
 fs/hfsplus/brec.c       | 9 +++++----
 fs/hfsplus/hfsplus_fs.h | 5 ++++-
 8 files changed, 34 insertions(+), 20 deletions(-)
Signature · 1 line
2.39.5
#7 lore raw

PATCH v2 1/2 hfsplus: pass the key buffer size to hfs_bnode_read_key()

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer.

hfs_brec_insert() reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node. A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough.

Do not let hfs_bnode_read_key() assume the size of the buffer. Record the size of each key buffer in struct hfs_find_data in hfs_find_init(), allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as before, and the error path clears only the buffer.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key")

---
v2: pass the key buffer size to hfs_bnode_read_key() and keep it in
hfs_find_data, as suggested by Viacheslav
v1: https://lore.kernel.org/all/[email protected]/

 fs/hfsplus/bfind.c      | 6 ++++--
 fs/hfsplus/bnode.c      | 7 ++++---
 fs/hfsplus/brec.c       | 9 +++++----
 fs/hfsplus/hfsplus_fs.h | 5 ++++-
 4 files changed, 17 insertions(+), 10 deletions(-)
fs/hfsplus/bfind.c +4 -2
index ca9813f58..5ec72fc98 100644
@@ -19,11 +19,13 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 	fd->tree = tree;
 	fd->bnode = NULL;
 	hfs_find_result_init(fd);
-	ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
+	/* search_key and key, each holding one key of the largest size */
+	fd->key_size = tree->max_key_len + 2;
+	ptr = kzalloc(2 * fd->key_size, GFP_KERNEL);
 	if (!ptr)
 		return -ENOMEM;
 	fd->search_key = ptr;
-	fd->key = ptr + tree->max_key_len + 2;
+	fd->key = ptr + fd->key_size;
 	hfs_dbg("cnid %d, caller %ps\n",
 		tree->cnid, __builtin_return_address(0));
 	mutex_lock_nested(&tree->tree_lock,
fs/hfsplus/bnode.c +4 -3
index 3a1a1fa6f..e2335c6d7 100644
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)
 	return data;
 }
 
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size)
 {
 	struct hfs_btree *tree;
 	u32 key_len;
@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 	else
 		key_len = tree->max_key_len + 2;
 
-	if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfsplus_btree_key));
+	if (key_len > key_size || key_len < 1) {
+		memset(key, 0, key_size);
 		pr_err("hfsplus: Invalid key length: %u\n", key_len);
 		return;
 	}
fs/hfsplus/brec.c +5 -4
index f416e562d..779d7df0b 100644
@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 	 * at the start of the node and it is not the new node
 	 */
 	if (!rec && new_node != node) {
-		hfs_bnode_read_key(node, fd->search_key, data_off + size);
+		hfs_bnode_read_key(node, fd->search_key, data_off + size,
+				   fd->key_size);
 		hfs_brec_update_parent(fd);
 	}
 
@@ -166,7 +167,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 		entry_len = sizeof(cnid);
 
 		/* get index key */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd->key_size);
 		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
 
 		hfs_bnode_put(new_node);
@@ -464,7 +465,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 		}
 		fd->bnode = hfs_bnode_find(tree, new_node->parent);
 		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd->key_size);
 		cnid = cpu_to_be32(new_node->this);
 
 		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
@@ -476,7 +477,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 			if (new_node == node)
 				goto out;
 			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key, 14);
+			hfs_bnode_read_key(node, fd->search_key, 14, fd->key_size);
 		}
 		new_node = NULL;
 	}
fs/hfsplus/hfsplus_fs.h +4 -1
index 6f240f87f..d0d4fea94 100644
@@ -268,6 +268,8 @@ struct hfs_find_data {
 	int record;
 	int keyoffset, keylength;
 	int entryoffset, entrylength;
+	/* size of each of the search_key and key buffers */
+	u32 key_size;
 };
 
 struct hfsplus_readdir_data {
@@ -372,7 +374,8 @@ void hfs_bmap_free(struct hfs_bnode *node);
 void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off);
 u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size);
 void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
 void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);
Signature · 1 line
2.39.5
#8 lore raw

Re: [PATCH v2 1/2] hfsplus: pass the key buffer size to hfs_bnode_read_key()

Quoted 59 lines · ozgun kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks
it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key
writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer.

hfs_brec_insert() reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node.
A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough.

Do not let hfs_bnode_read_key() assume the size of the buffer. Record the size of each key buffer in struct hfs_find_data in hfs_find_init(),
allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as
before, and the error path clears only the buffer.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in

hfs_bnode_read_key")

Closes:
https://lore.kernel.org/all/[email protected]/

---
v2: pass the key buffer size to hfs_bnode_read_key() and keep it in hfs_find_data, as suggested by Viacheslav
v1:
https://lore.kernel.org/all/[email protected]/

 fs/hfsplus/bfind.c      | 6 ++++--
 fs/hfsplus/bnode.c      | 7 ++++---
 fs/hfsplus/brec.c       | 9 +++++----
 fs/hfsplus/hfsplus_fs.h | 5 ++++-

 4 files changed, 17 insertions(+), 10 deletions(-)
fs/hfsplus/bfind.c +1 -1
index ca9813f58..5ec72fc98 100644
@@ -19,11 +19,13 @@ int hfs_find_init(struct hfs_btree *tree, struct
 hfs_find_data *fd)
  	fd->tree = tree;
  	fd->bnode = NULL;
  	hfs_find_result_init(fd);
-	ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
+	/* search_key and key, each holding one key of the largest

size */

fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) · line 23

Quoted 1 line
fs/hfsplus/bfind.c +1 -0
+	fd->key_size = tree->max_key_len + 2;

Do you mean + 4?

fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) · line 24

Quoted 1 line
fs/hfsplus/bfind.c +1 -0
+	ptr = kzalloc(2 * fd->key_size, GFP_KERNEL);

This is why I would like to exchange this tree->max_key_len * 2 + 4 on the statement that includes named constants instead of hardcoded 2 and
4. Please, rework it.

Quoted 60 lines

  if (!ptr)
  return -ENOMEM;
  fd->search_key = ptr;

fs/hfsplus/bfind.c +1 -1
-	fd->key = ptr + tree->max_key_len + 2;
+	fd->key = ptr + fd->key_size;

  hfs_dbg("cnid %d, caller %ps\n", tree->cnid, __builtin_return_address(0)); mutex_lock_nested(&tree->tree_lock,

fs/hfsplus/bnode.c +0 -0
index 3a1a1fa6f..e2335c6d7 100644
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32
 off)
  	return data;
  }

fs/hfsplus/bnode.c +2 -1
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size)

 {
  struct hfs_btree *tree;
  u32 key_len;

fs/hfsplus/bnode.c +0 -0
@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
 void *key, u32 off)

  else
  key_len = tree->max_key_len + 2;

fs/hfsplus/bnode.c +2 -2
-	if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfsplus_btree_key));
+	if (key_len > key_size || key_len < 1) {
+		memset(key, 0, key_size);

  pr_err("hfsplus: Invalid key length: %u\n", key_len);
  return;
  }

fs/hfsplus/brec.c +2 -1
index f416e562d..779d7df0b 100644
@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd,
 void *entry, u32 entry_len)
  	 * at the start of the node and it is not the new node
  	 */
  	if (!rec && new_node != node) {
-		hfs_bnode_read_key(node, fd->search_key, data_off +
 size);
+		hfs_bnode_read_key(node, fd->search_key, data_off +
 size,
+				   fd->key_size);

  hfs_brec_update_parent(fd);
  }

fs/hfsplus/brec.c +0 -0
@@ -166,7 +167,7 @@ int hfs_brec_insert(struct hfs_find_data *fd,
 void *entry, u32 entry_len)
  		entry_len = sizeof(cnid);


  /* get index key */

fs/hfsplus/brec.c +1 -1
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd-
Quoted 1 line
Quoted 1 line

key_size);

We need finally rework this 14 on named constant.

fs/hfsplus/brec.c static int hfs_brec_update_parent(struct · line 470

Quoted 11 lines

  __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
  hfs_bnode_put(new_node);

fs/hfsplus/brec.c +1 -1
@@ -464,7 +465,7 @@ static int hfs_brec_update_parent(struct
 hfs_find_data *fd)
  		}
  		fd->bnode = hfs_bnode_find(tree, new_node->parent);
  		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd-
 >key_size);

Ditto.

fs/hfsplus/brec.c static int hfs_brec_update_parent(struct · line 483

Quoted 12 lines
fs/hfsplus/brec.c +0 -0
  		cnid = cpu_to_be32(new_node->this);


  __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);

fs/hfsplus/brec.c +1 -1
@@ -476,7 +477,7 @@ static int hfs_brec_update_parent(struct
 hfs_find_data *fd)
  			if (new_node == node)
  				goto out;
  			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key,
 14);
+			hfs_bnode_read_key(node, fd->search_key, 14,
 fd->key_size);

Ditto.

fs/hfsplus/hfsplus_fs.h struct hfs_find_data { · line 271

Quoted 12 lines

  }
  new_node = NULL;
  }

fs/hfsplus/hfsplus_fs.h +1 -0
index 6f240f87f..d0d4fea94 100644
@@ -268,6 +268,8 @@ struct hfs_find_data {
  	int record;
  	int keyoffset, keylength;
  	int entryoffset, entrylength;
+	/* size of each of the search_key and key buffers */

We need to have a comment before structure declaration. Ideally, it will be good to comment every field.

fs/hfsplus/hfsplus_fs.h struct hfs_find_data { · line 272

Quoted 1 line
fs/hfsplus/hfsplus_fs.h +1 -0
+	u32 key_size;

This is not proper place for this declaration.

struct hfs_find_data {
	/* filled by caller */
	hfsplus_btree_key *search_key;
	hfsplus_btree_key *key;
	/* filled by find */
	struct hfs_btree *tree;
	struct hfs_bnode *bnode;
	/* filled by findrec */
	int record;
	int keyoffset, keylength;
	int entryoffset, entrylength;
};

This is the field is prepared by caller. So, it should be group in the first section.

From my point of view, keylength and key_size are not semantically different. Could we suggest some better word here?

Thanks,
Slava.

Quoted 15 lines
fs/hfsplus/hfsplus_fs.h +0 -0
  };


 struct hfsplus_readdir_data {

fs/hfsplus/hfsplus_fs.h +0 -0
@@ -372,7 +374,8 @@ void hfs_bmap_free(struct hfs_bnode *node);

 void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off); u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);

fs/hfsplus/hfsplus_fs.h +2 -1
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size);

 void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data); void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);

#9 lore raw

Re: [PATCH v2 1/2] hfsplus: pass the key buffer size to hfs_bnode_read_key()

Hi Slava,

Yes, 2 * (max_key_len + 2) is the same as max_key_len * 2 + 4, so the allocation size did not change. v3 writes it with named constants and addresses all your other points too, including a separate patch that replaces the 14 with HFS_BNODE_DESC_SIZE.

ozgun

Quoted 221 lines · Viacheslav Dubeyko
Quoted 58 lines · ozgun kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks
it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key
writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer.

hfs_brec_insert() reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node.
A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough.

Do not let hfs_bnode_read_key() assume the size of the buffer. Record the size of each key buffer in struct hfs_find_data in hfs_find_init(),
allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as
before, and the error path clears only the buffer.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in

hfs_bnode_read_key")

Closes:
https://lore.kernel.org/all/[email protected]/

---
v2: pass the key buffer size to hfs_bnode_read_key() and keep it in
hfs_find_data, as suggested by Viacheslav
v1:
https://lore.kernel.org/all/[email protected]/

 fs/hfsplus/bfind.c      | 6 ++++--
 fs/hfsplus/bnode.c      | 7 ++++---
 fs/hfsplus/brec.c       | 9 +++++----
 fs/hfsplus/hfsplus_fs.h | 5 ++++-
 4 files changed, 17 insertions(+), 10 deletions(-)
fs/hfsplus/bfind.c +1 -1
index ca9813f58..5ec72fc98 100644
@@ -19,11 +19,13 @@ int hfs_find_init(struct hfs_btree *tree, struct
 hfs_find_data *fd)
      fd->tree = tree;
      fd->bnode = NULL;
      hfs_find_result_init(fd);
-     ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
+     /* search_key and key, each holding one key of the largest
Quoted 2 lines

size */
+ fd->key_size = tree->max_key_len + 2;

Do you mean + 4?

Quoted 1 line

+ ptr = kzalloc(2 * fd->key_size, GFP_KERNEL);

This is why I would like to exchange this tree->max_key_len * 2 + 4 on the statement that includes named constants instead of hardcoded 2 and
4. Please, rework it.

Quoted 54 lines
      if (!ptr)
              return -ENOMEM;
      fd->search_key = ptr;
fs/hfsplus/bfind.c +1 -1
-     fd->key = ptr + tree->max_key_len + 2;
+     fd->key = ptr + fd->key_size;
      hfs_dbg("cnid %d, caller %ps\n",
              tree->cnid, __builtin_return_address(0));
      mutex_lock_nested(&tree->tree_lock,
fs/hfsplus/bnode.c +2 -1
index 3a1a1fa6f..e2335c6d7 100644
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32
 off)
      return data;
 }
 
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+                     u32 key_size)
 {
      struct hfs_btree *tree;
      u32 key_len;
fs/hfsplus/bnode.c +2 -2
@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
 void *key, u32 off)
      else
              key_len = tree->max_key_len + 2;
 
-     if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
-             memset(key, 0, sizeof(hfsplus_btree_key));
+     if (key_len > key_size || key_len < 1) {
+             memset(key, 0, key_size);
              pr_err("hfsplus: Invalid key length: %u\n",
 key_len);
              return;
      }
fs/hfsplus/brec.c +2 -1
index f416e562d..779d7df0b 100644
@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd,
 void *entry, u32 entry_len)
       * at the start of the node and it is not the new node
       */
      if (!rec && new_node != node) {
-             hfs_bnode_read_key(node, fd->search_key, data_off +
 size);
+             hfs_bnode_read_key(node, fd->search_key, data_off +
 size,
+                                fd->key_size);
              hfs_brec_update_parent(fd);
      }

fs/hfsplus/brec.c int hfs_brec_insert(struct hfs_find_data *fd, · line 172

Quoted 7 lines
fs/hfsplus/brec.c +1 -1
@@ -166,7 +167,7 @@ int hfs_brec_insert(struct hfs_find_data *fd,
 void *entry, u32 entry_len)
              entry_len = sizeof(cnid);
 
              /* get index key */
-             hfs_bnode_read_key(new_node, fd->search_key, 14);
+             hfs_bnode_read_key(new_node, fd->search_key, 14, fd-
 >key_size);

We need finally rework this 14 on named constant.

fs/hfsplus/brec.c static int hfs_brec_update_parent(struct · line 470

Quoted 11 lines
fs/hfsplus/brec.c +0 -0
              __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
              hfs_bnode_put(new_node);
fs/hfsplus/brec.c +1 -1
@@ -464,7 +465,7 @@ static int hfs_brec_update_parent(struct
 hfs_find_data *fd)
              }
              fd->bnode = hfs_bnode_find(tree, new_node->parent);
              /* create index key and entry */
-             hfs_bnode_read_key(new_node, fd->search_key, 14);
+             hfs_bnode_read_key(new_node, fd->search_key, 14, fd-
 >key_size);

Ditto.

fs/hfsplus/brec.c static int hfs_brec_update_parent(struct · line 483

Quoted 12 lines
fs/hfsplus/brec.c +0 -0
              cnid = cpu_to_be32(new_node->this);
              __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
fs/hfsplus/brec.c +1 -1
@@ -476,7 +477,7 @@ static int hfs_brec_update_parent(struct
 hfs_find_data *fd)
                      if (new_node == node)
                              goto out;
                      /* restore search_key */
-                     hfs_bnode_read_key(node, fd->search_key,
 14);
+                     hfs_bnode_read_key(node, fd->search_key, 14,
 fd->key_size);

Ditto.

fs/hfsplus/hfsplus_fs.h struct hfs_find_data { · line 271

Quoted 12 lines
              }
              new_node = NULL;
      }
fs/hfsplus/hfsplus_fs.h +1 -0
index 6f240f87f..d0d4fea94 100644
@@ -268,6 +268,8 @@ struct hfs_find_data {
      int record;
      int keyoffset, keylength;
      int entryoffset, entrylength;
+     /* size of each of the search_key and key buffers */

We need to have a comment before structure declaration. Ideally, it will be good to comment every field.

fs/hfsplus/hfsplus_fs.h struct hfs_find_data { · line 272

Quoted 1 line
fs/hfsplus/hfsplus_fs.h +1 -0
+     u32 key_size;

This is not proper place for this declaration.

struct hfs_find_data {
        /* filled by caller */
        hfsplus_btree_key *search_key;
        hfsplus_btree_key *key;
        /* filled by find */
        struct hfs_btree *tree;
        struct hfs_bnode *bnode;
        /* filled by findrec */
        int record;
        int keyoffset, keylength;
        int entryoffset, entrylength;
};

This is the field is prepared by caller. So, it should be group in the first section.

From my point of view, keylength and key_size are not semantically different. Could we suggest some better word here?

Thanks,
Slava.

Quoted 15 lines
fs/hfsplus/hfsplus_fs.h +2 -1
 };
 
 struct hfsplus_readdir_data {
@@ -372,7 +374,8 @@ void hfs_bmap_free(struct hfs_bnode *node);
 void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32
 len);
 u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off);
 u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+                     u32 key_size);
 void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32
 len);

void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data); void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);

#10 lore raw

PATCH v2 2/2 hfs: pass the key buffer size to hfs_bnode_read_key()

In reply to ozgun kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key trees) from disk and only checks it against sizeof(hfs_btree_key). The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is 7, so the whole allocation is 18 bytes and a crafted leaf key writes up to 256 bytes from the image past it. The error path clears sizeof(hfs_btree_key) bytes of the same buffer.

hfs_brec_insert() and hfs_brec_update_parent() reach this with keys that were never validated, the same way as in hfsplus.

Record the size of each key buffer in struct hfs_find_data in hfs_find_init(), allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as before, and the error path clears only the buffer.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key")

---
v2: pass the key buffer size to hfs_bnode_read_key() and keep it in
hfs_find_data, as suggested by Viacheslav
v1: https://lore.kernel.org/all/[email protected]/

 fs/hfs/bfind.c | 6 ++++--
 fs/hfs/bnode.c | 7 ++++---
 fs/hfs/brec.c  | 9 +++++----
 fs/hfs/btree.h | 5 ++++-
 4 files changed, 17 insertions(+), 10 deletions(-)
fs/hfs/bfind.c +4 -2
index d56e47bdc..15b0763ba 100644
@@ -21,11 +21,13 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 
 	fd->tree = tree;
 	fd->bnode = NULL;
-	ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
+	/* search_key and key, each holding one key of the largest size */
+	fd->key_size = tree->max_key_len + 2;
+	ptr = kzalloc(2 * fd->key_size, GFP_KERNEL);
 	if (!ptr)
 		return -ENOMEM;
 	fd->search_key = ptr;
-	fd->key = ptr + tree->max_key_len + 2;
+	fd->key = ptr + fd->key_size;
 	hfs_dbg("cnid %d, caller %ps\n",
 		tree->cnid, __builtin_return_address(0));
 	switch (tree->cnid) {
fs/hfs/bnode.c +4 -3
index fcb5b9cd1..11d3bfc9d 100644
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)
 	return data;
 }
 
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size)
 {
 	struct hfs_btree *tree;
 	u32 key_len;
@@ -83,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 	else
 		key_len = tree->max_key_len + 1;
 
-	if (key_len > sizeof(hfs_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfs_btree_key));
+	if (key_len > key_size || key_len < 1) {
+		memset(key, 0, key_size);
 		pr_err("hfs: Invalid key length: %u\n", key_len);
 		return;
 	}
fs/hfs/brec.c +5 -4
index 5a2f740dd..d64cc2e53 100644
@@ -138,7 +138,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 	 * at the start of the node and it is not the new node
 	 */
 	if (!rec && new_node != node) {
-		hfs_bnode_read_key(node, fd->search_key, data_off + size);
+		hfs_bnode_read_key(node, fd->search_key, data_off + size,
+				   fd->key_size);
 		hfs_brec_update_parent(fd);
 	}
 
@@ -156,7 +157,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 		entry_len = sizeof(cnid);
 
 		/* get index key */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd->key_size);
 		__hfs_brec_find(fd->bnode, fd);
 
 		hfs_bnode_put(new_node);
@@ -450,7 +451,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 		}
 		fd->bnode = hfs_bnode_find(tree, new_node->parent);
 		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd->key_size);
 		cnid = cpu_to_be32(new_node->this);
 
 		__hfs_brec_find(fd->bnode, fd);
@@ -462,7 +463,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 			if (new_node == node)
 				goto out;
 			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key, 14);
+			hfs_bnode_read_key(node, fd->search_key, 14, fd->key_size);
 		}
 		new_node = NULL;
 	}
fs/hfs/btree.h +4 -1
index 1f703f2c5..a24a3c966 100644
@@ -82,6 +82,8 @@ struct hfs_find_data {
 	int record;
 	int keyoffset, keylength;
 	int entryoffset, entrylength;
+	/* size of each of the search_key and key buffers */
+	u32 key_size;
 };
 
 static inline bool is_hfs_bnode_range_valid(struct hfs_find_data *fd,
@@ -117,7 +119,8 @@ extern void hfs_bmap_free(struct hfs_bnode *node);
 extern void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 extern u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off);
 extern u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);
-extern void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size);
 extern void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 extern void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
 extern void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data);
Signature · 1 line
2.39.5
#11 lore raw

Re: [PATCH v2 2/2] hfs: pass the key buffer size to hfs_bnode_read_key()

Quoted 166 lines · ozgun kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key trees) from disk and only checks it against sizeof(hfs_btree_key). The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is 7, so the whole allocation is
18 bytes and a crafted leaf key writes up to 256 bytes from the image past it. The error path clears sizeof(hfs_btree_key) bytes of the same
buffer.

hfs_brec_insert() and hfs_brec_update_parent() reach this with keys that were never validated, the same way as in hfsplus.

Record the size of each key buffer in struct hfs_find_data in hfs_find_init(), allocate the two buffers from it, and pass it to every
hfs_bnode_read_key() call. Keys longer than the buffer are rejected as
before, and the error path clears only the buffer.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in

hfs_bnode_read_key")

Closes:
https://lore.kernel.org/all/[email protected]/

---
v2: pass the key buffer size to hfs_bnode_read_key() and keep it in hfs_find_data, as suggested by Viacheslav
v1:
https://lore.kernel.org/all/[email protected]/

 fs/hfs/bfind.c | 6 ++++--
 fs/hfs/bnode.c | 7 ++++---
 fs/hfs/brec.c  | 9 +++++----
 fs/hfs/btree.h | 5 ++++-

 4 files changed, 17 insertions(+), 10 deletions(-)
fs/hfs/bfind.c +0 -0
index d56e47bdc..15b0763ba 100644
@@ -21,11 +21,13 @@ int hfs_find_init(struct hfs_btree *tree, struct
 hfs_find_data *fd)


  fd->tree = tree;
  fd->bnode = NULL;

fs/hfs/bfind.c +1 -1
-	ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
+	/* search_key and key, each holding one key of the largest

size */

fs/hfs/bfind.c +2 -0
+	fd->key_size = tree->max_key_len + 2;
+	ptr = kzalloc(2 * fd->key_size, GFP_KERNEL);

  if (!ptr)
  return -ENOMEM;
  fd->search_key = ptr;

fs/hfs/bfind.c +1 -1
-	fd->key = ptr + tree->max_key_len + 2;
+	fd->key = ptr + fd->key_size;

  hfs_dbg("cnid %d, caller %ps\n", tree->cnid, __builtin_return_address(0)); switch (tree->cnid) {

fs/hfs/bnode.c +0 -0
index fcb5b9cd1..11d3bfc9d 100644
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32
 off)
  	return data;
  }

fs/hfs/bnode.c +2 -1
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size)

 {
  struct hfs_btree *tree;
  u32 key_len;

fs/hfs/bnode.c +0 -0
@@ -83,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
 void *key, u32 off)

  else
  key_len = tree->max_key_len + 1;

fs/hfs/bnode.c +2 -2
-	if (key_len > sizeof(hfs_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfs_btree_key));
+	if (key_len > key_size || key_len < 1) {
+		memset(key, 0, key_size);

  pr_err("hfs: Invalid key length: %u\n", key_len); return;
  }

fs/hfs/brec.c +2 -1
index 5a2f740dd..d64cc2e53 100644
@@ -138,7 +138,8 @@ int hfs_brec_insert(struct hfs_find_data *fd,
 void *entry, u32 entry_len)
  	 * at the start of the node and it is not the new node
  	 */
  	if (!rec && new_node != node) {
-		hfs_bnode_read_key(node, fd->search_key, data_off +
 size);
+		hfs_bnode_read_key(node, fd->search_key, data_off +
 size,
+				   fd->key_size);

  hfs_brec_update_parent(fd);
  }

fs/hfs/brec.c +0 -0
@@ -156,7 +157,7 @@ int hfs_brec_insert(struct hfs_find_data *fd,
 void *entry, u32 entry_len)
  		entry_len = sizeof(cnid);


  /* get index key */

fs/hfs/brec.c +1 -1
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd-
Quoted 1 line

key_size);

  __hfs_brec_find(fd->bnode, fd);

  hfs_bnode_put(new_node);

fs/hfs/brec.c +1 -1
@@ -450,7 +451,7 @@ static int hfs_brec_update_parent(struct
 hfs_find_data *fd)
  		}
  		fd->bnode = hfs_bnode_find(tree, new_node->parent);
  		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14, fd-
 >key_size);
  		cnid = cpu_to_be32(new_node->this);


  __hfs_brec_find(fd->bnode, fd);

fs/hfs/brec.c +1 -1
@@ -462,7 +463,7 @@ static int hfs_brec_update_parent(struct
 hfs_find_data *fd)
  			if (new_node == node)
  				goto out;
  			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key,
 14);
+			hfs_bnode_read_key(node, fd->search_key, 14,
 fd->key_size);

  }
  new_node = NULL;
  }

fs/hfs/btree.h +2 -0
index 1f703f2c5..a24a3c966 100644
@@ -82,6 +82,8 @@ struct hfs_find_data {
  	int record;
  	int keyoffset, keylength;
  	int entryoffset, entrylength;
+	/* size of each of the search_key and key buffers */
+	u32 key_size;
  };


 static inline bool is_hfs_bnode_range_valid(struct hfs_find_data *fd,

fs/hfs/btree.h +0 -0
@@ -117,7 +119,8 @@ extern void hfs_bmap_free(struct hfs_bnode
 *node);

 extern void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 extern u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off); extern u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off); -extern void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);

fs/hfs/btree.h +2 -0
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_size);
Quoted 6 lines

 extern void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 extern void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
 extern void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data);

Please, see my remarks for HFS+ patch. I have the same comments here.

Thanks,
Slava.