• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check decoding of request_key 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 
32 #include <asm/unistd.h>
33 
34 #ifdef __NR_request_key
35 
36 # include <inttypes.h>
37 # include <stdio.h>
38 # include <unistd.h>
39 
40 void
print_val_str(const void * ptr,const char * str)41 print_val_str(const void *ptr, const char *str)
42 {
43 	if (str)
44 		printf("%s, ", str);
45 	else
46 		printf("%p, ", ptr);
47 }
48 
49 void
do_request_key(const char * type,const char * type_str,const char * desc,const char * desc_str,const char * info,const char * info_str,int32_t keyring,const char * keyring_str)50 do_request_key(const char *type, const char *type_str, const char *desc,
51 	const char *desc_str, const char *info, const char *info_str,
52 	int32_t keyring, const char *keyring_str)
53 {
54 	long rc = syscall(__NR_request_key, type, desc, info, keyring);
55 	const char *errstr = sprintrc(rc);
56 	printf("request_key(");
57 	print_val_str(type, type_str);
58 	print_val_str(desc, desc_str);
59 	print_val_str(info, info_str);
60 	if (keyring_str)
61 		printf("%s", keyring_str);
62 	else
63 		printf("%d", keyring);
64 	printf(") = %s\n", errstr);
65 }
66 
67 int
main(void)68 main(void)
69 {
70 	static const char unterminated1[] = { '\1', '\2', '\3', '\4', '\5' };
71 	static const char unterminated2[] = { '\6', '\7', '\10', '\11', '\12' };
72 	static const char unterminated3[] = { '\16', '\17', '\20', '\21', '\22' };
73 
74 	char *bogus_type = tail_memdup(unterminated1, sizeof(unterminated1));
75 	char *bogus_desc = tail_memdup(unterminated2, sizeof(unterminated2));
76 	char *bogus_info = tail_memdup(unterminated3, sizeof(unterminated3));
77 
78 	unsigned i;
79 	unsigned j;
80 	unsigned k;
81 	unsigned l;
82 
83 	struct {
84 		const char *type;
85 		const char *str;
86 	} types[] = {
87 		{ ARG_STR(NULL) },
88 		{ bogus_type + sizeof(unterminated1), NULL },
89 		{ bogus_type, NULL },
90 		{ ARG_STR("\20\21\22\23\24") },
91 		{ ARG_STR("user") },
92 	};
93 
94 	struct {
95 		const char *desc;
96 		const char *str;
97 	} descs[] = {
98 		{ ARG_STR(NULL) },
99 		{ bogus_desc + sizeof(unterminated2), NULL },
100 		{ bogus_desc, NULL },
101 		{ ARG_STR("\25\26\27\30\31") },
102 		{ ARG_STR("desc") },
103 		{ "overly long description", _STR("overly long ") "..." },
104 	};
105 
106 	struct {
107 		const char *info;
108 		const char *str;
109 	} infos[] = {
110 		{ ARG_STR(NULL) },
111 		{ bogus_info + sizeof(unterminated3), NULL },
112 		{ bogus_info, NULL },
113 		{ ARG_STR("\32\33\34\35\36") },
114 		{ ARG_STR("info") },
115 		{ "overly long info", _STR("overly long ") "..." },
116 	};
117 
118 	struct {
119 		uint32_t keyring;
120 		const char *str;
121 	} keyrings[] = {
122 		{ ARG_STR(0) },
123 		{ ARG_STR(1234567890) },
124 		{ ARG_STR(-1234567890) },
125 		{ -1, "KEY_SPEC_THREAD_KEYRING" },
126 	};
127 
128 	for (i = 0; i < ARRAY_SIZE(types); i++)
129 		for (j = 0; j < ARRAY_SIZE(descs); j++)
130 			for (k = 0; k < ARRAY_SIZE(infos); k++)
131 				for (l = 0; l < ARRAY_SIZE(keyrings); l++)
132 					do_request_key(
133 						types[i].type, types[i].str,
134 						descs[j].desc, descs[j].str,
135 						infos[k].info, infos[k].str,
136 						keyrings[l].keyring,
137 						keyrings[l].str);
138 
139 	puts("+++ exited with 0 +++");
140 
141 	return 0;
142 }
143 
144 #else
145 
146 SKIP_MAIN_UNDEFINED("__NR_request_key");
147 
148 #endif
149