Skip to content

Commit d6aef08

Browse files
kkdwivediAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add bpf_kallsyms_lookup_name helper
This helper allows us to get the address of a kernel symbol from inside a BPF_PROG_TYPE_SYSCALL prog (used by gen_loader), so that we can relocate typeless ksym vars. Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 2895f48 commit d6aef08

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,7 @@ extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
21102110
extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
21112111
extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
21122112
extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
2113+
extern const struct bpf_func_proto bpf_kallsyms_lookup_name_proto;
21132114

21142115
const struct bpf_func_proto *tracing_prog_func_proto(
21152116
enum bpf_func_id func_id, const struct bpf_prog *prog);

include/uapi/linux/bpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4923,6 +4923,21 @@ union bpf_attr {
49234923
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
49244924
* Return
49254925
* *sk* if casting is valid, or **NULL** otherwise.
4926+
*
4927+
* long bpf_kallsyms_lookup_name(const char *name, int name_sz, int flags, u64 *res)
4928+
* Description
4929+
* Get the address of a kernel symbol, returned in *res*. *res* is
4930+
* set to 0 if the symbol is not found.
4931+
* Return
4932+
* On success, zero. On error, a negative value.
4933+
*
4934+
* **-EINVAL** if *flags* is not zero.
4935+
*
4936+
* **-EINVAL** if string *name* is not the same size as *name_sz*.
4937+
*
4938+
* **-ENOENT** if symbol is not found.
4939+
*
4940+
* **-EPERM** if caller does not have permission to obtain kernel address.
49264941
*/
49274942
#define __BPF_FUNC_MAPPER(FN) \
49284943
FN(unspec), \
@@ -5104,6 +5119,7 @@ union bpf_attr {
51045119
FN(get_branch_snapshot), \
51055120
FN(trace_vprintk), \
51065121
FN(skc_to_unix_sock), \
5122+
FN(kallsyms_lookup_name), \
51075123
/* */
51085124

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

kernel/bpf/syscall.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4781,6 +4781,31 @@ static const struct bpf_func_proto bpf_sys_close_proto = {
47814781
.arg1_type = ARG_ANYTHING,
47824782
};
47834783

4784+
BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
4785+
{
4786+
if (flags)
4787+
return -EINVAL;
4788+
4789+
if (name_sz <= 1 || name[name_sz - 1])
4790+
return -EINVAL;
4791+
4792+
if (!bpf_dump_raw_ok(current_cred()))
4793+
return -EPERM;
4794+
4795+
*res = kallsyms_lookup_name(name);
4796+
return *res ? 0 : -ENOENT;
4797+
}
4798+
4799+
const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
4800+
.func = bpf_kallsyms_lookup_name,
4801+
.gpl_only = false,
4802+
.ret_type = RET_INTEGER,
4803+
.arg1_type = ARG_PTR_TO_MEM,
4804+
.arg2_type = ARG_CONST_SIZE,
4805+
.arg3_type = ARG_ANYTHING,
4806+
.arg4_type = ARG_PTR_TO_LONG,
4807+
};
4808+
47844809
static const struct bpf_func_proto *
47854810
syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
47864811
{
@@ -4791,6 +4816,8 @@ syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
47914816
return &bpf_btf_find_by_name_kind_proto;
47924817
case BPF_FUNC_sys_close:
47934818
return &bpf_sys_close_proto;
4819+
case BPF_FUNC_kallsyms_lookup_name:
4820+
return &bpf_kallsyms_lookup_name_proto;
47944821
default:
47954822
return tracing_prog_func_proto(func_id, prog);
47964823
}

tools/include/uapi/linux/bpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4923,6 +4923,21 @@ union bpf_attr {
49234923
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
49244924
* Return
49254925
* *sk* if casting is valid, or **NULL** otherwise.
4926+
*
4927+
* long bpf_kallsyms_lookup_name(const char *name, int name_sz, int flags, u64 *res)
4928+
* Description
4929+
* Get the address of a kernel symbol, returned in *res*. *res* is
4930+
* set to 0 if the symbol is not found.
4931+
* Return
4932+
* On success, zero. On error, a negative value.
4933+
*
4934+
* **-EINVAL** if *flags* is not zero.
4935+
*
4936+
* **-EINVAL** if string *name* is not the same size as *name_sz*.
4937+
*
4938+
* **-ENOENT** if symbol is not found.
4939+
*
4940+
* **-EPERM** if caller does not have permission to obtain kernel address.
49264941
*/
49274942
#define __BPF_FUNC_MAPPER(FN) \
49284943
FN(unspec), \
@@ -5104,6 +5119,7 @@ union bpf_attr {
51045119
FN(get_branch_snapshot), \
51055120
FN(trace_vprintk), \
51065121
FN(skc_to_unix_sock), \
5122+
FN(kallsyms_lookup_name), \
51075123
/* */
51085124

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

0 commit comments

Comments
 (0)