From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796183249; b=xX9fu3mc8BNqLi6CfyZesxktWPBco87JQWVCTH7Ifa6DmMqSZ4bUdfjxE2ZlgSI2hZWP 58XbwkONvlxEOe2woQA3wwrjUrgwe7pKk8YermgOXMtrMThieVSXeTbLVoa/MKs/qoOPH EMjPpy0rYbBJfRvL74jNRIk6uiSRznds1X564GZKJJNOQORkP4wHATJHfYMhxIcsBXeFM UI05OWliup8MlzoiZz/TSiTsZYV5ptJ+G8wF4ECC1x/E/WUtnDPydWgMFnldNHfFPHdtA BGc4dcQEfgiEGE98IGUfi6rhLMTmhEQTUNiuJJjSKRogqfPMQ9k0wqiJsuCV/D6lWhw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796183249; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=MZifnDoR4kRQeDPUAPoWCwvtNrtyXqzleBkEmnnZ2p8=; b=zh/xSpQ1SYedgyTL9cOgs3bfe61RygDau09hVSZOD04And2rsOE9jY4FZ/aNmQNl38hV yYwjpDikdjEDSsa5EITq89AZVxOlr4j2QdhoBjNM5v/ZayZZf+YMMnJRrp0EF5HN+C5k7 n97p5a1SBQNheBndZ3hdyzVT7Bck22X4Zb/PJgnyFKe4avmrNXnn3ZmNZzv+Pn0kw+CXA 6zVqcxONCXXFa/vaP1vnQEIh1LIrtoRpYIuvTrD7zYFMp3yRsehcsbKxF5w8tE2qF0xek tq6HgyTnS2BU1vvo7VtsIfAlnqKqgOiOeWsot/khoBNZuCWRDGe/1Ud5x3/5I/1j27A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=dDJ+1We1; arc=none smtp.remote-ip=209.85.210.171 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DABC606A322 for ; Wed, 2 Dec 2026 03:47:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6DABC606A322 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.a=rsa-sha256 header.s=20260707 header.b=dDJ+1We1 Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-8b5c3288e6cso5013036b3a.3 for ; Tue, 01 Dec 2026 19:47:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796183248; x=1796788048; 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=MZifnDoR4kRQeDPUAPoWCwvtNrtyXqzleBkEmnnZ2p8=; b=dDJ+1We19MMZ/5W64O7B20XtfMupya96tBwRcVpmRbXxQMTEuD2gzVQ5M/PJY8kBBf wHuVOOD9jTjdBdGymjy4ACGHFUYIvuvmv+DL3Gpq0RXMmRe9WXi6kCZzhV5roCxyIJwx tN6r5Ktr+T30kdsz4vxwtnjzgIL0gCQds++ohWypn2CGUNlcI86ylWbyio9W2bM4SXCu Lc4+oeCRdaPrMs/E+O3b9zWWLjVyWzpo0IwQEqOfusgSP8wa5x1pIkHUuITzn/C6+Squ GHKMKt9r0nd2HMPJNgZp232DO8amK5TycZMHSqpXIynfRaMO+VecmDNcEPJhfx8E6TXU uKYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796183249; x=1796788049; 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=MZifnDoR4kRQeDPUAPoWCwvtNrtyXqzleBkEmnnZ2p8=; b=M17JqzUiguuIy0hSg2P5T5mhY2aC15Vnvl+f5w6W5kq+NMpihf21lbCPhQ51OQbKkv ds6V8YxOL1F0x9tE670j4ljiVN5YHR7g7Xx68rV34FmYUuoV0mQb1HJcRMBaYnrimd5l CQzolr1E98DNvwpaBW1PQuTI+G+h9T0XNtbI0H7oRh42yGSqc8dQ1+6EPvfW68PlS/gP SvL+Hh6M87K5c9w9oXOt2E5qLz6aX4RRYELPj49gK6yVlEl9OuwbndWGgZ/uzUDN2aI9 w1n+Eyi41ehAdNvRGGBjveqO72GfVtKi9iqUm26xJfY9M36AsN/WX1ifhWICF7yUEEgB o2eA== X-Forwarded-Encrypted: i=1; AL+hErF0BEYZo6/RE48CpMLk7VBcnB9JYgjdI6+HVyPeSb+4YqlNPEIAVM5L6llXFVpztbSdCugVF6kn66re38M=@vger.kernel.org X-Gm-Message-State: APuU4Z9WEdjG4TsjXPuM0rRpScqeA2l/MkydFov9mDY8naxugIV9AmCu QEGj0+Huh4semhy+3v9gLtgyyef+rydKJELngxDsutZJCgPsxUqWAyu0dVvulw619/U= X-Gm-Gg: AZFdTZcVohPtZbWKHwymB+vyZuwz1J5MAyRM//bX7kt4tgZLWFQCz28ADncMkEM54ZI /RRT+4aiRG0oBQfU/BIYsmV3JM2Nk5G3nhMImvvDA8NQ+DVuTWCbbD18tbujd+dxS8dv2UTLEum m/Z9YLMDs8Yr1OyXAq79rcfrirNyDR0uDi4GMj5tBDua8FW0b80ZznaLpKEtlJhenC328GPlE2g G21Za9V+IkC4xbWuisvcq39hu2t/2aDFdHxJ78u9o4cexxqOlfLRobO+WSIQBWxNFPRsLbW6tID O09791+RfxJOQVAthuY+CyYwduCQmOOzkBSoK/HIbad83ThBR1IaGfXgj4/CWRqby5PHAxjZlhx 6FLXjlvK/0fxV+G8OtBamEv+r8zKUl1V9ALWFUb35OolYy6hw5aklzVEIekG1tl88QULSsHFdev tgAygi/wTE+PyA+0PS/VfJ/6ZBmP6Y2J7GZC0r9YcwD4dJge9rXKris4UDJodRKN5X/85gCenMY QCNc2bguvruW24nf+59W9pXonV2ikl9fSP8H48brGjCxdrUWEcqKcJvRW+iRhTjNZY/MFg3aWQ+ Bdio6a9cDRRxmU4= X-Received: by 2002:a05:6a00:39a3:b0:8a0:59d2:7d41 with SMTP id d2e1a72fcca58-8c87dcdf45cmr40024581b3a.56.1796183248422; Tue, 01 Dec 2026 19:47:28 -0800 (PST) Received: from dell ([58.246.183.50]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8d329855224sm9133009b3a.38.2026.12.01.19.47.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 19:47:28 -0800 (PST) From: Yuansheng Yang <0xbeac@gmail.com> To: Jonathan Cameron Cc: David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] iio: trigger: loop: fix use-after-free on configfs removal Date: Wed, 2 Dec 2026 11:47:18 +0800 Message-ID: <20261202034718.2917551-1-0xbeac@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Removing a tight-loop trigger directory via configfs while an IIO consumer still holds a reference to its iio_trigger frees iio_loop_info underneath the consumer: 1. iio_trig_loop_probe() allocates struct iio_loop_info (embedding struct iio_sw_trigger swt and storing struct task_struct *task) and sets it as the trigger's drvdata. 2. Attaching the trigger to a device's current_trigger acquires a reference on struct iio_trigger via iio_trigger_get(). 3. rmdir on the configfs trigger directory invokes iio_trig_loop_remove(), which calls iio_trigger_unregister() and iio_trigger_free() (only dropping one device reference), then configfs drops the last config_item reference and iio_loop_release() frees iio_loop_info. Any active loop kthread is also left running. 4. Disabling or re-enabling the consumer's buffer calls iio_loop_trigger_set_state(), which retrieves the freed iio_loop_info via iio_trigger_get_drvdata() and reads or writes loop_trig->task. On an x86_64 KASAN QEMU guest with commit 8fb20cdf9c86 applied, disabling the buffer after rmdir triggers: BUG: KASAN: slab-use-after-free in iio_loop_trigger_set_state+0x138/0x150 Read of size 8 at addr ffff888008b25a98 by task bash/336 Call Trace: iio_loop_trigger_set_state+0x138/0x150 iio_trigger_detach_poll_func+0x23a/0x2d0 iio_disable_buffers+0x38e/0x490 __iio_update_buffers+0x1dfc/0x2470 enable_store+0x1c1/0x1f0 Allocated by task 344: iio_trig_loop_probe+0x47/0x1d0 iio_sw_trigger_create+0x156/0x260 trigger_make_group+0x33/0x90 configfs_mkdir+0x521/0xe90 Freed by task 354: kfree+0x154/0x420 config_item_cleanup+0x143/0x1e0 config_item_put+0x91/0xa0 configfs_rmdir+0x881/0xac0 Re-enabling the buffer afterwards also triggers a slab-use-after-free write of size 8 at iio_loop_trigger_set_state+0x149/0x150. Commit 8fb20cdf9c86 ("iio: trigger: free hrtimer and loop triggers on configfs release") moved kfree(trig_info) from iio_trig_loop_remove() to iio_loop_release(), fixing the plain mkdir/rmdir config_item UAF, but left the consumer-outlives-rmdir lifetime mismatch intact. Serialize iio_loop_trigger_set_state() and iio_trig_loop_remove() with a static mutex. In iio_trig_loop_remove(), stop any running kthread after iio_trigger_unregister() and clear the trigger's drvdata to NULL before iio_trigger_free(). In iio_loop_trigger_set_state(), return 0 on disable or -ENODEV on enable once drvdata has been cleared. Discovered by manual code inspection following the sibling hrtimer trigger UAF report, with Gemini assisting in path verification, QEMU reproduction, and preparing this patch and changelog. Fixes: bc2e1126eccb ("iio:trigger: Experimental kthread tight loop trigger (thread only)") Cc: stable@vger.kernel.org Signed-off-by: Yuansheng Yang <0xbeac@gmail.com> --- Sibling hrtimer trigger UAF report on lkml/linux-iio: https://lore.kernel.org/lkml/20261201154946.904264-1-rongh7426@gmail.com/ Base: next-20261201 / fixes-togreg state of drivers/iio/trigger/iio-trig-loop.c (blob e6bea96cc565, i.e. 666539eecdf with 8fb20cdf9c86 applied). Testing: - drivers/iio/trigger/iio-trig-loop.o compiled with W=1 on x86_64 (GCC 15.2.0, CONFIG_IIO_TIGHTLOOP_TRIGGER=y, CONFIG_KASAN=y) with 0 warnings and 0 errors. - On an x86_64 KASAN QEMU guest (iio_simple_dummy with buffer support), before this fix rmdir of triggers/loop/t0 while a buffer held t0 followed by buffer disable/enable triggered KASAN slab-use-after-free reads and writes in iio_loop_trigger_set_state(). With this fix, the same sequence plus rmdir-before-enable, 10x create/bind/enable/ disable/unbind/rmdir cycles, and active kthread teardown checks completed with 0 KASAN reports or warnings; post-rmdir buffer enable returns -ENODEV cleanly. drivers/iio/trigger/iio-trig-loop.c | 43 +++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/drivers/iio/trigger/iio-trig-loop.c b/drivers/iio/trigger/iio-trig-loop.c index e6bea96cc56..661901ac95e 100644 --- a/drivers/iio/trigger/iio-trig-loop.c +++ b/drivers/iio/trigger/iio-trig-loop.c @@ -18,8 +18,10 @@ * element kfifo buffer. */ +#include #include #include +#include #include #include #include @@ -35,6 +37,12 @@ struct iio_loop_info { struct task_struct *task; }; +/* + * Serializes set_trigger_state() and remove(): iio_loop_info may be freed on + * configfs release while a consumer still holds a reference to iio_trigger. + */ +static DEFINE_MUTEX(iio_loop_lock); + static void iio_loop_release(struct config_item *item) { struct iio_sw_trigger *swt = to_iio_sw_trigger(item); @@ -66,18 +74,29 @@ static int iio_loop_thread(void *data) static int iio_loop_trigger_set_state(struct iio_trigger *trig, bool state) { - struct iio_loop_info *loop_trig = iio_trigger_get_drvdata(trig); + struct iio_loop_info *loop_trig; + struct task_struct *task; + + guard(mutex)(&iio_loop_lock); + + loop_trig = iio_trigger_get_drvdata(trig); + if (!loop_trig) + return state ? -ENODEV : 0; if (state) { - loop_trig->task = kthread_run(iio_loop_thread, - trig, trig->name); - if (IS_ERR(loop_trig->task)) { + if (loop_trig->task) + return -EBUSY; + + task = kthread_run(iio_loop_thread, trig, trig->name); + if (IS_ERR(task)) { dev_err(&trig->dev, "failed to create trigger loop thread\n"); - return PTR_ERR(loop_trig->task); + return PTR_ERR(task); } - } else { + loop_trig->task = task; + } else if (loop_trig->task) { kthread_stop(loop_trig->task); + loop_trig->task = NULL; } return 0; @@ -123,7 +142,19 @@ static struct iio_sw_trigger *iio_trig_loop_probe(const char *name) static int iio_trig_loop_remove(struct iio_sw_trigger *swt) { + struct iio_loop_info *trig_info; + iio_trigger_unregister(swt->trigger); + + scoped_guard(mutex, &iio_loop_lock) { + trig_info = iio_trigger_get_drvdata(swt->trigger); + if (trig_info && trig_info->task) { + kthread_stop(trig_info->task); + trig_info->task = NULL; + } + iio_trigger_set_drvdata(swt->trigger, NULL); + } + iio_trigger_free(swt->trigger); return 0; -- 2.53.0