BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Leon Hwang" <leon.hwang@linux.dev>, bpf@vger.kernel.org
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next v4 1/6] bpf: Add syscall common attrs support for link_create
Date: Sat, 24 Oct 2026 15:50:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-183784@kernel.org> (raw)
In-Reply-To: <20261024153608.58569-2-leon.hwang@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf: incorrect error log messages and ordering in link_create()
--

commit f2293d2ca04aeb83f219d17a0904b9b58058c572
Author: Leon Hwang <leon.hwang@linux.dev>

bpf: Add syscall common attrs support for link_create

This commit adds syscall common attributes support to link_create(),
creating and finalizing a verifier log instance to report errors for invalid
prog_fd, attach_type, and prog type.

Link: https://lore.kernel.org/bpf/20260514004634.3DA09C19425@smtp.kernel.org/

> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 90bc7230afa8a..db870e795d044 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -5862,8 +5862,11 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
>  }
>  
>  #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.path_fd
> -static int link_create(union bpf_attr *attr, bpfptr_t uattr)
> +static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_attr *attr_common,
> +		       bpfptr_t uattr_common, u32 size_common)
>  {
> +	struct bpf_verifier_log *log;
> +	struct bpf_log_attr attr_log;
>  	struct bpf_prog *prog;
>  	int ret;
>  
> @@ -5873,14 +5876,24 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
>  	if (attr->link_create.attach_type == BPF_STRUCT_OPS)
>  		return bpf_struct_ops_link_create(attr);
>  
> +	log = bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, size_common);
> +	if (IS_ERR(log))
> +		return PTR_ERR(log);
> +
>  	prog = bpf_prog_get(attr->link_create.prog_fd);
> -	if (IS_ERR(prog))
> -		return PTR_ERR(prog);
> +	if (IS_ERR(prog)) {
> +		bpf_log(log, "Invalid prog_fd.\n");
> +		ret = PTR_ERR(prog);
> +		prog = NULL;
> +		goto out;
> +	}
>  
>  	ret = bpf_prog_attach_check_attach_type(prog,
>  						attr->link_create.attach_type);
> -	if (ret)
> +	if (ret) {
> +		bpf_log(log, "Invalid attach_type.\n");
>  		goto out;
> +	}
>  
>  	switch (prog->type) {
>  	case BPF_PROG_TYPE_CGROUP_SKB:
> @@ -5959,12 +5972,20 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
>  			ret = bpf_uprobe_multi_link_attach(attr, prog);
>  		break;
>  	default:
> +		bpf_log(log, "Invalid prog type.\n");
>  		ret = -EINVAL;
>  	}

[Severity: Medium]
Can calling bpf_prog_attach_check_attach_type() before switching on
prog->type cause link_create() to log the wrong error message or skip
logging on mismatched attach types?

First, in kernel/bpf/syscall.c:bpf_prog_attach_check_attach_type(), a
cgroup skb program checks permissions before validating the attach type:

	case BPF_PROG_TYPE_CGROUP_SKB:
		if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
			/* cg-skb progs can be loaded by unpriv user.
			 * check permissions at attach time.
			 */
			return -EPERM;

When that capability check fails, link_create() still logs
"Invalid attach_type.\n" even though attach_type may be valid.

Second, for program types that are not supported by link_create() and have
no mapping in attach_type_to_prog_type() (such as socket filter, sched act,
raw tracepoint, or syscall programs), bpf_prog_attach_check_attach_type()
hits its default branch and returns -EINVAL before link_create() reaches
switch (prog->type):

	default:
		ptype = attach_type_to_prog_type(attach_type);
		if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type)
			return -EINVAL;
		return 0;

Doesn't that cause link_create() to log "Invalid attach_type.\n" and jump
to out, leaving the new default branch that logs "Invalid prog type.\n"
unreachable for almost all unsupported program types?

Third, for tracing and lsm programs, bpf_prog_attach_check_attach_type()
only checks that attach_type maps to prog->type in
attach_type_to_prog_type(), while the check against
prog->expected_attach_type happens later inside switch (prog->type) in
link_create():

	case BPF_PROG_TYPE_LSM:
	case BPF_PROG_TYPE_TRACING:
		if (attr->link_create.attach_type != prog->expected_attach_type) {
			ret = -EINVAL;
			goto out;
		}

Should that mismatch also log "Invalid attach_type.\n"?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261024153608.58569-1-leon.hwang@linux.dev?part=1

  reply	other threads:[~2026-10-24 15:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-24 15:36 [PATCH bpf-next v4 0/6] bpf: Add syscall common attrs support for xdp link_create Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 1/6] bpf: Add syscall common attrs support for link_create Leon Hwang
2026-10-24 15:50   ` sashiko-bot [this message]
2026-10-24 15:36 ` [PATCH bpf-next v4 2/6] bpf: Add syscall common attrs support for xdp link_create Leon Hwang
2026-10-25  1:19   ` Alexei Starovoitov
2026-10-26 15:06     ` Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 3/6] bpf: Retire tracepoint bpf_xdp_link_attach_failed Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 4/6] libbpf: Factor out __sys_bpf_fd helper Leon Hwang
2026-10-24 16:13   ` bot+bpf-ci
2026-10-26 15:07     ` Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 5/6] libbpf: Add log_opts support for bpf_link_create Leon Hwang
2026-10-24 16:13   ` bot+bpf-ci
2026-10-26 15:08     ` Leon Hwang
2026-10-24 15:36 ` [PATCH bpf-next v4 6/6] selftests/bpf: Add tests to verify link_create logs Leon Hwang

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=sashiko-outbox-183784@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=leon.hwang@linux.dev \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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