1 /*
2 * Check decoding of kexec_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 #include <stdio.h>
15 #include <unistd.h>
16
17 struct strval {
18 kernel_ulong_t val;
19 const char *str64;
20 const char *str32;
21 const char *str;
22 };
23
24 struct segm {
25 void *buf;
26 size_t bufsz;
27 void *mem;
28 size_t memsz;
29 };
30
31 int
32 main(void)
33 {
34 enum {
35 NUM_SEGMS = 17,
36 NUM_SEGMS_UNCUT = 5,
37 NUM_SEGMS_UNCUT_MAX = 9,
38 NUM_SEGMS_CUT = 12,
39 SEGMS_ARRAY_SIZE = sizeof(struct segm) * NUM_SEGMS,
40 };
41
42 static const kernel_ulong_t bogus_zero =
43 sizeof(long) < sizeof(kernel_long_t) ? F8ILL_KULONG_MASK : 0;
44 static const kernel_ulong_t bogus_entry =
45 (kernel_ulong_t) 0xdeadca57badda7a1ULL;
46 static const kernel_ulong_t bogus_nsegs =
47 (kernel_ulong_t) 0xdec0ded1defaced2ULL;
48
49 static const struct strval flags[] = {
50 { (kernel_ulong_t) 0xbadc0dedda7a1054ULL,
51 "0xda7a0000 /* KEXEC_ARCH_??? */|0xbadc0ded0000",
52 "0xda7a0000 /* KEXEC_ARCH_??? */|0x",
53 "1054 /* KEXEC_??? */" },
54 { 0, "", "", "KEXEC_ARCH_DEFAULT" },
55 { 0x2a0003, "", "",
56 "KEXEC_ARCH_SH|KEXEC_ON_CRASH|KEXEC_PRESERVE_CONTEXT" },
57 { 0xdead0000, "", "", "0xdead0000 /* KEXEC_ARCH_??? */" },
58 };
59
60 const char *errstr;
61 long rc;
62 struct segm *segms = tail_alloc(SEGMS_ARRAY_SIZE);
63
64 fill_memory(segms, SEGMS_ARRAY_SIZE);
65 segms[0].buf = segms[0].mem = NULL;
66
67 rc = syscall(__NR_kexec_load, bogus_zero, bogus_zero, bogus_zero,
68 flags[0].val);
69 printf("kexec_load(NULL, 0, NULL, %s%s) = %s\n",
70 sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
71 flags[0].str, sprintrc(rc));
72
73 rc = syscall(__NR_kexec_load, bogus_entry, bogus_nsegs,
74 segms + SEGMS_ARRAY_SIZE, flags[1].val);
75 printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
76 (unsigned long) bogus_entry, (unsigned long) bogus_nsegs,
77 segms + SEGMS_ARRAY_SIZE, flags[1].str, sprintrc(rc));
78
79 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS,
80 segms, flags[2].val);
81 printf("kexec_load(%#lx, %lu, %p, %s) = %s\n",
82 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS,
83 segms, flags[2].str, sprintrc(rc));
84
85 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
86 segms, flags[3].val);
87 errstr = sprintrc(rc);
88 printf("kexec_load(%#lx, %lu, [{buf=NULL, bufsz=%zu, mem=NULL, "
89 "memsz=%zu}, ",
90 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT,
91 segms[0].bufsz, segms[0].memsz);
92 for (unsigned int i = 1; i < NUM_SEGMS_UNCUT_MAX; ++i)
93 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
94 segms[i].buf, segms[i].bufsz,
95 segms[i].mem, segms[i].memsz);
96 printf("...], %s) = %s\n", flags[3].str, errstr);
97
98 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
99 segms + (NUM_SEGMS - NUM_SEGMS_UNCUT_MAX),
100 flags[0].val);
101 errstr = sprintrc(rc);
102 printf("kexec_load(%#lx, %lu, [",
103 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
104 for (unsigned int i = NUM_SEGMS - NUM_SEGMS_UNCUT_MAX;
105 i < NUM_SEGMS; ++i)
106 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
107 segms[i].buf, segms[i].bufsz,
108 segms[i].mem, segms[i].memsz);
109 printf("... /* %p */], %s%s) = %s\n",
110 segms + NUM_SEGMS,
111 sizeof(long) == 8 ? flags[0].str64 : flags[0].str32,
112 flags[0].str, errstr);
113
114 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_UNCUT,
115 segms + (NUM_SEGMS - NUM_SEGMS_UNCUT),
116 flags[1].val);
117 errstr = sprintrc(rc);
118 printf("kexec_load(%#lx, %lu, [",
119 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_UNCUT);
120 for (unsigned int i = NUM_SEGMS - NUM_SEGMS_UNCUT; i < NUM_SEGMS; ++i)
121 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}%s",
122 segms[i].buf, segms[i].bufsz,
123 segms[i].mem, segms[i].memsz,
124 (i == NUM_SEGMS - 1) ? "" : ", ");
125 printf("], %s) = %s\n", flags[1].str, errstr);
126
127 rc = syscall(__NR_kexec_load, bogus_entry, NUM_SEGMS_CUT,
128 segms + 1, flags[2].val);
129 errstr = sprintrc(rc);
130 printf("kexec_load(%#lx, %lu, [",
131 (unsigned long) bogus_entry, (unsigned long) NUM_SEGMS_CUT);
132 for (unsigned int i = 1; i < NUM_SEGMS_UNCUT_MAX + 1; ++i)
133 printf("{buf=%p, bufsz=%zu, mem=%p, memsz=%zu}, ",
134 segms[i].buf, segms[i].bufsz,
135 segms[i].mem, segms[i].memsz);
136 printf("...], %s) = %s\n", flags[2].str, errstr);
137
138 puts("+++ exited with 0 +++");
139
140 return 0;
141 }