LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Colin Ian King <colin.king@canonical.com>
Cc: Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Yonghong Song <yhs@fb.com>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	bpf <bpf@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: bpf: Implement minimal BPF perf link
Date: Tue, 17 Aug 2021 11:57:02 -0700	[thread overview]
Message-ID: <CAEf4BzYP6OU23D33d6dzgpYyXqSGrQUpenrJStyYFB3L7S93ew@mail.gmail.com> (raw)
In-Reply-To: <342670fc-948a-a76e-5a47-b3d44e3e3926@canonical.com>

On Tue, Aug 17, 2021 at 10:36 AM Colin Ian King
<colin.king@canonical.com> wrote:
>
> Hi,
>
> Static analysis with Coverity on linux-next has detected a potential
> issue with the following commit:
>
> commit b89fbfbb854c9afc3047e8273cc3a694650b802e
> Author: Andrii Nakryiko <andrii@kernel.org>
> Date:   Sun Aug 15 00:05:57 2021 -0700
>
>     bpf: Implement minimal BPF perf link
>
> The analysis is as follows:
>
> 2936 static int bpf_perf_link_attach(const union bpf_attr *attr, struct
> bpf_prog *prog)
> 2937 {
>
>     1. var_decl: Declaring variable link_primer without initializer.
>
> 2938        struct bpf_link_primer link_primer;
> 2939        struct bpf_perf_link *link;
> 2940        struct perf_event *event;
> 2941        struct file *perf_file;
> 2942        int err;
> 2943
>
>     2. Condition attr->link_create.flags, taking false branch.
>
> 2944        if (attr->link_create.flags)
> 2945                return -EINVAL;
> 2946
> 2947        perf_file = perf_event_get(attr->link_create.target_fd);
>
>     3. Condition IS_ERR(perf_file), taking false branch.
>
> 2948        if (IS_ERR(perf_file))
> 2949                return PTR_ERR(perf_file);
> 2950
> 2951        link = kzalloc(sizeof(*link), GFP_USER);
>
>     4. Condition !link, taking false branch.
>
> 2952        if (!link) {
> 2953                err = -ENOMEM;
> 2954                goto out_put_file;
> 2955        }
> 2956        bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT,
> &bpf_perf_link_lops, prog);
> 2957        link->perf_file = perf_file;
> 2958
> 2959        err = bpf_link_prime(&link->link, &link_primer);
>
>     5. Condition err, taking false branch.
>
> 2960        if (err) {
> 2961                kfree(link);
> 2962                goto out_put_file;
> 2963        }
> 2964
> 2965        event = perf_file->private_data;
> 2966        err = perf_event_set_bpf_prog(event, prog,
> attr->link_create.perf_event.bpf_cookie);
>
>     6. Condition err, taking true branch.
> 2967        if (err) {
>     7. uninit_use_in_call: Using uninitialized value link_primer.fd when
> calling bpf_link_cleanup.
>     8. uninit_use_in_call: Using uninitialized value link_primer.file
> when calling bpf_link_cleanup.
>     9. uninit_use_in_call: Using uninitialized value link_primer.id when
> calling bpf_link_cleanup.
>
>    Uninitialized pointer read (UNINIT)
>    10. uninit_use_in_call: Using uninitialized value link_primer.link
> when calling bpf_link_cleanup.
>
> 2968                bpf_link_cleanup(&link_primer);
> 2969                goto out_put_file;
> 2970        }
> 2971        /* perf_event_set_bpf_prog() doesn't take its own refcnt on
> prog */
> 2972        bpf_prog_inc(prog);
>
> I'm not 100% sure if these are false-positives, but I thought I should
> report the issues as potentially there is a pointer access on an
> uninitialized pointer on line 2968.

Look at bpf_link_prime() implementation. If it succeeds, link_primer
is fully initialized. We use this pattern in many places, this is the
first time someone reports any potential issues with it. It's a bit
strange that Coverity doesn't recognize such a typical output
parameter initialization pattern, tbh. Maybe the global nature of
bpf_link_prime() throws it off (it assumes it can be "overridden"
during linking?) But I double-checked everything twice, all seems to
be good. Zero-initializing link_primer would be a total waste.

>
> Colin

  reply	other threads:[~2021-08-17 18:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-17 17:36 Colin Ian King
2021-08-17 18:57 ` Andrii Nakryiko [this message]
2021-08-17 20:34   ` Colin Ian King

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=CAEf4BzYP6OU23D33d6dzgpYyXqSGrQUpenrJStyYFB3L7S93ew@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=colin.king@canonical.com \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=yhs@fb.com \
    --subject='Re: bpf: Implement minimal BPF perf link' \
    /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

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).