diff mbox series

[bpf-next] selftests/bpf: test narrow load from bpf_sysctl.write

Message ID 20191029143027.28681-1-iii@linux.ibm.com
State Accepted
Delegated to: BPF Maintainers
Headers show
Series [bpf-next] selftests/bpf: test narrow load from bpf_sysctl.write | expand

Commit Message

Ilya Leoshkevich Oct. 29, 2019, 2:30 p.m. UTC
There are tests for full and narrows loads from bpf_sysctl.file_pos, but
for bpf_sysctl.write only full load is tested. Add the missing test.

Suggested-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 tools/testing/selftests/bpf/test_sysctl.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Comments

Andrey Ignatov Oct. 29, 2019, 3:24 p.m. UTC | #1
Ilya Leoshkevich <iii@linux.ibm.com> [Tue, 2019-10-29 07:31 -0700]:
> There are tests for full and narrows loads from bpf_sysctl.file_pos, but
> for bpf_sysctl.write only full load is tested. Add the missing test.
> 
> Suggested-by: Andrey Ignatov <rdna@fb.com>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>

Thank you!

Acked-by: Andrey Ignatov <rdna@fb.com>

> ---
>  tools/testing/selftests/bpf/test_sysctl.c | 23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/test_sysctl.c b/tools/testing/selftests/bpf/test_sysctl.c
> index a320e3844b17..7aff907003d3 100644
> --- a/tools/testing/selftests/bpf/test_sysctl.c
> +++ b/tools/testing/selftests/bpf/test_sysctl.c
> @@ -120,6 +120,29 @@ static struct sysctl_test tests[] = {
>  		.newval = "(none)", /* same as default, should fail anyway */
>  		.result = OP_EPERM,
>  	},
> +	{
> +		.descr = "ctx:write sysctl:write read ok narrow",
> +		.insns = {
> +			/* u64 w = (u16)write & 1; */
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> +			BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_1,
> +				    offsetof(struct bpf_sysctl, write)),
> +#else
> +			BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_1,
> +				    offsetof(struct bpf_sysctl, write) + 2),
> +#endif
> +			BPF_ALU64_IMM(BPF_AND, BPF_REG_7, 1),
> +			/* return 1 - w; */
> +			BPF_MOV64_IMM(BPF_REG_0, 1),
> +			BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_7),
> +			BPF_EXIT_INSN(),
> +		},
> +		.attach_type = BPF_CGROUP_SYSCTL,
> +		.sysctl = "kernel/domainname",
> +		.open_flags = O_WRONLY,
> +		.newval = "(none)", /* same as default, should fail anyway */
> +		.result = OP_EPERM,
> +	},
>  	{
>  		.descr = "ctx:write sysctl:read write reject",
>  		.insns = {
> -- 
> 2.23.0
>
Daniel Borkmann Oct. 30, 2019, 3:54 p.m. UTC | #2
On Tue, Oct 29, 2019 at 03:30:27PM +0100, Ilya Leoshkevich wrote:
> There are tests for full and narrows loads from bpf_sysctl.file_pos, but
> for bpf_sysctl.write only full load is tested. Add the missing test.
> 
> Suggested-by: Andrey Ignatov <rdna@fb.com>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>

Applied, thanks!
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/test_sysctl.c b/tools/testing/selftests/bpf/test_sysctl.c
index a320e3844b17..7aff907003d3 100644
--- a/tools/testing/selftests/bpf/test_sysctl.c
+++ b/tools/testing/selftests/bpf/test_sysctl.c
@@ -120,6 +120,29 @@  static struct sysctl_test tests[] = {
 		.newval = "(none)", /* same as default, should fail anyway */
 		.result = OP_EPERM,
 	},
+	{
+		.descr = "ctx:write sysctl:write read ok narrow",
+		.insns = {
+			/* u64 w = (u16)write & 1; */
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+			BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_1,
+				    offsetof(struct bpf_sysctl, write)),
+#else
+			BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_1,
+				    offsetof(struct bpf_sysctl, write) + 2),
+#endif
+			BPF_ALU64_IMM(BPF_AND, BPF_REG_7, 1),
+			/* return 1 - w; */
+			BPF_MOV64_IMM(BPF_REG_0, 1),
+			BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_7),
+			BPF_EXIT_INSN(),
+		},
+		.attach_type = BPF_CGROUP_SYSCTL,
+		.sysctl = "kernel/domainname",
+		.open_flags = O_WRONLY,
+		.newval = "(none)", /* same as default, should fail anyway */
+		.result = OP_EPERM,
+	},
 	{
 		.descr = "ctx:write sysctl:read write reject",
 		.insns = {