From nobody Thu Feb 06 19:18:20 2025 X-Original-To: dev-commits-src-branches@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 4Ypn262qcpz5mvnp; Thu, 06 Feb 2025 19:18:22 +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 4Ypn250yDwz43WZ; Thu, 06 Feb 2025 19:18:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738869501; 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=mev52bWoKDTm/BB9ATlFYzUski95xBtk+Q9CkMrpAjM=; b=hLX+CUxvpCr6NeSjaoXDNyz1O+pBb6iw56fcXwl8kX1Leld896EAuKP/NhVIJZR/HyNhj9 Vzh4shlzV4iNxs3BnFLBlFXWskB3dbKa5Y5o/Ms9Igcr+D64QDmsqtBP8U2Su6WGijpksx Z5pQNqUGUb80FIZeXjmyy5TIZXqmqRSXfV7W8mDpflX2BdL7hjbTf30glHwKnMMwJsb5wN cZxM69w2Q5m+FifHfQ3sdKyyTu6MK6R+SDkrQYtYr3x2X7Fw1cE2mLUzP6Kqra+Es/dv5m pLbMtWY9V1VA/E62TwxhXGEpPZrID8YoLHhWQvRZRWmWtcDdXYV8CU4XgqcOYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738869501; 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=mev52bWoKDTm/BB9ATlFYzUski95xBtk+Q9CkMrpAjM=; b=FNYWI8wLo5UPbCu11OWO7KhX3V7Q4TIDdKZ92DJYdtEZ3TNwYnF4Wa6d7sHTXOiNpBR8Dw cCjtN/B0tQgQH3x/IK35wmFqVkxkkxc2B4ImVWh7kR2a+x7UYMMSyIAAqo5zpYgs2ubbuD L8BSjHyR2+3XXOvMUOK5/sEizkEeLU6zNUQ5Ti8lLWzXBQidbiMXqTIOD4cnPN6MZ2eoxU kHInyOIxVQJZXL5nadDg19D3Uw2XuaSXvOQsMtHcTmcaoJXgCCnUqv6BCX+El24cgwbsSP FwM0Do+Vf8K9U/wpUoVknEsiRqROGt4vJaT9i6c4NwgDpvmc6CkZivgUSJCowQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1738869501; a=rsa-sha256; cv=none; b=WBIzSxBXby3e+7ik7snpd7fTRW5KXlyc7kn5YKs71CKtrSHQiVR0HVrDSeMN22XGnNBAZU myubS99FpEI+QyULxyMSpzBCE/YiDHns/1qf1/hqfRaBmZ8Nz2tX09YA69djPstRmk3+Ca L5hQcZPZ5rBBG37QMA3iE5KjZIbj1n2yJ03Ws/obFmghZQvlYj1VaZcfznjD8Qeoiiw7sL ZsHcmhO4vYj+tMX/SaydnSKg0bRKwOXF7JqIJSi0WAy9rNkZKpkxME7nBRqSJLrRRB0+Fq 4hS9YwfU7nusPY7b0xB8tyxqF31y+I9FFcaK8RI5ie0iIluZh/hH2rHZqzn1Hw== 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 4Ypn2473Qgzr1L; Thu, 06 Feb 2025 19:18:20 +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 516JIKdQ015550; Thu, 6 Feb 2025 19:18:20 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 516JIKQr015547; Thu, 6 Feb 2025 19:18:20 GMT (envelope-from git) Date: Thu, 6 Feb 2025 19:18:20 GMT Message-Id: <202502061918.516JIKQr015547@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Zhenlei Huang Subject: git: 19142d2a9600 - stable/13 - bpf: Fix potential race conditions List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 19142d2a9600589d57df3b1a983b51b43065c0c1 Auto-Submitted: auto-generated The branch stable/13 has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=19142d2a9600589d57df3b1a983b51b43065c0c1 commit 19142d2a9600589d57df3b1a983b51b43065c0c1 Author: Zhenlei Huang AuthorDate: 2025-02-03 12:13:19 +0000 Commit: Zhenlei Huang CommitDate: 2025-02-06 18:49:48 +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 (cherry picked from commit 7def047a1ae93b3b10bd57ed1bd28e861f94b596) (cherry picked from commit 56c93859038ff3de9041834fc798b20dc213621b) --- 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 b7c964ac8ac4..70232ffd75bd 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.