From patchwork Fri Dec 6 02:01:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rong Tao X-Patchwork-Id: 13896272 X-Patchwork-Delegate: bpf@iogearbox.net Received: from out203-205-221-205.mail.qq.com (out203-205-221-205.mail.qq.com [203.205.221.205]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 878A242A8B; Fri, 6 Dec 2024 02:02:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=203.205.221.205 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733450568; cv=none; b=rcXudLVH2cliQ0XhyjAw4K708w+ORsqS6JE17RggyKC8xiEohbr/cetRXxP5gJWxJj0n6I6cnJG8IZH/ALVWBSpRDEH1p8ufYbjL3eq7j3GP6g5Eu9zX+HOrOCwl3BUEHAKPAmER4vPwsXtgA3xupmaZvoPu7WfPencdFCjJ4xc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733450568; c=relaxed/simple; bh=wkGNtB0WQS3OQNqJX4OLkMPAek5RAnOPOLpgg97nqYY=; h=Message-ID:From:To:Cc:Subject:Date:In-Reply-To:References: MIME-Version; b=bJocy+uSH3M1ZyYwLDOJK12Jvxz4RoURYNE35ThUd8523BjrjaWF80LoCMAGpS5QCHkcEnxmbYQYXLbZIWuGLVEr5f2n/5kXuStG3sZow+VmkwXHMjX0WUp5xZkj3HuLj2U2P8jz+ogCne6lc6xehu4HwFnPQnNhoa+x8gkRS7s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com; spf=pass smtp.mailfrom=foxmail.com; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b=GnE4n1SO; arc=none smtp.client-ip=203.205.221.205 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=foxmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b="GnE4n1SO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1733450563; bh=TRPIrWGmVaaZDiqrJ8tXhEXp75+ez3kZv2elBpARgw0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GnE4n1SOTdypVa0m84BkYidOHJ808HxcRkOzupVaKP4uaw9+Q/vBHmqvfsrb36SbD zvn1s84O0Wdl3pmTEJfHGnP5W9gdjWaftoaVluZb2Z+LT9su08/q4FlStnKO5SBv2s k/pbrb5j4ujR/R3JWv0MRuqZZo4BDjwmkogMsfTg= Received: from NUC11-F41.. ([39.156.73.10]) by newxmesmtplogicsvrsza29-0.qq.com (NewEsmtp) with SMTP id 5A21214; Fri, 06 Dec 2024 10:01:26 +0800 X-QQ-mid: xmsmtpt1733450486tynsvza0n Message-ID: X-QQ-XMAILINFO: OVFdYp27KdlJ3Mini784C6Tk6iEttLsaa8NuYR/ZhgRrEZsT2Se7TSGCd3ejnl RPAy6nJxnyp0Z4jMayL3RTkwAipxnPpB3z25WHhzAELY5Eh+n4Re7OhuQSQ/3B6GUmRfxrtJOWLP WmdRoPJzhdfHgh+LQHpCyXOj4CBddkeO7WoUftGzqogbV0FEi3Oak/Mm+KdXORH/Gw3ApXJpYAUM hO9x8tV8iLgbAOKo2HsnFWHINyG9wydkMcngyH7cAG3FVgv8oMlszBcV2MRxnc3zY1rM7QxR+c0A Pm8u+ZhdXED1t8UloQ+9oYR0XZvcIYnsiLg5HHcQxa2g4eJO10FccEOPktsknQlFCSDiT/1/vNbF 3zdWGAN0Z9GMcOBD+k3GaGZN8pidNpf0uuSbeJm29ghFtx1KkHdfzNjM7M5mfZvGvjUqJBEGynGH YG8rcZpxcl0BAkVgJHZdqappLgTzOsNJv40riwhJc0E341vkmQDPbAxKHF4V9KpbgTLDwtwHjgo0 Bj9IaiZ2IqDS9fTR/iJ7TmEiTpzg3hSNIr9E0StQ+w+rZQasqjWL49b/uXrDo8VEAlNAX4d/mcUY D0NUylXNLD5xR6TMKOvwB08GOa/EE1Xhek+t+xd4Ma99efO7DBX01CskL6/Tqkux7FNSdnzKKax7 VYFZeDQO2S5r5EAEcsQZkA0d0SkctEMcZmGsej/1w4QsbF5eQl80NK/M3UGcG3gsGtAoYJGC/vEK VJ4BRZ8O7gY1JpEOgDjzu/Qw69DNt3fXCOlmMsW14zHyznmf1ugLrHsqUJytrJU49ZlMPZsuCxKo 3gZa+VcI9sW3YuTOU9RkEAdnODvOc0cxm2SibGDx9Dfbf9EO65OK/VGA9a9thJ23AFVsiG2xMuty ZecpKjjphbslCMV7J+tgJPVNkyVkewnCqx8fVbV5Du0LCKBg6B20y9MiNUdWU9cAd/Cp/h4e9mRg 6uxHccz3s5AMjiNis4SAlp9BmeMe2iura/1v3RUOtlMcrvolLvcw== X-QQ-XMRINFO: OWPUhxQsoeAVDbp3OJHYyFg= From: Rong Tao To: andrii.nakryiko@gmail.com, qmo@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, rongtao@cestc.cn Cc: Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org (open list:BPF [TOOLING] (bpftool)), linux-kernel@vger.kernel.org (open list) Subject: [PATCH bpf-next v4 1/2] bpftool: Fix gen object segfault Date: Fri, 6 Dec 2024 10:01:24 +0800 X-OQ-MSGID: <128433fc5276c75f491b9dcdfdd80c0058b69645.1733449395.git.rongtao@cestc.cn> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Rong Tao 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=, opts=) 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 --- tools/bpf/bpftool/gen.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 5a4d3240689e..e5e3e8705cc7 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; + char **argv_cpy; if (!REQ_ARGS(2)) { usage(); @@ -1887,6 +1889,17 @@ static int do_object(int argc, char **argv) output_file = GET_ARG(); + argc_cpy = argc; + argv_cpy = argv; + + /* 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"); From patchwork Fri Dec 6 02:01:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rong Tao X-Patchwork-Id: 13896271 X-Patchwork-Delegate: bpf@iogearbox.net Received: from xmbghk7.mail.qq.com (xmbghk7.mail.qq.com [43.163.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8241428684; Fri, 6 Dec 2024 02:01:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=43.163.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733450520; cv=none; b=stUgiT5WWRXGHXRUAeius1i2PlsdLP75FytkGPzaUWV3PeD4CJBxoIlf0zUKyDBip5Gn0cr8T8Gy7RwvyKNWagfi4uiM3+6gajZEWQi+xEV1efkWhhrKvzLavHqw6YdzK8Lg0GuLkH9Y95fL0IC2e0qIPv+9AM5qMmb88o8oeQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733450520; c=relaxed/simple; bh=yte/P6IfsxWYuATcLIj5mbBTp6vjiDLPzwOmfO7iACI=; h=Message-ID:From:To:Cc:Subject:Date:In-Reply-To:References: MIME-Version; b=jlsAgqoMdld1Dyiznp460zbyie4yD23G+qxNG2nPjag9D5Gdk3KKHKPWroJr9uAAdk0Y8sbzAp+WgDG4SdYXNmGqSOJeoDaMQla420ZdJ1mlM/FZ5oH8t9DAhuT2L+qLGB8hDAc/guFY3+YTLGEVrxvyAaX57ut+ruVdxAQPNwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com; spf=pass smtp.mailfrom=foxmail.com; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b=fVLmBoKP; arc=none smtp.client-ip=43.163.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foxmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=foxmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=foxmail.com header.i=@foxmail.com header.b="fVLmBoKP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1733450499; bh=6vUOr8MtWlCU0Q+z2XXJh7WEQMJ73j5q0TRg4qBeawM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fVLmBoKPwbaEe+YeoIXgFdYE4qUlP4f6ltgwXwn1bUHqkSgkVnqKshFwWonHlZu5w T2LR9w3Mo133Vfv526l8fdTGhMB4r7zXIszM6I0COWKb/5RsWebt91lPJ0cCTVTp5c V18SYRM0pM4oaWOfNwlSPYsRvha+U2ydrA6wXx2o= Received: from NUC11-F41.. ([39.156.73.10]) by newxmesmtplogicsvrszb21-0.qq.com (NewEsmtp) with SMTP id 641F6E9; Fri, 06 Dec 2024 10:01:36 +0800 X-QQ-mid: xmsmtpt1733450496taqmzdsp7 Message-ID: X-QQ-XMAILINFO: NAuAIaytDrXpkNArbYHasb+WGckfmUjYanZkVlntwh2zSHLn+UvXCjWfM3+a/b yEg4ysfdrLdXNYAxCZUHT2dc3PlC0FASsCsgsoX4urFCrD6m1fg5+31jQUreFxgdgjrtxX9aOikt pqS4K45/IH/t2kkRYAM3iH7LwPRl7rUtyR+sLAMABXpc73ULyUBOBtNjPCwGSp2mrBQ2ch7iMF82 RmgCTRTle6Dy//OIUVvED5JbSq2bPe8HCEH+8MUZVMjtX3eO75BclRwgVBESar6StAeWw176IG2z 7vSFUV+jLMMNj40+MsXYtKM+t6cSsaGbnggbBn4+WjHi7lOFX8Q1oTTNNA7weJQEJpL70F+c+PS9 ntdcLAORF+Z0bNVCArEofjHYbuNh/GIJv1+lYgHxVFkkLSEwKnydhR6f6/cHrSHh22vf7Lrnduba NzUhUQOE9JoVSOvxDBlPpNR7dYWFOP1I+O8qzQBQk4NvsApeJ5q4UMOfMcWRgb0Uhf62cqyWqeLS Vd0hKSgnc+IawX/BNWXnbFkhbJhVX0a1jyiOH9d6rB0MrCWhTxLarsNsNSP6jO+DMhOBE8j43dnp KKEP4GGnktLcVxA+nRexTvG1E2lhQjA96z2TMEmVOKGdnEqByhMqP5Lg3UyHyMFxrexwyOYuUnQD /T1gqk4SpSkYGekejq4uEn9ZLUgEKi1xeDnfxRtvnvl6m9Qoqbyz/57m2Xnw+7S6eDf9wxNhEigq JWMQRqekEzfgUuTF9dRqWbxL/J1YNmOBe0XLl+UtBESWsgiO4CEw8STc+6b+R0Yx2Ku4VA6uJebO 8kIILlhXZq0IJAdCkvtx4tIcaCxpm6jhXwAFDhU+Qa5BIypT3xlp0zloGfYCxGDnH+tPE2nxotOa Fr0qlJrGda4tF8mHAKTlROskZy/PeIm969W953oW7w2TRou4Rk4dEOmNMBUYUIqdey2gqCRTPg X-QQ-XMRINFO: OWPUhxQsoeAVDbp3OJHYyFg= From: Rong Tao To: andrii.nakryiko@gmail.com, qmo@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, rongtao@cestc.cn Cc: Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org (open list:BPF [TOOLING] (bpftool)), linux-kernel@vger.kernel.org (open list) Subject: [PATCH bpf-next v4 2/2] libbpf: linker: Avoid using object file as both input and output Date: Fri, 6 Dec 2024 10:01:34 +0800 X-OQ-MSGID: X-Mailer: git-send-email 2.47.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Rong Tao When the target file is used as input and output at the same time, the input file will read a null value, resulting in a segmentation fault. $ 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=, opts=) 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 --- tools/lib/bpf/linker.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index cf71d149fe26..0b117cc5b6e4 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -448,6 +448,11 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, if (!linker->elf) return libbpf_err(-EINVAL); + if (!strcmp(filename, linker->filename)) { + pr_warn_elf("Input and output files cannot be the same"); + return libbpf_err(-EINVAL); + } + err = err ?: linker_load_obj_file(linker, filename, opts, &obj); err = err ?: linker_append_sec_data(linker, &obj); err = err ?: linker_append_elf_syms(linker, &obj);