Skip to content

Commit b67fd3b

Browse files
zhangyi089gregkh
authored andcommitted
ext4: fix potential use after free after remounting with noblock_validity
[ Upstream commit 7727ae5 ] Remount process will release system zone which was allocated before if "noblock_validity" is specified. If we mount an ext4 file system to two mountpoints with default mount options, and then remount one of them with "noblock_validity", it may trigger a use after free problem when someone accessing the other one. # mount /dev/sda foo # mount /dev/sda bar User access mountpoint "foo" | Remount mountpoint "bar" | ext4_map_blocks() | ext4_remount() check_block_validity() | ext4_setup_system_zone() ext4_data_block_valid() | ext4_release_system_zone() | free system_blks rb nodes access system_blks rb nodes | trigger use after free | This problem can also be reproduced by one mountpint, At the same time, add_system_zone() can get called during remount as well so there can be racing ext4_data_block_valid() reading the rbtree at the same time. This patch add RCU to protect system zone from releasing or building when doing a remount which inverse current "noblock_validity" mount option. It assign the rbtree after the whole tree was complete and do actual freeing after rcu grace period, avoid any intermediate state. Reported-by: syzbot+1e470567330b7ad711d5@syzkaller.appspotmail.com Signed-off-by: zhangyi (F) <yi.zhang@huawei.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent ad849db commit b67fd3b

2 files changed

Lines changed: 147 additions & 52 deletions

File tree

‎fs/ext4/block_validity.c‎

Lines changed: 138 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int __init ext4_init_system_zone(void)
3838

3939
void ext4_exit_system_zone(void)
4040
{
41+
rcu_barrier();
4142
kmem_cache_destroy(ext4_system_zone_cachep);
4243
}
4344

@@ -49,17 +50,26 @@ static inline int can_merge(struct ext4_system_zone *entry1,
4950
return 0;
5051
}
5152

53+
static void release_system_zone(struct ext4_system_blocks *system_blks)
54+
{
55+
struct ext4_system_zone *entry, *n;
56+
57+
rbtree_postorder_for_each_entry_safe(entry, n,
58+
&system_blks->root, node)
59+
kmem_cache_free(ext4_system_zone_cachep, entry);
60+
}
61+
5262
/*
5363
* Mark a range of blocks as belonging to the "system zone" --- that
5464
* is, filesystem metadata blocks which should never be used by
5565
* inodes.
5666
*/
57-
static int add_system_zone(struct ext4_sb_info *sbi,
67+
static int add_system_zone(struct ext4_system_blocks *system_blks,
5868
ext4_fsblk_t start_blk,
5969
unsigned int count)
6070
{
6171
struct ext4_system_zone *new_entry = NULL, *entry;
62-
struct rb_node **n = &sbi->system_blks.rb_node, *node;
72+
struct rb_node **n = &system_blks->root.rb_node, *node;
6373
struct rb_node *parent = NULL, *new_node = NULL;
6474

6575
while (*n) {
@@ -91,7 +101,7 @@ static int add_system_zone(struct ext4_sb_info *sbi,
91101
new_node = &new_entry->node;
92102

93103
rb_link_node(new_node, parent, n);
94-
rb_insert_color(new_node, &sbi->system_blks);
104+
rb_insert_color(new_node, &system_blks->root);
95105
}
96106

97107
/* Can we merge to the left? */
@@ -101,7 +111,7 @@ static int add_system_zone(struct ext4_sb_info *sbi,
101111
if (can_merge(entry, new_entry)) {
102112
new_entry->start_blk = entry->start_blk;
103113
new_entry->count += entry->count;
104-
rb_erase(node, &sbi->system_blks);
114+
rb_erase(node, &system_blks->root);
105115
kmem_cache_free(ext4_system_zone_cachep, entry);
106116
}
107117
}
@@ -112,7 +122,7 @@ static int add_system_zone(struct ext4_sb_info *sbi,
112122
entry = rb_entry(node, struct ext4_system_zone, node);
113123
if (can_merge(new_entry, entry)) {
114124
new_entry->count += entry->count;
115-
rb_erase(node, &sbi->system_blks);
125+
rb_erase(node, &system_blks->root);
116126
kmem_cache_free(ext4_system_zone_cachep, entry);
117127
}
118128
}
@@ -126,7 +136,7 @@ static void debug_print_tree(struct ext4_sb_info *sbi)
126136
int first = 1;
127137

128138
printk(KERN_INFO "System zones: ");
129-
node = rb_first(&sbi->system_blks);
139+
node = rb_first(&sbi->system_blks->root);
130140
while (node) {
131141
entry = rb_entry(node, struct ext4_system_zone, node);
132142
printk(KERN_CONT "%s%llu-%llu", first ? "" : ", ",
@@ -137,7 +147,47 @@ static void debug_print_tree(struct ext4_sb_info *sbi)
137147
printk(KERN_CONT "\n");
138148
}
139149

140-
static int ext4_protect_reserved_inode(struct super_block *sb, u32 ino)
150+
/*
151+
* Returns 1 if the passed-in block region (start_blk,
152+
* start_blk+count) is valid; 0 if some part of the block region
153+
* overlaps with filesystem metadata blocks.
154+
*/
155+
static int ext4_data_block_valid_rcu(struct ext4_sb_info *sbi,
156+
struct ext4_system_blocks *system_blks,
157+
ext4_fsblk_t start_blk,
158+
unsigned int count)
159+
{
160+
struct ext4_system_zone *entry;
161+
struct rb_node *n;
162+
163+
if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
164+
(start_blk + count < start_blk) ||
165+
(start_blk + count > ext4_blocks_count(sbi->s_es))) {
166+
sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
167+
return 0;
168+
}
169+
170+
if (system_blks == NULL)
171+
return 1;
172+
173+
n = system_blks->root.rb_node;
174+
while (n) {
175+
entry = rb_entry(n, struct ext4_system_zone, node);
176+
if (start_blk + count - 1 < entry->start_blk)
177+
n = n->rb_left;
178+
else if (start_blk >= (entry->start_blk + entry->count))
179+
n = n->rb_right;
180+
else {
181+
sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
182+
return 0;
183+
}
184+
}
185+
return 1;
186+
}
187+
188+
static int ext4_protect_reserved_inode(struct super_block *sb,
189+
struct ext4_system_blocks *system_blks,
190+
u32 ino)
141191
{
142192
struct inode *inode;
143193
struct ext4_sb_info *sbi = EXT4_SB(sb);
@@ -163,14 +213,15 @@ static int ext4_protect_reserved_inode(struct super_block *sb, u32 ino)
163213
if (n == 0) {
164214
i++;
165215
} else {
166-
if (!ext4_data_block_valid(sbi, map.m_pblk, n)) {
216+
if (!ext4_data_block_valid_rcu(sbi, system_blks,
217+
map.m_pblk, n)) {
167218
ext4_error(sb, "blocks %llu-%llu from inode %u "
168219
"overlap system zone", map.m_pblk,
169220
map.m_pblk + map.m_len - 1, ino);
170221
err = -EFSCORRUPTED;
171222
break;
172223
}
173-
err = add_system_zone(sbi, map.m_pblk, n);
224+
err = add_system_zone(system_blks, map.m_pblk, n);
174225
if (err < 0)
175226
break;
176227
i += n;
@@ -180,94 +231,130 @@ static int ext4_protect_reserved_inode(struct super_block *sb, u32 ino)
180231
return err;
181232
}
182233

234+
static void ext4_destroy_system_zone(struct rcu_head *rcu)
235+
{
236+
struct ext4_system_blocks *system_blks;
237+
238+
system_blks = container_of(rcu, struct ext4_system_blocks, rcu);
239+
release_system_zone(system_blks);
240+
kfree(system_blks);
241+
}
242+
243+
/*
244+
* Build system zone rbtree which is used for block validity checking.
245+
*
246+
* The update of system_blks pointer in this function is protected by
247+
* sb->s_umount semaphore. However we have to be careful as we can be
248+
* racing with ext4_data_block_valid() calls reading system_blks rbtree
249+
* protected only by RCU. That's why we first build the rbtree and then
250+
* swap it in place.
251+
*/
183252
int ext4_setup_system_zone(struct super_block *sb)
184253
{
185254
ext4_group_t ngroups = ext4_get_groups_count(sb);
186255
struct ext4_sb_info *sbi = EXT4_SB(sb);
256+
struct ext4_system_blocks *system_blks;
187257
struct ext4_group_desc *gdp;
188258
ext4_group_t i;
189259
int flex_size = ext4_flex_bg_size(sbi);
190260
int ret;
191261

192262
if (!test_opt(sb, BLOCK_VALIDITY)) {
193-
if (sbi->system_blks.rb_node)
263+
if (sbi->system_blks)
194264
ext4_release_system_zone(sb);
195265
return 0;
196266
}
197-
if (sbi->system_blks.rb_node)
267+
if (sbi->system_blks)
198268
return 0;
199269

270+
system_blks = kzalloc(sizeof(*system_blks), GFP_KERNEL);
271+
if (!system_blks)
272+
return -ENOMEM;
273+
200274
for (i=0; i < ngroups; i++) {
201275
cond_resched();
202276
if (ext4_bg_has_super(sb, i) &&
203277
((i < 5) || ((i % flex_size) == 0)))
204-
add_system_zone(sbi, ext4_group_first_block_no(sb, i),
278+
add_system_zone(system_blks,
279+
ext4_group_first_block_no(sb, i),
205280
ext4_bg_num_gdb(sb, i) + 1);
206281
gdp = ext4_get_group_desc(sb, i, NULL);
207-
ret = add_system_zone(sbi, ext4_block_bitmap(sb, gdp), 1);
282+
ret = add_system_zone(system_blks,
283+
ext4_block_bitmap(sb, gdp), 1);
208284
if (ret)
209-
return ret;
210-
ret = add_system_zone(sbi, ext4_inode_bitmap(sb, gdp), 1);
285+
goto err;
286+
ret = add_system_zone(system_blks,
287+
ext4_inode_bitmap(sb, gdp), 1);
211288
if (ret)
212-
return ret;
213-
ret = add_system_zone(sbi, ext4_inode_table(sb, gdp),
289+
goto err;
290+
ret = add_system_zone(system_blks,
291+
ext4_inode_table(sb, gdp),
214292
sbi->s_itb_per_group);
215293
if (ret)
216-
return ret;
294+
goto err;
217295
}
218296
if (ext4_has_feature_journal(sb) && sbi->s_es->s_journal_inum) {
219-
ret = ext4_protect_reserved_inode(sb,
297+
ret = ext4_protect_reserved_inode(sb, system_blks,
220298
le32_to_cpu(sbi->s_es->s_journal_inum));
221299
if (ret)
222-
return ret;
300+
goto err;
223301
}
224302

303+
/*
304+
* System blks rbtree complete, announce it once to prevent racing
305+
* with ext4_data_block_valid() accessing the rbtree at the same
306+
* time.
307+
*/
308+
rcu_assign_pointer(sbi->system_blks, system_blks);
309+
225310
if (test_opt(sb, DEBUG))
226311
debug_print_tree(sbi);
227312
return 0;
313+
err:
314+
release_system_zone(system_blks);
315+
kfree(system_blks);
316+
return ret;
228317
}
229318

230-
/* Called when the filesystem is unmounted */
319+
/*
320+
* Called when the filesystem is unmounted or when remounting it with
321+
* noblock_validity specified.
322+
*
323+
* The update of system_blks pointer in this function is protected by
324+
* sb->s_umount semaphore. However we have to be careful as we can be
325+
* racing with ext4_data_block_valid() calls reading system_blks rbtree
326+
* protected only by RCU. So we first clear the system_blks pointer and
327+
* then free the rbtree only after RCU grace period expires.
328+
*/
231329
void ext4_release_system_zone(struct super_block *sb)
232330
{
233-
struct ext4_system_zone *entry, *n;
331+
struct ext4_system_blocks *system_blks;
234332

235-
rbtree_postorder_for_each_entry_safe(entry, n,
236-
&EXT4_SB(sb)->system_blks, node)
237-
kmem_cache_free(ext4_system_zone_cachep, entry);
333+
system_blks = rcu_dereference_protected(EXT4_SB(sb)->system_blks,
334+
lockdep_is_held(&sb->s_umount));
335+
rcu_assign_pointer(EXT4_SB(sb)->system_blks, NULL);
238336

239-
EXT4_SB(sb)->system_blks = RB_ROOT;
337+
if (system_blks)
338+
call_rcu(&system_blks->rcu, ext4_destroy_system_zone);
240339
}
241340

242-
/*
243-
* Returns 1 if the passed-in block region (start_blk,
244-
* start_blk+count) is valid; 0 if some part of the block region
245-
* overlaps with filesystem metadata blocks.
246-
*/
247341
int ext4_data_block_valid(struct ext4_sb_info *sbi, ext4_fsblk_t start_blk,
248342
unsigned int count)
249343
{
250-
struct ext4_system_zone *entry;
251-
struct rb_node *n = sbi->system_blks.rb_node;
344+
struct ext4_system_blocks *system_blks;
345+
int ret;
252346

253-
if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
254-
(start_blk + count < start_blk) ||
255-
(start_blk + count > ext4_blocks_count(sbi->s_es))) {
256-
sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
257-
return 0;
258-
}
259-
while (n) {
260-
entry = rb_entry(n, struct ext4_system_zone, node);
261-
if (start_blk + count - 1 < entry->start_blk)
262-
n = n->rb_left;
263-
else if (start_blk >= (entry->start_blk + entry->count))
264-
n = n->rb_right;
265-
else {
266-
sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
267-
return 0;
268-
}
269-
}
270-
return 1;
347+
/*
348+
* Lock the system zone to prevent it being released concurrently
349+
* when doing a remount which inverse current "[no]block_validity"
350+
* mount option.
351+
*/
352+
rcu_read_lock();
353+
system_blks = rcu_dereference(sbi->system_blks);
354+
ret = ext4_data_block_valid_rcu(sbi, system_blks, start_blk,
355+
count);
356+
rcu_read_unlock();
357+
return ret;
271358
}
272359

273360
int ext4_check_blockref(const char *function, unsigned int line,

‎fs/ext4/ext4.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ struct ext4_map_blocks {
184184
unsigned int m_flags;
185185
};
186186

187+
/*
188+
* Block validity checking, system zone rbtree.
189+
*/
190+
struct ext4_system_blocks {
191+
struct rb_root root;
192+
struct rcu_head rcu;
193+
};
194+
187195
/*
188196
* Flags for ext4_io_end->flags
189197
*/
@@ -1420,7 +1428,7 @@ struct ext4_sb_info {
14201428
int s_jquota_fmt; /* Format of quota to use */
14211429
#endif
14221430
unsigned int s_want_extra_isize; /* New inodes should reserve # bytes */
1423-
struct rb_root system_blks;
1431+
struct ext4_system_blocks __rcu *system_blks;
14241432

14251433
#ifdef EXTENTS_STATS
14261434
/* ext4 extents stats */

0 commit comments

Comments
 (0)