Skip to contentSkip to conversation

bpf: fix incorrect name check pass logic in btf_name_valid_section

PATCH Applied Jeongjun Park 3 participants 5 messages Aug 31, 2024 – Sep 4, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover bpf: fix incorrect name check pass logic in btf_name_valid_section

This patch was written to fix an issue where btf_name_valid_section() would not properly check names with certain conditions and would throw an OOB vuln. And selftest was added to verify this patch.

Jeongjun Park (2):
bpf: add check for invalid name in btf_name_valid_section() selftest/bpf : Add a selftest test case to check for incorrect names

  kernel/bpf/btf.c                             |  4 ++-
  tools/testing/selftests/bpf/prog_tests/btf.c | 34 ++++++++++++++++++++
  2 files changed, 37 insertions(+), 1 deletion(-)
#1 lore raw

PATCH v2 1/2 bpf: add check for invalid name in btf_name_valid_section()

If the length of the name string is 1 and the value of name[0] is NULL byte, an OOB vulnerability occurs in btf_name_valid_section() and the return value is true, so the invalid name passes the check.

To solve this, you need to check if the first position is NULL byte and if the first character is printable.

Fixes bd70a8fb7ca4 ("bpf: Allow all printable characters in BTF DATASEC names")
---
 kernel/bpf/btf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +3 -1
index 520f49f422fe..f1e91bf367fa 100644
@@ -823,9 +823,11 @@ static bool btf_name_valid_section(const struct btf *btf, u32 offset)
 	const char *src = btf_str_by_offset(btf, offset);
 	const char *src_limit;
 
+	if (!*src)
+		return false;
+
 	/* set a limit on identifier length */
 	src_limit = src + KSYM_NAME_LEN;
-	src++;
 	while (*src && src < src_limit) {
 		if (!isprint(*src))
 			return false;

--

#2 lore raw

PATCH v2 2/2 selftest/bpf : Add a selftest test case to check for incorrect names

In reply to Jeongjun Park

Add selftest for cases where btf_name_valid_section() does not properly check for certain types of names.

---
 tools/testing/selftests/bpf/prog_tests/btf.c | 34 ++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/testing/selftests/bpf/prog_tests/btf.c +34 -0
index 00965a6e83bb..61de88cf4ad0 100644
@@ -3550,6 +3550,40 @@ static struct btf_raw_test raw_tests[] = {
 	},
 	BTF_STR_SEC("\0x\0?.foo bar:buz"),
 },
+{
+	.descr = "datasec: name with non-printable first char not is ok",
+	.raw_types = {
+		/* int */
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+		/* VAR x */                                     /* [2] */
+		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
+		BTF_VAR_STATIC,
+		/* DATASEC ?.data */                            /* [3] */
+		BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
+		BTF_VAR_SECINFO_ENC(2, 0, 4),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0x\0\7foo"),
+	.err_str = "Invalid name",
+	.btf_load_err = true,
+},
+{
+	.descr = "datasec: name '\\0' is not ok",
+	.raw_types = {
+		/* int */
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+		/* VAR x */                                     /* [2] */
+		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
+		BTF_VAR_STATIC,
+		/* DATASEC \0 */                                /* [3] */
+		BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
+		BTF_VAR_SECINFO_ENC(2, 0, 4),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0x\0"),
+	.err_str = "Invalid name",
+	.btf_load_err = true,
+},
 {
 	.descr = "type name '?foo' is not ok",
 	.raw_types = {

--

#4 lore raw
In reply to Jeongjun Park
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Sat, 31 Aug 2024 14:45:25 +0900 you wrote:

This patch was written to fix an issue where btf_name_valid_section() would not properly check names with certain conditions and would throw an OOB vuln. And selftest was added to verify this patch.

Jeongjun Park (2):
bpf: add check for invalid name in btf_name_valid_section() selftest/bpf : Add a selftest test case to check for incorrect names

[...]

Here is the summary with links:
- [bpf,v2,1/2] bpf: add check for invalid name in btf_name_valid_section()

    https://git.kernel.org/bpf/bpf/c/bb6705c3f93b
  - [bpf,v2,2/2] selftest/bpf : Add a selftest test case to check for incorrect names
    https://git.kernel.org/bpf/bpf/c/743070894724

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html