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 A2ADA35C6AC; Tue, 21 Jul 2026 21:29:53 +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=1784669394; cv=none; b=lokpTip4Uv8XM93zSkceblltf2ERzJ/E+6mv7nVeZiom5EEiANP5a2f1TU9yNwTOkPjUqtVcSoYobNTyLasUNJ3B49K+VYYo2DnVG30u8FMsfxohgnxec854uWAvzsWhcaF/15/Exka7eLWPoSII/lV5gK2nSRybXgbhOf7D8cM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784669394; c=relaxed/simple; bh=Z2j8R7GieNMwxqe9iZ8iB+VFfHDHNOQdjxldcddd6e0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NoZRnE5Og4IZydPpL2zX62Zx60TO9OB+XZkTbULNpc4/tzjJZhgfjyXQHSY1x11Hgn2ecp7tkVkUtMxmcqDFfl6+dnXIhXkO+UTdjsE6JZc1Dym5g5R4BIltr9pBe/jdFBJ18xJ2Y2tPSxnq5Rsic0TCfdJxV0MDr8YixybNqIM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=XZunwwnl; 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="XZunwwnl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1D8C1F000E9; Tue, 21 Jul 2026 21:29:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784669393; bh=MI0phwoCvU38Gl8iU2EIo7ASBq1Yj9r80/hjecfbI6k=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XZunwwnloF9frwsWXdkYivRLu3N+QOu/TocO+1DCfm42K2V45LGJ4yIkw142jqY/q tiCXJ5jcah6baUpu2GV69zJPVd/OAeIpp/TQcXLcBEhWI4I2NPK45nNmbP+MxSbVs1 rKi9f689MojYe8aEhozKDLjbyhQcPpIOdP9YczXE= 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.1 0538/1067] tools lib api: Fix missing null termination in filename__read_int/ull() Date: Tue, 21 Jul 2026 17:18:59 +0200 Message-ID: <20260721152436.644099865@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721152424.521567757@linuxfoundation.org> References: <20260721152424.521567757@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.1-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 82f53d81a7a784..e102714c6845e8 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c @@ -321,11 +321,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; } @@ -339,11 +342,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