From: Leon Romanovsky <leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Vlad Tsyrklevich <vlad-NIZqynvkaCU43zv7NVfAiQ@public.gmane.org>
Cc: linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH] infiniband: Fix integer overflows
Date: Wed, 22 Mar 2017 08:39:18 +0200 [thread overview]
Message-ID: <20170322063918.GG2079@mtr-leonro.local> (raw)
In-Reply-To: <1490041006-8092-1-git-send-email-vlad-NIZqynvkaCU43zv7NVfAiQ@public.gmane.org>
[-- Attachment #1: Type: text/plain, Size: 2605 bytes --]
On Mon, Mar 20, 2017 at 04:16:46PM -0400, Vlad Tsyrklevich wrote:
> The 'num_sge' variable is verfied to be smaller than the 'sge_count'
> variable; however, since both are user-controlled it's possible to cause
> an integer overflow for the kmalloc multiply on 32-bit platforms
> (num_sge and sge_count are both defined u32). By crafting an input that
> causes a smaller-than-expected allocation it's possible to write
> controlled data out-of-bounds.
> ---
> drivers/infiniband/core/uverbs_cmd.c | 19 +++++++++++++++----
> 1 file changed, 15 insertions(+), 4 deletions(-)
I would recommend to simply return EINVAL in such case, and don't bother
to complicate the code with wr_align.
>
> diff --git a/drivers/infiniband/core/uverbs_cmd.c b/drivers/infiniband/core/uverbs_cmd.c
> index 7007822..ebb34ae 100644
> --- a/drivers/infiniband/core/uverbs_cmd.c
> +++ b/drivers/infiniband/core/uverbs_cmd.c
> @@ -2542,9 +2542,12 @@ ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
>
> static void *alloc_wr(size_t wr_size, __u32 num_sge)
> {
> - return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
> - num_sge * sizeof (struct ib_sge), GFP_KERNEL);
> -};
> + size_t wr_align = ALIGN(wr_size, sizeof (struct ib_sge));
> + if (num_sge >= (U32_MAX - wr_align) / sizeof (struct ib_sge))
> + return NULL;
> +
> + return kmalloc(wr_align + num_sge * sizeof (struct ib_sge), GFP_KERNEL);
> +}
>
> ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
> struct ib_device *ib_dev,
> @@ -2742,6 +2745,7 @@ static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
> {
> struct ib_uverbs_recv_wr *user_wr;
> struct ib_recv_wr *wr = NULL, *last, *next;
> + size_t wr_align;
> int sg_ind;
> int i;
> int ret;
> @@ -2771,7 +2775,14 @@ static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
> goto err;
> }
>
> - next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
> + wr_align = ALIGN(sizeof *next, sizeof (struct ib_sge));
> + if (user_wr->num_sge >=
> + (U32_MAX - wr_align) / sizeof (struct ib_sge)) {
> + ret = -EINVAL;
> + goto err;
> + }
> +
> + next = kmalloc(wr_align +
> user_wr->num_sge * sizeof (struct ib_sge),
> GFP_KERNEL);
> if (!next) {
> --
> 2.7.0
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
prev parent reply other threads:[~2017-03-22 6:39 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-20 20:16 [PATCH] infiniband: Fix integer overflows Vlad Tsyrklevich
[not found] ` <1490041006-8092-1-git-send-email-vlad-NIZqynvkaCU43zv7NVfAiQ@public.gmane.org>
2017-03-22 6:39 ` Leon Romanovsky [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170322063918.GG2079@mtr-leonro.local \
--to=leon-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
--cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=vlad-NIZqynvkaCU43zv7NVfAiQ@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox