The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Tao Chen <chen.dylane@linux.dev>
To: Quentin Monnet <qmo@kernel.org>,
	ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
	yonghong.song@linux.dev, john.fastabend@gmail.com,
	kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com,
	jolsa@kernel.org
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next v2 2/2] bpftool: Fix UAF in get_delegate_value
Date: Thu, 18 Sep 2025 19:17:32 +0800	[thread overview]
Message-ID: <63eae916-a404-4fae-b870-203b7e376363@linux.dev> (raw)
In-Reply-To: <523d8d6c-de99-435f-a01b-1bac72810d53@kernel.org>

在 2025/9/18 00:30, Quentin Monnet 写道:
> 2025-09-17 11:47 UTC+0800 ~ Tao Chen <chen.dylane@linux.dev>
>> The return value ret pointer is pointing opts_copy, but opts_copy
>> gets freed in get_delegate_value before return, fix this by free
>> the mntent->mnt_opts strdup memory after show delegate value.
>>
>> Fixes: 2d812311c2b2 ("bpftool: Add bpf_token show")
>> Signed-off-by: Tao Chen <chen.dylane@linux.dev>
>> ---
>>   tools/bpf/bpftool/token.c | 75 +++++++++++++++++----------------------
>>   1 file changed, 33 insertions(+), 42 deletions(-)
>>
>> diff --git a/tools/bpf/bpftool/token.c b/tools/bpf/bpftool/token.c
>> index 82b829e44c8..05bc76c7276 100644
>> --- a/tools/bpf/bpftool/token.c
>> +++ b/tools/bpf/bpftool/token.c
>> @@ -28,15 +28,14 @@ static bool has_delegate_options(const char *mnt_ops)
>>   	       strstr(mnt_ops, "delegate_attachs");
>>   }
>>   
>> -static char *get_delegate_value(const char *opts, const char *key)
>> +static char *get_delegate_value(char *opts, const char *key)
>>   {
>>   	char *token, *rest, *ret = NULL;
>> -	char *opts_copy = strdup(opts);
>>   
>> -	if (!opts_copy)
>> +	if (!opts)
>>   		return NULL;
>>   
>> -	for (token = strtok_r(opts_copy, ",", &rest); token;
>> +	for (token = strtok_r(opts, ",", &rest); token;
>>   			token = strtok_r(NULL, ",", &rest)) {
>>   		if (strncmp(token, key, strlen(key)) == 0 &&
>>   		    token[strlen(key)] == '=') {
>> @@ -44,24 +43,19 @@ static char *get_delegate_value(const char *opts, const char *key)
>>   			break;
>>   		}
>>   	}
>> -	free(opts_copy);
>>   
>>   	return ret;
>>   }
>>   
>> -static void print_items_per_line(const char *input, int items_per_line)
>> +static void print_items_per_line(char *input, int items_per_line)
>>   {
>> -	char *str, *rest, *strs;
>> +	char *str, *rest;
>>   	int cnt = 0;
>>   
>>   	if (!input)
>>   		return;
>>   
>> -	strs = strdup(input);
>> -	if (!strs)
>> -		return;
>> -
>> -	for (str = strtok_r(strs, ":", &rest); str;
>> +	for (str = strtok_r(input, ":", &rest); str;
>>   			str = strtok_r(NULL, ":", &rest)) {
>>   		if (cnt % items_per_line == 0)
>>   			printf("\n\t  ");
>> @@ -69,38 +63,39 @@ static void print_items_per_line(const char *input, int items_per_line)
>>   		printf("%-20s", str);
>>   		cnt++;
>>   	}
>> -
>> -	free(strs);
>>   }
>>   
>> +#define PRINT_DELEGATE_OPT(opt_name) do {		\
>> +	char *opts, *value;				\
>> +	opts = strdup(mntent->mnt_opts);		\
>> +	value = get_delegate_value(opts, opt_name);	\
>> +	print_items_per_line(value, ITEMS_PER_LINE);	\
>> +	free(opts);					\
>> +} while (0)
> 
> 
> Thanks! The fix looks OK to me, but why do you need to have
> PRINT_DELEGATE_OPT*() as macros? Can't you just use functions instead?
> 
> Quentin

Well, actually, there is no special purpose about using macros, i will
use functions as you and Andrri suggested, thanks.

-- 
Best Regards
Tao Chen

      parent reply	other threads:[~2025-09-18 11:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-17  3:47 [PATCH bpf-next v2 1/2] bpftool: Add HELP_SPEC_OPTIONS in token.c Tao Chen
2025-09-17  3:47 ` [PATCH bpf-next v2 2/2] bpftool: Fix UAF in get_delegate_value Tao Chen
2025-09-17 16:30   ` Quentin Monnet
2025-09-17 21:00     ` Andrii Nakryiko
2025-09-18 11:17     ` Tao Chen [this message]

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=63eae916-a404-4fae-b870-203b7e376363@linux.dev \
    --to=chen.dylane@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=qmo@kernel.org \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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