mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Quentin Monnet <qmo@kernel.org>
To: Rong Tao <rtoax@foxmail.com>,
	ast@kernel.org, daniel@iogearbox.net, rongtao@cestc.cn
Cc: Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>,
	"open list:BPF [TOOLING] (bpftool)" <bpf@vger.kernel.org>,
	open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2] bpftool: Fix gen object segfault
Date: Thu, 5 Dec 2024 11:20:45 +0000	[thread overview]
Message-ID: <e8d94128-ea41-4e72-83b4-9a6020aaca10@kernel.org> (raw)
In-Reply-To: <tencent_F62A51AFF6A38188D70664421F5934974008@qq.com>

On 05/12/2024 11:10, Rong Tao wrote:
> From: Rong Tao <rongtao@cestc.cn>
> 
> If the input file and output file are the same, the input file is cleared
> due to opening, resulting in a NULL pointer access by libbpf.
> 
>     $ bpftool gen object prog.o prog.o
>     libbpf: failed to get ELF header for prog.o: invalid `Elf' handle
>     Segmentation fault
> 
>     (gdb) bt
>     #0  0x0000000000450285 in linker_append_elf_syms (linker=0x4feda0, obj=0x7fffffffe100) at linker.c:1296
>     #1  bpf_linker__add_file (linker=0x4feda0, filename=<optimized out>, opts=<optimized out>) at linker.c:453
>     #2  0x000000000040c235 in do_object ()
>     #3  0x00000000004021d7 in main ()
>     (gdb) frame 0
>     #0  0x0000000000450285 in linker_append_elf_syms (linker=0x4feda0, obj=0x7fffffffe100) at linker.c:1296
>     1296		Elf64_Sym *sym = symtab->data->d_buf;
> 
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
> v1: https://lore.kernel.org/lkml/tencent_410B8166C55CD2AB64BDEA8E92204619180A@qq.com/
> ---
>  tools/bpf/bpftool/gen.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> index 5a4d3240689e..506d205138db 100644
> --- a/tools/bpf/bpftool/gen.c
> +++ b/tools/bpf/bpftool/gen.c
> @@ -1879,6 +1879,8 @@ static int do_object(int argc, char **argv)
>  	struct bpf_linker *linker;
>  	const char *output_file, *file;
>  	int err = 0;
> +	int argc_cpy = argc;
> +	char **argv_cpy = argv;


Oops sorry, argc_cpy and argv_cpy need to be initialised _after_ the
call to GET_ARG() below, otherwise we always start comparing output_file
with itself. Please test this code on your side, too :)

pw-bot: cr


>  
>  	if (!REQ_ARGS(2)) {
>  		usage();
> @@ -1887,6 +1889,14 @@ static int do_object(int argc, char **argv)
>  
>  	output_file = GET_ARG();
>  
> +	/* Ensure we don't overwrite any input file */
> +	while (argc_cpy--) {
> +		if (!strcmp(output_file, *argv_cpy++)) {
> +			p_err("Input and output files cannot be the same");
> +			goto out;
> +		}
> +	}
> +
>  	linker = bpf_linker__new(output_file, NULL);
>  	if (!linker) {
>  		p_err("failed to create BPF linker instance");


  reply	other threads:[~2024-12-05 11:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-05 11:10 Rong Tao
2024-12-05 11:20 ` Quentin Monnet [this message]
2024-12-05 12:02   ` Rong Tao

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=e8d94128-ea41-4e72-83b4-9a6020aaca10@kernel.org \
    --to=qmo@kernel.org \
    --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=rongtao@cestc.cn \
    --cc=rtoax@foxmail.com \
    --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

all inboxes | Powered by JetHome®