diff mbox series

[v3,bpf-next,3/3] bpf/selftests: Test PTR_TO_RDONLY_MEM

Message ID 20211109003052.3499225-4-haoluo@google.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Prevent writing read-only memory | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
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 8 maintainers not CCed: songliubraving@fb.com linux-kselftest@vger.kernel.org memxor@gmail.com shuah@kernel.org john.fastabend@gmail.com yhs@fb.com netdev@vger.kernel.org kafai@fb.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: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 83 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 success VM_Test

Commit Message

Hao Luo Nov. 9, 2021, 12:30 a.m. UTC
This test verifies that a ksym of non-struct can not be directly
updated.

Signed-off-by: Hao Luo <haoluo@google.com>
---
 Changes since v2:
  - Rebase

 Changes since v1:
  - Replaced CHECK() with ASSERT_ERR_PTR()
  - Commented in the test for the reason of verification failure.

 .../selftests/bpf/prog_tests/ksyms_btf.c      | 14 +++++++++
 .../bpf/progs/test_ksyms_btf_write_check.c    | 29 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c

Comments

Andrii Nakryiko Nov. 10, 2021, 4:49 a.m. UTC | #1
On Mon, Nov 8, 2021 at 4:31 PM Hao Luo <haoluo@google.com> wrote:
>
> This test verifies that a ksym of non-struct can not be directly
> updated.
>
> Signed-off-by: Hao Luo <haoluo@google.com>
> ---

Love the simplicity of the test.

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  Changes since v2:
>   - Rebase
>
>  Changes since v1:
>   - Replaced CHECK() with ASSERT_ERR_PTR()
>   - Commented in the test for the reason of verification failure.
>
>  .../selftests/bpf/prog_tests/ksyms_btf.c      | 14 +++++++++
>  .../bpf/progs/test_ksyms_btf_write_check.c    | 29 +++++++++++++++++++
>  2 files changed, 43 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> index 79f6bd1e50d6..f6933b06daf8 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> @@ -8,6 +8,7 @@
>  #include "test_ksyms_btf_null_check.skel.h"
>  #include "test_ksyms_weak.skel.h"
>  #include "test_ksyms_weak.lskel.h"
> +#include "test_ksyms_btf_write_check.skel.h"
>
>  static int duration;
>
> @@ -137,6 +138,16 @@ static void test_weak_syms_lskel(void)
>         test_ksyms_weak_lskel__destroy(skel);
>  }
>
> +static void test_write_check(void)
> +{
> +       struct test_ksyms_btf_write_check *skel;
> +
> +       skel = test_ksyms_btf_write_check__open_and_load();
> +       ASSERT_ERR_PTR(skel, "unexpected load of a prog writing to ksym memory\n");
> +
> +       test_ksyms_btf_write_check__destroy(skel);
> +}
> +
>  void test_ksyms_btf(void)
>  {
>         int percpu_datasec;
> @@ -167,4 +178,7 @@ void test_ksyms_btf(void)
>
>         if (test__start_subtest("weak_ksyms_lskel"))
>                 test_weak_syms_lskel();
> +
> +       if (test__start_subtest("write_check"))
> +               test_write_check();
>  }
> diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c b/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
> new file mode 100644
> index 000000000000..2180c41cd890
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
> @@ -0,0 +1,29 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2021 Google */
> +
> +#include "vmlinux.h"
> +
> +#include <bpf/bpf_helpers.h>
> +
> +extern const int bpf_prog_active __ksym; /* int type global var. */
> +
> +SEC("raw_tp/sys_enter")
> +int handler(const void *ctx)
> +{
> +       int *active;
> +       __u32 cpu;
> +
> +       cpu = bpf_get_smp_processor_id();
> +       active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
> +       if (active) {
> +               /* Kernel memory obtained from bpf_{per,this}_cpu_ptr
> +                * is read-only, should _not_ pass verification.
> +                */
> +               /* WRITE_ONCE */
> +               *(volatile int *)active = -1;
> +       }
> +
> +       return 0;
> +}
> +
> +char _license[] SEC("license") = "GPL";
> --
> 2.34.0.rc0.344.g81b53c2807-goog
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
index 79f6bd1e50d6..f6933b06daf8 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
@@ -8,6 +8,7 @@ 
 #include "test_ksyms_btf_null_check.skel.h"
 #include "test_ksyms_weak.skel.h"
 #include "test_ksyms_weak.lskel.h"
+#include "test_ksyms_btf_write_check.skel.h"
 
 static int duration;
 
@@ -137,6 +138,16 @@  static void test_weak_syms_lskel(void)
 	test_ksyms_weak_lskel__destroy(skel);
 }
 
+static void test_write_check(void)
+{
+	struct test_ksyms_btf_write_check *skel;
+
+	skel = test_ksyms_btf_write_check__open_and_load();
+	ASSERT_ERR_PTR(skel, "unexpected load of a prog writing to ksym memory\n");
+
+	test_ksyms_btf_write_check__destroy(skel);
+}
+
 void test_ksyms_btf(void)
 {
 	int percpu_datasec;
@@ -167,4 +178,7 @@  void test_ksyms_btf(void)
 
 	if (test__start_subtest("weak_ksyms_lskel"))
 		test_weak_syms_lskel();
+
+	if (test__start_subtest("write_check"))
+		test_write_check();
 }
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c b/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
new file mode 100644
index 000000000000..2180c41cd890
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
@@ -0,0 +1,29 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Google */
+
+#include "vmlinux.h"
+
+#include <bpf/bpf_helpers.h>
+
+extern const int bpf_prog_active __ksym; /* int type global var. */
+
+SEC("raw_tp/sys_enter")
+int handler(const void *ctx)
+{
+	int *active;
+	__u32 cpu;
+
+	cpu = bpf_get_smp_processor_id();
+	active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
+	if (active) {
+		/* Kernel memory obtained from bpf_{per,this}_cpu_ptr
+		 * is read-only, should _not_ pass verification.
+		 */
+		/* WRITE_ONCE */
+		*(volatile int *)active = -1;
+	}
+
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";