All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: Bug report: KFIFO kfifo_init() may introduce buffer overflow
@ 2019-07-22 11:47 Dan Carpenter
  2019-07-22 11:50 ` Dan Carpenter
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Dan Carpenter @ 2019-07-22 11:47 UTC (permalink / raw)
  To: target-devel

On Mon, Jul 22, 2019 at 12:23:56PM +0800, laokz wrote:
> Hello,
> 
> A couple of weeks ago, I reported the respect author/maintainers. Haven't
> got reply yet, so I come here. 
> 
> The following is based on kernel 5.2-rc6.
> 
> include/linux/kfifo.h::kfifo_init() initialize a fifo using a preallocated
> buffer. It requests the buffer size should be power_of_two, if not so, the
> actual worker __kfifo_init() will round UP it to the next power of two. For
> it just records the new size in fifo's internal parameters, does not touch
> the real buffer, obviously this may introduce buffer overflow. 
> 
> In the source tree, I found an instance.
> 
> In drivers/scsi/ibmvscsi_tgt/libsrp.c::srp_iu_pool_alloc(), it calls
> kcalloc() and kfifo_init() with buffer size=max*sizeof(void
> *)=INITIAL_SRP_LIMIT*sizeof(void *), most likely be
> 800*8d00. That is NOT power of 2, KFIFO will treat it as 8192 big! Bad.
> 
> Here is its only call-chain:
>   #define INITIAL_SRP_LIMIT 800                       /* ibmvscsi_tgt.c */
>   ibmvscsis_probe()::vscsi->request_limit=INITIAL_SRP_LIMIT
>     -> srp_target_alloc(,vscsi->request_limit,)       /* libsrp.c */
>       ->srp_iu_pool_alloc(,nr,)                /* nr=vscsi->request_limit */
>         ->kfifo_init(,max*sizeof(void *),)     /* max=nr */
> 
> I know before kernel 3.9 __kfifo_init() algorithm was rounddown. I don't
> know why changed to roundup, but it is safe and more robust to rounddown
> instead of roundup.
> 
> If i'm wrong, please let me know also. Thanks.

It looks like you're right.  Probably the fix is to:
1) Change INITIAL_SRP_LIMIT to 8192
2) Change kfifo_init() to round down like you say
3) Add a WARN_ONCE() in kfifo_alloc and kfifo_init() if the size isn't
   a power of two.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-09-01 17:38 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-07-22 11:47 Bug report: KFIFO kfifo_init() may introduce buffer overflow Dan Carpenter
2019-07-22 11:50 ` Dan Carpenter
2019-08-02  7:30 ` Greg KH
2019-08-29 17:21 ` Kees Cook
2019-08-29 18:48 ` Linus Torvalds
2019-08-29 19:32 ` Linus Torvalds
2019-08-29 19:40 ` Stefani Seibold
2019-08-29 19:42 ` Kees Cook
2019-08-29 21:51 ` Will Deacon
2019-09-01 17:38 ` Linus Torvalds

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.