BPF List
 help / color / mirror / Atom feed
From: Mahe Tardy <mahe.tardy@gmail.com>
To: Song Liu <song@kernel.org>
Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	bpf@vger.kernel.org, andrew+netdev@lunn.ch, andrii@kernel.org,
	ast@kernel.org, daniel@iogearbox.net, davem@davemloft.net,
	eddyz87@gmail.com, edumazet@google.com, john.fastabend@gmail.com,
	kuba@kernel.org, liamwisehart@meta.com, martin.lau@linux.dev,
	pabeni@redhat.com, netdev@vger.kernel.org, sdf.kernel@gmail.com,
	ameryhung@gmail.com, kuniyu@google.com
Subject: Re: [PATCH bpf-next v3 2/5] bpf: Add ksock kfuncs
Date: Thu, 6 Aug 2026 19:21:30 +0200	[thread overview]
Message-ID: <anTCmv1esr3CWfSf@gmail.com> (raw)
In-Reply-To: <CAPhsuW7=6Wy31ZKqUiwOOPJA7-39y_DtJXBO8BFj+OUfgnzRWg@mail.gmail.com>

On Tue, Aug 04, 2026 at 09:55:44PM -0700, Song Liu wrote:
> On Tue, Aug 4, 2026 at 9:47 AM Mahe Tardy <mahe.tardy@gmail.com> wrote:
> [...]
> > +__bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
> > +                              u32 data__sz)
> > +{
> > +       struct msghdr msg = {
> > +               .msg_flags = MSG_DONTWAIT,
> > +       };
> > +       struct kvec iov = {
> > +               .iov_base = (void *)data,
> > +               .iov_len = data__sz,
> > +       };
> > +       int ret;
> > +
> > +       if (!bpf_ksock_has_user_task_context())
> > +               return -EOPNOTSUPP;
> > +
> > +       /* Early check for UDP. Exact limits enforced by kernel_sendmsg(). */
> > +       if (data__sz > IP_MAX_MTU)
> > +               return -EMSGSIZE;
> > +
> > +       if (current->in_bpf_ksock_send)
> > +               return -EBUSY;
> 
> Returning EBUSY is not ideal. Can we avoid recursion by disallowing
> calling bpf_ksock_send from certain hooks? You can find examples
> check_kfunc_call() or check_special_kfunc(). This probably means
> we need to grow special_kfunc_list, which is not ideal.

This is actually a really nice feedback. I didn't know we could add a
filter per attach BTF ID. This resolves the issue that the send kfunc
calls security_socket_sendmsg so we can remove all the recursion
protection and struggles, it's rejected at verifier time.

The filter now looks like this, I'd send this in the next version:

BTF_ID_LIST_SINGLE(bpf_lsm_socket_sendmsg_id, func, bpf_lsm_socket_sendmsg)

static int bpf_ksock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
{
	if (!btf_id_set8_contains(&ksock_kfunc_btf_ids, kfunc_id))
		return 0;

	if (prog->type == BPF_PROG_TYPE_SYSCALL)
		return 0;

	if (prog->type == BPF_PROG_TYPE_LSM &&
	    prog->aux->attach_btf_id != bpf_lsm_socket_sendmsg_id[0])
		return 0;

	return -EACCES;
}

I don't think I need to touch anything else. I'll adjust the tests
according to this. Thanks Song.

> 
> @Kumar, do you have strong objection that we grow special_kfunc_list
> for this use case?
> 
> Thanks,
> Song
> 
> 
> > +
> > +       current->in_bpf_ksock_send = true;
> > +       ret = kernel_sendmsg(ks->sock, &msg, &iov, 1, data__sz);
> > +       current->in_bpf_ksock_send = false;
> > +
> > +       return ret;
> > +}
> > +
> > +__bpf_kfunc_end_defs();

  reply	other threads:[~2026-08-06 17:21 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-04 16:46 [PATCH bpf-next v3 0/5] Introduce bpf_ksock Mahe Tardy
2026-08-04 16:46 ` [PATCH bpf-next v3 1/5] net: Add __sys_connect_socket() helper Mahe Tardy
2026-08-04 17:50   ` bot+bpf-ci
2026-08-05  4:05   ` Song Liu
2026-08-06 17:18     ` Mahe Tardy
2026-08-04 16:46 ` [PATCH bpf-next v3 2/5] bpf: Add ksock kfuncs Mahe Tardy
2026-08-05  4:55   ` Song Liu
2026-08-06 17:21     ` Mahe Tardy [this message]
2026-08-06 17:22       ` Kumar Kartikeya Dwivedi
2026-08-04 16:46 ` [PATCH bpf-next v3 3/5] selftests/bpf: Add ksock kfunc test Mahe Tardy
2026-08-04 16:46 ` [PATCH bpf-next v3 4/5] selftests/bpf: Add ksock LSM recursion test Mahe Tardy
2026-08-04 16:46 ` [PATCH bpf-next v3 5/5] selftests/bpf: Add ksock test for async callback guard Mahe Tardy

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=anTCmv1esr3CWfSf@gmail.com \
    --to=mahe.tardy@gmail.com \
    --cc=ameryhung@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=liamwisehart@meta.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf.kernel@gmail.com \
    --cc=song@kernel.org \
    /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