Skip to content

Commit 016e8d4

Browse files
jwboyerAl Viro
authored andcommitted
fs/minix: Verify bitmap block counts before mounting
Newer versions of MINIX can create filesystems that allocate an extra bitmap block. Mounting of this succeeds, but doing a statfs call will result in an oops in count_free because of a negative number being used for the bh index. Avoid this by verifying the number of allocated blocks at mount time, erroring out if there are not enough and make statfs ignore the extras if there are too many. This fixes https://bugzilla.kernel.org/show_bug.cgi?id=18792 Signed-off-by: Josh Boyer <jwboyer@redhat.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent d633180 commit 016e8d4

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

‎fs/minix/bitmap.c‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ static const int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
2020

2121
static DEFINE_SPINLOCK(bitmap_lock);
2222

23-
static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
23+
static unsigned long count_free(struct buffer_head *map[], unsigned blocksize, __u32 numbits)
2424
{
2525
unsigned i, j, sum = 0;
2626
struct buffer_head *bh;
27+
unsigned numblocks = minix_blocks_needed(numbits, blocksize);
2728

2829
for (i=0; i<numblocks-1; i++) {
2930
if (!(bh=map[i]))
@@ -105,10 +106,12 @@ int minix_new_block(struct inode * inode)
105106
return 0;
106107
}
107108

108-
unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
109+
unsigned long minix_count_free_blocks(struct super_block *sb)
109110
{
110-
return (count_free(sbi->s_zmap, sbi->s_zmap_blocks,
111-
sbi->s_nzones - sbi->s_firstdatazone + 1)
111+
struct minix_sb_info *sbi = minix_sb(sb);
112+
u32 bits = sbi->s_nzones - (sbi->s_firstdatazone + 1);
113+
114+
return (count_free(sbi->s_zmap, sb->s_blocksize, bits)
112115
<< sbi->s_log_zone_size);
113116
}
114117

@@ -273,7 +276,10 @@ struct inode *minix_new_inode(const struct inode *dir, int mode, int *error)
273276
return inode;
274277
}
275278

276-
unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
279+
unsigned long minix_count_free_inodes(struct super_block *sb)
277280
{
278-
return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);
281+
struct minix_sb_info *sbi = minix_sb(sb);
282+
u32 bits = sbi->s_ninodes + 1;
283+
284+
return count_free(sbi->s_imap, sb->s_blocksize, bits);
279285
}

‎fs/minix/inode.c‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,27 @@ static int minix_fill_super(struct super_block *s, void *data, int silent)
279279
else if (sbi->s_mount_state & MINIX_ERROR_FS)
280280
printk("MINIX-fs: mounting file system with errors, "
281281
"running fsck is recommended\n");
282+
283+
/* Apparently minix can create filesystems that allocate more blocks for
284+
* the bitmaps than needed. We simply ignore that, but verify it didn't
285+
* create one with not enough blocks and bail out if so.
286+
*/
287+
block = minix_blocks_needed(sbi->s_ninodes, s->s_blocksize);
288+
if (sbi->s_imap_blocks < block) {
289+
printk("MINIX-fs: file system does not have enough "
290+
"imap blocks allocated. Refusing to mount\n");
291+
goto out_iput;
292+
}
293+
294+
block = minix_blocks_needed(
295+
(sbi->s_nzones - (sbi->s_firstdatazone + 1)),
296+
s->s_blocksize);
297+
if (sbi->s_zmap_blocks < block) {
298+
printk("MINIX-fs: file system does not have enough "
299+
"zmap blocks allocated. Refusing to mount.\n");
300+
goto out_iput;
301+
}
302+
282303
return 0;
283304

284305
out_iput:
@@ -339,10 +360,10 @@ static int minix_statfs(struct dentry *dentry, struct kstatfs *buf)
339360
buf->f_type = sb->s_magic;
340361
buf->f_bsize = sb->s_blocksize;
341362
buf->f_blocks = (sbi->s_nzones - sbi->s_firstdatazone) << sbi->s_log_zone_size;
342-
buf->f_bfree = minix_count_free_blocks(sbi);
363+
buf->f_bfree = minix_count_free_blocks(sb);
343364
buf->f_bavail = buf->f_bfree;
344365
buf->f_files = sbi->s_ninodes;
345-
buf->f_ffree = minix_count_free_inodes(sbi);
366+
buf->f_ffree = minix_count_free_inodes(sb);
346367
buf->f_namelen = sbi->s_namelen;
347368
buf->f_fsid.val[0] = (u32)id;
348369
buf->f_fsid.val[1] = (u32)(id >> 32);

‎fs/minix/minix.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ extern struct minix_inode * minix_V1_raw_inode(struct super_block *, ino_t, stru
4848
extern struct minix2_inode * minix_V2_raw_inode(struct super_block *, ino_t, struct buffer_head **);
4949
extern struct inode * minix_new_inode(const struct inode *, int, int *);
5050
extern void minix_free_inode(struct inode * inode);
51-
extern unsigned long minix_count_free_inodes(struct minix_sb_info *sbi);
51+
extern unsigned long minix_count_free_inodes(struct super_block *sb);
5252
extern int minix_new_block(struct inode * inode);
5353
extern void minix_free_block(struct inode *inode, unsigned long block);
54-
extern unsigned long minix_count_free_blocks(struct minix_sb_info *sbi);
54+
extern unsigned long minix_count_free_blocks(struct super_block *sb);
5555
extern int minix_getattr(struct vfsmount *, struct dentry *, struct kstat *);
5656
extern int minix_prepare_chunk(struct page *page, loff_t pos, unsigned len);
5757

@@ -88,6 +88,11 @@ static inline struct minix_inode_info *minix_i(struct inode *inode)
8888
return list_entry(inode, struct minix_inode_info, vfs_inode);
8989
}
9090

91+
static inline unsigned minix_blocks_needed(unsigned bits, unsigned blocksize)
92+
{
93+
return DIV_ROUND_UP(bits, blocksize * 8);
94+
}
95+
9196
#if defined(CONFIG_MINIX_FS_NATIVE_ENDIAN) && \
9297
defined(CONFIG_MINIX_FS_BIG_ENDIAN_16BIT_INDEXED)
9398

0 commit comments

Comments
 (0)