linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/3] Modify some code in sysctl_net_core.c
@ 2022-05-09  6:57 Tiezhu Yang
  2022-05-09  6:57 ` [PATCH bpf-next 1/3] net: sysctl: Use SYSCTL_TWO instead of &two Tiezhu Yang
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-05-09  6:57 UTC (permalink / raw)
  To: davem, Jakub Kicinski, Paolo Abeni, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko
  Cc: netdev, bpf, linux-kernel

Tiezhu Yang (3):
  net: sysctl: Use SYSCTL_TWO instead of &two
  net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_*
  bpf: Print some info if disable bpf_jit_enable failed

 net/core/sysctl_net_core.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

-- 
2.1.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 1/3] net: sysctl: Use SYSCTL_TWO instead of &two
  2022-05-09  6:57 [PATCH bpf-next 0/3] Modify some code in sysctl_net_core.c Tiezhu Yang
@ 2022-05-09  6:57 ` Tiezhu Yang
  2022-05-09  6:57 ` [PATCH bpf-next 2/3] net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_* Tiezhu Yang
  2022-05-09  6:57 ` [PATCH bpf-next 3/3] bpf: Print some info if disable bpf_jit_enable failed Tiezhu Yang
  2 siblings, 0 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-05-09  6:57 UTC (permalink / raw)
  To: davem, Jakub Kicinski, Paolo Abeni, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko
  Cc: netdev, bpf, linux-kernel

It is better to use SYSCTL_TWO instead of &two, and then we can
remove the variable "two" in net/core/sysctl_net_core.c.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 net/core/sysctl_net_core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
index 8295e58..cf00dd7 100644
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -25,7 +25,6 @@
 
 #include "dev.h"
 
-static int two = 2;
 static int three = 3;
 static int int_3600 = 3600;
 static int min_sndbuf = SOCK_MIN_SNDBUF;
@@ -390,7 +389,7 @@ static struct ctl_table net_core_table[] = {
 		.extra2		= SYSCTL_ONE,
 # else
 		.extra1		= SYSCTL_ZERO,
-		.extra2		= &two,
+		.extra2		= SYSCTL_TWO,
 # endif
 	},
 # ifdef CONFIG_HAVE_EBPF_JIT
@@ -401,7 +400,7 @@ static struct ctl_table net_core_table[] = {
 		.mode		= 0600,
 		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
 		.extra1		= SYSCTL_ZERO,
-		.extra2		= &two,
+		.extra2		= SYSCTL_TWO,
 	},
 	{
 		.procname	= "bpf_jit_kallsyms",
@@ -546,7 +545,7 @@ static struct ctl_table net_core_table[] = {
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= SYSCTL_ZERO,
-		.extra2		= &two,
+		.extra2		= SYSCTL_TWO,
 	},
 	{
 		.procname	= "devconf_inherit_init_net",
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 2/3] net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_*
  2022-05-09  6:57 [PATCH bpf-next 0/3] Modify some code in sysctl_net_core.c Tiezhu Yang
  2022-05-09  6:57 ` [PATCH bpf-next 1/3] net: sysctl: Use SYSCTL_TWO instead of &two Tiezhu Yang
@ 2022-05-09  6:57 ` Tiezhu Yang
  2022-05-09 15:02   ` Daniel Borkmann
  2022-05-09  6:57 ` [PATCH bpf-next 3/3] bpf: Print some info if disable bpf_jit_enable failed Tiezhu Yang
  2 siblings, 1 reply; 6+ messages in thread
From: Tiezhu Yang @ 2022-05-09  6:57 UTC (permalink / raw)
  To: davem, Jakub Kicinski, Paolo Abeni, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko
  Cc: netdev, bpf, linux-kernel

The mode of the following procnames are defined as 0644, 0600, 0600
and 0600 respectively in net_core_table[], normal user can not write
them, so no need to check CAP_SYS_ADMIN in the related proc_handler
function, just remove the checks.

/proc/sys/net/core/bpf_jit_enable
/proc/sys/net/core/bpf_jit_harden
/proc/sys/net/core/bpf_jit_kallsyms
/proc/sys/net/core/bpf_jit_limit

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 net/core/sysctl_net_core.c | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
index cf00dd7..059352b 100644
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -268,9 +268,6 @@ static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
 	int ret, jit_enable = *(int *)table->data;
 	struct ctl_table tmp = *table;
 
-	if (write && !capable(CAP_SYS_ADMIN))
-		return -EPERM;
-
 	tmp.data = &jit_enable;
 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
 	if (write && !ret) {
@@ -291,9 +288,6 @@ static int
 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
 				    void *buffer, size_t *lenp, loff_t *ppos)
 {
-	if (!capable(CAP_SYS_ADMIN))
-		return -EPERM;
-
 	return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
 }
 # endif /* CONFIG_HAVE_EBPF_JIT */
@@ -302,9 +296,6 @@ static int
 proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
 				     void *buffer, size_t *lenp, loff_t *ppos)
 {
-	if (!capable(CAP_SYS_ADMIN))
-		return -EPERM;
-
 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
 }
 #endif
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 3/3] bpf: Print some info if disable bpf_jit_enable failed
  2022-05-09  6:57 [PATCH bpf-next 0/3] Modify some code in sysctl_net_core.c Tiezhu Yang
  2022-05-09  6:57 ` [PATCH bpf-next 1/3] net: sysctl: Use SYSCTL_TWO instead of &two Tiezhu Yang
  2022-05-09  6:57 ` [PATCH bpf-next 2/3] net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_* Tiezhu Yang
@ 2022-05-09  6:57 ` Tiezhu Yang
  2 siblings, 0 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-05-09  6:57 UTC (permalink / raw)
  To: davem, Jakub Kicinski, Paolo Abeni, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko
  Cc: netdev, bpf, linux-kernel

A user told me that bpf_jit_enable can be disabled on one system, but he
failed to disable bpf_jit_enable on the other system:

  # echo 0 > /proc/sys/net/core/bpf_jit_enable
  bash: echo: write error: Invalid argument

No useful info is available through the dmesg log, a quick analysis shows
that the issue is related with CONFIG_BPF_JIT_ALWAYS_ON.

When CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set
to 1 and setting any other value than that will return failure.

It is better to print some info to tell the user if disable bpf_jit_enable
failed.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 net/core/sysctl_net_core.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
index 059352b..f8a1d450 100644
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -266,6 +266,8 @@ static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
 					   loff_t *ppos)
 {
 	int ret, jit_enable = *(int *)table->data;
+	int min = *(int *)table->extra1;
+	int max = *(int *)table->extra2;
 	struct ctl_table tmp = *table;
 
 	tmp.data = &jit_enable;
@@ -280,6 +282,10 @@ static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
 			ret = -EPERM;
 		}
 	}
+
+	if (write && ret && min == max)
+		pr_info("CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1.\n");
+
 	return ret;
 }
 
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 2/3] net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_*
  2022-05-09  6:57 ` [PATCH bpf-next 2/3] net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_* Tiezhu Yang
@ 2022-05-09 15:02   ` Daniel Borkmann
  2022-05-10  2:42     ` Tiezhu Yang
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Borkmann @ 2022-05-09 15:02 UTC (permalink / raw)
  To: Tiezhu Yang, davem, Jakub Kicinski, Paolo Abeni,
	Alexei Starovoitov, Andrii Nakryiko
  Cc: netdev, bpf, linux-kernel

On 5/9/22 8:57 AM, Tiezhu Yang wrote:
> The mode of the following procnames are defined as 0644, 0600, 0600
> and 0600 respectively in net_core_table[], normal user can not write
> them, so no need to check CAP_SYS_ADMIN in the related proc_handler
> function, just remove the checks.
> 
> /proc/sys/net/core/bpf_jit_enable
> /proc/sys/net/core/bpf_jit_harden
> /proc/sys/net/core/bpf_jit_kallsyms
> /proc/sys/net/core/bpf_jit_limit
> 
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>

I don't think we can make this assumption - there are various other (non-BPF)
sysctl handlers in the tree doing similar check to prevent from userns' based
CAP_SYS_ADMIN.

> ---
>   net/core/sysctl_net_core.c | 9 ---------
>   1 file changed, 9 deletions(-)
> 
> diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
> index cf00dd7..059352b 100644
> --- a/net/core/sysctl_net_core.c
> +++ b/net/core/sysctl_net_core.c
> @@ -268,9 +268,6 @@ static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
>   	int ret, jit_enable = *(int *)table->data;
>   	struct ctl_table tmp = *table;
>   
> -	if (write && !capable(CAP_SYS_ADMIN))
> -		return -EPERM;
> -
>   	tmp.data = &jit_enable;
>   	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
>   	if (write && !ret) {
> @@ -291,9 +288,6 @@ static int
>   proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
>   				    void *buffer, size_t *lenp, loff_t *ppos)
>   {
> -	if (!capable(CAP_SYS_ADMIN))
> -		return -EPERM;
> -
>   	return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
>   }
>   # endif /* CONFIG_HAVE_EBPF_JIT */
> @@ -302,9 +296,6 @@ static int
>   proc_dolongvec_minmax_bpf_restricted(struct ctl_table *table, int write,
>   				     void *buffer, size_t *lenp, loff_t *ppos)
>   {
> -	if (!capable(CAP_SYS_ADMIN))
> -		return -EPERM;
> -
>   	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
>   }
>   #endif
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 2/3] net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_*
  2022-05-09 15:02   ` Daniel Borkmann
@ 2022-05-10  2:42     ` Tiezhu Yang
  0 siblings, 0 replies; 6+ messages in thread
From: Tiezhu Yang @ 2022-05-10  2:42 UTC (permalink / raw)
  To: Daniel Borkmann, davem, Jakub Kicinski, Paolo Abeni,
	Alexei Starovoitov, Andrii Nakryiko
  Cc: netdev, bpf, linux-kernel



On 05/09/2022 11:02 PM, Daniel Borkmann wrote:
> On 5/9/22 8:57 AM, Tiezhu Yang wrote:
>> The mode of the following procnames are defined as 0644, 0600, 0600
>> and 0600 respectively in net_core_table[], normal user can not write
>> them, so no need to check CAP_SYS_ADMIN in the related proc_handler
>> function, just remove the checks.
>>
>> /proc/sys/net/core/bpf_jit_enable
>> /proc/sys/net/core/bpf_jit_harden
>> /proc/sys/net/core/bpf_jit_kallsyms
>> /proc/sys/net/core/bpf_jit_limit
>>
>> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
>
> I don't think we can make this assumption - there are various other
> (non-BPF)
> sysctl handlers in the tree doing similar check to prevent from userns'
> based
> CAP_SYS_ADMIN.
>

OK, thank you for your reply, let me drop this patch now,
I will send v2 (patch #1 and #3) later.

Thanks,
Tiezhu


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-05-10  2:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-09  6:57 [PATCH bpf-next 0/3] Modify some code in sysctl_net_core.c Tiezhu Yang
2022-05-09  6:57 ` [PATCH bpf-next 1/3] net: sysctl: Use SYSCTL_TWO instead of &two Tiezhu Yang
2022-05-09  6:57 ` [PATCH bpf-next 2/3] net: sysctl: No need to check CAP_SYS_ADMIN for bpf_jit_* Tiezhu Yang
2022-05-09 15:02   ` Daniel Borkmann
2022-05-10  2:42     ` Tiezhu Yang
2022-05-09  6:57 ` [PATCH bpf-next 3/3] bpf: Print some info if disable bpf_jit_enable failed Tiezhu Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).