Skip to content

Commit

Permalink
bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly
Browse files Browse the repository at this point in the history
bpf_dynptr_is_null returns true if the dynptr is null / invalid
(determined by whether ptr->data is NULL), else false if
the dynptr is a valid dynptr.

bpf_dynptr_is_rdonly returns true if the dynptr is read-only,
else false if the dynptr is read-writable. If the dynptr is
null / invalid, false is returned by default.

Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20230420071414.570108-3-joannelkoong@gmail.com
  • Loading branch information
joannekoong authored and borkmann committed Apr 27, 2023
1 parent 987d024 commit 540ccf9
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions kernel/bpf/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
#define DYNPTR_SIZE_MASK 0xFFFFFF
#define DYNPTR_RDONLY_BIT BIT(31)

static bool bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
static bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
{
return ptr->size & DYNPTR_RDONLY_BIT;
}
Expand Down Expand Up @@ -1570,7 +1570,7 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, v
enum bpf_dynptr_type type;
int err;

if (!dst->data || bpf_dynptr_is_rdonly(dst))
if (!dst->data || __bpf_dynptr_is_rdonly(dst))
return -EINVAL;

err = bpf_dynptr_check_off_len(dst, offset, len);
Expand Down Expand Up @@ -1626,7 +1626,7 @@ BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u3
if (err)
return 0;

if (bpf_dynptr_is_rdonly(ptr))
if (__bpf_dynptr_is_rdonly(ptr))
return 0;

type = bpf_dynptr_get_type(ptr);
Expand Down Expand Up @@ -2276,7 +2276,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
__bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
void *buffer, u32 buffer__szk)
{
if (!ptr->data || bpf_dynptr_is_rdonly(ptr))
if (!ptr->data || __bpf_dynptr_is_rdonly(ptr))
return NULL;

/* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice.
Expand Down Expand Up @@ -2322,6 +2322,19 @@ __bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 en
return 0;
}

__bpf_kfunc bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr)
{
return !ptr->data;
}

__bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
{
if (!ptr->data)
return false;

return __bpf_dynptr_is_rdonly(ptr);
}

__bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
{
return obj;
Expand Down Expand Up @@ -2395,6 +2408,8 @@ BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
BTF_ID_FLAGS(func, bpf_dynptr_adjust)
BTF_ID_FLAGS(func, bpf_dynptr_is_null)
BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
BTF_SET8_END(common_btf_ids)

static const struct btf_kfunc_id_set common_kfunc_set = {
Expand Down

0 comments on commit 540ccf9

Please sign in to comment.