Commit f8d3da4e authored by Joanne Koong's avatar Joanne Koong Committed by Daniel Borkmann

bpf: Add flags arg to bpf_dynptr_read and bpf_dynptr_write APIs

Commit 13bbbfbe ("bpf: Add bpf_dynptr_read and bpf_dynptr_write")
added the bpf_dynptr_write() and bpf_dynptr_read() APIs.

However, it will be needed for some dynptr types to pass in flags as
well (e.g. when writing to a skb, the user may like to invalidate the
hash or recompute the checksum).

This patch adds a "u64 flags" arg to the bpf_dynptr_read() and
bpf_dynptr_write() APIs before their UAPI signature freezes where
we then cannot change them anymore with a 5.19.x released kernel.

Fixes: 13bbbfbe ("bpf: Add bpf_dynptr_read and bpf_dynptr_write")
Signed-off-by: default avatarJoanne Koong <joannelkoong@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20220706232547.4016651-1-joannelkoong@gmail.com
parent 0326195f
...@@ -5222,22 +5222,25 @@ union bpf_attr { ...@@ -5222,22 +5222,25 @@ union bpf_attr {
* Return * Return
* Nothing. Always succeeds. * Nothing. Always succeeds.
* *
* long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset) * long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset, u64 flags)
* Description * Description
* Read *len* bytes from *src* into *dst*, starting from *offset* * Read *len* bytes from *src* into *dst*, starting from *offset*
* into *src*. * into *src*.
* *flags* is currently unused.
* Return * Return
* 0 on success, -E2BIG if *offset* + *len* exceeds the length * 0 on success, -E2BIG if *offset* + *len* exceeds the length
* of *src*'s data, -EINVAL if *src* is an invalid dynptr. * of *src*'s data, -EINVAL if *src* is an invalid dynptr or if
* *flags* is not 0.
* *
* long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len) * long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len, u64 flags)
* Description * Description
* Write *len* bytes from *src* into *dst*, starting from *offset* * Write *len* bytes from *src* into *dst*, starting from *offset*
* into *dst*. * into *dst*.
* *flags* is currently unused.
* Return * Return
* 0 on success, -E2BIG if *offset* + *len* exceeds the length * 0 on success, -E2BIG if *offset* + *len* exceeds the length
* of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst* * of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
* is a read-only dynptr. * is a read-only dynptr or if *flags* is not 0.
* *
* void *bpf_dynptr_data(struct bpf_dynptr *ptr, u32 offset, u32 len) * void *bpf_dynptr_data(struct bpf_dynptr *ptr, u32 offset, u32 len)
* Description * Description
......
...@@ -1497,11 +1497,12 @@ const struct bpf_func_proto bpf_dynptr_from_mem_proto = { ...@@ -1497,11 +1497,12 @@ const struct bpf_func_proto bpf_dynptr_from_mem_proto = {
.arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT, .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT,
}; };
BPF_CALL_4(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src, u32, offset) BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src,
u32, offset, u64, flags)
{ {
int err; int err;
if (!src->data) if (!src->data || flags)
return -EINVAL; return -EINVAL;
err = bpf_dynptr_check_off_len(src, offset, len); err = bpf_dynptr_check_off_len(src, offset, len);
...@@ -1521,13 +1522,15 @@ const struct bpf_func_proto bpf_dynptr_read_proto = { ...@@ -1521,13 +1522,15 @@ const struct bpf_func_proto bpf_dynptr_read_proto = {
.arg2_type = ARG_CONST_SIZE_OR_ZERO, .arg2_type = ARG_CONST_SIZE_OR_ZERO,
.arg3_type = ARG_PTR_TO_DYNPTR, .arg3_type = ARG_PTR_TO_DYNPTR,
.arg4_type = ARG_ANYTHING, .arg4_type = ARG_ANYTHING,
.arg5_type = ARG_ANYTHING,
}; };
BPF_CALL_4(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src, u32, len) BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
u32, len, u64, flags)
{ {
int err; int err;
if (!dst->data || bpf_dynptr_is_rdonly(dst)) if (!dst->data || flags || bpf_dynptr_is_rdonly(dst))
return -EINVAL; return -EINVAL;
err = bpf_dynptr_check_off_len(dst, offset, len); err = bpf_dynptr_check_off_len(dst, offset, len);
...@@ -1547,6 +1550,7 @@ const struct bpf_func_proto bpf_dynptr_write_proto = { ...@@ -1547,6 +1550,7 @@ const struct bpf_func_proto bpf_dynptr_write_proto = {
.arg2_type = ARG_ANYTHING, .arg2_type = ARG_ANYTHING,
.arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
.arg4_type = ARG_CONST_SIZE_OR_ZERO, .arg4_type = ARG_CONST_SIZE_OR_ZERO,
.arg5_type = ARG_ANYTHING,
}; };
BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len) BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len)
......
...@@ -5222,22 +5222,25 @@ union bpf_attr { ...@@ -5222,22 +5222,25 @@ union bpf_attr {
* Return * Return
* Nothing. Always succeeds. * Nothing. Always succeeds.
* *
* long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset) * long bpf_dynptr_read(void *dst, u32 len, struct bpf_dynptr *src, u32 offset, u64 flags)
* Description * Description
* Read *len* bytes from *src* into *dst*, starting from *offset* * Read *len* bytes from *src* into *dst*, starting from *offset*
* into *src*. * into *src*.
* *flags* is currently unused.
* Return * Return
* 0 on success, -E2BIG if *offset* + *len* exceeds the length * 0 on success, -E2BIG if *offset* + *len* exceeds the length
* of *src*'s data, -EINVAL if *src* is an invalid dynptr. * of *src*'s data, -EINVAL if *src* is an invalid dynptr or if
* *flags* is not 0.
* *
* long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len) * long bpf_dynptr_write(struct bpf_dynptr *dst, u32 offset, void *src, u32 len, u64 flags)
* Description * Description
* Write *len* bytes from *src* into *dst*, starting from *offset* * Write *len* bytes from *src* into *dst*, starting from *offset*
* into *dst*. * into *dst*.
* *flags* is currently unused.
* Return * Return
* 0 on success, -E2BIG if *offset* + *len* exceeds the length * 0 on success, -E2BIG if *offset* + *len* exceeds the length
* of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst* * of *dst*'s data, -EINVAL if *dst* is an invalid dynptr or if *dst*
* is a read-only dynptr. * is a read-only dynptr or if *flags* is not 0.
* *
* void *bpf_dynptr_data(struct bpf_dynptr *ptr, u32 offset, u32 len) * void *bpf_dynptr_data(struct bpf_dynptr *ptr, u32 offset, u32 len)
* Description * Description
......
...@@ -140,12 +140,12 @@ int use_after_invalid(void *ctx) ...@@ -140,12 +140,12 @@ int use_after_invalid(void *ctx)
bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(read_data), 0, &ptr); bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(read_data), 0, &ptr);
bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0); bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
bpf_ringbuf_submit_dynptr(&ptr, 0); bpf_ringbuf_submit_dynptr(&ptr, 0);
/* this should fail */ /* this should fail */
bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0); bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
return 0; return 0;
} }
...@@ -338,7 +338,7 @@ int invalid_helper2(void *ctx) ...@@ -338,7 +338,7 @@ int invalid_helper2(void *ctx)
get_map_val_dynptr(&ptr); get_map_val_dynptr(&ptr);
/* this should fail */ /* this should fail */
bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 8, 0); bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 8, 0, 0);
return 0; return 0;
} }
...@@ -377,7 +377,7 @@ int invalid_write2(void *ctx) ...@@ -377,7 +377,7 @@ int invalid_write2(void *ctx)
memcpy((void *)&ptr + 8, &x, sizeof(x)); memcpy((void *)&ptr + 8, &x, sizeof(x));
/* this should fail */ /* this should fail */
bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0); bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
bpf_ringbuf_submit_dynptr(&ptr, 0); bpf_ringbuf_submit_dynptr(&ptr, 0);
...@@ -473,7 +473,7 @@ int invalid_read2(void *ctx) ...@@ -473,7 +473,7 @@ int invalid_read2(void *ctx)
get_map_val_dynptr(&ptr); get_map_val_dynptr(&ptr);
/* this should fail */ /* this should fail */
bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 1, 0); bpf_dynptr_read(read_data, sizeof(read_data), (void *)&ptr + 1, 0, 0);
return 0; return 0;
} }
......
...@@ -43,10 +43,10 @@ int test_read_write(void *ctx) ...@@ -43,10 +43,10 @@ int test_read_write(void *ctx)
bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(write_data), 0, &ptr); bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(write_data), 0, &ptr);
/* Write data into the dynptr */ /* Write data into the dynptr */
err = err ?: bpf_dynptr_write(&ptr, 0, write_data, sizeof(write_data)); err = bpf_dynptr_write(&ptr, 0, write_data, sizeof(write_data), 0);
/* Read the data that was written into the dynptr */ /* Read the data that was written into the dynptr */
err = err ?: bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0); err = err ?: bpf_dynptr_read(read_data, sizeof(read_data), &ptr, 0, 0);
/* Ensure the data we read matches the data we wrote */ /* Ensure the data we read matches the data we wrote */
for (i = 0; i < sizeof(read_data); i++) { for (i = 0; i < sizeof(read_data); i++) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment