Skip to content

Commit d79288b

Browse files
Russell KingAl Viro
authored andcommitted
fs/adfs: bigdir: calculate and validate directory checkbyte
When reading a big directory, calculate the validate the directory checkbyte to ensure that the directory contents are valid. Signed-off-by: Russell King <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent aa3d4e0 commit d79288b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

fs/adfs/dir_fplus.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,39 @@ static int adfs_fplus_validate_tail(const struct adfs_bigdirheader *h,
6767
return 0;
6868
}
6969

70+
static u8 adfs_fplus_checkbyte(struct adfs_dir *dir)
71+
{
72+
struct adfs_bigdirheader *h = dir->bighead;
73+
struct adfs_bigdirtail *t = dir->bigtail;
74+
unsigned int end, bs, bi, i;
75+
__le32 *bp;
76+
u32 dircheck;
77+
78+
end = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries)) +
79+
le32_to_cpu(h->bigdirnamesize);
80+
81+
/* Accumulate the contents of the header, entries and names */
82+
for (dircheck = 0, bi = 0; end; bi++) {
83+
bp = (void *)dir->bhs[bi]->b_data;
84+
bs = dir->bhs[bi]->b_size;
85+
if (bs > end)
86+
bs = end;
87+
88+
for (i = 0; i < bs; i += sizeof(u32))
89+
dircheck = ror32(dircheck, 13) ^ le32_to_cpup(bp++);
90+
91+
end -= bs;
92+
}
93+
94+
/* Accumulate the contents of the tail except for the check byte */
95+
dircheck = ror32(dircheck, 13) ^ le32_to_cpu(t->bigdirendname);
96+
dircheck = ror32(dircheck, 13) ^ t->bigdirendmasseq;
97+
dircheck = ror32(dircheck, 13) ^ t->reserved[0];
98+
dircheck = ror32(dircheck, 13) ^ t->reserved[1];
99+
100+
return dircheck ^ dircheck >> 8 ^ dircheck >> 16 ^ dircheck >> 24;
101+
}
102+
70103
static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
71104
unsigned int size, struct adfs_dir *dir)
72105
{
@@ -107,6 +140,11 @@ static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
107140
goto out;
108141
}
109142

143+
if (adfs_fplus_checkbyte(dir) != t->bigdircheckbyte) {
144+
adfs_error(sb, "dir %06x checkbyte mismatch\n", indaddr);
145+
goto out;
146+
}
147+
110148
dir->parent_id = le32_to_cpu(h->bigdirparent);
111149
return 0;
112150

0 commit comments

Comments
 (0)