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>
This commit is contained in:
Bob Copeland 2008-09-06 17:51:53 -04:00
parent 70d9e384aa
commit f068272cb2
4 changed files with 29 additions and 29 deletions

View File

@ -25,11 +25,10 @@ static struct buffer_head *omfs_get_bucket(struct inode *dir,
const char *name, int namelen, int *ofs) const char *name, int namelen, int *ofs)
{ {
int nbuckets = (dir->i_size - OMFS_DIR_START)/8; int nbuckets = (dir->i_size - OMFS_DIR_START)/8;
int block = clus_to_blk(OMFS_SB(dir->i_sb), dir->i_ino);
int bucket = omfs_hash(name, namelen, nbuckets); int bucket = omfs_hash(name, namelen, nbuckets);
*ofs = OMFS_DIR_START + bucket * 8; *ofs = OMFS_DIR_START + bucket * 8;
return sb_bread(dir->i_sb, block); return omfs_bread(dir->i_sb, dir->i_ino);
} }
static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block, 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,
*prev_block = ~0; *prev_block = ~0;
while (block != ~0) { while (block != ~0) {
bh = sb_bread(dir->i_sb, bh = omfs_bread(dir->i_sb, block);
clus_to_blk(OMFS_SB(dir->i_sb), block));
if (!bh) { if (!bh) {
err = -EIO; err = -EIO;
goto err; goto err;
@ -86,11 +84,10 @@ static struct buffer_head *omfs_find_entry(struct inode *dir,
int omfs_make_empty(struct inode *inode, struct super_block *sb) int omfs_make_empty(struct inode *inode, struct super_block *sb)
{ {
struct omfs_sb_info *sbi = OMFS_SB(sb); struct omfs_sb_info *sbi = OMFS_SB(sb);
int block = clus_to_blk(sbi, inode->i_ino);
struct buffer_head *bh; struct buffer_head *bh;
struct omfs_inode *oi; struct omfs_inode *oi;
bh = sb_bread(sb, block); bh = omfs_bread(sb, inode->i_ino);
if (!bh) if (!bh)
return -ENOMEM; return -ENOMEM;
@ -134,7 +131,7 @@ static int omfs_add_link(struct dentry *dentry, struct inode *inode)
brelse(bh); brelse(bh);
/* now set the sibling and parent pointers on the new inode */ /* now set the sibling and parent pointers on the new inode */
bh = sb_bread(dir->i_sb, clus_to_blk(OMFS_SB(dir->i_sb), inode->i_ino)); bh = omfs_bread(dir->i_sb, inode->i_ino);
if (!bh) if (!bh)
goto out; goto out;
@ -190,8 +187,7 @@ static int omfs_delete_entry(struct dentry *dentry)
if (prev != ~0) { if (prev != ~0) {
/* found in middle of list, get list ptr */ /* found in middle of list, get list ptr */
brelse(bh); brelse(bh);
bh = sb_bread(dir->i_sb, bh = omfs_bread(dir->i_sb, prev);
clus_to_blk(OMFS_SB(dir->i_sb), prev));
if (!bh) if (!bh)
goto out; goto out;
@ -224,8 +220,7 @@ static int omfs_dir_is_empty(struct inode *inode)
u64 *ptr; u64 *ptr;
int i; int i;
bh = sb_bread(inode->i_sb, clus_to_blk(OMFS_SB(inode->i_sb), bh = omfs_bread(inode->i_sb, inode->i_ino);
inode->i_ino));
if (!bh) if (!bh)
return 0; return 0;
@ -353,8 +348,7 @@ static int omfs_fill_chain(struct file *filp, void *dirent, filldir_t filldir,
/* follow chain in this bucket */ /* follow chain in this bucket */
while (fsblock != ~0) { while (fsblock != ~0) {
bh = sb_bread(dir->i_sb, clus_to_blk(OMFS_SB(dir->i_sb), bh = omfs_bread(dir->i_sb, fsblock);
fsblock));
if (!bh) if (!bh)
goto out; goto out;
@ -466,7 +460,7 @@ static int omfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
hchain = (filp->f_pos >> 20) - 1; hchain = (filp->f_pos >> 20) - 1;
hindex = filp->f_pos & 0xfffff; hindex = filp->f_pos & 0xfffff;
bh = sb_bread(dir->i_sb, clus_to_blk(OMFS_SB(dir->i_sb), dir->i_ino)); bh = omfs_bread(dir->i_sb, dir->i_ino);
if (!bh) if (!bh)
goto out; goto out;

View File

@ -50,7 +50,7 @@ int omfs_shrink_inode(struct inode *inode)
if (inode->i_size != 0) if (inode->i_size != 0)
goto out; goto out;
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next)); bh = omfs_bread(inode->i_sb, next);
if (!bh) if (!bh)
goto out; goto out;
@ -90,7 +90,7 @@ int omfs_shrink_inode(struct inode *inode)
if (next == ~0) if (next == ~0)
break; break;
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next)); bh = omfs_bread(inode->i_sb, next);
if (!bh) if (!bh)
goto out; goto out;
oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]); 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,
int remain; int remain;
ret = -EIO; ret = -EIO;
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, inode->i_ino)); bh = omfs_bread(inode->i_sb, inode->i_ino);
if (!bh) if (!bh)
goto out; goto out;
@ -265,7 +265,7 @@ static int omfs_get_block(struct inode *inode, sector_t block,
break; break;
brelse(bh); brelse(bh);
bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next)); bh = omfs_bread(inode->i_sb, next);
if (!bh) if (!bh)
goto out; goto out;
oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]); oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);

View File

@ -19,6 +19,15 @@ MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>");
MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux"); MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
struct buffer_head *omfs_bread(struct super_block *sb, sector_t block)
{
struct omfs_sb_info *sbi = OMFS_SB(sb);
if (block >= sbi->s_num_blocks)
return NULL;
return sb_bread(sb, clus_to_blk(sbi, block));
}
struct inode *omfs_new_inode(struct inode *dir, int mode) struct inode *omfs_new_inode(struct inode *dir, int mode)
{ {
struct inode *inode; struct inode *inode;
@ -93,15 +102,13 @@ static int __omfs_write_inode(struct inode *inode, int wait)
struct omfs_inode *oi; struct omfs_inode *oi;
struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb); struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
struct buffer_head *bh, *bh2; struct buffer_head *bh, *bh2;
unsigned int block;
u64 ctime; u64 ctime;
int i; int i;
int ret = -EIO; int ret = -EIO;
int sync_failed = 0; int sync_failed = 0;
/* get current inode since we may have written sibling ptrs etc. */ /* get current inode since we may have written sibling ptrs etc. */
block = clus_to_blk(sbi, inode->i_ino); bh = omfs_bread(inode->i_sb, inode->i_ino);
bh = sb_bread(inode->i_sb, block);
if (!bh) if (!bh)
goto out; goto out;
@ -140,8 +147,7 @@ static int __omfs_write_inode(struct inode *inode, int wait)
/* if mirroring writes, copy to next fsblock */ /* if mirroring writes, copy to next fsblock */
for (i = 1; i < sbi->s_mirrors; i++) { for (i = 1; i < sbi->s_mirrors; i++) {
bh2 = sb_bread(inode->i_sb, block + i * bh2 = omfs_bread(inode->i_sb, inode->i_ino + i);
(sbi->s_blocksize / sbi->s_sys_blocksize));
if (!bh2) if (!bh2)
goto out_brelse; goto out_brelse;
@ -193,7 +199,6 @@ struct inode *omfs_iget(struct super_block *sb, ino_t ino)
struct omfs_sb_info *sbi = OMFS_SB(sb); struct omfs_sb_info *sbi = OMFS_SB(sb);
struct omfs_inode *oi; struct omfs_inode *oi;
struct buffer_head *bh; struct buffer_head *bh;
unsigned int block;
u64 ctime; u64 ctime;
unsigned long nsecs; unsigned long nsecs;
struct inode *inode; struct inode *inode;
@ -204,8 +209,7 @@ struct inode *omfs_iget(struct super_block *sb, ino_t ino)
if (!(inode->i_state & I_NEW)) if (!(inode->i_state & I_NEW))
return inode; return inode;
block = clus_to_blk(sbi, ino); bh = omfs_bread(inode->i_sb, ino);
bh = sb_bread(inode->i_sb, block);
if (!bh) if (!bh)
goto iget_failed; goto iget_failed;
@ -319,6 +323,9 @@ static int omfs_get_imap(struct super_block *sb)
goto nomem; goto nomem;
block = clus_to_blk(sbi, sbi->s_bitmap_ino); block = clus_to_blk(sbi, sbi->s_bitmap_ino);
if (block >= sbi->s_num_blocks)
goto nomem;
ptr = sbi->s_imap; ptr = sbi->s_imap;
for (count = bitmap_size; count > 0; count -= sb->s_blocksize) { for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
bh = sb_bread(sb, block++); bh = sb_bread(sb, block++);
@ -417,7 +424,6 @@ static int omfs_fill_super(struct super_block *sb, void *data, int silent)
struct omfs_root_block *omfs_rb; struct omfs_root_block *omfs_rb;
struct omfs_sb_info *sbi; struct omfs_sb_info *sbi;
struct inode *root; struct inode *root;
sector_t start;
int ret = -EINVAL; int ret = -EINVAL;
save_mount_options(sb, (char *) data); save_mount_options(sb, (char *) data);
@ -486,8 +492,7 @@ static int omfs_fill_super(struct super_block *sb, void *data, int silent)
sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) - sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
get_bitmask_order(sbi->s_sys_blocksize); get_bitmask_order(sbi->s_sys_blocksize);
start = clus_to_blk(sbi, be64_to_cpu(omfs_sb->s_root_block)); bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block));
bh2 = sb_bread(sb, start);
if (!bh2) if (!bh2)
goto out_brelse_bh; goto out_brelse_bh;

View File

@ -58,6 +58,7 @@ extern void omfs_make_empty_table(struct buffer_head *bh, int offset);
extern int omfs_shrink_inode(struct inode *inode); extern int omfs_shrink_inode(struct inode *inode);
/* inode.c */ /* inode.c */
extern struct buffer_head *omfs_bread(struct super_block *sb, sector_t block);
extern struct inode *omfs_iget(struct super_block *sb, ino_t inode); extern struct inode *omfs_iget(struct super_block *sb, ino_t inode);
extern struct inode *omfs_new_inode(struct inode *dir, int mode); extern struct inode *omfs_new_inode(struct inode *dir, int mode);
extern int omfs_reserve_block(struct super_block *sb, sector_t block); extern int omfs_reserve_block(struct super_block *sb, sector_t block);