From nobody Sun Jun 14 06:09:01 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 5B767B672 for ; Sat, 2 May 2026 08:07:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777709242; cv=none; b=uMeIrQQ9WNgMAX5VexNmuGqBrUQ4voVhjHKqKkD9ia9Tr+RURK0fbYbKu/yF7IJpZISuOt8GMElJhZncsUzUamsKH8deVHHK+ncHHb9MOrhpkh9cUzjg3xnfUHEHeECNe574axestU0iZzhKVzX8NHPbxje+tuUiQJqcWwCJutI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777709242; c=relaxed/simple; bh=Y0GsKMub/4OTR8Zx0YcZL3MjZWu2UMTbWQrzAySvMSk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=lLx8JJJxuXfqi8ZhZHl1fK7DYBnMQfWUz+gEgf64KplEOf3swR5VjhKb4d/j+1eN/cHBI0WttezAk3In+hZo3eJAegqJpYIgbYGPWCaaXrHwaBxQ5bBNfjRvJQMUcaJrPmmVOY+EkhsnHA22NDONzAYwDYVz6MFmL8Ry19rUsGU= 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=mJcRV2T2; arc=none smtp.client-ip=209.85.214.172 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="mJcRV2T2" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2ab46931cf1so22838185ad.0 for ; Sat, 02 May 2026 01:07:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777709241; x=1778314041; 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=PWxVmRja0x7kd8GXoEoQw2TSVqotoHY1pCEAPI69Us0=; b=mJcRV2T2iex71CjfSdmKKIaKaPQfqgOSuncHEykZPDHJRhoh9avC5pvTfN7fTXcH3H OQWYVwrGO3AYQ1/RD6uGQjlg4K2sHGk7694Knw1ndLBmX84mhGIDtfzAx6iWJ+7TqT9W Hlll/1129j0EYuBPUhc/dfq1U8FEKcA26/JHZOAkVJo+QL2qmfop59yb8bCaQ3wBD1Un 2GpznAEP7TdrtQ2Q+aDJeKIxiwRHho89hLj6OKiLh25H+A0ZmS7C6NPeOMmwVhnNXTt+ SLuuY9jpZtjwW7j1pUFuX7aQGUh4n6Z5nJGT84KKtcJFWtMnDyI8M4gTpw+mENtm72EH h8Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777709241; x=1778314041; 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=PWxVmRja0x7kd8GXoEoQw2TSVqotoHY1pCEAPI69Us0=; b=qU6JHS/7/hDKRM7Rk/T2nZjCyAN/K9eaOk+UjER8DTjUsBe3uFsnU1Vy696a51B/nf oVkXi30JQPw8EpYSZuZfpDtGMnOnu2/L5V6rNUx1/l1MO8Foz6l9Vt6MK3L2UfLCTNin ylYj0GsNZZUP76HjkdMzzlB/KBZq+jSVc9VePmQCHDkcUl68ZLm9NlB7O6e+3sYUo/3r 4T9/KZDMEdR+X4QxLuuqB37/78+8EzGFHJsmqJ7PWaP6cN+ZmwMM1sSEsfqIlBx4cqjJ eysUrzDtBPYDsCUy8Qra7abjlbL6Gs1YzMnOJaYQVKltbi5FB2E1BsrLLQAdLSHcF//J 2eWA== X-Forwarded-Encrypted: i=1; AFNElJ/AbVJ2fwfiIe6ZjMl6NPSJ5GB2u+YGwQ0k9KkavGhYso11ngv1lMn/yti/8DNBjB/0U054+IdREzmpMwg=@vger.kernel.org X-Gm-Message-State: AOJu0Yw3gTvc4eRuf4r/w224LIGObub7xemHAqTfxXj3WVtjvwNIxXkO fNe5qrJRzWg42cwdK/lrqCo7rwc3XKIUhmDMg5LHacZ7WrC3sVO17DEP X-Gm-Gg: AeBDieuri/7Aru/H6CrL47r6ag1k+cJTHpFQ6z9Ko54o8+a9tEtugMyyrOAvOf76gsl 2NURJ9ygLAIZFS0amejT+BkewxKdLOtV0cwTqtH8+CIqnjg+9RBrYrT0Wc0kvmvwlB9C8WKWmku JeQsGMcKfSqxKZDCUaJMkYHjvPaPexFZ12xsmwlRxgfqRiSynydPIVif24lWqNadQ4SrN0nwGQ6 msumdLhxYD9lxVaNV6N1JOFWCIvRbx4TmxWE//MJGHKzH/p/R2XLRGFqYjpysagcjrL822LDEsm 3X1b7lHC9JNoE9SUqLW3HFJK0Au6pLImCxENMGA4CHMjXKrp5FSD7FuhOpSWX3ImHqMVMCdzVeM oEeA+hdBg+m9pNyHg087YH6hvMoBuzDY7IUoSI6bkgXbWyhmeq2qPJJw0/+eU3NUgV3lschJRkg adv7MLymrVmR/TkSnxVX7KkE0= X-Received: by 2002:a17:903:3c65:b0:2b0:4fb3:c771 with SMTP id d9443c01a7336-2b9a42ab0b9mr82316735ad.6.1777709240613; Sat, 02 May 2026 01:07:20 -0700 (PDT) Received: from lgs.. ([118.193.39.24]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b9caaaec82sm42845135ad.24.2026.05.02.01.07.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2026 01:07:20 -0700 (PDT) From: Guangshuo Li To: Borislav Petkov , Tony Luck , Qiushi Wu , Doug Thompson , Greg Kroah-Hartman , linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Guangshuo Li Subject: [PATCH v3] EDAC/sysfs: Fix kobject cleanup after kobject_init_and_add() failure Date: Sat, 2 May 2026 16:06:47 +0800 Message-ID: <20260502080647.522511-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" If kobject_init_and_add() fails, the initialized kobject should be released with kobject_put(). Otherwise the kobject may leak resources associated with it. Some EDAC sysfs error paths currently drop the parent kobject reference directly after kobject_init_and_add() fails. However, the corresponding release callbacks of the child kobjects already drop those parent references. Call kobject_put() on the initialized child kobject instead, so the release callbacks can unwind the references properly. In edac_device_register_sysfs_main_kobj(), kobject_put() may call edac_device_ctrl_master_release(), which drops the module reference and frees the edac_device_ctl_info object. The error path then calls module_put(edac_dev->owner). This dereferences edac_dev after it may have been freed, causing a possible use-after-free, and also drops the module reference twice. Track whether kobject_init_and_add() has actually been called. If it has, rely on the kobject release callback to drop the module reference; otherwise, drop the module reference directly. Also handle the EDAC PCI top-level kobject setup carefully: if kobject_init_and_add() was called and failed, use kobject_put(); if it was never called, free the allocated kobject directly. These issues were found by a static analysis tool I am developing. Fixes: 17ed808ad2431 ("EDAC: Fix reference count leaks") Fixes: b2ed215a3338 ("Kobject: change drivers/edac to use kobject_init_and_= add") Signed-off-by: Guangshuo Li --- v2: - Move kobj_initialized assignment to the kobject_init_and_add() call site so it records whether the kobject has actually been initialized. v3: - Fix similar kobject_init_and_add() error paths under drivers/edac/. - Fold in the previous edac_device_create_instance() cleanup fix. - Put the initialized child kobject instead of the parent kobject. - Avoid calling kobject_put() on edac_pci_top_main_kobj if kobject_init_and_add() was not called. drivers/edac/edac_device_sysfs.c | 12 ++++++++---- drivers/edac/edac_pci_sysfs.c | 11 +++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/edac/edac_device_sysfs.c b/drivers/edac/edac_device_sy= sfs.c index fcebc4ffea26..5e6141a94716 100644 --- a/drivers/edac/edac_device_sysfs.c +++ b/drivers/edac/edac_device_sysfs.c @@ -231,6 +231,7 @@ int edac_device_register_sysfs_main_kobj(struct edac_de= vice_ctl_info *edac_dev) struct device *dev_root; const struct bus_type *edac_subsys; int err =3D -ENODEV; + bool kobj_initialized =3D false; =20 edac_dbg(1, "\n"); =20 @@ -256,6 +257,7 @@ int edac_device_register_sysfs_main_kobj(struct edac_de= vice_ctl_info *edac_dev) if (dev_root) { err =3D kobject_init_and_add(&edac_dev->kobj, &ktype_device_ctrl, &dev_root->kobj, "%s", edac_dev->name); + kobj_initialized =3D true; put_device(dev_root); } if (err) { @@ -275,8 +277,10 @@ int edac_device_register_sysfs_main_kobj(struct edac_d= evice_ctl_info *edac_dev) =20 /* Error exit stack */ err_kobj_reg: - kobject_put(&edac_dev->kobj); - module_put(edac_dev->owner); + if (kobj_initialized) + kobject_put(&edac_dev->kobj); + else + module_put(edac_dev->owner); =20 err_out: return err; @@ -523,7 +527,7 @@ static int edac_device_create_block(struct edac_device_= ctl_info *edac_dev, "%s", block->name); if (err) { edac_dbg(1, "Failed to register instance '%s'\n", block->name); - kobject_put(main_kobj); + kobject_put(&block->kobj); err =3D -ENODEV; goto err_out; } @@ -622,7 +626,7 @@ static int edac_device_create_instance(struct edac_devi= ce_ctl_info *edac_dev, if (err !=3D 0) { edac_dbg(2, "Failed to register instance '%s'\n", instance->name); - kobject_put(main_kobj); + kobject_put(&instance->kobj); goto err_out; } =20 diff --git a/drivers/edac/edac_pci_sysfs.c b/drivers/edac/edac_pci_sysfs.c index 446fef0a9399..971ec14742b9 100644 --- a/drivers/edac/edac_pci_sysfs.c +++ b/drivers/edac/edac_pci_sysfs.c @@ -176,7 +176,7 @@ static int edac_pci_create_instance_kobj(struct edac_pc= i_ctl_info *pci, int idx) edac_pci_top_main_kobj, "pci%d", idx); if (err !=3D 0) { edac_dbg(2, "failed to register instance pci%d\n", idx); - kobject_put(edac_pci_top_main_kobj); + kobject_put(&pci->kobj); goto error_out; } =20 @@ -340,6 +340,7 @@ static int edac_pci_main_kobj_setup(void) int err =3D -ENODEV; const struct bus_type *edac_subsys; struct device *dev_root; + bool kobj_initialized =3D false; =20 edac_dbg(0, "\n"); =20 @@ -374,6 +375,7 @@ static int edac_pci_main_kobj_setup(void) err =3D kobject_init_and_add(edac_pci_top_main_kobj, &ktype_edac_pci_main_kobj, &dev_root->kobj, "pci"); + kobj_initialized =3D true; put_device(dev_root); } if (err) { @@ -392,7 +394,12 @@ static int edac_pci_main_kobj_setup(void) =20 /* Error unwind statck */ kobject_init_and_add_fail: - kobject_put(edac_pci_top_main_kobj); + if (kobj_initialized) { + kobject_put(edac_pci_top_main_kobj); + goto decrement_count_fail; + } + + kfree(edac_pci_top_main_kobj); =20 kzalloc_fail: module_put(THIS_MODULE); --=20 2.43.0