From mboxrd@z Thu Jan 1 00:00:00 1970 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 09F773ACA7B for ; Sun, 19 Jul 2026 16:08:37 +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=1784477319; cv=none; b=sGl4FJeNLL5sRtG2YpE4h+xJoCiPay4+eGyGq7dgZNtd5KSoLNRuQ2B8A4ALJ/CfJXoc+tYR1oAvNONV0GmODkjaEpDPy5ztZbtwxdoK1eAohCoIc6N28xVj1g13reNNJVLW+PLnciH+x+KQJmJr8k3C40C92cs8smVFq4VYE6U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784477319; c=relaxed/simple; bh=8sdnQwIMdO7CwPShC74pX/gtmmBu9zvdCtaHEui32XA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EVIcZECa3yvEZ31GbIZ9V7rT3j2u4wC3S1YOdWIj7GvWI5hHwx3TYNLN9mLHIobU3Z+nhV8IDnekRdRPP2p4lJFWnwXmaVXbbLi4DmsFhX4Xm4BZepZgoTB5coW6pb8aerGa/XMYb4Vl+qSinyyhRkNSxKBX8KaLflcMSVnBtII= 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=DW/jTCV2; 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="DW/jTCV2" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-49548aebcd8so14335965e9.3 for ; Sun, 19 Jul 2026 09:08:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784477316; x=1785082116; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/SUAt/6zWUy8VxjfuD7LmHF2gwOSZYdtNqcx/e+ByoY=; b=DW/jTCV2S3HFHpdNmnAIJkQh4qaLZdmdU7SPXoSzy60dW+KfNOWheL9rwc3lDTc0L5 P57PNBce04KyoJbgkW3kjcLplQzQjFE8DeeCcsVHOZkdPns002m6eXUd/Rd2tTavOUeU mIvzvcqnfx+AtJqWdSINxguTSHdSkDzOPmVFYa9VPHiOcI6z3o2ee2Jyqb6ZoA5/vgPl GgKFAEQgRulpwAzR7ndpymy3x9G26foYf2l810mq8vGKll24roFyrqm0BVBtKiKsd0I0 T7QC/Z025fvlRKC1tm8Rm96SritzUqkxV6SynBuIdgcfA9gYVC5qeriPt1NWwNMROyfC xN8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784477316; x=1785082116; h=content-transfer-encoding:mime-version:references:in-reply-to :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=/SUAt/6zWUy8VxjfuD7LmHF2gwOSZYdtNqcx/e+ByoY=; b=oJa6vDmBYiZIBJFYY0JHZHKhrMy04KNzk11/IsvnoVvg32mbMbxQFmrP3qp/vOs6BB DSMPbDpAtQgRPSnF9B16/kWkN8tfRpsWx14nCBiRDLepBrUmO8srnmm02BW3oiM0A/KT xdg+1PMRj3qXgcMSbQt/hH/CKXox3INf9Jd1HHjy1LSXuf/VKlwcOeph2lYQip5mYKOi hvDPx0vBFj3dOvn9T/sVnVIwZgyV3NrRSayDz41LzKiwMY8LWfdFxFDs374zGLAnRwM6 Zg3/UjhpDbye7FZRl7IqpXbY+HlRdIZ1dcBH/uQnyibzTTVvwmsCm2sKSBw1Ih5BFLdU H3dw== X-Forwarded-Encrypted: i=1; AHgh+RrGnKhVjL8Qvj7SsggJxILSTssy4zqAUh+GchQEPYkSGu+Rl4At7yOJPY7f9nQWfthtSO7br8Mf6y6WlSY=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9Ym5FNKpLKf1zAx0yRlrvgyQlE4M4DYlwnklqqmwVR05/tD6y 98i7NF8PMI7372I80/5I0MqHJYXKFH4DtcKlY+R3mfOwb3nVTJPUutAy X-Gm-Gg: AfdE7cmez0Oi3gcYC43vqaypwziLn48IxL9cz78j3I2flA+gwSZp+Iu21wkqZhCKXho Ul5/PccdHrsnpAajB6gYzfj11lwqJ5ufsGnDQDT/EgGj5827rQ2eiJHGmjTifepgTkLJxq+tzsu F8WrTvduxrlCxWQ5Qee+cXpc0vIk81NrxxTlkif9nXB01QGWT66ieq2wNrRlegmFrJbcZjUmDVR iBhz5ar+K0pNiqxegUa8TjzxoVanJt/atJIGOSN67i0kIXH3rWn3lIcMBR6xaX+GEv1MRmHN3Sk wuTZF6ONsvEUX/Xtl+hHajby2KJ+RwnGOmfdE8vFoMDLRtjtwDt9NByLsKJnRw3pMlAEfG31I49 cLR1IM/lnY6l5T5LIYjTuBfrhpNwhEgV5XNfuOniA2OVDwzynjT9iK2QezG5wYA5QQxedVi4obW 6rGEe4EGaTDLW9Me+fpmAUFh62CvGNTHiLW6NmbpnqcDq6yK34wiRh7n9bHhuO5t0I5n6mVHKU/ 4RhZpU492rzIpYVSiTyYUa6LqPWI3g44d4W+EIgdULvdlAWA6B89ZqMZs7SS6UmgBcBT6+Xf3e8 422l0bDOyw== X-Received: by 2002:a05:600c:4504:b0:493:eb71:5cc6 with SMTP id 5b1f17b1804b1-4954a40bbc8mr113521195e9.27.1784477315903; Sun, 19 Jul 2026 09:08:35 -0700 (PDT) Received: from localhost.localdomain (dynamic-2a02-3100-aeaf-bd01-78a0-2a2d-507f-c97c.310.pool.telefonica.de. [2a02:3100:aeaf:bd01:78a0:2a2d:507f:c97c]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4954a2edbe4sm223826745e9.12.2026.07.19.09.08.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 19 Jul 2026 09:08:35 -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, stable@vger.kernel.org Subject: [PATCH 4/4] serial: imx: clear imx_uart_ports[] entry on probe failure and removal Date: Sun, 19 Jul 2026 18:08:12 +0200 Message-Id: <20260719160812.35407-5-kmehltretter@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20260719160812.35407-1-kmehltretter@gmail.com> References: <20260719160812.35407-1-kmehltretter@gmail.com> 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() stores the port in imx_uart_ports[] before calling uart_add_one_port() because console setup during that call uses the table. The entry remains set if uart_add_one_port() fails. imx_uart_remove() also leaves it set after uart_remove_one_port(). sport is devm-allocated, so a failed probe or unbind frees it while imx_uart_ports[] still points to it. A later sibling probe can register the shared console with the old preferred index and dereference the stale entry in imx_uart_console_setup(). Reproduced on qemu's mcimx6ul-evk by unbinding a sibling UART, unbinding the console UART and rebinding the sibling: BUG: KASAN: slab-use-after-free in imx_uart_console_setup+0xd0/0x3d8 Read of size 4 at addr c49ad9d4 by task init/1 Call trace: ... imx_uart_console_setup from try_enable_preferred_console+0x158/0x1f8 try_enable_preferred_console from register_console+0x1cc/0x80c register_console from serial_core_register_port+0xe58/0xeb0 ... Freed by task 1: ... devres_release_all+0x100/0x18c device_unbind_cleanup+0x38/0xdc device_release_driver_internal+0x230/0x288 unbind_store+0x64/0xa8 ... The entry must remain visible while uart_add_one_port() and uart_remove_one_port() run. Clear it when adding the port fails and after removing the port. Save the line index before removal because the uart_port is no longer valid afterward. The probe-failure cleanup relies on the preceding serial-core change "serial: core: do fallible allocations before the console can be registered", which ensures that uart_add_one_port() cannot fail after registering the console. For complete coverage, both changes should be backported together. Fixes: dbff4e9ea2e8 ("IMX UART: remove statically initialized tables") Fixes: 9f322ad064f9 ("imx: serial: handle initialisation failure correctly") Cc: stable@vger.kernel.org Assisted-by: Claude:claude-fable-5 Signed-off-by: Karl Mehltretter --- drivers/tty/serial/imx.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c index 251a50c8aa38..617c35772056 100644 --- a/drivers/tty/serial/imx.c +++ b/drivers/tty/serial/imx.c @@ -2637,6 +2637,8 @@ static int imx_uart_probe(struct platform_device *pdev) platform_set_drvdata(pdev, 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); @@ -2647,8 +2649,10 @@ static int imx_uart_probe(struct platform_device *pdev) static void imx_uart_remove(struct platform_device *pdev) { struct imx_port *sport = platform_get_drvdata(pdev); + unsigned int line = sport->port.line; uart_remove_one_port(&imx_uart_uart_driver, &sport->port); + imx_uart_ports[line] = NULL; } static void imx_uart_restore_context(struct imx_port *sport) -- 2.53.0