Debian Codegraph @

testsuite/systemtap.syscall/keyctl.c

raw
1 /* COVERAGE: keyctl add_key request_key */
2
3 #define _GNU_SOURCE
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/syscall.h>
9 #include <linux/keyctl.h>
10 #include <sys/mman.h>
11
12 typedef int32_t key_serial_t;
13
14 #define __KEYCTL_ARG_TYPE unsigned long
15
16 // On arm (3.11.10-301.fc20.armv7hl), the KEYCTL_GET_PERSISTENT code
17 // causes a SIGSEGV.
18 #if defined(__arm__) && defined(KEYCTL_GET_PERSISTENT)
19 #undef KEYCTL_GET_PERSISTENT
20 #endif
21
22 key_serial_t __add_key(const char *type, const char *description,
23 const void *payload, size_t plen, key_serial_t ringid)
24 {
25 return syscall(__NR_add_key, type, description, payload, plen, ringid);
26 }
27
28 key_serial_t __request_key(const char *type, const char *description,
29 const char * callout_info, key_serial_t destringid)
30 {
31 return syscall(__NR_request_key, type, description, callout_info, destringid);
32 }
33
34 int main() {
35 mlockall(MCL_CURRENT);
36
37 key_serial_t ring_id, ring_id_2, ring_id_3, key_id, key_id_2;
38
39 // --- test normal operation for keyctl manpage scenarios
40
41 // (*) Get a keyring to work with
42 ring_id = syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID, KEY_SPEC_SESSION_KEYRING, 0);
43 //staptest// [[[[keyctl (KEYCTL_GET_KEYRING_ID, KEY_SPEC_SESSION_KEYRING, 0)!!!!ni_syscall ()]]]] = NNNN
44
45 // (*) Add a key to a keyring
46 key_id = __add_key("user", "testkey", "somedata", strlen("somedata"), ring_id);
47 //staptest// add_key ("user", "testkey", "somedata", 8, NNNN) = NNNN
48
49 // (*) Create a keyring
50 ring_id_2 = __add_key("keyring", "newring2", NULL, 0, ring_id);
51 //staptest// add_key ("keyring", "newring2",\ +0x0, 0, NNNN) = NNNN
52
53 // (*) Get persistent keyring
54 #ifdef KEYCTL_GET_PERSISTENT
55 ring_id_3 = syscall(__NR_keyctl, KEYCTL_GET_PERSISTENT, (unsigned long)-1, ring_id_2);
56 //staptest// [[[[keyctl (KEYCTL_GET_PERSISTENT, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
57
58 syscall(__NR_keyctl, KEYCTL_UNLINK, ring_id_3, ring_id_2);
59 //staptest// [[[[keyctl (KEYCTL_UNLINK, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
60 #endif
61
62 // (*) Request a key
63 __request_key("user", "testkey", 0, ring_id);
64 //staptest// request_key ("user", "testkey", 0x0, NNNN) = NNNN
65
66 // (*) Update a key
67 syscall(__NR_keyctl, KEYCTL_UPDATE, key_id, "blah", 4);
68 //staptest// [[[[keyctl (KEYCTL_UPDATE, NNNN, "blah", 4)!!!!ni_syscall ()]]]] = NNNN
69
70 // (*) Revoke a key
71 key_id_2 = __add_key("user", "testkey2", "somedata2", strlen("somedata2"), ring_id);
72 //staptest// add_key ("user", "testkey2", "somedata2", 9, NNNN) = NNNN
73 syscall(__NR_keyctl, KEYCTL_REVOKE, key_id_2);
74 //staptest// [[[[keyctl (KEYCTL_REVOKE, NNNN)!!!!ni_syscall ()]]]] = NNNN
75
76 // (*) Clear a keyring
77 syscall(__NR_keyctl, KEYCTL_CLEAR, ring_id_2);
78 //staptest// [[[[keyctl (KEYCTL_CLEAR, NNNN)!!!!ni_syscall ()]]]] = NNNN
79
80 // Remove unneeded keyring
81 syscall(__NR_keyctl, KEYCTL_UNLINK, ring_id_2, ring_id);
82 //staptest// [[[[keyctl (KEYCTL_UNLINK, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
83
84 // (*) Link a key to a keyring
85 syscall(__NR_keyctl, KEYCTL_LINK, key_id, ring_id);
86 //staptest// [[[[keyctl (KEYCTL_LINK, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
87
88 // (*) Unlink a key from a keyring or the session keyring tree
89 syscall(__NR_keyctl, KEYCTL_UNLINK, key_id_2, ring_id);
90 //staptest// [[[[keyctl (KEYCTL_UNLINK, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
91
92 // (*) Search a keyring
93 syscall(__NR_keyctl, KEYCTL_SEARCH, ring_id, "user", "testkey", 0);
94 //staptest// [[[[keyctl (KEYCTL_SEARCH, NNNN, "user", "testkey", 0)!!!!ni_syscall ()]]]] = NNNN
95
96 // (*) Read a key
97 // see "Get a keyring to work with" above
98
99 // (*) List a keyring
100 // see "Get a keyring to work with" above plus "Describe a key" below
101
102 // (*) Describe a key
103 syscall(__NR_keyctl, KEYCTL_DESCRIBE, key_id, NULL, 0);
104 //staptest// [[[[keyctl (KEYCTL_DESCRIBE, NNNN, 0x0, 0)!!!!ni_syscall ()]]]] = NNNN
105
106 // (*) Change the access controls on a key
107 syscall(__NR_keyctl, KEYCTL_CHOWN, key_id, getuid(), (unsigned long)-1);
108 //staptest// [[[[keyctl (KEYCTL_CHOWN, NNNN, NNNN, -1)!!!!ni_syscall ()]]]] = NNNN
109 syscall(__NR_keyctl, KEYCTL_CHOWN, key_id, (unsigned long)-1, getgid());
110 //staptest// [[[[keyctl (KEYCTL_CHOWN, NNNN, -1, NNNN)!!!!ni_syscall ()]]]] = NNNN
111
112 // (*) Set the permissions mask on a key
113 syscall(__NR_keyctl, KEYCTL_SETPERM, key_id, 0x3f3f3f3f);
114 // on rhel[56] i686 ths max string length appears to be too short
115 // to accomodate whole the perm string:
116 //staptest// [[[[keyctl (KEYCTL_SETPERM, NNNN, KEY_[^ ]+)!!!!ni_syscall ()]]]] = NNNN
117
118 // (*) Start a new session with fresh keyrings
119 // irrelevant here
120
121 // (*) Instantiate a key
122
123 /*
124 * keyctl_assume_authority, keyctl_instantiate, keyctl_instantiate_iov,
125 * keyctl_reject and keyctl_negate can only be successfully run if the
126 * program is run by /sbin/request-key. See `man 3 keyctl_assume_authority`
127 * and `man 3 keyctl` for more info. These (below) will not succeed here:
128 */
129
130 key_id_2 = __add_key("user", "testkey2", "somedata2", strlen("somedata2"), ring_id);
131 //staptest// add_key ("user", "testkey2", "somedata2", 9, NNNN) = NNNN
132
133 syscall(__NR_keyctl, KEYCTL_ASSUME_AUTHORITY, key_id_2);
134 //staptest// [[[[keyctl (KEYCTL_ASSUME_AUTHORITY, NNNN)!!!!ni_syscall ()]]]] = NNNN
135 syscall(__NR_keyctl, KEYCTL_INSTANTIATE, key_id_2, "somedata2", strlen("somedata2"), ring_id);
136 //staptest// [[[[keyctl (KEYCTL_INSTANTIATE, NNNN, "somedata2", 9, NNNN)!!!!ni_syscall ()]]]] = NNNN
137
138 #ifdef KEYCTL_INSTANTIATE_IOV
139 #include <sys/uio.h>
140 struct iovec iovecs[] = {
141 {"data1", 5},
142 {"data2", 5}
143 };
144
145 syscall(__NR_keyctl, KEYCTL_INSTANTIATE_IOV, key_id_2, &iovecs, 2, ring_id);
146 //staptest// [[[[keyctl (KEYCTL_INSTANTIATE_IOV, NNNN, XXXX, 2, NNNN)!!!!ni_syscall ()]]]] = NNNN
147 #endif
148 syscall(__NR_keyctl, KEYCTL_NEGATE, key_id_2, 30, ring_id);
149 //staptest// [[[[keyctl (KEYCTL_NEGATE, NNNN, 30, NNNN)!!!!ni_syscall ()]]]] = NNNN
150 #ifdef KEYCTL_REJECT
151 syscall(__NR_keyctl, KEYCTL_REJECT, key_id_2, 30, 64, ring_id);
152 //staptest// [[[[keyctl (KEYCTL_REJECT, NNNN, 30, 64, NNNN)!!!!ni_syscall ()]]]] = NNNN
153 #endif
154
155 syscall(__NR_keyctl, KEYCTL_UNLINK, key_id_2, ring_id);
156 //staptest// [[[[keyctl (KEYCTL_UNLINK, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
157
158 // (*) Set the expiry time on a key
159 key_id_2 = __add_key("user", "testkey2", "somedata2", strlen("somedata2"), ring_id);
160 //staptest// add_key ("user", "testkey2", "somedata2", 9, NNNN) = NNNN
161 syscall(__NR_keyctl, KEYCTL_SET_TIMEOUT, key_id_2, 100);
162 //staptest// [[[[keyctl (KEYCTL_SET_TIMEOUT, NNNN, 100)!!!!ni_syscall ()]]]] = NNNN
163 syscall(__NR_keyctl, KEYCTL_UNLINK, key_id_2, ring_id);
164 //staptest// [[[[keyctl (KEYCTL_UNLINK, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
165
166 // (*) Retrieve a key's security context
167 #ifdef KEYCTL_GET_SECURITY
168 syscall(__NR_keyctl, KEYCTL_GET_SECURITY, key_id, NULL, 0);
169 //staptest// [[[[keyctl (KEYCTL_GET_SECURITY, NNNN, 0x0, 0)!!!!ni_syscall ()]]]] = NNNN
170 #endif
171
172 // (*) Give the parent process a new session keyring
173 syscall(__NR_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL);
174 //staptest// [[[[keyctl (KEYCTL_JOIN_SESSION_KEYRING, 0x0)!!!!ni_syscall ()]]]] = NNNN
175
176 // following would cause irreversible change, so we'll skip testing it:
177 // syscall(__NR_keyctl, KEYCTL_SESSION_TO_PARENT);
178
179 // (*) Remove dead keys from the session keyring tree
180 // see "Unlink a key from a keyring or the session keyring tree"
181
182 // (*) Remove matching keys from the session keyring tree
183 // see "Unlink a key from a keyring or the session keyring tree"
184
185 // --- test normal operation on commands that weren't covered by named `man keyctl` scenarios
186
187
188 // KEYCTL_INVALIDATE
189 #ifdef KEYCTL_INVALIDATE
190 syscall(__NR_keyctl, KEYCTL_INVALIDATE, key_id);
191 //staptest// [[[[keyctl (KEYCTL_INVALIDATE, NNNN)!!!!ni_syscall ()]]]] = NNNN
192 #else
193 syscall(__NR_keyctl, KEYCTL_UNLINK, key_id, ring_id);
194 //staptest// [[[[keyctl (KEYCTL_UNLINK, NNNN, NNNN)!!!!ni_syscall ()]]]] = NNNN
195 #endif
196
197 // KEYCTL_GET_KEYRING_ID
198 syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID, KEY_SPEC_PROCESS_KEYRING, 1);
199 //staptest// [[[[keyctl (KEYCTL_GET_KEYRING_ID, KEY_SPEC_PROCESS_KEYRING, 1)!!!!ni_syscall ()]]]] = NNNN
200
201 // KEYCTL_SET_REQKEY_KEYRING
202 syscall(__NR_keyctl, KEYCTL_SET_REQKEY_KEYRING, KEY_REQKEY_DEFL_NO_CHANGE);
203 //staptest// [[[[keyctl (KEYCTL_SET_REQKEY_KEYRING, KEY_REQKEY_DEFL_NO_CHANGE)!!!!ni_syscall ()]]]] = NNNN
204
205 // --- test ugly calls
206
207 syscall(__NR_keyctl, (int)-1, (unsigned long)-1, (unsigned long)-1, (unsigned long)-1, (unsigned long)-1);
208 //staptest// [[[[keyctl (-1, -1, -1, -1, -1)!!!!ni_syscall ()]]]] = NNNN
209
210 __add_key((const char *)-1, (const char *)-1, (const char *)-1, -1, -1);
211 #ifdef __s390__
212 #if __WORDSIZE == 64
213 //staptest// add_key (0x[7]?[f]+, 0x[7]?[f]+, 0x[7]?[f]+, 18446744073709551615, -1) = NNNN (EINVAL)
214 #else
215 //staptest// add_key (0x[7]?[f]+, 0x[7]?[f]+, 0x[7]?[f]+, 4294967295, -1) = NNNN (EINVAL)
216 #endif
217 #else
218 #if __WORDSIZE == 64
219 //staptest// add_key (0x[f]+, 0x[f]+, 0x[f]+, 18446744073709551615, -1) = NNNN (EINVAL)
220 #else
221 //staptest// add_key (0x[f]+, 0x[f]+, 0x[f]+, 4294967295, -1) = NNNN (EINVAL)
222 #endif
223 #endif
224
225 __request_key((const char *)-1, (const char *)-1, (const char *)-1, -1);
226 #ifdef __s390__
227 //staptest// request_key (0x[7]?[f]+, 0x[7]?[f]+, 0x[7]?[f]+, -1) = -14 (EFAULT)
228 #else
229 //staptest// request_key (0x[7]?[f]+, 0x[7]?[f]+, 0x[f]+, -1) = -14 (EFAULT)
230 #endif
231
232 return 0;
233 }