public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: He Fengqing <hefengqing@huawei.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Song Liu <song@kernel.org>, 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>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	"David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	open list <linux-kernel@vger.kernel.org>
Subject: Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()
Date: Wed, 14 Jul 2021 09:53:46 +0800	[thread overview]
Message-ID: <cdb39df7-dbfa-c935-e819-97c2134dd2cf@huawei.com> (raw)
In-Reply-To: <CAADnVQ+XGGaXfte6aDdEp6euYckGtyP6S+VDUe4JusUz7xDLLg@mail.gmail.com>



在 2021/7/14 7:17, Alexei Starovoitov 写道:
> On Sun, Jul 11, 2021 at 7:17 PM He Fengqing <hefengqing@huawei.com> wrote:
>>
>>
>>
>> 在 2021/7/9 23:12, Alexei Starovoitov 写道:
>>> On Fri, Jul 9, 2021 at 4:11 AM He Fengqing <hefengqing@huawei.com> wrote:
>>>>
>>>>
>>>>
>>>> 在 2021/7/8 11:09, Alexei Starovoitov 写道:
>>>>> On Wed, Jul 7, 2021 at 8:00 PM He Fengqing <hefengqing@huawei.com> wrote:
>>>>>>
>>>>>> Ok, I will change this in next version.
>>>>>
>>>>> before you spam the list with the next version
>>>>> please explain why any of these changes are needed?
>>>>> I don't see an explanation in the patches and I don't see a bug in the code.
>>>>> Did you check what is the prog clone ?
>>>>> When is it constructed? Why verifier has anything to do with it?
>>>>> .
>>>>>
>>>>
>>>>
>>>> I'm sorry, I didn't describe these errors clearly.
>>>>
>>>> bpf_check(bpf_verifier_env)
>>>>        |
>>>>        |->do_misc_fixups(env)
>>>>        |    |
>>>>        |    |->bpf_patch_insn_data(env)
>>>>        |    |    |
>>>>        |    |    |->bpf_patch_insn_single(env->prog)
>>>>        |    |    |    |
>>>>        |    |    |    |->bpf_prog_realloc(env->prog)
>>>>        |    |    |    |    |
>>>>        |    |    |    |    |->construct new_prog
>>>>        |    |    |    |    |    free old_prog(env->prog)
>>>>        |    |    |    |    |
>>>>        |    |    |    |    |->return new_prog;
>>>>        |    |    |    |
>>>>        |    |    |    |->return new_prog;
>>>>        |    |    |
>>>>        |    |    |->adjust_insn_aux_data
>>>>        |    |    |    |
>>>>        |    |    |    |->return ENOMEM;
>>>>        |    |    |
>>>>        |    |    |->return NULL;
>>>>        |    |
>>>>        |    |->return ENOMEM;
>>>>
>>>> bpf_verifier_env->prog had been freed in bpf_prog_realloc function.
>>>>
>>>>
>>>> There are two errors here, the first is memleak in the
>>>> bpf_patch_insn_data function, and the second is use after free in the
>>>> bpf_check function.
>>>>
>>>> memleak in bpf_patch_insn_data:
>>>>
>>>> Look at the call chain above, if adjust_insn_aux_data function return
>>>> ENOMEM, bpf_patch_insn_data will return NULL, but we do not free the
>>>> new_prog.
>>>>
>>>> So in the patch 2, before bpf_patch_insn_data return NULL, we free the
>>>> new_prog.
>>>>
>>>> use after free in bpf_check:
>>>>
>>>> If bpf_patch_insn_data function return NULL, we will not assign new_prog
>>>> to the bpf_verifier_env->prog, but bpf_verifier_env->prog has been freed
>>>> in the bpf_prog_realloc function. Then in bpf_check function, we will
>>>> use bpf_verifier_env->prog after do_misc_fixups function.
>>>>
>>>> In the patch 3, I added a free_old parameter to bpf_prog_realloc, in
>>>> this scenario we don't free old_prog. Instead, we free it in the
>>>> do_misc_fixups function when bpf_patch_insn_data return a valid new_prog.
>>>
>>> Thanks for explaining.
>>> Why not to make adjust_insn_aux_data() in bpf_patch_insn_data() first then?
>>> Just changing the order will resolve both issues, no?
>>> .
>>>
>> adjust_insn_aux_data() need the new constructed new_prog as an input
>> parameter, so we must call bpf_patch_insn_single() before
>> adjust_insn_aux_data().
> 
> Right. I forgot about insn_has_def32() logic and
> commit b325fbca4b13 ("bpf: verifier: mark patched-insn with
> sub-register zext flag")
> that added that extra parameter.
> 
>> But we can make adjust_insn_aux_data() never return ENOMEM. In
>> bpf_patch_insn_data(), first we pre-malloc memory for new aux_data, then
>> call bpf_patch_insn_single() to constructed the new_prog, at last call
>> adjust_insn_aux_data() functin. In this way, adjust_insn_aux_data()
>> never fails.
>>
>> bpf_patch_insn_data(env) {
>>          struct bpf_insn_aux_data *new_data = vzalloc();
>>          struct bpf_prog *new_prog;
>>          if (new_data == NULL)
>>                  return NULL;
>>
>>          new_prog = bpf_patch_insn_single(env->prog);
>>          if (new_prog == NULL) {
>>                  vfree(new_data);
>>                  return NULL;
>>          }
>>
>>          adjust_insn_aux_data(new_prog, new_data);
>>          return new_prog;
>> }
>> What do you think about it?
> 
> That's a good idea. Let's do that. The new size for vzalloc is easy to compute.
> What should be the commit in the Fixes tag?
> commit 8041902dae52 ("bpf: adjust insn_aux_data when patching insns")
> right?

Ok, I will add this in the commit message.

> 4 year old bug then.
> I wonder why syzbot with malloc error injection didn't catch it sooner.
> .
> 

      reply	other threads:[~2021-07-14  1:53 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-07  4:38 [bpf-next 0/3] potential memleak and use after free in bpf verifier He Fengqing
2021-07-07  4:38 ` [bpf-next 1/3] bpf: Move bpf_prog_clone_free into filter.h file He Fengqing
2021-07-07  7:02   ` Song Liu
2021-07-07  4:38 ` [bpf-next 2/3] bpf: Fix a memory leak in an error handling path in 'bpf_patch_insn_data()' He Fengqing
2021-07-07  4:38 ` [bpf-next 3/3] bpf: Fix a use after free in bpf_check() He Fengqing
2021-07-07  7:25   ` Song Liu
2021-07-08  3:00     ` He Fengqing
2021-07-08  3:09       ` Alexei Starovoitov
2021-07-09 11:11         ` He Fengqing
2021-07-09 15:12           ` Alexei Starovoitov
2021-07-12  2:17             ` He Fengqing
2021-07-13 23:17               ` Alexei Starovoitov
2021-07-14  1:53                 ` He Fengqing [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=cdb39df7-dbfa-c935-e819-97c2134dd2cf@huawei.com \
    --to=hefengqing@huawei.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=song@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