diff mbox series

[bpf-next,v4,2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset

Message ID 20220206043107.18549-3-houtao1@huawei.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series selftests: add test for kfunc call | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 6 maintainers not CCed: linux-kselftest@vger.kernel.org delyank@fb.com kpsingh@kernel.org songliubraving@fb.com shuah@kernel.org memxor@gmail.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: From:/Signed-off-by: email address mismatch: 'From: Hou Tao <hotforest@gmail.com>' != 'Signed-off-by: Hou Tao <houtao1@huawei.com>' WARNING: line length of 84 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next fail VM_Test

Commit Message

Hou Tao Feb. 6, 2022, 4:31 a.m. UTC
In add_kfunc_call(), bpf_kfunc_desc->imm with type s32 is used to
represent the offset of called kfunc from __bpf_call_base, so
add a test to ensure that the offset will not be overflowed.

Signed-off-by: Hou Tao <houtao1@huawei.com>
---
 .../selftests/bpf/prog_tests/ksyms_module.c   | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)

Comments

Yonghong Song Feb. 7, 2022, 6:33 p.m. UTC | #1
On 2/5/22 8:31 PM, Hou Tao wrote:
> In add_kfunc_call(), bpf_kfunc_desc->imm with type s32 is used to
> represent the offset of called kfunc from __bpf_call_base, so
> add a test to ensure that the offset will not be overflowed.
> 
> Signed-off-by: Hou Tao <houtao1@huawei.com>
> ---
>   .../selftests/bpf/prog_tests/ksyms_module.c   | 42 +++++++++++++++++++
>   1 file changed, 42 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
> index a1ebac70ec29..8055fbbf720b 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
> @@ -3,9 +3,49 @@
>   
>   #include <test_progs.h>
>   #include <network_helpers.h>
> +#include <trace_helpers.h>
>   #include "test_ksyms_module.lskel.h"
>   #include "test_ksyms_module.skel.h"
>   
> +/*
> + * Check whether or not s32 in bpf_kfunc_desc is sufficient
> + * to represent the offset between bpf_testmod_test_mod_kfunc
> + * and __bpf_call_base.
> + */
> +static void test_ksyms_module_valid_offset(void)
> +{
> +	struct test_ksyms_module *skel;
> +	unsigned long long kfunc_addr;
> +	unsigned long long base_addr;
> +	long long actual_offset;
> +	int used_offset;
> +	int err;
> +
> +	if (!env.has_testmod) {
> +		test__skip();
> +		return;
> +	}
> +
> +	/* Ensure kfunc call is supported */
> +	skel = test_ksyms_module__open_and_load();
> +	if (!ASSERT_OK_PTR(skel, "test_ksyms_module__open"))
> +		return;
> +
> +	err = kallsyms_find("bpf_testmod_test_mod_kfunc", &kfunc_addr);
> +	if (!ASSERT_OK(err, "find kfunc addr"))
> +		goto cleanup;
> +
> +	err = kallsyms_find("__bpf_call_base", &base_addr);
> +	if (!ASSERT_OK(err, "find base addr"))
> +		goto cleanup;
> +
> +	used_offset = kfunc_addr - base_addr;
> +	actual_offset = kfunc_addr - base_addr;
> +	ASSERT_EQ((long long)used_offset, actual_offset, "kfunc offset overflowed");

I am a little bit confused about motivation here. Maybe I missed 
something. If we indeed have kfunc offset overflow,
should kernel verifier just reject the program? Specially,
we should make the above test_ksyms_module__open_and_load()
fail?

> +cleanup:
> +	test_ksyms_module__destroy(skel);
> +}
> +
>   static void test_ksyms_module_lskel(void)
>   {
>   	struct test_ksyms_module_lskel *skel;
> @@ -62,6 +102,8 @@ static void test_ksyms_module_libbpf(void)
>   
>   void test_ksyms_module(void)
>   {
> +	if (test__start_subtest("valid_offset"))
> +		test_ksyms_module_valid_offset();
>   	if (test__start_subtest("lskel"))
>   		test_ksyms_module_lskel();
>   	if (test__start_subtest("libbpf"))
Hou Tao Feb. 8, 2022, 2:47 a.m. UTC | #2
Hi,

On 2/8/2022 2:33 AM, Yonghong Song wrote:
>
>
> On 2/5/22 8:31 PM, Hou Tao wrote:
>> In add_kfunc_call(), bpf_kfunc_desc->imm with type s32 is used to
>> represent the offset of called kfunc from __bpf_call_base, so
>> add a test to ensure that the offset will not be overflowed.
>>
>> Signed-off-by: Hou Tao <houtao1@huawei.com>
>> ---
>>   .../selftests/bpf/prog_tests/ksyms_module.c   | 42 +++++++++++++++++++
>>   1 file changed, 42 insertions(+)
>>
[...]
>> +    /* Ensure kfunc call is supported */
>> +    skel = test_ksyms_module__open_and_load();
>> +    if (!ASSERT_OK_PTR(skel, "test_ksyms_module__open"))
>> +        return;
>> +
>> +    err = kallsyms_find("bpf_testmod_test_mod_kfunc", &kfunc_addr);
>> +    if (!ASSERT_OK(err, "find kfunc addr"))
>> +        goto cleanup;
>> +
>> +    err = kallsyms_find("__bpf_call_base", &base_addr);
>> +    if (!ASSERT_OK(err, "find base addr"))
>> +        goto cleanup;
>> +
>> +    used_offset = kfunc_addr - base_addr;
>> +    actual_offset = kfunc_addr - base_addr;
>> +    ASSERT_EQ((long long)used_offset, actual_offset, "kfunc offset
>> overflowed");
>
> I am a little bit confused about motivation here. Maybe I missed something. If
> we indeed have kfunc offset overflow,
> should kernel verifier just reject the program? Specially,
> we should make the above test_ksyms_module__open_and_load()
> fail?
In add_kfunc_call(), the calculation of imm doesn't consider the overflow
of s32. So test_ksyms_module__open_and_load() will succeed. I think the
better solution is to put the overflow check in add_kfunc_call(), so will
drop this patch and add the overflow check in add_kfunc_call() instead.

Regards,
Tao

[...]
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
index a1ebac70ec29..8055fbbf720b 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
@@ -3,9 +3,49 @@ 
 
 #include <test_progs.h>
 #include <network_helpers.h>
+#include <trace_helpers.h>
 #include "test_ksyms_module.lskel.h"
 #include "test_ksyms_module.skel.h"
 
+/*
+ * Check whether or not s32 in bpf_kfunc_desc is sufficient
+ * to represent the offset between bpf_testmod_test_mod_kfunc
+ * and __bpf_call_base.
+ */
+static void test_ksyms_module_valid_offset(void)
+{
+	struct test_ksyms_module *skel;
+	unsigned long long kfunc_addr;
+	unsigned long long base_addr;
+	long long actual_offset;
+	int used_offset;
+	int err;
+
+	if (!env.has_testmod) {
+		test__skip();
+		return;
+	}
+
+	/* Ensure kfunc call is supported */
+	skel = test_ksyms_module__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_ksyms_module__open"))
+		return;
+
+	err = kallsyms_find("bpf_testmod_test_mod_kfunc", &kfunc_addr);
+	if (!ASSERT_OK(err, "find kfunc addr"))
+		goto cleanup;
+
+	err = kallsyms_find("__bpf_call_base", &base_addr);
+	if (!ASSERT_OK(err, "find base addr"))
+		goto cleanup;
+
+	used_offset = kfunc_addr - base_addr;
+	actual_offset = kfunc_addr - base_addr;
+	ASSERT_EQ((long long)used_offset, actual_offset, "kfunc offset overflowed");
+cleanup:
+	test_ksyms_module__destroy(skel);
+}
+
 static void test_ksyms_module_lskel(void)
 {
 	struct test_ksyms_module_lskel *skel;
@@ -62,6 +102,8 @@  static void test_ksyms_module_libbpf(void)
 
 void test_ksyms_module(void)
 {
+	if (test__start_subtest("valid_offset"))
+		test_ksyms_module_valid_offset();
 	if (test__start_subtest("lskel"))
 		test_ksyms_module_lskel();
 	if (test__start_subtest("libbpf"))