History

bpf: Introduce bpf_sysctl_{get,set}_new_value helpers

Add helpers to work with new value being written to sysctl by user space. bpf_sysctl_get_new_value() copies value being written to sysctl into provided buffer. bpf_sysctl_set_new_value() overrides new value being written by user space with a one from provided buffer. Buffer should contain string representation of the value, similar to what can be seen in /proc/sys/. Both helpers can be used only on sysctl write. File position matters and can be managed by an interface that will be introduced separately. E.g. if user space calls sys_write to a file in /proc/sys/ at file position = X, where X > 0, then the value set by bpf_sysctl_set_new_value() will be written starting from X. If program wants to override whole value with specified buffer, file position has to be set to zero. Documentation for the new helpers is provided in bpf.h UAPI.

Andrey Ignatov committed 7 years ago

Commit 4e63acd1 parent 1d11b305 files · +142 −10Browse files

Showing the changes to include/uapi/linux (1 of 5 files). Show all

Unified · Split

include/uapi/linux/bpf.h+37−1
@@ -2541,6 +2541,40 @@ union bpf_attr { · 2540 unmodified lines
2541 *2541 *
2542 * **-EINVAL** if current value was unavailable, e.g. because2542 * **-EINVAL** if current value was unavailable, e.g. because
2543 * sysctl is uninitialized and read returns -EIO for it.2543 * sysctl is uninitialized and read returns -EIO for it.
2544 *
2545 * int bpf_sysctl_get_new_value(struct bpf_sysctl *ctx, char *buf, size_t buf_len)
2546 * Description
2547 * Get new value being written by user space to sysctl (before
2548 * the actual write happens) and copy it as a string into
2549 * provided by program buffer *buf* of size *buf_len*.
2550 *
2551 * User space may write new value at file position > 0.
2552 *
2553 * The buffer is always NUL terminated, unless it's zero-sized.
2554 * Return
2555 * Number of character copied (not including the trailing NUL).
2556 *
2557 * **-E2BIG** if the buffer wasn't big enough (*buf* will contain
2558 * truncated name in this case).
2559 *
2560 * **-EINVAL** if sysctl is being read.
2561 *
2562 * int bpf_sysctl_set_new_value(struct bpf_sysctl *ctx, const char *buf, size_t buf_len)
2563 * Description
2564 * Override new value being written by user space to sysctl with
2565 * value provided by program in buffer *buf* of size *buf_len*.
2566 *
2567 * *buf* should contain a string in same form as provided by user
2568 * space on sysctl write.
2569 *
2570 * User space may write new value at file position > 0. To override
2571 * the whole sysctl value file position should be set to zero.
2572 * Return
2573 * 0 on success.
2574 *
2575 * **-E2BIG** if the *buf_len* is too big.
2576 *
2577 * **-EINVAL** if sysctl is being read.
2544 */2578 */
2545#define __BPF_FUNC_MAPPER(FN) \2579#define __BPF_FUNC_MAPPER(FN) \
2546 FN(unspec), \2580 FN(unspec), \
@@ -2645,7 +2679,9 @@ union bpf_attr { · 98 unmodified lines
2645 FN(skc_lookup_tcp), \2679 FN(skc_lookup_tcp), \
2646 FN(tcp_check_syncookie), \2680 FN(tcp_check_syncookie), \
2647 FN(sysctl_get_name), \2681 FN(sysctl_get_name), \
2648 FN(sysctl_get_current_value),2682 FN(sysctl_get_current_value), \
2683 FN(sysctl_get_new_value), \
2684 FN(sysctl_set_new_value),
2649 2685
2650/* integer value in 'imm' field of BPF_CALL instruction selects which helper2686/* integer value in 'imm' field of BPF_CALL instruction selects which helper
2651 * function eBPF program intends to call2687 * function eBPF program intends to call