Skip to content

Commit 65023e7

Browse files
Mark RutlandKernel Patches Daemon
Mark Rutland
authored and
Kernel Patches Daemon
committed
cfi: add C CFI type macro
Currently x86 and riscv open-code 4 instances of the same logic to define a u32 variable with the KCFI typeid of a given function. Replace the duplicate logic with a common macro. Signed-off-by: Mark Rutland <[email protected]> Co-Developed-by: Maxwell Bland <[email protected]> Signed-off-by: Maxwell Bland <[email protected]> Co-Developed-by: Sami Tolvanen <[email protected]> Signed-off-by: Sami Tolvanen <[email protected]>
1 parent 44c3a1d commit 65023e7

File tree

3 files changed

+29
-64
lines changed

3 files changed

+29
-64
lines changed

arch/riscv/kernel/cfi.c

+3-32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* Copyright (C) 2023 Google LLC
66
*/
7+
#include <linux/cfi_types.h>
78
#include <linux/cfi.h>
89
#include <asm/insn.h>
910

@@ -82,41 +83,11 @@ struct bpf_insn;
8283
/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
8384
extern unsigned int __bpf_prog_runX(const void *ctx,
8485
const struct bpf_insn *insn);
85-
86-
/*
87-
* Force a reference to the external symbol so the compiler generates
88-
* __kcfi_typid.
89-
*/
90-
__ADDRESSABLE(__bpf_prog_runX);
91-
92-
/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
93-
asm (
94-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
95-
" .type cfi_bpf_hash,@object \n"
96-
" .globl cfi_bpf_hash \n"
97-
" .p2align 2, 0x0 \n"
98-
"cfi_bpf_hash: \n"
99-
" .word __kcfi_typeid___bpf_prog_runX \n"
100-
" .size cfi_bpf_hash, 4 \n"
101-
" .popsection \n"
102-
);
86+
DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX);
10387

10488
/* Must match bpf_callback_t */
10589
extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
106-
107-
__ADDRESSABLE(__bpf_callback_fn);
108-
109-
/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
110-
asm (
111-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
112-
" .type cfi_bpf_subprog_hash,@object \n"
113-
" .globl cfi_bpf_subprog_hash \n"
114-
" .p2align 2, 0x0 \n"
115-
"cfi_bpf_subprog_hash: \n"
116-
" .word __kcfi_typeid___bpf_callback_fn \n"
117-
" .size cfi_bpf_subprog_hash, 4 \n"
118-
" .popsection \n"
119-
);
90+
DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn);
12091

12192
u32 cfi_get_func_hash(void *func)
12293
{

arch/x86/kernel/alternative.c

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
#define pr_fmt(fmt) "SMP alternatives: " fmt
33

4+
#include <linux/cfi_types.h>
45
#include <linux/module.h>
56
#include <linux/sched.h>
67
#include <linux/perf_event.h>
@@ -934,41 +935,11 @@ struct bpf_insn;
934935
/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
935936
extern unsigned int __bpf_prog_runX(const void *ctx,
936937
const struct bpf_insn *insn);
937-
938-
/*
939-
* Force a reference to the external symbol so the compiler generates
940-
* __kcfi_typid.
941-
*/
942-
__ADDRESSABLE(__bpf_prog_runX);
943-
944-
/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
945-
asm (
946-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
947-
" .type cfi_bpf_hash,@object \n"
948-
" .globl cfi_bpf_hash \n"
949-
" .p2align 2, 0x0 \n"
950-
"cfi_bpf_hash: \n"
951-
" .long __kcfi_typeid___bpf_prog_runX \n"
952-
" .size cfi_bpf_hash, 4 \n"
953-
" .popsection \n"
954-
);
938+
DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX);
955939

956940
/* Must match bpf_callback_t */
957941
extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
958-
959-
__ADDRESSABLE(__bpf_callback_fn);
960-
961-
/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
962-
asm (
963-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
964-
" .type cfi_bpf_subprog_hash,@object \n"
965-
" .globl cfi_bpf_subprog_hash \n"
966-
" .p2align 2, 0x0 \n"
967-
"cfi_bpf_subprog_hash: \n"
968-
" .long __kcfi_typeid___bpf_callback_fn \n"
969-
" .size cfi_bpf_subprog_hash, 4 \n"
970-
" .popsection \n"
971-
);
942+
DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn);
972943

973944
u32 cfi_get_func_hash(void *func)
974945
{

include/linux/cfi_types.h

+23
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,28 @@
4141
SYM_TYPED_START(name, SYM_L_GLOBAL, SYM_A_ALIGN)
4242
#endif
4343

44+
#else /* __ASSEMBLY__ */
45+
46+
#ifdef CONFIG_CFI_CLANG
47+
#define DEFINE_CFI_TYPE(name, func) \
48+
/* \
49+
* Force a reference to the function so the compiler generates \
50+
* __kcfi_typeid_<func>. \
51+
*/ \
52+
__ADDRESSABLE(func); \
53+
/* u32 name = __kcfi_typeid_<func> */ \
54+
extern u32 name; \
55+
asm ( \
56+
" .pushsection .data..ro_after_init,\"aw\",@progbits \n" \
57+
" .type " #name ",@object \n" \
58+
" .globl " #name " \n" \
59+
" .p2align 2, 0x0 \n" \
60+
#name ": \n" \
61+
" .4byte __kcfi_typeid_" #func " \n" \
62+
" .size " #name ", 4 \n" \
63+
" .popsection \n" \
64+
);
65+
#endif
66+
4467
#endif /* __ASSEMBLY__ */
4568
#endif /* _LINUX_CFI_TYPES_H */

0 commit comments

Comments
 (0)