1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// SPDX-License-Identifier: GPL-2.0-only
#define __SANE_USERSPACE_TYPES__
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "pkey-helpers.h"
int iteration_nr = 1;
int test_nr;
int dprint_in_signal;
#if CONTROL_TRACING > 0
static void cat_into_file(char *str, char *file)
{
int fd = open(file, O_RDWR);
int ret;
dprintf2("%s(): writing '%s' to '%s'\n", __func__, str, file);
/*
* these need to be raw because they are called under
* pkey_assert()
*/
if (fd < 0) {
fprintf(stderr, "error opening '%s'\n", file);
perror("error: ");
exit(__LINE__);
}
ret = write(fd, str, strlen(str));
if (ret != strlen(str)) {
perror("write to file failed");
fprintf(stderr, "filename: '%s' str: '%s'\n", file, str);
exit(__LINE__);
}
close(fd);
}
static int warned_tracing;
static int tracing_root_ok(void)
{
if (geteuid() != 0) {
if (!warned_tracing)
fprintf(stderr, "WARNING: not run as root, "
"can not do tracing control\n");
warned_tracing = 1;
return 0;
}
return 1;
}
#endif
void tracing_on(void)
{
#if CONTROL_TRACING > 0
#define TRACEDIR "/sys/kernel/tracing"
char pidstr[32];
if (!tracing_root_ok())
return;
sprintf(pidstr, "%d", getpid());
cat_into_file("0", TRACEDIR "/tracing_on");
cat_into_file("\n", TRACEDIR "/trace");
if (1) {
cat_into_file("function_graph", TRACEDIR "/current_tracer");
cat_into_file("1", TRACEDIR "/options/funcgraph-proc");
} else {
cat_into_file("nop", TRACEDIR "/current_tracer");
}
cat_into_file(pidstr, TRACEDIR "/set_ftrace_pid");
cat_into_file("1", TRACEDIR "/tracing_on");
dprintf1("enabled tracing\n");
#endif
}
void tracing_off(void)
{
#if CONTROL_TRACING > 0
if (!tracing_root_ok())
return;
cat_into_file("0", "/sys/kernel/tracing/tracing_on");
#endif
}
void abort_hooks(void)
{
fflush(stdout);
fprintf(stderr, "running %s()...\n", __func__);
tracing_off();
#ifdef SLEEP_ON_ABORT
sleep(SLEEP_ON_ABORT);
#endif
}
int sys_pkey_alloc(unsigned long flags, unsigned long init_val)
{
int ret = syscall(SYS_pkey_alloc, flags, init_val);
dprintf1("%s(flags=%lx, init_val=%lx) syscall ret: %d errno: %d\n",
__func__, flags, init_val, ret, errno);
return ret;
}
int sys_pkey_free(unsigned long pkey)
{
int ret = syscall(SYS_pkey_free, pkey);
dprintf1("%s(pkey=%ld) syscall ret: %d\n", __func__, pkey, ret);
return ret;
}
int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
unsigned long pkey)
{
int sret;
dprintf2("%s(0x%p, %zx, prot=%lx, pkey=%lx)\n", __func__,
ptr, size, orig_prot, pkey);
errno = 0;
sret = syscall(__NR_pkey_mprotect, ptr, size, orig_prot, pkey);
if (errno) {
dprintf2("SYS_mprotect_key sret: %d\n", sret);
dprintf2("SYS_mprotect_key prot: 0x%lx\n", orig_prot);
dprintf2("SYS_mprotect_key failed, errno: %d\n", errno);
if (DEBUG_LEVEL >= 2)
perror("SYS_mprotect_pkey");
}
return sret;
}
|