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