Typeless/weak ksym for gen_loader + misc fixups
Subsystems (7)
Kumar Kartikeya Dwivedi
maintainerv3 cover Typeless/weak ksym for gen_loader + misc fixups
Patches (1,2,3,6) add typeless and weak ksym support to gen_loader. It is follow up for the recent kfunc from modules series.
The later patches (7,8) are misc fixes for selftests, and patch 4 for libbpf where we try to be careful to not end up with fds == 0, as libbpf assumes in various places that they are greater than 0. Patch 5 fixes up missing O_CLOEXEC in libbpf.
Please look at patch 4 and 5 closely and whether I missed any other places that need a change.
Changelog:
----------
v2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]
* Address feedback from Song
* Move ksym logging to separate helper to avoid code duplication
* Move src_reg mask stuff to separate helper
* Fix various other nits, add acks
* __builtin_expect is used instead of likely to as skel_internal.h is included in isolation.
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* Remove redundant OOM checks in emit_bpf_kallsyms_lookup_name
* Use designated initializer for sk_lookup fd array (Jakub)
* Do fd check for all fd returning low level APIs (Andrii, Alexei)
* Make Fixes: tag quote commit message, use selftests/bpf prefix (Song, Andrii)
* Split typeless and weak ksym support into separate patches, expand commit message (Song)
* Fix duplication in selftests stemming from use of LSKELS_EXTRA (Song)
Kumar Kartikeya Dwivedi (8):
bpf: Add bpf_kallsyms_lookup_name helper
libbpf: Add typeless ksym support to gen_loader
libbpf: Add weak ksym support to gen_loader
libbpf: Ensure that BPF syscall fds are never 0, 1, or 2
libbpf: Use O_CLOEXEC uniformly when opening fds selftests/bpf: Add weak/typeless ksym test for light skeleton selftests/bpf: Fix fd cleanup in sk_lookup test selftests/bpf: Fix memory leak in test_ima
include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 14 ++ kernel/bpf/syscall.c | 24 ++++ tools/include/uapi/linux/bpf.h | 14 ++ tools/lib/bpf/bpf.c | 28 ++-- tools/lib/bpf/bpf_gen_internal.h | 12 +- tools/lib/bpf/btf.c | 2 +- tools/lib/bpf/gen_loader.c | 132 ++++++++++++++++-- tools/lib/bpf/libbpf.c | 53 ++++--- tools/lib/bpf/libbpf_internal.h | 23 +++ tools/lib/bpf/libbpf_probes.c | 2 +- tools/lib/bpf/linker.c | 4 +- tools/lib/bpf/ringbuf.c | 2 +- tools/lib/bpf/skel_internal.h | 35 ++++- tools/lib/bpf/xsk.c | 6 +- tools/testing/selftests/bpf/Makefile | 7 +- .../selftests/bpf/prog_tests/ksyms_btf.c | 35 ++++- .../selftests/bpf/prog_tests/ksyms_module.c | 40 +++++- .../bpf/prog_tests/ksyms_module_libbpf.c | 28 ---- .../selftests/bpf/prog_tests/sk_lookup.c | 14 +- .../selftests/bpf/prog_tests/test_ima.c | 3 +- .../selftests/bpf/progs/test_ksyms_weak.c | 3 +- 22 files changed, 367 insertions(+), 115 deletions(-) delete mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 1/8 bpf: Add bpf_kallsyms_lookup_name helper
This helper allows us to get the address of a kernel symbol from inside a BPF_PROG_TYPE_SYSCALL prog (used by gen_loader), so that we can relocate typeless ksym vars.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 14 ++++++++++++++ kernel/bpf/syscall.c | 24 ++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 14 ++++++++++++++ 4 files changed, 53 insertions(+)
include/linux/bpf.h +1 -0
@@ -2107,6 +2107,7 @@ extern const struct bpf_func_proto bpf_for_each_map_elem_proto;extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;extern const struct bpf_func_proto bpf_sk_setsockopt_proto;extern const struct bpf_func_proto bpf_sk_getsockopt_proto; +extern const struct bpf_func_proto bpf_kallsyms_lookup_name_proto;const struct bpf_func_proto *tracing_prog_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);
include/uapi/linux/bpf.h +14 -0
@@ -4909,6 +4909,19 @@ union bpf_attr {* Return * The number of bytes written to the buffer, or a negative error * in case of failure. +* +* long bpf_kallsyms_lookup_name(const char *name, int name_sz, int flags, u64 *res) +* Description +* Get the address of a kernel symbol, returned in *res*. *res* is +* set to 0 if the symbol is not found. +* Return +* On success, zero. On error, a negative value. +* +* **-EINVAL** if *flags* is not zero. +* +* **-EINVAL** if string *name* is not the same size as *name_sz*. +* +* **-ENOENT** if symbol is not found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5089,6 +5102,7 @@ union bpf_attr {FN (task_pt_regs), \FN (get_branch_snapshot), \FN (trace_vprintk), \ +FN (kallsyms_lookup_name), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/syscall.c +24 -0
@@ -4753,6 +4753,28 @@ static const struct bpf_func_proto bpf_sys_close_proto = { .arg1_type = ARG_ANYTHING, }; +BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) +{ +if (flags) +return -EINVAL ; + +if (name_sz <=1 || name[name_sz -1 ]) +return -EINVAL ; + + *res =kallsyms_lookup_name (name); +return *res ?0 : -ENOENT ; +} + +const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { + .func = bpf_kallsyms_lookup_name, + .gpl_only = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_MEM , + .arg2_type =ARG_CONST_SIZE , + .arg3_type =ARG_ANYTHING , + .arg4_type =ARG_PTR_TO_LONG , +}; +static const struct bpf_func_proto *syscall_prog_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {
@@ -4763,6 +4785,8 @@ syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_btf_find_by_name_kind_proto;case BPF_FUNC_sys_close:return &bpf_sys_close_proto; +case BPF_FUNC_kallsyms_lookup_name: +return &bpf_kallsyms_lookup_name_proto;default :return tracing_prog_func_proto (func_id, prog); }
tools/include/uapi/linux/bpf.h +14 -0
@@ -4909,6 +4909,19 @@ union bpf_attr {* Return * The number of bytes written to the buffer, or a negative error * in case of failure. +* +* long bpf_kallsyms_lookup_name(const char *name, int name_sz, int flags, u64 *res) +* Description +* Get the address of a kernel symbol, returned in *res*. *res* is +* set to 0 if the symbol is not found. +* Return +* On success, zero. On error, a negative value. +* +* **-EINVAL** if *flags* is not zero. +* +* **-EINVAL** if string *name* is not the same size as *name_sz*. +* +* **-ENOENT** if symbol is not found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5089,6 +5102,7 @@ union bpf_attr {FN (task_pt_regs), \FN (get_branch_snapshot), \FN (trace_vprintk), \ +FN (kallsyms_lookup_name), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v3 1/8] bpf: Add bpf_kallsyms_lookup_name helper
Quoted 3 lines · Kumar Kartikeya Dwivedi
This helper allows us to get the address of a kernel symbol from inside a
BPF_PROG_TYPE_SYSCALLprog (used bygen_loader), so that we can relocate typeless ksym vars.
...
Quoted 5 lines
kernel/bpf/syscall.c +5
-0+BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res) +{ +if (flags) +return -EINVAL ; +
kernel/bpf/syscall.c static const struct bpf_func_proto bpf_sys_close_proto = { · line 4770
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+if (name_sz <=1 || name[name_sz -1 ]) +return -EINVAL ; + + *res =kallsyms_lookup_name (name); +return *res ?0 : -ENOENT ; +} + +const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { + .func = bpf_kallsyms_lookup_name, + .gpl_only = true,
When libbpf is processing of typeless ksyms it parses /proc/kallsyms. There is no gpl-ness in the action of reading this file. Hence above should be '= false' for consistency between light and regular skeleton.
But different check is necessary.
This helper is available to syscall_bpf prog type (which is CAP_BPF) and shouldn't be used to bypass kptr checks.
So bpf_kallsyms_lookup_name() should probably have:
if (!bpf_dump_raw_ok(current_cred())))
return -EPERM;
The rest of the patches look great.
Thank you for working on this!
Kumar Kartikeya Dwivedi
maintainerPATCH v3 2/8 libbpf: Add typeless ksym support to gen_loader
In reply to Kumar Kartikeya DwivediThis uses the bpf_kallsyms_lookup_name helper added in previous patches to relocate typeless ksyms. The return value ENOENT can be ignored, and the value written to 'res' can be directly stored to the insn, as it is overwritten to 0 on lookup failure. For repeating symbols, we can simply copy the previously populated bpf_insn.
Also, we need to take care to not close fds for typeless ksym_desc, so reuse the 'off' member's space to add a marker for typeless ksym and use that to skip them in cleanup_relos.
We add a emit_ksym_relo_log helper that avoids duplicating common logging instructions between typeless and weak ksym (for future commit).
--- tools/lib/bpf/bpf_gen_internal.h | 12 +++- tools/lib/bpf/gen_loader.c | 97 ++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.c | 13 ++--- 3 files changed, 99 insertions(+), 23 deletions(-)
tools/lib/bpf/bpf_gen_internal.h +9 -3
@@ -8,13 +8,19 @@ struct ksym_relo_desc {int kind;int insn_idx;bool is_weak; +bool is_typeless; };struct ksym_desc {const char *name;int ref;int kind; -int off; +union { +/* used for kfunc */ +int off; +/* used for typeless ksym */ +bool typeless; + };int insn; };
@@ -49,7 +55,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen, struct bpf_prog_load_params *load_avoid bpf_gen__map_update_elem (struct bpf_gen *gen,int map_idx,void *value,__u32 value_size);void bpf_gen__map_freeze (struct bpf_gen *gen,int map_idx);void bpf_gen__record_attach_target (struct bpf_gen *gen,const char *name,enum bpf_attach_type type); -void bpf_gen__record_extern (struct bpf_gen *gen,const char *name,bool is_weak,int kind, -int insn_idx); +void bpf_gen__record_extern (struct bpf_gen *gen,const char *name,bool is_weak, +bool is_typeless,int kind,int insn_idx);#endif
tools/lib/bpf/gen_loader.c +85 -12
143 lines not on this page · raw message
tools/lib/bpf/libbpf.c +5 -8
@@ -6355,17 +6355,14 @@ static int bpf_program__record_externs(struct bpf_program *prog)case RELO_EXTERN_VAR :if (ext->type !=EXT_KSYM )continue ; -if (!ext->ksym.type_id) { -pr_warn ("typeless ksym %s is not supported yet\n" , - ext->name); -return -ENOTSUP ; - } -bpf_gen__record_extern (obj->gen_loader, ext->name, ext->is_weak, +bpf_gen__record_extern (obj->gen_loader, ext->name, + ext->is_weak, !ext->ksym.type_id,BTF_KIND_VAR , relo->insn_idx);break ;case RELO_EXTERN_FUNC : -bpf_gen__record_extern (obj->gen_loader, ext->name, ext->is_weak, -BTF_KIND_FUNC , relo->insn_idx); +bpf_gen__record_extern (obj->gen_loader, ext->name, + ext->is_weak, false,BTF_KIND_FUNC , + relo->insn_idx);break ;default :continue ;
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v3 2/8] libbpf: Add typeless ksym support to gen_loader
Quoted 10 lines · Kumar Kartikeya Dwivedi
This uses the
bpf_kallsyms_lookup_namehelper added in previous patches to relocate typeless ksyms. The return value ENOENT can be ignored, and the value written to 'res' can be directly stored to the insn, as it is overwritten to 0 on lookup failure. For repeating symbols, we can simply copy the previously populatedbpf_insn.
Quoted 8 lines
Also, we need to take care to not close fds for typeless
ksym_desc, so reuse the 'off' member's space to add a marker for typeless ksym and use that to skip them incleanup_relos.We add a
emit_ksym_relo_loghelper that avoids duplicating common logging instructions between typeless and weak ksym (for future commit).Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
LGTM!
Kumar Kartikeya Dwivedi
maintainerPATCH v3 3/8 libbpf: Add weak ksym support to gen_loader
In reply to Kumar Kartikeya DwivediThis extends existing ksym relocation code to also support relocating weak ksyms. Care needs to be taken to zero out the src_reg (currently BPF_PSEUOD_BTF_ID, always set for gen_loader by bpf_object__relocate_data) when the BTF ID lookup fails at runtime. This is not a problem for libbpf as it only sets ext->is_set when BTF ID lookup succeeds (and only proceeds in case of failure if ext->is_weak, leading to src_reg remaining as 0 for weak unresolved ksym).
A pattern similar to emit_relo_kfunc_btf is followed of first storing the default values and then jumping over actual stores in case of an error. For src_reg adjustment, we also need to perform it when copying the populated instruction, so depending on if copied insn[0].imm is 0 or not, we decide to jump over the adjustment.
We cannot reach that point unless the ksym was weak and resolved and zeroed out, as the emit_check_err will cause us to jump to cleanup label, so we do not need to recheck whether the ksym is weak before doing the adjustment after copying BTF ID and BTF FD.
This is consistent with how libbpf relocates weak ksym. Logging statements are added to show the relocation result and aid debugging.
--- tools/lib/bpf/gen_loader.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-)
tools/lib/bpf/gen_loader.c +32 -3
@@ -13,6 +13,7 @@#include "hashmap.h" #include "bpf_gen_internal.h" #include "skel_internal.h" +#include <asm/byteorder.h> #define MAX_USED_MAPS 64#define MAX_USED_PROGS 32
@@ -776,12 +777,24 @@ static void emit_relo_ksym_typeless(struct bpf_gen *gen,emit_ksym_relo_log (gen, relo, kdesc->ref); } +static __u32 src_reg_mask (void ) +{ +#if defined(__LITTLE_ENDIAN_BITFIELD) +return 0x0f ;/* src_reg,dst_reg,... */ +#elif defined(__BIG_ENDIAN_BITFIELD) +return 0xf0 ;/* dst_reg,src_reg,... */ +#else +#error "Unsupported bit endianness, cannot proceed" +#endif +} +/* Expects: * BPF_REG_8 - pointer to instruction */ static void emit_relo_ksym_btf (struct bpf_gen *gen,struct ksym_relo_desc *relo,int insn) {struct ksym_desc *kdesc; +__u32 reg_mask; kdesc =get_ksym_desc (gen, relo);if (!kdesc)
@@ -792,19 +805,35 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, kdesc->insn + offsetof(struct bpf_insn, imm));move_blob2blob (gen, insn +sizeof (struct bpf_insn ) + offsetof(struct bpf_insn , imm),4 , kdesc->insn +sizeof (struct bpf_insn ) + offsetof(struct bpf_insn , imm)); - goto log; +emit (gen,BPF_LDX_MEM (BPF_W ,BPF_REG_9 ,BPF_REG_8 , offsetof(struct bpf_insn , imm))); +/* jump over src_reg adjustment if imm is not 0 */ +emit (gen,BPF_JMP_IMM (BPF_JNE ,BPF_REG_9 ,0 ,3 )); + goto clear_src_reg; }/* remember insn offset, so we can copy BTF ID and FD later */ kdesc->insn = insn;emit_bpf_find_by_name_kind (gen, relo); -emit_check_err (gen); +if (!relo->is_weak) +emit_check_err (gen); +/* set default values as 0 */ +emit (gen,BPF_ST_MEM (BPF_W ,BPF_REG_8 , offsetof(struct bpf_insn , imm),0 )); +emit (gen,BPF_ST_MEM (BPF_W ,BPF_REG_8 ,sizeof (struct bpf_insn ) + offsetof(struct bpf_insn , imm),0 )); +/* skip success case stores if ret < 0 */ +emit (gen,BPF_JMP_IMM (BPF_JSLT ,BPF_REG_7 ,0 ,4 ));/* store btf_id into insn[insn_idx].imm */ emit (gen,BPF_STX_MEM (BPF_W ,BPF_REG_8 ,BPF_REG_7 , offsetof(struct bpf_insn , imm)));/* store btf_obj_fd into insn[insn_idx + 1].imm */ emit (gen,BPF_ALU64_IMM (BPF_RSH ,BPF_REG_7 ,32 ));emit (gen,BPF_STX_MEM (BPF_W ,BPF_REG_8 ,BPF_REG_7 ,sizeof (struct bpf_insn ) + offsetof(struct bpf_insn , imm))); -log: +emit (gen,BPF_JMP_IMM (BPF_JSGE ,BPF_REG_7 ,0 ,3 )); +clear_src_reg: +/* clear bpf_object__relocate_data's src_reg assignment, otherwise we get a verifier failure */ + reg_mask =src_reg_mask (); + emit(gen, BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_8, offsetofend(struct bpf_insn, code))); +emit (gen,BPF_ALU32_IMM (BPF_AND ,BPF_REG_9 , reg_mask)); + emit(gen, BPF_STX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, offsetofend(struct bpf_insn, code))); +emit_ksym_relo_log (gen, relo, kdesc->ref); }
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v3 3/8] libbpf: Add weak ksym support to gen_loader
Quoted 23 lines · Kumar Kartikeya Dwivedi
This extends existing ksym relocation code to also support relocating weak ksyms. Care needs to be taken to zero out the
src_reg(currentlyBPF_PSEUOD_BTF_ID, always set forgen_loaderbybpf_object__relocate_data) when the BTF ID lookup fails at runtime. This is not a problem for libbpf as it only setsext->is_setwhen BTF ID lookup succeeds (and only proceeds in case of failure ifext->is_weak, leading tosrc_regremaining as 0 for weak unresolved ksym).A pattern similar to
emit_relo_kfunc_btfis followed of first storing the default values and then jumping over actual stores in case of an error. Forsrc_regadjustment, we also need to perform it when copying the populated instruction, so depending on if copied insn[0].imm is 0 or not, we decide to jump over the adjustment.We cannot reach that point unless the ksym was weak and resolved and zeroed out, as the
emit_check_errwill cause us to jump to cleanup label, so we do not need to recheck whether the ksym is weak before doing the adjustment after copying BTF ID and BTF FD.
Quoted 4 lines
This is consistent with how libbpf relocates weak ksym. Logging statements are added to show the relocation result and aid debugging.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
Kumar Kartikeya Dwivedi
maintainerPATCH v3 4/8 libbpf: Ensure that BPF syscall fds are never 0, 1, or 2
In reply to Kumar Kartikeya DwivediAdd a simple wrapper for passing an fd and getting a new one >= 3 if it is one of 0, 1, or 2. There are two primary reasons to make this change:
First, libbpf relies on the assumption a certain BPF fd is never 0 (e.g. most recently noticed in [0]). Second, Alexei pointed out in [1] that some environments reset stdin, stdout, and stderr if they notice an invalid fd at these numbers. To protect against both these cases, switch all internal BPF syscall wrappers in libbpf to always return an fd >= 3. We only need to modify the syscall wrappers and not other code that assumes a valid fd by doing >= 0, to avoid pointless churn, and because it is still a valid assumption. The cost paid is two additional syscalls if fd is in range [0, 2].
This is only done for fds that are held by objects created using libbpf, or returned from libbpf functions, not for intermediate fds closed soon after their use is over. This implies that one of the assumptions is that the environment resetting fds in the starting range [0, 2] only do so before libbpf function call or after, but never in parallel, since that would close fds while we dup them.
[0]: e31eec77e4ab ("bpf: selftests: Fix fd cleanup in get_branch_snapshot")
[1]: https://lore.kernel.org/bpf/CAADnVQKVKY8o_3aU8Gzke443+uHa-eGoM0h7W4srChMXU1S4Bg@mail.gmail.com
--- tools/lib/bpf/bpf.c | 28 ++++++++++++------------- tools/lib/bpf/libbpf.c | 36 ++++++++++++++++----------------- tools/lib/bpf/libbpf_internal.h | 23 +++++++++++++++++++++ tools/lib/bpf/linker.c | 2 +- tools/lib/bpf/ringbuf.c | 2 +- tools/lib/bpf/skel_internal.h | 35 +++++++++++++++++++++++++++++++- 6 files changed, 91 insertions(+), 35 deletions(-)
tools/lib/bpf/bpf.c +14 -14
@@ -74,7 +74,7 @@ static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size) fd =sys_bpf (BPF_PROG_LOAD , attr, size); }while (fd <0 && errno ==EAGAIN && retries-- >0 ); -return fd; +return ensure_good_fd (fd); }int bpf_create_map_xattr (const struct bpf_create_map_attr *create_attr)
@@ -104,7 +104,7 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) attr.inner_map_fd = create_attr->inner_map_fd; fd =sys_bpf (BPF_MAP_CREATE , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
@@ -182,7 +182,7 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name, } fd =sys_bpf (BPF_MAP_CREATE , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
@@ -330,7 +330,7 @@ int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr)/* free() doesn't affect errno, so we don't need to restore it */ free (finfo);free (linfo); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
@@ -610,7 +610,7 @@ int bpf_obj_get(const char *pathname) attr.pathname =ptr_to_u64 ((void *)pathname); fd =sys_bpf (BPF_OBJ_GET , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
@@ -721,7 +721,7 @@ int bpf_link_create(int prog_fd, int target_fd, } proceed: fd =sys_bpf (BPF_LINK_CREATE , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); }int bpf_link_detach (int link_fd)
@@ -764,7 +764,7 @@ int bpf_iter_create(int link_fd) attr.iter_create.link_fd = link_fd; fd =sys_bpf (BPF_ITER_CREATE , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
@@ -922,7 +922,7 @@ int bpf_prog_get_fd_by_id(__u32 id) attr.prog_id = id; fd =sys_bpf (BPF_PROG_GET_FD_BY_ID , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); }int bpf_map_get_fd_by_id (__u32 id)
@@ -934,7 +934,7 @@ int bpf_map_get_fd_by_id(__u32 id) attr.map_id = id; fd =sys_bpf (BPF_MAP_GET_FD_BY_ID , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); }int bpf_btf_get_fd_by_id (__u32 id)
@@ -946,7 +946,7 @@ int bpf_btf_get_fd_by_id(__u32 id) attr.btf_id = id; fd =sys_bpf (BPF_BTF_GET_FD_BY_ID , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); }int bpf_link_get_fd_by_id (__u32 id)
@@ -958,7 +958,7 @@ int bpf_link_get_fd_by_id(__u32 id) attr.link_id = id; fd =sys_bpf (BPF_LINK_GET_FD_BY_ID , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); }int bpf_obj_get_info_by_fd (int bpf_fd,void *info,__u32 *info_len)
@@ -989,7 +989,7 @@ int bpf_raw_tracepoint_open(const char *name, int prog_fd) attr.raw_tracepoint.prog_fd = prog_fd; fd =sys_bpf (BPF_RAW_TRACEPOINT_OPEN , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
@@ -1015,7 +1015,7 @@ int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_s goto retry; } -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
@@ -1051,7 +1051,7 @@ int bpf_enable_stats(enum bpf_stats_type type) attr.enable_stats.type = type; fd =sys_bpf (BPF_ENABLE_STATS , &attr,sizeof (attr)); -return libbpf_err_errno (fd); +return libbpf_err_errno (ensure_good_fd (fd)); } int bpf_prog_bind_map(int prog_fd, int map_fd,
tools/lib/bpf/libbpf.c +18 -18
@@ -1223,7 +1223,7 @@ static int bpf_object__elf_init(struct bpf_object *obj) obj->efile.elf =elf_memory ((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); }else { - obj->efile.fd =open (obj->path,O_RDONLY ); + obj->efile.fd =ensure_good_fd (open (obj->path,O_RDONLY ));if (obj->efile.fd <0 ) {char errmsg[STRERR_BUFSIZE ], *cp;
@@ -9312,10 +9312,10 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, attr.config2 = offset;/* kprobe_addr or probe_offset */ /* pid filter is meaningful only for uprobes */ - pfd =syscall (__NR_perf_event_open, &attr, - pid <0 ?-1 : pid/* pid */ , - pid ==-1 ?0 :-1 /* cpu */ , --1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ); + pfd =ensure_good_fd (syscall (__NR_perf_event_open, &attr, + pid <0 ?-1 : pid/* pid */ , + pid ==-1 ?0 :-1 /* cpu */ , +-1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ));if (pfd <0 ) { err = -errno; pr_warn("%s perf_event_open() failed: %s\n",
@@ -9406,10 +9406,10 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, attr.config = type; attr.type =PERF_TYPE_TRACEPOINT ; - pfd =syscall (__NR_perf_event_open, &attr, - pid <0 ?-1 : pid,/* pid */ - pid ==-1 ?0 :-1 ,/* cpu */ --1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ); + pfd =ensure_good_fd (syscall (__NR_perf_event_open, &attr, + pid <0 ?-1 : pid,/* pid */ + pid ==-1 ?0 :-1 ,/* cpu */ +-1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ));if (pfd <0 ) { err = -errno; pr_warn("legacy kprobe perf_event_open() failed: %s\n",
@@ -9601,10 +9601,10 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, attr.config = type; attr.type =PERF_TYPE_TRACEPOINT ; - pfd =syscall (__NR_perf_event_open, &attr, - pid <0 ?-1 : pid,/* pid */ - pid ==-1 ?0 :-1 ,/* cpu */ --1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ); + pfd =ensure_good_fd (syscall (__NR_perf_event_open, &attr, + pid <0 ?-1 : pid,/* pid */ + pid ==-1 ?0 :-1 ,/* cpu */ +-1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ));if (pfd <0 ) { err = -errno;pr_warn ("legacy uprobe perf_event_open() failed: %d\n" , err);
@@ -9733,8 +9733,8 @@ static int perf_event_open_tracepoint(const char *tp_category, attr.size =sizeof (attr); attr.config = tp_id; - pfd =syscall (__NR_perf_event_open, &attr,-1 /* pid */ ,0 /* cpu */ , --1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ); + pfd =ensure_good_fd (syscall (__NR_perf_event_open, &attr,-1 /* pid */ ,0 /* cpu */ , +-1 /* group_fd */ ,PERF_FLAG_FD_CLOEXEC ));if (pfd <0 ) { err = -errno; pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
@@ -10253,8 +10253,8 @@ perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr, cpu_buf->cpu = cpu; cpu_buf->map_key = map_key; - cpu_buf->fd =syscall (__NR_perf_event_open, attr,-1 /* pid */ , cpu, --1 ,PERF_FLAG_FD_CLOEXEC ); + cpu_buf->fd =ensure_good_fd (syscall (__NR_perf_event_open, attr,-1 /* pid */ , cpu, +-1 ,PERF_FLAG_FD_CLOEXEC ));if (cpu_buf->fd <0 ) { err = -errno; pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
@@ -10380,7 +10380,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, pb->mmap_size = pb->page_size * page_cnt; pb->map_fd = map_fd; - pb->epoll_fd =epoll_create1 (EPOLL_CLOEXEC ); + pb->epoll_fd =ensure_good_fd (epoll_create1 (EPOLL_CLOEXEC ));if (pb->epoll_fd <0 ) { err = -errno; pr_warn("failed to create epoll instance: %s\n",
tools/lib/bpf/libbpf_internal.h +23 -0
@@ -13,6 +13,8 @@#include <limits.h> #include <errno.h> #include <linux/err.h> +#include <fcntl.h> +#include <unistd.h> #include "libbpf_legacy.h" #include "relo_core.h"
@@ -472,4 +474,25 @@ static inline bool is_ldimm64_insn(struct bpf_insn *insn) return insn->code == (BPF_LD | BPF_IMM | BPF_DW); } +/* if fd is stdin, stdout, or stderr, dup to a fd greater than 2 +* Takes ownership of the fd passed in, and closes it if calling +* fcntl(fd, F_DUPFD_CLOEXEC, 3). +*/ +static inline int ensure_good_fd (int fd) +{ +int old_fd = fd, save_errno; + +if (unlikely (fd >=0 && fd <3 )) { + fd =fcntl (fd,F_DUPFD_CLOEXEC ,3 ); +if (fd <0 ) { + save_errno = errno; +pr_warn ("failed to dup FD %d to FD > 2: %d\n" , old_fd, -errno); + } +close (old_fd); +if (fd <0 ) + errno = save_errno; + } +return fd; +} +#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
tools/lib/bpf/linker.c +1 -1
@@ -302,7 +302,7 @@ static int init_output_elf(struct bpf_linker *linker, const char *file)if (!linker->filename)return -ENOMEM ; - linker->fd =open (file,O_WRONLY |O_CREAT |O_TRUNC ,0644 ); + linker->fd =ensure_good_fd (open (file,O_WRONLY |O_CREAT |O_TRUNC ,0644 ));if (linker->fd <0 ) { err = -errno;pr_warn ("failed to create '%s': %d\n" , file, err);
tools/lib/bpf/ringbuf.c +1 -1
@@ -173,7 +173,7 @@ ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx, rb->page_size =getpagesize (); - rb->epoll_fd =epoll_create1 (EPOLL_CLOEXEC ); + rb->epoll_fd =ensure_good_fd (epoll_create1 (EPOLL_CLOEXEC ));if (rb->epoll_fd <0 ) { err = -errno;pr_warn ("ringbuf: failed to create epoll instance: %d\n" , err);
tools/lib/bpf/skel_internal.h +34 -1
@@ -6,6 +6,7 @@#include <unistd.h> #include <sys/syscall.h> #include <sys/mman.h> +#include <fcntl.h> /* This file is a base header for auto-generated *.lskel.h files. * Its contents will change and may become part of auto-generation in the future.
@@ -60,11 +61,39 @@ static inline int skel_closenz(int fd)return -EINVAL ; } +static inline int skel_reserve_bad_fds (struct bpf_load_and_run_opts *opts,int *fds) +{ +int fd, err, i; + +for (i =0 ; i <3 ; i++) { + fd =open ("/dev/null" ,O_RDONLY |O_CLOEXEC ); +if (fd <0 ) { + opts->errstr ="failed to reserve fd 0, 1, and 2" ; + err = -errno; +return err; + } +if (__builtin_expect (fd >=3 ,1 )) { +close (fd); +break ; + } + fds[i] = fd; + } +return 0 ; +} +static inline int bpf_load_and_run (struct bpf_load_and_run_opts *opts) { -int map_fd =-1 , prog_fd =-1 , key =0 , err; +int map_fd =-1 , prog_fd =-1 , key =0 , err, i; +int res_fds[3 ] = {-1 ,-1 ,-1 };union bpf_attr attr; +/* ensures that we don't open fd 0, 1, or 2 from here on out */ + err =skel_reserve_bad_fds (opts, res_fds); +if (err <0 ) { + errno = -err; + goto out; + } + map_fd =bpf_create_map_name (BPF_MAP_TYPE_ARRAY ,"__loader.map" ,4 , opts->data_sz,1 ,0 );if (map_fd <0 ) {
@@ -115,6 +144,10 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) } err =0 ; out: +for (i =0 ; i <3 ; i++) { +if (res_fds[i] >=0 ) +close (res_fds[i]); + }if (map_fd >=0 )close (map_fd);if (prog_fd >=0 )
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 5/8 libbpf: Use O_CLOEXEC uniformly when opening fds
In reply to Kumar Kartikeya DwivediThere are some instances where we don't use O_CLOEXEC when opening an fd, fix these up. Otherwise, it is possible that a parallel fork causes these fds to leak into a child process on execve.
--- tools/lib/bpf/btf.c | 2 +- tools/lib/bpf/libbpf.c | 6 +++--- tools/lib/bpf/libbpf_probes.c | 2 +- tools/lib/bpf/linker.c | 4 ++-- tools/lib/bpf/xsk.c | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-)
tools/lib/bpf/btf.c +1 -1
@@ -886,7 +886,7 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,return ERR_PTR (-LIBBPF_ERRNO__LIBELF ); } - fd =open (path,O_RDONLY ); + fd =open (path,O_RDONLY |O_CLOEXEC );if (fd <0 ) { err = -errno;pr_warn ("failed to open %s: %s\n" , path,strerror (errno));
tools/lib/bpf/libbpf.c +3 -3
@@ -1223,7 +1223,7 @@ static int bpf_object__elf_init(struct bpf_object *obj) obj->efile.elf =elf_memory ((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); }else { - obj->efile.fd =ensure_good_fd (open (obj->path,O_RDONLY )); + obj->efile.fd =ensure_good_fd (open (obj->path,O_RDONLY |O_CLOEXEC ));if (obj->efile.fd <0 ) {char errmsg[STRERR_BUFSIZE ], *cp;
@@ -9331,7 +9331,7 @@ static int append_to_file(const char *file, const char *fmt, ...)int fd, n, err =0 ;va_list ap; - fd =open (file,O_WRONLY |O_APPEND ,0 ); + fd =open (file,O_WRONLY |O_APPEND |O_CLOEXEC ,0 );if (fd <0 )return -errno;
@@ -10976,7 +10976,7 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)int fd, err =0 , len;char buf[128 ]; - fd =open (fcpu,O_RDONLY ); + fd =open (fcpu,O_RDONLY |O_CLOEXEC );if (fd <0 ) { err = -errno;pr_warn ("Failed to open cpu mask file %s: %d\n" , fcpu, err);
tools/lib/bpf/libbpf_probes.c +1 -1
@@ -33,7 +33,7 @@ static int get_vendor_id(int ifindex)snprintf (path,sizeof (path),"/sys/class/net/%s/device/vendor" , ifname); - fd =open (path,O_RDONLY ); + fd =open (path,O_RDONLY |O_CLOEXEC );if (fd <0 )return -1 ;
tools/lib/bpf/linker.c +2 -2
@@ -302,7 +302,7 @@ static int init_output_elf(struct bpf_linker *linker, const char *file)if (!linker->filename)return -ENOMEM ; - linker->fd =ensure_good_fd (open (file,O_WRONLY |O_CREAT |O_TRUNC ,0644 )); + linker->fd =ensure_good_fd (open (file,O_WRONLY |O_CREAT |O_TRUNC |O_CLOEXEC ,0644 ));if (linker->fd <0 ) { err = -errno;pr_warn ("failed to create '%s': %d\n" , file, err);
@@ -557,7 +557,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, obj->filename = filename; - obj->fd =open (filename,O_RDONLY ); + obj->fd =open (filename,O_RDONLY |O_CLOEXEC );if (obj->fd <0 ) { err = -errno;pr_warn ("failed to open file '%s': %d\n" , filename, err);
tools/lib/bpf/xsk.c +3 -3
@@ -300,7 +300,7 @@ int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area,if (!umem)return -ENOMEM ; - umem->fd =socket (AF_XDP ,SOCK_RAW ,0 ); + umem->fd =socket (AF_XDP ,SOCK_RAW |SOCK_CLOEXEC ,0 );if (umem->fd <0 ) { err = -errno; goto out_umem_alloc;
@@ -549,7 +549,7 @@ static int xsk_get_max_queues(struct xsk_socket *xsk)struct ifreq ifr = {};int fd, err, ret; - fd =socket (AF_LOCAL ,SOCK_DGRAM ,0 ); + fd =socket (AF_LOCAL ,SOCK_DGRAM |SOCK_CLOEXEC ,0 );if (fd <0 )return -errno;
@@ -1046,7 +1046,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, }if (umem->refcount++ >0 ) { - xsk->fd =socket (AF_XDP ,SOCK_RAW ,0 ); + xsk->fd =socket (AF_XDP ,SOCK_RAW |SOCK_CLOEXEC ,0 );if (xsk->fd <0 ) { err = -errno; goto out_xsk_alloc;
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v3 5/8] libbpf: Use O_CLOEXEC uniformly when opening fds
Quoted 9 lines · Kumar Kartikeya Dwivedi
There are some instances where we don't use
O_CLOEXECwhen opening an fd, fix these up. Otherwise, it is possible that a parallel fork causes these fds to leak into a child process on execve.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
Kumar Kartikeya Dwivedi
maintainerPATCH v3 6/8 selftests/bpf: Add weak/typeless ksym test for light skeleton
In reply to Kumar Kartikeya DwivediAlso, avoid using CO-RE features, as lskel doesn't support CO-RE, yet. Include both light and libbpf skeleton in same file to test both of them together.
In c48e51c8b07a ("bpf: selftests: Add selftests for module kfunc support"), I added support for generating both lskel and libbpf skel for a BPF object, however the name parameter for bpftool caused collisions when included in same file together. This meant that every test needed a separate file for a libbpf/light skeleton separation instead of subtests.
Change that by appending a "_light" suffix to the name for files listed in LSKELS_EXTRA, such that both light and libbpf skeleton can be used in the same file for subtests, leading to better code sharing.
While at it, improve the build output by saying GEN-LSKEL instead of GEN-SKEL for light skeleton generation recipe.
--- tools/testing/selftests/bpf/Makefile | 7 ++-- .../selftests/bpf/prog_tests/ksyms_btf.c | 35 +++++++++++++++- .../selftests/bpf/prog_tests/ksyms_module.c | 40 +++++++++++++++++-- .../bpf/prog_tests/ksyms_module_libbpf.c | 28 ------------- .../selftests/bpf/progs/test_ksyms_weak.c | 3 +- 5 files changed, 74 insertions(+), 39 deletions(-) delete mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c
tools/testing/selftests/bpf/Makefile +4 -3
@@ -325,7 +325,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c # Generate both light skeleton and libbpf skeleton for these -LSKELS_EXTRA := test_ksyms_module.c +LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c SKEL_BLACKLIST += $$(LSKELS) test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
@@ -399,12 +399,13 @@ $(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@ $(TRUNNER_BPF_LSKELS): %.lskel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) - $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) + $$(call msg,GEN-LSKEL,$(TRUNNER_BINARY),$$@) $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$< $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o) $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) - $(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@ + $$(eval LSKEL_NAME := $$(notdir $$(<:.o=$$(if $$(filter $$(notdir $$(<:.o=.c)),$(LSKELS_EXTRA)),_light,)))) + $(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.linked3.o) name $$(LSKEL_NAME) > $$@ $(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT) $$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.o))
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c +33 -2
@@ -7,6 +7,7 @@#include "test_ksyms_btf.skel.h" #include "test_ksyms_btf_null_check.skel.h" #include "test_ksyms_weak.skel.h" +#include "test_ksyms_weak.lskel.h" static int duration;
@@ -89,11 +90,11 @@ static void test_weak_syms(void)int err; skel =test_ksyms_weak__open_and_load (); -if (CHECK (!skel,"test_ksyms_weak__open_and_load" ,"failed\n" )) +if (!ASSERT_OK_PTR (skel,"test_ksyms_weak__open_and_load" ))return ; err =test_ksyms_weak__attach (skel); -if (CHECK (err,"test_ksyms_weak__attach" ,"skeleton attach failed: %d\n" , err)) +if (!ASSERT_OK (err,"test_ksyms_weak__attach" )) goto cleanup;/* trigger tracepoint */
@@ -109,6 +110,33 @@ static void test_weak_syms(void)test_ksyms_weak__destroy (skel); } +static void test_weak_syms_light (void ) +{ +struct test_ksyms_weak_light *skel; +struct test_ksyms_weak_light__data *data; +int err; + + skel =test_ksyms_weak_light__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_ksyms_weak_light__open_and_load" )) +return ; + + err =test_ksyms_weak_light__attach (skel); +if (!ASSERT_OK (err,"test_ksyms_weak_light__attach" )) + goto cleanup; + +/* trigger tracepoint */ +usleep (1 ); + + data = skel->data; +ASSERT_EQ (data->out__existing_typed,0 ,"existing typed ksym" ); +ASSERT_NEQ (data->out__existing_typeless,-1 ,"existing typeless ksym" ); +ASSERT_EQ (data->out__non_existent_typeless,0 ,"nonexistent typeless ksym" ); +ASSERT_EQ (data->out__non_existent_typed,0 ,"nonexistent typed ksym" ); + +cleanup: +test_ksyms_weak_light__destroy (skel); +} +void test_ksyms_btf (void ) {int percpu_datasec;
@@ -136,4 +164,7 @@ void test_ksyms_btf(void)if (test__start_subtest ("weak_ksyms" ))test_weak_syms (); + +if (test__start_subtest ("weak_ksyms_light" )) +test_weak_syms_light (); }
tools/testing/selftests/bpf/prog_tests/ksyms_module.c +36 -4
@@ -4,10 +4,11 @@#include <test_progs.h> #include <network_helpers.h> #include "test_ksyms_module.lskel.h" +#include "test_ksyms_module.skel.h" -void test_ksyms_module (void ) +void test_ksyms_module_light (void ) { -struct test_ksyms_module *skel; +struct test_ksyms_module_light *skel;int retval;int err;
@@ -16,8 +17,8 @@ void test_ksyms_module(void)return ; } - skel =test_ksyms_module__open_and_load (); -if (!ASSERT_OK_PTR (skel,"test_ksyms_module__open_and_load" )) + skel =test_ksyms_module_light__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_ksyms_module_light__open_and_load" ))return ; err =bpf_prog_test_run (skel->progs.load.prog_fd,1 , &pkt_v4,sizeof (pkt_v4),NULL ,NULL , (__u32 *)&retval,NULL );
@@ -25,6 +26,37 @@ void test_ksyms_module(void) goto cleanup;ASSERT_EQ (retval,0 ,"retval" );ASSERT_EQ (skel->bss->out_bpf_testmod_ksym,42 ,"bpf_testmod_ksym" ); +cleanup: +test_ksyms_module_light__destroy (skel); +} + +void test_ksyms_module_libbpf (void ) +{ +struct test_ksyms_module *skel; +int retval, err; + +if (!env.has_testmod) { +test__skip (); +return ; + } + + skel =test_ksyms_module__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_ksyms_module__open" )) +return ; + err =bpf_prog_test_run (bpf_program__fd (skel->progs.load),1 , &pkt_v4, +sizeof (pkt_v4),NULL ,NULL , (__u32 *)&retval,NULL ); +if (!ASSERT_OK (err,"bpf_prog_test_run" )) + goto cleanup; +ASSERT_EQ (retval,0 ,"retval" ); +ASSERT_EQ (skel->bss->out_bpf_testmod_ksym,42 ,"bpf_testmod_ksym" ); cleanup:test_ksyms_module__destroy (skel); } + +void test_ksyms_module (void ) +{ +if (test__start_subtest ("light" )) +test_ksyms_module_light (); +if (test__start_subtest ("libbpf" )) +test_ksyms_module_libbpf (); +}
tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c deleted +0 -28
@@ -1,28 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -#include <test_progs.h> -#include <network_helpers.h> -#include "test_ksyms_module.skel.h" - -void test_ksyms_module_libbpf (void ) -{ -struct test_ksyms_module *skel; -int retval, err; - -if (!env.has_testmod) { -test__skip (); -return ; - } - - skel =test_ksyms_module__open_and_load (); -if (!ASSERT_OK_PTR (skel,"test_ksyms_module__open" )) -return ; - err =bpf_prog_test_run (bpf_program__fd (skel->progs.load),1 , &pkt_v4, -sizeof (pkt_v4),NULL ,NULL , (__u32 *)&retval,NULL ); -if (!ASSERT_OK (err,"bpf_prog_test_run" )) - goto cleanup; -ASSERT_EQ (retval,0 ,"retval" ); -ASSERT_EQ (skel->bss->out_bpf_testmod_ksym,42 ,"bpf_testmod_ksym" ); -cleanup: -test_ksyms_module__destroy (skel); -}
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +1 -2
@@ -21,7 +21,6 @@ __u64 out__non_existent_typed = -1; extern const struct rq runqueues __ksym __weak; /* typed */ extern const void bpf_prog_active __ksym __weak; /* typeless */ -/* non-existent weak symbols. */ /* typeless symbols, default to zero. */
@@ -38,7 +37,7 @@ int pass_handler(const void *ctx)/* tests existing symbols. */ rq = (struct rq *)bpf_per_cpu_ptr (&runqueues,0 );if (rq) - out__existing_typed = rq->cpu; + out__existing_typed =0 ; out__existing_typeless = (__u64)&bpf_prog_active;/* tests non-existent symbols. */
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v3 6/8] selftests/bpf: Add weak/typeless ksym test for light skeleton
Quoted 15 lines · Kumar Kartikeya Dwivedi
Also, avoid using CO-RE features, as lskel doesn't support CO-RE, yet. Include both light and libbpf skeleton in same file to test both of them together.
In
c48e51c8b07a("bpf: selftests: Add selftests for module kfunc support"), I added support for generating both lskel and libbpf skel for a BPF object, however the name parameter for bpftool caused collisions when included in same file together. This meant that every test needed a separate file for a libbpf/light skeleton separation instead of subtests.
Quoted 8 lines
Change that by appending a "
_light" suffix to the name for files listed inLSKELS_EXTRA, such that both light and libbpf skeleton can be used in the same file for subtests, leading to better code sharing.While at it, improve the build output by saying GEN-LSKEL instead of GEN-SKEL for light skeleton generation recipe.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
Kumar Kartikeya Dwivedi
maintainerPATCH v3 7/8 selftests/bpf: Fix fd cleanup in sk_lookup test
In reply to Kumar Kartikeya DwivediSimilar to the fix in commit:e31eec77e4ab ("bpf: selftests: Fix fd cleanup in get_branch_snapshot")
We use designated initializer to set fds to -1 without breaking on future changes to MAX_SERVER constant denoting the array size.
The particular close(0) occurs on non-reuseport tests, so it can be seen with -n 115/{2,3} but not 115/4. This can cause problems with future tests if they depend on BTF fd never being acquired as fd 0, breaking internal libbpf assumptions.
--- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +8 -6
@@ -598,7 +598,7 @@ static void query_lookup_prog(struct test_sk_lookup *skel)static void run_lookup_prog (const struct test *t) { -int server_fds[MAX_SERVERS ] = {-1 }; +int server_fds[] = { [0 ...MAX_SERVERS -1 ] =-1 };int client_fd, reuse_conn_fd =-1 ;struct bpf_link *lookup_link;int i, err;
@@ -663,8 +663,9 @@ static void run_lookup_prog(const struct test *t)if (reuse_conn_fd !=-1 )close (reuse_conn_fd);for (i =0 ; i <ARRAY_SIZE (server_fds); i++) { -if (server_fds[i] !=-1 ) -close (server_fds[i]); +if (server_fds[i] ==-1 ) +break ; +close (server_fds[i]); }bpf_link__destroy (lookup_link); }
@@ -1053,7 +1054,7 @@ static void run_sk_assign(struct test_sk_lookup *skel,struct bpf_program *lookup_prog, const char *remote_ip, const char *local_ip) { - int server_fds[MAX_SERVERS] = { -1 }; + int server_fds[] = { [0 ... MAX_SERVERS - 1] = -1 };struct bpf_sk_lookup ctx;__u64 server_cookie;int i, err;
@@ -1097,8 +1098,9 @@ static void run_sk_assign(struct test_sk_lookup *skel, close_servers:for (i =0 ; i <ARRAY_SIZE (server_fds); i++) { -if (server_fds[i] !=-1 ) -close (server_fds[i]); +if (server_fds[i] ==-1 ) +break ; +close (server_fds[i]); } }
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v3 8/8 selftests/bpf: Fix memory leak in test_ima
In reply to Kumar Kartikeya DwivediThe allocated ring buffer is never freed, do so in the cleanup path.
--- tools/testing/selftests/bpf/prog_tests/test_ima.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/test_ima.c +2 -1
@@ -43,7 +43,7 @@ static int process_sample(void *ctx, void *data, size_t len)void test_test_ima (void ) {char measured_dir_template[] ="/tmp/ima_measuredXXXXXX" ; -struct ring_buffer *ringbuf; +struct ring_buffer *ringbuf =NULL ;const char *measured_dir;char cmd[256 ];
@@ -85,5 +85,6 @@ void test_test_ima(void) err =system (cmd);CHECK (err,"failed to run command" ,"%s, errno = %d\n" , cmd, errno); close_prog: +ring_buffer__free (ringbuf);ima__destroy (skel); }
Signature · 1 line
2.33.0