Skip to content

Commit f068272

Browse files
committed
omfs: check bounds on block numbers before passing to sb_bread
In case of filesystem corruption, passing unchecked block numbers into sb_bread can result in an infinite loop in __getblk(). Introduce a wrapper function omfs_sbread() to check the block numbers and to also perform the clus_to_blk() scaling. Signed-off-by: Bob Copeland <me@bobcopeland.com>
1 parent 70d9e38 commit f068272

4 files changed

Lines changed: 29 additions & 29 deletions

File tree

‎fs/omfs/dir.c‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ static struct buffer_head *omfs_get_bucket(struct inode *dir,
2525
const char *name, int namelen, int *ofs)
2626
{
2727
int nbuckets = (dir->i_size - OMFS_DIR_START)/8;
28-
int block = clus_to_blk(OMFS_SB(dir->i_sb), dir->i_ino);
2928
int bucket = omfs_hash(name, namelen, nbuckets);
3029

3130
*ofs = OMFS_DIR_START + bucket * 8;
32-
return sb_bread(dir->i_sb, block);
31+
return omfs_bread(dir->i_sb, dir->i_ino);
3332
}
3433

3534
static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
@@ -42,8 +41,7 @@ static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
4241
*prev_block = ~0;
4342

4443
while (block != ~0) {
45-
bh = sb_bread(dir->i_sb,
46-
clus_to_blk(OMFS_SB(dir->i_sb), block));
44+
bh = omfs_bread(dir->i_sb, block);
4745
if (!bh) {
4846
err = -EIO;
4947
goto err;
@@ -86,11 +84,10 @@ static struct buffer_head *omfs_find_entry(struct inode *dir,
8684
int omfs_make_empty(struct inode *inode, struct super_block *sb)
8785
{
8886
struct omfs_sb_info *sbi = OMFS_SB(sb);
89-
int block = clus_to_blk(sbi, inode->i_ino);
9087
struct buffer_head *bh;
9188
struct omfs_inode *oi;
9289

93-
bh = sb_bread(sb, block);
90+
bh = omfs_bread(sb, inode->i_ino);
9491
if (!bh)
9592
return -ENOMEM;
9693

@@ -134,7 +131,7 @@ static int omfs_add_link(struct dentry *dentry, struct inode *inode)
134131
brelse(bh);
135132

136133
/* now set the sibling and parent pointers on the new inode */
137-
bh = sb_bread(dir->i_sb, clus_to_blk(OMFS_SB(dir->i_sb), inode->i_ino));
134+
bh = omfs_bread(dir->i_sb, inode->i_ino);
138135
if (!bh)
139136
goto out;
140137

@@ -190,8 +187,7 @@ static int omfs_delete_entry(struct dentry *dentry)
190187
if (prev != ~0) {
191188
/* found in middle of list, get list ptr */
192189
brelse(bh);
193-
bh = sb_bread(dir->i_sb,
194-
clus_to_blk(OMFS_SB(dir->i_sb), prev));
190+
bh = omfs_bread(dir->i_sb, prev);
195191
if (!bh)
196192
goto out;
197193

@@ -224,8 +220,7 @@ static int omfs_dir_is_empty(struct inode *inode)
224220
u64 *ptr;
225221
int i;
226222

227-
bh = sb_bread(inode->i_sb, clus_to_blk(OMFS_SB(inode->i_sb),
228-
inode->i_ino));
223+
bh = omfs_bread(inode->i_sb, inode->i_ino);
229224

230225
if (!bh)
231226
return 0;
@@ -353,8 +348,7 @@ static int omfs_fill_chain(struct file *filp, void *dirent, filldir_t filldir,
353348

354349
/* follow chain in this bucket */
355350
while (fsblock != ~0) {
356-
bh = sb_bread(dir->i_sb, clus_to_blk(OMFS_SB(dir->i_sb),
357-
fsblock));
351+
bh = omfs_bread(dir->i_sb, fsblock);
358352
if (!bh)
359353
goto out;
360354

@@ -466,7 +460,7 @@ static int omfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
466460
hchain = (filp->f_pos >> 20) - 1;
467461
hindex = filp->f_pos & 0xfffff;
468462

469-
bh = sb_bread(dir->i_sb, clus_to_blk(OMFS_SB(dir->i_sb), dir->i_ino));
463+
bh = omfs_bread(dir->i_sb, dir->i_ino);
470464
if (!bh)
471465
goto out;
472466

‎fs/omfs/file.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int omfs_shrink_inode(struct inode *inode)
5050
if (inode->i_size != 0)
5151
goto out;
5252

53-
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next));
53+
bh = omfs_bread(inode->i_sb, next);
5454
if (!bh)
5555
goto out;
5656

@@ -90,7 +90,7 @@ int omfs_shrink_inode(struct inode *inode)
9090
if (next == ~0)
9191
break;
9292

93-
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next));
93+
bh = omfs_bread(inode->i_sb, next);
9494
if (!bh)
9595
goto out;
9696
oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
@@ -232,7 +232,7 @@ static int omfs_get_block(struct inode *inode, sector_t block,
232232
int remain;
233233

234234
ret = -EIO;
235-
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, inode->i_ino));
235+
bh = omfs_bread(inode->i_sb, inode->i_ino);
236236
if (!bh)
237237
goto out;
238238

@@ -265,7 +265,7 @@ static int omfs_get_block(struct inode *inode, sector_t block,
265265
break;
266266

267267
brelse(bh);
268-
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next));
268+
bh = omfs_bread(inode->i_sb, next);
269269
if (!bh)
270270
goto out;
271271
oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);

‎fs/omfs/inode.c‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>");
1919
MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux");
2020
MODULE_LICENSE("GPL");
2121

22+
struct buffer_head *omfs_bread(struct super_block *sb, sector_t block)
23+
{
24+
struct omfs_sb_info *sbi = OMFS_SB(sb);
25+
if (block >= sbi->s_num_blocks)
26+
return NULL;
27+
28+
return sb_bread(sb, clus_to_blk(sbi, block));
29+
}
30+
2231
struct inode *omfs_new_inode(struct inode *dir, int mode)
2332
{
2433
struct inode *inode;
@@ -93,15 +102,13 @@ static int __omfs_write_inode(struct inode *inode, int wait)
93102
struct omfs_inode *oi;
94103
struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
95104
struct buffer_head *bh, *bh2;
96-
unsigned int block;
97105
u64 ctime;
98106
int i;
99107
int ret = -EIO;
100108
int sync_failed = 0;
101109

102110
/* get current inode since we may have written sibling ptrs etc. */
103-
block = clus_to_blk(sbi, inode->i_ino);
104-
bh = sb_bread(inode->i_sb, block);
111+
bh = omfs_bread(inode->i_sb, inode->i_ino);
105112
if (!bh)
106113
goto out;
107114

@@ -140,8 +147,7 @@ static int __omfs_write_inode(struct inode *inode, int wait)
140147

141148
/* if mirroring writes, copy to next fsblock */
142149
for (i = 1; i < sbi->s_mirrors; i++) {
143-
bh2 = sb_bread(inode->i_sb, block + i *
144-
(sbi->s_blocksize / sbi->s_sys_blocksize));
150+
bh2 = omfs_bread(inode->i_sb, inode->i_ino + i);
145151
if (!bh2)
146152
goto out_brelse;
147153

@@ -193,7 +199,6 @@ struct inode *omfs_iget(struct super_block *sb, ino_t ino)
193199
struct omfs_sb_info *sbi = OMFS_SB(sb);
194200
struct omfs_inode *oi;
195201
struct buffer_head *bh;
196-
unsigned int block;
197202
u64 ctime;
198203
unsigned long nsecs;
199204
struct inode *inode;
@@ -204,8 +209,7 @@ struct inode *omfs_iget(struct super_block *sb, ino_t ino)
204209
if (!(inode->i_state & I_NEW))
205210
return inode;
206211

207-
block = clus_to_blk(sbi, ino);
208-
bh = sb_bread(inode->i_sb, block);
212+
bh = omfs_bread(inode->i_sb, ino);
209213
if (!bh)
210214
goto iget_failed;
211215

@@ -319,6 +323,9 @@ static int omfs_get_imap(struct super_block *sb)
319323
goto nomem;
320324

321325
block = clus_to_blk(sbi, sbi->s_bitmap_ino);
326+
if (block >= sbi->s_num_blocks)
327+
goto nomem;
328+
322329
ptr = sbi->s_imap;
323330
for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
324331
bh = sb_bread(sb, block++);
@@ -417,7 +424,6 @@ static int omfs_fill_super(struct super_block *sb, void *data, int silent)
417424
struct omfs_root_block *omfs_rb;
418425
struct omfs_sb_info *sbi;
419426
struct inode *root;
420-
sector_t start;
421427
int ret = -EINVAL;
422428

423429
save_mount_options(sb, (char *) data);
@@ -486,8 +492,7 @@ static int omfs_fill_super(struct super_block *sb, void *data, int silent)
486492
sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
487493
get_bitmask_order(sbi->s_sys_blocksize);
488494

489-
start = clus_to_blk(sbi, be64_to_cpu(omfs_sb->s_root_block));
490-
bh2 = sb_bread(sb, start);
495+
bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block));
491496
if (!bh2)
492497
goto out_brelse_bh;
493498

‎fs/omfs/omfs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern void omfs_make_empty_table(struct buffer_head *bh, int offset);
5858
extern int omfs_shrink_inode(struct inode *inode);
5959

6060
/* inode.c */
61+
extern struct buffer_head *omfs_bread(struct super_block *sb, sector_t block);
6162
extern struct inode *omfs_iget(struct super_block *sb, ino_t inode);
6263
extern struct inode *omfs_new_inode(struct inode *dir, int mode);
6364
extern int omfs_reserve_block(struct super_block *sb, sector_t block);

0 commit comments

Comments
 (0)