* [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() @ 2012-02-20 22:41 Haogang Chen 2012-02-20 23:47 ` Eric Sandeen 2012-05-28 18:24 ` Ted Ts'o 0 siblings, 2 replies; 6+ messages in thread From: Haogang Chen @ 2012-02-20 22:41 UTC (permalink / raw) To: Theodore Tso, Andreas Dilger, linux-ext4; +Cc: linux-kernel, haogangchen In alloc_flex_gd(), when flexbg_size is large, kmalloc size would overflow and flex_gd->groups would point to a buffer smaller than expected, causing OOB accesses when it is used. Note that in ext4_resize_fs(), flexbg_size is calculated using sbi->s_log_groups_per_flex, which is read from the disk and only bounded to [1, 31]. The patch returns NULL for too large flexbg_size. Signed-off-by: Haogang Chen <haogangchen@gmail.com> --- fs/ext4/resize.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-) diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c index f9d948f..8601f4b 100644 --- a/fs/ext4/resize.c +++ b/fs/ext4/resize.c @@ -161,6 +161,8 @@ static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) if (flex_gd == NULL) goto out3; + if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_flex_group_data)) + goto out2; flex_gd->count = flexbg_size; flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) * -- 1.7.5.4 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() 2012-02-20 22:41 [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() Haogang Chen @ 2012-02-20 23:47 ` Eric Sandeen 2012-02-21 13:55 ` Xi Wang 2012-05-28 18:24 ` Ted Ts'o 1 sibling, 1 reply; 6+ messages in thread From: Eric Sandeen @ 2012-02-20 23:47 UTC (permalink / raw) To: Haogang Chen Cc: Theodore Tso, Andreas Dilger, linux-ext4, linux-kernel, Yongqiang Yang On 2/20/12 4:41 PM, Haogang Chen wrote: > In alloc_flex_gd(), when flexbg_size is large, kmalloc size would > overflow and flex_gd->groups would point to a buffer smaller than > expected, causing OOB accesses when it is used. > > Note that in ext4_resize_fs(), flexbg_size is calculated using > sbi->s_log_groups_per_flex, which is read from the disk and only bounded > to [1, 31]. The patch returns NULL for too large flexbg_size Hm this raises a few questions I think. On the one hand, making sure the kmalloc arg doesn't overflow here is certainly a good thing and probably the right thing to do in the short term. So I guess: Reviewed-by: Eric Sandeen <sandeen@redhat.com> for that, to close the hole. But the types are a mess; alloc_flex_gd() takes an unsigned long; it's passed an int, and assigns to flex_gd->count, an ext4_group_t (which is an unsigned int). They should probably all be ext4_group_t for consistency. But that's not the worst of it... Doesn't this also mean that a valid s_log_groups_per_flex (i.e. 31) will fail in this resize code? That would be an unexpected outcome. 2^31 groups per flex is a little crazy, but still technically valid according to the limits in the code. So really, trying to allocate an array of all possible groups-per-flex in the resize code is probably a really bad idea to start with, and the resize code has got serious problems if kmalloc(UINT_MAX-1) is expected to work... -Eric > Signed-off-by: Haogang Chen <haogangchen@gmail.com> > --- > fs/ext4/resize.c | 2 ++ > 1 files changed, 2 insertions(+), 0 deletions(-) > > diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c > index f9d948f..8601f4b 100644 > --- a/fs/ext4/resize.c > +++ b/fs/ext4/resize.c > @@ -161,6 +161,8 @@ static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) > if (flex_gd == NULL) > goto out3; > > + if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_flex_group_data)) > + goto out2; > flex_gd->count = flexbg_size; > > flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) * ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() 2012-02-20 23:47 ` Eric Sandeen @ 2012-02-21 13:55 ` Xi Wang 2012-02-21 16:36 ` Eric Sandeen 0 siblings, 1 reply; 6+ messages in thread From: Xi Wang @ 2012-02-21 13:55 UTC (permalink / raw) To: Eric Sandeen Cc: Haogang Chen, Theodore Tso, Andreas Dilger, linux-ext4, linux-kernel, Yongqiang Yang, Andrew Morton On Feb 20, 2012, at 6:47 PM, Eric Sandeen wrote: > Hm this raises a few questions I think. > > On the one hand, making sure the kmalloc arg doesn't overflow here is > certainly a good thing and probably the right thing to do in the short term. > > So I guess: > > Reviewed-by: Eric Sandeen <sandeen@redhat.com> > > for that, to close the hole. Another possibility is to wait for knalloc/kmalloc_array in the -mm tree, which is basically the non-zeroing version of kcalloc that performs overflow checking. > Doesn't this also mean that a valid s_log_groups_per_flex (i.e. 31) > will fail in this resize code? That would be an unexpected outcome. > 2^31 groups per flex is a little crazy, but still technically valid > according to the limits in the code. Or we could limit s_log_groups_per_flex/groups_per_flex to a reasonable upper bound in ext4_fill_flex_info(), right? - xi ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() 2012-02-21 13:55 ` Xi Wang @ 2012-02-21 16:36 ` Eric Sandeen 2012-02-21 17:19 ` Andreas Dilger 0 siblings, 1 reply; 6+ messages in thread From: Eric Sandeen @ 2012-02-21 16:36 UTC (permalink / raw) To: Xi Wang Cc: Haogang Chen, Theodore Tso, Andreas Dilger, linux-ext4, linux-kernel, Yongqiang Yang, Andrew Morton On 02/21/2012 07:55 AM, Xi Wang wrote: > On Feb 20, 2012, at 6:47 PM, Eric Sandeen wrote: >> Hm this raises a few questions I think. >> >> On the one hand, making sure the kmalloc arg doesn't overflow here is >> certainly a good thing and probably the right thing to do in the short term. >> >> So I guess: >> >> Reviewed-by: Eric Sandeen <sandeen@redhat.com> >> >> for that, to close the hole. > > Another possibility is to wait for knalloc/kmalloc_array in the -mm > tree, which is basically the non-zeroing version of kcalloc that > performs overflow checking. > >> Doesn't this also mean that a valid s_log_groups_per_flex (i.e. 31) >> will fail in this resize code? That would be an unexpected outcome. >> 2^31 groups per flex is a little crazy, but still technically valid >> according to the limits in the code. > > Or we could limit s_log_groups_per_flex/groups_per_flex to a > reasonable upper bound in ext4_fill_flex_info(), right? Depends on the "flex_bg" design intent, I guess. I don't know if the 2^31 was an intended design limit, or just a mathematical limit that based on container sizes etc... I'd have to look at the resize code more carefully but I can't imagine that it's imperative to allocate this stuff all at once. -Eric > - xi > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() 2012-02-21 16:36 ` Eric Sandeen @ 2012-02-21 17:19 ` Andreas Dilger 0 siblings, 0 replies; 6+ messages in thread From: Andreas Dilger @ 2012-02-21 17:19 UTC (permalink / raw) To: Eric Sandeen Cc: Xi Wang, Haogang Chen, Theodore Tso, linux-ext4, linux-kernel, Yongqiang Yang, Andrew Morton On 2012-02-21, at 9:36 AM, Eric Sandeen wrote: > On 02/21/2012 07:55 AM, Xi Wang wrote: >> On Feb 20, 2012, at 6:47 PM, Eric Sandeen wrote: >>> Hm this raises a few questions I think. >>> >>> On the one hand, making sure the kmalloc arg doesn't overflow here is >>> certainly a good thing and probably the right thing to do in the short term. >>> >>> So I guess: >>> >>> Reviewed-by: Eric Sandeen <sandeen@redhat.com> >>> >>> for that, to close the hole. >> >> Another possibility is to wait for knalloc/kmalloc_array in the -mm >> tree, which is basically the non-zeroing version of kcalloc that >> performs overflow checking. >> >>> Doesn't this also mean that a valid s_log_groups_per_flex (i.e. 31) >>> will fail in this resize code? That would be an unexpected outcome. >>> 2^31 groups per flex is a little crazy, but still technically valid >>> according to the limits in the code. >> >> Or we could limit s_log_groups_per_flex/groups_per_flex to a >> reasonable upper bound in ext4_fill_flex_info(), right? > > Depends on the "flex_bg" design intent, I guess. > > I don't know if the 2^31 was an intended design limit, or just a > mathematical limit that based on container sizes etc... > > I'd have to look at the resize code more carefully but I can't imagine > that it's imperative to allocate this stuff all at once. We previously tried to use a large flex_bg size to put all metadata into a single group so it could easily be allocated on a separate SSD device, but that didn't work very well. Once the number of bitmaps in group 0 is more than the number of free blocks in that group (below 16k groups, due to group descriptors) then they need to overflow into group 1 and collide with the group descriptors there. Then mke2fs chokes, AFAIR. It may be different with bigalloc, since the number of blocks in a group can be very large, I haven't tried that. In any case, I don't think anyone expects vmalloc(2^32 * struct size) to work, but I wouldn't sweat fixing this until there is some real reason to do so. Cheers, Andreas ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() 2012-02-20 22:41 [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() Haogang Chen 2012-02-20 23:47 ` Eric Sandeen @ 2012-05-28 18:24 ` Ted Ts'o 1 sibling, 0 replies; 6+ messages in thread From: Ted Ts'o @ 2012-05-28 18:24 UTC (permalink / raw) To: Haogang Chen; +Cc: Andreas Dilger, linux-ext4, linux-kernel On Mon, Feb 20, 2012 at 05:41:24PM -0500, Haogang Chen wrote: > In alloc_flex_gd(), when flexbg_size is large, kmalloc size would > overflow and flex_gd->groups would point to a buffer smaller than > expected, causing OOB accesses when it is used. > > Note that in ext4_resize_fs(), flexbg_size is calculated using > sbi->s_log_groups_per_flex, which is read from the disk and only bounded > to [1, 31]. The patch returns NULL for too large flexbg_size. > > Signed-off-by: Haogang Chen <haogangchen@gmail.com> Thanks, applied. Apologies for missing this during the last cycle. - Ted ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-05-28 18:24 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2012-02-20 22:41 [PATCH] FS: ext4: fix integer overflow in alloc_flex_gd() Haogang Chen 2012-02-20 23:47 ` Eric Sandeen 2012-02-21 13:55 ` Xi Wang 2012-02-21 16:36 ` Eric Sandeen 2012-02-21 17:19 ` Andreas Dilger 2012-05-28 18:24 ` Ted Ts'o
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).