Skip to contentSkip to conversation

Typeless/weak ksym for gen_loader + misc fixups

PATCH Superseded Kumar Kartikeya Dwivedi 6 participants 27 messages Oct 6, 2021 – Oct 7, 2021

Subsystems (7)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Typeless/weak ksym for gen_loader + misc fixups

Patches (1,2,4) add typeless and weak ksym support to gen_loader. It is follow up for the recent kfunc from modules series.

The later patches (5,6) are misc fixes for selftests, and patch 3 for libbpf where we try to be careful to not end up with mod_btf->fd set as 0 (as that leads to a confusing error message about btf_id not being found on load and it is not clear what went wrong, instead we can just dup fd 0).

Kumar Kartikeya Dwivedi (6):
bpf: Add bpf_kallsyms_lookup_name helper
libbpf: Add typeless and weak ksym support to gen_loader
libbpf: Ensure that module BTF fd is never 0
bpf: selftests: Move test_ksyms_weak test to lskel, add libbpf test
bpf: selftests: Fix fd cleanup in sk_lookup test
bpf: selftests: 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_gen_internal.h              |  12 +-
 tools/lib/bpf/gen_loader.c                    | 123 ++++++++++++++++--
 tools/lib/bpf/libbpf.c                        |  27 ++--
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/ksyms_btf.c      |   6 +-
 .../bpf/prog_tests/ksyms_weak_libbpf.c        |  31 +++++
 .../selftests/bpf/prog_tests/sk_lookup.c      |  20 ++-
 .../selftests/bpf/prog_tests/test_ima.c       |   3 +-
 .../selftests/bpf/progs/test_ksyms_weak.c     |   3 +-
 13 files changed, 247 insertions(+), 33 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c

--
2.33.0

author maintainer
#1 lore raw

PATCH 1/6 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 weak and 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
index d604c8251d88..17206aae329d 100644
@@ -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
index 6fc59d61937a..bbd0a3f4e5f6 100644
@@ -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
index 4e50c0bfdb7d..073ca9ebe58b 100644
@@ -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
index 6fc59d61937a..bbd0a3f4e5f6 100644
@@ -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
reviewer Acked-by on v1 1/6
#2 lore raw

Re: [PATCH bpf-next v1 1/6] bpf: Add bpf_kallsyms_lookup_name helper

Quoted 6 lines · Kumar Kartikeya Dwivedi

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 weak and typeless ksym vars.

LGTM.

Quoted 128 lines
---
 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
index d604c8251d88..17206aae329d 100644
@@ -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
index 6fc59d61937a..bbd0a3f4e5f6 100644
@@ -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
index 4e50c0bfdb7d..073ca9ebe58b 100644
@@ -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 -1
index 6fc59d61937a..bbd0a3f4e5f6 100644
@@ -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
--

2.33.0

author maintainer
#3 lore raw

PATCH 2/6 libbpf: Add typeless and weak ksym support to gen_loader

In reply to Kumar Kartikeya Dwivedi

This patch adds typeless and weak ksym support to BTF_KIND_VAR relocation code in gen_loader. For typeless ksym, we use the newly added bpf_kallsyms_lookup_name helper.

For weak ksym, we simply skip error check, and fix up the srg_reg for the insn, as keeping it as BPF_PSEUDO_BTF_ID for weak ksym with its insn[0].imm and insn[1].imm set as 0 will cause a failure. This is consistent with how libbpf relocates these two cases of BTF_KIND_VAR.

We also modify cleanup_relos to check for typeless ksyms in fd closing loop, since those have no fd associated with the ksym. For this we can reuse the unused 'off' member of ksym_desc.

---
 tools/lib/bpf/bpf_gen_internal.h |  12 ++-
 tools/lib/bpf/gen_loader.c       | 123 ++++++++++++++++++++++++++++---
 tools/lib/bpf/libbpf.c           |  13 ++--
 3 files changed, 128 insertions(+), 20 deletions(-)
tools/lib/bpf/bpf_gen_internal.h +9 -3
index 70eccbffefb1..9f328d044636 100644
@@ -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_a
 void 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 +114 -9

190 lines not on this page · raw message

tools/lib/bpf/libbpf.c +5 -8
index f32fa51b1e63..d286dec73b5f 100644
@@ -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, 0, BTF_KIND_FUNC,
+					       relo->insn_idx);
 			break;
 		default:
 			continue;
Signature · 1 line
2.33.0
reviewer
#4 lore raw

Re: [PATCH bpf-next v1 2/6] libbpf: Add typeless and weak ksym support to gen_loader

Quoted 10 lines · Kumar Kartikeya Dwivedi

This patch adds typeless and weak ksym support to BTF_KIND_VAR relocation code in gen_loader. For typeless ksym, we use the newly added bpf_kallsyms_lookup_name helper.

For weak ksym, we simply skip error check, and fix up the srg_reg for the insn, as keeping it as BPF_PSEUDO_BTF_ID for weak ksym with its insn[0].imm and insn[1].imm set as 0 will cause a failure. This is consistent with how libbpf relocates these two cases of BTF_KIND_VAR.

Quoted 5 lines

We also modify cleanup_relos to check for typeless ksyms in fd closing loop, since those have no fd associated with the ksym. For this we can reuse the unused 'off' member of ksym_desc.

[...]

Everything above (trimmed) makes sense to me.

tools/lib/bpf/gen_loader.c static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, · line 782

Quoted 5 lines
tools/lib/bpf/gen_loader.c +5 -0
+/* Expects:
+ * BPF_REG_8 - pointer to instruction
+ */
+static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn)
+{

But I don't quite follow why we need these changes to emit_relo_ksym_btf. Maybe we should have these changes in a separate patch and add some more explanations?

Thanks,
Song

[...]

author maintainer
#5 lore raw

Re: [PATCH bpf-next v1 2/6] libbpf: Add typeless and weak ksym support to gen_loader

Quoted 20 lines · Song Liu
Quoted 10 lines · Kumar Kartikeya Dwivedi

This patch adds typeless and weak ksym support to BTF_KIND_VAR relocation code in gen_loader. For typeless ksym, we use the newly added bpf_kallsyms_lookup_name helper.

For weak ksym, we simply skip error check, and fix up the srg_reg for the insn, as keeping it as BPF_PSEUDO_BTF_ID for weak ksym with its insn[0].imm and insn[1].imm set as 0 will cause a failure. This is consistent with how libbpf relocates these two cases of BTF_KIND_VAR.

Quoted 5 lines

We also modify cleanup_relos to check for typeless ksyms in fd closing loop, since those have no fd associated with the ksym. For this we can reuse the unused 'off' member of ksym_desc.

[...]

Everything above (trimmed) makes sense to me.

Quoted 10 lines

tools/lib/bpf/gen_loader.c static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, · line 782

Quoted 5 lines
tools/lib/bpf/gen_loader.c +5 -0
+/* Expects:
+ * BPF_REG_8 - pointer to instruction
+ */
+static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn)
+{

But I don't quite follow why we need these changes to emit_relo_ksym_btf. Maybe we should have these changes in a separate patch and add some more explanations?

Before, if the bpf_btf_find_by_name_kind call failed, we just bailed out due to the emit_check_err. Now, if it is weak, the error check is conditional, so we set 0 as the default values and skip the store for btf_id and btf_fd if the btf lookup failed. Till here, it is similar to the case for emit_relo_kfunc_btf.

Note that we only reach this path once for each unique symbol: the next time, we enter the kdesc->ref > 1 branch, which copies from the existing insn.

Regarding src_reg stuff: in bpf_object__relocate_data, for obj->gen_loader, ext->is_set is always true. For the normal libbpf case, it is only true if the lookup succeeded for BTF (in bpf_object__resolve_ksym_var_btf_id). So depending on if ext->is_set, it skips assigning BPF_PSEUDO_BTF_ID to src_reg and zeroes out insn[0].imm and insn[1].imm. Also, the case for ext->is_set = false for libbpf is only reached if we don't fail on lookup error, and that depends on ext->is_weak. TLDR; ext->is_weak and lookup failure means src_reg is not assigned.

For gen_loader, since this src_reg assignment is always there, we need to clear it for the case where lookup failed, hence the:
-log:
+ emit(gen, BPF_JMP_IMM(BPF_JSGE, BPF_REG_7, 0, 3));

otherwise we end up with src_reg = BPF_PSEUDO_BTF_ID, imm[0] = 0, imm[1] = 0, which ends up failing the load.

Similarly, we jump over the src_reg adjustment from the kdesc->ref > 1 case if imm is not equal to 0 (if it were 0, then this is weak ksym). Error check ensures this instruction is only reached if relo->is_weak (for the same symbol), so we don't need to check it again there.

Doing it the other way around (not assigning BPF_PSEUDO_BTF_ID by default for gen_loader) would still involve writing to it in the success case, so IMO touching it seems unavoidable. If there are better ideas, please lmk.

I added the debug statements so that the selftest reloc result can be inspected easily, but not sure I can/should verify it from the selftest itself. I'll split typeless and weak ksym support into separate patches next time, and explain this in the commit message.

Quoted 4 lines

Thanks,
Song

[...]

--
Kartikeya

reviewer
#6 lore raw

Re: [PATCH bpf-next v1 2/6] libbpf: Add typeless and weak ksym support to gen_loader

Quoted 63 lines · Kumar Kartikeya Dwivedi
Quoted 20 lines · Song Liu
Quoted 10 lines · Kumar Kartikeya Dwivedi

This patch adds typeless and weak ksym support to BTF_KIND_VAR relocation code in gen_loader. For typeless ksym, we use the newly added bpf_kallsyms_lookup_name helper.

For weak ksym, we simply skip error check, and fix up the srg_reg for the insn, as keeping it as BPF_PSEUDO_BTF_ID for weak ksym with its insn[0].imm and insn[1].imm set as 0 will cause a failure. This is consistent with how libbpf relocates these two cases of BTF_KIND_VAR.

Quoted 5 lines

We also modify cleanup_relos to check for typeless ksyms in fd closing loop, since those have no fd associated with the ksym. For this we can reuse the unused 'off' member of ksym_desc.

[...]

Everything above (trimmed) makes sense to me.

Quoted 10 lines

tools/lib/bpf/gen_loader.c static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, · line 782

Quoted 5 lines
tools/lib/bpf/gen_loader.c +5 -0
+/* Expects:
+ * BPF_REG_8 - pointer to instruction
+ */
+static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, int insn)
+{

But I don't quite follow why we need these changes to emit_relo_ksym_btf. Maybe we should have these changes in a separate patch and add some more explanations?

Before, if the bpf_btf_find_by_name_kind call failed, we just bailed out due to the emit_check_err. Now, if it is weak, the error check is conditional, so we set 0 as the default values and skip the store for btf_id and btf_fd if the btf lookup failed. Till here, it is similar to the case for emit_relo_kfunc_btf.

Note that we only reach this path once for each unique symbol: the next time, we enter the kdesc->ref > 1 branch, which copies from the existing insn.

Regarding src_reg stuff: in bpf_object__relocate_data, for obj->gen_loader, ext->is_set is always true. For the normal libbpf case, it is only true if the lookup succeeded for BTF (in bpf_object__resolve_ksym_var_btf_id). So depending on if ext->is_set, it skips assigning BPF_PSEUDO_BTF_ID to src_reg and zeroes out insn[0].imm and insn[1].imm. Also, the case for ext->is_set = false for libbpf is only reached if we don't fail on lookup error, and that depends on ext->is_weak. TLDR; ext->is_weak and lookup failure means src_reg is not assigned.

For gen_loader, since this src_reg assignment is always there, we need to clear it for the case where lookup failed, hence the:

tools/lib/bpf/gen_loader.c +1 -1
-log:
+       emit(gen, BPF_JMP_IMM(BPF_JSGE, BPF_REG_7, 0, 3));

otherwise we end up with src_reg = BPF_PSEUDO_BTF_ID, imm[0] = 0, imm[1] = 0, which ends up failing the load.

Similarly, we jump over the src_reg adjustment from the kdesc->ref > 1 case if imm is not equal to 0 (if it were 0, then this is weak ksym). Error check ensures this instruction is only reached if relo->is_weak (for the same symbol), so we don't need to check it again there.

Quoted 8 lines

Doing it the other way around (not assigning BPF_PSEUDO_BTF_ID by default for gen_loader) would still involve writing to it in the success case, so IMO touching it seems unavoidable. If there are better ideas, please lmk.

I added the debug statements so that the selftest reloc result can be inspected easily, but not sure I can/should verify it from the selftest itself. I'll split typeless and weak ksym support into separate patches next time, and explain this in the commit message.

Thanks for these explanations.It will be helpful to include them in the commit log.

Song

author maintainer
#7 lore raw

PATCH 3/6 libbpf: Ensure that module BTF fd is never 0

In reply to Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +14 -0
index d286dec73b5f..3e5e460fe63e 100644
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
 			pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
 			return err;
 		}
+		/* Make sure module BTF fd is never 0, as kernel depends on it
+		 * being > 0 to distinguish between vmlinux and module BTFs,
+		 * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+		 */
+		if (!fd) {
+			fd = dup(0);
+			if (fd < 0) {
+				err = -errno;
+				pr_warn("failed to dup BTF object #%d FD 0 to FD > 0: %d\n", id, err);
+				close(0);
+				return err;
+			}
+			close(0);
+		}
 
 		len = sizeof(info);
 		memset(&info, 0, sizeof(info));
Signature · 1 line
2.33.0
#8 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 18 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index d286dec73b5f..3e5e460fe63e 100644

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

Doing something like that in bpf_object__open() or bpf_object__load() would make everything much simpler and we'll have a guarantee that fd == 0 is not going to be allocated (unless someone accidentally or not accidentally does close(0), but that's entirely different story).

Quoted 14 lines
tools/lib/bpf/libbpf.c +8 -1
+                       if (fd < 0) {
+                               err = -errno;
+                               pr_warn("failed to dup BTF object #%d FD 0 to FD > 0: %d\n", id, err);
+                               close(0);
+                               return err;
+                       }
+                       close(0);
+               }
 
                len = sizeof(info);
                memset(&info, 0, sizeof(info));
--

2.33.0

author maintainer
#9 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 27 lines · Andrii Nakryiko
Quoted 26 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +5 -0
index d286dec73b5f..3e5e460fe63e 100644
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
Quoted 9 lines

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

We can, e.g. using access("/proc/self/fd/0", F_OK), but I think just calling open unconditonally and doing if (ret > 0) close(ret) is better. Also, do I leave it lingering, or should I close(0) if we created it on destroy?

Quoted 19 lines

Doing something like that in bpf_object__open() or bpf_object__load() would make everything much simpler and we'll have a guarantee that fd == 0 is not going to be allocated (unless someone accidentally or not accidentally does close(0), but that's entirely different story).

Quoted 14 lines
tools/lib/bpf/libbpf.c +8 -1
+                       if (fd < 0) {
+                               err = -errno;
+                               pr_warn("failed to dup BTF object #%d FD 0 to FD > 0: %d\n", id, err);
+                               close(0);
+                               return err;
+                       }
+                       close(0);
+               }
 
                len = sizeof(info);
                memset(&info, 0, sizeof(info));
--

2.33.0

--
Kartikeya

#10 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 27 lines · Andrii Nakryiko
Quoted 26 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +5 -0
index d286dec73b5f..3e5e460fe63e 100644
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
Quoted 9 lines

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

Quoted 2 lines

We can, e.g. using access("/proc/self/fd/0", F_OK), but I think just calling open unconditonally and doing if (ret > 0) close(ret) is better. Also, do I

yeah, I like this idea, let's go with it

Quoted 1 line

leave it lingering, or should I close(0) if we created it on destroy?

I don't mind leaving it open indefinitely, but can you please check that it doesn't trigger LeakSanitizer errors?

Quoted 23 lines
Quoted 20 lines

Doing something like that in bpf_object__open() or bpf_object__load() would make everything much simpler and we'll have a guarantee that fd == 0 is not going to be allocated (unless someone accidentally or not accidentally does close(0), but that's entirely different story).

Quoted 14 lines
tools/lib/bpf/libbpf.c +8 -1
+                       if (fd < 0) {
+                               err = -errno;
+                               pr_warn("failed to dup BTF object #%d FD 0 to FD > 0: %d\n", id, err);
+                               close(0);
+                               return err;
+                       }
+                       close(0);
+               }
 
                len = sizeof(info);
                memset(&info, 0, sizeof(info));
--

2.33.0

--
Kartikeya

#11 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 42 lines · Andrii Nakryiko
Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 36 lines · Andrii Nakryiko
Quoted 18 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index d286dec73b5f..3e5e460fe63e 100644

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

Quoted 4 lines
Quoted 2 lines

We can, e.g. using access("/proc/self/fd/0", F_OK), but I think just calling open unconditonally and doing if (ret > 0) close(ret) is better. Also, do I

yeah, I like this idea, let's go with it

FYI some production environments may detect that FDs 0,1,2 are not pointing to stdin, stdout, stderr and will force close whatever files are there and open 0,1,2 with canonical files.

libbpf doesn't have to resort to such measures, but it would be prudent to make libbpf operate on FDs > 2 for all bpf objects to make sure other frameworks don't ruin libbpf's view of FDs.

#12 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 50 lines · Alexei Starovoitov
Quoted 42 lines · Andrii Nakryiko
Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 36 lines · Andrii Nakryiko
Quoted 18 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index d286dec73b5f..3e5e460fe63e 100644

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

Quoted 4 lines
Quoted 2 lines

We can, e.g. using access("/proc/self/fd/0", F_OK), but I think just calling open unconditonally and doing if (ret > 0) close(ret) is better. Also, do I

yeah, I like this idea, let's go with it

Quoted 7 lines

FYI some production environments may detect that FDs 0,1,2 are not pointing to stdin, stdout, stderr and will force close whatever files are there and open 0,1,2 with canonical files.

libbpf doesn't have to resort to such measures, but it would be prudent to make libbpf operate on FDs > 2 for all bpf objects to make sure other frameworks don't ruin libbpf's view of FDs.

oh well, even without those production complications this would be a bit fragile, e.g., if the application temporarily opened FD 0 and then closed it.

Ok, Kumar, can you please do it as a simple helper that would dup()'ing until we have FD>2, and use it in as few places as possible to make sure that all FDs (not just module BTF) are covered. I'd suggest doing that only in low-level helpers in btf.c, I think libbpf's logic always goes through those anyways (but please double-check that we don't call bpf syscall directly anywhere else).

#13 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 64 lines · Andrii Nakryiko
Quoted 50 lines · Alexei Starovoitov
Quoted 42 lines · Andrii Nakryiko
Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 36 lines · Andrii Nakryiko
Quoted 18 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index d286dec73b5f..3e5e460fe63e 100644

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

Quoted 4 lines
Quoted 2 lines

We can, e.g. using access("/proc/self/fd/0", F_OK), but I think just calling open unconditonally and doing if (ret > 0) close(ret) is better. Also, do I

yeah, I like this idea, let's go with it

Quoted 7 lines

FYI some production environments may detect that FDs 0,1,2 are not pointing to stdin, stdout, stderr and will force close whatever files are there and open 0,1,2 with canonical files.

libbpf doesn't have to resort to such measures, but it would be prudent to make libbpf operate on FDs > 2 for all bpf objects to make sure other frameworks don't ruin libbpf's view of FDs.

oh well, even without those production complications this would be a bit fragile, e.g., if the application temporarily opened FD 0 and then closed it.

Quoted 6 lines

Ok, Kumar, can you please do it as a simple helper that would dup()'ing until we have FD>2, and use it in as few places as possible to make sure that all FDs (not just module BTF) are covered. I'd suggest doing that only in low-level helpers in btf.c, I think libbpf's logic always goes through those anyways (but please double-check that we don't call bpf syscall directly anywhere else).

FYI, you can use fcntl() with F_DUPFD{,_CLOEXEC} and tell it the minimum fd number you're interested in for the clone. We do that in libxdp to protect against fd 0:

https://github.com/xdp-project/xdp-tools/blob/master/lib/libxdp/libxdp.c#L1184

Given Alexei's comments above, maybe we should be '3' for the last arg instead of 1...

-Toke

author maintainer
#14 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 66 lines · Toke Høiland-Jørgensen
Quoted 64 lines · Andrii Nakryiko
Quoted 50 lines · Alexei Starovoitov
Quoted 42 lines · Andrii Nakryiko
Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 36 lines · Andrii Nakryiko
Quoted 18 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index d286dec73b5f..3e5e460fe63e 100644

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

Quoted 4 lines
Quoted 2 lines

We can, e.g. using access("/proc/self/fd/0", F_OK), but I think just calling open unconditonally and doing if (ret > 0) close(ret) is better. Also, do I

yeah, I like this idea, let's go with it

Quoted 7 lines

FYI some production environments may detect that FDs 0,1,2 are not pointing to stdin, stdout, stderr and will force close whatever files are there and open 0,1,2 with canonical files.

libbpf doesn't have to resort to such measures, but it would be prudent to make libbpf operate on FDs > 2 for all bpf objects to make sure other frameworks don't ruin libbpf's view of FDs.

oh well, even without those production complications this would be a bit fragile, e.g., if the application temporarily opened FD 0 and then closed it.

Quoted 7 lines
Quoted 6 lines

Ok, Kumar, can you please do it as a simple helper that would dup()'ing until we have FD>2, and use it in as few places as possible to make sure that all FDs (not just module BTF) are covered. I'd suggest doing that only in low-level helpers in btf.c, I think libbpf's logic always goes through those anyways (but please double-check that we don't call bpf syscall directly anywhere else).

Just to make sure I am on the same page:

I have to...
1. Add a small wrapper (currently named fd_gt_2, any other suggestions?) that takes in the fd, and dups it to fd >= 3 if in range [0, 2] (and closes original fd in this case).
Use this for all fd returning bpf syscalls in bpf.c (btf.c is a typo?). Audit other places directly calling syscall(__NR_bpf, ...).
2. Assume that the situation Alexei mentioned only occurs at startup, or sometime later, not in parallel (which would race with us, and not sure we can deal with it). I'm thinking of a case where such an fd gets passed to an exec'd binary which closes invalids fds on startup (so keeping them

Quoted 1 line

= 3 allows proper inheritance).

3. gen_loader can hit the same case, so short of adding a bpf_sys_fcntl (or the helper only exposing F_DUPFD), next best option is to reserve the three fds from skel_internal.h or gen_trace (in bpftool) and close later after loading is done.

Feedback needed on 3 (and whether a generic bpf_sys_dup providing functionality of existing fcntl and dup{,2,3} is better than simply reserving the three fds at load time).

Quoted 4 lines

FYI, you can use fcntl() with F_DUPFD{,_CLOEXEC} and tell it the minimum fd number you're interested in for the clone. We do that in libxdp to protect against fd 0:

Thanks, will switch the dup to fcntl in the next version.

Quoted 7 lines

https://github.com/xdp-project/xdp-tools/blob/master/lib/libxdp/libxdp.c#L1184

Given Alexei's comments above, maybe we should be '3' for the last arg instead of 1...

-Toke

--
Kartikeya

#15 lore raw

Re: [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0

Quoted 76 lines · Kumar Kartikeya Dwivedi
Quoted 66 lines · Toke Høiland-Jørgensen
Quoted 64 lines · Andrii Nakryiko
Quoted 50 lines · Alexei Starovoitov
Quoted 42 lines · Andrii Nakryiko
Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 36 lines · Andrii Nakryiko
Quoted 18 lines · Kumar Kartikeya Dwivedi

Since the code assumes in various places that BTF fd for modules is never 0, if we end up getting fd as 0, obtain a new fd > 0. Even though fd 0 being free for allocation is usually an application error, it is still possible that we end up getting fd 0 if the application explicitly closes its stdin. Deal with this by getting a new fd using dup and closing fd 0.

---
 tools/lib/bpf/libbpf.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index d286dec73b5f..3e5e460fe63e 100644

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 4983

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
@@ -4975,6 +4975,20 @@ static int load_module_btfs(struct bpf_object *obj)
                        pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
                        return err;
                }
+               /* Make sure module BTF fd is never 0, as kernel depends on it
+                * being > 0 to distinguish between vmlinux and module BTFs,
+                * e.g. for BPF_PSEUDO_BTF_ID ld_imm64 insns (ksyms).
+                */
+               if (!fd) {
+                       fd = dup(0);

This is not the only place where we make assumptions that fd > 0 but technically can get fd == 0. Instead of doing such a check in every such place, would it be possible to open (cheaply) some FD (/dev/null or whatever, don't know what's the best file to open), if we detect that FD == 0 is not allocated? Can we detect that fd 0 is not allocated?

Quoted 4 lines
Quoted 2 lines

We can, e.g. using access("/proc/self/fd/0", F_OK), but I think just calling open unconditonally and doing if (ret > 0) close(ret) is better. Also, do I

yeah, I like this idea, let's go with it

Quoted 7 lines

FYI some production environments may detect that FDs 0,1,2 are not pointing to stdin, stdout, stderr and will force close whatever files are there and open 0,1,2 with canonical files.

libbpf doesn't have to resort to such measures, but it would be prudent to make libbpf operate on FDs > 2 for all bpf objects to make sure other frameworks don't ruin libbpf's view of FDs.

oh well, even without those production complications this would be a bit fragile, e.g., if the application temporarily opened FD 0 and then closed it.

Quoted 7 lines
Quoted 6 lines

Ok, Kumar, can you please do it as a simple helper that would dup()'ing until we have FD>2, and use it in as few places as possible to make sure that all FDs (not just module BTF) are covered. I'd suggest doing that only in low-level helpers in btf.c, I think libbpf's logic always goes through those anyways (but please double-check that we don't call bpf syscall directly anywhere else).

Quoted 4 lines

Just to make sure I am on the same page:

I have to...
1. Add a small wrapper (currently named fd_gt_2, any other suggestions?)

ensure_good_fd() or something? 2 is a tiny detail there.

Quoted 3 lines

that takes in the fd, and dups it to fd >= 3 if in range [0, 2] (and closes original fd in this case).
Use this for all fd returning bpf syscalls in bpf.c (btf.c is a typo?).

yep, typo, I meant bpf.c

Quoted 1 line

Audit other places directly calling syscall(__NR_bpf, ...).

yep

Quoted 5 lines

2. Assume that the situation Alexei mentioned only occurs at startup, or sometime later, not in parallel (which would race with us, and not sure we can deal with it). I'm thinking of a case where such an fd gets passed to an exec'd binary which closes invalids fds on startup (so keeping them

Quoted 1 line

= 3 allows proper inheritance).

with checking it next to syscall(__NR_bpf) and fcntl suggestion from Toke, why does it matter?

Quoted 3 lines

3. gen_loader can hit the same case, so short of adding a bpf_sys_fcntl (or the helper only exposing F_DUPFD), next best option is to reserve the three fds from skel_internal.h or gen_trace (in bpftool) and close later after loading is done.

Not sure, will defer to Alexei.

Quoted 22 lines

Feedback needed on 3 (and whether a generic bpf_sys_dup providing functionality of existing fcntl and dup{,2,3} is better than simply reserving the three fds at load time).

Quoted 4 lines

FYI, you can use fcntl() with F_DUPFD{,_CLOEXEC} and tell it the minimum fd number you're interested in for the clone. We do that in libxdp to protect against fd 0:

Thanks, will switch the dup to fcntl in the next version.

Quoted 7 lines

https://github.com/xdp-project/xdp-tools/blob/master/lib/libxdp/libxdp.c#L1184

Given Alexei's comments above, maybe we should be '3' for the last arg instead of 1...

-Toke

--
Kartikeya

author maintainer
#16 lore raw

PATCH 4/6 bpf: selftests: Move test_ksyms_weak test to lskel, add libbpf test

In reply to Kumar Kartikeya Dwivedi

Also, avoid using CO-RE features, as lskel doesn't support CO-RE, yet. Create a file for testing libbpf skeleton as well, so that both gen_loader and libbpf get tested.

---
 tools/testing/selftests/bpf/Makefile          |  2 +-
 .../selftests/bpf/prog_tests/ksyms_btf.c      |  6 ++--
 .../bpf/prog_tests/ksyms_weak_libbpf.c        | 31 +++++++++++++++++++
 .../selftests/bpf/progs/test_ksyms_weak.c     |  3 +-
 4 files changed, 36 insertions(+), 6 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c
tools/testing/selftests/bpf/Makefile +1 -1
index c5c9a9f50d8d..4ae5bc852ca8 100644
@@ -317,7 +317,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
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c +3 -3
index cf3acfa5a91d..21ff02d47076 100644
@@ -6,7 +6,7 @@
 #include <bpf/btf.h>
 #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 +89,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 */
tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c new file +31 -0
new file mode 100644
index 000000000000..b75725e28647
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "test_ksyms_weak.skel.h"
+
+void test_ksyms_weak_libbpf(void)
+{
+	struct test_ksyms_weak *skel;
+	struct test_ksyms_weak__data *data;
+	int err;
+
+	skel = test_ksyms_weak__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_ksyms_weak__open_and_load"))
+		return;
+
+	err = test_ksyms_weak__attach(skel);
+	if (!ASSERT_OK(err, "test_ksyms_weak__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__destroy(skel);
+}
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +1 -2
index 5f8379aadb29..521e7b99db08 100644
@@ -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
reviewer
#17 lore raw

Re: [PATCH bpf-next v1 4/6] bpf: selftests: Move test_ksyms_weak test to lskel, add libbpf test

Quoted 6 lines · Kumar Kartikeya Dwivedi

Also, avoid using CO-RE features, as lskel doesn't support CO-RE, yet. Create a file for testing libbpf skeleton as well, so that both gen_loader and libbpf get tested.

[...]

tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c line 6

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c new file +6 -0
new file mode 100644
index 000000000000..b75725e28647
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "test_ksyms_weak.skel.h"
+
+void test_ksyms_weak_libbpf(void)

This is (almost?) the same as test_weak_syms(), right? Why do we need both?

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c new file +8 -0
+{
+       struct test_ksyms_weak *skel;
+       struct test_ksyms_weak__data *data;
+       int err;
+
+       skel = test_ksyms_weak__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "test_ksyms_weak__open_and_load"))
+               return;

[...]

Quoted 13 lines
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +0 -1
index 5f8379aadb29..521e7b99db08 100644
@@ -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. */

tools/testing/selftests/bpf/progs/test_ksyms_weak.c int pass_handler(const void *ctx) · line 40

Quoted 5 lines
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +1 -1
@@ -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;

Why do we need this change?

Quoted 6 lines
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +0 -1
        out__existing_typeless = (__u64)&bpf_prog_active;
 
        /* tests non-existent symbols. */
--

2.33.0

author maintainer
#18 lore raw

Re: [PATCH bpf-next v1 4/6] bpf: selftests: Move test_ksyms_weak test to lskel, add libbpf test

Quoted 8 lines · Song Liu
Quoted 6 lines · Kumar Kartikeya Dwivedi

Also, avoid using CO-RE features, as lskel doesn't support CO-RE, yet. Create a file for testing libbpf skeleton as well, so that both gen_loader and libbpf get tested.

[...]

Quoted 15 lines

tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c line 6

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c new file +6 -0
new file mode 100644
index 000000000000..b75725e28647
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "test_ksyms_weak.skel.h"
+
+void test_ksyms_weak_libbpf(void)

This is (almost?) the same as test_weak_syms(), right? Why do we need both?

One includes lskel.h (light skeleton), the other includes skel.h (libbpf skeleton). Trying to include both in the same file, it ends up redefining the same struct. I am not sure whether adding a prefix/suffix to light skeleton struct names is possible now, maybe through another option to bpftool in addition to name?

Quoted 22 lines
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c new file +8 -0
+{
+       struct test_ksyms_weak *skel;
+       struct test_ksyms_weak__data *data;
+       int err;
+
+       skel = test_ksyms_weak__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "test_ksyms_weak__open_and_load"))
+               return;

[...]

Quoted 11 lines
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +0 -1
index 5f8379aadb29..521e7b99db08 100644
@@ -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. */
 
Quoted 10 lines

tools/testing/selftests/bpf/progs/test_ksyms_weak.c int pass_handler(const void *ctx) · line 40

Quoted 7 lines
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +1 -1
 /* 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;

Why do we need this change?

Since they share the same BPF object for generating skeleton, it needs to remove dependency on CO-RE which gen_loader does not support.

If it is kept, we get this:
...
libbpf: // TODO core_relo: prog 0 insn[5] rq kind 0
libbpf: prog 'pass_handler': relo #0: failed to relocate: -95
libbpf: failed to perform CO-RE relocations: -95
libbpf: failed to load object 'test_ksyms_weak'
...

Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_ksyms_weak.c +0 -1
        out__existing_typeless = (__u64)&bpf_prog_active;
 
        /* tests non-existent symbols. */
--

2.33.0

--
Kartikeya

author maintainer
#19 lore raw

Re: [PATCH bpf-next v1 4/6] bpf: selftests: Move test_ksyms_weak test to lskel, add libbpf test

Quoted 25 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Song Liu
Quoted 6 lines · Kumar Kartikeya Dwivedi

Also, avoid using CO-RE features, as lskel doesn't support CO-RE, yet. Create a file for testing libbpf skeleton as well, so that both gen_loader and libbpf get tested.

[...]

Quoted 15 lines

tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c line 6

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/ksyms_weak_libbpf.c new file +6 -0
new file mode 100644
index 000000000000..b75725e28647
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "test_ksyms_weak.skel.h"
+
+void test_ksyms_weak_libbpf(void)

This is (almost?) the same as test_weak_syms(), right? Why do we need both?

Quoted 5 lines

One includes lskel.h (light skeleton), the other includes skel.h (libbpf skeleton). Trying to include both in the same file, it ends up redefining the same struct. I am not sure whether adding a prefix/suffix to light skeleton struct names is possible now, maybe through another option to bpftool in addition to name?

Sorry, I misremembered. The name option is enough, it is because of how I did it in the Makefile (using LSKELS_EXTRA). I'll fix this in the next spin.

Quoted 1 line

[...]

--
Kartikeya

reviewer
#20 lore raw

Re: [PATCH bpf-next v1 4/6] bpf: selftests: Move test_ksyms_weak test to lskel, add libbpf test

In reply to Kumar Kartikeya Dwivedi
Quoted 1 line · Kumar Kartikeya Dwivedi

[...]

Quoted 15 lines
Quoted 11 lines

tools/testing/selftests/bpf/progs/test_ksyms_weak.c int pass_handler(const void *ctx) · line 40

Quoted 8 lines

/* typeless symbols, default to zero. */

tools/testing/selftests/bpf/progs/test_ksyms_weak.c +1 -1
@@ -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;

Why do we need this change?

Since they share the same BPF object for generating skeleton, it needs to remove dependency on CO-RE which gen_loader does not support.

Quoted 6 lines

If it is kept, we get this:
...
libbpf: // TODO core_relo: prog 0 insn[5] rq kind 0
libbpf: prog 'pass_handler': relo #0: failed to relocate: -95
libbpf: failed to perform CO-RE relocations: -95
libbpf: failed to load object 'test_ksyms_weak'

I see. Thanks for the explanation.

Song

author maintainer
#21 lore raw

PATCH 5/6 bpf: selftests: Fix fd cleanup in sk_lookup test

In reply to Kumar Kartikeya Dwivedi

Similar to the fix in commit:
e31eec77e4ab (bpf: selftests: Fix fd cleanup in get_branch_snapshot)

We use memset to set fds to -1 without breaking on future changes to the array size (when MAX_SERVER constant is bumped).

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.

Cc Jakub Sitnicki <[email protected]>
Fixes 0ab5539f8584 (selftests/bpf: Tests for BPF_SK_LOOKUP attach point)
---
 .../selftests/bpf/prog_tests/sk_lookup.c      | 20 +++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +14 -6
index aee41547e7f4..572220065bdf 100644
@@ -598,11 +598,14 @@ 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 client_fd, reuse_conn_fd = -1;
 	struct bpf_link *lookup_link;
+	int server_fds[MAX_SERVERS];
 	int i, err;
 
+	/* set all fds to -1 */
+	memset(server_fds, 0xFF, sizeof(server_fds));
+
 	lookup_link = attach_lookup_prog(t->lookup_prog);
 	if (!lookup_link)
 		return;
@@ -663,8 +666,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,11 +1057,14 @@ 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[MAX_SERVERS];
 	struct bpf_sk_lookup ctx;
 	__u64 server_cookie;
 	int i, err;
 
+	/* set all fds to -1 */
+	memset(server_fds, 0xFF, sizeof(server_fds));
+
 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
 		.ctx_in = &ctx,
 		.ctx_size_in = sizeof(ctx),
@@ -1097,8 +1104,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
Reviewed-by on v1 5/6
#22 lore raw

Re: [PATCH bpf-next v1 5/6] bpf: selftests: Fix fd cleanup in sk_lookup test

Quoted 67 lines · Kumar Kartikeya Dwivedi

Similar to the fix in commit:
e31eec77e4ab (bpf: selftests: Fix fd cleanup in get_branch_snapshot)

We use memset to set fds to -1 without breaking on future changes to the array size (when MAX_SERVER constant is bumped).

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.

Cc Jakub Sitnicki <[email protected]>
Fixes 0ab5539f8584 (selftests/bpf: Tests for BPF_SK_LOOKUP attach point)
---
 .../selftests/bpf/prog_tests/sk_lookup.c      | 20 +++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +11 -4
index aee41547e7f4..572220065bdf 100644
@@ -598,11 +598,14 @@ 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 client_fd, reuse_conn_fd = -1;
 	struct bpf_link *lookup_link;
+	int server_fds[MAX_SERVERS];
 	int i, err;
 
+	/* set all fds to -1 */
+	memset(server_fds, 0xFF, sizeof(server_fds));
+
 	lookup_link = attach_lookup_prog(t->lookup_prog);
 	if (!lookup_link)
 		return;
@@ -663,8 +666,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,11 +1057,14 @@ 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[MAX_SERVERS];
 	struct bpf_sk_lookup ctx;
 	__u64 server_cookie;
 	int i, err;
 
+	/* set all fds to -1 */
+	memset(server_fds, 0xFF, sizeof(server_fds));
+
 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
 		.ctx_in = &ctx,
 		.ctx_size_in = sizeof(ctx),

tools/testing/selftests/bpf/prog_tests/sk_lookup.c static void run_sk_assign(struct test_sk_lookup *skel, · line 1111

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +3 -2
@@ -1097,8 +1104,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]);
 	}
 }

My mistake. Thanks for the fix.

Alternatively we can use an extended designated initializer:

int server_fds[] = { [0 ... MAX_SERVERS] = -1 };

reviewer Acked-by
#23 lore raw

Re: [PATCH bpf-next v1 5/6] bpf: selftests: Fix fd cleanup in sk_lookup test

Quoted 8 lines · Jakub Sitnicki
Quoted 6 lines · Kumar Kartikeya Dwivedi

Similar to the fix in commit:
e31eec77e4ab (bpf: selftests: Fix fd cleanup in get_branch_snapshot)

We use memset to set fds to -1 without breaking on future changes to the array size (when MAX_SERVER constant is bumped).

Quoted 7 lines
Quoted 7 lines

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.

Cc Jakub Sitnicki <[email protected]>
Fixes 0ab5539f8584 (selftests/bpf: Tests for BPF_SK_LOOKUP attach point)

Similar to Andrii's comment in 6/6, we need add " to the Fixes tag.

Quoted 1 line
Quoted 1 line
Quoted 2 lines
author maintainer
#24 lore raw

PATCH 6/6 bpf: selftests: Fix memory leak in test_ima

In reply to Kumar Kartikeya Dwivedi

The allocated ring buffer is never freed, do so in the cleanup path.

Fixes f446b570ac7e (bpf/selftests: Update the IMA test to use BPF ring buffer)
---
 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
index 0252f61d611a..97d8a6f84f4a 100644
@@ -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
Acked-by on v1 6/6
#25 lore raw

Re: [PATCH bpf-next v1 6/6] bpf: selftests: Fix memory leak in test_ima

Quoted 6 lines · Kumar Kartikeya Dwivedi

The allocated ring buffer is never freed, do so in the cleanup path.

Fixes f446b570ac7e (bpf/selftests: Update the IMA test to use BPF ring buffer)

---

Please stick to "selftests/bpf: " prefix which we use consistently for BPF selftests patches.

Other than that LGTM.

Quoted 26 lines
 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 -2
index 0252f61d611a..97d8a6f84f4a 100644
@@ -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);
 }
--

2.33.0

reviewer Acked-by
#26 lore raw

Re: [PATCH bpf-next v1 6/6] bpf: selftests: Fix memory leak in test_ima

Quoted 9 lines · Andrii Nakryiko
Quoted 6 lines · Kumar Kartikeya Dwivedi

The allocated ring buffer is never freed, do so in the cleanup path.

Fixes f446b570ac7e (bpf/selftests: Update the IMA test to use BPF ring buffer)

---

Quoted 6 lines

Please stick to "selftests/bpf: " prefix which we use consistently for BPF selftests patches.

Other than that LGTM.