Article View: linux.kernel.netdev
Article #6870Re: [PATCH bpf-next 1/2] bpf: Add bpf_ksock_bind() kfunc
From: Alan Maguire
Date: Fri, 23 Oct 2026 16:05
Date: Fri, 23 Oct 2026 16:05
115 lines
12869 bytes
12869 bytes
On 10/23/26 9:42 AM, Mahe Tardy wrote:
> On Thu, Oct 22, 2026 at 06:45:34PM +0000, bot+bpf-ci@kernel.org wrote:
>>> bpf: Add bpf_ksock_bind() kfunc
>>
>> TL;DR: bpf_ksock_bind() called from a bpf_wq callback runs with kworker's full
>> capabilities, so a CAP_BPF-only SYSCALL program may bypass the privileged-port
>> (CAP_NET_BIND_SERVICE) check; the review asks whether to block it.
>
> Honestly don't know if it makes sense to prevent it given you are
> creating this kernel socket from a BPF context, if anyone with more
> experience on this can help?
>
> If the answer is yes, I imagine preventing bind() to run from a
> workqueue is the easy answer so that all creds are respected as usual
> and it's not a huge limitation (we mostly care about being able to
> send()/sendto() from a wq).
>
I suppose one way out of this would be to forbid bind()ing to the privileged
port range; that would sidestep the issue I think. Hard to know how restrictive
that would be in terms of real use cases, what do you think?
>>
>>> diff --git a/net/core/bpf_ksock.c b/net/core/bpf_ksock.c
>>> index 37168f43c16c..c7b487d90cd0 100644
>>> --- a/net/core/bpf_ksock.c
>>> +++ b/net/core/bpf_ksock.c
>>
>> [ ... ]
>>
>>> @@ -178,6 +178,33 @@ bpf_ksock_create(const struct bpf_ksock_create_opts *opts, u32 opts__sz,
>>> return NULL;
>>> }
>>>
>>> +/**
>>> + * bpf_ksock_bind() - Bind a BPF kernel socket to a local address.
>>
>> [ ... ]
>>
>>> +__bpf_kfunc int bpf_ksock_bind(struct bpf_ksock *ks,
>>> + const union bpf_ksock_addr *addr, u32 addr__sz)
>>> +{
>>> + struct sockaddr_storage sa;
>>> + int addrlen;
>>> +
>>> + addrlen = bpf_ksock_addr_to_sockaddr(addr, addr__sz, &sa);
>>> + if (addrlen < 0)
>>> + return addrlen;
>>> +
>>> + return __sys_bind_socket(ks->sock, &sa, addrlen);
>>> +}
>>> +
>>
>> Can the CAP_NET_BIND_SERVICE check for privileged ports be bypassed when
>> this runs from a bpf_wq callback?
>>
>> bpf_ksock_bind() is added to ksock_init_kfunc_btf_ids, the same set as
>> bpf_ksock_connect(). Commit 60bbe76a3f9e ("bpf: Allow ksock connect/send
>> calls from wq") made the kfuncs in this set callable from bpf_wq callbacks
>> of SYSCALL programs, and tools/testing/selftests/bpf/progs/ksock_wq.c
>> already calls bpf_ksock_connect() from such a callback.
>>
>> bpf_wq_work() runs that callback in a kworker, where current->cred is
>> init_cred with cap_effective set to CAP_FULL_SET (see init/init_task.c).
>>
>> The bind path is:
>>
>> bpf_ksock_bind()
>> __sys_bind_socket()
>> security_socket_bind()
>> inet_bind() -> inet_bind_sk() -> __inet_bind()
>>
>> and __inet_bind() then does:
>>
>> snum = ntohs(addr->sin_port);
>> err = -EACCES;
>> if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) &&
>> snum && inet_port_requires_bind_service(net, snum) &&
>> !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
>> goto out;
>>
>> __inet6_bind() has the same check. With init_cred, cap_capable_helper()
>> walks up to init_user_ns and finds the capability raised, so the check
>> passes for any netns.
>>
>> For example, a task holding only CAP_BPF can load a SYSCALL program, since
>> is_net_admin_prog_type() does not ask for CAP_NET_ADMIN for that type.
>> Such a program could bind its ksock to UDP port 53, 123 or 514 from a wq
>> callback, while the same call made synchronously from the SYSCALL program
>> returns -EACCES. A bpf_task_work callback looks like it has the same
>> problem in a different form, since the check would be evaluated against
>> whichever task the work was scheduled on.
>>
>> Commit 60bbe76a3f9e only argued about LSM checks ("bypassing LSM checks for
>> BPF-created sockets could be acceptable"), and connect and send on a UDP
>> ksock have no capability gate. bind appears to be the first ksock
>> operation that does, and the commit message does not address the wq
>> context.
>>
>> Should bpf_ksock_bind() refuse to run from kthread context, the way
>> bpf_ksock_create() does with its PF_KTHREAD check? Alternatively, should
>> it be kept out of the set that is reachable from wq callbacks, or should
>> the commit message state that bypassing the privileged-port check is
>> intended?
>>
>> No fix for this was found in the provided range; 76406b1f799f
>> ("selftests/bpf: Test bpf_ksock_bind() kfunc") only adds tests.
>>
>>
>> ---
>> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
>> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
>>
>> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/39831790314
>
Technical Details
Message-ID:
<b28db1fb-ea02-4f83-be99-c8d4b180dc6b@kernel.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-351146-linux-kernel-netdev=dewy.ruReferences:
<20261022181620.247201-1-mahe.tardy@gmail.com> <e31194dd72efd3c182707e870b7e7a6d67b78be23cea06aded0275678373c64c@mail.kernel.org> <atseCFvfx9FbTXP3@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-351146-linux-kernel-netdev=dewy.ru From: almagui@kernel.org (Alan Maguire) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH bpf-next 1/2] bpf: Add bpf_ksock_bind() kfunc Date: Fri, 23 Oct 2026 17:05:10 +0100 Organization: Usenet Network Message-ID: <b28db1fb-ea02-4f83-be99-c8d4b180dc6b@kernel.org> References: <20261022181620.247201-1-mahe.tardy@gmail.com> <e31194dd72efd3c182707e870b7e7a6d67b78be23cea06aded0275678373c64c@mail.kernel.org> <atseCFvfx9FbTXP3@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="545194"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:q+Rg+kA/QGXY8mkZtO1OZQsYb3E= sha256:EQF0WtRL6dpT+miQqaB1DwxkGqHR9CJO0BeDUDwfKBg= sha1:pROc+1u5CJJ3A9yjW8xoe3calwI= sha256:OOPm0PL7JFREMZFvnS/kMSVf1+g/M7reklhVwvDQxi8= Content-Language: en-GB List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 04D8811151B; Fri, 23 Oct 2026 19:10:22 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.739 X-Spam-Status: No, score=-3.739 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.339, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LJum3A8hdy_U; Fri, 23 Oct 2026 19:10:18 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-351146-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 04EC3111505; Fri, 23 Oct 2026 19:10:09 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id AEF9238716; Fri, 23 Oct 2026 16:05:20 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1792771519; b=MXiSXaWQsj0ngKwtNiETNhLuU96wWfxbpMbbCH81AXCzKth9GcmeUb3SJmH7SMPlsjZT 2UXrchbDw0k2EjPMFtUUV/+otsNHtkQivN34kYhRDyrMor8bl7ario/qFRJWSWecesKY1 iZrKP9O6WQXRRWGLmxe8DhkXGvzu7xWtXAplAOLoMQ5G3Ee/oBW5/frK5lXRQ2BoBLlm4 nCcF4hpxmDLq2J4nBRD1hnpUKw6Pvjh5kIwfU4gBHIY/Im4lQx8a6vcWx0lgHVMu5w9+n q4KAzL5qohF7ARyHxGa248b6B/1Cw5Vo4LmBlM9TsXMpWPTI517mE5qUnbJ2q8/EI3Q== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792771519; h=X-Original-To:Received:Received:DKIM-Signature:Message-ID:Date: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=UocDU7ImLSN8bUn3qE+8ros6Ps+AObI529MubY0oH/4=; b=eSnYhkaSSgGI3P1SS5Q7kvBQ2+91zehuS/5+lPlSEojQ89Ovq6Nm0wMJ5LehjwNzBBBA q/cv5B+j/bgy/d+xvFzu8qEz2qkLh9wAy5qhpXv9kQy+nZeQN6WddpClqzc5x6edCnriM C9Sxw9jO+fY+ScRy+7SWfuv/CsarE2qm1rYjZqsfh1Vm5WQTMrFgqosOCW3kEH75IjnO+ gDVVPymi/Rrm9Vw5Rzy30eDlTo1rZ6hd3sUY9BEuHdjbKSQTfghKcLZsbVEjsCezbQEaN BolsNWSf9jgKiP8Nhm/uzc/xd4xqBkHWQciton25vJCTkq9dgcjrbJ/aydlfOI2UAaQ== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CHzppK64; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D349B606A338; Fri, 23 Oct 2026 16:05:19 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CHzppK64 X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792771519; b=MQGfrIBhvbXxj12xg1yJOPoHCxwp/x91gMYDZw2rfzLqqWbReDOGh50aORmmUw0T7WaA 4X0bAdhZwnQNXh/LQX+azY1gKfY2HCpNmIEjloZS2ftxiyffxGOyNGqJ1Wk6vRa+5ocdT 5RPst1iEY1iV/OqKz9FFGrKu73DAEl/jkhbq3fFXvJ6bwq417Rux1OeUgoWKpkDfcSY3D N3JLKpfXIEdma6zMcU1oP0UNRDE6EpyssuqbSbDr0yIaSHFUfA9VZ9uSxPbYbT1C5txAD b4DA56iTlB6oWVUlYhhM+I1hRBL162xX1lkNMBXQeNwctw1AxmLvW3ueDcrForogjlg== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792771519; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=UocDU7ImLSN8bUn3qE+8ros6Ps+AObI529MubY0oH/4=; b=SCktZS6IfcJtK/qt3V4mAut/sVcKob7xwsyioJe/8qngW8lwJ6Gu3ViECdmTAH/NePoE 9lDww3BtYzRxQKoRVuZ/r4cMcQMmvcUpurM8tmR9dUKksW412jPMUjRfuAW2kJ4vwf6ky g7mo+ZugFnXQACByQ2+gGHhHeX3Uow26ANJVTXW7JfI5Tyw86sfKAvv8SBpsnvxUBg4IO kQZo00lDY7wG9sTYm+ruPoXp2xsqbsGY0zLtecdEB1lzeD4NDF+VDI9vucm7tARA5kNTg AZnAT79KQYi58votDdBM7gKPxo8pFBz/YWlsvPN1bAeei0lITLbFb4frtFWBGAvBLyw== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CHzppK64; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAC6D606A326; Fri, 23 Oct 2026 16:05:18 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id BAD6B1F01825; Fri, 23 Oct 2026 16:05:12 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1792771518; bh=UocDU7ImLSN8bUn3qE+8ros6Ps+AObI529MubY0oH/4=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=CHzppK641dtPEq3WhYoFez5r9mHJ+3OBQKq4TIHeNzjw38VFpwUEiF3RYFs9B3iua T85cXDpCsvvYQFzcXZjNlA5VpGfMv8kC7mjzqNFtQlEBCBzsNIQpL6SvLUg6AAmKVQ hzZOaEL+qqGjfnYwCbAZNLmDMU3MHmNqUTDldMWvcZ9TxRH1KHLR/ERa1VUdiTWsAc zMsfB4zdUqK46YYgfdifXHG6UtmADc4uub/3BdOP1sWPmZADl+OMSbrQg3PCqGHJTM XFQUfZ2roPk4Htm0v2LLF8bdsxUDS7pBFGoNmRf/KD3bniFMoebjqu+i6nvId9nuNF PH92+H5cd+9vw== X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-User-Agent: Mozilla Thunderbird X-Cc: bpf@vger.kernel.org, andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net, davem@davemloft.net, willemb@google.com, eddyz87@gmail.com, edumazet@google.com, john.fastabend@gmail.com, kuba@kernel.org, horms@kernel.org, liamwisehart@meta.com, martin.lau@linux.dev, pabeni@redhat.com, song@kernel.org, netdev@vger.kernel.org, sdf.kernel@gmail.com, yonghong.song@linux.dev, jolsa@kernel.org, kuniyu@google.com, memxor@gmail.com, emil@etsalapatis.com, martin.lau@kernel.org, mason@kernel.org, ihor.solodrai@linux.dev X-In-Reply-To: <atseCFvfx9FbTXP3@gmail.com>