From nobody Fri Dec 19 19:00:34 2025 Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) (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 C936F33E358 for ; Thu, 6 Nov 2025 15:56:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762444620; cv=none; b=JR1PVt9MjUmUN9wTZSHazZplPnUGaS91wngmXmDajr8GmfgtSIXx0CXJCKasSMYLNZ4GC54fYe8NG4cHUkChwCqFs4QSPNCpfv7jsVlq4dVCzOXRobpfxJSbwOmnV2tjelYH00JcUBdXdHQIgL7lzkON4mTUE6m0g75D5ID5PyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762444620; c=relaxed/simple; bh=PBufnVhc0vYw6K1yqEtQER08QOskv29ELpqGFdftBe8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oOH9RDuLbiFd5YA314IldsCxDZHoj2LGp77tnY24tA6n9vrSUjk2Icpy9VR3XCWMfKPK2ABkPAwwvZHsgUMVCZxWen9EFAWa9tjRTWxaZTuEvHXknka64bN9dhq23sEpei8ggsv21v7wuVLHGaSYGu2ptqOxaihxkEy8m+yLKDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.208.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-640b0639dabso2294226a12.3 for ; Thu, 06 Nov 2025 07:56:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762444617; x=1763049417; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GjGKFyPmmnOORJmfJYb79vkMgO3eaKsfbWlFTGwHffE=; b=iu+pRd5nQb+l0yyQhp8vnIDzMMhIbwVVJKQl6a0QcOZVouziK3QvXrtmk4Ab6rg9xH Jij8AW5UlNR9ctXINSLZlsZ7rjfpplec16J1jmLLhFCoze+mzA2fP5SALD+M5jBH22Oq B3G2TFwmJd5bRb/2o8ywp87X7h9R3DMSnG4mOMgY6OPC4CCCcG1jsjeLdZbbVicihAU1 m7fSRrKdU7eTKjLQtQDE6F+bae/k5KbkhLFWnhsnQtERYKaqVcTXVBz6mtSe4fY2B+LF 6ss7vXmFX8LU9o0N/EFUIPoQ0+5crbsZuhgHm3zl0qN+df4AaXOA/m6qKSZez0udo9LQ A3rg== X-Gm-Message-State: AOJu0YxWbQGLWx7Q/ATtPuoRg6zNQ5imZEBaW5V3tOvHZVHExr3dSVw2 fHMS4/uC42K+0YQ5zi7mVBQxZqrTCo8e7+HSePgnwwWOJYpjS7o8kvRE X-Gm-Gg: ASbGncvnS4xXx1fPrPFOKCPLEzluefglkoaQIG2O8JvZLc3gXm2bWohwRk5Jacde6jJ FBbjqwnUqrB3U/sMP5+g1lb+3ogxPD9QuU6hyU/ys+tWTX+N/0edo2K7o7RgKe1PJbfCzgfnQF7 UInGCO4wETVyWdFtsqcOA0TYwkjphNMAFnxHx/WLTlWJAsPQBIA9yi0Pt2NwZr+cG1Yto4BdyvT /oO6VpsMzskHR5NYIQ8ht4EVO9lIzEVIYKDyfK8Z7MEJtrHVa+j1HjvkL0/wze5avetg+gFj3sT dIDcR/7mYXs1by3tyEcg6c/3cFaOoxZVMnxOzeNQEhW2shKsvYxkdG42f5WbP9WJXq6+MHYHEJ0 okWImggC6wgm2PiF6ZGnOE2H5LZFs/RiMvfiq1eG5YZBiKaPvA8K7XONXKmyI0l1FoCu8PNR/9w bTyA== X-Google-Smtp-Source: AGHT+IE1I3KsdrFfPzR8ZbLblQmlN8xA2lL4chWTQNh9/e/sKcWfQSx/nPRWrqzoh4RnHnBtbfozTg== X-Received: by 2002:a17:906:f855:b0:b72:95ff:3996 with SMTP id a640c23a62f3a-b7295ff4f7amr227875066b.10.1762444616910; Thu, 06 Nov 2025 07:56:56 -0800 (PST) Received: from localhost ([2a03:2880:30ff:73::]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b7289334090sm250482366b.13.2025.11.06.07.56.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Nov 2025 07:56:56 -0800 (PST) From: Breno Leitao Date: Thu, 06 Nov 2025 07:56:47 -0800 Subject: [PATCH net v9 1/4] net: netpoll: fix incorrect refcount handling causing incorrect cleanup Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251106-netconsole_torture-v9-1-f73cd147c13c@debian.org> References: <20251106-netconsole_torture-v9-0-f73cd147c13c@debian.org> In-Reply-To: <20251106-netconsole_torture-v9-0-f73cd147c13c@debian.org> To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , Simon Horman , david decotigny Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, asantostc@gmail.com, efault@gmx.de, calvin@wbinvd.org, kernel-team@meta.com, calvin@wbinvd.org, jv@jvosburgh.net, Breno Leitao , stable@vger.kernel.org X-Mailer: b4 0.15-dev-dd21f X-Developer-Signature: v=1; a=openpgp-sha256; l=2686; i=leitao@debian.org; h=from:subject:message-id; bh=PBufnVhc0vYw6K1yqEtQER08QOskv29ELpqGFdftBe8=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpDMVFPcvHaEIvqY/K0FMrxc5XuLCG7DPYcVfKr zAJADuB4YiJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaQzFRQAKCRA1o5Of/Hh3 baWiD/4+Jm/bGM23oRiaStiDbJzcHe9o+DQub4eeoFUmIU9ViAwTIOYJKC2Nqx44QF5Nx8VSWWf U55ww+CydeULqtsdCNc2D1LQf2uqpuVKSY2yEhgy416XNIB960v0vxXwc6PNUWqu9qNtn2GfXoO g1zIdEe3lPWnVzETl1lt8lsapfeQ21XNwWIY+QQbD3FICLeTGLhzEOsgFyRjw3M/tdbI5OetPef O7qwcMiXmdoZvQP1GpQfwZX+F0rLI4+vAVvI3gWMKlKqYRf33nCY8ErINNcKCcrPI3DVuIojB7G eLwPPnpn4RAcs6PAkaoU9Nof5DA50QAndSBbjLMr4E7ez3GtlUE6q5SjnS/HGs/OdgPyf4sL5sz lXQI6/GJgcCIhciHAFVD5vlOSderxZGNeEXfvkJGgqtVLnw7EAbH+u0UmQchNTlCixBV4VEpYi1 4BwLALvT6ST/xqAacz7ffxsGDAMiAAQ5wRy4y72V6kmBu4AvCFyZC9r7Ea4cFuorwtl+Vf3mVPF qN1Kwga41B9UVEe/YD9UeNdg+q8E06PtLpdLF96ocI0+VRK0Tt5lGoTpYZgq8ljGhglTbtF7fyN Hiaal6823bpg7LCdP7+xCRFdx98EpWj4VoWZWzBhYskGMkmosh1qJAQVUyoybopbHFQM+lXOe+t 8nTugHL0cqI6kRw== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D commit efa95b01da18 ("netpoll: fix use after free") incorrectly ignored the refcount and prematurely set dev->npinfo to NULL during netpoll cleanup, leading to improper behavior and memory leaks. Scenario causing lack of proper cleanup: 1) A netpoll is associated with a NIC (e.g., eth0) and netdev->npinfo is allocated, and refcnt =3D 1 - Keep in mind that npinfo is shared among all netpoll instances. In this case, there is just one. 2) Another netpoll is also associated with the same NIC and npinfo->refcnt +=3D 1. - Now dev->npinfo->refcnt =3D 2; - There is just one npinfo associated to the netdev. 3) When the first netpolls goes to clean up: - The first cleanup succeeds and clears np->dev->npinfo, ignoring refcnt. - It basically calls `RCU_INIT_POINTER(np->dev->npinfo, NULL);` - Set dev->npinfo =3D NULL, without proper cleanup - No ->ndo_netpoll_cleanup() is either called 4) Now the second target tries to clean up - The second cleanup fails because np->dev->npinfo is already NULL. * In this case, ops->ndo_netpoll_cleanup() was never called, and the skb pool is not cleaned as well (for the second netpoll instance) - This leaks npinfo and skbpool skbs, which is clearly reported by kmemleak. Revert commit efa95b01da18 ("netpoll: fix use after free") and adds clarifying comments emphasizing that npinfo cleanup should only happen once the refcount reaches zero, ensuring stable and correct netpoll behavior. Cc: # 3.17.x Cc: Jay Vosburgh Fixes: efa95b01da18 ("netpoll: fix use after free") Signed-off-by: Breno Leitao Reviewed-by: Simon Horman --- net/core/netpoll.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/net/core/netpoll.c b/net/core/netpoll.c index c85f740065fc6..331764845e8fa 100644 --- a/net/core/netpoll.c +++ b/net/core/netpoll.c @@ -811,6 +811,10 @@ static void __netpoll_cleanup(struct netpoll *np) if (!npinfo) return; =20 + /* At this point, there is a single npinfo instance per netdevice, and + * its refcnt tracks how many netpoll structures are linked to it. We + * only perform npinfo cleanup when the refcnt decrements to zero. + */ if (refcount_dec_and_test(&npinfo->refcnt)) { const struct net_device_ops *ops; =20 @@ -820,8 +824,7 @@ static void __netpoll_cleanup(struct netpoll *np) =20 RCU_INIT_POINTER(np->dev->npinfo, NULL); call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info); - } else - RCU_INIT_POINTER(np->dev->npinfo, NULL); + } =20 skb_pool_flush(np); } --=20 2.47.3