Skip to content

Commit 13bbbfb

Browse files
joannekoonganakryiko
authored andcommitted
bpf: Add bpf_dynptr_read and bpf_dynptr_write
This patch adds two helper functions, bpf_dynptr_read and bpf_dynptr_write: long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset); long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len); The dynptr passed into these functions must be valid dynptrs that have been initialized. Signed-off-by: Joanne Koong <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent bc34dee commit 13bbbfb

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

Diff for: include/uapi/linux/bpf.h

+19
Original file line numberDiff line numberDiff line change
@@ -5221,6 +5221,23 @@ union bpf_attr {
52215221
* 'bpf_ringbuf_discard'.
52225222
* Return
52235223
* Nothing. Always succeeds.
5224+
*
5225+
* long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset)
5226+
* Description
5227+
* Read *len* bytes from *src* into *dst*, starting from *offset*
5228+
* into *src*.
5229+
* Return
5230+
* 0 on success, -E2BIG if *offset* + *len* exceeds the length
5231+
* of *src*'s data, -EINVAL if *src* is an invalid dynptr.
5232+
*
5233+
* long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len)
5234+
* Description
5235+
* Write *len* bytes from *src* into *dst*, starting from *offset*
5236+
* into *dst*.
5237+
* Return
5238+
* 0 on success, -E2BIG if *offset* + *len* exceeds the length
5239+
* of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
5240+
* is a read-only dynptr.
52245241
*/
52255242
#define __BPF_FUNC_MAPPER(FN) \
52265243
FN(unspec), \
@@ -5424,6 +5441,8 @@ union bpf_attr {
54245441
FN(ringbuf_reserve_dynptr), \
54255442
FN(ringbuf_submit_dynptr), \
54265443
FN(ringbuf_discard_dynptr), \
5444+
FN(dynptr_read), \
5445+
FN(dynptr_write), \
54275446
/* */
54285447

54295448
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

Diff for: kernel/bpf/helpers.c

+78
Original file line numberDiff line numberDiff line change
@@ -1417,12 +1417,24 @@ const struct bpf_func_proto bpf_kptr_xchg_proto = {
14171417
*/
14181418
#define DYNPTR_MAX_SIZE ((1UL << 24) - 1)
14191419
#define DYNPTR_TYPE_SHIFT 28
1420+
#define DYNPTR_SIZE_MASK 0xFFFFFF
1421+
#define DYNPTR_RDONLY_BIT BIT(31)
1422+
1423+
static bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
1424+
{
1425+
return ptr->size & DYNPTR_RDONLY_BIT;
1426+
}
14201427

14211428
static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_type type)
14221429
{
14231430
ptr->size |= type << DYNPTR_TYPE_SHIFT;
14241431
}
14251432

1433+
static u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr)
1434+
{
1435+
return ptr->size & DYNPTR_SIZE_MASK;
1436+
}
1437+
14261438
int bpf_dynptr_check_size(u32 size)
14271439
{
14281440
return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -1442,6 +1454,16 @@ void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
14421454
memset(ptr, 0, sizeof(*ptr));
14431455
}
14441456

1457+
static int bpf_dynptr_check_off_len(struct bpf_dynptr_kern *ptr, u32 offset, u32 len)
1458+
{
1459+
u32 size = bpf_dynptr_get_size(ptr);
1460+
1461+
if (len > size || offset > size - len)
1462+
return -E2BIG;
1463+
1464+
return 0;
1465+
}
1466+
14451467
BPF_CALL_4(bpf_dynptr_from_mem, void *, data, u32, size, u64, flags, struct bpf_dynptr_kern *, ptr)
14461468
{
14471469
int err;
@@ -1475,6 +1497,58 @@ const struct bpf_func_proto bpf_dynptr_from_mem_proto = {
14751497
.arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT,
14761498
};
14771499

1500+
BPF_CALL_4(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src, u32, offset)
1501+
{
1502+
int err;
1503+
1504+
if (!src->data)
1505+
return -EINVAL;
1506+
1507+
err = bpf_dynptr_check_off_len(src, offset, len);
1508+
if (err)
1509+
return err;
1510+
1511+
memcpy(dst, src->data + src->offset + offset, len);
1512+
1513+
return 0;
1514+
}
1515+
1516+
const struct bpf_func_proto bpf_dynptr_read_proto = {
1517+
.func = bpf_dynptr_read,
1518+
.gpl_only = false,
1519+
.ret_type = RET_INTEGER,
1520+
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
1521+
.arg2_type = ARG_CONST_SIZE_OR_ZERO,
1522+
.arg3_type = ARG_PTR_TO_DYNPTR,
1523+
.arg4_type = ARG_ANYTHING,
1524+
};
1525+
1526+
BPF_CALL_4(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src, u32, len)
1527+
{
1528+
int err;
1529+
1530+
if (!dst->data || bpf_dynptr_is_rdonly(dst))
1531+
return -EINVAL;
1532+
1533+
err = bpf_dynptr_check_off_len(dst, offset, len);
1534+
if (err)
1535+
return err;
1536+
1537+
memcpy(dst->data + dst->offset + offset, src, len);
1538+
1539+
return 0;
1540+
}
1541+
1542+
const struct bpf_func_proto bpf_dynptr_write_proto = {
1543+
.func = bpf_dynptr_write,
1544+
.gpl_only = false,
1545+
.ret_type = RET_INTEGER,
1546+
.arg1_type = ARG_PTR_TO_DYNPTR,
1547+
.arg2_type = ARG_ANYTHING,
1548+
.arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1549+
.arg4_type = ARG_CONST_SIZE_OR_ZERO,
1550+
};
1551+
14781552
const struct bpf_func_proto bpf_get_current_task_proto __weak;
14791553
const struct bpf_func_proto bpf_get_current_task_btf_proto __weak;
14801554
const struct bpf_func_proto bpf_probe_read_user_proto __weak;
@@ -1537,6 +1611,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)
15371611
return &bpf_strncmp_proto;
15381612
case BPF_FUNC_dynptr_from_mem:
15391613
return &bpf_dynptr_from_mem_proto;
1614+
case BPF_FUNC_dynptr_read:
1615+
return &bpf_dynptr_read_proto;
1616+
case BPF_FUNC_dynptr_write:
1617+
return &bpf_dynptr_write_proto;
15401618
default:
15411619
break;
15421620
}

Diff for: tools/include/uapi/linux/bpf.h

+19
Original file line numberDiff line numberDiff line change
@@ -5221,6 +5221,23 @@ union bpf_attr {
52215221
* 'bpf_ringbuf_discard'.
52225222
* Return
52235223
* Nothing. Always succeeds.
5224+
*
5225+
* long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset)
5226+
* Description
5227+
* Read *len* bytes from *src* into *dst*, starting from *offset*
5228+
* into *src*.
5229+
* Return
5230+
* 0 on success, -E2BIG if *offset* + *len* exceeds the length
5231+
* of *src*'s data, -EINVAL if *src* is an invalid dynptr.
5232+
*
5233+
* long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len)
5234+
* Description
5235+
* Write *len* bytes from *src* into *dst*, starting from *offset*
5236+
* into *dst*.
5237+
* Return
5238+
* 0 on success, -E2BIG if *offset* + *len* exceeds the length
5239+
* of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
5240+
* is a read-only dynptr.
52245241
*/
52255242
#define __BPF_FUNC_MAPPER(FN) \
52265243
FN(unspec), \
@@ -5424,6 +5441,8 @@ union bpf_attr {
54245441
FN(ringbuf_reserve_dynptr), \
54255442
FN(ringbuf_submit_dynptr), \
54265443
FN(ringbuf_discard_dynptr), \
5444+
FN(dynptr_read), \
5445+
FN(dynptr_write), \
54275446
/* */
54285447

54295448
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)