From mboxrd@z Thu Jan 1 00:00:00 1970 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 C468C3A9636 for ; Thu, 25 Jun 2026 02:10:48 +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=1782353450; cv=none; b=XCQmCBf5+DeNcm8P5yzRlBo2fXSzuP3LxlBKdWJi5dBZivNIidxEjPSjZoD8Kexoj1PWFkz9W2FHamHnoQdEMQFr9ja62/Q6wOWAVxiNJcJA5uSJdJiiiiOgDduMe62Gblz+aS5WoYznO9l/t//+Y7cM2Oc1vP7sMD6Dwg+2K+I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782353450; c=relaxed/simple; bh=G88W9m5tP11+92mh/AHTy4cpi91NeFSzYPjssTER1No=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=Vly2mwW8nCz+YBMMZM7bK7vRKFqn7MfFArKX+x7oRaX1z2AUm4XPsDf5JJ9I0EQbYXE7s6eYFNn/vB2f9c+FAPQLX79sqNzs4X1h2A1vcta7Ebs1wXQpKPLVgeygLq8Fdp8/bGlcpiCMignxq0aD0cooB6/kHW3OeOx5jZQuq6Y= 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=lvO5+joD; 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="lvO5+joD" Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-8dc09919aa2so28399946d6.3 for ; Wed, 24 Jun 2026 19:10:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782353448; x=1782958248; darn=vger.kernel.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=VgSzTrWn88ZvsaE1I9E2bWBD7BhNYgtYr9BXBbmGjqA=; b=lvO5+joDzE8jE3WSZOEnXLliKaZT6U+5H6Va82iRXkHimVNapYN/F64sYevuXSkeo0 WeIshN4b1PF02fy7aK+UgR5y4SWM4QfFobi0h/nfydTipIaklCyk7Ha1rEfFnw6iV3Bd tSAOWl1GgwCAlB5DRM9/p7T4GzewQ0xoUbL8H8RLF0bv06fCB6o7u1lHL1TjOGASc4do gA8q9FpTAXSAwIbvlXKr4COJGyBzIBje2Ytebcsu9rZ0fHOR5CmJX+/39LurkS1NvSPu SUwZGyCADth/vUSpE3+nNPA6nnJ54RQtrDQUSl9RLV8JKR8S4O507fESUPrbP23AlDnb 7EhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782353448; x=1782958248; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=VgSzTrWn88ZvsaE1I9E2bWBD7BhNYgtYr9BXBbmGjqA=; b=GouEJZ+Ov1ZK6uvFNesjxriKbSxT2fjvNkoqUTfeNLg2szFB3jKhuCXLJBDRfzDxAs neljp+H/sGdQVj2QdM62A9pdxljyfbNUsIGv1Fcjlgg6SPyXo9V2B0JT0M4CvwScU6Bu YwpO9d+9tv7ALGUvYljFIzzgPs8dwQIZ7IkK+NsEp8jVJaiUjb+RLZuao0nYJHIJpRLM JzVcvGOy5QGEe2oX1cM4m8Gqwbh2vyXf8Zs2HBIJ/xWz1O0cjAbEJ75Prb8r8cVVweNq wR2VVhTdmKvb1d4iJflCtGz3+qnjPAqw957pNHoA3/nO8FVBTqkRtnP+44SCl0059/Mh T2CA== X-Gm-Message-State: AOJu0YxEUpCV6be7kotyClb0EKAsrgbi0pXhLa+fUS9FeRTBGUOxVXeE Hr3zGA7wcA1PHYFWP19Mbe5VH8EzLH3uWHZub17kvT5zcMYByYY2boqv04cbkMqcejI= X-Gm-Gg: AfdE7cnbncZUEIqi26HyrBWzV9A4ngHBhBGaEYlOGRvCba1ZVXeQhF66Pd6QIpOTeR1 USl4Yrz+7knvuHRqfGrQsbqBdCmncndwSYRbss++r8n5FfksUJIBQyPXbrjl6WtiTLqe6JSDmrn 0c5NcngkTIPC3+RO1v6UF/Y5kkrsPWq+O++qEPWLcgjUaCiWRAUSCqU1HcUoNxcuIUbyGIHgxu+ GUctQZjWMCNrCwcy2eUmsr2vVMqKU+9S8sHch6FYj4ymG0ZR2lscpIVYIlBIRQ+chAgv5+hozBs B5b/OBorzwOd5OJzBoB2+I11xzrka8QDySvneabzRl4267kJzZY74SCh1jCi/eLJtHXHXQJxY2f QwcTN0jCINTmxyhK8xfl6FRfjTijXi9xTb/EjkurB915PE2oC3Tuju2xMk/xWiqlqazCtpIQcTr UIUqJA+xT4LkU6zhb0DSl+2HlRCaYnTnVMuCKsQkaZ7ESbucBtp2cAzw== X-Received: by 2002:a05:6214:3188:b0:8ac:a6a5:1f41 with SMTP id 6a1803df08f44-8e6d490df79mr13581826d6.27.1782353447637; Wed, 24 Jun 2026 19:10:47 -0700 (PDT) Received: from smtpclient.apple ([2601:985:4601:5df0:b155:68a5:8cf5:d9e8]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8e683cbd054sm11389436d6.28.2026.06.24.19.10.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jun 2026 19:10:46 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: [RFC] ipack: ipoctal: fix use-after-free and null-ptr-deref on remove From: Shuangpeng In-Reply-To: <6819a66c577a1a737dff96dc2d25d2abb1997a55.1782350823.git.xiaopei01@kylinos.cn> Date: Wed, 24 Jun 2026 22:10:15 -0400 Cc: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <2424869F-ABF4-4102-8DD4-6F2E0BCAB25E@gmail.com> <6819a66c577a1a737dff96dc2d25d2abb1997a55.1782350823.git.xiaopei01@kylinos.cn> To: Pei Xiao X-Mailer: Apple Mail (2.3864.600.51.1.1) Hi Pei, Thanks for the updated patch! I applied it and reran the same reproducer. The previous crash in ipoctal_write_tty() is no longer triggered, but the same reproducer now hits another Oops after removing the device while the tty fd is still = open. The new report looks like this: BUG: unable to handle page fault for address: ffffc9000617b005 #PF: supervisor write access in kernel mode Oops: Oops: 0002 [#1] SMP KASAN PTI RIP: 0010:iowrite8 (lib/iomap.c:207) Call Trace: ipoctal_set_termios (drivers/ipack/devices/ipoctal.c:66 = drivers/ipack/devices/ipoctal.c:517) tty_set_termios (drivers/tty/tty_ioctl.c:341) set_termios (drivers/tty/tty_ioctl.c:516) tty_mode_ioctl (drivers/tty/tty_ioctl.c:?) tty_ioctl (drivers/tty/tty_io.c:2801) __se_sys_ioctl (fs/ioctl.c:51 fs/ioctl.c:597 fs/ioctl.c:583) do_syscall_64 (arch/x86/entry/syscall_64.c:63 = arch/x86/entry/syscall_64.c:94) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) The faulting access is from ipoctal_reset_channel(): drivers/ipack/devices/ipoctal.c:66 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr); called by ipoctal_set_termios(): drivers/ipack/devices/ipoctal.c:517 ipoctal_reset_channel(channel); Best, Shuangpeng > On Jun 24, 2026, at 21:33, Pei Xiao wrote: >=20 > A use-after-free occurs when the device is removed while a tty > session is still active. The remove callback frees the ipoctal > structure via kfree() while ipoctal_write_tty() and other tty ops > may still access it. >=20 > Fix the UAF by introducing kref-based lifetime management for the > ipoctal structure. A kref is taken in ipoctal_install() when a tty is > initialized, and released in ipoctal_cleanup() when the tty is finally > destroyed. The remove callback replaces direct kfree() with = kref_put(), > ensuring the memory is only freed after all tty references have been > released. >=20 > However, keeping the ipoctal struct alive via kref exposed a null > pointer dereference: __ipoctal_remove() calls tty_port_free_xmit_buf() > which frees and NULLs xmit_buf, but a userspace process may still hold > the tty fd and call write(), leading to a crash in > ipoctal_copy_write_buffer() when it dereferences xmit_buf. >=20 > Fix this by checking for a NULL xmit_buf in ipoctal_write_tty() and > returning 0 if the buffer has been freed. >=20 > Reported-by: Shuangpeng Bai > Closes: = https://lore.kernel.org/lkml/178144969601.60470.1257088106279546587@gmail.= com/ > Fixes: 05e5027efc9c ("Staging: ipack: move out of staging") > Signed-off-by: Pei Xiao > --- > drivers/ipack/devices/ipoctal.c | 22 ++++++++++++++++++++-- > 1 file changed, 20 insertions(+), 2 deletions(-) >=20 > diff --git a/drivers/ipack/devices/ipoctal.c = b/drivers/ipack/devices/ipoctal.c > index 1bbefc6de708..6bbbfb651dea 100644 > --- a/drivers/ipack/devices/ipoctal.c > +++ b/drivers/ipack/devices/ipoctal.c > @@ -10,6 +10,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -25,6 +26,8 @@ >=20 > static const struct tty_operations ipoctal_fops; >=20 > +static void ipoctal_release(struct kref *kref); > + > struct ipoctal_channel { > struct ipoctal_stats stats; > unsigned int nb_bytes; > @@ -49,6 +52,7 @@ struct ipoctal { > struct tty_driver *tty_drv; > u8 __iomem *mem8_space; > u8 __iomem *int_space; > + struct kref kref; > }; >=20 > static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel = *chan, > @@ -95,6 +99,7 @@ static int ipoctal_install(struct tty_driver = *driver, struct tty_struct *tty) > if (res) > goto err_put_carrier; >=20 > + kref_get(&ipoctal->kref); > tty->driver_data =3D channel; >=20 > return 0; > @@ -462,6 +467,9 @@ static ssize_t ipoctal_write_tty(struct tty_struct = *tty, const u8 *buf, > struct ipoctal_channel *channel =3D tty->driver_data; > size_t char_copied; >=20 > + if (!channel->tty_port.xmit_buf) > + return 0; > + > char_copied =3D ipoctal_copy_write_buffer(channel, buf, count); >=20 > /* As the IP-OCTAL 485 only supports half duplex, do it manually */ > @@ -666,6 +674,7 @@ static void ipoctal_cleanup(struct tty_struct = *tty) >=20 > /* release the carrier driver */ > ipack_put_carrier(ipoctal->dev); > + kref_put(&ipoctal->kref, ipoctal_release); > } >=20 > static const struct tty_operations ipoctal_fops =3D { > @@ -683,6 +692,13 @@ static const struct tty_operations ipoctal_fops =3D= { > .cleanup =3D ipoctal_cleanup, > }; >=20 > +static void ipoctal_release(struct kref *kref) > +{ > + struct ipoctal *ipoctal =3D container_of(kref, struct ipoctal, = kref); > + > + kfree(ipoctal); > +} > + > static int ipoctal_probe(struct ipack_device *dev) > { > int res; > @@ -692,6 +708,8 @@ static int ipoctal_probe(struct ipack_device *dev) > if (ipoctal =3D=3D NULL) > return -ENOMEM; >=20 > + kref_init(&ipoctal->kref); > + > ipoctal->dev =3D dev; > res =3D ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot); > if (res) > @@ -701,7 +719,7 @@ static int ipoctal_probe(struct ipack_device *dev) > return 0; >=20 > out_uninst: > - kfree(ipoctal); > + kref_put(&ipoctal->kref, ipoctal_release); > return res; > } >=20 > @@ -725,7 +743,7 @@ static void __ipoctal_remove(struct ipoctal = *ipoctal) > tty_unregister_driver(ipoctal->tty_drv); > kfree(ipoctal->tty_drv->name); > tty_driver_kref_put(ipoctal->tty_drv); > - kfree(ipoctal); > + kref_put(&ipoctal->kref, ipoctal_release); > } >=20 > static void ipoctal_remove(struct ipack_device *idev) > --=20 > 2.25.1 >=20