From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6D9A4433A8; Tue, 22 Jul 2025 13:48:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753192097; cv=none; b=MTDi/EkpcRwT7KkJJXGNx9kC87CNwdWibSHIDDV14LBhymA46iZY1SvSDQSRjzAaFXCIik8x5fdb5duKNgq8XvGktZra+Sym4dWNszCZaWj1xCa6JneH1kR7l+yik6itccy1FCcwAcNdTe7Jby+f+/xRi8WEwJSm323/70g2JNs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753192097; c=relaxed/simple; bh=kpLe++4et1TsGvFIVMK91emYVsNfjAXR2AH/IQrUF0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VURWVJFVFFAGv9NMTijb1wEB0dBeAoBE7eD2/DScCfmyrp9DDkaKHnfbHTY60rGh+c/DawUnE7AEOysrdbL+YqY416Wbttxm+kJmzhjJx2FeIlJllqHLXZoU3kaXk13/Tq0giGjJ0HP3Zaf5VnMbcMzR2PdNC10sV1e2H2khj48= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Zlcinl0o; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="Zlcinl0o" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB8E9C4CEEB; Tue, 22 Jul 2025 13:48:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1753192097; bh=kpLe++4et1TsGvFIVMK91emYVsNfjAXR2AH/IQrUF0s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zlcinl0o38rq2ZY3V9RwJqqiOZ99nb9lPy9at34EiDMbW1uNLb+Obr1Rvo86kc0GS mhNgIHc7TIMCeB7cKT4wN46mB87GdTbgm46lNwLvcRd632rQtpEAumGlDbfAi9RGdD gxSrpuXxzQEG479+BinKOTSsCta0P+zsLPftQvKc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, syzbot+e2c932aec5c8a6e1d31c@syzkaller.appspotmail.com, Yonghong Song , Paul Chaignon , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.1 42/79] bpf: Reject %p% format string in bprintf-like helpers Date: Tue, 22 Jul 2025 15:44:38 +0200 Message-ID: <20250722134329.927611591@linuxfoundation.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250722134328.384139905@linuxfoundation.org> References: <20250722134328.384139905@linuxfoundation.org> User-Agent: quilt/0.68 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: Paul Chaignon [ Upstream commit f8242745871f81a3ac37f9f51853d12854fd0b58 ] static const char fmt[] = "%p%"; bpf_trace_printk(fmt, sizeof(fmt)); The above BPF program isn't rejected and causes a kernel warning at runtime: Please remove unsupported %\x00 in format string WARNING: CPU: 1 PID: 7244 at lib/vsprintf.c:2680 format_decode+0x49c/0x5d0 This happens because bpf_bprintf_prepare skips over the second %, detected as punctuation, while processing %p. This patch fixes it by not skipping over punctuation. %\x00 is then processed in the next iteration and rejected. Reported-by: syzbot+e2c932aec5c8a6e1d31c@syzkaller.appspotmail.com Fixes: 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf") Acked-by: Yonghong Song Signed-off-by: Paul Chaignon Link: https://lore.kernel.org/r/a0e06cc479faec9e802ae51ba5d66420523251ee.1751395489.git.paul.chaignon@gmail.com Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/helpers.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 94e85d311641b..be9dc396537f1 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -876,6 +876,13 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args, if (fmt[i] == 'p') { sizeof_cur_arg = sizeof(long); + if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) || + ispunct(fmt[i + 1])) { + if (tmp_buf) + cur_arg = raw_args[num_spec]; + goto nocopy_fmt; + } + if ((fmt[i + 1] == 'k' || fmt[i + 1] == 'u') && fmt[i + 2] == 's') { fmt_ptype = fmt[i + 1]; @@ -883,11 +890,9 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args, goto fmt_str; } - if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) || - ispunct(fmt[i + 1]) || fmt[i + 1] == 'K' || + if (fmt[i + 1] == 'K' || fmt[i + 1] == 'x' || fmt[i + 1] == 's' || fmt[i + 1] == 'S') { - /* just kernel pointers */ if (tmp_buf) cur_arg = raw_args[num_spec]; i++; -- 2.39.5