提交 a7081a13 编写于 作者: Y Yonghong Song 提交者: Zheng Zengkai

bpf: Support BTF_KIND_TYPE_TAG for btf_type_tag attributes

mainline inclusion
from mainline-5.17-rc1
commit 8c42d2fa
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8c42d2fa4eeab6c37a0b1b1aa7a2715248ef4f34

-------------------------------------------------

LLVM patches ([1] for clang, [2] and [3] for BPF backend)
added support for btf_type_tag attributes. This patch
added support for the kernel.

The main motivation for btf_type_tag is to bring kernel
annotations __user, __rcu etc. to btf. With such information
available in btf, bpf verifier can detect mis-usages
and reject the program. For example, for __user tagged pointer,
developers can then use proper helper like bpf_probe_read_user()
etc. to read the data.

BTF_KIND_TYPE_TAG may also useful for other tracing
facility where instead of to require user to specify
kernel/user address type, the kernel can detect it
by itself with btf.

  [1] https://reviews.llvm.org/D111199
  [2] https://reviews.llvm.org/D113222
  [3] https://reviews.llvm.org/D113496Signed-off-by: NYonghong Song <yhs@fb.com>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Acked-by: NAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211112012609.1505032-1-yhs@fb.com
(cherry picked from commit 8c42d2fa)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 57b7bdeb
...@@ -43,7 +43,7 @@ struct btf_type { ...@@ -43,7 +43,7 @@ struct btf_type {
* "size" tells the size of the type it is describing. * "size" tells the size of the type it is describing.
* *
* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
* FUNC, FUNC_PROTO, VAR and DECL_TAG. * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
* "type" is a type_id referring to another type. * "type" is a type_id referring to another type.
*/ */
union { union {
...@@ -75,6 +75,7 @@ enum { ...@@ -75,6 +75,7 @@ enum {
BTF_KIND_DATASEC = 15, /* Section */ BTF_KIND_DATASEC = 15, /* Section */
BTF_KIND_FLOAT = 16, /* Floating point */ BTF_KIND_FLOAT = 16, /* Floating point */
BTF_KIND_DECL_TAG = 17, /* Decl Tag */ BTF_KIND_DECL_TAG = 17, /* Decl Tag */
BTF_KIND_TYPE_TAG = 18, /* Type Tag */
NR_BTF_KINDS, NR_BTF_KINDS,
BTF_KIND_MAX = NR_BTF_KINDS - 1, BTF_KIND_MAX = NR_BTF_KINDS - 1,
......
...@@ -281,6 +281,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = { ...@@ -281,6 +281,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
[BTF_KIND_VAR] = "VAR", [BTF_KIND_VAR] = "VAR",
[BTF_KIND_DATASEC] = "DATASEC", [BTF_KIND_DATASEC] = "DATASEC",
[BTF_KIND_DECL_TAG] = "DECL_TAG", [BTF_KIND_DECL_TAG] = "DECL_TAG",
[BTF_KIND_TYPE_TAG] = "TYPE_TAG",
}; };
const char *btf_type_str(const struct btf_type *t) const char *btf_type_str(const struct btf_type *t)
...@@ -417,6 +418,7 @@ static bool btf_type_is_modifier(const struct btf_type *t) ...@@ -417,6 +418,7 @@ static bool btf_type_is_modifier(const struct btf_type *t)
case BTF_KIND_VOLATILE: case BTF_KIND_VOLATILE:
case BTF_KIND_CONST: case BTF_KIND_CONST:
case BTF_KIND_RESTRICT: case BTF_KIND_RESTRICT:
case BTF_KIND_TYPE_TAG:
return true; return true;
} }
...@@ -1735,6 +1737,7 @@ __btf_resolve_size(const struct btf *btf, const struct btf_type *type, ...@@ -1735,6 +1737,7 @@ __btf_resolve_size(const struct btf *btf, const struct btf_type *type,
case BTF_KIND_VOLATILE: case BTF_KIND_VOLATILE:
case BTF_KIND_CONST: case BTF_KIND_CONST:
case BTF_KIND_RESTRICT: case BTF_KIND_RESTRICT:
case BTF_KIND_TYPE_TAG:
id = type->type; id = type->type;
type = btf_type_by_id(btf, type->type); type = btf_type_by_id(btf, type->type);
break; break;
...@@ -2343,6 +2346,8 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env, ...@@ -2343,6 +2346,8 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,
const struct btf_type *t, const struct btf_type *t,
u32 meta_left) u32 meta_left)
{ {
const char *value;
if (btf_type_vlen(t)) { if (btf_type_vlen(t)) {
btf_verifier_log_type(env, t, "vlen != 0"); btf_verifier_log_type(env, t, "vlen != 0");
return -EINVAL; return -EINVAL;
...@@ -2358,7 +2363,7 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env, ...@@ -2358,7 +2363,7 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,
return -EINVAL; return -EINVAL;
} }
/* typedef type must have a valid name, and other ref types, /* typedef/type_tag type must have a valid name, and other ref types,
* volatile, const, restrict, should have a null name. * volatile, const, restrict, should have a null name.
*/ */
if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) { if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF) {
...@@ -2367,6 +2372,12 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env, ...@@ -2367,6 +2372,12 @@ static int btf_ref_type_check_meta(struct btf_verifier_env *env,
btf_verifier_log_type(env, t, "Invalid name"); btf_verifier_log_type(env, t, "Invalid name");
return -EINVAL; return -EINVAL;
} }
} else if (BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG) {
value = btf_name_by_offset(env->btf, t->name_off);
if (!value || !value[0]) {
btf_verifier_log_type(env, t, "Invalid name");
return -EINVAL;
}
} else { } else {
if (t->name_off) { if (t->name_off) {
btf_verifier_log_type(env, t, "Invalid name"); btf_verifier_log_type(env, t, "Invalid name");
...@@ -3932,6 +3943,7 @@ static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = { ...@@ -3932,6 +3943,7 @@ static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS] = {
[BTF_KIND_VAR] = &var_ops, [BTF_KIND_VAR] = &var_ops,
[BTF_KIND_DATASEC] = &datasec_ops, [BTF_KIND_DATASEC] = &datasec_ops,
[BTF_KIND_DECL_TAG] = &decl_tag_ops, [BTF_KIND_DECL_TAG] = &decl_tag_ops,
[BTF_KIND_TYPE_TAG] = &modifier_ops,
}; };
static s32 btf_check_meta(struct btf_verifier_env *env, static s32 btf_check_meta(struct btf_verifier_env *env,
......
...@@ -43,7 +43,7 @@ struct btf_type { ...@@ -43,7 +43,7 @@ struct btf_type {
* "size" tells the size of the type it is describing. * "size" tells the size of the type it is describing.
* *
* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
* FUNC, FUNC_PROTO, VAR and DECL_TAG. * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
* "type" is a type_id referring to another type. * "type" is a type_id referring to another type.
*/ */
union { union {
...@@ -75,6 +75,7 @@ enum { ...@@ -75,6 +75,7 @@ enum {
BTF_KIND_DATASEC = 15, /* Section */ BTF_KIND_DATASEC = 15, /* Section */
BTF_KIND_FLOAT = 16, /* Floating point */ BTF_KIND_FLOAT = 16, /* Floating point */
BTF_KIND_DECL_TAG = 17, /* Decl Tag */ BTF_KIND_DECL_TAG = 17, /* Decl Tag */
BTF_KIND_TYPE_TAG = 18, /* Type Tag */
NR_BTF_KINDS, NR_BTF_KINDS,
BTF_KIND_MAX = NR_BTF_KINDS - 1, BTF_KIND_MAX = NR_BTF_KINDS - 1,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册