]> granicus.if.org Git - strace/blob - tests/kexec_file_load.c
Update copyright headers
[strace] / tests / kexec_file_load.c
1 /*
2  * Check decoding of kexec_file_load syscall.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12 #include <asm/unistd.h>
13 #include "scno.h"
14
15 #ifdef __NR_kexec_file_load
16
17 # include <inttypes.h>
18 # include <stdio.h>
19 # include <unistd.h>
20
21 struct strval {
22         kernel_ulong_t val;
23         const char *str64;
24         const char *str32;
25         const char *str;
26 };
27
28 #define CMDLINE_STR "deadcodebaddatadefaced"
29
30 int
31 main(void)
32 {
33         static const kernel_ulong_t bogus_kernel_fd =
34                 (kernel_ulong_t) 0xdeadca57badda7a1ULL;
35         static const kernel_ulong_t bogus_initrd_fd =
36                 (kernel_ulong_t) 0xdec0ded1defaced2ULL;
37         static const char cmdline_str[] = CMDLINE_STR;
38         static const char cmdline_short_str[] = "abcdef";
39
40         static const kernel_ulong_t cmdline_lens[] = {
41                 0,
42                 (kernel_ulong_t) 0xcaffeeeddeadbeefULL,
43                 sizeof(cmdline_str),
44                 sizeof(cmdline_str) - 1,
45                 sizeof(cmdline_short_str),
46                 sizeof(cmdline_short_str) - 1,
47                 sizeof(cmdline_short_str) + 1,
48         };
49         static const struct strval flags[] = {
50                 { (kernel_ulong_t) 0xbadc0dedda7a1058ULL,
51                         "0xbadc0ded", "0x",
52                         "da7a1058 /* KEXEC_FILE_??? */" },
53                 { 0, "", "", "0" },
54                 { 0xdeadbeef, "", "", "KEXEC_FILE_UNLOAD|KEXEC_FILE_ON_CRASH|"
55                         "KEXEC_FILE_NO_INITRAMFS|0xdeadbee8" },
56         };
57
58
59         long rc;
60         char *cmdline = tail_memdup(cmdline_str, sizeof(cmdline_str));
61         char *cmdline_short =
62                 tail_memdup(cmdline_short_str, sizeof(cmdline_short_str));
63         char cmdline_ptr[sizeof("0x") + sizeof(void *) * 2];
64         char cmdline_short_ptr[sizeof("0x") + sizeof(void *) * 2];
65         unsigned int i;
66         unsigned int j;
67
68         struct strval cmdlines[] = {
69                 { (uintptr_t) NULL, "", "", "NULL" },
70                 { (uintptr_t) (cmdline + sizeof(cmdline_str)), "", "",
71                         cmdline_ptr },
72                 { (uintptr_t) cmdline, "", "", "\"deadcodeb\"..." },
73                 { (uintptr_t) cmdline, "", "", "\"deadcodeb\"..." },
74                 { (uintptr_t) cmdline_short, "", "", "\"abcdef\\0\"" },
75                 { (uintptr_t) cmdline_short, "", "", "\"abcdef\"" },
76                 { (uintptr_t) cmdline_short, "", "", cmdline_short_ptr },
77         };
78
79
80         snprintf(cmdline_ptr, sizeof(cmdline_ptr), "%p",
81                 cmdline + sizeof(cmdline_str));
82         snprintf(cmdline_short_ptr, sizeof(cmdline_short_ptr), "%p",
83                 cmdline_short);
84
85         for (i = 0; i < ARRAY_SIZE(flags); i++) {
86                 for (j = 0; j < ARRAY_SIZE(cmdlines); j++) {
87                         rc = syscall(__NR_kexec_file_load, bogus_kernel_fd,
88                                      bogus_initrd_fd, cmdline_lens[j],
89                                      cmdlines[j].val, flags[i].val);
90                         printf("kexec_file_load(%d, %d, %llu, %s, %s%s) = %s\n",
91                                (int) bogus_kernel_fd, (int) bogus_initrd_fd,
92                                (unsigned long long) cmdline_lens[j],
93                                cmdlines[j].str,
94                                sizeof(kernel_ulong_t) == 8 ? flags[i].str64 :
95                                flags[i].str32, flags[i].str, sprintrc(rc));
96                 }
97         }
98
99         puts("+++ exited with 0 +++");
100
101         return 0;
102 }
103
104 #else
105
106 SKIP_MAIN_UNDEFINED("__NR_kexec_file_load");
107
108 #endif