History

bpf: Reject offset refcount acquire arguments

bpf_refcount_acquire() increments the refcount at the caller-supplied pointer plus the refcount field offset, then returns the caller-supplied pointer unchanged. The verifier records the return value as a base pointer to the refcounted object. bpf_list_pop_front() and bpf_rbtree_remove() can return embedded graph-node pointers as PTR_TO_BTF_ID | MEM_ALLOC with a fixed offset equal to the node field offset. Passing such a pointer directly to bpf_refcount_acquire() currently passes the refcounted-kptr type check. That makes the runtime operation start from base + node_off while the verifier models the returned pointer as the object base. Require refcount-acquire arguments to have zero fixed offset by carrying the requirement through check_func_arg_reg_off() to __check_ptr_off_reg(). Programs can still acquire a refcount from a graph-node-derived pointer after normalizing it with container_of().

Yiyang Chen committed 4 months ago

Commit 931a5771 parent 66bb9521 file · +22 −10Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/bpf/verifier.c+22−10
@@ -7996,9 +7996,10 @@ reg_find_field_offset(const struct bpf_reg_state *reg, s32 off, u32 fields) · 7995 unmodified lines
79967996 return field;
79977997 }
79987998
7999−static int check_func_arg_reg_off(struct bpf_verifier_env *env,
8000− const struct bpf_reg_state *reg, argno_t argno,
8001− enum bpf_arg_type arg_type)
7999+static int __check_func_arg_reg_off(struct bpf_verifier_env *env,
8000+ const struct bpf_reg_state *reg, argno_t argno,
8001+ enum bpf_arg_type arg_type,
8002+ bool btf_id_fixed_off_ok)
80028003 {
80038004 u32 type = reg->type;
80048005
@@ -8055,12 +8056,11 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env, · 50 unmodified lines
80558056 case PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF | MEM_RCU:
80568057 /* When referenced PTR_TO_BTF_ID is passed to release function,
80578058 * its fixed offset must be 0. In the other cases, fixed offset
8058− * can be non-zero. This was already checked above. So pass
8059− * fixed_off_ok as true to allow fixed offset for all other
8060− * cases. var_off always must be 0 for PTR_TO_BTF_ID, hence we
8061− * still need to do checks instead of returning.
8059+ * can be non-zero unless the caller requires otherwise.
8060+ * var_off always must be 0 for PTR_TO_BTF_ID, hence we still
8061+ * need to do checks instead of returning.
80628062 */
8063− return __check_ptr_off_reg(env, reg, argno, true);
8063+ return __check_ptr_off_reg(env, reg, argno, btf_id_fixed_off_ok);
80648064 case PTR_TO_CTX:
80658065 /*
80668066 * Allow fixed and variable offsets for syscall context, but
@@ -8076,6 +8076,13 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env, · 9 unmodified lines
80768076 }
80778077 }
80788078
8079+static int check_func_arg_reg_off(struct bpf_verifier_env *env,
8080+ const struct bpf_reg_state *reg, argno_t argno,
8081+ enum bpf_arg_type arg_type)
8082+{
8083+ return __check_func_arg_reg_off(env, reg, argno, arg_type, true);
8084+}
8085+
80798086 static int check_arg_const_str(struct bpf_verifier_env *env,
80808087 struct bpf_reg_state *reg, argno_t argno)
80818088 {
@@ -11947,6 +11954,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 3865 unmodified lines
1194711954 enum bpf_arg_type arg_type = ARG_DONTCARE;
1194811955 argno_t argno = argno_from_arg(i + 1);
1194911956 int regno = reg_from_argno(argno);
11957+ bool btf_id_fixed_off_ok = true;
1195011958 u32 ref_id, type_size;
1195111959 bool is_ret_buf_sz = false;
1195211960 int kf_arg_type;
@@ -12120,7 +12128,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 167 unmodified lines
1212012128 case KF_ARG_PTR_TO_MEM:
1212112129 case KF_ARG_PTR_TO_MEM_SIZE:
1212212130 case KF_ARG_PTR_TO_CALLBACK:
12123− case KF_ARG_PTR_TO_REFCOUNTED_KPTR:
1212412131 case KF_ARG_PTR_TO_CONST_STR:
1212512132 case KF_ARG_PTR_TO_WORKQUEUE:
1212612133 case KF_ARG_PTR_TO_TIMER:
@@ -12134,6 +12141,10 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 7 unmodified lines
1213412141 case KF_ARG_PTR_TO_CTX:
1213512142 arg_type = ARG_PTR_TO_CTX;
1213612143 break;
12144+ case KF_ARG_PTR_TO_REFCOUNTED_KPTR:
12145+ arg_type = ARG_PTR_TO_BTF_ID;
12146+ btf_id_fixed_off_ok = false;
12147+ break;
1213712148 default:
1213812149 verifier_bug(env, "unknown kfunc arg type %d", kf_arg_type);
1213912150 return -EFAULT;
@@ -12141,7 +12152,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 1 unmodified line
1214112152
1214212153 if (regno == meta->release_regno)
1214312154 arg_type |= OBJ_RELEASE;
12144− ret = check_func_arg_reg_off(env, reg, argno, arg_type);
12155+ ret = __check_func_arg_reg_off(env, reg, argno, arg_type,
12156+ btf_id_fixed_off_ok);
1214512157 if (ret < 0)
1214612158 return ret;
1214712159