nilfs2: fix slab-out-of-bounds in nilfs_direct_propagate after truncation
Ryusuke Konishi
maintainerPATCH 1/1 nilfs2: fix slab-out-of-bounds in nilfs_direct_propagate after truncation
Shuangpeng Bai reported that KASAN detected a slab-out-of-bounds error in nilfs_direct_propagate() during testing.
Analysis revealed that after truncating a file, a node block immediately below the B-tree root was not deleted. Instead, it remained in the B-tree node cache in a dirty state. The log writer subsequently detected this block and incorrectly invoked nilfs_direct_propagate() on it, which is designed to handle only data blocks in direct mapping.
B-tree nodes in the cache are managed by virtual block numbers, and their logical keys typically exceed the range expected by direct mapping. Consequently, processing such a node as a direct mapping entry triggers a slab-out-of-bounds access.
The root cause is that when a B-tree mapping collapses into a direct mapping during truncation, an intermediate node block pointed to by the root node is left behind as garbage instead of being explicitly deleted.
This resolves the issue by adding a nilfs_btree_discard() operation to delete the remaining intermediate node block during the conversion. A 'deform' flag is added to the bop_delete interface to explicitly signal that the deletion is part of a mapping transformation. This allows the B-tree mapping implementation to perform the necessary cleanup and discarding of the residual node structure that would be otherwise be left orphaned after the transition.
---
Hi Viacheslav,
Please apply this for the next cycle.
This fixes a flaw in the original B-tree implementation related to truncation and resolves the reported out-of-bounds memory access issue.
Thanks,
Ryusuke Konishi
fs/nilfs2/bmap.c | 2 +- fs/nilfs2/bmap.h | 2 +- fs/nilfs2/btree.c | 39 ++++++++++++++++++++++++++++++++------- fs/nilfs2/direct.c | 4 ++-- 4 files changed, 36 insertions(+), 11 deletions(-)
fs/nilfs2/bmap.c +1 -1
@@ -175,7 +175,7 @@ static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)return ret; } -return bmap->b_ops->bop_delete (bmap, key); +return bmap->b_ops->bop_delete (bmap, key, false); }/**
fs/nilfs2/bmap.h +1 -1
@@ -63,7 +63,7 @@ struct nilfs_bmap_operations {int (*bop_lookup_contig)(const struct nilfs_bmap *,__u64 ,__u64 *,unsigned int );int (*bop_insert)(struct nilfs_bmap *,__u64 ,__u64 ); -int (*bop_delete)(struct nilfs_bmap *,__u64 ); +int (*bop_delete)(struct nilfs_bmap *bmap,__u64 key,bool deform);void (*bop_clear)(struct nilfs_bmap *);int (*bop_propagate)(struct nilfs_bmap *,struct buffer_head *);
fs/nilfs2/btree.c +32 -7
@@ -1425,6 +1425,28 @@ static void nilfs_btree_shrink(struct nilfs_bmap *btree, path[level].bp_bh =NULL ; } +/** +* nilfs_btree_discard - discard the last node for the mapping transformation +* @btree: bmap struct of btree +* @path: array of nilfs_btree_path struct +* @level: level of the B-tree node being operated on +* @keyp: argument for passing a key (unused) +* @ptrp: argument for passing a pointer (unused) +*/ +static void nilfs_btree_discard (struct nilfs_bmap *btree, +struct nilfs_btree_path *path,int level, +__u64 *keyp,__u64 *ptrp) +{ +struct nilfs_btree_node *root =nilfs_btree_get_root (btree); + +nilfs_btree_node_delete (root,0 ,NULL ,NULL , +NILFS_BTREE_ROOT_NCHILDREN_MAX ); +nilfs_btree_node_set_level (root, level); + +nilfs_btnode_delete (path[level].bp_bh); + path[level].bp_bh =NULL ; +} +static void nilfs_btree_nop (struct nilfs_bmap *btree,struct nilfs_btree_path *path,int level,__u64 *keyp,__u64 *ptrp)
@@ -1435,7 +1457,7 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,struct nilfs_btree_path *path, int *levelp, struct nilfs_bmap_stats *stats, - struct inode *dat) + struct inode *dat, bool deform) { struct buffer_head *bh;struct nilfs_btree_node *node, *parent, *sib;
@@ -1522,15 +1544,17 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,if (nilfs_btree_node_get_nchildren (node) -1 <=NILFS_BTREE_ROOT_NCHILDREN_MAX ) { path[level].bp_op = nilfs_btree_shrink; - stats->bs_nblocks +=2 ; - level++; - path[level].bp_op = nilfs_btree_nop; - goto shrink_root_child; + }else if (deform) { + path[level].bp_op = nilfs_btree_discard; }else { path[level].bp_op = nilfs_btree_do_delete; stats->bs_nblocks++; goto out; } + stats->bs_nblocks +=2 ; + level++; + path[level].bp_op = nilfs_btree_nop; + goto shrink_root_child; } }
@@ -1581,7 +1605,7 @@ static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,nilfs_bmap_set_dirty (btree); } -static int nilfs_btree_delete (struct nilfs_bmap *btree,__u64 key) +static int nilfs_btree_delete (struct nilfs_bmap *btree,__u64 key,bool deform) {struct nilfs_btree_path *path;
@@ -1601,7 +1625,8 @@ static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key) dat =NILFS_BMAP_USE_VBN (btree) ?nilfs_bmap_get_dat (btree) :NULL ; - ret =nilfs_btree_prepare_delete (btree, path, &level, &stats, dat); + ret =nilfs_btree_prepare_delete (btree, path, &level, &stats, dat, + deform);if (ret <0 ) goto out;nilfs_btree_commit_delete (btree, path, level, dat);
fs/nilfs2/direct.c +2 -2
@@ -144,7 +144,7 @@ static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)return ret; } -static int nilfs_direct_delete (struct nilfs_bmap *bmap,__u64 key) +static int nilfs_direct_delete (struct nilfs_bmap *bmap,__u64 key,bool deform) {union nilfs_bmap_ptr_req req;struct inode *dat;
@@ -234,7 +234,7 @@ int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,/* no need to allocate any resource for conversion */ /* delete */ - ret = bmap->b_ops->bop_delete (bmap, key); + ret = bmap->b_ops->bop_delete (bmap, key, true);if (ret <0 )return ret;
Signature · 1 line
2.43.0
Quoted 215 lines · Ryusuke Konishi
Shuangpeng Bai reported that KASAN detected a slab-out-of-bounds error
innilfs_direct_propagate()during testing.Analysis revealed that after truncating a file, a node block immediately
below the B-tree root was not deleted. Instead, it remained in the B-tree
node cache in a dirty state. The log writer subsequently detected this
block and incorrectly invokednilfs_direct_propagate()on it, which is
designed to handle only data blocks in direct mapping.B-tree nodes in the cache are managed by virtual block numbers, and their
logical keys typically exceed the range expected by direct mapping. Consequently, processing such a node as a direct mapping entry triggers
a slab-out-of-bounds access.The root cause is that when a B-tree mapping collapses into a direct mapping during truncation, an intermediate node block pointed to by the
root node is left behind as garbage instead of being explicitly deleted.This resolves the issue by adding a
nilfs_btree_discard()operation to delete the remaining intermediate node block during the conversion.
A 'deform' flag is added to thebop_deleteinterface to explicitly signal
that the deletion is part of a mapping transformation. This allows the
B-tree mapping implementation to perform the necessary cleanup and discarding of the residual node structure that would be otherwise be left
orphaned after the transition.Reported-by Shuangpeng Bai <[email protected]>Closes:
https://lore.kernel.org/r/[email protected]Fixes 36a580eb489f ("nilfs2: direct block mapping")Signed-off-by Ryusuke Konishi <[email protected]>---
Hi Viacheslav,Please apply this for the next cycle.
This fixes a flaw in the original B-tree implementation related to truncation
and resolves the reported out-of-bounds memory access issue.Thanks,
Ryusuke Konishifs/nilfs2/bmap.c | 2 +-
fs/nilfs2/bmap.h | 2 +-
fs/nilfs2/btree.c | 39 ++++++++++++++++++++++++++++++++-------fs/nilfs2/direct.c| 4 ++--4 files changed, 36 insertions(+), 11 deletions(-)fs/nilfs2/bmap.c +0
-0@@ -175,7 +175,7 @@ static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key) return ret; }fs/nilfs2/bmap.c +1
-1-return bmap->b_ops->bop_delete (bmap, key); +return bmap->b_ops->bop_delete (bmap, key, false);}
/**fs/nilfs2/bmap.h +1
-1@@ -63,7 +63,7 @@ struct nilfs_bmap_operations { int (*bop_lookup_contig)(const struct nilfs_bmap *, __u64,__u64 *, unsigned int); int (*bop_insert)(struct nilfs_bmap *, __u64, __u64); -int (*bop_delete)(struct nilfs_bmap *,__u64 ); +int (*bop_delete)(struct nilfs_bmap *bmap,__u64 key,bool deform); void (*bop_clear)(struct nilfs_bmap *);
int (*bop_propagate)(structnilfs_bmap*, structbuffer_head*);fs/nilfs2/btree.c +0
-0@@ -1425,6 +1425,28 @@ static void nilfs_btree_shrink(structnilfs_bmap *btree, path[level].bp_bh = NULL; }
+/**
+ *nilfs_btree_discard- discard the last node for the mapping transformation
+ * @btree: bmap struct of btree
+ * @path: array ofnilfs_btree_pathstruct
+ * @level: level of the B-tree node being operated on
+ * @keyp: argument for passing a key (unused)
+ * @ptrp: argument for passing a pointer (unused)
+ */fs/nilfs2/btree.c +2
-0+static void nilfs_btree_discard (struct nilfs_bmap *btree, + struct nilfs_btree_path *path, intlevel,
fs/nilfs2/btree.c +12
-0+ __u64 *keyp, __u64 *ptrp) +{ +struct nilfs_btree_node *root =nilfs_btree_get_root (btree); + +nilfs_btree_node_delete (root,0 ,NULL ,NULL , +NILFS_BTREE_ROOT_NCHILDREN_MAX ); +nilfs_btree_node_set_level (root, level); + +nilfs_btnode_delete (path[level].bp_bh); + path[level].bp_bh =NULL ; +} +static void
nilfs_btree_nop(struct nilfs_bmap *btree, struct nilfs_btree_path *path, int level, __u64 *keyp, __u64 *ptrp)fs/nilfs2/btree.c +0
-0@@ -1435,7 +1457,7 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree, struct nilfs_btree_path *path, int *levelp,struct
nilfs_bmap_stats*stats,fs/nilfs2/btree.c +1
-1- struct inode *dat) + struct inode *dat, booldeform)
{
structbuffer_head*bh;
structnilfs_btree_node*node, *parent, *sib;fs/nilfs2/btree.c +0
-0@@ -1522,15 +1544,17 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,if (
nilfs_btree_node_get_nchildren(node)- 1 <=
NILFS_BTREE_ROOT_NCHILDREN_MAX) { path[level].bp_op =nilfs_btree_shrink;fs/nilfs2/btree.c +2
-4- stats->bs_nblocks +=2 ; - level++; - path[level].bp_op = nilfs_btree_nop; - goto shrink_root_child; + }else if (deform) { + path[level].bp_op =
nilfs_btree_discard;
} else { path[level].bp_op =nilfs_btree_do_delete;
stats->bs_nblocks++; goto out;
}fs/nilfs2/btree.c +4
-0+ stats->bs_nblocks +=2 ; + level++; + path[level].bp_op = nilfs_btree_nop; + goto shrink_root_child;}
}fs/nilfs2/btree.c +0
-0@@ -1581,7 +1605,7 @@ static void nilfs_btree_commit_delete(structnilfs_bmap *btree, nilfs_bmap_set_dirty(btree); }fs/nilfs2/btree.c +1
-1-static int nilfs_btree_delete (struct nilfs_bmap *btree,__u64 key) +static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key,bool deform)
{
structnilfs_btree_path*path;fs/nilfs2/btree.c +0
-0@@ -1601,7 +1625,8 @@ static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
dat =NILFS_BMAP_USE_VBN(btree)?nilfs_bmap_get_dat(btree): NULL;fs/nilfs2/btree.c +0
-1- ret = nilfs_btree_prepare_delete(btree, path, &level,&stats, dat);
fs/nilfs2/btree.c +1
-0+ ret = nilfs_btree_prepare_delete(btree, path, &level,&stats, dat,
fs/nilfs2/btree.c +1
-0+ deform);if (ret < 0)
goto out;nilfs_btree_commit_delete(btree, path, level, dat);fs/nilfs2/direct.c +0
-0@@ -144,7 +144,7 @@ static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr) return ret; }fs/nilfs2/direct.c +1
-1-static int nilfs_direct_delete (struct nilfs_bmap *bmap,__u64 key) +static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key,bool deform)
{
unionnilfs_bmap_ptr_reqreq;
struct inode *dat;fs/nilfs2/direct.c +0
-0@@ -234,7 +234,7 @@ int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap, /* no need to allocate any resource for conversion */
/* delete */fs/nilfs2/direct.c +1
-1- ret = bmap->b_ops->bop_delete (bmap, key); + ret = bmap->b_ops->bop_delete (bmap, key, true);
Quoted 3 lines
if (ret < 0)
return ret;
Applied.
Thanks,
Slava.