2 * Check decoding of kexec_file_load syscall.
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * Copyright (c) 2016-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <asm/unistd.h>
35 #ifdef __NR_kexec_file_load
37 # include <inttypes.h>
48 #define CMDLINE_STR "deadcodebaddatadefaced"
53 static const kernel_ulong_t bogus_kernel_fd =
54 (kernel_ulong_t) 0xdeadca57badda7a1ULL;
55 static const kernel_ulong_t bogus_initrd_fd =
56 (kernel_ulong_t) 0xdec0ded1defaced2ULL;
57 static const char cmdline_str[] = CMDLINE_STR;
58 static const char cmdline_short_str[] = "abcdef";
60 static const kernel_ulong_t cmdline_lens[] = {
62 (kernel_ulong_t) 0xcaffeeeddeadbeefULL,
64 sizeof(cmdline_str) - 1,
65 sizeof(cmdline_short_str),
66 sizeof(cmdline_short_str) - 1,
67 sizeof(cmdline_short_str) + 1,
69 static const struct strval flags[] = {
70 { (kernel_ulong_t) 0xbadc0dedda7a1058ULL,
72 "da7a1058 /* KEXEC_FILE_??? */" },
74 { 0xdeadbeef, "", "", "KEXEC_FILE_UNLOAD|KEXEC_FILE_ON_CRASH|"
75 "KEXEC_FILE_NO_INITRAMFS|0xdeadbee8" },
80 char *cmdline = tail_memdup(cmdline_str, sizeof(cmdline_str));
82 tail_memdup(cmdline_short_str, sizeof(cmdline_short_str));
83 char cmdline_ptr[sizeof("0x") + sizeof(void *) * 2];
84 char cmdline_short_ptr[sizeof("0x") + sizeof(void *) * 2];
88 struct strval cmdlines[] = {
89 { (uintptr_t) NULL, "", "", "NULL" },
90 { (uintptr_t) (cmdline + sizeof(cmdline_str)), "", "",
92 { (uintptr_t) cmdline, "", "", "\"deadcodeb\"..." },
93 { (uintptr_t) cmdline, "", "", "\"deadcodeb\"..." },
94 { (uintptr_t) cmdline_short, "", "", "\"abcdef\\0\"" },
95 { (uintptr_t) cmdline_short, "", "", "\"abcdef\"" },
96 { (uintptr_t) cmdline_short, "", "", cmdline_short_ptr },
100 snprintf(cmdline_ptr, sizeof(cmdline_ptr), "%p",
101 cmdline + sizeof(cmdline_str));
102 snprintf(cmdline_short_ptr, sizeof(cmdline_short_ptr), "%p",
105 for (i = 0; i < ARRAY_SIZE(flags); i++) {
106 for (j = 0; j < ARRAY_SIZE(cmdlines); j++) {
107 rc = syscall(__NR_kexec_file_load, bogus_kernel_fd,
108 bogus_initrd_fd, cmdline_lens[j],
109 cmdlines[j].val, flags[i].val);
110 printf("kexec_file_load(%d, %d, %llu, %s, %s%s) = %s\n",
111 (int) bogus_kernel_fd, (int) bogus_initrd_fd,
112 (unsigned long long) cmdline_lens[j],
114 sizeof(kernel_ulong_t) == 8 ? flags[i].str64 :
115 flags[i].str32, flags[i].str, sprintrc(rc));
119 puts("+++ exited with 0 +++");
126 SKIP_MAIN_UNDEFINED("__NR_kexec_file_load");