BPF List
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Yonghong Song <yhs@fb.com>, Eugene Syromiatnikov <esyr@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	Jiri Olsa <jolsa@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	bpf@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] bpf: Use safer kvmalloc_array() where possible
Date: Thu, 26 May 2022 21:52:22 +0300	[thread overview]
Message-ID: <20220526185221.GI2168@kadam> (raw)
In-Reply-To: <67e7906e-3f93-a979-f534-bfe7199f843f@fb.com>

On Thu, May 26, 2022 at 08:31:10AM -0700, Yonghong Song wrote:
> 
> 
> On 5/26/22 3:24 AM, Dan Carpenter wrote:
> > The kvmalloc_array() function is safer because it has a check for
> > integer overflows.  These sizes come from the user and I was not
> > able to see any bounds checking so an integer overflow seems like a
> > realistic concern.
> > 
> > Fixes: 0dcac2725406 ("bpf: Add multi kprobe link")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >   kernel/trace/bpf_trace.c | 8 ++++----
> >   1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> > index 10b157a6d73e..7a13e6ac6327 100644
> > --- a/kernel/trace/bpf_trace.c
> > +++ b/kernel/trace/bpf_trace.c
> > @@ -2263,11 +2263,11 @@ static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32
> >   	int err = -ENOMEM;
> >   	unsigned int i;
> > -	syms = kvmalloc(cnt * sizeof(*syms), GFP_KERNEL);
> > +	syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
> >   	if (!syms)
> >   		goto error;
> > -	buf = kvmalloc(cnt * KSYM_NAME_LEN, GFP_KERNEL);
> > +	buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
> >   	if (!buf)
> >   		goto error;
> > @@ -2464,7 +2464,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
> 
> For this part of change, there is a similar pending patch here:
> https://lore.kernel.org/bpf/399e634781822329e856103cddba975f58f0498c.1652982525.git.esyr@redhat.com/
> which waits for further review. That patch tries to detect the overflow
> explicitly to avoid possible kernel dmesg warnings. (See function
> kvmalloc_node()).

That patch doesn't apply any more.

Static checkers will insist that kvmalloc_array() is cleaner and safer
than kvmalloc(n * size, and they don't care if the integer overflow is
real or not.

-EOVERFLOW is the wrong error code.  Just return -ENOMEM.  Checking for
size > INT_MAX is ugly.  Use a correct limit based on what the maximum
reasonable size is.  Or if we only want to prevent the stack dump then
just pass __GFP_NOWARN.

It annoyed me that size was type unsigned int.  Sizes should be unsigned
long.  Every alloc() function takes an unsigned long so using a u32
temporary value for the size is what made this code so dangerous.  If
it had been:

	addrs = kvmalloc(cnt * sizeof(*addrs), GFP_KERNEL);

instead of:

	size = cnt * sizeof(*addrs);
	addrs = kvmalloc(size, GFP_KERNEL);

Then the integer overflow bug would only have affected 32 bit systems
and those are pretty rare.  Choosing the wrong type took a minor bug and
made it affect everyone.

regards,
dan carpenter


  reply	other threads:[~2022-05-26 18:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-26 10:24 [PATCH] bpf: Use safer kvmalloc_array() where possible Dan Carpenter
2022-05-26 15:31 ` Yonghong Song
2022-05-26 18:52   ` Dan Carpenter [this message]
2022-05-26 15:33 ` Jiri Olsa
2022-06-01 17:40 ` patchwork-bot+netdevbpf

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=20220526185221.GI2168@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=esyr@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kafai@fb.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.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