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