Skip to content

Commit 082c744

Browse files
author
Kent Overstreet
committed
bcachefs: Fix downgrade_table_extra()
Fix a UAF: we were calling darray_make_room() and retaining a pointer to the old buffer. And fix an UBSAN warning: struct bch_sb_field_downgrade_entry uses __counted_by, so set dst->nr_errors before assigning to the array entry. Reported-by: [email protected] Signed-off-by: Kent Overstreet <[email protected]>
1 parent 757601e commit 082c744

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

fs/bcachefs/sb-downgrade.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ DOWNGRADE_TABLE()
253253

254254
static int downgrade_table_extra(struct bch_fs *c, darray_char *table)
255255
{
256+
unsigned dst_offset = table->nr;
256257
struct bch_sb_field_downgrade_entry *dst = (void *) &darray_top(*table);
257258
unsigned bytes = sizeof(*dst) + sizeof(dst->errors[0]) * le16_to_cpu(dst->nr_errors);
258259
int ret = 0;
@@ -268,6 +269,9 @@ static int downgrade_table_extra(struct bch_fs *c, darray_char *table)
268269
if (ret)
269270
return ret;
270271

272+
dst = (void *) &table->data[dst_offset];
273+
dst->nr_errors = cpu_to_le16(nr_errors + 1);
274+
271275
/* open coded __set_bit_le64, as dst is packed and
272276
* dst->recovery_passes is misaligned */
273277
unsigned b = BCH_RECOVERY_PASS_STABLE_check_allocations;
@@ -278,7 +282,6 @@ static int downgrade_table_extra(struct bch_fs *c, darray_char *table)
278282
break;
279283
}
280284

281-
dst->nr_errors = cpu_to_le16(nr_errors);
282285
return ret;
283286
}
284287

0 commit comments

Comments
 (0)