Debian Codegraph @

testsuite/systemtap.syscall/kexec_load.c

raw
1 /* COVERAGE: kexec_load kexec_file_load */
2
3 #define _GNU_SOURCE
4 #include <unistd.h>
5 #include <sys/syscall.h>
6 #include <linux/capability.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <linux/version.h>
12 #include <sys/mman.h>
13
14 #define capget(x,y) syscall(__NR_capget,x,y)
15 #define capset(x,y) syscall(__NR_capset,x,y)
16
17 static struct __user_cap_header_struct header;
18 static struct __user_cap_data_struct data;
19
20 // The linux/kexec.h header file was exported to userspace starting
21 // with kernel 3.7.
22 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
23 #include <linux/kexec.h>
24 #endif
25
26 // kexec flags for different usage scenarios
27 #ifndef KEXEC_ON_CRASH
28 #define KEXEC_ON_CRASH 0x00000001
29 #endif
30
31 // These values match the ELF architecture values.
32 #ifndef KEXEC_ARCH_DEFAULT
33 #define KEXEC_ARCH_DEFAULT ( 0 << 16)
34 #endif
35 #ifndef KEXEC_ARCH_386
36 #define KEXEC_ARCH_386 ( 3 << 16)
37 #endif
38
39 // These constants were added in kernel 3.17 for kexec_file_load().
40 #ifndef KEXEC_FILE_UNLOAD
41 #define KEXEC_FILE_UNLOAD 0x00000001
42 #endif
43 #ifndef KEXEC_FILE_ON_CRASH
44 #define KEXEC_FILE_ON_CRASH 0x00000002
45 #endif
46 #ifndef KEXEC_FILE_NO_INITRAMFS
47 #define KEXEC_FILE_NO_INITRAMFS 0x00000004
48 #endif
49
50 static inline int __kexec_load(unsigned long entry, unsigned long nr_segments,
51 void *segments, unsigned long flags)
52 {
53 return syscall(__NR_kexec_load, entry, nr_segments, segments, flags);
54 }
55
56 #ifdef __NR_kexec_file_load
57 static inline int __kexec_file_load(int kernel_fd, int initrd_fd,
58 unsigned long cmdline_len,
59 const char * cmdline, unsigned long flags)
60 {
61 return syscall(__NR_kexec_file_load, kernel_fd, initrd_fd, cmdline_len,
62 cmdline, flags);
63 }
64 #endif
65
66 int main()
67 {
68 mlockall(MCL_CURRENT);
69
70 #ifdef __NR_kexec_file_load
71 int fd;
72 char *cmdline = "KEYTABLE=us LANG=en_US.UTF-8";
73 #endif
74
75 // Ensure the syscall won't be able to succeed
76 header.version = _LINUX_CAPABILITY_VERSION;
77 header.pid = getpid();
78 capget(&header, &data);
79 data.effective &= ~(1 << CAP_SYS_BOOT);
80 capset(&header, &data);
81
82 // Test normal operation
83 __kexec_load(0, 0, NULL, KEXEC_ON_CRASH|KEXEC_ARCH_386);
84 //staptest// [[[[kexec_load (0x0, 0, 0x0, KEXEC_ON_CRASH|KEXEC_ARCH_386)!!!!ni_syscall ()]]]] = NNNN
85
86 // Limit testing
87 __kexec_load((unsigned long)-1, 0, NULL, KEXEC_ARCH_DEFAULT);
88 #if __WORDSIZE == 64
89 //staptest// [[[[kexec_load (0xffffffffffffffff, 0, 0x0, KEXEC_ARCH_DEFAULT)!!!!ni_syscall ()]]]] = -NNNN
90 #else
91 //staptest// [[[[kexec_load (0xffffffff, 0, 0x0, KEXEC_ARCH_DEFAULT)!!!!ni_syscall ()]]]] = -NNNN
92 #endif
93
94 __kexec_load(0, (unsigned long)-1, NULL, KEXEC_ARCH_DEFAULT);
95 #if __WORDSIZE == 64
96 //staptest// [[[[kexec_load (0x0, 18446744073709551615, 0x0, KEXEC_ARCH_DEFAULT)!!!!ni_syscall ()]]]] = -NNNN
97 #else
98 //staptest// [[[[kexec_load (0x0, 4294967295, 0x0, KEXEC_ARCH_DEFAULT)!!!!ni_syscall ()]]]] = -NNNN
99 #endif
100
101 __kexec_load(0, 0, (struct kexec_segment *)-1, KEXEC_ARCH_DEFAULT);
102 #ifdef __s390__
103 //staptest// kexec_load (0x0, 0, 0x[7]?[f]+, KEXEC_ARCH_DEFAULT) = -NNNN
104 #else
105 //staptest// [[[[kexec_load (0x0, 0, 0x[f]+, KEXEC_ARCH_DEFAULT)!!!!ni_syscall ()]]]] = -NNNN
106 #endif
107
108 __kexec_load(0, 0, NULL, (unsigned long)-1);
109 //staptest// [[[[kexec_load (0x0, 0, 0x0, KEXEC_ON_CRASH|[^)]+)!!!!ni_syscall ()]]]] = -NNNN
110
111 #ifdef __NR_kexec_file_load
112 /* Note that these calls won't succeed, since we dropped the
113 * CAP_SYS_BOOT capability. */
114
115 fd = open("/dev/null", O_RDONLY);
116 //staptest// [[[[open (!!!!openat (AT_FDCWD, ]]]]"/dev/null", O_RDONLY) = NNNN
117
118 __kexec_file_load(fd, fd, strlen(cmdline), cmdline, KEXEC_FILE_ON_CRASH);
119 //staptest// [[[[kexec_file_load (NNNN, NNNN, NNNN, "KEYTABLE=us LANG=en_US.UTF-8", KEXEC_FILE_ON_CRASH)!!!!ni_syscall ()]]]] = -NNNN
120
121 /* Limit testing. */
122 __kexec_file_load(-1, fd, strlen(cmdline), cmdline, KEXEC_FILE_UNLOAD);
123 //staptest// [[[[kexec_file_load (-1, NNNN, NNNN, "KEYTABLE=us LANG=en_US.UTF-8", KEXEC_FILE_UNLOAD)!!!!ni_syscall ()]]]] = -NNNN
124
125 __kexec_file_load(fd, -1, strlen(cmdline), cmdline, KEXEC_FILE_NO_INITRAMFS);
126 //staptest// [[[[kexec_file_load (NNNN, -1, NNNN, "KEYTABLE=us LANG=en_US.UTF-8", KEXEC_FILE_NO_INITRAMFS)!!!!ni_syscall ()]]]] = -NNNN
127
128 __kexec_file_load(fd, fd, -1, cmdline, KEXEC_FILE_ON_CRASH);
129 #if __WORDSIZE == 64
130 //staptest// [[[[kexec_file_load (NNNN, NNNN, 18446744073709551615, "KEYTABLE=us LANG=en_US.UTF-8", KEXEC_FILE_ON_CRASH)!!!!ni_syscall ()]]]] = -NNNN
131 #else
132 //staptest// [[[[kexec_file_load (NNNN, NNNN, 4294967295, "KEYTABLE=us LANG=en_US.UTF-8", KEXEC_FILE_ON_CRASH)!!!!ni_syscall ()]]]] = -NNNN
133 #endif
134
135 __kexec_file_load(fd, fd, strlen(cmdline), (char *)-1, KEXEC_FILE_ON_CRASH);
136 //staptest// [[[[kexec_file_load (NNNN, NNNN, NNNN, 0x[f]+, KEXEC_FILE_ON_CRASH)!!!!ni_syscall ()]]]] = -NNNN
137
138 __kexec_file_load(fd, fd, strlen(cmdline), cmdline, -1);
139 //staptest// [[[[kexec_file_load (NNNN, NNNN, NNNN, "KEYTABLE=us LANG=en_US.UTF-8", KEXEC_FILE_[^ ]+|XXXX)!!!!ni_syscall ()]]]] = -NNNN
140
141 close(fd);
142 #endif
143 return 0;
144 }