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 AA5932D8376; Tue, 21 Jul 2026 20:40:40 +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=1784666441; cv=none; b=u+4OgP87YnI5fVXx13Sz7lJ0AkwBkazldSiCe7UNYHa3UeCJdDAYWPSVp6/6KhJOCkOAO0tkoz1OvXPfaK/hcjWIqVw01dt/G8jm5rCkPHYvClPBD4VqJvUSRp1gyzvsfeQ5qqbZ47pHiG1cXpMhjRnVGR4lIRqy2DLVMeb0sGU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784666441; c=relaxed/simple; bh=5musZPfpIJvNRv0E5AVxTHbeV6jy7LmB6PTt7fPAmIg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KLI7VKABrIk14vAVXmsUUX5XcqJaytw+3gRrL9L+KNJ41X5leb5hDQzZQAFtEtEghC0QE9JbcuKt3gNoaAY8dJA9qLWerVvfKSw9csfgMfvKJjV+W9SA/06Z8fdWSBPYn3XmiwEsRx6dfYFCciz3UqyXxLgOH9/JCUqXzP6/9BY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=eD1sEQwO; 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="eD1sEQwO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C5C31F000E9; Tue, 21 Jul 2026 20:40:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784666440; bh=hNwm91ICfU/TvETxf+kpsqotS6iouDdR1lj302IUMA0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eD1sEQwOFmqkIsoz+kt4DbKxkZZZVLeFwWvPEWk3IWw601zlAC2u12jYMgtNH2vtL iAdhHZQWNJ5E1h0jT1U+zp9WHCl0z6CeXdaHQPtgzttwl76MMAOsCtTkkU+Uhwxj0Q NnUTyreTNHAhmf3deULYSoDCEb03pJ1tfihH/hMo= 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 0682/1266] tools lib api: Fix missing null termination in filename__read_int/ull() Date: Tue, 21 Jul 2026 17:18:39 +0200 Message-ID: <20260721152457.116778651@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-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Arnaldo Carvalho de Melo [ Upstream commit 52b1f9678499b13b7aeb0186d9c6f486c043283f ] filename__read_int() passes a stack buffer to read() using the full sizeof(line) and then hands it to atoi() without null-terminating. If a sysfs file fills the 64-byte buffer exactly, atoi() reads past the array into uninitialized stack memory. filename__read_ull_base() has the same issue with strtoull(). Fix both by reading sizeof(line) - 1 bytes and explicitly null-terminating after a successful read. Fixes: 3a351127cbc682c3 ("tools lib fs: Adopt filename__read_int from tools/perf/") Reported-by: sashiko-bot Assisted-by: Claude:claude-opus-4.6 Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/lib/api/fs/fs.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c index 5cb0eeec2c8a6c..1ea76e5ba9d482 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c @@ -293,11 +293,14 @@ int filename__read_int(const char *filename, int *value) { char line[64]; int fd = open(filename, O_RDONLY), err = -1; + ssize_t n; if (fd < 0) return -1; - if (read(fd, line, sizeof(line)) > 0) { + n = read(fd, line, sizeof(line) - 1); + if (n > 0) { + line[n] = '\0'; *value = atoi(line); err = 0; } @@ -311,11 +314,14 @@ static int filename__read_ull_base(const char *filename, { char line[64]; int fd = open(filename, O_RDONLY), err = -1; + ssize_t n; if (fd < 0) return -1; - if (read(fd, line, sizeof(line)) > 0) { + n = read(fd, line, sizeof(line) - 1); + if (n > 0) { + line[n] = '\0'; *value = strtoull(line, NULL, base); if (*value != ULLONG_MAX) err = 0; -- 2.53.0