From nobody Mon Jun 15 15:08:18 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 3A93A32D0DC for ; Mon, 13 Apr 2026 11:57:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776081430; cv=none; b=PWsbPOhA98UmI73oWQA5wMka976+Yw6OHz6I4U1Hr5E4Bo/Aou6qHHEwWyA+6+gH1QoYqlJWveSLXszVTMvyLbt4HV2Nu+vrCMFDQ1WNfdJSejA1UOCBrxqTOBpXCX1CK6S3FSMawifju7No2R1Ru+xGStZHCN61o984sSznRm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776081430; c=relaxed/simple; bh=0bBqSUvIxzOrINLgMycPclzNqzsWJ/9kPBc7hSL1BtQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=h1Me7eugGgSjAVYMhW5PrB9QNAOjfNDSG7G5GG1z8UsoShZK3x4z42I2sQkSiHlfDBkzObURpDay1xD7ufCiUcImfCD1FN/bhjIDfOpvFqPYD64lUxv0raxpitdC9ZTwCKb2HfMYVx3XXk7/+RhsyN4BPAKdqScVdQXq1cO/wG8= 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=PuoeL84/; arc=none smtp.client-ip=209.85.214.173 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="PuoeL84/" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2b2d3a9e149so11258065ad.1 for ; Mon, 13 Apr 2026 04:57:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776081428; x=1776686228; 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; bh=RTEfxdc1l3m8peU3ZVlNLuiK9i3AY5YcO1a6Vk2UrX8=; b=PuoeL84/WerVXeYRkwEUsRk/IoBvJdXDLo3e+SnjKjjSuAwtA+C0BXhhonkwwlEo3W nt7pUXWOGex9Ikkuu0ZDlan7zx2lD/zv8mLOitWomcxutDo54+bqxkDLNWQ9ZJdZhVsW NilK3dj/uA2JaQx5jh7OGUUFHp0FT5ubYki3kfdbvq9brKtVEkxPB1dE6fd1uXd48CKn oY+OLfMBMRXZG92qY+8xq7rpejGFQwg+zkDokTtvifZQi4xr+ji0VEF46QUsbX026+Hm mUTq9YwK9KV1OmNoc/biVDHB1TxouEVwgIWuMswihAIJg0LC127r5Mz2cyZqVwQk6+im 08qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776081428; x=1776686228; 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; bh=RTEfxdc1l3m8peU3ZVlNLuiK9i3AY5YcO1a6Vk2UrX8=; b=ITWRX7r/P0gdOBwqIESD7bCF7DfTkLk/DzE1DKwGk8LVyEB1cO327N0R7xeZFkKprE YLe1uPuAv6q+GVVGjMn14T7nmMYl6rBljq1XK52UKIjfvcwIcgg2BdjB4zckQRXbvdmi jW3PhORJnurebexLnEYQirUPTchH2bkm0/W5bLLwwUrYKFEKyLjkfgBcIRvUppXPXMtG 04bE29c5PjxsRaUqlaTcrlmr1JaSxcPWG3kLBxOkBsEL3Idik48bKN3l0ScqPr4BUJlt ZW9K2EX94KBJ3FDgp65RI4YHV/smw7IweIrYmVUcSs46+LwUO3pU02KKzd1jsfW7iRST M9zg== X-Forwarded-Encrypted: i=1; AFNElJ9yyqfuK4RK3tnNE85FlTsUUDzZf2IjxO3T976bH9sC+ZOy5zYVt7qlpGqgMLKxGlg4B4C4mzBDwkXVocs=@vger.kernel.org X-Gm-Message-State: AOJu0YwmeJnXgKC+FF8hudkhr5pslnhgbfYu0ZquOf48bCANGoEiuAEY uSYxlAC4mZWMBJjObHppL53Fv89pvuizTLVHimTHZvWG2Euijlnn8qu6 X-Gm-Gg: AeBDieu1IyxsSXkDcCxDwOtsbiAy+fxNZLBTc0ks8zm7lnSsyUw4C+q3Z9DepOU+RJQ vW7/piUASftmxPNsz+nzA8bywe0xUAK89kz2ot9tzfWHUhjeL3Vbt/5w5asrra+MuV+CX63icrR knGucjB1gzTlpz0p9E4ibfofCBkG+HPE+ojWLQA59z8jiXtYhfgxteX2EmKb5gvOSmNP4+i61gE Z47U3m7CCyl/TF8CbH70ybKB2g9ulAtUyUNOeXzN5O9x6gNpu0sakFTA/pJ1g5khgwtNUpYl3gM KAg6/FXuy3VittC8Fn3iCAsoKO46AYJJz3Xg89+m9CSXIruQ24JhfHMuNNPYmdKOoUZNUXYrdeG GmKxkFIgMOM8c/5Af4uERXQUbhs85e9K17n1bOJq1AO1OTKYBGHRJCOL8x7KocRp1/eHdL5eBoU 5TKF5HwUKRBdfR+zeZuIEfdQqg8D+zrI8= X-Received: by 2002:a17:903:249:b0:2ae:47b0:dc80 with SMTP id d9443c01a7336-2b2c727c6b2mr151783215ad.11.1776081428501; Mon, 13 Apr 2026 04:57:08 -0700 (PDT) Received: from lgs.. ([2409:893d:1188:142d:db27:7a46:955d:48f7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2d4f2771esm109736585ad.63.2026.04.13.04.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Apr 2026 04:57:08 -0700 (PDT) From: Guangshuo Li To: Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , Dan Carpenter , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Guangshuo Li , stable@vger.kernel.org Subject: [PATCH v2] iio: trigger: Fix refcount leak in viio_trigger_alloc() error path Date: Mon, 13 Apr 2026 19:56:56 +0800 Message-ID: <20260413115656.2789049-1-lgs201920130244@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After device_initialize(), the lifetime of the embedded struct device is expected to be managed through the device core reference counting. In viio_trigger_alloc(), if irq_alloc_descs() or kvasprintf() fails, the error path frees trig directly with kfree() rather than releasing the device reference with put_device(). This bypasses the normal device lifetime rules and may leave the reference count of the embedded struct device unbalanced, resulting in a refcount leak and potentially leading to a use-after-free. The issue was identified by a static analysis tool I developed and confirmed by manual review. Fix this by using put_device(&trig->dev) in the failure path and let iio_trig_release() handle the final cleanup. Also update the subirq_base check in iio_trig_release() to test for >=3D 0, so that a negative error code from irq_alloc_descs() is not treated as a valid IRQ descriptor base during cleanup. Fixes: 2c99f1a09da3 ("iio: trigger: clean up viio_trigger_alloc()") Cc: stable@vger.kernel.org Signed-off-by: Guangshuo Li --- v2: - note that the issue was identified by my static analysis tool - and confirmed by manual review drivers/iio/industrialio-trigger.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-= trigger.c index 54416a384232..ab544976018f 100644 --- a/drivers/iio/industrialio-trigger.c +++ b/drivers/iio/industrialio-trigger.c @@ -509,7 +509,7 @@ static void iio_trig_release(struct device *device) struct iio_trigger *trig =3D to_iio_trigger(device); int i; =20 - if (trig->subirq_base) { + if (trig->subirq_base >=3D 0) { for (i =3D 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { irq_modify_status(trig->subirq_base + i, IRQ_NOAUTOEN, @@ -572,11 +572,11 @@ struct iio_trigger *viio_trigger_alloc(struct device = *parent, CONFIG_IIO_CONSUMERS_PER_TRIGGER, 0); if (trig->subirq_base < 0) - goto free_trig; + goto err_put; =20 trig->name =3D kvasprintf(GFP_KERNEL, fmt, vargs); if (trig->name =3D=3D NULL) - goto free_descs; + goto err_put; =20 INIT_LIST_HEAD(&trig->list); =20 @@ -594,10 +594,8 @@ struct iio_trigger *viio_trigger_alloc(struct device *= parent, =20 return trig; =20 -free_descs: - irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER); -free_trig: - kfree(trig); +err_put: + put_device(&trig->dev); return NULL; } =20 --=20 2.43.0