From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 E88AD3AFCE3 for ; Fri, 31 Jul 2026 18:05:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785521126; cv=none; b=aq6LT5A91vGsDRCacKIuaJ8djyTiLt3mzLv7bkew2pBQ69rSZpHip+o9baajP2i+YiLivDBjqRg061GGnz1TzeQGG8f3DRnhEJ5PBdOKZ7tm0hZtYX+4AnJNCPcPG6bITcjV4n+VNEJkkCNgEiSJQZEr0EAiUQPpBbc8B8czQdE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785521126; c=relaxed/simple; bh=PsKOHHQKdkTPVr5CbgWuTRjCZmIzAuWQ/T5zBgCOXzw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=fT1vb8+Y+WdsFJ79UCErt68y1Hz4pXI1qrZhvG4M76tKQ9itzuNDYr7hXJDkqYN4B/pcA870WEyClLwLaVALeaqRbE3FPm4fWAIibVg3/M0rEtP7mo5Fb01G8hcZjFpB3cGZxc6VznyUgj8KOrJtN+s9RfAEMDQsHb8HVWsvXOY= 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=dRUYLd+X; arc=none smtp.client-ip=209.85.221.41 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="dRUYLd+X" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-47db714766aso1463522f8f.0 for ; Fri, 31 Jul 2026 11:05:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785521123; x=1786125923; 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:content-type; bh=6m+8w0QVgGm8urXe/cU07WfU5xAZ+QJVFPsiFH0egMo=; b=dRUYLd+XZDObOfJu7MYRna9XibQmadJYmxFq3QwgpDohKY40cTu72fcHSnYtooKM4F Cu5OBIrI6PcZvhnuHRZ9X4mTK67hgeLzdWDzor82yCM0g91xT4qYpiZqGxuRu0H0ONIC q5dcaSUmIzQ7N2piCxiK3SluYjRe18qUvQJICwNFyIQGnfu6nFT7pQlQb02WD8BIQnwJ up0e4aKM3TcYSRb5gcJlq6bpLSxGYEbbhKPXSepErqLyIKtBeIcPHowq19wX3sQ0q07Z 2/H0F41wezJbcJRnMNYPq2ddHQwp6mQdHYXzERsrRxeGrE3/7NfyWHxmRv70S9e19BX2 JsHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785521123; x=1786125923; 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:content-type; bh=6m+8w0QVgGm8urXe/cU07WfU5xAZ+QJVFPsiFH0egMo=; b=AChfd6kSA1ghaRnAHnCdljrP2/SdpuNplAmhwf/mWdctA/VVAdyu1zpV6wWxrXGPSM 4aKp1ucbYUuZovo3AJKWtxl4RYGD+wky769mV1rs6VmZsAz8/cB/xmP9l2hCAGAeFDkp rde08VbrUkMwRmm7iqBsaFdfjIulXxXSexQJt/gBb/Ukl+P51aGom4kwbMbuA9S2q9ih FRo/PgX/j612gHKppDcNaaS7PAG+Ut3VzCWfZkDXDUckVExmR4GKVqz8kmCTYOu92Uub o3+dbu9dMaCtD81aQCzExYfHrMUu+9Q8AHddUlsse1m79fh8zJ1AeWGDSB04/uDqZL6A dhtw== X-Forwarded-Encrypted: i=1; AHgh+RrPOdqLoUq8A1shoECKQBA3DquiAYKvhP5nICyno77xnMRFYCBh0bpAhXOVRG7X0yr3WZQbFQOz73XLiwo=@vger.kernel.org X-Gm-Message-State: AOJu0YxNbYMZFN/6Tq9C1PZ+SDeZlAhytF+1mjWKRaormduw+Z5DD/wH 5yLSdCyU9E5xIyJKVPOSgM3/N2e8Gt9MnoHSJg99p4Bzax191SZz8AYR X-Gm-Gg: AR+sD135Dn27iEEN9bbZcOT3V4rSChxfl3bMz6GU4j47MhBEU/ZHYcG6PqkhK4Lbz/Y G6MBnbUhMv8EAtR9DsjnuFVqO3T4Zb2aM86zPQcWAQ9LC62DmTUM8L9na0nn3hiWdQ5yp2rJyRM AW1fJVH4BYRb8v8+fpW+xkra8mr2rForpQbyEOBtfa9Rn/LdqcjcBdsOfuQHFpEtZl2ocO5P6vb 7adCHadZdZ0ghw8M+vHfIUh5vRdyUZaIe7H0elG856Fs7kmqI+m+k+xfmsvS0z1njRrMacr3V3V QYy2TGxPdgu4DsiqfseYpWEDKBO9a7/WA14Q4jUgzxtfBLhrpKxGg2uG3QxB93970LFHdXnPDGi vjosNedJBAi3pMoZXBdWj3GuM5oRgRMSBLf5mmQbNAXXtxVUW5sAa9+ijq/AXn6UCRY6ASVm1ri bFW5V+mbs7LanpC9z5clGg0ToAj8zHiI6+3/h5XE3jTZplVfJMQu0w0EYw200xv23du4mKfEERP hLFCxO/oHNtOJ1n5wazqp19RM1SZkAfYUy3iNYAqwLiaQKDY7AoViY/9cHYZWF49p5I68PP5Wei 1xZNEgyRcj5lJAzUX+p0hz3OT1D8qt+jz227D4THYckHWlg4L6ixhhH04+ewXsgQaj0L X-Received: by 2002:a05:6000:310f:b0:47f:775b:f5dc with SMTP id ffacd0b85a97d-47fd32e1c76mr5907524f8f.19.1785521123140; Fri, 31 Jul 2026 11:05:23 -0700 (PDT) Received: from localhost.localdomain (dynamic-2a02-3100-a8d0-bc01-6d31-3d12-3a11-d2d0.310.pool.telefonica.de. [2a02:3100:a8d0:bc01:6d31:3d12:3a11:d2d0]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47fd456a6besm7094559f8f.22.2026.07.31.11.05.21 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 31 Jul 2026 11:05:22 -0700 (PDT) From: Karl Mehltretter To: Greg Kroah-Hartman , Jiri Slaby Cc: Karl Mehltretter , Frank Li , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, Sashiko , stable@vger.kernel.org Subject: [PATCH v3 5/5] serial: imx: serialize imx_uart_ports[] lifetime Date: Fri, 31 Jul 2026 20:05:19 +0200 Message-Id: <20260731180519.10988-1-kmehltretter@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit imx_uart_probe() publishes its devm-allocated port in imx_uart_ports[] before uart_add_one_port() because console setup uses the table. The entry is not cleared when adding the port fails or after removal, leaving a dangling pointer. A sibling probe can register the shared console through that stale entry. This was reproduced under KASAN on QEMU mcimx6ul-evk by unbinding a sibling UART, unbinding the console UART and rebinding the sibling. Keep the entry valid through uart_remove_one_port(), then clear it. Protect port addition and removal together with their table updates so sibling operations cannot interleave. Reject an occupied slot rather than clobbering an active port during a duplicate-line probe. Fixes: dbff4e9ea2e8 ("IMX UART: remove statically initialized tables") Fixes: 9f322ad064f9 ("imx: serial: handle initialisation failure correctly") Reported-by: Sashiko Link: https://lore.kernel.org/all/20260719162850.043B41F000E9@smtp.kernel.org Link: https://lore.kernel.org/all/20260719222501.CB4CB1F000E9@smtp.kernel.org Cc: stable@vger.kernel.org Assisted-by: Claude:claude-fable-5 Signed-off-by: Karl Mehltretter --- Backport note: the removal fix is self-contained. Complete probe-failure coverage also requires patch 1. With patch 1, all fallible allocations precede console registration, so rollback can safely clear the table. Without it, a late allocation failure (only reachable with fault injection) can leave the console registered after imx_uart_ports[] is cleared, turning the pre-existing use-after-free into a NULL dereference. drivers/tty/serial/imx.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c index 251a50c8aa38..b0f34a6e7d4f 100644 --- a/drivers/tty/serial/imx.c +++ b/drivers/tty/serial/imx.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -2080,6 +2081,9 @@ static const struct uart_ops imx_uart_pops = { static struct imx_port *imx_uart_ports[UART_NR]; +/* Held across uart_add/remove_one_port(); console callbacks must not take it. */ +static DEFINE_MUTEX(imx_uart_ports_lock); + #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE) static void imx_uart_console_putchar(struct uart_port *port, unsigned char ch) { @@ -2632,11 +2636,19 @@ static int imx_uart_probe(struct platform_device *pdev) } } - imx_uart_ports[sport->port.line] = sport; - platform_set_drvdata(pdev, sport); - ret = uart_add_one_port(&imx_uart_uart_driver, &sport->port); + scoped_guard(mutex, &imx_uart_ports_lock) { + if (imx_uart_ports[sport->port.line]) { + ret = -EBUSY; + } else { + imx_uart_ports[sport->port.line] = sport; + ret = uart_add_one_port(&imx_uart_uart_driver, + &sport->port); + if (ret) + imx_uart_ports[sport->port.line] = NULL; + } + } err_clk: clk_disable_unprepare(sport->clk_ipg); @@ -2648,7 +2660,9 @@ static void imx_uart_remove(struct platform_device *pdev) { struct imx_port *sport = platform_get_drvdata(pdev); + guard(mutex)(&imx_uart_ports_lock); uart_remove_one_port(&imx_uart_uart_driver, &sport->port); + imx_uart_ports[sport->port.line] = NULL; } static void imx_uart_restore_context(struct imx_port *sport) -- 2.53.0