From nobody Sun Feb 8 12:32:47 2026 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 94A4A2D7DF6 for ; Wed, 22 Oct 2025 17:40:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761154840; cv=none; b=eqXO0IyAYunuHSfKRx0mRK/Pzf7y8pshX2O6rHsOuJJxSdPlvqGQoj4sbfHVWSDxcuKijj9AABMuMsIszhZncPjy9wuiouRdUsh4LjWRwGbfOLK/f2Bw1ZOwn66hYSw8SkrZuUb3R7MUsDw2Ux+8gyJyNdxzOR4QE9o/3s2jW40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761154840; c=relaxed/simple; bh=oCR56LMNxbJLVPpG2Gxtqc3kBZKa3X3HExddSDRVgK8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mBbNTRIptXvG6bd0/rr5TcWJ9UyoPDSZeuVG28hpyD1TEy9Nbl1LacMm8PgGReqfIqsQyrtPBU1kfz2msOWm0v/LLg4Egwyx5w9dPV7aM0FJuSAnxsybNlezp2I4y1/fVhCwBhrBIE7bGbPYIhh9M8Zzc45KPbPQtGs3xfje9lc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Onm42/E2; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Onm42/E2" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-42844985499so511548f8f.2 for ; Wed, 22 Oct 2025 10:40:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761154837; x=1761759637; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AgD0EgpQ1bwVIze+M9LiD/EeIFj+v/rXiYxks5RmUv4=; b=Onm42/E2WAA2X5zZc8PppDnA95yZwMbtpY1ZbVP0wQQLjLj7hr0CpCQIYvnfsWRheY lxSxR9FrjDdebES6CU7ItS/pi2AowBTxs46onppnlSnY9n9M05T1p7hj3PMtAUm8cIYJ zIrXOLnvYewJGtpZP9cViO0P65/DrOsozKARRzZuE7eI/cTpBbU2wKghG3vdy24FBoZF t2e/fPWj5vLUaJk9qpRRkUj8m1vhmFtnesFuhtu2LhOk5N1Tk4eH2avCu6+od5bEu2/w MNZHqDxXgv6iia996rZ7oPvdDRxI98HGYvw12oCeAMVcUe77P+CdwOPmRkt5mNU5in/A OJ5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761154837; x=1761759637; 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=AgD0EgpQ1bwVIze+M9LiD/EeIFj+v/rXiYxks5RmUv4=; b=pBwLxzT+5EBdz9714yuvh3Gs+6NqXodhhOJ2qs8LPMfJVtPZnUQwYietzxYl4K+Fqm 8u9N8qv6rZmeUo3Z+G7e7L3pGggGdMDo8VhhjclcT29gswqdmuk1zdLlNqL2tGxiU4TV cdVIN4cYNAdg9ZT+jQvfJ2x9lJysWPJgE2EnRTfzJRlUSQAMyZ1aPKmSPJA+fhe/8wD7 8DKvs5sPuKwAa2/XlVNZUkAXLimuuZgnTZjpUqlOZ+bxorGlBGH5HoJyEINlEVmiSkHR 4SR43uxxrN1c/F533pw7JjU+ImvOLAsCINdznJWr2L14CsQVDy8xFEmRo5at/DS6VuAS KkHQ== X-Forwarded-Encrypted: i=1; AJvYcCUxCzFyF+HTigQZZqkaRjS6kjfDgTEQltDa3PByr0DWuWPJMdFPESBqf9AsES30uBpv/KpFZJajFDtIEgY=@vger.kernel.org X-Gm-Message-State: AOJu0Yxv2kBp9GOXI+0dnQExD4Aa5flVtvVyF2H3QtBYJJo81/Pky0A7 F8D2H6aRS/fVQT92Dy9XYsry+iV4HTKEZmRxM7rSRVj8uiVFa/z51HYt X-Gm-Gg: ASbGncuP83/vAkGkvgMVU8WpkEmQxOg9Z/ULJ/8XImADfev5eN9tH2gV1pURjNIgAKS UEp/ICkj5sza8HDV7SF+/hpBR6tAgszWoheXUrkpqCHNV13nwD70PFOZMVPP6RUgK5OXhvxVxtL 4IGqs+yj8RgQWnT+rSYyd1LRi7ykMh9IlUIpXvSzj8T7xoVT/dkTj4/zCiRkS5ntLmcG2yU+ECJ Qk9tcDZQUyqtDayCpksZkBshycA2Wpl7PTW51jAAOHH2C4NZ2DcdWI65cUppLzdemX0h9DAga4z 1ue1hmEv4BILmTY/z2selvInXTfymChVosjI0rclTXuhpuwdIOanqpsu+bvxd/IP7O/2FrdyuRT Q85bmEvHwMNWM1odJz3H9gdwJX1Koulsx1XzkjakHeXrGhQTIKDpX3uS/Ebkt+yBCAYCWdrfQsq bwUtlf X-Google-Smtp-Source: AGHT+IGo0Rt1P+fnF95QCjRY9TTnpGTG3b3OqZamIFsM0FWgRiah419XNsZpM4zjKsCv8BLdL6naAw== X-Received: by 2002:a05:600c:1e8b:b0:46e:36f9:c57e with SMTP id 5b1f17b1804b1-47494305991mr35917595e9.5.1761154836724; Wed, 22 Oct 2025 10:40:36 -0700 (PDT) Received: from localhost ([2a03:2880:31ff:47::]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-474949e0a3csm43661225e9.0.2025.10.22.10.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Oct 2025 10:40:36 -0700 (PDT) From: Gustavo Luiz Duarte Date: Wed, 22 Oct 2025 10:39:57 -0700 Subject: [PATCH net v2 1/2] netconsole: Fix race condition in between reader and writer of userdata 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: <20251022-netconsole-fix-race-v2-1-337241338079@meta.com> References: <20251022-netconsole-fix-race-v2-0-337241338079@meta.com> In-Reply-To: <20251022-netconsole-fix-race-v2-0-337241338079@meta.com> To: Andre Carvalho , Simon Horman , Breno Leitao , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Matthew Wood , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Gustavo Luiz Duarte X-Mailer: b4 0.13.0 The update_userdata() function constructs the complete userdata string in nt->extradata_complete and updates nt->userdata_length. This data is then read by write_msg() and write_ext_msg() when sending netconsole messages. However, update_userdata() was not holding target_list_lock during this process, allowing concurrent message transmission to read partially updated userdata. This race condition could result in netconsole messages containing incomplete or inconsistent userdata - for example, reading the old userdata_length with new extradata_complete content, or vice versa, leading to truncated or corrupted output. Fix this by acquiring target_list_lock with spin_lock_irqsave() before updating extradata_complete and userdata_length, and releasing it after both fields are fully updated. This ensures that readers see a consistent view of the userdata, preventing corruption during concurrent access. The fix aligns with the existing locking pattern used throughout the netconsole code, where target_list_lock protects access to target fields including buf[] and msgcounter that are accessed during message transmission. Fixes: df03f830d099 ("net: netconsole: cache userdata formatted string in n= etconsole_target") Signed-off-by: Gustavo Luiz Duarte --- drivers/net/netconsole.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 194570443493..1f9cf6b12dfc 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -888,6 +888,9 @@ static void update_userdata(struct netconsole_target *n= t) { int complete_idx =3D 0, child_count =3D 0; struct list_head *entry; + unsigned long flags; + + spin_lock_irqsave(&target_list_lock, flags); =20 /* Clear the current string in case the last userdatum was deleted */ nt->userdata_length =3D 0; @@ -918,6 +921,8 @@ static void update_userdata(struct netconsole_target *n= t) } nt->userdata_length =3D strnlen(nt->extradata_complete, sizeof(nt->extradata_complete)); + + spin_unlock_irqrestore(&target_list_lock, flags); } =20 static ssize_t userdatum_value_store(struct config_item *item, const char = *buf, --=20 2.47.3 From nobody Sun Feb 8 12:32:47 2026 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 C87B12D73BD for ; Wed, 22 Oct 2025 17:40:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761154845; cv=none; b=Zh0Ou1FA7UtBYn/fs9PD7qZW0c4ijeqzyVwJXmGF2FpRIWyakCVgBRsLMrcduXsBdRF9knOy0sFa0nb4uY5IpgvpWhR4fhVqLne9nkRliuS0HQoiSglXfoIK58d9TJU0TbQkxyKk6cw1OE0Y2mbFAHWAjgpwPgkRgwz9pbp/Tyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761154845; c=relaxed/simple; bh=zVFKrMjT7Yg8KzgZuFRm2mf+JLW0KJOFTZQ6lqdZFRk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Z9l76syA5yX7u/fw8IL3H/ioQ5ze3vvEAUErUbbyWFadix9sOLEqayhZ7SVDrX0fStcmRzoXPBNqwr2dRpOVa7tp8zttmMXTu4fJroPdqMpogiQUwZNkN6Nl5+K9c0adpBfHNfLqSDH+EEAWuAt3aHuookeYn6Gc65+CJ6VFXQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BkFVZJav; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BkFVZJav" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-472cbd003feso2865025e9.3 for ; Wed, 22 Oct 2025 10:40:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761154842; x=1761759642; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AP35Lhe113Y360Zl88AVM66sXWalstyx7sHvh40y3sA=; b=BkFVZJave9AG9e/yHekQUqBgYiZkapx/5cZMoJ4MaVxlAgnXnefSAYSg48H7pqZRxY nq08ZRYqKJVN4CZClPv++4xuuLUSawfKrJlimIirOtS6jqg/ESKb5cHjB2OyKWrn4nB5 MbMS2BE3eKM2JyV8zl2cqKzKEbC2PuvK62r93Wb3f8HNW7Ixh1MEjkXdEosJBq5WVgXZ 450gUUTvosUgiizoO0+mAf7ukBXoJd98GRgadqwr0kTeulczf2Y4ma/ZW0mY4UEhvQGj dCOFo+MXaCMXAYsIqm95ETQpRu1Lrb1Xr9EEE7hEuQNhsvJFt/BccNSXMcUR4xzaizrq Oweg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761154842; x=1761759642; 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=AP35Lhe113Y360Zl88AVM66sXWalstyx7sHvh40y3sA=; b=uxe18QxyC6nVHIGtwEK6uqVVWm90CqdIkiqQ0J4FWDhmVJCuP0RIFVAL+Gu4vCMh+b 2koYkj54cLLSztmrv/ibb8px+v0fCGdx9brVfIcGISbhducRTynV4b2TU2+PBm/1pxLs a3DlphwYjfNUtGsfI8nphxyDeeUX7o0Ii2jxt0/rrMcv9DI8+P64aPe2rdDV9ESfLjfR wbSMGlkpuEs1T40oMJBiRejanrld0qo0NEq85PSahjfHO0iLOGcrh2r44BNvWAJV1JC7 xme9BXDmlBmnVXFb8QAkgmyneA/2OeJbhBnxnU5/RWo9YOO7vdv0NAVI4hNJXE3IsNAt aI2w== X-Forwarded-Encrypted: i=1; AJvYcCWBkMZmvdFLcb7kRTukpTZjmuOt1w2z5uWWeBduSlnPUfAURTavA2U9O9H3ow3Tr3LCSRFA+RtJoezTQCM=@vger.kernel.org X-Gm-Message-State: AOJu0Yztf1gmVpHWzNcO0KOdoxw9e6sSN6qGHnwtxLGlZiHs43Q7HfH1 7QOZFS/d715k8c2HVHgn8T4QYc30FunzlaBSRuQvwiq0LKG0H/0yF+1E X-Gm-Gg: ASbGncubevNEbAw4U9/oR40RzP4a7HDeEAm9UEIwILOZlWnY9bRa2WPWQCO6aTf4ehv TI9PYt2ihZ3rhb8xXL0GWESb0t0N0u5y5D7+/8KsRqtViBC38FAcxkp3EKt9/EerUCObQvSC7yD 7z8+zt8vkcpDyu4/ruQbJ1h0IvV5uZkQwwPqm3gIBSicPfaLwsBw2Sp5iT9Ea1s67QYk/YAdxa0 ZNLi7TPs7uy+a2eQfyGPOK4bi7iBxele+LnSuJdF5zfFxJSnMEoa3UV9pe7zbL/8wveoSd66M6L vr1dR3z2O8lxM/WW0G9oH1N/C2lPx4U9qDzj8kgmAJSyeioqvilD+WRctdgN0IlGQkZsJXkssAf QLTtDadLDVtpsgJJFN8SPaXdMhbOTX2aQY1Vs8NfI0byO9dat7hxKmpXb5n/m15pJ4tfB1Z6PNf yJXA8p X-Google-Smtp-Source: AGHT+IEecb4eLK37gAPhC+kUHjZRQLAVXxIkgkcbFBCIuxAll0AonuANG7ebbYJGBJSoVbFYl4u3hQ== X-Received: by 2002:a05:600c:1c25:b0:45f:2d70:2af6 with SMTP id 5b1f17b1804b1-474942c4fabmr36464545e9.1.1761154841890; Wed, 22 Oct 2025 10:40:41 -0700 (PDT) Received: from localhost ([2a03:2880:31ff:4e::]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-475c41ca845sm55430385e9.0.2025.10.22.10.40.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Oct 2025 10:40:41 -0700 (PDT) From: Gustavo Luiz Duarte Date: Wed, 22 Oct 2025 10:39:58 -0700 Subject: [PATCH net v2 2/2] selftests: netconsole: Add race condition test for userdata corruption 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: <20251022-netconsole-fix-race-v2-2-337241338079@meta.com> References: <20251022-netconsole-fix-race-v2-0-337241338079@meta.com> In-Reply-To: <20251022-netconsole-fix-race-v2-0-337241338079@meta.com> To: Andre Carvalho , Simon Horman , Breno Leitao , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Matthew Wood , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Gustavo Luiz Duarte X-Mailer: b4 0.13.0 Add a test to verify that netconsole userdata handling is properly synchronized under concurrent read/write operations. The test creates two competing loops: one continuously sending netconsole messages (which read userdata), and another rapidly alternating userdata values between two distinct 198-byte patterns filled with 'A' and 'B' characters. Without proper synchronization, concurrent reads and writes could result in torn reads where a message contains mixed userdata (e.g., starting with 'A' but containing 'B', or vice versa). The test monitors 10,000 messages and fails if it detects any such corruption, ensuring that the netconsole implementation maintains data consistency through proper locking mechanisms. This test validates the fix for potential race conditions in the netconsole userdata path and serves as a regression test to prevent similar issues in the future. Signed-off-by: Gustavo Luiz Duarte --- tools/testing/selftests/drivers/net/Makefile | 1 + .../selftests/drivers/net/netcons_race_userdata.sh | 87 ++++++++++++++++++= ++++ 2 files changed, 88 insertions(+) diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/s= elftests/drivers/net/Makefile index 6e41635bd55a..ba7dedc54711 100644 --- a/tools/testing/selftests/drivers/net/Makefile +++ b/tools/testing/selftests/drivers/net/Makefile @@ -17,6 +17,7 @@ TEST_PROGS :=3D \ netcons_cmdline.sh \ netcons_fragmented_msg.sh \ netcons_overflow.sh \ + netcons_race_userdata.sh \ netcons_sysdata.sh \ netpoll_basic.py \ ping.py \ diff --git a/tools/testing/selftests/drivers/net/netcons_race_userdata.sh b= /tools/testing/selftests/drivers/net/netcons_race_userdata.sh new file mode 100755 index 000000000000..017ee042eb75 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netcons_race_userdata.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-2.0 + +# This test verifies that netconsole userdata remains consistent under con= current +# read/write operations. It creates two loops: one continuously writing ne= tconsole +# messages (which read userdata) and another rapidly alternating userdata = values +# between two distinct patterns. The test checks that no message contains = corrupted +# or mixed userdata, ensuring proper synchronization in the netconsole imp= lementation. +# +# Author: Gustavo Luiz Duarte + +set -euo pipefail + +SCRIPTDIR=3D$(dirname "$(readlink -e "${BASH_SOURCE[0]}")") + +source "${SCRIPTDIR}"/lib/sh/lib_netcons.sh + +function loop_set_userdata() { + MSGA=3D$(printf 'A%.0s' {1..198}) + MSGB=3D$(printf 'B%.0s' {1..198}) + + while true; do + echo "$MSGA" > "${NETCONS_PATH}/userdata/${USERDATA_KEY}/value" + echo "$MSGB" > "${NETCONS_PATH}/userdata/${USERDATA_KEY}/value" + done +} + +function loop_print_msg() { + while true; do + echo "test msg" > /dev/kmsg + done +} + +cleanup_children() { + pkill_socat + kill "$child1" "$child2" 2> /dev/null || true + wait "$child1" "$child2" 2> /dev/null || true + # Remove the namespace, interfaces and netconsole target + cleanup +} + +modprobe netdevsim 2> /dev/null || true +modprobe netconsole 2> /dev/null || true + +OUTPUT_FILE=3D"stdout" +# Check for basic system dependency and exit if not found +check_for_dependencies +# Set current loglevel to KERN_INFO(6), and default to KERN_NOTICE(5) +echo "6 5" > /proc/sys/kernel/printk +# kill child processes and remove interfaces on exit +trap cleanup_children EXIT + +# Create one namespace and two interfaces +set_network +# Create a dynamic target for netconsole +create_dynamic_target +# Set userdata "key" with the "value" value +set_user_data + +# Start userdata read loop (printk) +loop_print_msg & +child1=3D$! + +# Start userdata write loop +loop_set_userdata & +child2=3D$! + +# Start socat to listen for netconsole messages and check for corrupted us= erdata. +MAX_COUNT=3D10000 +i=3D0 +while read -r line; do + if [ $i -ge $MAX_COUNT ]; then + echo "Test passed." + exit "${ksft_pass}" + fi + + if [[ "$line" =3D=3D "key=3DA"* && "$line" =3D=3D *"B"* || + "$line" =3D=3D "key=3DB"* && "$line" =3D=3D *"A"* ]]; then + echo "Test failed. Found corrupted userdata: $line" + exit "${ksft_fail}" + fi + + i=3D$((i + 1)) +done < <(listen_port_and_save_to ${OUTPUT_FILE} 2> /dev/null) + +echo "socat died before we could check $MAX_COUNT messages. Skipping test." +exit "${ksft_skip}" --=20 2.47.3