From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 606E81AC45D for ; Thu, 25 Jun 2026 01:31:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782351089; cv=none; b=QkX0S7MKguPuBETYR48/D4fCbA+V/dNHm3r3gFAtOlU9vLXkXzrPrvI7WsW3Uqp1FBRDmqx3avEcAD5xJ1M8a9JW8gIRUxTLG99zMcd7Z/zUGCXmTBAEAi+h2B8KKGOOgHpa7i5dcRIhnNvbg9A3aQDBZ3JnjGdlUYy1tO51VVo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782351089; c=relaxed/simple; bh=aWmWZir0YwijWpklzcODjMD2lDV3DD8aTRipZLOfda0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=SaP7Dy75zfnR9G5ys/AKbIOZ8H7Zu2QB2O40Cyq0k2nKfhHAwNroyhAJQnU7vWG3plJ9NfFe1OUfn8sKX8A5c/3V2E4scsnJpU1nz05hxVLT5xKXp3ANQNenNAhfoeAdMJDzdR3v+IWLs+YFWMkx+LdzZ6tw6raOp76vZesvTJE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 90cdb882703511f1aa26b74ffac11d73-20260625 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.12,REQID:0fa7a959-1f6c-4f50-b397-ac03451875d2,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:e7bac3a,CLOUDID:de63394e65caac224b002d7cade39287,BulkI D:nil,BulkQuantity:0,Recheck:0,SF:80|81|82|83|102|865|898,TC:nil,Content:0 |15|52,EDM:-3,IP:nil,URL:99|1,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL: 0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_ULS X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 90cdb882703511f1aa26b74ffac11d73-20260625 X-User: xiaopei01@kylinos.cn Received: from [10.42.12.251] [(10.44.16.150)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_128_GCM_SHA256 128/128) with ESMTP id 394006956; Thu, 25 Jun 2026 09:31:19 +0800 Message-ID: Date: Thu, 25 Jun 2026 09:31:16 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC] ipack: ipoctal: fix use-after-free on remove To: Shuangpeng Cc: linux-kernel@vger.kernel.org References: <78d586276bdb049f06af77d7f484116e213ba58c.1782200408.git.xiaopei01@kylinos.cn> <2424869F-ABF4-4102-8DD4-6F2E0BCAB25E@gmail.com> From: Pei Xiao In-Reply-To: <2424869F-ABF4-4102-8DD4-6F2E0BCAB25E@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 2026/6/25 03:47, Shuangpeng 写道: > Hi Pei, > > Thanks for working on this! > > I applied the patch to commit: > > 390d73adf896bf4883c7d3bcd13c1b53d64351e3 (Jun 19 2026) > > and reran the same reproducer. The original slab-use-after-free report did > not show up in the same form, but the reproducer still triggers another > KASAN r > eport in ipoctal_write_tty(): Hi shuangpeng, I really appreciate that you've spent so much time helping me test this patch. I thought it would just go unnoticed. /I've made another revision. Could you please test it again?/ Thanks! > [ 73.728267] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN PTI > [ 73.730941] KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] > [ 73.731780] CPU: 0 UID: 0 PID: 8289 Comm: ipoctal_fd_afte Not tainted 7.1.0-10997-g390d73adf896-dirty #33 PREEMPT(full) > [ 73.734223] RIP: 0010:ipoctal_write_tty (drivers/ipack/devices/ipoctal.c:453 drivers/ipack/devices/ipoctal.c:470) > > Call Trace: > [ 73.746486] n_tty_write (drivers/tty/n_tty.c:2388) > [ 73.752256] file_tty_write (drivers/tty/tty_io.c:1006 drivers/tty/tty_io.c:1081) > [ 73.752746] vfs_write (fs/read_write.c:595 fs/read_write.c:687) > [ 73.754695] ksys_write (fs/read_write.c:739) > [ 73.756837] do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94) > [ 73.757309] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) > > So the same open-tty-fd-after-tpci200-unbind reproducer still crashes the > kernel after this patch. > > Best, > Shuangpeng > > > >> On Jun 23, 2026, at 03:50, Pei Xiao wrote: >> >> 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. >> >> Fix this 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. >> >> 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 | 19 +++++++++++++++++-- >> 1 file changed, 17 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/ipack/devices/ipoctal.c b/drivers/ipack/devices/ipoctal.c >> index 1bbefc6de708..079bda93ad5a 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 @@ >> >> static const struct tty_operations ipoctal_fops; >> >> +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; >> }; >> >> 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; >> >> + kref_get(&ipoctal->kref); >> tty->driver_data = channel; >> >> return 0; >> @@ -666,6 +671,7 @@ static void ipoctal_cleanup(struct tty_struct *tty) >> >> /* release the carrier driver */ >> ipack_put_carrier(ipoctal->dev); >> + kref_put(&ipoctal->kref, ipoctal_release); >> } >> >> static const struct tty_operations ipoctal_fops = { >> @@ -683,6 +689,13 @@ static const struct tty_operations ipoctal_fops = { >> .cleanup = ipoctal_cleanup, >> }; >> >> +static void ipoctal_release(struct kref *kref) >> +{ >> + struct ipoctal *ipoctal = container_of(kref, struct ipoctal, kref); >> + >> + kfree(ipoctal); >> +} >> + >> static int ipoctal_probe(struct ipack_device *dev) >> { >> int res; >> @@ -692,6 +705,8 @@ static int ipoctal_probe(struct ipack_device *dev) >> if (ipoctal == NULL) >> return -ENOMEM; >> >> + kref_init(&ipoctal->kref); >> + >> ipoctal->dev = dev; >> res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot); >> if (res) >> @@ -701,7 +716,7 @@ static int ipoctal_probe(struct ipack_device *dev) >> return 0; >> >> out_uninst: >> - kfree(ipoctal); >> + kref_put(&ipoctal->kref, ipoctal_release); >> return res; >> } >> >> @@ -725,7 +740,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); >> } >> >> static void ipoctal_remove(struct ipack_device *idev) >> -- >> 2.25.1 >>