From nobody Mon Feb 3 12:17:02 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YmlqM09Zgz5n6P1; Mon, 03 Feb 2025 12:17:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YmlqL5vHPz3xZl; Mon, 03 Feb 2025 12:17:02 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738585022; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dJK277PoiTE+XSfXA4OfjrwFzKTwBoloGUhej8dCAiA=; b=r3NOEO8OLiouVy3DAjzygeKRg/I6nU1v7xv3AbpQwAMuVulg1t9X2jsACMLnbplLH1cpKt 0sIsHmtZXQDhMh7TBKr9QFMinFm18TPZ5hABDfCO3Tg2kuA3DX0juE1h/sV6swn/RVS2md XSYFz91Q4uGIOgfSwb32QEhL5EdnuE1MMNIu+Bvz/vhsCMETj7GFYb30bdPnS9LRoSEaSK OVSyT9ZAyA8gNNEOAzCrDdka7xdw1Z+wf4yafRW8CpUnJSFSgYwW65BgpDEwF83TG2DkJC tIQ8gtj+T2fjf3LNvw10ls4FWiHjWaZY3aGTqEgHIYrsi6peJE6pBAWwYCwrnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738585022; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dJK277PoiTE+XSfXA4OfjrwFzKTwBoloGUhej8dCAiA=; b=s7RSGmVAwYmICi51ttddm/aeRq7vRPh76UEzszx5EFbLOtdDw01/WLZSFQDQCEP+YrhdhH okX/OPDJli8Aho7IcGay4YECGur1xl1mv+47Jf2kuFn3k8neJiZrEq1oUJBZOR4V0f+FhK /XYj83xhX1VtqMK3b+uYq/UfeEoU+g1PCYgrjfvtb5DhtmMhEGSDy5OxNrXmhJTRjVnjoC +kQ8+QRbfX9EA2/cjzeNxErCUu59wJFN9m2sGAOuthYoQi1EL+X7jov9CudpBgwRJZmuAH bZAN4AKJImWYfV5BMhEfGS67Wr83z6TeXoHuK4bUNL2SK8hRF8LTx8FjRV1RVg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1738585022; a=rsa-sha256; cv=none; b=jnWrEqG0Xr6YjwWiX7n28vJpQRG34Amj0dx+D3aDS6GcLIHFmTZtxn7FbYTKYifatOjSj6 AT2LsGO78UHEd8CQJunKCijst1Zu+33IAMpapSH2udD2tPAA+lcQ1vIZE14o1Gi7/eqdxF iZesriwLGle3NktS8zf/RBs5qMjM21Rac2fxXhJAecAF+EUnU32N8NiyJGJ2wWNJxYSFgC M5NsinDl5zmCBKAqROLoeY0V8rhQDb9O9c16Bkiwwp0z87tbC0jt5U0Nuq5QgLudOJ/gtt vM0jzm9wYhMFzjui9Ma/jFeC0i3oLVSVNU7ZboPiGZcsnszFuC+1+0Y53SV/Gw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4YmlqL5SpQzc5c; Mon, 03 Feb 2025 12:17:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 513CH2cG081363; Mon, 3 Feb 2025 12:17:02 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 513CH2e8081360; Mon, 3 Feb 2025 12:17:02 GMT (envelope-from git) Date: Mon, 3 Feb 2025 12:17:02 GMT Message-Id: <202502031217.513CH2e8081360@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Zhenlei Huang Subject: git: 7def047a1ae9 - main - bpf: Fix potential race conditions List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: zlei X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7def047a1ae93b3b10bd57ed1bd28e861f94b596 Auto-Submitted: auto-generated The branch main has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=7def047a1ae93b3b10bd57ed1bd28e861f94b596 commit 7def047a1ae93b3b10bd57ed1bd28e861f94b596 Author: Zhenlei Huang AuthorDate: 2025-02-03 12:13:19 +0000 Commit: Zhenlei Huang CommitDate: 2025-02-03 12:13:19 +0000 bpf: Fix potential race conditions There're two possible race conditions, 1. Concurrent bpfattach() and bpf_setif(), i.e., BIOCSETIF ioctl, 2. Concurrent bpfdetach() and bpf_setif(). For the first case, userland may see BPF interface attached but it has not been in the attached interfaces list `bpf_iflist` yet. Well it will eventually be so this case does not matter. For the second one, bpf_setif() may reference `dead_bpf_if` and the kernel will panic (spotted by change [1], without the change we will end up silently corrupted memory). A simple fix could be that, we add additional check for `dead_bpf_if` in the function `bpf_setif()`. But that requires to extend protection of global lock (BPF_LOCK), i.e., BPF_LOCK should also protect the assignment of `ifp->if_bpf`. That simple fix works but is apparently not a good design. Since the attached interfaces list `bpf_iflist` is the single source of truth, we look through it rather than check against the interface's side, aka `ifp->if_bpf`. This change has performance regression, that the cost of BPF interface attach operation (BIOCSETIF ioctl) goes back from O(1) to O(N) (where N is the number of BPF interfaces). Well we normally have sane amounts of interfaces, an O(N) should be affordable. [1] 7a974a649848 bpf: Make dead_bpf_if const Fixes: 16d878cc99ef Fix the following bpf(4) race condition ... MFC after: 4 days Differential Revision: https://reviews.freebsd.org/D45725 --- sys/net/bpf.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sys/net/bpf.c b/sys/net/bpf.c index 657a34827bdb..a7d17109ed1a 100644 --- a/sys/net/bpf.c +++ b/sys/net/bpf.c @@ -2090,10 +2090,20 @@ bpf_setif(struct bpf_d *d, struct ifreq *ifr) BPF_LOCK_ASSERT(); theywant = ifunit(ifr->ifr_name); - if (theywant == NULL || theywant->if_bpf == NULL) + if (theywant == NULL) + return (ENXIO); + /* + * Look through attached interfaces for the named one. + */ + CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) { + if (bp->bif_ifp == theywant && + bp->bif_bpf == &theywant->if_bpf) + break; + } + if (bp == NULL) return (ENXIO); - bp = theywant->if_bpf; + MPASS(bp == theywant->if_bpf); /* * At this point, we expect the buffer is already allocated. If not, * return an error.