Skip to content

Commit 119e448

Browse files
zhouyun1306kleikamp
authored andcommitted
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>
1 parent c83abc7 commit 119e448

2 files changed

Lines changed: 107 additions & 4 deletions

File tree

fs/jfs/jfs_dtree.c

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ struct dtsplit {
115115
do { \
116116
BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \
117117
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)) { \
122119
BT_PUTPAGE(MP); \
123120
jfs_error((IP)->i_sb, \
124121
"DT_GETPAGE: dtree page corrupt\n"); \
@@ -4383,3 +4380,107 @@ bool check_dtroot(dtroot_t *p)
43834380

43844381
return true;
43854382
}
4383+
4384+
bool check_dtpage(dtpage_t *p)
4385+
{
4386+
DECLARE_BITMAP(bitmap, DTPAGEMAXSLOT) = {0};
4387+
const int stblsize = ((PSIZE >> L2DTSLOTSIZE) + 31) >> L2DTSLOTSIZE;
4388+
int i;
4389+
4390+
/* Validate maxslot (maximum number of slots in the page)
4391+
* dtpage_t slot array is defined to hold up to DTPAGEMAXSLOT (128) slots
4392+
*/
4393+
if (unlikely(p->header.maxslot != DTPAGEMAXSLOT)) {
4394+
jfs_err("Bad maxslot:%d in dtpage (expected %d)\n",
4395+
p->header.maxslot, DTPAGEMAXSLOT);
4396+
return false;
4397+
}
4398+
4399+
/* freecnt cannot be negative or exceed DTPAGEMAXSLOT-1
4400+
* (since slot[0] is occupied by the header).
4401+
*/
4402+
if (unlikely(p->header.freecnt < 0 ||
4403+
p->header.freecnt > DTPAGEMAXSLOT - 1)) {
4404+
jfs_err("Bad freecnt:%d in dtpage\n", p->header.freecnt);
4405+
return false;
4406+
} else if (p->header.freecnt == 0) {
4407+
/* No free slots: freelist must be -1 */
4408+
if (unlikely(p->header.freelist != -1)) {
4409+
jfs_err("freecnt=0 but freelist=%d in dtpage\n",
4410+
p->header.freelist);
4411+
return false;
4412+
}
4413+
} else {
4414+
int fsi;
4415+
/* When there are free slots, freelist must be a valid slot index in
4416+
* 1~DTROOTMAXSLOT-1(since slot[0] is occupied by the header).
4417+
*/
4418+
if (unlikely(p->header.freelist < 1 ||
4419+
p->header.freelist >= DTPAGEMAXSLOT)) {
4420+
jfs_err("Bad freelist:%d in dtpage\n", p->header.freelist);
4421+
return false;
4422+
}
4423+
4424+
/* Traverse the free list to check validity of all node indices */
4425+
fsi = p->header.freelist;
4426+
for (i = 0; i < p->header.freecnt - 1; i++) {
4427+
/* Check for duplicate indices in the free list */
4428+
if (unlikely(__test_and_set_bit(fsi, bitmap))) {
4429+
jfs_err("duplicate index%d in slot in dtpage\n", fsi);
4430+
return false;
4431+
}
4432+
fsi = p->slot[fsi].next;
4433+
4434+
/* Ensure the next slot index in the free list is valid */
4435+
if (unlikely(fsi < 1 || fsi >= DTPAGEMAXSLOT)) {
4436+
jfs_err("Bad index:%d in slot in dtpage\n", fsi);
4437+
return false;
4438+
}
4439+
}
4440+
4441+
/* The last node in the free list must terminate with next = -1 */
4442+
if (unlikely(p->slot[fsi].next != -1)) {
4443+
jfs_err("Bad next:%d of the last slot in dtpage\n",
4444+
p->slot[fsi].next);
4445+
return false;
4446+
}
4447+
}
4448+
4449+
/* stbl must be little then DTPAGEMAXSLOT */
4450+
if (unlikely(p->header.stblindex >= DTPAGEMAXSLOT - stblsize)) {
4451+
jfs_err("Bad stblindex:%d in dtpage (stbl size %d)\n",
4452+
p->header.stblindex, stblsize);
4453+
return false;
4454+
}
4455+
4456+
/* nextindex must be little then stblsize*32 */
4457+
if (unlikely(p->header.nextindex > (stblsize << L2DTSLOTSIZE))) {
4458+
jfs_err("Bad nextindex:%d in dtpage (stbl size %d)\n",
4459+
p->header.nextindex, stblsize);
4460+
return false;
4461+
}
4462+
4463+
/* Validate stbl entries
4464+
* Each entry is a slot index, valid range: -1 (invalid) or
4465+
* [0, nextindex-1] (valid data slots)
4466+
* (stblindex and higher slots are reserved for stbl itself)
4467+
*/
4468+
for (i = 0; i < p->header.nextindex; i++) {
4469+
int idx = DT_GETSTBL(p)[i];
4470+
4471+
/* Check if index is out of valid data slot range */
4472+
if (unlikely(idx < 1 || idx >= DTPAGEMAXSLOT)) {
4473+
jfs_err("Bad stbl[%d] index:%d (stblindex %d) in dtpage\n",
4474+
i, idx, p->header.stblindex);
4475+
return false;
4476+
}
4477+
4478+
/* Check for duplicate valid indices (skip -1) */
4479+
if (unlikely(__test_and_set_bit(idx, bitmap))) {
4480+
jfs_err("Duplicate index:%d in stbl of dtpage\n", idx);
4481+
return false;
4482+
}
4483+
}
4484+
4485+
return true;
4486+
}

fs/jfs/jfs_dtree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,6 @@ extern int dtModify(tid_t tid, struct inode *ip, struct component_name * key,
255255
extern int jfs_readdir(struct file *file, struct dir_context *ctx);
256256

257257
extern bool check_dtroot(dtroot_t *p);
258+
259+
extern bool check_dtpage(dtpage_t *p);
258260
#endif /* !_H_JFS_DTREE */

0 commit comments

Comments
 (0)