netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@redhat.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>, Andrii Nakryiko <andriin@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	Jiri Olsa <jolsa@redhat.com>,
	Eelco Chaudron <echaudro@redhat.com>,
	KP Singh <kpsingh@chromium.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v5 2/8] bpf: verifier: refactor check_attach_btf_id()
Date: Thu, 17 Sep 2020 12:06:18 +0200	[thread overview]
Message-ID: <87lfh8ogyt.fsf@toke.dk> (raw)
In-Reply-To: <CAEf4BzbAsnzAUPksUs+bcNuuUPkumc15RLESu3jOGf87mzabBA@mail.gmail.com>

Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:

>>
>> +int bpf_check_attach_target(struct bpf_verifier_log *log,
>> +                           const struct bpf_prog *prog,
>> +                           const struct bpf_prog *tgt_prog,
>> +                           u32 btf_id,
>> +                           struct btf_func_model *fmodel,
>> +                           long *tgt_addr,
>> +                           const char **tgt_name,
>> +                           const struct btf_type **tgt_type);
>
> So this is obviously an abomination of a function signature,
> especially for a one exported to other files.
>
> One candidate to remove would be tgt_type, which is supposed to be a
> derivative of target BTF (vmlinux or tgt_prog->btf) + btf_id,
> **except** (and that's how I found the bug below), in case of
> fentry/fexit programs attaching to "conservative" BPF functions, in
> which case what's stored in aux->attach_func_proto is different from
> what is passed into btf_distill_func_proto. So that's a bug already
> (you'll return NULL in some cases for tgt_type, while it has to always
> be non-NULL).

Okay, looked at this in more detail, and I don't think the refactored
code is doing anything different from the pre-refactor version?

Before we had this:

		if (tgt_prog && conservative) {
			prog->aux->attach_func_proto = NULL;
			t = NULL;
		}

and now we just have

		if (tgt_prog && conservative)
			t = NULL;

in bpf_check_attach_target(), which gets returned as tgt_type and
subsequently assigned to prog->aux->attach_func_proto.

> But related to that is fmodel. It seems like bpf_check_attach_target()
> has no interest in fmodel itself and is just passing it from
> btf_distill_func_proto(). So I was about to suggest dropping fmodel
> and calling btf_distill_func_proto() outside of
> bpf_check_attach_target(), but given the conservative + fentry/fexit
> quirk, it's probably going to be more confusing.
>
> So with all this, I suggest dropping the tgt_type output param
> altogether and let callers do a `btf__type_by_id(tgt_prog ?
> tgt_prog->aux->btf : btf_vmlinux, btf_id);`. That will both fix the
> bug and will make this function's signature just a tad bit less
> horrible.

Thought about this, but the logic also does a few transformations of the
type itself, e.g., this for bpf_trace_raw_tp:

		tname += sizeof(prefix) - 1;
		t = btf_type_by_id(btf, t->type);
		if (!btf_type_is_ptr(t))
			/* should never happen in valid vmlinux build */
			return -EINVAL;
		t = btf_type_by_id(btf, t->type);
		if (!btf_type_is_func_proto(t))
			/* should never happen in valid vmlinux build */
			return -EINVAL;

so to catch this we really do have to return the type from the function
as well.

I do agree that the function signature is a tad on the long side, but I
couldn't think of any good way of making it smaller. I considered
replacing the last two return values with a boolean 'save' parameter,
that would just make it same the values directly in prog->aux; but I
actually find it easier to reason about a function that is strictly
checking things and returning the result, instead of 'sometimes modify'
semantics...

-Toke


  parent reply	other threads:[~2020-09-17 10:22 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-15 11:40 [PATCH bpf-next v5 0/8] bpf: Support multi-attach for freplace programs Toke Høiland-Jørgensen
2020-09-15 11:40 ` [PATCH bpf-next v5 1/8] bpf: change logging calls from verbose() to bpf_log() and use log pointer Toke Høiland-Jørgensen
2020-09-16 17:08   ` Andrii Nakryiko
2020-09-15 11:40 ` [PATCH bpf-next v5 2/8] bpf: verifier: refactor check_attach_btf_id() Toke Høiland-Jørgensen
2020-09-16 17:32   ` Andrii Nakryiko
2020-09-16 21:07     ` Toke Høiland-Jørgensen
2020-09-17 10:06     ` Toke Høiland-Jørgensen [this message]
2020-09-17 16:38       ` Andrii Nakryiko
2020-09-17 16:54         ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 3/8] bpf: move prog->aux->linked_prog and trampoline into bpf_link on attach Toke Høiland-Jørgensen
2020-09-16 18:22   ` Andrii Nakryiko
2020-09-15 11:41 ` [PATCH bpf-next v5 4/8] bpf: support attaching freplace programs to multiple attach points Toke Høiland-Jørgensen
2020-09-16 19:49   ` Andrii Nakryiko
2020-09-16 21:13     ` Toke Høiland-Jørgensen
2020-09-16 21:17       ` Andrii Nakryiko
2020-09-16 21:27         ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 5/8] bpf: Fix context type resolving for extension programs Toke Høiland-Jørgensen
2020-09-16 19:59   ` Andrii Nakryiko
2020-09-16 20:28     ` Andrii Nakryiko
2020-09-16 21:15       ` Toke Høiland-Jørgensen
2020-09-17 17:10       ` Toke Høiland-Jørgensen
2020-09-17 18:06         ` Andrii Nakryiko
2020-09-17 18:44           ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 6/8] libbpf: add support for freplace attachment in bpf_link_create Toke Høiland-Jørgensen
2020-09-16 20:37   ` Andrii Nakryiko
2020-09-16 20:45     ` Andrii Nakryiko
2020-09-16 21:21       ` Toke Høiland-Jørgensen
2020-09-16 21:24         ` Andrii Nakryiko
2020-09-16 21:41           ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 7/8] selftests: add test for multiple attachments of freplace program Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 8/8] selftests/bpf: Adding test for arg dereference in extension trace Toke Høiland-Jørgensen
2020-09-16 20:44   ` Andrii Nakryiko

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=87lfh8ogyt.fsf@toke.dk \
    --to=toke@redhat.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=echaudro@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --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).