Skip to content

Commit 9250f92

Browse files
Eric SandeenChristoph Hellwig
authored andcommitted
hfsplus: handle more on-disk corruptions without oopsing
hfs seems prone to bad things when it encounters on disk corruption. Many values are read from disk, and used as lengths to memcpy, as an example. This patch fixes up several of these problematic cases. o sanity check the on-disk maximum key lengths on mount (these are set to a defined value at mkfs time and shouldn't differ) o check on-disk node keylens against the maximum key length for each tree o fix hfs_btree_open so that going out via free_tree: doesn't wind up in hfs_releasepage, which wants to follow the very pointer we were trying to set up: HFS_SB(sb)->cat_tree = hfs_btree_open() . failure gets to hfs_releasepage and tries to follow HFS_SB(sb)->cat_tree Tested with the fsfuzzer; it survives more than it used to. [hch: ported of commit cf05946 from hfs] [hch: added the fixes from 5581d018ed3493d226e7a4d645d9c8a5af6c36b] Signed-off-by: Eric Sandeen <sandeen@redhat.com> Signed-off-by: Christoph Hellwig <hch@tuxera.com>
1 parent b6b4142 commit 9250f92

4 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎fs/hfsplus/bfind.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
5252
rec = (e + b) / 2;
5353
len = hfs_brec_lenoff(bnode, rec, &off);
5454
keylen = hfs_brec_keylen(bnode, rec);
55+
if (keylen == 0) {
56+
res = -EINVAL;
57+
goto fail;
58+
}
5559
hfs_bnode_read(bnode, fd->key, off, keylen);
5660
cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
5761
if (!cmpval) {
@@ -67,6 +71,10 @@ int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
6771
if (rec != e && e >= 0) {
6872
len = hfs_brec_lenoff(bnode, e, &off);
6973
keylen = hfs_brec_keylen(bnode, e);
74+
if (keylen == 0) {
75+
res = -EINVAL;
76+
goto fail;
77+
}
7078
hfs_bnode_read(bnode, fd->key, off, keylen);
7179
}
7280
done:
@@ -75,6 +83,7 @@ int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
7583
fd->keylength = keylen;
7684
fd->entryoffset = off + keylen;
7785
fd->entrylength = len - keylen;
86+
fail:
7887
return res;
7988
}
8089

@@ -198,6 +207,10 @@ int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
198207

199208
len = hfs_brec_lenoff(bnode, fd->record, &off);
200209
keylen = hfs_brec_keylen(bnode, fd->record);
210+
if (keylen == 0) {
211+
res = -EINVAL;
212+
goto out;
213+
}
201214
fd->keyoffset = off;
202215
fd->keylength = keylen;
203216
fd->entryoffset = off + keylen;

‎fs/hfsplus/brec.c‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,21 @@ u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec)
4242
recoff = hfs_bnode_read_u16(node, node->tree->node_size - (rec + 1) * 2);
4343
if (!recoff)
4444
return 0;
45-
if (node->tree->attributes & HFS_TREE_BIGKEYS)
45+
if (node->tree->attributes & HFS_TREE_BIGKEYS) {
4646
retval = hfs_bnode_read_u16(node, recoff) + 2;
47-
else
47+
if (retval > node->tree->max_key_len + 2) {
48+
printk(KERN_ERR "hfs: keylen %d too large\n",
49+
retval);
50+
retval = 0;
51+
}
52+
} else {
4853
retval = (hfs_bnode_read_u8(node, recoff) | 1) + 1;
54+
if (retval > node->tree->max_key_len + 1) {
55+
printk(KERN_ERR "hfs: keylen %d too large\n",
56+
retval);
57+
retval = 0;
58+
}
59+
}
4960
}
5061
return retval;
5162
}

‎fs/hfsplus/btree.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,32 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
6363
tree->max_key_len = be16_to_cpu(head->max_key_len);
6464
tree->depth = be16_to_cpu(head->depth);
6565

66-
/* Set the correct compare function */
67-
if (id == HFSPLUS_EXT_CNID) {
66+
/* Verify the tree and set the correct compare function */
67+
switch (id) {
68+
case HFSPLUS_EXT_CNID:
69+
if (tree->max_key_len != HFSPLUS_EXT_KEYLEN - sizeof(u16)) {
70+
printk(KERN_ERR "hfs: invalid extent max_key_len %d\n",
71+
tree->max_key_len);
72+
goto fail_page;
73+
}
6874
tree->keycmp = hfsplus_ext_cmp_key;
69-
} else if (id == HFSPLUS_CAT_CNID) {
75+
break;
76+
case HFSPLUS_CAT_CNID:
77+
if (tree->max_key_len != HFSPLUS_CAT_KEYLEN - sizeof(u16)) {
78+
printk(KERN_ERR "hfs: invalid catalog max_key_len %d\n",
79+
tree->max_key_len);
80+
goto fail_page;
81+
}
82+
7083
if (test_bit(HFSPLUS_SB_HFSX, &HFSPLUS_SB(sb)->flags) &&
7184
(head->key_type == HFSPLUS_KEY_BINARY))
7285
tree->keycmp = hfsplus_cat_bin_cmp_key;
7386
else {
7487
tree->keycmp = hfsplus_cat_case_cmp_key;
7588
set_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags);
7689
}
77-
} else {
90+
break;
91+
default:
7892
printk(KERN_ERR "hfs: unknown B*Tree requested\n");
7993
goto fail_page;
8094
}
@@ -84,6 +98,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
8498
goto fail_page;
8599
if (!tree->node_count)
86100
goto fail_page;
101+
87102
tree->node_size_shift = ffs(size) - 1;
88103

89104
tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
@@ -93,9 +108,9 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
93108
return tree;
94109

95110
fail_page:
96-
tree->inode->i_mapping->a_ops = &hfsplus_aops;
97111
page_cache_release(page);
98112
free_inode:
113+
tree->inode->i_mapping->a_ops = &hfsplus_aops;
99114
iput(tree->inode);
100115
free_tree:
101116
kfree(tree);

‎fs/hfsplus/hfsplus_raw.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ struct hfsplus_cat_key {
200200
struct hfsplus_unistr name;
201201
} __packed;
202202

203+
#define HFSPLUS_CAT_KEYLEN (sizeof(struct hfsplus_cat_key))
203204

204205
/* Structs from hfs.h */
205206
struct hfsp_point {
@@ -323,7 +324,7 @@ struct hfsplus_ext_key {
323324
__be32 start_block;
324325
} __packed;
325326

326-
#define HFSPLUS_EXT_KEYLEN 12
327+
#define HFSPLUS_EXT_KEYLEN sizeof(struct hfsplus_ext_key)
327328

328329
/* HFS+ generic BTree key */
329330
typedef union {

0 commit comments

Comments
 (0)