From nobody Thu Feb 6 19:11:14 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 4Ypmst2fgDz5mvQr; Thu, 06 Feb 2025 19:11:14 +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 4Ypmst24nQz41C2; Thu, 06 Feb 2025 19:11:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738869074; 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=wdYHQJ+VOc7mvMaCrimRuFK8jjiYQxN/eivqMiTB8AA=; b=IobfNkNXzJAmjm6tKLAfihhF2GlA19XJx2V5O+jQiITHcW/A3Z8+Amk5lflCmpwZs4xuYc vpH4glIYcoAXoVcqTyQHEwSGRZkCgQAK/bd2aUHzyukJVcdM7BYQ2FxBKU/ZFKDTNmiUsi 7VYhqpuccMoACgT3Ym3UcEDYPsgT78T09kNYi8qfXGV1CCeVMbWc6TEVsNASZHNKQCu6bH aUDKQoBHSGoCTZGSKFq0pjAzBREyg9dRb6S8BG7wjEIvHawEUIiL8fvBaSNqfW6phrnLhq rUsKcDhJZGZRueea5D5/+1DR46CXR7wPc/iFiQj/8VyioUPvuddjLVgj2RK/ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738869074; 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=wdYHQJ+VOc7mvMaCrimRuFK8jjiYQxN/eivqMiTB8AA=; b=wPv6Z7xgB8z/7YtNxwj+/JMCtKlGh6CQ0oO2xDG9uxQynsIAG5Sz4DskJa4SqXZdowr6S7 LjNliH/6dvshqouPnVAKvLtwLLUmTJnKTiwaj2snPL/IJHsfeix/aY057vnoP+O90MSg56 9J2siDq9Hvi3x8s8QcH0IxWBZ+xhRrJH7oZu1vb3oM7nKj3dJcisubUFUH7mtpM1/OtyjE 66j4ecMQcWBi/6auADizh033ujZNU5H2QT4yJ4bQ+89XqX4t4a25v2VhDoyX5lk+UPqGmZ wofiglXyLjGq8AwOTMXJX7oB2/WRALkvzWozmHbzNGa9OdnJpcQRAS2di3zOKg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1738869074; a=rsa-sha256; cv=none; b=cxTk3kgUJgriw7pDACWrGkK39s/vW8elc/lY+RrgEZ5ValWOUgyTIRBtn0hJXFmWQ/nkty uapypAFJNK3UotAWBh+AbLFP5ElEBlfU3kNM1jGpQXdqdbE/BmLDtu3Xm4u2lQvJSLJqpx wLGjSvujct9loiJoYWt0oaDLkeflgYKLWYjqRYkg9YtPNfi21Bb5hm6agyMULj5Swu0iqU wDS8X5PVJksw/yS5lA2SeGuSYTG9qfuFwiepovHsvn34914hHHmxbQURTKmC34JW8h2R0j Gl7j2vGIeMPSNOA49urQLSvCST8BIjVNimChEuOnMcVQPfrJplalUstb8pxZsg== 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 4Ypmst1YBXzr4P; Thu, 06 Feb 2025 19:11:14 +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 516JBE2M007832; Thu, 6 Feb 2025 19:11:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 516JBEtV007829; Thu, 6 Feb 2025 19:11:14 GMT (envelope-from git) Date: Thu, 6 Feb 2025 19:11:14 GMT Message-Id: <202502061911.516JBEtV007829@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: 56c93859038f - stable/14 - 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/stable/14 X-Git-Reftype: branch X-Git-Commit: 56c93859038ff3de9041834fc798b20dc213621b Auto-Submitted: auto-generated The branch stable/14 has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=56c93859038ff3de9041834fc798b20dc213621b commit 56c93859038ff3de9041834fc798b20dc213621b Author: Zhenlei Huang AuthorDate: 2025-02-03 12:13:19 +0000 Commit: Zhenlei Huang CommitDate: 2025-02-06 18:35:11 +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) --- 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 8a68f65a80f7..7c7f6e84d9ee 100644 --- a/sys/net/bpf.c +++ b/sys/net/bpf.c @@ -2092,10 +2092,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.