netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Westphal <fw@strlen.de>
To: Florian Westphal <fw@strlen.de>
Cc: pablo@netfilter.org, netfilter-devel@vger.kernel.org,
	Lorenzo Bianconi <lorenzo@kernel.org>
Subject: Re: [PATCH RFC] netfilter: nf_tables: add flowtable map for xdp offload
Date: Thu, 9 Nov 2023 12:09:28 +0100	[thread overview]
Message-ID: <20231109110928.GB26681@breakpoint.cc> (raw)
In-Reply-To: <20231102083042.GB6174@breakpoint.cc>

Florian Westphal <fw@strlen.de> wrote:

Pablo, I am going to make changes to the flowtable infra, it would
be nice if you could nack/ack the following approach before I start to
spend cycles on this:

> Florian Westphal <fw@strlen.de> wrote:
> This is fine or at least can be made to work.
> 
> > +	case FLOW_BLOCK_UNBIND:
> > +		nf_flowtable_by_dev_remove(dev);
> 
> This is broken.  UNBIND comes too late when things are torn down.
> 
> I only see two solutions:
> 
> 1. add a new nf_flow_offload_unbind_prepare() that does this
> 2. Decouple nf_flowtable from nft_flowtable and make nf_flowtable
>    refcounted.  As-is, the UNBIND will result in UAF because the
>    underlying structures will be free'd immediately after this,
>    without any synchronize_rcu().

I'll go with 2).  Rough Plan is:

1. Do not embed nf_flowtable into nft_flowtable or the act_ct struct,
   make this a pointer, which is allocated/freed via kmalloc/kfree.

2. add a refcount_t to nf_flowtable, so the nf_flowtable can have
   its reference incremented for as long as an XDP program might
   be using this.

3. Change nf_flowtable_free so that it will honor the reference count.
   Last _put will queue destruction to rcu worker, so the teardown of
   the rhashtable and other items passes through another
   synchronize_rcu().  This will also move kfree() of nf_flowtable into
   nf_flowtable_free.

4. Refactor the nf_flowtable init function so it will allocate and
   return the flowtable structure.

Only alternative I see is to rework both act_ct and nf_tables_api.c
to perform UNBINDs *before* synchronize_rcu (and the flowtable
teardown).

Doing that means that the xdp prog will not be able to 'pin' the
underlying nf_flowtable (as its embedded in another data structure,
either tcf_ct_flow_table or nft_flowtable), and would have to rely
on the callers to prevent the kfunc from every returning an nf_flowtable
that has already been free'd.

Not a problem now, but would be a problem in case bpf would gain
the ability to expose struct nf_flowtable as a refcounted kptr.

  reply	other threads:[~2023-11-09 11:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-19 20:25 [PATCH RFC] netfilter: nf_tables: add flowtable map for xdp offload Florian Westphal
2023-10-23 10:33 ` Lorenzo Bianconi
2023-10-23 11:16   ` Florian Westphal
2023-11-02  8:30 ` Florian Westphal
2023-11-09 11:09   ` Florian Westphal [this message]
2023-11-02 10:49 ` Toke Høiland-Jørgensen
2023-11-02 10:54   ` Florian Westphal
2023-11-02 11:07     ` Toke Høiland-Jørgensen
2023-11-02 11:11       ` Florian Westphal
2023-11-02 11:07   ` Florian Westphal
2023-11-02 11:25     ` Toke Høiland-Jørgensen

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=20231109110928.GB26681@breakpoint.cc \
    --to=fw@strlen.de \
    --cc=lorenzo@kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.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;
as well as URLs for NNTP newsgroup(s).