mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Jiri Olsa <olsajiri@gmail.com>,
	aef2617b-ce03-4830-96a7-39df0c93aaad@kernel.org
Cc: qmo@kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org, Yuan Chen <chenyuan@kylinos.cn>
Subject: Re: [PATCH v5] bpftool: Add CET-aware symbol matching for x86_64 architectures
Date: Wed, 23 Jul 2025 08:52:06 -0700	[thread overview]
Message-ID: <74709a08-4536-4c5a-8140-12d8b42e97c0@linux.dev> (raw)
In-Reply-To: <aIDe3IR2SR6S0WM9@krava>



On 7/23/25 6:08 AM, Jiri Olsa wrote:
> On Wed, Jul 23, 2025 at 10:20:43AM +0800, chenyuan_fl@163.com wrote:
>> From: Yuan Chen <chenyuan@kylinos.cn>
>>
>> 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.
>>
>> 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.
>>
>> Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>
>> ---
>>   tools/bpf/bpftool/link.c | 26 ++++++++++++++++++++++++--
>>   1 file changed, 24 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
>> index a773e05d5ade..288bf9a032a5 100644
>> --- a/tools/bpf/bpftool/link.c
>> +++ b/tools/bpf/bpftool/link.c
>> @@ -282,6 +282,28 @@ get_addr_cookie_array(__u64 *addrs, __u64 *cookies, __u32 count)
>>   	return data;
>>   }
>>   
>> +static bool
>> +symbol_matches_target(__u64 sym_addr, __u64 target_addr)
>> +{
>> +	if (sym_addr == target_addr)
>> +		return true;
>> +
>> +#if defined(__x86_64__)
>> +	/*
>> +	 * 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 (sym_addr == target_addr - 4)
>> +		return true;
>> +#endif
> looks good.. perhaps it might be too much, but should we try to read
> CONFIG_X86_KERNEL_IBT value and do the check based on that? there's
> already some code reading options in probe_kernel_image_config

Sounds a good idea. Maybe we can abstract out a helper function
based on probe_kernel_image_config() so it can be used in
both probe_kernel_image_config() and for this symbol_matches_target
case. We can have a variable like 'ibt_supported = ...' outside
the loop. In the above we can do
	if (ibt_supported && sym_addr == target_addr - 4)
		return true;

>
> jirka
>
>> +
>> +	return false;
>> +}
>> +
>>   static void
>>   show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
>>   {
>> @@ -307,7 +329,7 @@ show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
>>   		goto error;
>>   
>>   	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))
>>   			continue;
>>   		jsonw_start_object(json_wtr);
>>   		jsonw_uint_field(json_wtr, "addr", dd.sym_mapping[i].address);
>> @@ -744,7 +766,7 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info)
>>   
>>   	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))
>>   			continue;
>>   		printf("\n\t%016lx %-16llx %s",
>>   		       dd.sym_mapping[i].address, data[j].cookie, dd.sym_mapping[i].name);
>> -- 
>> 2.25.1
>>
>>


  reply	other threads:[~2025-07-23 15:52 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-23  2:20 chenyuan_fl
2025-07-23 10:24 ` Quentin Monnet
2025-07-23 13:08 ` Jiri Olsa
2025-07-23 15:52   ` Yonghong Song [this message]
2025-08-15  2:52     ` [PATCH 0/2] bpftool: Refactor config parsing and add CET symbol matching chenyuan_fl
2025-08-15  2:52       ` [PATCH v6 1/2] bpftool: Refactor kernel config reading into common helper chenyuan_fl
2025-08-15  2:52       ` [PATCH v6 2/2] bpftool: Add CET-aware symbol matching for x86_64 architectures chenyuan_fl
2025-08-18  9:55         ` Jiri Olsa
2025-08-25  2:20           ` [PATCH v7 0/2] bpftool: Refactor config parsing and add CET symbol matching chenyuan_fl
2025-08-25  2:20             ` [PATCH v7 1/2] bpftool: Refactor kernel config reading into common helper chenyuan_fl
2025-08-25 20:29               ` Yonghong Song
2025-08-25  2:20             ` [PATCH v7 2/2] bpftool: Add CET-aware symbol matching for x86_64 architectures chenyuan_fl
2025-08-25 20:39               ` Yonghong Song
2025-08-25 22:44             ` [PATCH v7 0/2] bpftool: Refactor config parsing and add CET symbol matching Jiri Olsa
2025-08-27 21:53             ` Andrii Nakryiko
2025-08-28 21:50               ` Quentin Monnet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=74709a08-4536-4c5a-8140-12d8b42e97c0@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=aef2617b-ce03-4830-96a7-39df0c93aaad@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chenyuan@kylinos.cn \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=olsajiri@gmail.com \
    --cc=qmo@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®