bpftool: Refactor config parsing and add CET symbol matching
v8 cover bpftool: Refactor config parsing and add CET symbol matching
From: Yuan CHen <[email protected]>
1. **Refactor kernel config parsing**
- Moves duplicate config file handling from feature.c to common.c
- Keeps all existing functionality while enabling code reuse
2. **Add CET-aware symbol matching**
- Adjusts kprobe hook detection for x86_64 CET (endbr32/64 prefixes)
- Matches symbols at both original and CET-adjusted addresses
Changed in PATCH v4:
* Refactor repeated code into a function.
* Add detection for the x86 architecture.
Changed int PATH v5:
* Remove detection for the x86 architecture.
Changed in PATCH v6:
* Add new helper patch (1/2) to refactor kernel config reading
* Use the new read_kernel_config() in CET symbol matching (2/2) to check CONFIG_X86_KERNEL_IBT
Changed in PATCH v7:
* Display actual kprobe attachment addresses instead of symbol addresses
Yuan Chen (2):
bpftool: Refactor kernel config reading into common helper
bpftool: Add CET-aware symbol matching for x86_64 architectures
tools/bpf/bpftool/common.c | 93 +++++++++++++++++++++++++++++++++++++ tools/bpf/bpftool/feature.c | 86 ++-------------------------------- tools/bpf/bpftool/link.c | 38 ++++++++++++++- tools/bpf/bpftool/main.h | 9 ++++ 4 files changed, 142 insertions(+), 84 deletions(-)
Signature · 1 line
2.39.5
PATCH v8 1/2 bpftool: Refactor kernel config reading into common helper
From: Yuan Chen <[email protected]>
Extract the kernel configuration file parsing logic from feature.c into a new read_kernel_config() function in common.c. This includes:
1. Moving the config file handling and option parsing code
2. Adding required headers and struct definition
3. Keeping all existing functionality
The refactoring enables sharing this logic with other components while maintaining current behavior. This will be used by subsequent patches that need to check kernel config options.
--- tools/bpf/bpftool/common.c | 93 +++++++++++++++++++++++++++++++++++++ tools/bpf/bpftool/feature.c | 86 ++-------------------------------- tools/bpf/bpftool/main.h | 9 ++++ 3 files changed, 106 insertions(+), 82 deletions(-)
tools/bpf/bpftool/common.c +93 -0
@@ -21,6 +21,7 @@#include <sys/resource.h> #include <sys/stat.h> #include <sys/vfs.h> +#include <sys/utsname.h> #include <linux/filter.h> #include <linux/limits.h>
@@ -31,6 +32,7 @@#include <bpf/hashmap.h> #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */ #include <bpf/btf.h> +#include <zlib.h> #include "main.h"
@@ -1208,3 +1210,94 @@ int pathname_concat(char *buf, int buf_sz, const char *path,return 0 ; } + +static bool read_next_kernel_config_option (gzFile file,char *buf,size_t n, +char **value) +{ +char *sep; + +while (gzgets (file, buf, n)) { +if (strncmp (buf,"CONFIG_" ,7 )) +continue ; + + sep =strchr (buf,'=' ); +if (!sep) +continue ; + +/* Trim ending '\n' */ + buf[strlen (buf) -1 ] ='\0' ; + +/* Split on '=' and ensure that a value is present. */ + *sep ='\0' ; +if (!sep[1 ]) +continue ; + + *value = sep +1 ; +return true; + } + +return false; +} + +int read_kernel_config (const struct kernel_config_option *requested_options, +size_t num_options,char **out_values, +const char *define_prefix) +{ +struct utsname utsn; +char path[PATH_MAX ]; +gzFile file =NULL ; +char buf[4096 ]; +char *value; +size_t i; +int ret =0 ; + +if (!requested_options || !out_values || num_options ==0 ) +return -1 ; + +if (!uname (&utsn)) { +snprintf (path,sizeof (path),"/boot/config-%s" , utsn.release); + +/* gzopen also accepts uncompressed files. */ + file =gzopen (path,"r" ); + } + +if (!file) { +/* Some distributions build with CONFIG_IKCONFIG=y and put the +* config file at /proc/config.gz. +*/ + file =gzopen ("/proc/config.gz" ,"r" ); + } + +if (!file) { +p_info ("skipping kernel config, can't open file: %s" , +strerror (errno)); +return -1 ; + } + +if (!gzgets (file, buf,sizeof (buf)) || !gzgets (file, buf,sizeof (buf))) { +p_info ("skipping kernel config, can't read from file: %s" , +strerror (errno)); + ret =-1 ; + goto end_parse; + } + +if (strcmp (buf,"# Automatically generated file; DO NOT EDIT.\n" )) { +p_info ("skipping kernel config, can't find correct file" ); + ret =-1 ; + goto end_parse; + } + +while (read_next_kernel_config_option (file, buf,sizeof (buf), &value)) { +for (i =0 ; i < num_options; i++) { +if ((define_prefix && !requested_options[i].macro_dump) || + out_values[i] ||strcmp (buf, requested_options[i].name)) +continue ; + + out_values[i] =strdup (value); + } + } + +end_parse: +gzclose (file); +return ret; +}
tools/bpf/bpftool/feature.c +4 -82
120 lines not on this page · raw message
tools/bpf/bpftool/main.h +9 -0
@@ -275,4 +275,13 @@ int pathname_concat(char *buf, int buf_sz, const char *path,/* print netfilter bpf_link info */ void netfilter_dump_plain (const struct bpf_link_info *info);void netfilter_dump_json (const struct bpf_link_info *info,json_writer_t *wtr); + +struct kernel_config_option { +const char *name; +bool macro_dump; +}; + +int read_kernel_config (const struct kernel_config_option *requested_options, +size_t num_options,char **out_values, +const char *define_prefix);#endif
Signature · 1 line
2.39.5
PATCH v8 2/2 bpftool: Add CET-aware symbol matching for x86_64 architectures
In reply to [email protected]From: Yuan Chen <[email protected]>
Adjust symbol matching logic to account for Control-flow Enforcement Technology (CET) on x86_64 systems. CET prefixes functions with a 4-byte 'endbr' instruction, shifting the actual hook entry point to symbol + 4.
--- tools/bpf/bpftool/link.c | 54 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-)
tools/bpf/bpftool/link.c +50 -4
@@ -282,11 +282,52 @@ get_addr_cookie_array(__u64 *addrs, __u64 *cookies, __u32 count)return data; } +static bool is_x86_ibt_enabled (void ) +{ +#if defined(__x86_64__) +struct kernel_config_option options[] = { + {"CONFIG_X86_KERNEL_IBT" , }, + }; +char *values[ARRAY_SIZE (options)] = { }; +bool ret; + +if (read_kernel_config (options,ARRAY_SIZE (options), values,NULL )) +return false; + + ret = !!values[0 ]; +free (values[0 ]); +return ret; +#else +return false; +#endif +} + +static bool +symbol_matches_target (__u64 sym_addr,__u64 target_addr,bool is_ibt_enabled) +{ +if (sym_addr == target_addr) +return true; + +/* +* On x86_64 architectures with CET (Control-flow Enforcement Technology), +* function entry points have a 4-byte 'endbr' instruction prefix. +* This causes kprobe hooks to target the address *after* 'endbr' +* (symbol address + 4), preserving the CET instruction. +* Here we check if the symbol address matches the hook target address +* minus 4, indicating a CET-enabled function entry point. +*/ +if (is_ibt_enabled && sym_addr == target_addr -4 ) +return true; + +return false; +} +static void show_kprobe_multi_json (struct bpf_link_info *info,json_writer_t *wtr) {struct addr_cookie *data;__u32 i, j =0 ; +bool is_ibt_enabled;jsonw_bool_field (json_wtr,"retprobe" , info->kprobe_multi.flags &BPF_F_KPROBE_MULTI_RETURN );
@@ -306,11 +347,13 @@ show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)if (!dd.sym_count) goto error; + is_ibt_enabled =is_x86_ibt_enabled ();for (i =0 ; i < dd.sym_count; i++) { -if (dd.sym_mapping[i].address != data[j].addr) +if (!symbol_matches_target (dd.sym_mapping[i].address, + data[j].addr, is_ibt_enabled))continue ;jsonw_start_object (json_wtr); -jsonw_uint_field (json_wtr,"addr" , dd.sym_mapping[i].address); +jsonw_uint_field (json_wtr,"addr" , (unsigned long )data[j].addr);jsonw_string_field (json_wtr,"func" , dd.sym_mapping[i].name);/* Print null if it is vmlinux */ if (dd.sym_mapping[i].module[0 ] =='\0' ) {
@@ -719,6 +762,7 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info) {struct addr_cookie *data;__u32 i, j =0 ; +bool is_ibt_enabled;if (!info->kprobe_multi.count)return ;
@@ -742,12 +786,14 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info)if (!dd.sym_count) goto error; + is_ibt_enabled =is_x86_ibt_enabled ();printf ("\n\t%-16s %-16s %s" ,"addr" ,"cookie" ,"func [module]" );for (i =0 ; i < dd.sym_count; i++) { -if (dd.sym_mapping[i].address != data[j].addr) +if (!symbol_matches_target (dd.sym_mapping[i].address, + data[j].addr, is_ibt_enabled))continue ;printf ("\n\t%016lx %-16llx %s" , - dd.sym_mapping[i].address, data[j].cookie, dd.sym_mapping[i].name); + (unsigned long )data[j].addr, data[j].cookie, dd.sym_mapping[i].name);if (dd.sym_mapping[i].module[0 ] !='\0' )printf (" [%s] " , dd.sym_mapping[i].module);else
Signature · 1 line
2.39.5
Quentin Monnet
maintainer2025-08-29 07:11 UTC+0100 ~ [email protected]
Quoted 17 lines
From: Yuan CHen <[email protected]>
1. **Refactor kernel config parsing**
- Moves duplicate config file handling fromfeature.ctocommon.c
- Keeps all existing functionality while enabling code reuse2. **Add CET-aware symbol matching**
- Adjusts kprobe hook detection forx86_64CET (endbr32/64 prefixes)
- Matches symbols at both original and CET-adjusted addressesChanged in PATCH v4:
* Refactor repeated code into a function.
* Add detection for the x86 architecture.Changed int PATH v5:
* Remove detection for the x86 architecture.
Quoted 6 lines
Changed in PATCH v6:
* Add new helper patch (1/2) to refactor kernel config reading
* Use the newread_kernel_config()in CET symbol matching (2/2) to checkCONFIG_X86_KERNEL_IBTChanged in PATCH v7:
* Display actual kprobe attachment addresses instead of symbol addresses
Hi Yuan,
Is there any difference between v7 and v8 of your series? They seem identical, from what I can see.
Quentin
Quentin Monnet
maintainer2025-09-01 10:32 UTC+0100 ~ Quentin Monnet <[email protected]>
Quoted 22 lines
2025-08-29 07:11 UTC+0100 ~ [email protected]
Quoted 21 lines
From: Yuan CHen <[email protected]>
1. **Refactor kernel config parsing**
- Moves duplicate config file handling fromfeature.ctocommon.c
- Keeps all existing functionality while enabling code reuse2. **Add CET-aware symbol matching**
- Adjusts kprobe hook detection forx86_64CET (endbr32/64 prefixes)
- Matches symbols at both original and CET-adjusted addressesChanged in PATCH v4:
* Refactor repeated code into a function.
* Add detection for the x86 architecture.Changed int PATH v5:
* Remove detection for the x86 architecture.Changed in PATCH v6:
* Add new helper patch (1/2) to refactor kernel config reading
* Use the newread_kernel_config()in CET symbol matching (2/2) to checkCONFIG_X86_KERNEL_IBT
Quoted 7 lines
Quoted 2 lines
Changed in PATCH v7:
* Display actual kprobe attachment addresses instead of symbol addressesHi Yuan,
Is there any difference between v7 and v8 of your series? They seem identical, from what I can see.
From Yuan's reply (please let's stick to the ML :) )
Quoted 2 lines
The v7 and v8 patches are identical in code changes. The only update in v8 is the addition of "Ack-by" tags in commit messages.
OK thanks for confirming! No need to repost just to add the Ack tags next time, maintainers can pick them up automatically before applying to their tree.
Thanks,
Quentin
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 11 lines · On Fri, 29 Aug 2025 07:11:05 +0100 you wrote:
From: Yuan CHen <[email protected]>
1. **Refactor kernel config parsing**
- Moves duplicate config file handling fromfeature.ctocommon.c
- Keeps all existing functionality while enabling code reuse2. **Add CET-aware symbol matching**
- Adjusts kprobe hook detection forx86_64CET (endbr32/64 prefixes)
- Matches symbols at both original and CET-adjusted addresses[...]
Here is the summary with links:
- [v8,1/2] bpftool: Refactor kernel config reading into common helper
https://git.kernel.org/bpf/bpf-next/c/70f32a10ad42 - [v8,2/2] bpftool: Add CET-aware symbol matching for x86_64 architectures https://git.kernel.org/bpf/bpf-next/c/6417ca85305e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html