From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5FEFC433EF for ; Fri, 27 May 2022 12:22:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245342AbiE0MWl convert rfc822-to-8bit (ORCPT ); Fri, 27 May 2022 08:22:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345421AbiE0MVk (ORCPT ); Fri, 27 May 2022 08:21:40 -0400 Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [IPv6:2a0a:51c0:0:12e:520::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFB465F27B; Fri, 27 May 2022 05:02:31 -0700 (PDT) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1nuYfZ-0003yW-RK; Fri, 27 May 2022 14:02:17 +0200 Date: Fri, 27 May 2022 14:02:17 +0200 From: Florian Westphal To: Kumar Kartikeya Dwivedi Cc: Florian Westphal , Lorenzo Bianconi , bpf@vger.kernel.org, netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, davem@davemloft.net, kuba@kernel.org, edumazet@google.com, pabeni@redhat.com, pablo@netfilter.org, netfilter-devel@vger.kernel.org, lorenzo.bianconi@redhat.com, brouer@redhat.com, toke@redhat.com, yhs@fb.com Subject: Re: [PATCH v4 bpf-next 06/14] bpf: Whitelist some fields in nf_conn for BPF_WRITE Message-ID: <20220527120217.GG7680@breakpoint.cc> References: <2954ab26de09afeecf3a56ba93624f9629072102.1653600578.git.lorenzo@kernel.org> <20220526214558.GA31193@breakpoint.cc> <20220527113343.h3q5zmkmqm7fev7r@apollo.legion> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: 8BIT In-Reply-To: <20220527113343.h3q5zmkmqm7fev7r@apollo.legion> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Kumar Kartikeya Dwivedi wrote: > On Fri, May 27, 2022 at 03:15:58AM IST, Florian Westphal wrote: > > Lorenzo Bianconi wrote: > > > From: Kumar Kartikeya Dwivedi > > > > > > Since we want to allow user to set some fields in nf_conn after it is > > > allocated but before it is inserted, we can permit BPF_WRITE for normal > > > nf_conn, and then mark return value as read only on insert, preventing > > > further BPF_WRITE. This way, nf_conn can be written to using normal > > > BPF instructions after allocation, but not after insertion. > > > > > > Note that we special nf_conn a bit here, inside the btf_struct_access > > > callback for XDP and TC programs. Since this is the only struct for > > > these programs requiring such adjustments, making this mechanism > > > more generic has been left as an exercise for a future patch adding > > > custom callbacks for more structs. > > > > Are you sure this is safe? > > As far as I can see this allows nf_conn->status = ~0ul. > > I'm fairly sure this isn't a good idea, see nf_ct_delete() for example. > > This only allows writing to an allocated but not yet inserted nf_conn. The idea > was that insert checks whether ct->status only has permitted bits set before > making the entry visible, and then we make nf_conn pointer read only, however > the runtime check seems to be missing right now in patch 12; something to fix in > v5. With that sorted, would it be fine? Its fragile, e.g. what if I set TEMPLATE bit? If refcount goes down to 0, object is released via kfree() instead of kmem_cache_free. What if I clear SNAT_DONE bit? Would it leave the (freed) entry on the bysource hash list (see nf_nat_core.c)? Or is there some magic that prevents this from happening? I have no idea how processing pipeline looks like...