From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9A943B4EAB; Tue, 21 Jul 2026 20:40:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784666452; cv=none; b=oCe/HeyHPF1EKX6axBFb0ccPqCUucKx9RjyGGu/QPt/xWTbn3Kh3huJOr7IapYXnga1WEYj8hwmRae3zOBtH6dJBm+BCvVd4xp/BjftcIbupdG3jf3Ac6kXTiojgkNn5y35adPEKAaRD2oW2a0e2RjnlDSw2btuJa1Wpk3ucsuo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784666452; c=relaxed/simple; bh=q97KBT5UF4B+vRrVIjviEMJHw9foSPQBFh+ILRZbe88=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kbkIZJRKM2Ey4A8cMf5zt9qk2B7Iy9EWM52H8zuj+AIiQt68QeIh3cuK6Gtbgg4njjQ8UzSV/yg8Y6r7NgRdnoM/QU6nB7dkRbDQAygN7cVYry4/dVeJJjEGaKLEwkk/M3E855DzqDG3pllTb5T0KHjpiY0nunQ+jfD6et7wgmg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=v8p3b9Rs; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="v8p3b9Rs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9802C1F000E9; Tue, 21 Jul 2026 20:40:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784666451; bh=zyoxuavMFw3ZIfJXkf8nzxqObim7ATJ3Gd27HJHSXJA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=v8p3b9Rs6GbLkRNPv6lY8NT0rHR7dLrnSka2phzpYYqRpsux7i/e57k5UALqmpOR7 09EYVz8GWACO9wyk+M25YxqPOZqZr2kRJZYRxvCBSQqDUj64BE8v1mUDbWDR7YNZAh Ikz/SqKyzhdrdtPpA/dC+oj88+WamVG2eVGExW8M= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, sashiko-bot , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 6.6 0686/1266] perf tools: Fix thread__set_comm_from_proc() on empty comm file Date: Tue, 21 Jul 2026 17:18:43 +0200 Message-ID: <20260721152457.203463576@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721152441.786066624@linuxfoundation.org> References: <20260721152441.786066624@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Arnaldo Carvalho de Melo [ Upstream commit 31d596054550f793508abe7dd593853ece47d428 ] thread__set_comm_from_proc() calls procfs__read_str() then strips the trailing newline via comm[sz - 1] = '\0'. procfs__read_str() allocates the buffer before reading, so on an empty /proc/pid/comm (reachable during late exit teardown) it returns success with sz = 0 and an unterminated heap buffer. The sz - 1 underflow was the original sashiko finding: it writes a null byte before the allocation. But even with a sz > 0 guard on the newline strip, the unterminated buffer would still be passed to thread__set_comm() which calls strlen() — an unbounded heap read. Fix by treating sz == 0 as failure: free the buffer and return -1. This is consistent with pmu.c's perf_pmu__parse_scale/unit which already treat len == 0 from filename__read_str as an error. Fixes: 2f3027ac28bf6bc3 ("perf thread: Introduce method to set comm from /proc/pid/self") Reported-by: sashiko-bot Assisted-by: Claude:claude-opus-4.6 Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/thread.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 61e9f449c72581..ec0ccc310cdad8 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -273,6 +273,11 @@ int thread__set_comm_from_proc(struct thread *thread) if (!(snprintf(path, sizeof(path), "%d/task/%d/comm", thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) && procfs__read_str(path, &comm, &sz) == 0) { + /* sz==0: read got nothing, e.g. race during exit teardown */ + if (sz == 0) { + free(comm); + return -1; + } comm[sz - 1] = '\0'; err = thread__set_comm(thread, comm, 0); } -- 2.53.0