bpf: allow cgroup progs to export custom retval to userspace
Subsystems (5)
v3 cover bpf: allow cgroup progs to export custom retval to userspace
Right now, most cgroup hooks are best used for permission checks. They can only reject a syscall with -EPERM, so a cause of a rejection, if the rejected by eBPF cgroup hooks, is ambiguous to userspace. Additionally, if the syscalls are implemented in eBPF, all permission checks and the implementation has to happen within the same filter, as programs executed later in the series of progs are unaware of the return values return by the previous progs.
This patch series adds two helpers, bpf_get_retval and bpf_set_retval, that allows hooks to get/set the return value of syscall to userspace. This also allows later progs to retrieve retval set by previous progs.
For legacy programs that rejects a syscall without setting the retval, for backwards compatibility, if a prog rejects without itself or a prior prog setting retval to an -err, the retval is set by the kernel to -EPERM.
For getsockopt hooks that has ctx->retval, this variable mirrors that that accessed by the helpers.
Additionally, the following user-visible behavior for getsockopt hooks has changed:
- If a prior filter rejected the syscall, it will be visible
in ctx->retval.
- Attempting to change the retval arbitrarily is now allowed and will not cause an -EFAULT.
- If kernel rejects a getsockopt syscall before running the hooks, the error will be visible in ctx->retval. Returning 0 from the prog will not overwrite the error to -EPERM unless there is an explicit call of bpf_set_retval(-EPERM)
Tests have been added in this series to test the behavior of the helper with cgroup setsockopt getsockopt hooks.
Patch 1 changes the API of macros to prepare for the next patch and
should be a no-op.
Patch 2 moves ctx->retval to a struct pointed to by current
task_struct.
Patch 3 implements the helpers.
Patch 4 tests the behaviors of the helpers.
v1 -> v2:
- errno -> retval
- split one helper to get & set helpers
- allow retval to be set arbitrarily in the general case
- made the helper retval and context retval mirror each other
v2 -> v3:
- squashed sockopt_sk test change to the patch that broke it
YiFei Zhu (4):
bpf: Make BPF_PROG_RUN_ARRAY return -err instead of allow boolean
bpf: Move getsockopt retval to struct bpf_cg_run_ctx
bpf: Add cgroup helpers bpf_{get,set}_retval to get/set syscall return
value
selftests/bpf: Test bpf_{get,set}_retval behavior with cgroup/sockopt
include/linux/bpf.h | 34 +- include/linux/filter.h | 5 +- include/uapi/linux/bpf.h | 18 + kernel/bpf/cgroup.c | 149 ++++-- security/device_cgroup.c | 2 +- tools/include/uapi/linux/bpf.h | 18 + .../bpf/prog_tests/cgroup_getset_retval.c | 481 ++++++++++++++++++ .../selftests/bpf/prog_tests/sockopt_sk.c | 2 +- .../progs/cgroup_getset_retval_getsockopt.c | 45 ++ .../progs/cgroup_getset_retval_setsockopt.c | 52 ++ .../testing/selftests/bpf/progs/sockopt_sk.c | 32 +- 11 files changed, 750 insertions(+), 88 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_getset_retval.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_getset_retval_getsockopt.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_getset_retval_setsockopt.c
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
PATCH v3 1/4 bpf: Make BPF_PROG_RUN_ARRAY return -err instead of allow boolean
Right now BPF_PROG_RUN_ARRAY and related macros return 1 or 0 for whether the prog array allows or rejects whatever is being hooked. The caller of these macros then return -EPERM or continue processing based on thw macro's return value. Unforunately this is inflexible, since -EPERM is the only err that can be returned.
This patch should be a no-op; it prepares for the next patch. The returning of the -EPERM is moved to inside the macros, so the outer functions are directly returning what the macros returned if they are non-zero.
--- include/linux/bpf.h | 16 +++++++++------- kernel/bpf/cgroup.c | 41 +++++++++++++++------------------------- security/device_cgroup.c | 2 +- 3 files changed, 25 insertions(+), 34 deletions(-)
include/linux/bpf.h +9 -7
@@ -1278,7 +1278,7 @@ static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)typedef u32 (*bpf_prog_run_fn )(const struct bpf_prog *prog,const void *ctx); -static __always_inline u32 +static __always_inline intBPF_PROG_RUN_ARRAY_CG_FLAGS (const struct bpf_prog_array __rcu *array_rcu,const void *ctx,bpf_prog_run_fn run_prog,u32 *ret_flags)
@@ -1288,7 +1288,7 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,const struct bpf_prog_array *array;struct bpf_run_ctx *old_run_ctx;struct bpf_cg_run_ctx run_ctx; -u32 ret =1 ; +int ret =0 ;u32 func_ret;migrate_disable ();
@@ -1299,7 +1299,8 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; func_ret =run_prog (prog, ctx); - ret &= (func_ret &1 ); +if (!(func_ret &1 )) + ret = -EPERM ; *(ret_flags) |= (func_ret >>1 ); item++; }
@@ -1309,7 +1310,7 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,return ret; } -static __always_inline u32 +static __always_inline intBPF_PROG_RUN_ARRAY_CG (const struct bpf_prog_array __rcu *array_rcu,const void *ctx,bpf_prog_run_fn run_prog) {
@@ -1318,7 +1319,7 @@ BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu,const struct bpf_prog_array *array;struct bpf_run_ctx *old_run_ctx;struct bpf_cg_run_ctx run_ctx; -u32 ret =1 ; +int ret =0 ;migrate_disable ();rcu_read_lock ();
@@ -1327,7 +1328,8 @@ BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu, old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx);while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; - ret &=run_prog (prog, ctx); +if (!run_prog (prog, ctx)) + ret = -EPERM ; item++; }bpf_reset_run_ctx (old_run_ctx);
@@ -1395,7 +1397,7 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,u32 _ret; \ _ret =BPF_PROG_RUN_ARRAY_CG_FLAGS (array, ctx, func, &_flags); \ _cn = _flags &BPF_RET_SET_CN ; \ -if (_ret) \ +if (!_ret) \ _ret = (_cn ?NET_XMIT_CN :NET_XMIT_SUCCESS ); \else \ _ret = (_cn ?NET_XMIT_DROP : -EPERM ); \
kernel/bpf/cgroup.c +15 -26
110 lines not on this page · raw message
security/device_cgroup.c +1 -1
@@ -838,7 +838,7 @@ int devcgroup_check_permission(short type, u32 major, u32 minor, short access)int rc =BPF_CGROUP_RUN_PROG_DEVICE_CGROUP (type, major, minor, access);if (rc) -return -EPERM ; +return rc;#ifdef CONFIG_CGROUP_DEVICE return devcgroup_legacy_check_permission (type, major, minor, access);
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
PATCH v3 2/4 bpf: Move getsockopt retval to struct bpf_cg_run_ctx
In reply to YiFei ZhuThe retval value is moved to struct bpf_cg_run_ctx for ease of access in different prog types with different context structs layouts. The helper implementation (to be added in a later patch in the series) can simply perform a container_of from current->bpf_ctx to retrieve bpf_cg_run_ctx.
Unfortunately, there is no easy way to access the current task_struct via the verifier BPF bytecode rewrite, aside from possibly calling a helper, so a pointer to current task is added to struct bpf_sockopt_kern so that the rewritten BPF bytecode can access struct bpf_cg_run_ctx with an indirection.
For backward compatibility, if a getsockopt program rejects a syscall by returning 0, an -EPERM will be generated, by having the BPF_PROG_RUN_ARRAY_CG family macros automatically set the retval to -EPERM. Unlike prior to this patch, this -EPERM will be visible to ctx->retval for any other hooks down the line in the prog array.
Additionally, the restriction that getsockopt filters can only set the retval to 0 is removed, considering that certain getsockopt implementations may return optlen. Filters are now able to set the value arbitrarily.
--- include/linux/bpf.h | 20 ++++++----- include/linux/filter.h | 5 ++- kernel/bpf/cgroup.c | 82 ++++++++++++++++++++++++------------------ 3 files changed, 63 insertions(+), 44 deletions(-)
include/linux/bpf.h +11 -9
@@ -1246,6 +1246,7 @@ struct bpf_run_ctx {};struct bpf_cg_run_ctx {struct bpf_run_ctx run_ctx;const struct bpf_prog_array_item *prog_item; +int retval; };struct bpf_trace_run_ctx {
@@ -1281,16 +1282,16 @@ typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx); static __always_inline intBPF_PROG_RUN_ARRAY_CG_FLAGS (const struct bpf_prog_array __rcu *array_rcu,const void *ctx,bpf_prog_run_fn run_prog, -u32 *ret_flags) +int retval,u32 *ret_flags) {const struct bpf_prog_array_item *item;const struct bpf_prog *prog;const struct bpf_prog_array *array;struct bpf_run_ctx *old_run_ctx;struct bpf_cg_run_ctx run_ctx; -int ret =0 ;u32 func_ret; + run_ctx.retval = retval;migrate_disable ();rcu_read_lock (); array =rcu_dereference (array_rcu);
@@ -1300,27 +1301,28 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu, run_ctx.prog_item = item; func_ret =run_prog (prog, ctx);if (!(func_ret &1 )) - ret = -EPERM ; + run_ctx.retval = -EPERM ; *(ret_flags) |= (func_ret >>1 ); item++; }bpf_reset_run_ctx (old_run_ctx);rcu_read_unlock ();migrate_enable (); -return ret; +return run_ctx.retval; } static __always_inline intBPF_PROG_RUN_ARRAY_CG (const struct bpf_prog_array __rcu *array_rcu, -const void *ctx,bpf_prog_run_fn run_prog) +const void *ctx,bpf_prog_run_fn run_prog, +int retval) {const struct bpf_prog_array_item *item;const struct bpf_prog *prog;const struct bpf_prog_array *array;struct bpf_run_ctx *old_run_ctx;struct bpf_cg_run_ctx run_ctx; -int ret =0 ; + run_ctx.retval = retval;migrate_disable ();rcu_read_lock (); array =rcu_dereference (array_rcu);
@@ -1329,13 +1331,13 @@ BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu,while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item;if (!run_prog (prog, ctx)) - ret = -EPERM ; + run_ctx.retval = -EPERM ; item++; }bpf_reset_run_ctx (old_run_ctx);rcu_read_unlock ();migrate_enable (); -return ret; +return run_ctx.retval; }static __always_inline u32
@@ -1395,7 +1397,7 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,u32 _flags =0 ; \bool _cn; \u32 _ret; \ - _ret =BPF_PROG_RUN_ARRAY_CG_FLAGS (array, ctx, func, &_flags); \ + _ret =BPF_PROG_RUN_ARRAY_CG_FLAGS (array, ctx, func,0 , &_flags); \ _cn = _flags &BPF_RET_SET_CN ; \if (!_ret) \ _ret = (_cn ?NET_XMIT_CN :NET_XMIT_SUCCESS ); \
include/linux/filter.h +4 -1
@@ -1352,7 +1352,10 @@ struct bpf_sockopt_kern {s32 level;s32 optname;s32 optlen; -s32 retval; +/* for retval in struct bpf_cg_run_ctx */ +struct task_struct *current_task; +/* Temporary "register" for indirect stores to ppos. */ +u64 tmp_reg; };int copy_bpf_fprog_from_user (struct sock_fprog *dst,sockptr_t src,int len);
kernel/bpf/cgroup.c +48 -34
181 lines not on this page · raw message
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
PATCH v3 3/4 bpf: Add cgroup helpers bpf_{get,set}_retval to get/set syscall return value
In reply to YiFei ZhuThe helpers continue to use int for retval because all the hooks are int-returning rather than long-returning. The return value of bpf_set_retval is int for future-proofing, in case in the future there may be errors trying to set the retval.
After the previous patch, if a program rejects a syscall by returning 0, an -EPERM will be generated no matter if the retval is already set to -err. This patch change it being forced only if retval is not -err. This is because we want to support, for example, invoking bpf_set_retval(-EINVAL) and return 0, and have the syscall return value be -EINVAL not -EPERM.
This change is reflected in the sockopt_sk test which has been updated to assert the errno is EINVAL instead of the EPERM. The eBPF prog has to explicitly bpf_set_retval(-EPERM) if EPERM is wanted. I also removed the explicit mentions of EPERM in the comments in the prog.
For BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY, the prior behavior is that, if the return value is NET_XMIT_DROP, the packet is silently dropped. We preserve this behavior for backward compatibility reasons, so even if an errno is set, the errno does not return to caller. However, setting a non-err to retval cannot propagate so this is not allowed and we return a -EFAULT in that case.
--- include/linux/bpf.h | 10 +++-- include/uapi/linux/bpf.h | 18 +++++++++ kernel/bpf/cgroup.c | 38 ++++++++++++++++++- tools/include/uapi/linux/bpf.h | 18 +++++++++ .../selftests/bpf/prog_tests/sockopt_sk.c | 2 +- .../testing/selftests/bpf/progs/sockopt_sk.c | 32 ++++++++-------- 6 files changed, 96 insertions(+), 22 deletions(-)
include/linux/bpf.h +6 -4
@@ -1300,7 +1300,7 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; func_ret =run_prog (prog, ctx); -if (!(func_ret &1 )) +if (!(func_ret &1 ) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; *(ret_flags) |= (func_ret >>1 ); item++;
@@ -1330,7 +1330,7 @@ BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu, old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx);while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; -if (!run_prog (prog, ctx)) +if (!run_prog (prog, ctx) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; item++; }
@@ -1390,7 +1390,7 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,* 0: NET_XMIT_SUCCESS skb should be transmitted * 1: NET_XMIT_DROP skb should be dropped and cn * 2: NET_XMIT_CN skb should be transmitted and cn -* 3: -EPERM skb should be dropped +* 3: -err skb should be dropped */ #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY (array, ctx, func) \ ({ \
@@ -1399,10 +1399,12 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,u32 _ret; \ _ret =BPF_PROG_RUN_ARRAY_CG_FLAGS (array, ctx, func,0 , &_flags); \ _cn = _flags &BPF_RET_SET_CN ; \ +if (_ret && !IS_ERR_VALUE ((long )_ret)) \ + _ret = -EFAULT ; \if (!_ret) \ _ret = (_cn ?NET_XMIT_CN :NET_XMIT_SUCCESS ); \else \ - _ret = (_cn ?NET_XMIT_DROP : -EPERM ); \ + _ret = (_cn ?NET_XMIT_DROP : _ret); \ _ret; \ })
include/uapi/linux/bpf.h +18 -0
@@ -5018,6 +5018,22 @@ union bpf_attr {* * Return * The number of arguments of the traced function. +* +* int bpf_get_retval(void) +* Description +* Get the syscall's return value that will be returned to userspace. +* +* This helper is currently supported by cgroup programs only. +* Return +* The syscall's return value. +* +* int bpf_set_retval(int retval) +* Description +* Set the syscall's return value that will be returned to userspace. +* +* This helper is currently supported by cgroup programs only. +* Return +* 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5206,6 +5222,8 @@ union bpf_attr {FN (get_func_arg), \FN (get_func_ret), \FN (get_func_arg_cnt), \ +FN (get_retval), \ +FN (set_retval), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/cgroup.c +37 -1
@@ -1044,7 +1044,7 @@ int cgroup_bpf_prog_query(const union bpf_attr *attr,* NET_XMIT_DROP (1) - drop packet and notify TCP to call cwr * NET_XMIT_CN (2) - continue with packet output and notify TCP * to call cwr -* -EPERM - drop packet +* -err - drop packet * * For ingress packets, this function will return -EPERM if any * attached program was found and if it returned != 1 during execution.
@@ -1080,6 +1080,8 @@ int __cgroup_bpf_run_filter_skb(struct sock *sk, }else { ret =BPF_PROG_RUN_ARRAY_CG (cgrp->bpf.effective[atype], skb, __bpf_prog_run_save_cb,0 ); +if (ret && !IS_ERR_VALUE ((long )ret)) + ret = -EFAULT ; }bpf_restore_data_end (skb, saved_data_end);__skb_pull (skb, offset);
@@ -1205,6 +1207,36 @@ int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,return ret; } +BPF_CALL_0 (bpf_get_retval) +{ +struct bpf_cg_run_ctx *ctx = + container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); + +return ctx->retval; +} + +static const struct bpf_func_proto bpf_get_retval_proto = { + .func = bpf_get_retval, + .gpl_only = false, + .ret_type =RET_INTEGER , +}; + +BPF_CALL_1(bpf_set_retval, int, retval) +{ +struct bpf_cg_run_ctx *ctx = + container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); + + ctx->retval = retval; +return 0 ; +} + +static const struct bpf_func_proto bpf_set_retval_proto = { + .func = bpf_set_retval, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_ANYTHING , +}; +static const struct bpf_func_proto *cgroup_base_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {
@@ -1217,6 +1249,10 @@ cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_get_current_cgroup_id_proto;case BPF_FUNC_perf_event_output:return &bpf_event_output_data_proto; +case BPF_FUNC_get_retval: +return &bpf_get_retval_proto; +case BPF_FUNC_set_retval: +return &bpf_set_retval_proto;default :return bpf_base_func_proto (func_id); }
tools/include/uapi/linux/bpf.h +18 -0
@@ -5018,6 +5018,22 @@ union bpf_attr {* * Return * The number of arguments of the traced function. +* +* int bpf_get_retval(void) +* Description +* Get the syscall's return value that will be returned to userspace. +* +* This helper is currently supported by cgroup programs only. +* Return +* The syscall's return value. +* +* int bpf_set_retval(int retval) +* Description +* Set the syscall's return value that will be returned to userspace. +* +* This helper is currently supported by cgroup programs only. +* Return +* 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5206,6 +5222,8 @@ union bpf_attr {FN (get_func_arg), \FN (get_func_ret), \FN (get_func_arg_cnt), \ +FN (get_retval), \ +FN (set_retval), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +1 -1
@@ -177,7 +177,7 @@ static int getsetsockopt(void) optlen =sizeof (buf.zc); errno =0 ; err =getsockopt (fd,SOL_TCP ,TCP_ZEROCOPY_RECEIVE , &buf, &optlen); -if (errno !=EPERM ) { +if (errno !=EINVAL ) {log_err ("Unexpected getsockopt(TCP_ZEROCOPY_RECEIVE) err=%d errno=%d" , err, errno); goto err;
tools/testing/selftests/bpf/progs/sockopt_sk.c +16 -16
@@ -73,17 +73,17 @@ int _getsockopt(struct bpf_sockopt *ctx)*/ if (optval +sizeof (struct tcp_zerocopy_receive ) > optval_end) -return 0 ;/* EPERM, bounds check */ +return 0 ;/* bounds check */ if (((struct tcp_zerocopy_receive *)optval)->address !=0 ) -return 0 ;/* EPERM, unexpected data */ +return 0 ;/* unexpected data */ return 1 ; }if (ctx->level ==SOL_IP && ctx->optname ==IP_FREEBIND ) {if (optval +1 > optval_end) -return 0 ;/* EPERM, bounds check */ +return 0 ;/* bounds check */ ctx->retval =0 ;/* Reset system call return value to zero */
@@ -96,24 +96,24 @@ int _getsockopt(struct bpf_sockopt *ctx)* bytes of data. */ if (optval_end - optval != page_size) -return 0 ;/* EPERM, unexpected data size */ +return 0 ;/* unexpected data size */ return 1 ; }if (ctx->level !=SOL_CUSTOM ) -return 0 ;/* EPERM, deny everything except custom level */ +return 0 ;/* deny everything except custom level */ if (optval +1 > optval_end) -return 0 ;/* EPERM, bounds check */ +return 0 ;/* bounds check */ storage =bpf_sk_storage_get (&socket_storage_map, ctx->sk,0 ,BPF_SK_STORAGE_GET_F_CREATE );if (!storage) -return 0 ;/* EPERM, couldn't get sk storage */ +return 0 ;/* couldn't get sk storage */ if (!ctx->retval) -return 0 ;/* EPERM, kernel should not have handled +return 0 ;/* kernel should not have handled * SOL_CUSTOM, something is wrong! */ ctx->retval =0 ;/* Reset system call return value to zero */
@@ -152,7 +152,7 @@ int _setsockopt(struct bpf_sockopt *ctx)/* Overwrite SO_SNDBUF value */ if (optval +sizeof (__u32) > optval_end) -return 0 ;/* EPERM, bounds check */ +return 0 ;/* bounds check */ *(__u32 *)optval =0x55AA ; ctx->optlen =4 ;
@@ -164,7 +164,7 @@ int _setsockopt(struct bpf_sockopt *ctx)/* Always use cubic */ if (optval +5 > optval_end) -return 0 ;/* EPERM, bounds check */ +return 0 ;/* bounds check */ memcpy (optval,"cubic" ,5 ); ctx->optlen =5 ;
@@ -175,10 +175,10 @@ int _setsockopt(struct bpf_sockopt *ctx)if (ctx->level ==SOL_IP && ctx->optname ==IP_FREEBIND ) {/* Original optlen is larger than PAGE_SIZE. */ if (ctx->optlen != page_size *2 ) -return 0 ;/* EPERM, unexpected data size */ +return 0 ;/* unexpected data size */ if (optval +1 > optval_end) -return 0 ;/* EPERM, bounds check */ +return 0 ;/* bounds check */ /* Make sure we can trim the buffer. */ optval[0 ] =0 ;
@@ -189,21 +189,21 @@ int _setsockopt(struct bpf_sockopt *ctx)* bytes of data. */ if (optval_end - optval != page_size) -return 0 ;/* EPERM, unexpected data size */ +return 0 ;/* unexpected data size */ return 1 ; }if (ctx->level !=SOL_CUSTOM ) -return 0 ;/* EPERM, deny everything except custom level */ +return 0 ;/* deny everything except custom level */ if (optval +1 > optval_end) -return 0 ;/* EPERM, bounds check */ +return 0 ;/* bounds check */ storage =bpf_sk_storage_get (&socket_storage_map, ctx->sk,0 ,BPF_SK_STORAGE_GET_F_CREATE );if (!storage) -return 0 ;/* EPERM, couldn't get sk storage */ +return 0 ;/* couldn't get sk storage */ storage->val = optval[0 ]; ctx->optlen =-1 ;/* BPF has consumed this option, don't call kernel
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
Re: [PATCH v3 bpf-next 3/4] bpf: Add cgroup helpers bpf_{get,set}_retval to get/set syscall return value
Quoted 69 lines · YiFei Zhu
The helpers continue to use int for retval because all the hooks are int-returning rather than long-returning. The return value of
bpf_set_retvalis int for future-proofing, in case in the future there may be errors trying to set the retval.After the previous patch, if a program rejects a syscall by returning 0, an
-EPERMwill be generated no matter if the retval is already set to -err. This patch change it being forced only if retval is not -err. This is because we want to support, for example, invokingbpf_set_retval(-EINVAL)and return 0, and have the syscall return value be-EINVALnot-EPERM.This change is reflected in the
sockopt_sktest which has been updated to assert the errno is EINVAL instead of the EPERM. The eBPF prog has to explicitlybpf_set_retval(-EPERM)if EPERM is wanted. I also removed the explicit mentions of EPERM in the comments in the prog.For
BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY, the prior behavior is that, if the return value isNET_XMIT_DROP, the packet is silently dropped. We preserve this behavior for backward compatibility reasons, so even if an errno is set, the errno does not return to caller. However, setting a non-err to retval cannot propagate so this is not allowed and we return a-EFAULTin that case.Signed-off-by YiFei Zhu <[email protected]>Reviewed-by Stanislav Fomichev <[email protected]>--- include/linux/bpf.h | 10 +++-- include/uapi/linux/bpf.h | 18 +++++++++ kernel/bpf/cgroup.c | 38 ++++++++++++++++++- tools/include/uapi/linux/bpf.h | 18 +++++++++ .../selftests/bpf/prog_tests/sockopt_sk.c | 2 +- .../testing/selftests/bpf/progs/sockopt_sk.c | 32 ++++++++-------- 6 files changed, 96 insertions(+), 22 deletions(-)include/linux/bpf.h +3
-3@@ -1300,7 +1300,7 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; func_ret =run_prog (prog, ctx); -if (!(func_ret &1 )) +if (!(func_ret &1 ) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; *(ret_flags) |= (func_ret >>1 ); item++;@@ -1330,7 +1330,7 @@ BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu, old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx);while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; -if (!run_prog (prog, ctx)) +if (!run_prog (prog, ctx) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; item++; }@@ -1390,7 +1390,7 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,* 0: NET_XMIT_SUCCESS skb should be transmitted * 1: NET_XMIT_DROP skb should be dropped and cn * 2: NET_XMIT_CN skb should be transmitted and cn -* 3: -EPERM skb should be dropped +* 3: -err skb should be dropped */ #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY (array, ctx, func) \ ({ \
include/linux/bpf.h BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu, · line 1407
Quoted 10 lines
include/linux/bpf.h +3
-1@@ -1399,10 +1399,12 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,u32 _ret; \ _ret =BPF_PROG_RUN_ARRAY_CG_FLAGS (array, ctx, func,0 , &_flags); \ _cn = _flags &BPF_RET_SET_CN ; \ +if (_ret && !IS_ERR_VALUE ((long )_ret)) \ + _ret = -EFAULT ; \if (!_ret) \ _ret = (_cn ?NET_XMIT_CN :NET_XMIT_SUCCESS ); \else \ - _ret = (_cn ?NET_XMIT_DROP : -EPERM ); \ + _ret = (_cn ?NET_XMIT_DROP : _ret); \
Sorry for the long delay in reviewing.
Overall it looks very good.
Few questions:
Why change this behavior for BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY ? It's for an inet_egress hook only. In other words ip_output. What kind of different error codes do you want to pass to the stack from there?
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c static int getsetsockopt(void) · line 180
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +1
-1@@ -177,7 +177,7 @@ static int getsetsockopt(void) optlen =sizeof (buf.zc); errno =0 ; err =getsockopt (fd,SOL_TCP ,TCP_ZEROCOPY_RECEIVE , &buf, &optlen); -if (errno !=EPERM ) { +if (errno !=EINVAL ) {
Could you explain which part of this patch caused this change in user visible behavior?
I understand the desire to do bpf_set_retval(-EINVAL) and return 0, but progs/sockopt_sk.c is not doing it.
Where does EINVAL come from?
Re: [PATCH v3 bpf-next 3/4] bpf: Add cgroup helpers bpf_{get,set}_retval to get/set syscall return value
Quoted 86 lines · Alexei Starovoitov
Quoted 69 lines · YiFei Zhu
The helpers continue to use int for retval because all the hooks are int-returning rather than long-returning. The return value of
bpf_set_retvalis int for future-proofing, in case in the future there may be errors trying to set the retval.After the previous patch, if a program rejects a syscall by returning 0, an
-EPERMwill be generated no matter if the retval is already set to -err. This patch change it being forced only if retval is not -err. This is because we want to support, for example, invokingbpf_set_retval(-EINVAL)and return 0, and have the syscall return value be-EINVALnot-EPERM.This change is reflected in the
sockopt_sktest which has been updated to assert the errno is EINVAL instead of the EPERM. The eBPF prog has to explicitlybpf_set_retval(-EPERM)if EPERM is wanted. I also removed the explicit mentions of EPERM in the comments in the prog.For
BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY, the prior behavior is that, if the return value isNET_XMIT_DROP, the packet is silently dropped. We preserve this behavior for backward compatibility reasons, so even if an errno is set, the errno does not return to caller. However, setting a non-err to retval cannot propagate so this is not allowed and we return a-EFAULTin that case.Signed-off-by YiFei Zhu <[email protected]>Reviewed-by Stanislav Fomichev <[email protected]>--- include/linux/bpf.h | 10 +++-- include/uapi/linux/bpf.h | 18 +++++++++ kernel/bpf/cgroup.c | 38 ++++++++++++++++++- tools/include/uapi/linux/bpf.h | 18 +++++++++ .../selftests/bpf/prog_tests/sockopt_sk.c | 2 +- .../testing/selftests/bpf/progs/sockopt_sk.c | 32 ++++++++-------- 6 files changed, 96 insertions(+), 22 deletions(-)include/linux/bpf.h +3
-3@@ -1300,7 +1300,7 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; func_ret =run_prog (prog, ctx); -if (!(func_ret &1 )) +if (!(func_ret &1 ) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; *(ret_flags) |= (func_ret >>1 ); item++;@@ -1330,7 +1330,7 @@ BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu, old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx);while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; -if (!run_prog (prog, ctx)) +if (!run_prog (prog, ctx) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; item++; }@@ -1390,7 +1390,7 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,* 0: NET_XMIT_SUCCESS skb should be transmitted * 1: NET_XMIT_DROP skb should be dropped and cn * 2: NET_XMIT_CN skb should be transmitted and cn -* 3: -EPERM skb should be dropped +* 3: -err skb should be dropped */ #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY (array, ctx, func) \ ({ \include/linux/bpf.h BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu, · line 1407
Quoted 10 lines
include/linux/bpf.h +3
-1@@ -1399,10 +1399,12 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,u32 _ret; \ _ret =BPF_PROG_RUN_ARRAY_CG_FLAGS (array, ctx, func,0 , &_flags); \ _cn = _flags &BPF_RET_SET_CN ; \ +if (_ret && !IS_ERR_VALUE ((long )_ret)) \ + _ret = -EFAULT ; \if (!_ret) \ _ret = (_cn ?NET_XMIT_CN :NET_XMIT_SUCCESS ); \else \ - _ret = (_cn ?NET_XMIT_DROP : -EPERM ); \ + _ret = (_cn ?NET_XMIT_DROP : _ret); \Sorry for the long delay in reviewing.
Overall it looks very good.
Few questions:
Quoted 4 lines
Why change this behavior for
BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY? It's for aninet_egresshook only. In other wordsip_output. What kind of different error codes do you want to pass to the stack from there?
I don't really have a use case in mind for a different error code for an egress hook (my use cases are for sockopt hooks) at the moment, but it sounds to me that it would a surprising behavior if bpf_set_retval is provided yet it would still be -EPERM.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +0
-0@@ -177,7 +177,7 @@ static int getsetsockopt(void) optlen =sizeof (buf.zc); errno =0 ; err =getsockopt (fd,SOL_TCP ,TCP_ZEROCOPY_RECEIVE , &buf, &optlen);
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c static int getsetsockopt(void) · line 180
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +1
-1-if (errno !=EPERM ) { +if (errno !=EINVAL ) {Could you explain which part of this patch caused this change in user visible behavior?
I understand the desire to dobpf_set_retval(-EINVAL)and return 0, butprogs/sockopt_sk.cis not doing it.
Where does EINVAL come from?
This comes from the kernel. In for an anvalid address to the getsockopt handler in tcp_zerocopy_receive [1]. The original behavior prior to this series is that the eBPF getsockopt hook generating an -EPERM overrides that of the kernel's -EINVAL, but now when the eBPF hook returns 0, it sees that an -EINVAL is already set by the kernel and does not modify the error code.
[1] https://elixir.bootlin.com/linux/v5.16.1/source/net/ipv4/tcp.c#L2060
YiFei Zhu
Re: [PATCH v3 bpf-next 3/4] bpf: Add cgroup helpers bpf_{get,set}_retval to get/set syscall return value
Quoted 94 lines · YiFei Zhu
Quoted 86 lines · Alexei Starovoitov
Quoted 69 lines · YiFei Zhu
The helpers continue to use int for retval because all the hooks are int-returning rather than long-returning. The return value of
bpf_set_retvalis int for future-proofing, in case in the future there may be errors trying to set the retval.After the previous patch, if a program rejects a syscall by returning 0, an
-EPERMwill be generated no matter if the retval is already set to -err. This patch change it being forced only if retval is not -err. This is because we want to support, for example, invokingbpf_set_retval(-EINVAL)and return 0, and have the syscall return value be-EINVALnot-EPERM.This change is reflected in the
sockopt_sktest which has been updated to assert the errno is EINVAL instead of the EPERM. The eBPF prog has to explicitlybpf_set_retval(-EPERM)if EPERM is wanted. I also removed the explicit mentions of EPERM in the comments in the prog.For
BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY, the prior behavior is that, if the return value isNET_XMIT_DROP, the packet is silently dropped. We preserve this behavior for backward compatibility reasons, so even if an errno is set, the errno does not return to caller. However, setting a non-err to retval cannot propagate so this is not allowed and we return a-EFAULTin that case.Signed-off-by YiFei Zhu <[email protected]>Reviewed-by Stanislav Fomichev <[email protected]>--- include/linux/bpf.h | 10 +++-- include/uapi/linux/bpf.h | 18 +++++++++ kernel/bpf/cgroup.c | 38 ++++++++++++++++++- tools/include/uapi/linux/bpf.h | 18 +++++++++ .../selftests/bpf/prog_tests/sockopt_sk.c | 2 +- .../testing/selftests/bpf/progs/sockopt_sk.c | 32 ++++++++-------- 6 files changed, 96 insertions(+), 22 deletions(-)include/linux/bpf.h +3
-3@@ -1300,7 +1300,7 @@ BPF_PROG_RUN_ARRAY_CG_FLAGS(const struct bpf_prog_array __rcu *array_rcu,while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; func_ret =run_prog (prog, ctx); -if (!(func_ret &1 )) +if (!(func_ret &1 ) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; *(ret_flags) |= (func_ret >>1 ); item++;@@ -1330,7 +1330,7 @@ BPF_PROG_RUN_ARRAY_CG(const struct bpf_prog_array __rcu *array_rcu, old_run_ctx =bpf_set_run_ctx (&run_ctx.run_ctx);while ((prog =READ_ONCE (item->prog))) { run_ctx.prog_item = item; -if (!run_prog (prog, ctx)) +if (!run_prog (prog, ctx) && !IS_ERR_VALUE ((long )run_ctx.retval)) run_ctx.retval = -EPERM ; item++; }@@ -1390,7 +1390,7 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,* 0: NET_XMIT_SUCCESS skb should be transmitted * 1: NET_XMIT_DROP skb should be dropped and cn * 2: NET_XMIT_CN skb should be transmitted and cn -* 3: -EPERM skb should be dropped +* 3: -err skb should be dropped */ #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY (array, ctx, func) \ ({ \include/linux/bpf.h BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu, · line 1407
Quoted 10 lines
include/linux/bpf.h +3
-1@@ -1399,10 +1399,12 @@ BPF_PROG_RUN_ARRAY(const struct bpf_prog_array __rcu *array_rcu,u32 _ret; \ _ret =BPF_PROG_RUN_ARRAY_CG_FLAGS (array, ctx, func,0 , &_flags); \ _cn = _flags &BPF_RET_SET_CN ; \ +if (_ret && !IS_ERR_VALUE ((long )_ret)) \ + _ret = -EFAULT ; \if (!_ret) \ _ret = (_cn ?NET_XMIT_CN :NET_XMIT_SUCCESS ); \else \ - _ret = (_cn ?NET_XMIT_DROP : -EPERM ); \ + _ret = (_cn ?NET_XMIT_DROP : _ret); \Sorry for the long delay in reviewing.
Overall it looks very good.
Few questions:Quoted 4 lines
Why change this behavior for
BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY? It's for aninet_egresshook only. In other wordsip_output. What kind of different error codes do you want to pass to the stack from there?
Quoted 4 lines
I don't really have a use case in mind for a different error code for an egress hook (my use cases are for sockopt hooks) at the moment, but it sounds to me that it would a surprising behavior if
bpf_set_retvalis provided yet it would still be-EPERM.
Good point.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +0
-0@@ -177,7 +177,7 @@ static int getsetsockopt(void) optlen =sizeof (buf.zc); errno =0 ; err =getsockopt (fd,SOL_TCP ,TCP_ZEROCOPY_RECEIVE , &buf, &optlen);Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c static int getsetsockopt(void) · line 180
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +1
-1-if (errno !=EPERM ) { +if (errno !=EINVAL ) {Could you explain which part of this patch caused this change in user visible behavior?
I understand the desire to dobpf_set_retval(-EINVAL)and return 0, butprogs/sockopt_sk.cis not doing it.
Where does EINVAL come from?
Quoted 6 lines
This comes from the kernel. In for an anvalid address to the getsockopt handler in
tcp_zerocopy_receive[1]. The original behavior prior to this series is that the eBPF getsockopt hook generating an-EPERMoverrides that of the kernel's-EINVAL, but now when the eBPF hook returns 0, it sees that an-EINVALis already set by the kernel and does not modify the error code.
Got it.
I've added the following to the last patch to clarify it:
- buf.zc.address = 12345; /* rejected by BPF */
+ buf.zc.address = 12345; /* Not page aligned. Rejected bytcp_zerocopy_receive() */
and applied the set to bpf-next. Thanks!
PATCH v3 4/4 selftests/bpf: Test bpf_{get,set}_retval behavior with cgroup/sockopt
In reply to YiFei ZhuThe tests checks how different ways of interacting with the helpers (getting retval, setting EUNATCH, EISCONN, and legacy reject returning 0 without setting retval), produce different results in both the setsockopt syscall and the retval returned by the helper. A few more tests verify the interaction between the retval of the helper and the retval in getsockopt context.
--- .../bpf/prog_tests/cgroup_getset_retval.c | 481 ++++++++++++++++++ .../progs/cgroup_getset_retval_getsockopt.c | 45 ++ .../progs/cgroup_getset_retval_setsockopt.c | 52 ++ 3 files changed, 578 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_getset_retval.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_getset_retval_getsockopt.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_getset_retval_setsockopt.c
tools/testing/selftests/bpf/prog_tests/cgroup_getset_retval.c new file +481 -0
@@ -0,0 +1,481 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright 2021 Google LLC. +*/ + +#include <test_progs.h> +#include <cgroup_helpers.h> +#include <network_helpers.h> + +#include "cgroup_getset_retval_setsockopt.skel.h" +#include "cgroup_getset_retval_getsockopt.skel.h" + +#define SOL_CUSTOM 0xdeadbeef + +static int zero; + +static void test_setsockopt_set (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_setsockopt *obj; +struct bpf_link *link_set_eunatch =NULL ; + + obj =cgroup_getset_retval_setsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach setsockopt that sets EUNATCH, assert that +* we actually get that error when we run setsockopt() +*/ + link_set_eunatch =bpf_program__attach_cgroup (obj->progs.set_eunatch, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eunatch,"cg-attach-set_eunatch" )) + goto close_bpf_object; + +if (!ASSERT_ERR (setsockopt (sock_fd,SOL_SOCKET ,SO_REUSEADDR , + &zero,sizeof (int )),"setsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EUNATCH ,"setsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,1 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_set_eunatch); + +cgroup_getset_retval_setsockopt__destroy (obj); +} + +static void test_setsockopt_set_and_get (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_setsockopt *obj; +struct bpf_link *link_set_eunatch =NULL , *link_get_retval =NULL ; + + obj =cgroup_getset_retval_setsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach setsockopt that sets EUNATCH, and one that gets the +* previously set errno. Assert that we get the same errno back. +*/ + link_set_eunatch =bpf_program__attach_cgroup (obj->progs.set_eunatch, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eunatch,"cg-attach-set_eunatch" )) + goto close_bpf_object; + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + +if (!ASSERT_ERR (setsockopt (sock_fd,SOL_SOCKET ,SO_REUSEADDR , + &zero,sizeof (int )),"setsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EUNATCH ,"setsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,2 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value, -EUNATCH ,"retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_set_eunatch); +bpf_link__destroy (link_get_retval); + +cgroup_getset_retval_setsockopt__destroy (obj); +} + +static void test_setsockopt_default_zero (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_setsockopt *obj; +struct bpf_link *link_get_retval =NULL ; + + obj =cgroup_getset_retval_setsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach setsockopt that gets the previously set errno. +* Assert that, without anything setting one, we get 0. +*/ + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + +if (!ASSERT_OK (setsockopt (sock_fd,SOL_SOCKET ,SO_REUSEADDR , + &zero,sizeof (int )),"setsockopt" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,1 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value,0 ,"retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_get_retval); + +cgroup_getset_retval_setsockopt__destroy (obj); +} + +static void test_setsockopt_default_zero_and_set (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_setsockopt *obj; +struct bpf_link *link_get_retval =NULL , *link_set_eunatch =NULL ; + + obj =cgroup_getset_retval_setsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach setsockopt that gets the previously set errno, and then +* one that sets the errno to EUNATCH. Assert that the get does not +* see EUNATCH set later, and does not prevent EUNATCH from being set. +*/ + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + link_set_eunatch =bpf_program__attach_cgroup (obj->progs.set_eunatch, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eunatch,"cg-attach-set_eunatch" )) + goto close_bpf_object; + +if (!ASSERT_ERR (setsockopt (sock_fd,SOL_SOCKET ,SO_REUSEADDR , + &zero,sizeof (int )),"setsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EUNATCH ,"setsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,2 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value,0 ,"retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_get_retval); +bpf_link__destroy (link_set_eunatch); + +cgroup_getset_retval_setsockopt__destroy (obj); +} + +static void test_setsockopt_override (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_setsockopt *obj; +struct bpf_link *link_set_eunatch =NULL , *link_set_eisconn =NULL ; +struct bpf_link *link_get_retval =NULL ; + + obj =cgroup_getset_retval_setsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach setsockopt that sets EUNATCH, then one that sets EISCONN, +* and then one that gets the exported errno. Assert both the syscall +* and the helper sees the last set errno. +*/ + link_set_eunatch =bpf_program__attach_cgroup (obj->progs.set_eunatch, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eunatch,"cg-attach-set_eunatch" )) + goto close_bpf_object; + link_set_eisconn =bpf_program__attach_cgroup (obj->progs.set_eisconn, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eisconn,"cg-attach-set_eisconn" )) + goto close_bpf_object; + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + +if (!ASSERT_ERR (setsockopt (sock_fd,SOL_SOCKET ,SO_REUSEADDR , + &zero,sizeof (int )),"setsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EISCONN ,"setsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,3 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value, -EISCONN ,"retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_set_eunatch); +bpf_link__destroy (link_set_eisconn); +bpf_link__destroy (link_get_retval); + +cgroup_getset_retval_setsockopt__destroy (obj); +} + +static void test_setsockopt_legacy_eperm (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_setsockopt *obj; +struct bpf_link *link_legacy_eperm =NULL , *link_get_retval =NULL ; + + obj =cgroup_getset_retval_setsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach setsockopt that return a reject without setting errno +* (legacy reject), and one that gets the errno. Assert that for +* backward compatibility the syscall result in EPERM, and this +* is also visible to the helper. +*/ + link_legacy_eperm =bpf_program__attach_cgroup (obj->progs.legacy_eperm, + cgroup_fd); +if (!ASSERT_OK_PTR (link_legacy_eperm,"cg-attach-legacy_eperm" )) + goto close_bpf_object; + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + +if (!ASSERT_ERR (setsockopt (sock_fd,SOL_SOCKET ,SO_REUSEADDR , + &zero,sizeof (int )),"setsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EPERM ,"setsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,2 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value, -EPERM ,"retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_legacy_eperm); +bpf_link__destroy (link_get_retval); + +cgroup_getset_retval_setsockopt__destroy (obj); +} + +static void test_setsockopt_legacy_no_override (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_setsockopt *obj; +struct bpf_link *link_set_eunatch =NULL , *link_legacy_eperm =NULL ; +struct bpf_link *link_get_retval =NULL ; + + obj =cgroup_getset_retval_setsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach setsockopt that sets EUNATCH, then one that return a reject +* without setting errno, and then one that gets the exported errno. +* Assert both the syscall and the helper's errno are unaffected by +* the second prog (i.e. legacy rejects does not override the errno +* to EPERM). +*/ + link_set_eunatch =bpf_program__attach_cgroup (obj->progs.set_eunatch, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eunatch,"cg-attach-set_eunatch" )) + goto close_bpf_object; + link_legacy_eperm =bpf_program__attach_cgroup (obj->progs.legacy_eperm, + cgroup_fd); +if (!ASSERT_OK_PTR (link_legacy_eperm,"cg-attach-legacy_eperm" )) + goto close_bpf_object; + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + +if (!ASSERT_ERR (setsockopt (sock_fd,SOL_SOCKET ,SO_REUSEADDR , + &zero,sizeof (int )),"setsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EUNATCH ,"setsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,3 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value, -EUNATCH ,"retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_set_eunatch); +bpf_link__destroy (link_legacy_eperm); +bpf_link__destroy (link_get_retval); + +cgroup_getset_retval_setsockopt__destroy (obj); +} + +static void test_getsockopt_get (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_getsockopt *obj; +struct bpf_link *link_get_retval =NULL ; +int buf; +socklen_t optlen =sizeof (buf); + + obj =cgroup_getset_retval_getsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach getsockopt that gets previously set errno. Assert that the +* error from kernel is in both ctx_retval_value and retval_value. +*/ + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + +if (!ASSERT_ERR (getsockopt (sock_fd,SOL_CUSTOM ,0 , + &buf, &optlen),"getsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EOPNOTSUPP ,"getsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,1 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value, -EOPNOTSUPP ,"retval_value" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->ctx_retval_value, -EOPNOTSUPP ,"ctx_retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_get_retval); + +cgroup_getset_retval_getsockopt__destroy (obj); +} + +static void test_getsockopt_override (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_getsockopt *obj; +struct bpf_link *link_set_eisconn =NULL ; +int buf; +socklen_t optlen =sizeof (buf); + + obj =cgroup_getset_retval_getsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach getsockopt that sets retval to -EISCONN. Assert that this +* overrides the value from kernel. +*/ + link_set_eisconn =bpf_program__attach_cgroup (obj->progs.set_eisconn, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eisconn,"cg-attach-set_eisconn" )) + goto close_bpf_object; + +if (!ASSERT_ERR (getsockopt (sock_fd,SOL_CUSTOM ,0 , + &buf, &optlen),"getsockopt" )) + goto close_bpf_object; +if (!ASSERT_EQ (errno,EISCONN ,"getsockopt-errno" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,1 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_set_eisconn); + +cgroup_getset_retval_getsockopt__destroy (obj); +} + +static void test_getsockopt_retval_sync (int cgroup_fd,int sock_fd) +{ +struct cgroup_getset_retval_getsockopt *obj; +struct bpf_link *link_set_eisconn =NULL , *link_clear_retval =NULL ; +struct bpf_link *link_get_retval =NULL ; +int buf; +socklen_t optlen =sizeof (buf); + + obj =cgroup_getset_retval_getsockopt__open_and_load (); +if (!ASSERT_OK_PTR (obj,"skel-load" )) +return ; + +/* Attach getsockopt that sets retval to -EISCONN, and one that clears +* ctx retval. Assert that the clearing ctx retval is synced to helper +* and clears any errors both from kernel and BPF.. +*/ + link_set_eisconn =bpf_program__attach_cgroup (obj->progs.set_eisconn, + cgroup_fd); +if (!ASSERT_OK_PTR (link_set_eisconn,"cg-attach-set_eisconn" )) + goto close_bpf_object; + link_clear_retval =bpf_program__attach_cgroup (obj->progs.clear_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_clear_retval,"cg-attach-clear_retval" )) + goto close_bpf_object; + link_get_retval =bpf_program__attach_cgroup (obj->progs.get_retval, + cgroup_fd); +if (!ASSERT_OK_PTR (link_get_retval,"cg-attach-get_retval" )) + goto close_bpf_object; + +if (!ASSERT_OK (getsockopt (sock_fd,SOL_CUSTOM ,0 , + &buf, &optlen),"getsockopt" )) + goto close_bpf_object; + +if (!ASSERT_EQ (obj->bss->invocations,3 ,"invocations" )) + goto close_bpf_object; +if (!ASSERT_FALSE (obj->bss->assertion_error,"assertion_error" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->retval_value,0 ,"retval_value" )) + goto close_bpf_object; +if (!ASSERT_EQ (obj->bss->ctx_retval_value,0 ,"ctx_retval_value" )) + goto close_bpf_object; + +close_bpf_object: +bpf_link__destroy (link_set_eisconn); +bpf_link__destroy (link_clear_retval); +bpf_link__destroy (link_get_retval); + +cgroup_getset_retval_getsockopt__destroy (obj); +} + +void test_cgroup_getset_retval (void ) +{ +int cgroup_fd =-1 ; +int sock_fd =-1 ; + + cgroup_fd =test__join_cgroup ("/cgroup_getset_retval" ); +if (!ASSERT_GE (cgroup_fd,0 ,"cg-create" )) + goto close_fd; + + sock_fd =start_server (AF_INET ,SOCK_DGRAM ,NULL ,0 ,0 ); +if (!ASSERT_GE (sock_fd,0 ,"start-server" )) + goto close_fd; + +if (test__start_subtest ("setsockopt-set" )) +test_setsockopt_set (cgroup_fd, sock_fd); + +if (test__start_subtest ("setsockopt-set_and_get" )) +test_setsockopt_set_and_get (cgroup_fd, sock_fd); + +if (test__start_subtest ("setsockopt-default_zero" )) +test_setsockopt_default_zero (cgroup_fd, sock_fd); + +if (test__start_subtest ("setsockopt-default_zero_and_set" )) +test_setsockopt_default_zero_and_set (cgroup_fd, sock_fd); + +if (test__start_subtest ("setsockopt-override" )) +test_setsockopt_override (cgroup_fd, sock_fd); + +if (test__start_subtest ("setsockopt-legacy_eperm" )) +test_setsockopt_legacy_eperm (cgroup_fd, sock_fd); + +if (test__start_subtest ("setsockopt-legacy_no_override" )) +test_setsockopt_legacy_no_override (cgroup_fd, sock_fd); + +if (test__start_subtest ("getsockopt-get" )) +test_getsockopt_get (cgroup_fd, sock_fd); + +if (test__start_subtest ("getsockopt-override" )) +test_getsockopt_override (cgroup_fd, sock_fd); + +if (test__start_subtest ("getsockopt-retval_sync" )) +test_getsockopt_retval_sync (cgroup_fd, sock_fd); + +close_fd: +close (cgroup_fd); +}
tools/testing/selftests/bpf/progs/cgroup_getset_retval_getsockopt.c new file +45 -0
45 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgroup_getset_retval_setsockopt.c new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog