Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

jfs: add dtpage integrity check to prevent index/pointer overflows

Add check_dtpage() to validate dtpage_t integrity, focusing on
preventing index/pointer overflows from on-disk corruption.

Key checks:
- maxslot must be exactly DTPAGEMAXSLOT (128) as defined for dtpage
slot array.
- freecnt bounded by [0, DTPAGEMAXSLOT-1] (slot[0] reserved for header).
- freelist validity: -1 when freecnt=0; 1~DTPAGEMAXSLOT-1 when non-zero,
with linked list checks (no duplicates, proper termination via next=-1).
- stblindex bounds: must be within range that avoids overlapping with
stbl itself (stblindex < DTPAGEMAXSLOT - stblsize).
- nextindex bounded by stbl size (stblsize << L2DTSLOTSIZE). stbl entries
validity: within 1~DTPAGEMAXSLOT-1, no duplicates(excluding invalid
entries marked as -1).

Invoked when loading dtpage (in BT_GETPAGE macro context) to catch
corruption early before directory operations trigger out-of-bounds access.

Signed-off-by: Yun Zhou <yun.zhou@windriver.com>
Signed-off-by: Dave Kleikamp <dave.kleikamp@oracle.com>

authored by

Yun Zhou and committed by
Dave Kleikamp
119e448b c83abc76

+107 -4
+105 -4
fs/jfs/jfs_dtree.c
··· 115 115 do { \ 116 116 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \ 117 117 if (!(RC)) { \ 118 - if (((P)->header.nextindex > \ 119 - (((BN) == 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \ 120 - ((BN) && (((P)->header.maxslot > DTPAGEMAXSLOT) || \ 121 - ((P)->header.stblindex >= DTPAGEMAXSLOT)))) { \ 118 + if ((BN) && !check_dtpage(P)) { \ 122 119 BT_PUTPAGE(MP); \ 123 120 jfs_error((IP)->i_sb, \ 124 121 "DT_GETPAGE: dtree page corrupt\n"); \ ··· 4374 4377 /* Check for duplicate valid indices (skip check for idx=0) */ 4375 4378 if (unlikely(idx && __test_and_set_bit(idx, bitmap))) { 4376 4379 jfs_err("Duplicate index:%d in stbl in dtroot\n", idx); 4380 + return false; 4381 + } 4382 + } 4383 + 4384 + return true; 4385 + } 4386 + 4387 + bool check_dtpage(dtpage_t *p) 4388 + { 4389 + DECLARE_BITMAP(bitmap, DTPAGEMAXSLOT) = {0}; 4390 + const int stblsize = ((PSIZE >> L2DTSLOTSIZE) + 31) >> L2DTSLOTSIZE; 4391 + int i; 4392 + 4393 + /* Validate maxslot (maximum number of slots in the page) 4394 + * dtpage_t slot array is defined to hold up to DTPAGEMAXSLOT (128) slots 4395 + */ 4396 + if (unlikely(p->header.maxslot != DTPAGEMAXSLOT)) { 4397 + jfs_err("Bad maxslot:%d in dtpage (expected %d)\n", 4398 + p->header.maxslot, DTPAGEMAXSLOT); 4399 + return false; 4400 + } 4401 + 4402 + /* freecnt cannot be negative or exceed DTPAGEMAXSLOT-1 4403 + * (since slot[0] is occupied by the header). 4404 + */ 4405 + if (unlikely(p->header.freecnt < 0 || 4406 + p->header.freecnt > DTPAGEMAXSLOT - 1)) { 4407 + jfs_err("Bad freecnt:%d in dtpage\n", p->header.freecnt); 4408 + return false; 4409 + } else if (p->header.freecnt == 0) { 4410 + /* No free slots: freelist must be -1 */ 4411 + if (unlikely(p->header.freelist != -1)) { 4412 + jfs_err("freecnt=0 but freelist=%d in dtpage\n", 4413 + p->header.freelist); 4414 + return false; 4415 + } 4416 + } else { 4417 + int fsi; 4418 + /* When there are free slots, freelist must be a valid slot index in 4419 + * 1~DTROOTMAXSLOT-1(since slot[0] is occupied by the header). 4420 + */ 4421 + if (unlikely(p->header.freelist < 1 || 4422 + p->header.freelist >= DTPAGEMAXSLOT)) { 4423 + jfs_err("Bad freelist:%d in dtpage\n", p->header.freelist); 4424 + return false; 4425 + } 4426 + 4427 + /* Traverse the free list to check validity of all node indices */ 4428 + fsi = p->header.freelist; 4429 + for (i = 0; i < p->header.freecnt - 1; i++) { 4430 + /* Check for duplicate indices in the free list */ 4431 + if (unlikely(__test_and_set_bit(fsi, bitmap))) { 4432 + jfs_err("duplicate index%d in slot in dtpage\n", fsi); 4433 + return false; 4434 + } 4435 + fsi = p->slot[fsi].next; 4436 + 4437 + /* Ensure the next slot index in the free list is valid */ 4438 + if (unlikely(fsi < 1 || fsi >= DTPAGEMAXSLOT)) { 4439 + jfs_err("Bad index:%d in slot in dtpage\n", fsi); 4440 + return false; 4441 + } 4442 + } 4443 + 4444 + /* The last node in the free list must terminate with next = -1 */ 4445 + if (unlikely(p->slot[fsi].next != -1)) { 4446 + jfs_err("Bad next:%d of the last slot in dtpage\n", 4447 + p->slot[fsi].next); 4448 + return false; 4449 + } 4450 + } 4451 + 4452 + /* stbl must be little then DTPAGEMAXSLOT */ 4453 + if (unlikely(p->header.stblindex >= DTPAGEMAXSLOT - stblsize)) { 4454 + jfs_err("Bad stblindex:%d in dtpage (stbl size %d)\n", 4455 + p->header.stblindex, stblsize); 4456 + return false; 4457 + } 4458 + 4459 + /* nextindex must be little then stblsize*32 */ 4460 + if (unlikely(p->header.nextindex > (stblsize << L2DTSLOTSIZE))) { 4461 + jfs_err("Bad nextindex:%d in dtpage (stbl size %d)\n", 4462 + p->header.nextindex, stblsize); 4463 + return false; 4464 + } 4465 + 4466 + /* Validate stbl entries 4467 + * Each entry is a slot index, valid range: -1 (invalid) or 4468 + * [0, nextindex-1] (valid data slots) 4469 + * (stblindex and higher slots are reserved for stbl itself) 4470 + */ 4471 + for (i = 0; i < p->header.nextindex; i++) { 4472 + int idx = DT_GETSTBL(p)[i]; 4473 + 4474 + /* Check if index is out of valid data slot range */ 4475 + if (unlikely(idx < 1 || idx >= DTPAGEMAXSLOT)) { 4476 + jfs_err("Bad stbl[%d] index:%d (stblindex %d) in dtpage\n", 4477 + i, idx, p->header.stblindex); 4478 + return false; 4479 + } 4480 + 4481 + /* Check for duplicate valid indices (skip -1) */ 4482 + if (unlikely(__test_and_set_bit(idx, bitmap))) { 4483 + jfs_err("Duplicate index:%d in stbl of dtpage\n", idx); 4377 4484 return false; 4378 4485 } 4379 4486 }
+2
fs/jfs/jfs_dtree.h
··· 255 255 extern int jfs_readdir(struct file *file, struct dir_context *ctx); 256 256 257 257 extern bool check_dtroot(dtroot_t *p); 258 + 259 + extern bool check_dtpage(dtpage_t *p); 258 260 #endif /* !_H_JFS_DTREE */