Skip to content

Commit 71f8e80

Browse files
author
Kent Overstreet
committed
bcachefs: Stricter checks on "key allowed in this btree"
Syzbot managed to come up with a filesystem where check/repair got rather confused at finding a reflink pointer in the inodes btree. Currently, the "key allowed in this btree" checks only apply at commit time, not read time - for forwards compatibility. It seems this is too loose. Now, strict key type allowed checks apply: - at commit time (no forward compatibility issues) - for btree node pointers - if it's a known btree, known key type, and the key type has the "BKEY_TYPE_strict_btree_checks" flag. This means we still have the option of using generic key types - e.g. KEY_TYPE_error, KEY_TYPE_set - on more existing btrees in the future, while most key types that are intended for only a specific btree get stricter checks. Reported-by: syzbot+baee8591f336cab0958b@syzkaller.appspotmail.com Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 417f01e commit 71f8e80

2 files changed

Lines changed: 62 additions & 42 deletions

File tree

fs/bcachefs/bcachefs_format.h

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ static inline void bkey_init(struct bkey *k)
366366
#define __BKEY_PADDED(key, pad) \
367367
struct bkey_i key; __u64 key ## _pad[pad]
368368

369+
enum bch_bkey_type_flags {
370+
BKEY_TYPE_strict_btree_checks = BIT(0),
371+
};
372+
369373
/*
370374
* - DELETED keys are used internally to mark keys that should be ignored but
371375
* override keys in composition order. Their version number is ignored.
@@ -383,46 +387,46 @@ static inline void bkey_init(struct bkey *k)
383387
*
384388
* - WHITEOUT: for hash table btrees
385389
*/
386-
#define BCH_BKEY_TYPES() \
387-
x(deleted, 0) \
388-
x(whiteout, 1) \
389-
x(error, 2) \
390-
x(cookie, 3) \
391-
x(hash_whiteout, 4) \
392-
x(btree_ptr, 5) \
393-
x(extent, 6) \
394-
x(reservation, 7) \
395-
x(inode, 8) \
396-
x(inode_generation, 9) \
397-
x(dirent, 10) \
398-
x(xattr, 11) \
399-
x(alloc, 12) \
400-
x(quota, 13) \
401-
x(stripe, 14) \
402-
x(reflink_p, 15) \
403-
x(reflink_v, 16) \
404-
x(inline_data, 17) \
405-
x(btree_ptr_v2, 18) \
406-
x(indirect_inline_data, 19) \
407-
x(alloc_v2, 20) \
408-
x(subvolume, 21) \
409-
x(snapshot, 22) \
410-
x(inode_v2, 23) \
411-
x(alloc_v3, 24) \
412-
x(set, 25) \
413-
x(lru, 26) \
414-
x(alloc_v4, 27) \
415-
x(backpointer, 28) \
416-
x(inode_v3, 29) \
417-
x(bucket_gens, 30) \
418-
x(snapshot_tree, 31) \
419-
x(logged_op_truncate, 32) \
420-
x(logged_op_finsert, 33) \
421-
x(accounting, 34) \
422-
x(inode_alloc_cursor, 35)
390+
#define BCH_BKEY_TYPES() \
391+
x(deleted, 0, 0) \
392+
x(whiteout, 1, 0) \
393+
x(error, 2, 0) \
394+
x(cookie, 3, 0) \
395+
x(hash_whiteout, 4, BKEY_TYPE_strict_btree_checks) \
396+
x(btree_ptr, 5, BKEY_TYPE_strict_btree_checks) \
397+
x(extent, 6, BKEY_TYPE_strict_btree_checks) \
398+
x(reservation, 7, BKEY_TYPE_strict_btree_checks) \
399+
x(inode, 8, BKEY_TYPE_strict_btree_checks) \
400+
x(inode_generation, 9, BKEY_TYPE_strict_btree_checks) \
401+
x(dirent, 10, BKEY_TYPE_strict_btree_checks) \
402+
x(xattr, 11, BKEY_TYPE_strict_btree_checks) \
403+
x(alloc, 12, BKEY_TYPE_strict_btree_checks) \
404+
x(quota, 13, BKEY_TYPE_strict_btree_checks) \
405+
x(stripe, 14, BKEY_TYPE_strict_btree_checks) \
406+
x(reflink_p, 15, BKEY_TYPE_strict_btree_checks) \
407+
x(reflink_v, 16, BKEY_TYPE_strict_btree_checks) \
408+
x(inline_data, 17, BKEY_TYPE_strict_btree_checks) \
409+
x(btree_ptr_v2, 18, BKEY_TYPE_strict_btree_checks) \
410+
x(indirect_inline_data, 19, BKEY_TYPE_strict_btree_checks) \
411+
x(alloc_v2, 20, BKEY_TYPE_strict_btree_checks) \
412+
x(subvolume, 21, BKEY_TYPE_strict_btree_checks) \
413+
x(snapshot, 22, BKEY_TYPE_strict_btree_checks) \
414+
x(inode_v2, 23, BKEY_TYPE_strict_btree_checks) \
415+
x(alloc_v3, 24, BKEY_TYPE_strict_btree_checks) \
416+
x(set, 25, 0) \
417+
x(lru, 26, BKEY_TYPE_strict_btree_checks) \
418+
x(alloc_v4, 27, BKEY_TYPE_strict_btree_checks) \
419+
x(backpointer, 28, BKEY_TYPE_strict_btree_checks) \
420+
x(inode_v3, 29, BKEY_TYPE_strict_btree_checks) \
421+
x(bucket_gens, 30, BKEY_TYPE_strict_btree_checks) \
422+
x(snapshot_tree, 31, BKEY_TYPE_strict_btree_checks) \
423+
x(logged_op_truncate, 32, BKEY_TYPE_strict_btree_checks) \
424+
x(logged_op_finsert, 33, BKEY_TYPE_strict_btree_checks) \
425+
x(accounting, 34, BKEY_TYPE_strict_btree_checks) \
426+
x(inode_alloc_cursor, 35, BKEY_TYPE_strict_btree_checks)
423427

424428
enum bch_bkey_type {
425-
#define x(name, nr) KEY_TYPE_##name = nr,
429+
#define x(name, nr, ...) KEY_TYPE_##name = nr,
426430
BCH_BKEY_TYPES()
427431
#undef x
428432
KEY_TYPE_MAX,

fs/bcachefs/bkey_methods.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "xattr.h"
2222

2323
const char * const bch2_bkey_types[] = {
24-
#define x(name, nr) #name,
24+
#define x(name, nr, ...) #name,
2525
BCH_BKEY_TYPES()
2626
#undef x
2727
NULL
@@ -115,7 +115,7 @@ static bool key_type_set_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_
115115
})
116116

117117
const struct bkey_ops bch2_bkey_ops[] = {
118-
#define x(name, nr) [KEY_TYPE_##name] = bch2_bkey_ops_##name,
118+
#define x(name, nr, ...) [KEY_TYPE_##name] = bch2_bkey_ops_##name,
119119
BCH_BKEY_TYPES()
120120
#undef x
121121
};
@@ -155,6 +155,12 @@ static u64 bch2_key_types_allowed[] = {
155155
#undef x
156156
};
157157

158+
static const enum bch_bkey_type_flags bch2_bkey_type_flags[] = {
159+
#define x(name, nr, flags) [KEY_TYPE_##name] = flags,
160+
BCH_BKEY_TYPES()
161+
#undef x
162+
};
163+
158164
const char *bch2_btree_node_type_str(enum btree_node_type type)
159165
{
160166
return type == BKEY_TYPE_btree ? "internal btree node" : bch2_btree_id_str(type - 1);
@@ -177,8 +183,18 @@ int __bch2_bkey_validate(struct bch_fs *c, struct bkey_s_c k,
177183
if (type >= BKEY_TYPE_NR)
178184
return 0;
179185

180-
bkey_fsck_err_on(k.k->type < KEY_TYPE_MAX &&
181-
(type == BKEY_TYPE_btree || (from.flags & BCH_VALIDATE_commit)) &&
186+
enum bch_bkey_type_flags bkey_flags = k.k->type < KEY_TYPE_MAX
187+
? bch2_bkey_type_flags[k.k->type]
188+
: 0;
189+
190+
bool strict_key_type_allowed =
191+
(from.flags & BCH_VALIDATE_commit) ||
192+
type == BKEY_TYPE_btree ||
193+
(from.btree < BTREE_ID_NR &&
194+
(bkey_flags & BKEY_TYPE_strict_btree_checks));
195+
196+
bkey_fsck_err_on(strict_key_type_allowed &&
197+
k.k->type < KEY_TYPE_MAX &&
182198
!(bch2_key_types_allowed[type] & BIT_ULL(k.k->type)),
183199
c, bkey_invalid_type_for_btree,
184200
"invalid key type for btree %s (%s)",

0 commit comments

Comments
 (0)