From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 22C57372ED5 for ; Mon, 6 Jul 2026 11:12:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783336352; cv=none; b=DBrBfrSbsJZp6zuzfRWtZwjnuNCEAbXtx+MgXcMvDm9YAc8S/dBOZ9Wx0xbPPuOf8moTdtXtX+JyXX4iAHAhyHuCDpN/M4jaUmYKLinu89/zNMjWjdn7vd8G9TC7ednsjRj8f5hqGufbIYb0w5L2P3Hb9gsDmZHiyyzPzA4asPs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783336352; c=relaxed/simple; bh=v6VNb2kA3dX/6UZSsX1QTZQrVcfCyJaycbGUQAGD1Cg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=IqSb1EpgZV8FqZVmhRU2337Ir2ofj1A2vUwiCJklyG1/+7XS7w7KKh66O/PoS3IAPZNkcKxcwRtoCSCKzoRev3xzrDeCUv8bwT8xtWkwYg9SAT905nlMtRsoCTZEVWm5mfO9Nxli3LikuBvoaKf1jcSlO+PWTFzyYKRl3Dn8EGk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=trailofbits.com; spf=pass smtp.mailfrom=trailofbits.com; dkim=pass (2048-bit key) header.d=trailofbits.com header.i=@trailofbits.com header.b=P1ITyr/w; arc=none smtp.client-ip=209.85.222.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=trailofbits.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=trailofbits.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=trailofbits.com header.i=@trailofbits.com header.b="P1ITyr/w" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-92e6a434cabso141219485a.1 for ; Mon, 06 Jul 2026 04:12:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=trailofbits.com; s=google; t=1783336350; x=1783941150; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=c80RNcEdDYH5aFuHZOlETLwAydyaWG6HsrRsklQ+JMU=; b=P1ITyr/wi8I0RbYBcUNNOuRSMs9EsAkt3BH+slYltZ63mVFsdgeMEDEccVKtIYZvCV TMvoLMV3CiJjx+dbILpkBOlsYckbRc0wowagyjMEezJGGW1BFMnwerKBG0tjp18tKJ7B FDPWpKGlAtTY+d4bds9rDXc8yn44bsoX0S6bf/6CaOqow1Z6zVIX/wJL6/vpZzPwQaHl HVxpClpA5svdYBOgqQepNvGGM7Q21VR4va3hHVGtxNMfw63sBKIWwUZP5BYQcjzQyNN3 4mZ9sh+uDmINgZloX4MWNKVDeeIrdomYWXitH0TH7sgbSu1REpEtTr9FCXDbdbev6S9K fvtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783336350; x=1783941150; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=c80RNcEdDYH5aFuHZOlETLwAydyaWG6HsrRsklQ+JMU=; b=NVbRDAvycIT1B8JfuDKHr1OirRkG/TqAXrHoACHYOX/ybbYCIX0x4jLEEA/EFR6Czd jvqS5jumukNfrszof2QVYEEwwnmDxDH3zr+friZ4+qBULutSnFs0NU8mLqteEj4fWDpo aA5UL/CZJ2CMsZ5ahTBjkMCWi1x4j+Mr6/XTdhdaBobyVS56Y8bHNSMqLk5fXXCk8Fz3 EXtCgIipTj9TsEo3w5YVgNsaccI8qnq9nEgBFcqhLdlb/huiQVLmUEjCVbAa57nnQt7L O0KiD7c7W/z4PcL/3+ZT8VC0XaHidhhiFqtaZEvZb9ZP6O1KwfdwrWzovISOBqbJHmjd oRUA== X-Forwarded-Encrypted: i=1; AHgh+RoacRtlyHRm+OAf8D1MuWszjq+vZd6/XYltKjJdvX3mq2Yk6pw1Utr4a52JV2jjVavipMvud75P3KTiPb8=@vger.kernel.org X-Gm-Message-State: AOJu0YybDZ+DjqE+TaYkUdxeJ/GMXyH9qRKtAdyf5DUNWOG86chI3qqD 5me8AxzWZmx7MKCa3rLi1PC1Y+IeX2hWoCEr81UyUsBGm/gjEaiIYOn1TWAgYrJuFwE= X-Gm-Gg: AfdE7cmDyvxfnT+uauKRkqy3fW69y+JR6gsRcM+x0x5jyfvS2lKwRmKq3kP/N5yh9UW pqlhR9hDiyztmHr5M2f51WfYGCHmnncMuuSkzHke/s17L3xUZVP/aC11frVnY6ddk4HzlrPvNta yu6ecUYEC0DRyCboNJ98FuspT6b2qsCVXl5IHzzWG+etQTUMwrW9Dn6GYLV0mYKFk7TXSQvKv2L GpK2u+0OkggaczHh6/VTtAD2tlRdNKRMFNGETbS02mswpV58D6LKS1e1oZKkUBS2x3bdJA2MxNK IoUoU0bPOqXKHTJfC4b6TOO6smAj+XPo/Y135AuNwwrCoNjjz6aELX3jjQheKriNw1z1ZKky1nF UyFMv/8x46w9TU45Ke1bLSYMbFXBx+x73MYWqh0Z1u978QEd8067l6V0mWjM4tTNV0yKElpsQPx fnKZsTmeXXNWFmrKe2fA== X-Received: by 2002:a05:620a:1a20:b0:92e:4595:fde9 with SMTP id af79cd13be357-92ebb553528mr7548485a.38.1783336349931; Mon, 06 Jul 2026 04:12:29 -0700 (PDT) Received: from localhost ([146.190.222.192]) by smtp.gmail.com with UTF8SMTPSA id af79cd13be357-92e90ba41fesm926453285a.17.2026.07.06.04.12.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 Jul 2026 04:12:29 -0700 (PDT) From: David Lee To: Willem de Bruijn Cc: David Lee , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , "Dominik 'Disconnect3d' Czarnota" , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net v2] packet: avoid re-registering fanout hook after device unregister Date: Mon, 6 Jul 2026 11:12:23 +0000 Message-ID: <20260706111226.309506-1-david.lee@trailofbits.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit packet_set_ring() temporarily detaches a socket from packet delivery while reconfiguring its ring. It records the previous running state, clears po->num, unregisters the protocol hook when needed, drops po->bind_lock, and later restores po->num and re-registers the hook from the saved was_running value. That unlocked window can race with NETDEV_UNREGISTER. The notifier can observe the socket as not running, skip __unregister_prot_hook(), and invalidate the per-socket binding by setting po->ifindex to -1 and clearing po->prot_hook.dev. A one-member fanout group can still retain its shared fanout hook device pointer. When packet_set_ring() resumes, re-registering solely from the stale was_running state can re-add the fanout hook after the device has been unregistered. Treat po->ifindex == -1 as an invalidated binding after reacquiring po->bind_lock. Restore po->num as before, but do not re-register the hook if device unregister already detached the socket. Fixes: dc99f600698d ("packet: Add fanout support.") Reported-by: David Lee Signed-off-by: David Lee Assisted-by: Codex:gpt-5 --- Changes in v2: - Add Fixes and Reported-by tags. - Fix the incorrect Signed-off-by tag from v1. Trail of Bits has a PoC that achieves local privilege escalation using this bug on a custom kernel config with CONFIG_LIST_HARDENED disabled, which can be shared further if needed. net/packet/af_packet.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -4561,7 +4561,11 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, spin_lock(&po->bind_lock); WRITE_ONCE(po->num, num); - if (was_running) + /* + * NETDEV_UNREGISTER may have invalidated the binding while bind_lock + * was dropped above. Do not re-add a fanout hook to a dead device. + */ + if (was_running && READ_ONCE(po->ifindex) != -1) register_prot_hook(sk); spin_unlock(&po->bind_lock); -- 2.43.0