1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Check decoding of kexec_load syscall.
3*cf84ac9aSAndroid Build Coastguard Worker *
4*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2016 Eugene Syromyatnikov <[email protected]>
5*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2016-2018 The strace developers.
6*cf84ac9aSAndroid Build Coastguard Worker * All rights reserved.
7*cf84ac9aSAndroid Build Coastguard Worker *
8*cf84ac9aSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
9*cf84ac9aSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
10*cf84ac9aSAndroid Build Coastguard Worker * are met:
11*cf84ac9aSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
12*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
13*cf84ac9aSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
14*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
15*cf84ac9aSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
16*cf84ac9aSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
17*cf84ac9aSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
18*cf84ac9aSAndroid Build Coastguard Worker *
19*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*cf84ac9aSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*cf84ac9aSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*cf84ac9aSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*cf84ac9aSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*cf84ac9aSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*cf84ac9aSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*cf84ac9aSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*cf84ac9aSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*cf84ac9aSAndroid Build Coastguard Worker */
30*cf84ac9aSAndroid Build Coastguard Worker
31*cf84ac9aSAndroid Build Coastguard Worker #include "tests.h"
32*cf84ac9aSAndroid Build Coastguard Worker
33*cf84ac9aSAndroid Build Coastguard Worker #include <asm/unistd.h>
34*cf84ac9aSAndroid Build Coastguard Worker
35*cf84ac9aSAndroid Build Coastguard Worker #ifdef __NR_kexec_load
36*cf84ac9aSAndroid Build Coastguard Worker
37*cf84ac9aSAndroid Build Coastguard Worker # include <stdio.h>
38*cf84ac9aSAndroid Build Coastguard Worker # include <unistd.h>
39*cf84ac9aSAndroid Build Coastguard Worker
40*cf84ac9aSAndroid Build Coastguard Worker struct strval {
41*cf84ac9aSAndroid Build Coastguard Worker kernel_ulong_t val;
42*cf84ac9aSAndroid Build Coastguard Worker const char *str64;
43*cf84ac9aSAndroid Build Coastguard Worker const char *str32;
44*cf84ac9aSAndroid Build Coastguard Worker const char *str;
45*cf84ac9aSAndroid Build Coastguard Worker };
46*cf84ac9aSAndroid Build Coastguard Worker
47*cf84ac9aSAndroid Build Coastguard Worker struct segm {
48*cf84ac9aSAndroid Build Coastguard Worker void *buf;
49*cf84ac9aSAndroid Build Coastguard Worker size_t bufsz;
50*cf84ac9aSAndroid Build Coastguard Worker void *mem;
51*cf84ac9aSAndroid Build Coastguard Worker size_t memsz;
52*cf84ac9aSAndroid Build Coastguard Worker };
53*cf84ac9aSAndroid Build Coastguard Worker
54*cf84ac9aSAndroid Build Coastguard Worker int
main(void)55*cf84ac9aSAndroid Build Coastguard Worker main(void)
56*cf84ac9aSAndroid Build Coastguard Worker {
57*cf84ac9aSAndroid Build Coastguard Worker enum {
58*cf84ac9aSAndroid Build Coastguard Worker NUM_SEGMS = 17,
59*cf84ac9aSAndroid Build Coastguard Worker NUM_SEGMS_UNCUT = 5,
60*cf84ac9aSAndroid Build Coastguard Worker NUM_SEGMS_UNCUT_MAX = 9,
61*cf84ac9aSAndroid Build Coastguard Worker NUM_SEGMS_CUT = 12,
62*cf84ac9aSAndroid Build Coastguard Worker SEGMS_ARRAY_SIZE = sizeof(struct segm) * NUM_SEGMS,
63*cf84ac9aSAndroid Build Coastguard Worker };
64*cf84ac9aSAndroid Build Coastguard Worker
65*cf84ac9aSAndroid Build Coastguard Worker static const kernel_ulong_t bogus_zero =
66*cf84ac9aSAndroid Build Coastguard Worker sizeof(long) < sizeof(kernel_long_t) ? F8ILL_KULONG_MASK : 0;
67*cf84ac9aSAndroid Build Coastguard Worker static const kernel_ulong_t bogus_entry =
68*cf84ac9aSAndroid Build Coastguard Worker (kernel_ulong_t) 0xdeadca57badda7a1ULL;
69*cf84ac9aSAndroid Build Coastguard Worker static const kernel_ulong_t bogus_nsegs =
70*cf84ac9aSAndroid Build Coastguard Worker (kernel_ulong_t) 0xdec0ded1defaced2ULL;
71*cf84ac9aSAndroid Build Coastguard Worker
72*cf84ac9aSAndroid Build Coastguard Worker static const struct strval flags[] = {
73*cf84ac9aSAndroid Build Coastguard Worker { (kernel_ulong_t) 0xbadc0dedda7a1054ULL,
74*cf84ac9aSAndroid Build Coastguard Worker "0xda7a0000 /* KEXEC_ARCH_??? */|0xbadc0ded0000",
75*cf84ac9aSAndroid Build Coastguard Worker "0xda7a0000 /* KEXEC_ARCH_??? */|0x",
76*cf84ac9aSAndroid Build Coastguard Worker "1054 /* KEXEC_??? */" },
77*cf84ac9aSAndroid Build Coastguard Worker { 0, "", "", "KEXEC_ARCH_DEFAULT" },
78*cf84ac9aSAndroid Build Coastguard Worker { 0x2a0003, "", "",
79*cf84ac9aSAndroid Build Coastguard Worker "KEXEC_ARCH_SH|KEXEC_ON_CRASH|KEXEC_PRESERVE_CONTEXT" },
80*cf84ac9aSAndroid Build Coastguard Worker { 0xdead0000, "", "", "0xdead0000 /* KEXEC_ARCH_??? */" },
81*cf84ac9aSAndroid Build Coastguard Worker };
82*cf84ac9aSAndroid Build Coastguard Worker
83*cf84ac9aSAndroid Build Coastguard Worker const char *errstr;
84*cf84ac9aSAndroid Build Coastguard Worker long rc;
85*cf84ac9aSAndroid Build Coastguard Worker struct segm *segms = tail_alloc(SEGMS_ARRAY_SIZE);
86*cf84ac9aSAndroid Build Coastguard Worker unsigned int i;
87*cf84ac9aSAndroid Build Coastguard Worker
88*cf84ac9aSAndroid Build Coastguard Worker fill_memory(segms, SEGMS_ARRAY_SIZE);
89*cf84ac9aSAndroid Build Coastguard Worker segms[0].buf = segms[0].mem = NULL;
90*cf84ac9aSAndroid Build Coastguard Worker
91*cf84ac9aSAndroid Build Coastguard Worker rc = syscall(__NR_kexec_load, bogus_zero, bogus_zero, bogus_zero,
92*cf84ac9aSAndroid Build Coastguard Worker flags[0].val);
93*cf84ac9aSAndroid Build Coastguard Worker printf("kexec_load(NULL, 0, NULL, %s%s) = %s\n",
94*cf84ac9aSAndroid Build Coastguard Worker sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
95*cf84ac9aSAndroid Build Coastguard Worker flags[0].str, sprintrc(rc));
96*cf84ac9aSAndroid Build Coastguard Worker
97*cf84ac9aSAndroid Build Coastguard Worker rc = syscall(__NR_kexec_load, bogus_entry, bogus_nsegs,
98*cf84ac9aSAndroid Build Coastguard Worker segms + SEGMS_ARRAY_SIZE, flags[1].val);
99*cf84ac9aSAndroid Build Coastguard Worker printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
100*cf84ac9aSAndroid Build Coastguard Worker (unsigned long) bogus_entry, (unsigned long) bogus_nsegs,
101*cf84ac9aSAndroid Build Coastguard Worker segms + SEGMS_ARRAY_SIZE, flags[1].str, sprintrc(rc));
102*cf84ac9aSAndroid Build Coastguard Worker
103*cf84ac9aSAndroid Build Coastguard Worker rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS,
104*cf84ac9aSAndroid Build Coastguard Worker segms, flags[2].val);
105*cf84ac9aSAndroid Build Coastguard Worker printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
106*cf84ac9aSAndroid Build Coastguard Worker (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS,
107*cf84ac9aSAndroid Build Coastguard Worker segms, flags[2].str, sprintrc(rc));
108*cf84ac9aSAndroid Build Coastguard Worker
109*cf84ac9aSAndroid Build Coastguard Worker rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
110*cf84ac9aSAndroid Build Coastguard Worker segms, flags[3].val);
111*cf84ac9aSAndroid Build Coastguard Worker errstr = sprintrc(rc);
112*cf84ac9aSAndroid Build Coastguard Worker printf("kexec_load(%#lx, %lu, [{buf=NULL, bufsz=%zu, mem=NULL, "
113*cf84ac9aSAndroid Build Coastguard Worker "memsz=%zu}, ",
114*cf84ac9aSAndroid Build Coastguard Worker (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT,
115*cf84ac9aSAndroid Build Coastguard Worker segms[0].bufsz, segms[0].memsz);
116*cf84ac9aSAndroid Build Coastguard Worker for (i = 1; i < NUM_SEGMS_UNCUT_MAX; i++)
117*cf84ac9aSAndroid Build Coastguard Worker printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
118*cf84ac9aSAndroid Build Coastguard Worker segms[i].buf, segms[i].bufsz,
119*cf84ac9aSAndroid Build Coastguard Worker segms[i].mem, segms[i].memsz);
120*cf84ac9aSAndroid Build Coastguard Worker printf("...], %s) = %s\n", flags[3].str, errstr);
121*cf84ac9aSAndroid Build Coastguard Worker
122*cf84ac9aSAndroid Build Coastguard Worker rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
123*cf84ac9aSAndroid Build Coastguard Worker segms + (NUM_SEGMS - NUM_SEGMS_UNCUT_MAX),
124*cf84ac9aSAndroid Build Coastguard Worker flags[0].val);
125*cf84ac9aSAndroid Build Coastguard Worker errstr = sprintrc(rc);
126*cf84ac9aSAndroid Build Coastguard Worker printf("kexec_load(%#lx, %lu, [",
127*cf84ac9aSAndroid Build Coastguard Worker (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
128*cf84ac9aSAndroid Build Coastguard Worker for (i = NUM_SEGMS - NUM_SEGMS_UNCUT_MAX; i < NUM_SEGMS; i++)
129*cf84ac9aSAndroid Build Coastguard Worker printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
130*cf84ac9aSAndroid Build Coastguard Worker segms[i].buf, segms[i].bufsz,
131*cf84ac9aSAndroid Build Coastguard Worker segms[i].mem, segms[i].memsz);
132*cf84ac9aSAndroid Build Coastguard Worker printf("... /* %p */], %s%s) = %s\n",
133*cf84ac9aSAndroid Build Coastguard Worker segms + NUM_SEGMS,
134*cf84ac9aSAndroid Build Coastguard Worker sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
135*cf84ac9aSAndroid Build Coastguard Worker flags[0].str, errstr);
136*cf84ac9aSAndroid Build Coastguard Worker
137*cf84ac9aSAndroid Build Coastguard Worker rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_UNCUT,
138*cf84ac9aSAndroid Build Coastguard Worker segms + (NUM_SEGMS - NUM_SEGMS_UNCUT),
139*cf84ac9aSAndroid Build Coastguard Worker flags[1].val);
140*cf84ac9aSAndroid Build Coastguard Worker errstr = sprintrc(rc);
141*cf84ac9aSAndroid Build Coastguard Worker printf("kexec_load(%#lx, %lu, [",
142*cf84ac9aSAndroid Build Coastguard Worker (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_UNCUT);
143*cf84ac9aSAndroid Build Coastguard Worker for (i = NUM_SEGMS - NUM_SEGMS_UNCUT; i < NUM_SEGMS; i++)
144*cf84ac9aSAndroid Build Coastguard Worker printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}%s",
145*cf84ac9aSAndroid Build Coastguard Worker segms[i].buf, segms[i].bufsz,
146*cf84ac9aSAndroid Build Coastguard Worker segms[i].mem, segms[i].memsz,
147*cf84ac9aSAndroid Build Coastguard Worker (i == NUM_SEGMS - 1) ? "" : ", ");
148*cf84ac9aSAndroid Build Coastguard Worker printf("], %s) = %s\n", flags[1].str, errstr);
149*cf84ac9aSAndroid Build Coastguard Worker
150*cf84ac9aSAndroid Build Coastguard Worker rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
151*cf84ac9aSAndroid Build Coastguard Worker segms + 1, flags[2].val);
152*cf84ac9aSAndroid Build Coastguard Worker errstr = sprintrc(rc);
153*cf84ac9aSAndroid Build Coastguard Worker printf("kexec_load(%#lx, %lu, [",
154*cf84ac9aSAndroid Build Coastguard Worker (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
155*cf84ac9aSAndroid Build Coastguard Worker for (i = 1; i < NUM_SEGMS_UNCUT_MAX + 1; i++)
156*cf84ac9aSAndroid Build Coastguard Worker printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
157*cf84ac9aSAndroid Build Coastguard Worker segms[i].buf, segms[i].bufsz,
158*cf84ac9aSAndroid Build Coastguard Worker segms[i].mem, segms[i].memsz);
159*cf84ac9aSAndroid Build Coastguard Worker printf("...], %s) = %s\n", flags[2].str, errstr);
160*cf84ac9aSAndroid Build Coastguard Worker
161*cf84ac9aSAndroid Build Coastguard Worker puts("+++ exited with 0 +++");
162*cf84ac9aSAndroid Build Coastguard Worker
163*cf84ac9aSAndroid Build Coastguard Worker return 0;
164*cf84ac9aSAndroid Build Coastguard Worker }
165*cf84ac9aSAndroid Build Coastguard Worker
166*cf84ac9aSAndroid Build Coastguard Worker #else
167*cf84ac9aSAndroid Build Coastguard Worker
168*cf84ac9aSAndroid Build Coastguard Worker SKIP_MAIN_UNDEFINED("__NR_kexec_load");
169*cf84ac9aSAndroid Build Coastguard Worker
170*cf84ac9aSAndroid Build Coastguard Worker #endif
171