* [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation
@ 2023-11-07 4:57 Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 1/3] bpf: Add __bpf_dynptr_data* for in kernel use Song Liu
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Song Liu @ 2023-11-07 4:57 UTC (permalink / raw)
To: bpf, fsverity
Cc: ast, daniel, andrii, martin.lau, kernel-team, ebiggers, tytso,
roberto.sassu, kpsingh, vadfed, Song Liu
This set contains the first 3 patches of set [1]. Currently, [1] is waiting
for [3] to be merged to bpf-next tree. So send these 3 patches first to
unblock other works, such as [2]. This set is verified with new version of
[1] in CI run [4].
Changes since v12 of [1]:
1. Reuse bpf_dynptr_slice() in __bpf_dynptr_data(). (Andrii)
2. Add Acked-by from Vadim Fedorenko.
[1] https://lore.kernel.org/bpf/20231104001313.3538201-1-song@kernel.org/
[2] https://lore.kernel.org/bpf/20231031134900.1432945-1-vadfed@meta.com/
[3] https://lore.kernel.org/bpf/20231031215625.2343848-1-davemarchevsky@fb.com/
[4] https://github.com/kernel-patches/bpf/actions/runs/6779945063/job/18427926114
Song Liu (3):
bpf: Add __bpf_dynptr_data* for in kernel use
bpf: Factor out helper check_reg_const_str()
bpf: Introduce KF_ARG_PTR_TO_CONST_STR
Documentation/bpf/kfuncs.rst | 24 ++++++++
include/linux/bpf.h | 2 +
kernel/bpf/helpers.c | 19 +++++++
kernel/bpf/verifier.c | 104 +++++++++++++++++++++++------------
kernel/trace/bpf_trace.c | 12 ++--
5 files changed, 121 insertions(+), 40 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH bpf-next 1/3] bpf: Add __bpf_dynptr_data* for in kernel use
2023-11-07 4:57 [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation Song Liu
@ 2023-11-07 4:57 ` Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 2/3] bpf: Factor out helper check_reg_const_str() Song Liu
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2023-11-07 4:57 UTC (permalink / raw)
To: bpf, fsverity
Cc: ast, daniel, andrii, martin.lau, kernel-team, ebiggers, tytso,
roberto.sassu, kpsingh, vadfed, Song Liu, Vadim Fedorenko
Different types of bpf dynptr have different internal data storage.
Specifically, SKB and XDP type of dynptr may have non-continuous data.
Therefore, it is not always safe to directly access dynptr->data.
Add __bpf_dynptr_data and __bpf_dynptr_data_rw to replace direct access to
dynptr->data.
Update bpf_verify_pkcs7_signature to use __bpf_dynptr_data instead of
dynptr->data.
Signed-off-by: Song Liu <song@kernel.org>
Acked-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
---
include/linux/bpf.h | 2 ++
kernel/bpf/helpers.c | 19 +++++++++++++++++++
kernel/trace/bpf_trace.c | 12 ++++++++----
3 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index b4825d3cdb29..eb84caf133df 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1222,6 +1222,8 @@ enum bpf_dynptr_type {
int bpf_dynptr_check_size(u32 size);
u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr);
+const void *__bpf_dynptr_data(const struct bpf_dynptr_kern *ptr, u32 len);
+void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len);
#ifdef CONFIG_BPF_JIT
int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index e46ac288a108..6b7c8163e128 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2611,3 +2611,22 @@ static int __init kfunc_init(void)
}
late_initcall(kfunc_init);
+
+/* Get a pointer to dynptr data up to len bytes for read only access. If
+ * the dynptr doesn't have continuous data up to len bytes, return NULL.
+ */
+const void *__bpf_dynptr_data(const struct bpf_dynptr_kern *ptr, u32 len)
+{
+ return bpf_dynptr_slice(ptr, 0, NULL, len);
+}
+
+/* Get a pointer to dynptr data up to len bytes for read write access. If
+ * the dynptr doesn't have continuous data up to len bytes, or the dynptr
+ * is read only, return NULL.
+ */
+void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len)
+{
+ if (__bpf_dynptr_is_rdonly(ptr))
+ return NULL;
+ return (void *)__bpf_dynptr_data(ptr, len);
+}
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index df697c74d519..d525a22b8d56 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1378,6 +1378,8 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
struct bpf_dynptr_kern *sig_ptr,
struct bpf_key *trusted_keyring)
{
+ const void *data, *sig;
+ u32 data_len, sig_len;
int ret;
if (trusted_keyring->has_ref) {
@@ -1394,10 +1396,12 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
return ret;
}
- return verify_pkcs7_signature(data_ptr->data,
- __bpf_dynptr_size(data_ptr),
- sig_ptr->data,
- __bpf_dynptr_size(sig_ptr),
+ data_len = __bpf_dynptr_size(data_ptr);
+ data = __bpf_dynptr_data(data_ptr, data_len);
+ sig_len = __bpf_dynptr_size(sig_ptr);
+ sig = __bpf_dynptr_data(sig_ptr, sig_len);
+
+ return verify_pkcs7_signature(data, data_len, sig, sig_len,
trusted_keyring->key,
VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
NULL);
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH bpf-next 2/3] bpf: Factor out helper check_reg_const_str()
2023-11-07 4:57 [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 1/3] bpf: Add __bpf_dynptr_data* for in kernel use Song Liu
@ 2023-11-07 4:57 ` Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 3/3] bpf: Introduce KF_ARG_PTR_TO_CONST_STR Song Liu
2023-11-07 18:10 ` [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation patchwork-bot+netdevbpf
3 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2023-11-07 4:57 UTC (permalink / raw)
To: bpf, fsverity
Cc: ast, daniel, andrii, martin.lau, kernel-team, ebiggers, tytso,
roberto.sassu, kpsingh, vadfed, Song Liu, Vadim Fedorenko
ARG_PTR_TO_CONST_STR is used to specify constant string args for BPF
helpers. The logic that verifies a reg is ARG_PTR_TO_CONST_STR is
implemented in check_func_arg().
As we introduce kfuncs with constant string args, it is necessary to
do the same check for kfuncs (in check_kfunc_args). Factor out the logic
for ARG_PTR_TO_CONST_STR to a new check_reg_const_str() so that it can be
reused.
check_func_arg() ensures check_reg_const_str() is only called with reg of
type PTR_TO_MAP_VALUE. Add a redundent type check in check_reg_const_str()
to avoid misuse in the future. Other than this redundent check, there is
no change in behavior.
Signed-off-by: Song Liu <song@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
---
kernel/bpf/verifier.c | 85 +++++++++++++++++++++++++------------------
1 file changed, 49 insertions(+), 36 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 2197385d91dc..618446006d5a 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -8718,6 +8718,54 @@ static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env,
return state->stack[spi].spilled_ptr.dynptr.type;
}
+static int check_reg_const_str(struct bpf_verifier_env *env,
+ struct bpf_reg_state *reg, u32 regno)
+{
+ struct bpf_map *map = reg->map_ptr;
+ int err;
+ int map_off;
+ u64 map_addr;
+ char *str_ptr;
+
+ if (reg->type != PTR_TO_MAP_VALUE)
+ return -EINVAL;
+
+ if (!bpf_map_is_rdonly(map)) {
+ verbose(env, "R%d does not point to a readonly map'\n", regno);
+ return -EACCES;
+ }
+
+ if (!tnum_is_const(reg->var_off)) {
+ verbose(env, "R%d is not a constant address'\n", regno);
+ return -EACCES;
+ }
+
+ if (!map->ops->map_direct_value_addr) {
+ verbose(env, "no direct value access support for this map type\n");
+ return -EACCES;
+ }
+
+ err = check_map_access(env, regno, reg->off,
+ map->value_size - reg->off, false,
+ ACCESS_HELPER);
+ if (err)
+ return err;
+
+ map_off = reg->off + reg->var_off.value;
+ err = map->ops->map_direct_value_addr(map, &map_addr, map_off);
+ if (err) {
+ verbose(env, "direct value access on string failed\n");
+ return err;
+ }
+
+ str_ptr = (char *)(long)(map_addr);
+ if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) {
+ verbose(env, "string is not zero-terminated\n");
+ return -EINVAL;
+ }
+ return 0;
+}
+
static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
struct bpf_call_arg_meta *meta,
const struct bpf_func_proto *fn,
@@ -8962,44 +9010,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
}
case ARG_PTR_TO_CONST_STR:
{
- struct bpf_map *map = reg->map_ptr;
- int map_off;
- u64 map_addr;
- char *str_ptr;
-
- if (!bpf_map_is_rdonly(map)) {
- verbose(env, "R%d does not point to a readonly map'\n", regno);
- return -EACCES;
- }
-
- if (!tnum_is_const(reg->var_off)) {
- verbose(env, "R%d is not a constant address'\n", regno);
- return -EACCES;
- }
-
- if (!map->ops->map_direct_value_addr) {
- verbose(env, "no direct value access support for this map type\n");
- return -EACCES;
- }
-
- err = check_map_access(env, regno, reg->off,
- map->value_size - reg->off, false,
- ACCESS_HELPER);
+ err = check_reg_const_str(env, reg, regno);
if (err)
return err;
-
- map_off = reg->off + reg->var_off.value;
- err = map->ops->map_direct_value_addr(map, &map_addr, map_off);
- if (err) {
- verbose(env, "direct value access on string failed\n");
- return err;
- }
-
- str_ptr = (char *)(long)(map_addr);
- if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) {
- verbose(env, "string is not zero-terminated\n");
- return -EINVAL;
- }
break;
}
case ARG_PTR_TO_KPTR:
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH bpf-next 3/3] bpf: Introduce KF_ARG_PTR_TO_CONST_STR
2023-11-07 4:57 [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 1/3] bpf: Add __bpf_dynptr_data* for in kernel use Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 2/3] bpf: Factor out helper check_reg_const_str() Song Liu
@ 2023-11-07 4:57 ` Song Liu
2023-11-07 18:10 ` [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation patchwork-bot+netdevbpf
3 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2023-11-07 4:57 UTC (permalink / raw)
To: bpf, fsverity
Cc: ast, daniel, andrii, martin.lau, kernel-team, ebiggers, tytso,
roberto.sassu, kpsingh, vadfed, Song Liu, Vadim Fedorenko
Similar to ARG_PTR_TO_CONST_STR for BPF helpers, KF_ARG_PTR_TO_CONST_STR
specifies kfunc args that point to const strings. Annotation "__str" is
used to specify kfunc arg of type KF_ARG_PTR_TO_CONST_STR. Also, add
documentation for the "__str" annotation.
bpf_get_file_xattr() will be the first kfunc that uses this type.
Signed-off-by: Song Liu <song@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
---
Documentation/bpf/kfuncs.rst | 24 ++++++++++++++++++++++++
kernel/bpf/verifier.c | 19 +++++++++++++++++++
2 files changed, 43 insertions(+)
diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
index 0d2647fb358d..bfe065f7e23c 100644
--- a/Documentation/bpf/kfuncs.rst
+++ b/Documentation/bpf/kfuncs.rst
@@ -137,6 +137,30 @@ Either way, the returned buffer is either NULL, or of size buffer_szk. Without t
annotation, the verifier will reject the program if a null pointer is passed in with
a nonzero size.
+2.2.5 __str Annotation
+----------------------------
+This annotation is used to indicate that the argument is a constant string.
+
+An example is given below::
+
+ __bpf_kfunc bpf_get_file_xattr(..., const char *name__str, ...)
+ {
+ ...
+ }
+
+In this case, ``bpf_get_file_xattr()`` can be called as::
+
+ bpf_get_file_xattr(..., "xattr_name", ...);
+
+Or::
+
+ const char name[] = "xattr_name"; /* This need to be global */
+ int BPF_PROG(...)
+ {
+ ...
+ bpf_get_file_xattr(..., name, ...);
+ ...
+ }
.. _BPF_kfunc_nodef:
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 618446006d5a..bf94ba50c6ee 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -10803,6 +10803,11 @@ static bool is_kfunc_arg_nullable(const struct btf *btf, const struct btf_param
return __kfunc_param_match_suffix(btf, arg, "__nullable");
}
+static bool is_kfunc_arg_const_str(const struct btf *btf, const struct btf_param *arg)
+{
+ return __kfunc_param_match_suffix(btf, arg, "__str");
+}
+
static bool is_kfunc_arg_scalar_with_name(const struct btf *btf,
const struct btf_param *arg,
const char *name)
@@ -10946,6 +10951,7 @@ enum kfunc_ptr_arg_type {
KF_ARG_PTR_TO_RB_ROOT,
KF_ARG_PTR_TO_RB_NODE,
KF_ARG_PTR_TO_NULL,
+ KF_ARG_PTR_TO_CONST_STR,
};
enum special_kfunc_type {
@@ -11090,6 +11096,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
if (is_kfunc_arg_rbtree_node(meta->btf, &args[argno]))
return KF_ARG_PTR_TO_RB_NODE;
+ if (is_kfunc_arg_const_str(meta->btf, &args[argno]))
+ return KF_ARG_PTR_TO_CONST_STR;
+
if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) {
if (!btf_type_is_struct(ref_t)) {
verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n",
@@ -11713,6 +11722,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
case KF_ARG_PTR_TO_MEM_SIZE:
case KF_ARG_PTR_TO_CALLBACK:
case KF_ARG_PTR_TO_REFCOUNTED_KPTR:
+ case KF_ARG_PTR_TO_CONST_STR:
/* Trusted by default */
break;
default:
@@ -11984,6 +11994,15 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
meta->arg_btf = reg->btf;
meta->arg_btf_id = reg->btf_id;
break;
+ case KF_ARG_PTR_TO_CONST_STR:
+ if (reg->type != PTR_TO_MAP_VALUE) {
+ verbose(env, "arg#%d doesn't point to a const string\n", i);
+ return -EINVAL;
+ }
+ ret = check_reg_const_str(env, reg, regno);
+ if (ret)
+ return ret;
+ break;
}
}
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation
2023-11-07 4:57 [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation Song Liu
` (2 preceding siblings ...)
2023-11-07 4:57 ` [PATCH bpf-next 3/3] bpf: Introduce KF_ARG_PTR_TO_CONST_STR Song Liu
@ 2023-11-07 18:10 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-11-07 18:10 UTC (permalink / raw)
To: Song Liu
Cc: bpf, fsverity, ast, daniel, andrii, martin.lau, kernel-team,
ebiggers, tytso, roberto.sassu, kpsingh, vadfed
Hello:
This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Mon, 6 Nov 2023 20:57:22 -0800 you wrote:
> This set contains the first 3 patches of set [1]. Currently, [1] is waiting
> for [3] to be merged to bpf-next tree. So send these 3 patches first to
> unblock other works, such as [2]. This set is verified with new version of
> [1] in CI run [4].
>
> Changes since v12 of [1]:
> 1. Reuse bpf_dynptr_slice() in __bpf_dynptr_data(). (Andrii)
> 2. Add Acked-by from Vadim Fedorenko.
>
> [...]
Here is the summary with links:
- [bpf-next,1/3] bpf: Add __bpf_dynptr_data* for in kernel use
https://git.kernel.org/bpf/bpf-next/c/afa58570c3f4
- [bpf-next,2/3] bpf: Factor out helper check_reg_const_str()
https://git.kernel.org/bpf/bpf-next/c/d8ace21aece8
- [bpf-next,3/3] bpf: Introduce KF_ARG_PTR_TO_CONST_STR
https://git.kernel.org/bpf/bpf-next/c/6d857121c63b
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-11-07 18:10 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-07 4:57 [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 1/3] bpf: Add __bpf_dynptr_data* for in kernel use Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 2/3] bpf: Factor out helper check_reg_const_str() Song Liu
2023-11-07 4:57 ` [PATCH bpf-next 3/3] bpf: Introduce KF_ARG_PTR_TO_CONST_STR Song Liu
2023-11-07 18:10 ` [PATCH bpf-next 0/3] bpf: __bpf_dynptr_data* and __str annotation patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox