From: Rong Tao <rtoax@foxmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
Quentin Monnet <qmo@kernel.org>
Cc: ast@kernel.org, daniel@iogearbox.net, rongtao@cestc.cn,
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 v3] bpftool: Fix gen object segfault
Date: Fri, 6 Dec 2024 09:11:44 +0800 [thread overview]
Message-ID: <tencent_0F0D028440B2BE2E37547C5EFF467511FD09@qq.com> (raw)
In-Reply-To: <CAEf4BzbLmXF9XB=fBvL7NLMoPmfD=DFFvuM8Fw5h6T7vfFXUFg@mail.gmail.com>
On 12/6/24 05:34, Andrii Nakryiko wrote:
> On Thu, Dec 5, 2024 at 4:22 AM Quentin Monnet <qmo@kernel.org> wrote:
>> On 05/12/2024 12:09, 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>
>> Tested-by: Quentin Monnet <qmo@kernel.org>
>> Reviewed-by: Quentin Monnet <qmo@kernel.org>
> Isn't this papering over a deeper underlying issue? Why do we get
> SIGSEGV inside the linker at all instead of just erroring out?
> Comparison based on file path isn't a reliable way to check if input
> and output are both the same file, so this fixes the most obvious
> case, but not the actual issue.
Thanks for your replay! The current scenario is similar to the following
code.
After a.txt is opened in read mode, it is opened in write mode again, which
causes the contents of a.txt file to be cleared, resulting in no data
being read,
fpr = fopen("a.txt", "r");
fpw = fopen("a.txt", "w");
/* fgets() will get nothing, It's not glibc's fault. */
while (fgets(buff, sizeof(buff), fpr))
printf("%s", buff);
fprintf(fpw, "....");
fclose(fpr);
fclose(fpw);
corresponding to the SEGV of bpftool. Perhaps we can add the following
warning
if (x == NULL) {
fprintf(stderr, "Maybe the file was opened for writing after
opened for read\n");
return -EINVAL;
}
Whether this warning can be added may depend on libelf's processing. I will
try to fix this SEGV in libbpf, hopefully it can be fixed.
Thanks,
Rong Tao
>> Thank you!
next prev parent reply other threads:[~2024-12-06 1:11 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-05 12:09 [PATCH bpf-next v3] bpftool: Fix gen object segfault Rong Tao
2024-12-05 12:22 ` Quentin Monnet
2024-12-05 21:34 ` Andrii Nakryiko
2024-12-06 1:11 ` Rong Tao [this message]
2024-12-06 1:56 ` Quentin Monnet
2024-12-06 2:06 ` Rong Tao
2024-12-06 2:22 ` Rong Tao
2024-12-06 10:23 ` 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=tencent_0F0D028440B2BE2E37547C5EFF467511FD09@qq.com \
--to=rtoax@foxmail.com \
--cc=andrii.nakryiko@gmail.com \
--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=rongtao@cestc.cn \
--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