bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bui Quang Minh <minhquangbui99@gmail.com>
To: Daniel Borkmann <daniel@iogearbox.net>,
	Lorenz Bauer <lmb@cloudflare.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	"David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	hawk@kernel.org, John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>, Martin Lau <kafai@fb.com>,
	Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
	kpsingh@kernel.org, Jakub Sitnicki <jakub@cloudflare.com>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] bpf: Fix integer overflow in argument calculation for bpf_map_area_alloc
Date: Mon, 17 May 2021 22:10:03 +0700	[thread overview]
Message-ID: <728b238e-a481-eb50-98e9-b0f430ab01e7@gmail.com> (raw)
In-Reply-To: <f4d20d92-2370-a8d3-d56c-408819a5f7f4@iogearbox.net>

On 1/28/21 7:41 AM, Daniel Borkmann wrote:
> On 1/27/21 5:23 AM, Bui Quang Minh wrote:
>> On Tue, Jan 26, 2021 at 09:36:57AM +0000, Lorenz Bauer wrote:
>>> On Tue, 26 Jan 2021 at 08:26, Bui Quang Minh 
>>> <minhquangbui99@gmail.com> wrote:
>>>>
>>>> In 32-bit architecture, the result of sizeof() is a 32-bit integer so
>>>> the expression becomes the multiplication between 2 32-bit integer 
>>>> which
>>>> can potentially leads to integer overflow. As a result,
>>>> bpf_map_area_alloc() allocates less memory than needed.
>>>>
>>>> Fix this by casting 1 operand to u64.
>>>
>>> Some quick thoughts:
>>> * Should this have a Fixes tag?
>>
>> Ok, I will add Fixes tag in later version patch.
>>
>>> * Seems like there are quite a few similar calls scattered around
>>> (cpumap, etc.). Did you audit these as well?
>>
> [...]
>> In cpumap,
>>
>>     static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
>>     {
>>         cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
>>                            sizeof(struct bpf_cpu_map_entry *),
>>                            cmap->map.numa_node);
>>     }
>>
>> I think this is safe because max_entries is not permitted to be larger 
>> than NR_CPUS.
> 
> Yes.
> 
>> In stackmap, there is a place that I'm not very sure about
>>
>>     static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
>>     {
>>         u32 elem_size = sizeof(struct stack_map_bucket) + 
>> smap->map.value_size;
>>         smap->elems = bpf_map_area_alloc(elem_size * 
>> smap->map.max_entries,
>>                          smap->map.numa_node);
>>     }
>>
>> This is called after another bpf_map_area_alloc in stack_map_alloc(). 
>> In the first
>> bpf_map_area_alloc() the argument is calculated in an u64 variable; so 
>> if in the second
>> one, there is an integer overflow then the first one must be called 
>> with size > 4GB. I
>> think the first one will probably fail (I am not sure about the actual 
>> limit of vmalloc()),
>> so the second one might not be called.
> 
> I would sanity check this as well. Looks like k*alloc()/v*alloc() call 
> sites typically
> use array_size() which returns SIZE_MAX on overflow, 610b15c50e86 
> ("overflow.h: Add
> allocation size calculation helpers").

Hi,

I almost forget about this patch, I have checked the bpf_map_area_alloc 
in in stackmap.c and I can see that integer overflow cannot happen in 
this stackmap.c case.

In stack_map_alloc(),

	u64 cost;
	...
	cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
	cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
	smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr)); (1)
	...
	prealloc_elems_and_freelist(smap);

In prealloc_elems_and_freelist(),

	u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
	smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries, 
smap->map.numa_node); (2)

Argument calculation at (1) is safe. Argument calculation at (2) can 
potentially result in an integer overflow in 32-bit architecture. 
However, if the integer overflow happens, it means argument at (1) must 
be 2**32, which cannot pass the SIZE_MAX check in __bpf_map_area_alloc()

In __bpf_map_area_alloc()

	if (size >= SIZE_MAX)
		return NULL;

So I think the original patch has fixed instances of this bug pattern.

Thank you,
Quang Minh.

      reply	other threads:[~2021-05-17 16:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-26  8:26 [PATCH] bpf: Fix integer overflow in argument calculation for bpf_map_area_alloc Bui Quang Minh
2021-01-26  9:36 ` Lorenz Bauer
2021-01-27  4:23   ` Bui Quang Minh
2021-01-27  5:09     ` Bui Quang Minh
2021-01-28  0:41     ` Daniel Borkmann
2021-05-17 15:10       ` Bui Quang Minh [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=728b238e-a481-eb50-98e9-b0f430ab01e7@gmail.com \
    --to=minhquangbui99@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=hawk@kernel.org \
    --cc=jakub@cloudflare.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lmb@cloudflare.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    /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;
as well as URLs for NNTP newsgroup(s).