2 * Check decoding of kexec_load syscall.
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <asm/unistd.h>
34 #ifdef __NR_kexec_load
39 # include "kernel_types.h"
61 NUM_SEGMS_UNCUT_MAX = 9,
63 SEGMS_ARRAY_SIZE = sizeof(struct segm) * NUM_SEGMS,
66 static const kernel_ulong_t bogus_zero =
67 sizeof(long) < sizeof(kernel_long_t) ?
68 (kernel_ulong_t) 0xffffffff00000000ULL : 0;
69 static const kernel_ulong_t bogus_entry =
70 (kernel_ulong_t) 0xdeadca57badda7a1ULL;
71 static const kernel_ulong_t bogus_nsegs =
72 (kernel_ulong_t) 0xdec0ded1defaced2ULL;
74 static const struct strval flags[] = {
75 { (kernel_ulong_t) 0xbadc0dedda7a1054ULL,
76 "0xda7a0000 /* KEXEC_ARCH_??? */|0xbadc0ded0000",
77 "0xda7a0000 /* KEXEC_ARCH_??? */|0x",
78 "1054 /* KEXEC_??? */" },
79 { 0, "", "", "KEXEC_ARCH_DEFAULT" },
81 "KEXEC_ARCH_SH|KEXEC_ON_CRASH|KEXEC_PRESERVE_CONTEXT" },
82 { 0xdead0000, "", "", "0xdead0000 /* KEXEC_ARCH_??? */" },
87 struct segm *segms = tail_alloc(SEGMS_ARRAY_SIZE);
90 fill_memory((char *) segms, SEGMS_ARRAY_SIZE);
91 segms[0].buf = segms[0].mem = NULL;
93 rc = syscall(__NR_kexec_load, bogus_zero, bogus_zero, bogus_zero,
95 printf("kexec_load(NULL, 0, NULL, %s%s) = %s\n",
96 sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
97 flags[0].str, sprintrc(rc));
99 rc = syscall(__NR_kexec_load, bogus_entry, bogus_nsegs,
100 segms + SEGMS_ARRAY_SIZE, flags[1].val);
101 printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
102 (unsigned long) bogus_entry, (unsigned long) bogus_nsegs,
103 segms + SEGMS_ARRAY_SIZE, flags[1].str, sprintrc(rc));
105 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS,
106 segms, flags[2].val);
107 printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
108 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS,
109 segms, flags[2].str, sprintrc(rc));
111 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
112 segms, flags[3].val);
113 errstr = sprintrc(rc);
114 printf("kexec_load(%#lx, %lu, [{buf=NULL, bufsz=%zu, mem=NULL, "
116 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT,
117 segms[0].bufsz, segms[0].memsz);
118 for (i = 1; i < NUM_SEGMS_UNCUT_MAX; i++)
119 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
120 segms[i].buf, segms[i].bufsz,
121 segms[i].mem, segms[i].memsz);
122 printf("...], %s) = %s\n", flags[3].str, errstr);
124 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
125 segms + (NUM_SEGMS - NUM_SEGMS_UNCUT_MAX),
127 errstr = sprintrc(rc);
128 printf("kexec_load(%#lx, %lu, [",
129 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
130 for (i = NUM_SEGMS - NUM_SEGMS_UNCUT_MAX; i < NUM_SEGMS; i++)
131 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
132 segms[i].buf, segms[i].bufsz,
133 segms[i].mem, segms[i].memsz);
134 printf("%p], %s%s) = %s\n",
136 sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
137 flags[0].str, errstr);
139 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_UNCUT,
140 segms + (NUM_SEGMS - NUM_SEGMS_UNCUT),
142 errstr = sprintrc(rc);
143 printf("kexec_load(%#lx, %lu, [",
144 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_UNCUT);
145 for (i = NUM_SEGMS - NUM_SEGMS_UNCUT; i < NUM_SEGMS; i++)
146 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}%s",
147 segms[i].buf, segms[i].bufsz,
148 segms[i].mem, segms[i].memsz,
149 (i == NUM_SEGMS - 1) ? "" : ", ");
150 printf("], %s) = %s\n", flags[1].str, errstr);
152 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
153 segms + 1, flags[2].val);
154 errstr = sprintrc(rc);
155 printf("kexec_load(%#lx, %lu, [",
156 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
157 for (i = 1; i < NUM_SEGMS_UNCUT_MAX + 1; i++)
158 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
159 segms[i].buf, segms[i].bufsz,
160 segms[i].mem, segms[i].memsz);
161 printf("...], %s) = %s\n", flags[2].str, errstr);
163 puts("+++ exited with 0 +++");
170 SKIP_MAIN_UNDEFINED("__NR_kexec_load");