From: bot+bpf-ci@kernel.org
To: nicoyip.dev@gmail.com,pablo@netfilter.org,fw@strlen.de,phil@nwl.cc,davem@davemloft.net,edumazet@google.com,kuba@kernel.org,pabeni@redhat.com,horms@kernel.org,ast@kernel.org,daniel@iogearbox.net,hawk@kernel.org,john.fastabend@gmail.com,sdf@fomichev.me,memxor@gmail.com,lorenzo@kernel.org
Cc: netfilter-devel@vger.kernel.org,coreteam@netfilter.org,netdev@vger.kernel.org,linux-kernel@vger.kernel.org,bpf@vger.kernel.org,nicoyip.dev@gmail.com,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,clm@meta.com,ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf v3] bpf: Fix netns reference imbalance in conntrack kfuncs
Date: Fri, 31 Jul 2026 20:41:04 +0000 (UTC) [thread overview]
Message-ID: <1617752d86587cdab81d79caeab2cbbffb3ca997fd18cb68c56bf5f8214d7127@mail.kernel.org> (raw)
In-Reply-To: <20260731160921.3245840-1-nicoyip.dev@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 4374 bytes --]
> diff --git a/net/netfilter/nf_conntrack_bpf.c b/net/netfilter/nf_conntrack_bpf.c
> --- a/net/netfilter/nf_conntrack_bpf.c
> +++ b/net/netfilter/nf_conntrack_bpf.c
> @@ -122,42 +122,54 @@ __bpf_nf_ct_alloc_entry(struct net *net, struct bpf_sock_tuple *bpf_tuple,
> struct nf_conntrack_tuple otuple, rtuple;
> struct nf_conntrack_zone ct_zone;
> struct nf_conn *ct;
> + u8 ct_zone_dir = 0;
> + u16 ct_zone_id;
> + s32 netns_id;
> + u8 l4proto;
> int err;
>
> if (!(opts_len == NF_BPF_CT_OPTS_SZ || opts_len == 12))
> return ERR_PTR(-EINVAL);
> +
> + netns_id = READ_ONCE(opts->netns_id);
> + l4proto = READ_ONCE(opts->l4proto);
> + ct_zone_id = READ_ONCE(opts->ct_zone_id);
> if (opts_len == NF_BPF_CT_OPTS_SZ) {
> - if (opts->reserved[0] || opts->reserved[1] || opts->reserved[2])
> + ct_zone_dir = READ_ONCE(opts->ct_zone_dir);
> + if (READ_ONCE(opts->reserved[0]) ||
> + READ_ONCE(opts->reserved[1]) ||
> + READ_ONCE(opts->reserved[2]))
> return ERR_PTR(-EINVAL);
> } else {
> - if (opts->ct_zone_id)
> + if (ct_zone_id)
> return ERR_PTR(-EINVAL);
> }
>
> - if (unlikely(opts->netns_id < BPF_F_CURRENT_NETNS))
> + if (unlikely(netns_id < BPF_F_CURRENT_NETNS))
> return ERR_PTR(-EINVAL);
[ ... ]
> if (opts_len == NF_BPF_CT_OPTS_SZ) {
> - if (opts->ct_zone_dir == 0)
> - opts->ct_zone_dir = NF_CT_DEFAULT_ZONE_DIR;
> - nf_ct_zone_init(&ct_zone,
> - opts->ct_zone_id, opts->ct_zone_dir, 0);
> + if (ct_zone_dir == 0) {
> + ct_zone_dir = NF_CT_DEFAULT_ZONE_DIR;
> + opts->ct_zone_dir = ct_zone_dir;
> + }
> + nf_ct_zone_init(&ct_zone, ct_zone_id, ct_zone_dir, 0);
> } else {
> ct_zone = nf_ct_zone_dflt;
> }
[ ... ]
> @@ -186,46 +198,58 @@ static struct nf_conn *__bpf_nf_ct_lookup(struct net *net,
> struct nf_conntrack_tuple tuple;
> struct nf_conntrack_zone ct_zone;
> struct nf_conn *ct;
> + u8 ct_zone_dir = 0;
> + u16 ct_zone_id;
> + s32 netns_id;
> + u8 l4proto;
> int err;
>
> if (!opts || !bpf_tuple)
> return ERR_PTR(-EINVAL);
> if (!(opts_len == NF_BPF_CT_OPTS_SZ || opts_len == 12))
> return ERR_PTR(-EINVAL);
> +
> + netns_id = READ_ONCE(opts->netns_id);
> + l4proto = READ_ONCE(opts->l4proto);
> + ct_zone_id = READ_ONCE(opts->ct_zone_id);
> if (opts_len == NF_BPF_CT_OPTS_SZ) {
> - if (opts->reserved[0] || opts->reserved[1] || opts->reserved[2])
> + ct_zone_dir = READ_ONCE(opts->ct_zone_dir);
> + if (READ_ONCE(opts->reserved[0]) ||
> + READ_ONCE(opts->reserved[1]) ||
> + READ_ONCE(opts->reserved[2]))
> return ERR_PTR(-EINVAL);
> } else {
> - if (opts->ct_zone_id)
> + if (ct_zone_id)
> return ERR_PTR(-EINVAL);
> }
> - if (unlikely(opts->l4proto != IPPROTO_TCP && opts->l4proto != IPPROTO_UDP))
> + if (unlikely(l4proto != IPPROTO_TCP && l4proto != IPPROTO_UDP))
> return ERR_PTR(-EPROTO);
> - if (unlikely(opts->netns_id < BPF_F_CURRENT_NETNS))
> + if (unlikely(netns_id < BPF_F_CURRENT_NETNS))
> return ERR_PTR(-EINVAL);
[ ... ]
> if (opts_len == NF_BPF_CT_OPTS_SZ) {
> - if (opts->ct_zone_dir == 0)
> - opts->ct_zone_dir = NF_CT_DEFAULT_ZONE_DIR;
> - nf_ct_zone_init(&ct_zone,
> - opts->ct_zone_id, opts->ct_zone_dir, 0);
> + if (ct_zone_dir == 0) {
> + ct_zone_dir = NF_CT_DEFAULT_ZONE_DIR;
> + opts->ct_zone_dir = ct_zone_dir;
> + }
> + nf_ct_zone_init(&ct_zone, ct_zone_id, ct_zone_dir, 0);
> } else {
> ct_zone = nf_ct_zone_dflt;
> }
This isn't a bug, but both __bpf_nf_ct_alloc_entry() and
__bpf_nf_ct_lookup() now snapshot and validate the same five opts fields
in the same order with the same declarations (u8 ct_zone_dir = 0; u16
ct_zone_id; s32 netns_id; u8 l4proto;), and both carry the identical
zone-init block. The two functions now share about 25 lines of duplicated
code, and the patch grows each copy from roughly 8 lines to roughly 14.
Would it be worth factoring the snapshot and validation into a shared
helper so a future opts field only has to be handled once? One of the
Fixes targets, aed8ee7feb44, was itself a de-duplication of similar code
in this file, so the direction of travel here has been toward sharing
rather than copying.
---
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/30647492237
next prev parent reply other threads:[~2026-07-31 20:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-31 16:09 Chengfeng Ye
2026-07-31 20:41 ` bot+bpf-ci [this message]
2026-07-31 20:52 ` Emil Tsalapatis
2026-08-01 19:00 ` patchwork-bot+netdevbpf
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=1617752d86587cdab81d79caeab2cbbffb3ca997fd18cb68c56bf5f8214d7127@mail.kernel.org \
--to=bot+bpf-ci@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=clm@meta.com \
--cc=coreteam@netfilter.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=fw@strlen.de \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lorenzo@kernel.org \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=nicoyip.dev@gmail.com \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--cc=phil@nwl.cc \
--cc=sdf@fomichev.me \
--cc=yonghong.song@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
all inboxes | Powered by JetHome®