From nobody Sun Feb 8 10:33:24 2026 Received: from cstnet.cn (smtp81.cstnet.cn [159.226.251.81]) (using TLSv1.2 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A90ED3A1D2 for ; Fri, 14 Nov 2025 01:57:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.226.251.81 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763085440; cv=none; b=a4z/9RV5a0WPtoqkAsVSLPSuyW87CjXU0UN81nlMMHi4U4KUjxw+445xv7Nq7tFLSvglpNs7gWoB63seCPQ4HMd+IpDdYbwy/lIcuDUgovspnVqZNwjd4yMJgtUDD58gn1+EM9SHepmhfF0HjO14P8ZAox3nw0liy9v+NPya8YI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763085440; c=relaxed/simple; bh=ZoKeU/TmuDXYJmvfJ+4uQ++RPbB22I6DZfAuEDamaZ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s9ClJGdK7+X+aouUYQPwafCPcL5023BJUvzuNAvPN74NWmZRF/Jz/4Le/CH8lG+X+7DwniUpG9Y81SK0hQa+oBamrYACgrCYAOG3ZD9YYbLLeJUmOFv/l6YnbTd5CGrQGLgeoaB9oIDjHmWxfVWZv7BQaj1TMRG1bXSAd9L7+EU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn; spf=pass smtp.mailfrom=iscas.ac.cn; arc=none smtp.client-ip=159.226.251.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iscas.ac.cn Received: from DESKTOP-L0HPE2S (unknown [124.16.141.245]) by APP-03 (Coremail) with SMTP id rQCowAAntddwjBZpO7OvAA--.1811S2; Fri, 14 Nov 2025 09:57:06 +0800 (CST) From: Haotian Zhang To: Andrew Morton , Thomas Gleixner , Kuan-Wei Chiu Cc: linux-kernel@vger.kernel.org, Haotian Zhang Subject: [PATCH v3] debugobjects: Fix inconsistent return handling and potential ERR_PTR dereference Date: Fri, 14 Nov 2025 09:56:31 +0800 Message-ID: <20251114015631.1729-1-vulab@iscas.ac.cn> X-Mailer: git-send-email 2.50.1.windows.1 In-Reply-To: <20251110075746.1680-1-vulab@iscas.ac.cn> References: <20251110075746.1680-1-vulab@iscas.ac.cn> 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 X-CM-TRANSID: rQCowAAntddwjBZpO7OvAA--.1811S2 X-Coremail-Antispam: 1UD129KBjvJXoWxCw18XF15XFy3XrW3ZryDAwb_yoW5trykpw 1SgasFyrW8trnxXr4kAa1kCw13Wa4fur17CF9Y93srArZ8ZF4fKF1rtryq9F1DurZ7XF1U Ar1jkF93Zr4UA3JanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkE14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AKxV WxJr0_GcWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2Wl Yx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbV WUJVW8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lc7CjxVAaw2AF wI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4 xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1D MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I 0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWU JVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x0JUSNt xUUUUU= X-CM-SenderInfo: pyxotu46lvutnvoduhdfq/1tbiBgwGA2kWc015zQAAs6 Content-Type: text/plain; charset="utf-8" The lookup_object_or_alloc() function can return NULL on memory allocation failure, while returning an error pointer for other errors. Call sites such as __debug_object_init() and debug_object_activate() only check for errors using IS_ERR(), which does not evaluate to true for a NULL pointer. This can lead to a NULL pointer dereference if memory allocation fails. To fix this, modify lookup_object_or_alloc() to consistently return ERR_PTR(-ENOMEM) on allocation failure. This ensures that all error paths return a valid error pointer that can be caught by IS_ERR(). Update all three call sites (__debug_object_init, debug_object_activate, and debug_object_assert_init) to use IS_ERR() for error checking and handle -ENOMEM by calling debug_objects_oom(), thus preventing the potential bug and making the error handling robust and consistent. Fixes: 63a759694eed ("debugobject: Prevent init race with static objects") Suggested-by: Kuan-Wei Chiu Signed-off-by: Haotian Zhang --- Changes in v3: -Reword commit message to clearly describe the NULL pointer dereference bug, as suggested by Kuan-Wei Chiu. -Change subject prefix to "debugobjects:". --- Changes in v2: -Make error handling consistent across all call sites as suggested by Kuan-Wei Chiu --- lib/debugobjects.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/debugobjects.c b/lib/debugobjects.c index 7f50c4480a4e..d2d1979e2d12 100644 --- a/lib/debugobjects.c +++ b/lib/debugobjects.c @@ -691,7 +691,7 @@ static struct debug_obj *lookup_object_or_alloc(void *a= ddr, struct debug_bucket =20 /* Out of memory. Do the cleanup outside of the locked region */ debug_objects_enabled =3D false; - return NULL; + return ERR_PTR(-ENOMEM); } =20 static void debug_objects_fill_pool(void) @@ -741,9 +741,10 @@ __debug_object_init(void *addr, const struct debug_obj= _descr *descr, int onstack raw_spin_lock_irqsave(&db->lock, flags); =20 obj =3D lookup_object_or_alloc(addr, db, descr, onstack, false); - if (unlikely(!obj)) { + if (IS_ERR(obj)) { raw_spin_unlock_irqrestore(&db->lock, flags); - debug_objects_oom(); + if (PTR_ERR(obj) =3D=3D -ENOMEM) + debug_objects_oom(); return; } =20 @@ -818,11 +819,13 @@ int debug_object_activate(void *addr, const struct de= bug_obj_descr *descr) raw_spin_lock_irqsave(&db->lock, flags); =20 obj =3D lookup_object_or_alloc(addr, db, descr, false, true); - if (unlikely(!obj)) { - raw_spin_unlock_irqrestore(&db->lock, flags); - debug_objects_oom(); - return 0; - } else if (likely(!IS_ERR(obj))) { + if (IS_ERR(obj)) { + if (PTR_ERR(obj) =3D=3D -ENOMEM) { + raw_spin_unlock_irqrestore(&db->lock, flags); + debug_objects_oom(); + return 0; + } + } else { switch (obj->state) { case ODEBUG_STATE_ACTIVE: case ODEBUG_STATE_DESTROYED: @@ -1007,11 +1010,11 @@ void debug_object_assert_init(void *addr, const str= uct debug_obj_descr *descr) raw_spin_lock_irqsave(&db->lock, flags); obj =3D lookup_object_or_alloc(addr, db, descr, false, true); raw_spin_unlock_irqrestore(&db->lock, flags); - if (likely(!IS_ERR_OR_NULL(obj))) + if (!IS_ERR(obj)) return; =20 /* If NULL the allocation has hit OOM */ - if (!obj) { + if (PTR_ERR(obj) =3D=3D -ENOMEM) { debug_objects_oom(); return; } --=20 2.50.1.windows.1