• Home
  • History
  • Annotate
  • 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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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.
28  */
29 
30 #include "tests.h"
31 #include <asm/unistd.h>
32 #include "scno.h"
33 
34 #ifdef __NR_kexec_file_load
35 
36 # include <inttypes.h>
37 # include <stdio.h>
38 # include <unistd.h>
39 
40 struct strval {
41 	kernel_ulong_t val;
42 	const char *str64;
43 	const char *str32;
44 	const char *str;
45 };
46 
47 #define CMDLINE_STR "deadcodebaddatadefaced";
48 
49 int
main(void)50 main(void)
51 {
52 	static const kernel_ulong_t bogus_kernel_fd =
53 		(kernel_ulong_t) 0xdeadca57badda7a1ULL;
54 	static const kernel_ulong_t bogus_initrd_fd =
55 		(kernel_ulong_t) 0xdec0ded1defaced2ULL;
56 	static const char cmdline_str[] = CMDLINE_STR;
57 	static const char cmdline_short_str[] = "abcdef";
58 
59 	static const kernel_ulong_t cmdline_lens[] = {
60 		0,
61 		(kernel_ulong_t) 0xcaffeeeddeadbeefULL,
62 		sizeof(cmdline_str),
63 		sizeof(cmdline_str) - 1,
64 		sizeof(cmdline_short_str),
65 		sizeof(cmdline_short_str) - 1,
66 		sizeof(cmdline_short_str) + 1,
67 	};
68 	static const struct strval flags[] = {
69 		{ (kernel_ulong_t) 0xbadc0dedda7a1058ULL,
70 			"0xbadc0ded", "0x",
71 			"da7a1058 /* KEXEC_FILE_??? */" },
72 		{ 0, "", "", "0" },
73 		{ 0xdeadbeef, "", "", "KEXEC_FILE_UNLOAD|KEXEC_FILE_ON_CRASH|"
74 			"KEXEC_FILE_NO_INITRAMFS|0xdeadbee8" },
75 	};
76 
77 
78 	long rc;
79 	char *cmdline = tail_memdup(cmdline_str, sizeof(cmdline_str));
80 	char *cmdline_short =
81 		tail_memdup(cmdline_short_str, sizeof(cmdline_short_str));
82 	char cmdline_ptr[sizeof("0x") + sizeof(void *) * 2];
83 	char cmdline_short_ptr[sizeof("0x") + sizeof(void *) * 2];
84 	unsigned int i;
85 	unsigned int j;
86 
87 	struct strval cmdlines[] = {
88 		{ (uintptr_t) NULL, "", "", "NULL" },
89 		{ (uintptr_t) (cmdline + sizeof(cmdline_str)), "", "",
90 			cmdline_ptr },
91 		{ (uintptr_t) cmdline, "", "", "\"deadcodeb\"..." },
92 		{ (uintptr_t) cmdline, "", "", "\"deadcodeb\"..." },
93 		{ (uintptr_t) cmdline_short, "", "", "\"abcdef\\0\"" },
94 		{ (uintptr_t) cmdline_short, "", "", "\"abcdef\"" },
95 		{ (uintptr_t) cmdline_short, "", "", cmdline_short_ptr },
96 	};
97 
98 
99 	snprintf(cmdline_ptr, sizeof(cmdline_ptr), "%p",
100 		cmdline + sizeof(cmdline_str));
101 	snprintf(cmdline_short_ptr, sizeof(cmdline_short_ptr), "%p",
102 		cmdline_short);
103 
104 	for (i = 0; i < ARRAY_SIZE(flags); i++) {
105 		for (j = 0; j < ARRAY_SIZE(cmdlines); j++) {
106 			rc = syscall(__NR_kexec_file_load, bogus_kernel_fd,
107 				     bogus_initrd_fd, cmdline_lens[j],
108 				     cmdlines[j].val, flags[i].val);
109 			printf("kexec_file_load(%d, %d, %llu, %s, %s%s) = %s\n",
110 			       (int) bogus_kernel_fd, (int) bogus_initrd_fd,
111 			       (unsigned long long) cmdline_lens[j],
112 			       cmdlines[j].str,
113 			       sizeof(kernel_ulong_t) == 8 ? flags[i].str64 :
114 			       flags[i].str32, flags[i].str, sprintrc(rc));
115 		}
116 	}
117 
118 	puts("+++ exited with 0 +++");
119 
120 	return 0;
121 }
122 
123 #else
124 
125 SKIP_MAIN_UNDEFINED("__NR_kexec_file_load");
126 
127 #endif
128