From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795920394; b=ZH2DJqnLYLZ15tMKFpGFzcuBPrax/dnYZXzlnFjlPEW22r7CNH4aFEHfuuQtzhpX8I/B j8OVcQLJ22MF03y4mZoJDX/r/LyF//tMYU9/C+7ycZWmvUwDmimMWoOdGp7UIc8cUtF6z 07hePAOT59D61N1yXrMFR/7gCaceW31AUuptfibFmN64NS+7W7+YHDnEPBRcFVWqgQ5/p mSPSidhtT/PI1L40CuhnTN2tffTLYipNfYB3h3mkhunsz0Pqjo/LTTy6prE+urYJmAAEE B3W9GHHonq0EsHanI1OEnDwF7n/XKvf1K+pREam0Nf3Xhw6ItZTLNU3cGfyOsh+tHVA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795920394; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=iCBO3qwn5rUxg6UMNURDb1GuigS3WeB7o1xl0f8SM3I=; b=Fn+wOcY8MiZCGJ9ezXkFKTIrDD35SH4rL/Ez8xRGB3Y5sIPeztnI1646GZSGitFmBIbV lc28nYW1QF2RHWBNCwSSnurKntu3Sd9nhSGiw8Btfok3acDvFvsCcnVgoYaEW8T13ibgB jtv+rZAewfOXgQWEDAno9GloN+NPg9NyxM4E9+bJPIhd3jUrWDn3eueMbHq1w1wEJuYxG EV4cJIIsLHobXGKv3pgjNEkJuVtvIU/upPfbVNUFEExyw2Dp1LOcJWDdHALQK10CbSB4A H1gVZ00RrM7XeYY4lZOZA7rHHhpbQlhAp1tQA6wXTmmXYEibNSbzI7OYFty73lUb4AA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=W3SZH45K; arc=none smtp.remote-ip=209.85.214.198 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CA75606A31C for ; Sun, 29 Nov 2026 02:46:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0CA75606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zhenwenliang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=W3SZH45K Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2f033dc37cfso86564965ad.0 for ; Sat, 28 Nov 2026 18:46:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795920393; x=1796525193; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=iCBO3qwn5rUxg6UMNURDb1GuigS3WeB7o1xl0f8SM3I=; b=W3SZH45K+cDqW+DmMyWSdv2pa5bNvrKpGFhfYnPKcq/V3xA30pE1zhfkczcrGwKN5K IEM/BVVYaKVlYXR5sVGf0gEUr11l9FWU9YvxylcaGP2PlrCpRj0VukFaPt/a6fCFRZgw clZy4rvNbECyj0+xB/6dDzXgP5s6tVu8M6C0FoUfeAsyLKF6gH5w0gm3dJ74lgontS3u 6WRAh3QuiLWtKtwZcz21KRSsUXFF7kORBjc6Ptswnh6EJyNXzI1QqpuhGxcbRuy5AKWh fH17KYxBT33jNpZ/DCcABOSP4pDGC89LSH/23d99bCn6ekQVLS3H8Vqk3gwRF9C6CLY/ Qldw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795920393; x=1796525193; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=iCBO3qwn5rUxg6UMNURDb1GuigS3WeB7o1xl0f8SM3I=; b=wjbwRFVpawjfeSYmUH3zHusFmOLUTpjj0dd1ewM4jUIKTjyWYPzEmOgrq23v8GLSbm 8GtJvmWAovFn1Nx7TGPgg7T7gymz65UR71APXU6GL1gpyA1VG+Ucr1cCBzBLGvLFHui0 SnSAfRbrRuEf5Q/TxmloOcQIoiYOmuEuk8GEnAxrGwVmy3BFNm7aK7zr7uoGMyQ3Syvh bEdhuyYkoOFTTmALLWccN1yLPYNQjHWhWLiyUmvNVzoQv1RYEcQqSBTmDOeNiNKZES4R ok+mNJjj7Kc3kFHJ8UhaAaY1hH98Zt19eELLyYMv8yVt9lWCJugyHBjA+xY/XrlBdp39 eSKg== X-Forwarded-Encrypted: i=1; AL+hErGoPyvSlTC/mpE2AeB2Gr4pQ30vOJxzyeFM52jEyOifUGodFmv/oWk9HOtfoLMl8Ajp/8YQPMr+b02yfTQ=@vger.kernel.org X-Gm-Message-State: APuU4Z+zky7TMawX21DaeFnMncZLS6eySNszg7wd2hhl8ZotOQJcUP9A PBX3WKkpAGo18krjixwMbGmhQQNhVUQmUWgI9kwKOuITodIYiw4Uf/v+KDFsCKuRlMYUitr8fgy mynbUjTCVCMHVH381meTPSDhn1RM5iw== X-Received: from plwp3.prod.google.com ([2002:a17:903:2483:b0:2ef:f380:a66c]) (user=zhenwenliang job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:26ce:b0:2ed:3d3b:c5f5 with SMTP id d9443c01a7336-2f0f4a36319mr107092465ad.64.1795920392880; Sat, 28 Nov 2026 18:46:32 -0800 (PST) Date: Sun, 29 Nov 2026 02:46:31 +0000 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261129024631.271472-1-zhenwenliang@google.com> Subject: [PATCH] seccomp: Use wq_has_sleeper() when an unused filter wakes its waiters From: Zhenwen Liang To: Kees Cook Cc: Andy Lutomirski , Will Drewry , Christian Brauner , Andrei Vagin , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" __seccomp_filter_orphan() drops the last user of a filter with refcount_dec_and_test() and wakes the notifier's waiters only if waitqueue_active() sees them. refcount_dec_and_test() provides release ordering, and acquire ordering when it returns true, but it is not a full barrier: its store to filter->users is not ordered before the later lockless load in waitqueue_active(). The waiters add themselves to filter->wqh before they check users. recv_wait_event() calls prepare_to_wait_event(), whose set_current_state() is a full barrier, and then refcount_read(); seccomp_notify_poll() calls poll_wait(), which ends with smp_mb(), and then refcount_read(). As the comment above waitqueue_active() explains, the waker needs a full barrier too. Without it, a waiter can read the old value of users while the waker sees an empty wait queue, and no wakeup is sent. SECCOMP_IOCTL_NOTIF_RECV can then sleep until a signal arrives; this applies since commit 95036a79e7b5 ("seccomp: interrupt SECCOMP_IOCTL_NOTIF_RECV when all users have exited"), which made it return once the filter is unused. A poll(), select() or epoll waiter can likewise miss EPOLLHUP and sleep until a timeout, a signal or another event. Use wq_has_sleeper(), which issues smp_mb() before it checks the wait queue. This covers both waiters, since they share this waker. smp_mb__after_atomic() is not used here: it is defined for the atomic_t operations, not for the refcount_t API. For both waiters, the outcome is allowed by the Linux kernel memory model for a litmus test derived from the code, and by the AArch64 and PPC architecture models for a litmus test of the same pattern. The fix was checked with herd7 on litmus tests derived from the patched code (LKMM, AArch64, PPC models: forbidden). Maintainers may want this change for stable. The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch. Compile-tested on arm64 (clang) and x86-64 (gcc) with W=1; not runtime-tested. Fixes: 99cdb8b9a573 ("seccomp: notify about unused filter") Signed-off-by: Zhenwen Liang --- kernel/seccomp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 3eecba5ccdef..1f48bfdb3076 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -534,7 +534,7 @@ static inline void seccomp_filter_free(struct seccomp_filter *filter) static void __seccomp_filter_orphan(struct seccomp_filter *orig) { while (orig && refcount_dec_and_test(&orig->users)) { - if (waitqueue_active(&orig->wqh)) + if (wq_has_sleeper(&orig->wqh)) wake_up_poll(&orig->wqh, EPOLLHUP); orig = orig->prev; } -- 2.56.1.839.g22a8290db9-goog