Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6370885rdb; Mon, 1 Jan 2024 22:12:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IELyZ+nN32pDKpt4LayQyainoGaHpy+DMbgRB3WgMe15Mw7NN0Ubs6koC66lAck6F0VqHok X-Received: by 2002:a05:6808:22a4:b0:3bc:1b95:35d9 with SMTP id bo36-20020a05680822a400b003bc1b9535d9mr164601oib.4.1704175944138; Mon, 01 Jan 2024 22:12:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704175944; cv=none; d=google.com; s=arc-20160816; b=Rk9DX9UgvQaNraOypkM6pvQYLCg83GZv7JpG4X66nFAlxDyHetuw97q0lKTvan9LLs JCRYIFdSdAjECzikt2be67Nj1E30VmJmVZ2LeY6XuzlsekrVcf44JbfsVs7kSbc5o/Iq qikSKhic4DV/il7nhg3SOfnCw3gyQX9Rj1pHI2lRLO3jtkk6lTeQTIxIyzf0TWgE2LG1 eBjLVoiInkLKk1qUc0wO9ETsE+TjuGFJhT89euy2cCwxNv/dZi+9HilHccFJQnrTQPMD +XegOwqjF+DQZETIQ2JpfNqbLBm7O36bprPR7ffHVH7Ah7wzCOKRgLlrbTYgW+vGsdUq TSFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from; bh=hS3Ro8pfbmONFEkXuIdGhBL5zvMCiDkGDydpUg8Kgto=; fh=0j9hEtKEGSulx/lgeQcpDl+4ujiVFr5DCPGOZLIRfAc=; b=zqZ8ACeXy32w1MmOv2VAp+0iUzV1TaUM625qVdlBXp4T8sijrUyqaCR92mp4Dr0sFe yELjhq7+fzTeKfrfM9keZoRME3Hx9nr5/PqYpxsVVn5nO02bsPtmKcO8pwAT4T6RiAEN HUSe8PlDQe+/CR+V8Pbt3CnJSferskHAzK2O6YG2kLDj5TPVY4DQNDsZWZedWTW0PhaL qjUY/3b3TwJAd2Rd2PZoOzeO5Cor2tuBFxS7K6+ygwcWJTkdYVpPEx0YHdXP2w784V0E 3gT1TZO+DyGWuvpu6BdJYPpEREOEA1tXBO07ABbE7E5j/u9393yxqnwEuFfXqNgKSrHC r5LQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-14075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14075-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id az12-20020a05620a170c00b0076f19d66ddfsi28169575qkb.138.2024.01.01.22.12.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 22:12:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-14075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14075-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 9C0D21C21380 for ; Tue, 2 Jan 2024 06:12:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6BEB663CB; Tue, 2 Jan 2024 06:11:33 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E26884C84; Tue, 2 Jan 2024 06:11:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R651e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=alibuda@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0VzmJOEx_1704175881; Received: from j66a10360.sqa.eu95.tbsite.net(mailfrom:alibuda@linux.alibaba.com fp:SMTPD_---0VzmJOEx_1704175881) by smtp.aliyun-inc.com; Tue, 02 Jan 2024 14:11:21 +0800 From: "D. Wythe" To: pablo@netfilter.org, kadlec@netfilter.org, fw@strlen.de Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, coreteam@netfilter.org, netfilter-devel@vger.kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, ast@kernel.org Subject: [RFC nf-next v5 1/2] netfilter: bpf: support prog update Date: Tue, 2 Jan 2024 14:11:16 +0800 Message-Id: <1704175877-28298-2-git-send-email-alibuda@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1704175877-28298-1-git-send-email-alibuda@linux.alibaba.com> References: <1704175877-28298-1-git-send-email-alibuda@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: From: "D. Wythe" To support the prog update, we need to ensure that the prog seen within the hook is always valid. Considering that hooks are always protected by rcu_read_lock(), which provide us the ability to access the prog under rcu. Signed-off-by: D. Wythe --- net/netfilter/nf_bpf_link.c | 50 ++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/net/netfilter/nf_bpf_link.c b/net/netfilter/nf_bpf_link.c index e502ec0..47bbdf1 100644 --- a/net/netfilter/nf_bpf_link.c +++ b/net/netfilter/nf_bpf_link.c @@ -8,26 +8,26 @@ #include #include -static unsigned int nf_hook_run_bpf(void *bpf_prog, struct sk_buff *skb, - const struct nf_hook_state *s) -{ - const struct bpf_prog *prog = bpf_prog; - struct bpf_nf_ctx ctx = { - .state = s, - .skb = skb, - }; - - return bpf_prog_run(prog, &ctx); -} - struct bpf_nf_link { struct bpf_link link; struct nf_hook_ops hook_ops; struct net *net; u32 dead; const struct nf_defrag_hook *defrag_hook; + struct rcu_head head; }; +static unsigned int nf_hook_run_bpf(void *bpf_link, struct sk_buff *skb, + const struct nf_hook_state *s) +{ + const struct bpf_nf_link *nf_link = bpf_link; + struct bpf_nf_ctx ctx = { + .state = s, + .skb = skb, + }; + return bpf_prog_run(rcu_dereference_raw(nf_link->link.prog), &ctx); +} + #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) static const struct nf_defrag_hook * get_proto_defrag_hook(struct bpf_nf_link *link, @@ -126,8 +126,7 @@ static void bpf_nf_link_release(struct bpf_link *link) static void bpf_nf_link_dealloc(struct bpf_link *link) { struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); - - kfree(nf_link); + kfree_rcu(nf_link, head); } static int bpf_nf_link_detach(struct bpf_link *link) @@ -162,7 +161,22 @@ static int bpf_nf_link_fill_link_info(const struct bpf_link *link, static int bpf_nf_link_update(struct bpf_link *link, struct bpf_prog *new_prog, struct bpf_prog *old_prog) { - return -EOPNOTSUPP; + struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); + int err = 0; + + if (nf_link->dead) + return -EPERM; + + if (old_prog) { + /* target old_prog mismatch */ + if (cmpxchg(&link->prog, old_prog, new_prog) != old_prog) + return -EPERM; + } else { + old_prog = xchg(&link->prog, new_prog); + } + + bpf_prog_put(old_prog); + return err; } static const struct bpf_link_ops bpf_nf_link_lops = { @@ -226,7 +240,11 @@ int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) link->hook_ops.hook = nf_hook_run_bpf; link->hook_ops.hook_ops_type = NF_HOOK_OP_BPF; - link->hook_ops.priv = prog; + + /* bpf_nf_link_release & bpf_nf_link_dealloc() can ensures that link remains + * valid at all times within nf_hook_run_bpf(). + */ + link->hook_ops.priv = link; link->hook_ops.pf = attr->link_create.netfilter.pf; link->hook_ops.priority = attr->link_create.netfilter.priority; -- 1.8.3.1