• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check decoding of kexec_file_load syscall.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * Copyright (c) 2016-2017 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
18  *
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.
29  */
30 
31 #include "tests.h"
32 #include <asm/unistd.h>
33 #include "scno.h"
34 
35 #ifdef __NR_kexec_file_load
36 
37 # include <inttypes.h>
38 # include <stdio.h>
39 # include <unistd.h>
40 
41 struct strval {
42 	kernel_ulong_t val;
43 	const char *str64;
44 	const char *str32;
45 	const char *str;
46 };
47 
48 #define CMDLINE_STR "deadcodebaddatadefaced"
49 
50 int
main(void)51 main(void)
52 {
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";
59 
60 	static const kernel_ulong_t cmdline_lens[] = {
61 		0,
62 		(kernel_ulong_t) 0xcaffeeeddeadbeefULL,
63 		sizeof(cmdline_str),
64 		sizeof(cmdline_str) - 1,
65 		sizeof(cmdline_short_str),
66 		sizeof(cmdline_short_str) - 1,
67 		sizeof(cmdline_short_str) + 1,
68 	};
69 	static const struct strval flags[] = {
70 		{ (kernel_ulong_t) 0xbadc0dedda7a1058ULL,
71 			"0xbadc0ded", "0x",
72 			"da7a1058 /* KEXEC_FILE_??? */" },
73 		{ 0, "", "", "0" },
74 		{ 0xdeadbeef, "", "", "KEXEC_FILE_UNLOAD|KEXEC_FILE_ON_CRASH|"
75 			"KEXEC_FILE_NO_INITRAMFS|0xdeadbee8" },
76 	};
77 
78 
79 	long rc;
80 	char *cmdline = tail_memdup(cmdline_str, sizeof(cmdline_str));
81 	char *cmdline_short =
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];
85 	unsigned int i;
86 	unsigned int j;
87 
88 	struct strval cmdlines[] = {
89 		{ (uintptr_t) NULL, "", "", "NULL" },
90 		{ (uintptr_t) (cmdline + sizeof(cmdline_str)), "", "",
91 			cmdline_ptr },
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 },
97 	};
98 
99 
100 	snprintf(cmdline_ptr, sizeof(cmdline_ptr), "%p",
101 		cmdline + sizeof(cmdline_str));
102 	snprintf(cmdline_short_ptr, sizeof(cmdline_short_ptr), "%p",
103 		cmdline_short);
104 
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],
113 			       cmdlines[j].str,
114 			       sizeof(kernel_ulong_t) == 8 ? flags[i].str64 :
115 			       flags[i].str32, flags[i].str, sprintrc(rc));
116 		}
117 	}
118 
119 	puts("+++ exited with 0 +++");
120 
121 	return 0;
122 }
123 
124 #else
125 
126 SKIP_MAIN_UNDEFINED("__NR_kexec_file_load");
127 
128 #endif
129