Skip to content

Commit 6674eca

Browse files
Russell KingAl Viro
authored andcommitted
fs/adfs: bigdir: extract directory validation
Extract the directory validation from the directory reading function as we will want to re-use this code. Signed-off-by: Russell King <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 0db35a0 commit 6674eca

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

fs/adfs/dir_fplus.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ static unsigned int adfs_fplus_offset(const struct adfs_bigdirheader *h,
1616
pos * sizeof(struct adfs_bigdirentry);
1717
}
1818

19+
static int adfs_fplus_validate_header(const struct adfs_bigdirheader *h)
20+
{
21+
unsigned int size = le32_to_cpu(h->bigdirsize);
22+
23+
if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
24+
h->bigdirversion[2] != 0 ||
25+
h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME) ||
26+
size & 2047)
27+
return -EIO;
28+
29+
return 0;
30+
}
31+
32+
static int adfs_fplus_validate_tail(const struct adfs_bigdirheader *h,
33+
const struct adfs_bigdirtail *t)
34+
{
35+
if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
36+
t->bigdirendmasseq != h->startmasseq ||
37+
t->reserved[0] != 0 || t->reserved[1] != 0)
38+
return -EIO;
39+
40+
return 0;
41+
}
42+
1943
static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
2044
unsigned int size, struct adfs_dir *dir)
2145
{
@@ -30,20 +54,18 @@ static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
3054
return ret;
3155

3256
dir->bighead = h = (void *)dir->bhs[0]->b_data;
57+
if (adfs_fplus_validate_header(h)) {
58+
adfs_error(sb, "dir %06x has malformed header", indaddr);
59+
goto out;
60+
}
61+
3362
dirsize = le32_to_cpu(h->bigdirsize);
3463
if (dirsize != size) {
3564
adfs_msg(sb, KERN_WARNING,
3665
"dir %06x header size %X does not match directory size %X",
3766
indaddr, dirsize, size);
3867
}
3968

40-
if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
41-
h->bigdirversion[2] != 0 || size & 2047 ||
42-
h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME)) {
43-
adfs_error(sb, "dir %06x has malformed header", indaddr);
44-
goto out;
45-
}
46-
4769
/* Read remaining buffers */
4870
ret = adfs_dir_read_buffers(sb, indaddr, dirsize, dir);
4971
if (ret)
@@ -52,9 +74,8 @@ static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
5274
dir->bigtail = t = (struct adfs_bigdirtail *)
5375
(dir->bhs[dir->nr_buffers - 1]->b_data + (sb->s_blocksize - 8));
5476

55-
if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
56-
t->bigdirendmasseq != h->startmasseq ||
57-
t->reserved[0] != 0 || t->reserved[1] != 0) {
77+
ret = adfs_fplus_validate_tail(h, t);
78+
if (ret) {
5879
adfs_error(sb, "dir %06x has malformed tail", indaddr);
5980
goto out;
6081
}

0 commit comments

Comments
 (0)