xref: /aosp_15_r20/external/ltp/testcases/kernel/security/kallsyms/kallsyms.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Utilize kernel's symbol table for unauthorized address access.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Access the system symbols with root permission to test whether it's
12*49cdfc7eSAndroid Build Coastguard Worker  * possible to read and write the memory addresses of kernel-space
13*49cdfc7eSAndroid Build Coastguard Worker  * from user-space. This helps in identifying potential vulnerabilities
14*49cdfc7eSAndroid Build Coastguard Worker  * where user-space processes can inappropriately access kernel memory.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * Steps:
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  *  1. Start a process that reads all symbols and their addresses from
19*49cdfc7eSAndroid Build Coastguard Worker  *     /proc/kallsyms and stores them in a linked list.
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  *  2. Attempt to write to each kernel address found in the linked list.
22*49cdfc7eSAndroid Build Coastguard Worker  *     The expectation is that each attempt will fail with a SIGSEGV
23*49cdfc7eSAndroid Build Coastguard Worker  *     (segmentation fault), indicating that the user-space process
24*49cdfc7eSAndroid Build Coastguard Worker  *     cannot write to kernel memory.
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  *  3. Handle each SIGSEGV using a signal handler that sets a flag and
27*49cdfc7eSAndroid Build Coastguard Worker  *     long jumps out of the faulting context.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  *  4. If any write operation does not result in a SIGSEGV, log this as
30*49cdfc7eSAndroid Build Coastguard Worker  *     a potential security vulnerability.
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  *  5. Observe and log the behavior and any system responses to these
33*49cdfc7eSAndroid Build Coastguard Worker  *     unauthorized access attempts.
34*49cdfc7eSAndroid Build Coastguard Worker  */
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <assert.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <setjmp.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
45*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_stdio.h"
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker struct kallsym {
48*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long addr;
49*49cdfc7eSAndroid Build Coastguard Worker 	char type;
50*49cdfc7eSAndroid Build Coastguard Worker 	char name[128];
51*49cdfc7eSAndroid Build Coastguard Worker };
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static struct kallsym *sym_table;
54*49cdfc7eSAndroid Build Coastguard Worker static unsigned int nr_symbols;
55*49cdfc7eSAndroid Build Coastguard Worker static sigjmp_buf jmpbuf;
56*49cdfc7eSAndroid Build Coastguard Worker volatile sig_atomic_t segv_caught;
57*49cdfc7eSAndroid Build Coastguard Worker 
segv_handler(int sig)58*49cdfc7eSAndroid Build Coastguard Worker static void segv_handler(int sig)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	if (sig == SIGSEGV)
61*49cdfc7eSAndroid Build Coastguard Worker 		segv_caught++;
62*49cdfc7eSAndroid Build Coastguard Worker 	else
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Unexpected signal %s", strsignal(sig));
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	siglongjmp(jmpbuf, 1);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker 
read_kallsyms(struct kallsym * table,unsigned int table_size)68*49cdfc7eSAndroid Build Coastguard Worker static unsigned int read_kallsyms(struct kallsym *table, unsigned int table_size)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker 	char *line = NULL;
71*49cdfc7eSAndroid Build Coastguard Worker 	size_t len = 0;
72*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int nr_syms = 0;
73*49cdfc7eSAndroid Build Coastguard Worker 	FILE *stream = SAFE_FOPEN("/proc/kallsyms", "r");
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	while (getline(&line, &len, stream) != -1) {
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 		if (table && nr_syms < table_size) {
78*49cdfc7eSAndroid Build Coastguard Worker 			sscanf(line, "%lx %c %s",
79*49cdfc7eSAndroid Build Coastguard Worker 					&table[nr_syms].addr,
80*49cdfc7eSAndroid Build Coastguard Worker 					&table[nr_syms].type,
81*49cdfc7eSAndroid Build Coastguard Worker 					table[nr_syms].name);
82*49cdfc7eSAndroid Build Coastguard Worker 		}
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 		nr_syms++;
85*49cdfc7eSAndroid Build Coastguard Worker 	}
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FCLOSE(stream);
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	return nr_syms;
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)92*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sa;
95*49cdfc7eSAndroid Build Coastguard Worker 	memset(&sa, 0, sizeof(sa));
96*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_handler = segv_handler;
97*49cdfc7eSAndroid Build Coastguard Worker 	sigaction(SIGSEGV, &sa, NULL);
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	nr_symbols = read_kallsyms(NULL, 0);
100*49cdfc7eSAndroid Build Coastguard Worker 	sym_table = SAFE_CALLOC(nr_symbols, sizeof(*sym_table));
101*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int read_symbols = read_kallsyms(sym_table, nr_symbols);
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	if (nr_symbols != read_symbols)
104*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN, "/proc/kallsyms changed size!?");
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker 
access_ksymbols_address(struct kallsym * table)107*49cdfc7eSAndroid Build Coastguard Worker static void access_ksymbols_address(struct kallsym *table)
108*49cdfc7eSAndroid Build Coastguard Worker {
109*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TDEBUG, "Access kernel addr: 0x%lx (%c) (%s)",
110*49cdfc7eSAndroid Build Coastguard Worker 				table->addr, table->type, table->name);
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	if (sigsetjmp(jmpbuf, 1) == 0) {
113*49cdfc7eSAndroid Build Coastguard Worker 		*(volatile unsigned long *)table->addr = 0;
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Successfully accessed kernel addr 0x%lx (%c) (%s)",
116*49cdfc7eSAndroid Build Coastguard Worker 				table->addr, table->type, table->name);
117*49cdfc7eSAndroid Build Coastguard Worker 	}
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker 
test_access_kernel_address(void)120*49cdfc7eSAndroid Build Coastguard Worker static void test_access_kernel_address(void)
121*49cdfc7eSAndroid Build Coastguard Worker {
122*49cdfc7eSAndroid Build Coastguard Worker 	segv_caught = 0;
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	for (unsigned int i = 0; i < nr_symbols; i++)
125*49cdfc7eSAndroid Build Coastguard Worker 		access_ksymbols_address(&sym_table[i]);
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	if (segv_caught == (sig_atomic_t)nr_symbols)
128*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Caught %d SIGSEGV in access ksymbols addr", segv_caught);
129*49cdfc7eSAndroid Build Coastguard Worker 	else
130*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Caught %d SIGSEGV but expected %d", segv_caught, nr_symbols);
131*49cdfc7eSAndroid Build Coastguard Worker }
132*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)133*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
134*49cdfc7eSAndroid Build Coastguard Worker {
135*49cdfc7eSAndroid Build Coastguard Worker 	if (sym_table)
136*49cdfc7eSAndroid Build Coastguard Worker 		free(sym_table);
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
140*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
141*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
142*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
143*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = 60,
144*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_access_kernel_address,
145*49cdfc7eSAndroid Build Coastguard Worker };
146