From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 6CC4B27FB3A for ; Sat, 11 Apr 2026 08:21:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775895683; cv=none; b=Vh2YEeuL9b5oFTIdXWxmNkhGedqFkLCpa1e0NBFqlh5xgoFOtHJ8eKvf6DRl+b2Mkr17doyRJ4jmW/mpyLA2YslDMBZRh/x8QPfmh5U+3gCgjkfqah50t/XJLhQ7GVV1ryJ0YUKxFKU8MqX7TX35AptQYbX6rL1rd3p8rmDM4bI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775895683; c=relaxed/simple; bh=vcBjvKGJQYEODT9LGC4wGmOBHIm2sXHhdRxekpKCYUk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=gC/N5AkuALmHGfEH7O/bm9myJYWdZKgrCbOwz+FiaqijJ8xUrxQ8XRTLduZp+bERpe5eSnazaTEExEq2D+QwpQkH/fL+1WYY9t3Hx9zgWlwGvNGv3g2uBDAlOe4dBkCy1LXZQ7ryVqlGlDCWkmVBY3BnB+/5DC4XhHtdnIgHv4Q= 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=hZBZIM5N; arc=none smtp.client-ip=209.85.128.46 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="hZBZIM5N" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-488e1a8ac40so157565e9.2 for ; Sat, 11 Apr 2026 01:21:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775895681; x=1776500481; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=RiA2W6ra0oBmucOD+pQuH4lyD1NksJUw1bImSOanyJE=; b=hZBZIM5NJOSF6X4LQZfSmPudh6ZP/NaUCYDgevsN/TPzKFfObKLie5ztgp9KTyZylJ Snl4wJ3BWuIlauD3PgoIXy7Pa3bEANsGDyNEXuTEnrMNkSfonRkHHBPG41iFl13RTyWh d2Im2A7P3y3FfrNsad4ai9PwSxjhDjv7DKXTwMdHu7hJs8LSFe5f1v8JegL8elFaQL1y Fu8qRvQC8xLGTbr/HQVuytJAIcTdjMFObKrC9qmlaRWuAFNQMNfMC1922PzfaKR+Ux4X Tu/0IdJYIj/4lU1jqovt4XjkiMYLdirV4CY8dFB5ePy6S8cBl1PaWVveQxVzqODu2U/3 pgbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775895681; x=1776500481; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RiA2W6ra0oBmucOD+pQuH4lyD1NksJUw1bImSOanyJE=; b=IRxgd6VjPvYhRDDkgBjvrnRUo5ThzADPQ7jMajQef87kOeJVYrI5p2/LhTWLlTODvs VGGaH7yWiW/fqKH2WFNXZf2anegkgYwWm6CqCXcy8zhQKfPlK4H8NX3YUQRfBChCJyxF wiDiiNYlnJm+CC6OSVs8AVEypDBIsIXiJiR6SlxVhXF8CoD51KgaXL5DHBDjNPVA8Rny LLC3fu45o3oSF+gOUGoH/6cyOB35ZNOiTcQ/bTtgOYDRdbywEnXbzjlFab72fU/f1sn9 /5NCksnv2eyVADkKz3E1TdHDKEMAGWzN5gAT/7BycTnfxmP055Xhe6p4/nzasUNubGQj SbJA== X-Forwarded-Encrypted: i=1; AJvYcCX5biWtTHHJeRFcJ0n3jxTwE3bvzZzjJn0BedFp/xGVns5vLz8nkQTvukCnje4+v6zTvMPi3y0MqR0hhzo=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1hz2uHQak/sVQN/3yaU10mzohqfFXG7vRodCgslb1nhepIV7K fCShjgZ7BF1VPtH7iMapUvR3JhYmRQFDQZVg0WduVmIbtl+jWCFj4KzC X-Gm-Gg: AeBDietHm5087yzd2wgsR/jaT1yM2o3cKazHZP50EMBLMRDEzsQfto3Zb+S7MqQ8LiS zDUg1pFodiFrpUAOpoFCy5OEdNZYDVdyMWqt7yDxRf1afvgT6sWXBnVPvBhY9v8xkmFH/SMGNkB v5fV8ToudEFz3uhdnnwFpOULyAGkUqZDYolmKNFhkWXWHDmh26cuAFa4dwnjs3oVYrUg2E37LZ2 CqarFpZYG3kl+Zm5cQIQc/x8/aO4Uyc62rM9ZerN77GKGMVC6qesAl45UW5U2ecmngh7yZmb4JB RaMC5a+4fK/M721eaADfLDyl1jGEF3JH+eUfOCTozfhGB7+xLckTM0hfZYRUO3S2e4etCQUMo4R R3rKOatRmT6LdJjCIO8j+SDU5ijZby/LP8BKQixkV1B+m/FTMOM4DUzUApiVitVK8mBY1yOpXCp Mkw7tBZfvIjtHXIguTeb9Ffkv6/xUETw== X-Received: by 2002:a05:600c:64c4:b0:485:30d4:6b9e with SMTP id 5b1f17b1804b1-488d686c04fmr73932525e9.21.1775895680711; Sat, 11 Apr 2026 01:21:20 -0700 (PDT) Received: from localhost ([196.207.164.177]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-488d532ef00sm147467045e9.5.2026.04.11.01.21.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2026 01:21:19 -0700 (PDT) Date: Sat, 11 Apr 2026 11:21:17 +0300 From: Dan Carpenter To: Guangshuo Li Cc: Jonathan Cameron , David Lechner , Nuno =?iso-8859-1?Q?S=E1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] iio: trigger: Fix refcount leak in viio_trigger_alloc() error path Message-ID: References: <20260411080435.2125626-1-lgs201920130244@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260411080435.2125626-1-lgs201920130244@gmail.com> On Sat, Apr 11, 2026 at 04:04:35PM +0800, Guangshuo Li wrote: > After device_initialize(), the lifetime of the embedded struct device ^^^^^ The commit message says after but you're changing the before. > 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. > > 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 >= 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 > --- > 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 = to_iio_trigger(device); > int i; > > - if (trig->subirq_base) { > + if (trig->subirq_base >= 0) { > for (i = 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; > > trig->name = kvasprintf(GFP_KERNEL, fmt, vargs); > if (trig->name == NULL) > - goto free_descs; > + goto err_put; At this point we haven't done: trig->dev.type = &iio_trig_type; or device_initialize(&trig->dev); So the original code is fine and the new code just introduces memory leaks. regards, dan carpenter > > INIT_LIST_HEAD(&trig->list); > > @@ -594,10 +594,8 @@ struct iio_trigger *viio_trigger_alloc(struct device *parent, > > return trig; > > -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; > }