From nobody Mon Jun 15 19:14:34 2026 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 4F9F53750A2 for ; Mon, 13 Apr 2026 11:39:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776080381; cv=none; b=nuRB1tnNyNiu2ka38lgdZGfRFwRSUYD25hmdj+q/xC0+3RrMc0M2nnWhem9QvEggyZWYPgWwApr5QYjqIrwOIs70LBLF4WANwdrwJ7cfco3PKAzeFSFvQrITkjDuWYL2f1LjpXnjQaE/4YRUJb+hKZFwA2Q+3YnRtY/VWkk682s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776080381; c=relaxed/simple; bh=YntEt/LZoVGnThRGEiUzRWkG7HPchILZZyy2yOKUjvo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=o3uzFriYwe2nn4vBI3jDT5d394mOia2UMHTRo5jFJsU1eT+/uEI9bOJBqezjFOfXxPXRKFRHYY0P0z6WtgLUH+QBq3u4xsSJXKDMOJwyXm676f27Bxws/e0o48ZzhylbTAeinCIwQks5q/Dh3PIMGQGSHil5PLwq/ENx80AsE4k= 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=hGl2PfJT; arc=none smtp.client-ip=209.85.216.52 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="hGl2PfJT" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-35691a231a7so2791591a91.3 for ; Mon, 13 Apr 2026 04:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776080380; x=1776685180; 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=sKy2zr8o44SlqpTL5OnNaWPmsx1DrrUAZd9+1Le4qeA=; b=hGl2PfJTnjuCwxYEWAfQfovu7QfrVAkyOBKiN5iFIQyNeUXHHzmnl3AbZmVVBRKLRJ I/h5PhvSBA3ySE9hfF8RlMZUawrxGjePHufSb/pX/+V4cEnDWuQEAs1UBnCxqkZ7pzjT bsmVGTGWz8dR2s7PfCP8tt8Z7Ez4qxFthWXiV0x4vqvaypA2r83I7QPQoYR9lamm23N0 cR0vbHz5fmWdTDxcDjcBlPLBHqSIQjV29HLZKa9oqM2UPqSuwRzM3mVTvgaViULcZDhD UJ40Ns40Tp2edVMhcKJ8LBFQsGbhKsl1J70TKtoFoxmLu9t6iwuWTmo6Thj2GHXxS2XF 6Z3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776080380; x=1776685180; 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=sKy2zr8o44SlqpTL5OnNaWPmsx1DrrUAZd9+1Le4qeA=; b=ArIoti+36RmSo/uV9AvCqDfO3SGApQ19ikyF2Qp1mu9XPMYnRLJbbZsvFzxdoO8pM4 K1/hlfSkOgqfYT830CtqP0NsXxwvV6flneUXh/LrX3tUrkz8KDI5tHf92vl//5TNWFro 1Ada1l2IrN7nxO+NYDdtTGELcpb0SMY+rM8g13EoBz7qWMFMLXeu0A/ms5VCCCH+A3c+ fFNO+RZWQOen5RWOdjaWnizyhk3P2CcYALVJzUBylrIBSc6r4/zUWw3o91FIRBnem/UM h9zoRDRpqTsFdFUV21PUofNeIkZwJdcbtjN0brlaN9ARZU0pe8GMM9wbMo/vfQcl4v5Z aHMQ== X-Forwarded-Encrypted: i=1; AFNElJ8hx8XSZ/BWgH5tgzju7MMCs4lrKVcYWQ9CWOsAOeFmwqjaS5rdO73UDGYPA5UbqmKWJYjW4zv3kEeE2LE=@vger.kernel.org X-Gm-Message-State: AOJu0YzfnNtgNjsoTJqYhDuKcD0O9svVTmjLKYlmquhbmaT3XDsgGTax crdtVDBCe/hNS5ldFP5gSVyAj63wTFAuORpNke9pyst67UgyCqagILpI X-Gm-Gg: AeBDietC+z47C4tuhebsG3zAV/ECFQvrgZZXu3wQpkGRMo46ZvDLsCdK9QEZhAjlLeF wKlu43cWzN8bS37ZNaT0lgV9zVSGl9zP6rbZhHuxHV82J3T/wFYXzPH4MUYo/rVTE9fTp9el4gS oLN+NaazQ2ICkr3VCMBMGtRr8gSrvt4+EYT9+njDhNYf27Yfyq7f9/GgrSuHkJHrqTFIvJBNc9E WuzZ9xNs33HPiV8Qdj5uUB5hjfkhXokYmyK4xoQHoiNf4Q2RQaKHs6MGHQqt3tfhKdtyx6IHGPV U3UXNkTJLE2W/0HgWTYwuUmEgxrp3BLdkB9eODPS39bKpD5dtdxPahTPqmEAWCct+LE14Y3neU7 G0avAphRJfbLwCXXluCPJOIpLG3XSbK0cZ2zyg4tgPMFpEh3/TMRh/7Ef6gYJMI55el4Oppk2yJ fXXpprC7hklhHEDD5zZou0yA== X-Received: by 2002:a17:90b:1d86:b0:35b:93d8:6aaf with SMTP id 98e67ed59e1d1-35e42827d1emr12889237a91.19.1776080379468; Mon, 13 Apr 2026 04:39:39 -0700 (PDT) Received: from lgs.. ([112.224.67.108]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35e4133695bsm15853279a91.13.2026.04.13.04.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Apr 2026 04:39:39 -0700 (PDT) From: Guangshuo Li To: Vinicius Costa Gomes , Dave Jiang , Vinod Koul , Fenghua Yu , dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Guangshuo Li , stable@vger.kernel.org Subject: [PATCH v2] dmaengine: idxd: fix double free in idxd_cdev_open() error path Date: Mon, 13 Apr 2026 19:39:27 +0800 Message-ID: <20260413113927.2753349-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" When dev_set_name() or device_add() fails, the call chain is: idxd_cdev_open() -> device_initialize(fdev) -> dev_set_name() / device_add() -> failure -> put_device(fdev) -> idxd_file_dev_release() -> kfree(ctx) Then control returns to idxd_cdev_open(), where the error path continues with: failed: -> kfree(ctx) Thus, ctx is freed twice. In addition, idxd_file_dev_release() also calls ida_free() and idxd_wq_put(), but in the current code ctx->id is allocated and the wq reference is taken only after device_add() succeeds. If put_device(fdev) runs the release callback before that point, the cleanup is not balanced. The issue was identified by a static analysis tool I developed and confirmed by manual review. Allocate the file ida and take the wq reference before device_initialize(), so the device release callback can own the cleanup after put_device(). For the dev_set_name() and device_add() failure path, let put_device() and the release callback handle resource teardown. Fixes: e6fd6d7e5f0fe ("dmaengine: idxd: add a device to represent the file = opened") 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/dma/idxd/cdev.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/dma/idxd/cdev.c b/drivers/dma/idxd/cdev.c index 7e4715f92773..001d233e091c 100644 --- a/drivers/dma/idxd/cdev.c +++ b/drivers/dma/idxd/cdev.c @@ -225,6 +225,7 @@ static int idxd_cdev_open(struct inode *inode, struct f= ile *filp) struct iommu_sva *sva =3D NULL; unsigned int pasid; struct idxd_cdev *idxd_cdev; + bool wq_ref =3D false; =20 wq =3D inode_wq(inode); idxd =3D wq->idxd; @@ -280,12 +281,15 @@ static int idxd_cdev_open(struct inode *inode, struct= file *filp) } } =20 - idxd_cdev =3D wq->idxd_cdev; ctx->id =3D ida_alloc(&file_ida, GFP_KERNEL); if (ctx->id < 0) { dev_warn(dev, "ida alloc failure\n"); goto failed_ida; } + idxd_wq_get(wq); + wq_ref =3D true; + + idxd_cdev =3D wq->idxd_cdev; ctx->idxd_dev.type =3D IDXD_DEV_CDEV_FILE; fdev =3D user_ctx_dev(ctx); device_initialize(fdev); @@ -305,20 +309,23 @@ static int idxd_cdev_open(struct inode *inode, struct= file *filp) goto failed_dev_add; } =20 - idxd_wq_get(wq); mutex_unlock(&wq->wq_lock); return 0; =20 failed_dev_add: failed_dev_name: put_device(fdev); -failed_ida: + mutex_unlock(&wq->wq_lock); + return rc; failed_set_pasid: if (device_user_pasid_enabled(idxd)) idxd_xa_pasid_remove(ctx); failed_get_pasid: if (device_user_pasid_enabled(idxd) && !IS_ERR_OR_NULL(sva)) iommu_sva_unbind_device(sva); +failed_ida: + if (wq_ref) + idxd_wq_put(wq); failed: mutex_unlock(&wq->wq_lock); kfree(ctx); --=20 2.43.0