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 96CB043B3DA; Tue, 21 Jul 2026 22:11:20 +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=1784671881; cv=none; b=p2iCWOaxNPMYpViiTgUTFfJcR08FKv8hcyI/QjGzygPmsis9MRjR4JoIJVbA+eFQ1NtyrhrBnyydLa4xL2g20LZ/L1FVxzIcLyl6gq6GmXzikoYHKsivbSFjbuY+Oq4kS6XbNn0B/G76FPtbZ7HVBBxKpa17UYFvu6q3SLxA15Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784671881; c=relaxed/simple; bh=xCPhSp9Si0Jt6/9FrOk8P1znyfLJwTyjrBOufkXoK4U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ShiruVbfYUqRwsjvGmaOCy4iP6rl6mH0S11bXvGgHtW08R3q2KsPukqJJpfU2pH4EmlTAH+f47EM1kXHUbg2ZN1sE5/v7iehbKIn+Z8Rqg3hhKjXI7LU/vH5KzpUuY+UI80y3AfMAEE2t+cjQ41a2TQrN3hCOuH5hglJR8zrRVY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=I1iUJ2Ak; 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="I1iUJ2Ak" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0808D1F000E9; Tue, 21 Jul 2026 22:11:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784671880; bh=6G5jqs0XygqJ0rDnc7QDTeFLkJQTbj9COR/4kMa10vw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=I1iUJ2AkeF5fpUZ8cITwBR3YcDTuZXmemfA0LfMdMsW9qNnAHMYGfsgVFVV293Mbn dPo4wmtSQiTjfLLxfgfwDHu2VfeL/Irn8HhFxAWOry6gAfkMlErlR4uWSWdkNGAAOI OtDBjbuR2uCeJEOKG7iOVBoDwvOE8Bn4f/j8WzGs= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, sashiko-bot , Namhyung Kim , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.15 415/843] perf symbols: Fix signed overflow in sysfs__read_build_id() size check Date: Tue, 21 Jul 2026 17:20:50 +0200 Message-ID: <20260721152415.372600980@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721152405.946368001@linuxfoundation.org> References: <20260721152405.946368001@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 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Arnaldo Carvalho de Melo [ Upstream commit 6eaa8ee3e2abe5112e80e94c27196bb175689469 ] sysfs__read_build_id() reads ELF note headers from sysfs files. The note's namesz and descsz fields are used to compute the skip size: int n = namesz + descsz; if (n > (int)sizeof(bf)) Both namesz and descsz are size_t from NOTE_ALIGN() of 32-bit note header fields. Their sum can exceed INT_MAX, overflowing the signed int n to a negative value. The check n > sizeof(bf) then evaluates false (negative < positive in signed comparison), and read(fd, bf, n) reinterprets the negative n as a huge size_t count — the kernel writes up to MAX_RW_COUNT bytes into the 8192-byte stack buffer. In practice the overflow is bounded by the sysfs file's actual size, so a real sysfs notes file won't trigger it organically. But crafted input (e.g. via a mounted debugfs/sysfs image) could. Fix by validating namesz and descsz individually against the buffer size before summing, and change n to size_t to avoid the signed overflow entirely. Fixes: f1617b40596cb341 ("perf symbols: Record the build_ids of kernel modules too") Reported-by: sashiko-bot Cc: Namhyung Kim Assisted-by: Claude:claude-opus-4.6 Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/symbol-elf.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index bbc3a150597a47..e3ae6c30b6e74e 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -694,14 +694,17 @@ int sysfs__read_build_id(const char *filename, struct build_id *bid) } else if (read(fd, bf, descsz) != (ssize_t)descsz) break; } else { - int n = namesz + descsz; + size_t n; - if (n > (int)sizeof(bf)) { + /* int sum of namesz+descsz can overflow negative, bypassing size check */ + if (namesz > sizeof(bf) || descsz > sizeof(bf) - namesz) { n = sizeof(bf); pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n", __func__, filename, nhdr.n_namesz, nhdr.n_descsz); + } else { + n = namesz + descsz; } - if (read(fd, bf, n) != n) + if (read(fd, bf, n) != (ssize_t)n) break; } } -- 2.53.0