public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/1] Fix for bpf_sysctl_set_new_value
@ 2024-05-04 10:23 Raman Shukhau
  2024-05-04 10:23 ` [PATCH bpf-next 1/1] " Raman Shukhau
  0 siblings, 1 reply; 4+ messages in thread
From: Raman Shukhau @ 2024-05-04 10:23 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel; +Cc: Raman Shukhau

Noticed that call to bpf_sysctl_set_new_value doesn't change final value
of the parameter, when called from cgroup/syscall bpf handler. No error 
thrown in this case, new value is simply ignored and original value, sent
to sysctl, is set. Example (see test added to this change for BPF handler
logic):

sysctl -w net.ipv4.ip_local_reserved_ports = 11111
... cgroup/syscal handler call bpf_sysctl_set_new_value	and set 22222
sysctl net.ipv4.ip_local_reserved_ports
... returns 11111

On investigation I found 2 things that needs to be changed:
* return value check
* new_len provided by bpf back to sysctl. proc_sys_call_handler	expects
  this value NOT to include \0 symbol, e.g. if user do

	```
  open("/proc/sys/net/ipv4/ip_local_reserved_ports", ...)
  write(fd, "11111", sizeof("22222"))
  ```

  or `echo -n "11111" > /proc/sys/net/ipv4/ip_local_reserved_ports`

  or `sysctl -w	net.ipv4.ip_local_reserved_ports=11111

  proc_sys_call_handler receives count equal to `5`. To make it consistent
  with bpf_sysctl_set_new_value, this change also adjust `new_len` with
  `-1`, if `\0` passed as last character. Alternatively, using
  `sizeof("11111") - 1` in BPF handler should work, but it might not be
  obvious and spark confusion. Note: if incorrect count is used, sysctl
  returns EINVAL to the user.

Raman Shukhau (1):
  Fix for bpf_sysctl_set_new_value

 kernel/bpf/cgroup.c                           |  7 ++-
 .../bpf/progs/test_sysctl_overwrite.c         | 47 +++++++++++++++++++
 tools/testing/selftests/bpf/test_sysctl.c     | 35 +++++++++++++-
 3 files changed, 85 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_sysctl_overwrite.c

-- 
2.43.0


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

end of thread, other threads:[~2024-05-16 21:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-04 10:23 [PATCH bpf-next 0/1] Fix for bpf_sysctl_set_new_value Raman Shukhau
2024-05-04 10:23 ` [PATCH bpf-next 1/1] " Raman Shukhau
2024-05-07 23:20   ` Martin KaFai Lau
2024-05-16 21:16     ` Raman Shukhau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox