Skip to content

Commit fca1aa7

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Handle MEM_RCU type properly
Commit 9bb00b2 ("bpf: Add kfunc bpf_rcu_read_lock/unlock()") introduced MEM_RCU and bpf_rcu_read_lock/unlock() support. In that commit, a rcu pointer is tagged with both MEM_RCU and PTR_TRUSTED so that it can be passed into kfuncs or helpers as an argument. Martin raised a good question in [1] such that the rcu pointer, although being able to accessing the object, might have reference count of 0. This might cause a problem if the rcu pointer is passed to a kfunc which expects trusted arguments where ref count should be greater than 0. This patch makes the following changes related to MEM_RCU pointer: - MEM_RCU pointer might be NULL (PTR_MAYBE_NULL). - Introduce KF_RCU so MEM_RCU ptr can be acquired with a KF_RCU tagged kfunc which assumes ref count of rcu ptr could be zero. - For mem access 'b = ptr->a', say 'ptr' is a MEM_RCU ptr, and 'a' is tagged with __rcu as well. Let us mark 'b' as MEM_RCU | PTR_MAYBE_NULL. [1] https://lore.kernel.org/bpf/ac70f574-4023-664e-b711-e0d3b18117fd@linux.dev/ Fixes: 9bb00b2 ("bpf: Add kfunc bpf_rcu_read_lock/unlock()") Signed-off-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/r/20221203184602.477272-1-yhs@fb.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 7068194 commit fca1aa7

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

include/linux/bpf_verifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
682682
}
683683
}
684684

685-
#define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | MEM_RCU | PTR_TRUSTED)
685+
#define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED)
686686

687687
static inline bool bpf_type_has_unsafe_modifiers(u32 type)
688688
{

include/linux/btf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
7171
#define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */
7272
#define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */
73+
#define KF_RCU (1 << 7) /* kfunc only takes rcu pointer arguments */
7374

7475
/*
7576
* Return the name of the passed struct, if exists, or halt the build if for

kernel/bpf/helpers.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,19 @@ struct task_struct *bpf_task_acquire(struct task_struct *p)
18371837
return p;
18381838
}
18391839

1840+
/**
1841+
* bpf_task_acquire_not_zero - Acquire a reference to a rcu task object. A task
1842+
* acquired by this kfunc which is not stored in a map as a kptr, must be
1843+
* released by calling bpf_task_release().
1844+
* @p: The task on which a reference is being acquired.
1845+
*/
1846+
struct task_struct *bpf_task_acquire_not_zero(struct task_struct *p)
1847+
{
1848+
if (!refcount_inc_not_zero(&p->rcu_users))
1849+
return NULL;
1850+
return p;
1851+
}
1852+
18401853
/**
18411854
* bpf_task_kptr_get - Acquire a reference on a struct task_struct kptr. A task
18421855
* kptr acquired by this kfunc which is not subsequently stored in a map, must
@@ -2013,6 +2026,7 @@ BTF_ID_FLAGS(func, bpf_list_push_back)
20132026
BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL)
20142027
BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL)
20152028
BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
2029+
BTF_ID_FLAGS(func, bpf_task_acquire_not_zero, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
20162030
BTF_ID_FLAGS(func, bpf_task_kptr_get, KF_ACQUIRE | KF_KPTR_GET | KF_RET_NULL)
20172031
BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE)
20182032
#ifdef CONFIG_CGROUPS

kernel/bpf/verifier.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4275,7 +4275,7 @@ static bool is_trusted_reg(const struct bpf_reg_state *reg)
42754275
return true;
42764276

42774277
/* If a register is not referenced, it is trusted if it has the
4278-
* MEM_ALLOC, MEM_RCU or PTR_TRUSTED type modifiers, and no others. Some of the
4278+
* MEM_ALLOC or PTR_TRUSTED type modifiers, and no others. Some of the
42794279
* other type modifiers may be safe, but we elect to take an opt-in
42804280
* approach here as some (e.g. PTR_UNTRUSTED and PTR_MAYBE_NULL) are
42814281
* not.
@@ -4287,6 +4287,11 @@ static bool is_trusted_reg(const struct bpf_reg_state *reg)
42874287
!bpf_type_has_unsafe_modifiers(reg->type);
42884288
}
42894289

4290+
static bool is_rcu_reg(const struct bpf_reg_state *reg)
4291+
{
4292+
return reg->type & MEM_RCU;
4293+
}
4294+
42904295
static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
42914296
const struct bpf_reg_state *reg,
42924297
int off, int size, bool strict)
@@ -4785,14 +4790,16 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
47854790

47864791
if (flag & MEM_RCU) {
47874792
/* Mark value register as MEM_RCU only if it is protected by
4788-
* bpf_rcu_read_lock() and the ptr reg is trusted. MEM_RCU
4793+
* bpf_rcu_read_lock() and the ptr reg is rcu or trusted. MEM_RCU
47894794
* itself can already indicate trustedness inside the rcu
4790-
* read lock region. Also mark it as PTR_TRUSTED.
4795+
* read lock region. Also mark rcu pointer as PTR_MAYBE_NULL since
4796+
* it could be null in some cases.
47914797
*/
4792-
if (!env->cur_state->active_rcu_lock || !is_trusted_reg(reg))
4798+
if (!env->cur_state->active_rcu_lock ||
4799+
!(is_trusted_reg(reg) || is_rcu_reg(reg)))
47934800
flag &= ~MEM_RCU;
47944801
else
4795-
flag |= PTR_TRUSTED;
4802+
flag |= PTR_MAYBE_NULL;
47964803
} else if (reg->type & MEM_RCU) {
47974804
/* ptr (reg) is marked as MEM_RCU, but the struct field is not tagged
47984805
* with __rcu. Mark the flag as PTR_UNTRUSTED conservatively.
@@ -5957,7 +5964,7 @@ static const struct bpf_reg_types btf_ptr_types = {
59575964
.types = {
59585965
PTR_TO_BTF_ID,
59595966
PTR_TO_BTF_ID | PTR_TRUSTED,
5960-
PTR_TO_BTF_ID | MEM_RCU | PTR_TRUSTED,
5967+
PTR_TO_BTF_ID | MEM_RCU,
59615968
},
59625969
};
59635970
static const struct bpf_reg_types percpu_btf_ptr_types = {
@@ -6136,7 +6143,7 @@ int check_func_arg_reg_off(struct bpf_verifier_env *env,
61366143
case PTR_TO_BTF_ID:
61376144
case PTR_TO_BTF_ID | MEM_ALLOC:
61386145
case PTR_TO_BTF_ID | PTR_TRUSTED:
6139-
case PTR_TO_BTF_ID | MEM_RCU | PTR_TRUSTED:
6146+
case PTR_TO_BTF_ID | MEM_RCU:
61406147
case PTR_TO_BTF_ID | MEM_ALLOC | PTR_TRUSTED:
61416148
/* When referenced PTR_TO_BTF_ID is passed to release function,
61426149
* it's fixed offset must be 0. In the other cases, fixed offset
@@ -8038,6 +8045,11 @@ static bool is_kfunc_destructive(struct bpf_kfunc_call_arg_meta *meta)
80388045
return meta->kfunc_flags & KF_DESTRUCTIVE;
80398046
}
80408047

8048+
static bool is_kfunc_rcu(struct bpf_kfunc_call_arg_meta *meta)
8049+
{
8050+
return meta->kfunc_flags & KF_RCU;
8051+
}
8052+
80418053
static bool is_kfunc_arg_kptr_get(struct bpf_kfunc_call_arg_meta *meta, int arg)
80428054
{
80438055
return arg == 0 && (meta->kfunc_flags & KF_KPTR_GET);
@@ -8722,13 +8734,20 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
87228734
switch (kf_arg_type) {
87238735
case KF_ARG_PTR_TO_ALLOC_BTF_ID:
87248736
case KF_ARG_PTR_TO_BTF_ID:
8725-
if (!is_kfunc_trusted_args(meta))
8737+
if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta))
87268738
break;
87278739

87288740
if (!is_trusted_reg(reg)) {
8729-
verbose(env, "R%d must be referenced or trusted\n", regno);
8730-
return -EINVAL;
8741+
if (!is_kfunc_rcu(meta)) {
8742+
verbose(env, "R%d must be referenced or trusted\n", regno);
8743+
return -EINVAL;
8744+
}
8745+
if (!is_rcu_reg(reg)) {
8746+
verbose(env, "R%d must be a rcu pointer\n", regno);
8747+
return -EINVAL;
8748+
}
87318749
}
8750+
87328751
fallthrough;
87338752
case KF_ARG_PTR_TO_CTX:
87348753
/* Trusted arguments have the same offset checks as release arguments */
@@ -8839,7 +8858,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
88398858
case KF_ARG_PTR_TO_BTF_ID:
88408859
/* Only base_type is checked, further checks are done here */
88418860
if ((base_type(reg->type) != PTR_TO_BTF_ID ||
8842-
bpf_type_has_unsafe_modifiers(reg->type)) &&
8861+
(bpf_type_has_unsafe_modifiers(reg->type) && !is_rcu_reg(reg))) &&
88438862
!reg2btf_ids[base_type(reg->type)]) {
88448863
verbose(env, "arg#%d is %s ", i, reg_type_str(env, reg->type));
88458864
verbose(env, "expected %s or socket\n",
@@ -8954,7 +8973,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
89548973
} else if (rcu_unlock) {
89558974
bpf_for_each_reg_in_vstate(env->cur_state, state, reg, ({
89568975
if (reg->type & MEM_RCU) {
8957-
reg->type &= ~(MEM_RCU | PTR_TRUSTED);
8976+
reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL);
89588977
reg->type |= PTR_UNTRUSTED;
89598978
}
89608979
}));
@@ -11294,7 +11313,7 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
1129411313
bool is_null)
1129511314
{
1129611315
if (type_may_be_null(reg->type) && reg->id == id &&
11297-
!WARN_ON_ONCE(!reg->id)) {
11316+
(is_rcu_reg(reg) || !WARN_ON_ONCE(!reg->id))) {
1129811317
/* Old offset (both fixed and variable parts) should have been
1129911318
* known-zero, because we don't allow pointer arithmetic on
1130011319
* pointers that might be NULL. If we see this happening, don't

0 commit comments

Comments
 (0)