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=1796326330; b=xlYMvhKQu8/eWCeOjEudxTHSPNR8tcVTztiXLlE3ZCO4eitt7tBypqd/xVqrE+D0puDt vUz+l9xvbJ/4NuGTf0V4zRcRZAydX1qDBhp/J+7uWc3cdLheoxFLTDbh67T2TVOru4zlP xFAsXNi+s11FIkIDktYzYpefTDPdPd6pj8Dn5Mv4jfDcZDwqncqE80AOqKLwzsEEeLF/Z tzGsfL9CJJyKsps0NIZbxDwNlNhv3Srmmu7Bajkcf80XN9fnQKAGuWIEf4gltFDbGmMgE WnIw/37OyQ6eoMtreDgXQ1y+zRliEcJPeopZFFpLJQKswNknrcwcZAKMEwI0b8ZrYhA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796326330; 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=9f+y7aMTyZ6OFzyB9HxD6e83R2XEUFsUTO1dD+oUbdQ=; b=fKNkmi4CXw57jIDBn3rL7HNOogcuTz1zoY27bLQErDJNI+kqborjA/StymQwWL8GmV3d oSZ2iGuDtpvnSqh0mvthLwim+dqaxWdbvofIWXj/6EXyD2MZIDz59rzoa3vGN2bdc5B/1 YtwWT2lygDSWT9rHTBBpAR5rANArxdwXw5EIJ3Xv5p0hDli/7MU4Rv8vgOn2gviaBgO/T 5xX5gxtVWKsteTyNCVs6ihlH6yjS8ELop1fnRlgbqarZg3cCKdJ+CsKUx5Iq+OaUl7afQ bt46jC/gujoCrEzrOKYA1AO80/lXKNqafqicdd1R/W6dQ9zMz3aXDKHvUBWwJv5eAZQ== 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=izIGZWrD; 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 5FA06606A332 for ; Thu, 3 Dec 2026 19:32:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5FA06606A332 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=izIGZWrD Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-8b4c8b51b58so139114b3a.1 for ; Thu, 03 Dec 2026 11:32:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796326329; x=1796931129; 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=9f+y7aMTyZ6OFzyB9HxD6e83R2XEUFsUTO1dD+oUbdQ=; b=izIGZWrD6uI9vDT9bCoxrOYyRk+RdhON9/bd4ydEQekuDe1UM3rdFSmsrJs9ylFEif /3yI1yJwhrNYiSEI3gJnbk/DFx+Te4+1fgmVmalsxPyAmVAaxAIF7+ee+uvlU/v65WVB WYdpC6YITgiW13NQvKuOzRhsWHC2mUg0U4GNg7W4uLc/zjZ+bmrRYbLF/PkkhR6zMdxh ON66yEoEgWPPXzQgtstlcXxM9hSVJP8oXB1e9ZsrNBcd9I2SitR/iql2qJnJHX0WYIY5 z2ov4zqHrgnPIFp375/Q+h9oaueGobnAvajdiXjcaqEtGT5/pWbl27sfssHBj+d1B+Mi 6foQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796326329; x=1796931129; 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=9f+y7aMTyZ6OFzyB9HxD6e83R2XEUFsUTO1dD+oUbdQ=; b=vUR56ocTUY8zyhHD6W/Hm7q49LBM+RBvdk0A4JO+GggDdKuICZfoYDo7A7Qd+UQGWi uUxq79pIP5yoskaUl4zS8Ui20S9d/JhD/q6h9O65vcJlK5+fPErDO07Kpx1JwCBNrWJ/ oxUzyKWBaBegb7QCY08x8LpHuvENp7DnYLlcRiAk9bZVt+3R5+EVxZkCrdrnooSTcLPA MPBLi+htYJvQeB9PU+82b5mne+us6x/7Iwg90l5Rtq7Wxj5orKP7VM4NZl3HuxhzqjCm /L503JcGaFEYLSpoQCBYgVfgog3I2gWOSpw3YobdscCUyp+bInD1N3irmNS4wj3riEsp cGuw== X-Forwarded-Encrypted: i=1; AL+hErGtg07brSPxKqgoO7jppcGRLL66FDTN9Nj4LJDBdz/IQupPIJlC1336rgLTVD4qm9b4mY7hUG+ULeY=@vger.kernel.org X-Gm-Message-State: APuU4Z88cGDaC0dRLsqYgjDiK8bdzT2MAo1ysJVwi6qq3Q9FZg3mqBRD wyorx8OFSohwayXMyjlbAzVwv0cJUlAmEuyu2yZTXgf87EuMjgxWaq+ucTc5j8xTssMzIg== X-Gm-Gg: AZFdTZeX0vhbiaKDGlO0dC9ifjClmlT7SLIXomw4g5b3oLiZ1S7VxmQT0OI7ZHx3lYc o/kYyMSvL23k144rticnjMWZVpMjmWpEY4m8ocAZWR7NNaZI4y7TC6jZRAersXqrLNPS1XvdZUu iuUmsQ1TNdt6yjwCU+KcjBOAoPyLfx+tlkYZtSt89mq/vvZyNtDYIWxcasjpVIXVmPrkQUOriiP xI6+4pc+y0XVpi5FEkFS/yeeYrZgRiTzyBbKw/9ssuKhCM7zRGVSbDElUvdJUiuOWWqkQpKT2zp +KFiufdtSCvl9kbvKAdB9+o0oxdR3D6I/U9OgjbWbUpt4dxGKAn0fN3YK7Q9+JLFcTh37ovr7OE 7PBi216NhGr8fJLn7GSobt9vJ0/2DCfOxtDPXZ10/l/n6mlgZvrfq45dWoB2orOIS2NNdmhJh4I auFNxtaMx3HWyCAm6FI51J/2lvC2bnxsZemMxQHfOcOl3kTGc5WXr04Ik7sMYfk9x73DmQ7dBCZ RcF4w+GWBuju24lRdBP6zxFV5/wrLb5N1k30xZb3aRCfrc/ZQ8QLU6Pb96kLJqGvPLNQTznd2gc dVgetm1+VNeP0KdSmkGa5qBktpi4W2o1+KHElnNEQUeK X-Received: by 2002:a17:90b:388c:b0:3b4:1c0c:6863 with SMTP id 98e67ed59e1d1-3b41c557600mr45733827a91.0.1796326329499; Thu, 03 Dec 2026 11:32:09 -0800 (PST) Received: from dell (ai097146.d.east.v6connect.net. [138.64.97.146]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5ddc55a47sm182189a91.13.2026.12.03.11.32.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 11:32:09 -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 v2] iio: trigger: loop: fix use-after-free on configfs removal Date: Fri, 4 Dec 2026 03:32:02 +0800 Message-ID: <20261203193202.550807-1-0xbeac@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-iio@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 loop trigger via configfs frees struct iio_loop_info in iio_loop_release() while the trigger is still referenced by an IIO device, and leaves any running loop kthread behind. Later disabling or re-enabling the buffer calls iio_loop_trigger_set_state(), dereferencing the freed iio_loop_info via iio_trigger_get_drvdata(): BUG: KASAN: slab-use-after-free in iio_loop_trigger_set_state Call Trace: iio_loop_trigger_set_state iio_trigger_detach_poll_func iio_disable_buffers __iio_update_buffers enable_store Serialize iio_loop_trigger_set_state() and iio_trig_loop_remove() with a mutex. In iio_trig_loop_remove(), stop any running kthread and clear the trigger's drvdata to NULL before calling iio_trigger_free(). In iio_loop_trigger_set_state(), return 0 on disable or -ENODEV on enable when drvdata is NULL, and avoid storing an ERR_PTR() in loop_trig->task if kthread_run() fails. Gemini was used to assist with code path verification, QEMU KASAN reproduction, and drafting the initial patch; the final code and this changelog were reviewed and edited by me. Fixes: bc2e1126eccb ("iio:trigger: Experimental kthread tight loop trigger (thread only)") Cc: stable@vger.kernel.org Signed-off-by: Yuansheng Yang <0xbeac@gmail.com> --- Changes in v2: - Rewrite the commit message to be concise (also document the existing ERR_PTR() handling in iio_loop_trigger_set_state(); no code changes). - Trim the KASAN backtrace per Documentation/process/submitting-patches.rst (drop hex offsets, addresses, PIDs, and redundant alloc/free traces). v1: https://lore.kernel.org/r/20261202034718.2917551-1-0xbeac@gmail.com Base: mainline 666539eecdf4 ("Merge tag 'assoc-array-fixes-v2'") with commit 8fb20cdf9c86 ("iio: trigger: free hrtimer and loop triggers on configfs release") applied. Related hrtimer trigger UAF report: https://lore.kernel.org/lkml/20261201154946.904264-1-rongh7426@gmail.com/ 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