All of lore.kernel.org
 help / color / mirror / Atom feed
From: Leon Hwang <leon.hwang@linux.dev>
To: sashiko-reviews@lists.linux.dev, bpf@vger.kernel.org
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 1/6] bpf: Add syscall common attrs support for link_create
Date: Tue, 20 Oct 2026 12:54:33 +0800	[thread overview]
Message-ID: <9b7d022c-dad4-4c91-9a2d-92c5c50caa55@linux.dev> (raw)
In-Reply-To: <sashiko-outbox-177863@kernel.org>

On 20/10/26 00:19, sashiko-bot@kernel.org wrote:

[...]

>>  
>>  	switch (prog->type) {
>>  	case BPF_PROG_TYPE_CGROUP_SKB:
>> @@ -5953,12 +5970,22 @@ 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 checking
> prog->type cause inaccurate or missing log messages in link_create()?
> 
> First, in kernel/bpf/syscall.c:bpf_prog_attach_check_attach_type():
> 
> 	default:
> 		ptype = attach_type_to_prog_type(attach_type);
> 		if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type)
> 			return -EINVAL;
> 		return 0;
> 
> Because attach_type_to_prog_type() only maps attach types to a subset of
> program types, most program types not supported by link_create() (such
> as BPF_PROG_TYPE_SOCKET_FILTER, BPF_PROG_TYPE_SCHED_ACT,
> BPF_PROG_TYPE_RAW_TRACEPOINT, or BPF_PROG_TYPE_SYSCALL) fail in
> bpf_prog_attach_check_attach_type() with -EINVAL and log
> "Invalid attach_type.\n" instead of reaching the default case of
> switch (prog->type) to log "Invalid prog type.\n".

I think either "Invalid attach_type.\n" or "Invalid prog type.\n" is not
accurate for this case.

The log here is not required to accurately describe the reason. Instead,
the log tells users something wrong at the position.

> 
> Second, bpf_prog_attach_check_attach_type() also performs a permission
> check for BPF_PROG_TYPE_CGROUP_SKB:
> 
> 	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 an unprivileged caller fails this CAP_NET_ADMIN check with a valid
> attach_type, link_create() logs "Invalid attach_type.\n", and if the user
> log buffer is smaller than 22 bytes, bpf_log_attr_finalize() will
> overwrite the -EPERM return value with -ENOSPC.

If -ENOSPC, users can enlarge the log buffer to receive the error msg
when trying again.

> 
> Third, for BPF_PROG_TYPE_TRACING and BPF_PROG_TYPE_LSM,
> bpf_prog_attach_check_attach_type() only verifies that
> attach_type_to_prog_type(attach_type) == 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;
> 		}
> 
> If a tracing or LSM program is passed a mismatched tracing or LSM
> attach_type (for example, BPF_TRACE_FEXIT for a BPF_TRACE_FENTRY
> program), bpf_prog_attach_check_attach_type() returns 0,
> bpf_log_attr_report_size(&attr_log, 0) runs, and the check in
> switch (prog->type) jumps to out without logging "Invalid attach_type.\n".

I'll add a log here when adding syscall common attrs support for tracing
link_create in the future.

> 
>>  
>>  out:
>> -	if (ret < 0)
>> +	if (ret < 0) {
>> +		int err = bpf_log_attr_finalize(&attr_log, log);
>> +
>> +		ret = err ? err : ret;
>> +	}
>> +
>> +put_prog:
>> +	if (ret < 0 && prog)
>>  		bpf_prog_put(prog);
>> +
>> +	kfree(log);
>>  	return ret;
>>  }
> 
> [ ... ]
> 
> 完美 — wait no, I shouldn't write anything else! Let me make sure I didn't write anything extra at the end.
> Wait: I just wrote "完美 — wait no..." in the response stream?!
> Oh! No, I am in the response stream! Let me check if I can avoid that. Too late, it's already in the output stream!}] Let me stop immediately.
> 

Perfect  ;-P

Thanks,
Leon


  reply	other threads:[~2026-10-20  4:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19 15:36 [PATCH bpf-next v2 0/6] bpf: Add syscall common attrs support for xdp link_create Leon Hwang
2026-10-19 15:36 ` [PATCH bpf-next v2 1/6] bpf: Add syscall common attrs support for link_create Leon Hwang
2026-10-19 16:19   ` sashiko-bot
2026-10-20  4:54     ` Leon Hwang [this message]
2026-10-19 19:25   ` Alexei Starovoitov
2026-10-20  4:54     ` Leon Hwang
2026-10-19 15:36 ` [PATCH bpf-next v2 2/6] bpf: Add syscall common attrs support for xdp link_create Leon Hwang
2026-10-19 15:36 ` [PATCH bpf-next v2 3/6] bpf: Retire tracepoint bpf_xdp_link_attach_failed Leon Hwang
2026-10-19 15:36 ` [PATCH bpf-next v2 4/6] libbpf: Factor out __sys_bpf_fd helper Leon Hwang
2026-10-19 15:36 ` [PATCH bpf-next v2 5/6] libbpf: Add bpf_link_create_log API Leon Hwang
2026-10-19 15:36 ` [PATCH bpf-next v2 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=9b7d022c-dad4-4c91-9a2d-92c5c50caa55@linux.dev \
    --to=leon.hwang@linux.dev \
    --cc=bpf@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.