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