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 6ACC621A420; Tue, 26 Aug 2025 13:37:26 +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=1756215446; cv=none; b=hU6WWY/rX+sEYyihxQN8UEjWdwivjlbBZQdtCIaBruhszw6cbWbsd5jVm/ukDB3HXadw9v5ZtqKlHMy9jeg2nlAaWClJ4kFpG30HV/+iMzDtigmNzrfWVtFlHAMqTlFykFecA+4KRrk5kxYTsfoF2Pqi/7/I3Ji8b/UIlHOcduY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756215446; c=relaxed/simple; bh=oSu8S8hUkNmtkLzzy0bdOfg4KLUb94ddke7Eiqgrpt0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M10m5YryQrktkibJRiCE1xbGiGsNCU05R5dXwWgEfdWtz5wQBh3172cSlHr67XxG/vx+h/qUcQpNT3TNUt7uv9F6x4AmrlXx68RL6RbGnYsEbTdGSw0H2wy7rtaMFDXUh/ekdDa2N00rGeCCIxWQoF8x/JRGFfOmbA1HHKN9nqQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=MwjP+aXO; 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="MwjP+aXO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED2CEC4CEF1; Tue, 26 Aug 2025 13:37:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1756215446; bh=oSu8S8hUkNmtkLzzy0bdOfg4KLUb94ddke7Eiqgrpt0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MwjP+aXOPDwXCSVZ0jt+uR0MoYPpR93TPY/2970aqjfgF/VnZwdj/KN4hpK5CigYh dD1fzbB7SPkCxN6us7vitlag8m+8TvfGU3LHL/Z7xnHfpmAzJEIB4d1jcjFSbXKARy 7hb9SFl5zYjvGd056wc7HKlZmE+d8EPn8t/fcPow= 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 5.15 037/644] bpf: Reject %p% format string in bprintf-like helpers Date: Tue, 26 Aug 2025 13:02:08 +0200 Message-ID: <20250826110947.431357368@linuxfoundation.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250826110946.507083938@linuxfoundation.org> References: <20250826110946.507083938@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 5.15-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 d3feaf6d68eba..b40ca025d9a44 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -832,6 +832,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]; @@ -839,11 +846,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