From nobody Mon Jun 8 14:37:30 2026 Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) (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 5EBD63FBEB0 for ; Thu, 28 May 2026 15:50:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779983438; cv=none; b=Zb+IcY1TtxG5blLaNNU9k8dATqbn/pqdx0QQQS0YAYVbQ1/VLGkI6nq3UvoSyoC4nRQlHLSt0aqgpc73LGzWrCh45oMiG9MGXAOb+dVBKA2MSCegEf3P6Jo0qOqA3hL0ZjVC8JOWadJCLUfx3Dk+ZFy5LurbJb5Hs6RV0sUZpDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779983438; c=relaxed/simple; bh=WcBmjmk/ISWJMhUszl7o1a95GVVtLaJ+X1L9bMnSr7c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=tkoUoncLVsMEDItLTYldH6OqNoyaQfRUqZM13f2zYgf7jwVhA6By5WOhtZhEmRLghVoLv8hqvrT0mCh8eId0Ik+alYc7mDyP2mlkGbxrADTzR9sw4unbtfYT4qGbmiVrWfXRz+fRYWZObfMtrY0KBoQACD0ZF/S+A+mbOUuFOT0= 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=H59ztwBa; arc=none smtp.client-ip=209.85.219.43 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="H59ztwBa" Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-8cac189e516so77685256d6.2 for ; Thu, 28 May 2026 08:50:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779983434; x=1780588234; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=qNlnOA1DAQ1rF4tBNb/+VHnT9xc4QGgj3T49JVzI4iQ=; b=H59ztwBakF3H3IOgCco9K8uBjs2/YM+7QYBGw3NAd93Eu9jn1XkeJS1RyRae8X7NhZ 14bEGOm6cP8YGhYA+cYZgAs6Rmyr8PBUMlkeXd1xiFfmjBgSS1uySwZRzoKePTyk2GUF oywVs6+uyM20up5PQOkZ6Tnhk5IeyzldYbl/vDGi8bHdzKvW+2PRTUGK92m6UvQZLDTk 1GEhzIgfdVDodQ0PUb4pCD/l9aEATCEIZU6Y6Z0XAv698V2RzTnZ0kh/8rItqtrYzumc qdGiygOI5wTBY+uQJ6/tAXhRjaJ7nii7BG3v2NZSVfvjA0//lP/01npAqBTcE/8A/5j0 xg8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779983434; x=1780588234; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qNlnOA1DAQ1rF4tBNb/+VHnT9xc4QGgj3T49JVzI4iQ=; b=c6Mm/HJNhDG3HytEZt6+eP4Qpa8qEKijkpt5LRSia+dIwdMKtYstOoEFqAKPhGhBaq DZalgmtOBc0N8BB/xoK7NjqQPXO888KV23aLme4IX1lo04ZrIBYsaT/1SNShBZrTGyug ASE+DgfSjJPNHdEcAr3liWvQRe2hZv9sq0WaYv8DMtEVO1hnWOsK1/ph/4MY3ejuLERv 8hh5FwPFJDhqvbBfs0KlFTjC10MgmuTjtQ/i/73j1h9OMidrD/DSUInwZ4H5A5o0W8ro 91OGVFQMS48qSzbtce+wpYi3OGkXELI+bLZgrnh2iACjKzxyuz9YBRUZaZePy+cZIYcw NL9g== X-Gm-Message-State: AOJu0YyG2W5BHaLWR6rjXwxVg3FJGadfqN6NJCwjExjBnO0O0X+YHiqh o92UjhxzpJJP7w6THe0IiegE7jAlhkylJcuhdvv2S9RHOCikd2NDQBWq X-Gm-Gg: Acq92OGt1Q/dtXDQH5G2xpWJm5tO3aazzQ+MJJPm6371ad5Ke3sc07wrLCi1fESxdWl qmATXAX9EVDEyex+fzvK2EYVYjiV0ueWTd5i79FBQj0XbRYjZsnPCqyOyATRBAFjDLfjZcvYMGw PyU66PR+qschyyyCcIeI8OzOGZL5XAA+BbK3hKPJ4EZ6/nJEiLpqzoSYn2btLvNQa86paWnm4ys g0EgujUpYz63Bv3cB5fePlyl2jB6DvdVhje//kvQLZ2H4bYqGHgj5DrIOp7ADk0YCqqN4tVOLno 6aCcDwFtscxzQoWvuNprVrgJIqzIZJBrtx0KVl6VrKaEJqKNpUKKq3dM2aZb7gymRtxzTP6q6/I TeaGvkAd6pA54eW9erUX4fEglcTQTcAozYPrT+4puqDhtFgmQCWYIefGG8CnGVeErq3niJNI3TU 2hwOiWvuxtiYrzPzxqQCUn41JXpjGpQCfWT5djbIJ3 X-Received: by 2002:a05:6214:27ef:b0:8ca:7f:89b8 with SMTP id 6a1803df08f44-8cc7b512329mr434826076d6.8.1779983434018; Thu, 28 May 2026 08:50:34 -0700 (PDT) Received: from wujing.localdomain ([23.254.208.9]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8cc8131524esm202958526d6.40.2026.05.28.08.50.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2026 08:50:33 -0700 (PDT) From: Qiliang Yuan Date: Thu, 28 May 2026 23:50:30 +0800 Subject: [PATCH v2] serial: 8250: fix use-after-free in IRQ chain handling 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: <20260528-bug-221579-8250-shared-irq-race-v2-1-06531202e54d@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/42NQQ6CMBAAv0J6ds12sVI8+Q/DoZYFNhGqrRIN4 e9WXuBx5jCzqMRROKlTsajIsyQJUwbaFcoPbuoZpM2sCOmIhixcXz0QaVPVYMkgpMFFbkHiA6L zDMZqPtSITldW5co9cifv7XBpMg+SniF+tuGsf/b/9qxBQ4m1Re8dUlee+9HJbe/DqJp1Xb/dR 8tfzgAAAA== X-Change-ID: 20260528-bug-221579-8250-shared-irq-race-581e4900a178 To: Greg Kroah-Hartman , Jiri Slaby , Anton Vorontsov , Alan Cox Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, wangzhaolong@fnnas.com, Qiliang Yuan X-Mailer: b4 0.14.3 serial_unlink_irq_chain() holds hash_mutex and calls free_irq() + kfree(i) when it sees an empty port list. serial_link_irq_chain() released hash_mutex after serial_get_or_create_irq_info() but before acquiring i->lock. This gap allowed a concurrent unlink to observe list_empty() as true while a new port was still being added, free i, and trigger a use-after-free. The corrupted list/irq_info then causes an "Unbalanced enable for IRQ" warning (kernel/irq/manage.c:774) because irq_shutdown() in the premature free_irq() path hard-sets desc->depth to 1, breaking the disable_irq/ enable_irq pairing in serial8250_THRE_test(). Fix by pulling hash_mutex into serial_link_irq_chain() so that it covers the i->head check and the list_add/INIT_LIST_HEAD under i->lock. serial_unlink_irq_chain() already holds hash_mutex throughout, so the race window is closed. Closes: https://bugzilla.kernel.org/show_bug.cgi?id=3D221579 Reported-by: Wang Zhaolong Fixes: 768aec0b5bcc ("serial: 8250: fix shared interrupts issues with SMP a= nd RT kernels") Signed-off-by: Qiliang Yuan --- Changes in v2: - EDITME: describe what is new in this series revision. - EDITME: use bulletpoints and terse descriptions. - Link to v1: https://lore.kernel.org/r/20260528-bug-221579-8250-shared-irq= -race-v1-1-30980cca02f3@gmail.com --- drivers/tty/serial/8250/8250_core.c | 46 ++++++++++++++++++++++++++++-----= ---- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/= 8250_core.c index a428e88938eb7..dfe76223ce10c 100644 --- a/drivers/tty/serial/8250/8250_core.c +++ b/drivers/tty/serial/8250/8250_core.c @@ -134,7 +134,7 @@ static struct irq_info *serial_get_or_create_irq_info(c= onst struct uart_8250_por { struct irq_info *i; =20 - guard(mutex)(&hash_mutex); + lockdep_assert_held(&hash_mutex); =20 hash_for_each_possible(irq_lists, i, node, up->port.irq) if (i->irq =3D=3D up->port.irq) @@ -151,27 +151,51 @@ static struct irq_info *serial_get_or_create_irq_info= (const struct uart_8250_por return i; } =20 +/* + * serial_link_irq_chain() hooks the given 8250 port into the IRQ chain. + * + * hash_mutex must be held across checking i->head and adding the port to + * the list. Without this, a concurrent serial_unlink_irq_chain() can race + * in after hash_mutex is dropped but before i->lock is acquired, observe + * list_empty(i->head) as true, call free_irq() and kfree(i) =E2=80=94 tri= ggering a + * use-after-free and ultimately an "Unbalanced enable for IRQ" warning in + * kernel/irq/manage.c. + */ static int serial_link_irq_chain(struct uart_8250_port *up) { struct irq_info *i; int ret; =20 + mutex_lock(&hash_mutex); + i =3D serial_get_or_create_irq_info(up); - if (IS_ERR(i)) + if (IS_ERR(i)) { + mutex_unlock(&hash_mutex); return PTR_ERR(i); + } =20 - scoped_guard(spinlock_irq, &i->lock) { - if (i->head) { - list_add(&up->list, i->head); - - return 0; - } + /* + * Serialise against the list manipulation in the interrupt handler + * and in serial_unlink_irq_chain(). hash_mutex is still held which + * prevents serial_unlink_irq_chain() from running concurrently. + */ + spin_lock_irq(&i->lock); + if (i->head) { + list_add(&up->list, i->head); + spin_unlock_irq(&i->lock); + mutex_unlock(&hash_mutex); =20 - INIT_LIST_HEAD(&up->list); - i->head =3D &up->list; + return 0; } =20 - ret =3D request_irq(up->port.irq, serial8250_interrupt, up->port.irqflags= , up->port.name, i); + INIT_LIST_HEAD(&up->list); + i->head =3D &up->list; + spin_unlock_irq(&i->lock); + + mutex_unlock(&hash_mutex); + + ret =3D request_irq(up->port.irq, serial8250_interrupt, + up->port.irqflags, up->port.name, i); if (ret < 0) serial_do_unlink(i, up); =20 --- base-commit: eb3f4b7426cfd2b79d65b7d37155480b32259a11 change-id: 20260528-bug-221579-8250-shared-irq-race-581e4900a178 Best regards, --=20 Qiliang Yuan