blob: 50d6fb482b07af976e75224be64c7d3682fcba52 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
2 * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000028#include "defs.h"
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000029
30typedef int32_t key_serial_t;
31
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000032#include "xlat/key_spec.h"
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000033
34static void
35print_keyring_serial_number(key_serial_t id)
36{
Dmitry V. Levin9134aab2016-05-14 21:46:05 +000037 const char *str = xlookup(key_spec, (unsigned int) id);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000038
39 if (str)
40 tprints(str);
41 else
42 tprintf("%d", id);
43}
44
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000045SYS_FUNC(add_key)
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000046{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000047 /* type */
48 printstr(tcp, tcp->u_arg[0], -1);
49 /* description */
50 tprints(", ");
51 printstr(tcp, tcp->u_arg[1], -1);
52 /* payload */
53 tprints(", ");
54 printstr(tcp, tcp->u_arg[2], tcp->u_arg[3]);
55 /* payload length */
56 tprintf(", %lu, ", tcp->u_arg[3]);
57 /* keyring serial number */
58 print_keyring_serial_number(tcp->u_arg[4]);
59
60 return RVAL_DECODED;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000061}
62
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000063SYS_FUNC(request_key)
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000064{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000065 /* type */
66 printstr(tcp, tcp->u_arg[0], -1);
67 /* description */
68 tprints(", ");
69 printstr(tcp, tcp->u_arg[1], -1);
70 /* callout_info */
71 tprints(", ");
72 printstr(tcp, tcp->u_arg[2], -1);
73 /* keyring serial number */
74 tprints(", ");
75 print_keyring_serial_number(tcp->u_arg[3]);
76
77 return RVAL_DECODED;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000078}
79
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000080static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000081keyctl_get_keyring_id(struct tcb *tcp, key_serial_t id, int create)
82{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000083 print_keyring_serial_number(id);
84 tprintf(", %d", create);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000085}
86
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000087static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000088keyctl_update_key(struct tcb *tcp, key_serial_t id, long addr, long len)
89{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000090 print_keyring_serial_number(id);
91 tprints(", ");
92 printstr(tcp, addr, len);
93 tprintf(", %lu", len);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000094}
95
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000096static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +000097keyctl_handle_key_key(struct tcb *tcp, key_serial_t id1, key_serial_t id2)
98{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +000099 print_keyring_serial_number(id1);
100 tprints(", ");
101 print_keyring_serial_number(id2);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000102}
103
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000104static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000105keyctl_read_key(struct tcb *tcp, key_serial_t id, long addr, long len)
106{
107 if (entering(tcp)) {
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000108 print_keyring_serial_number(id);
109 tprints(", ");
110 } else {
Dmitry V. Levin5f6ca3d2015-07-20 00:26:23 +0000111 if (syserror(tcp))
112 printaddr(addr);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000113 else {
114 long rval = tcp->u_rval > len ?
115 len : (tcp->u_rval ? -1 : 0);
116 printstr(tcp, addr, rval);
117 }
118 tprintf(", %lu", len);
119 }
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000120}
121
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000122static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000123keyctl_keyring_search(struct tcb *tcp, key_serial_t id1, long addr1,
124 long addr2, key_serial_t id2)
125{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000126 print_keyring_serial_number(id1);
127 tprints(", ");
128 printstr(tcp, addr1, -1);
129 tprints(", ");
130 printstr(tcp, addr2, -1);
131 tprints(", ");
132 print_keyring_serial_number(id2);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000133}
134
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000135static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000136keyctl_chown_key(struct tcb *tcp, key_serial_t id, int user, int group)
137{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000138 print_keyring_serial_number(id);
139 tprintf(", %d, %d", user, group);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000140}
141
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000142static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000143keyctl_instantiate_key(struct tcb *tcp, key_serial_t id1, long addr,
144 long len, key_serial_t id2)
145{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000146 print_keyring_serial_number(id1);
147 tprints(", ");
148 printstr(tcp, addr, len);
149 tprintf(", %lu, ", len);
150 print_keyring_serial_number(id2);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000151}
152
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000153static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000154keyctl_instantiate_key_iov(struct tcb *tcp, key_serial_t id1,
155 long addr, long len, key_serial_t id2)
156{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000157 print_keyring_serial_number(id1);
158 tprints(", ");
159 tprint_iov(tcp, len, addr, 1);
160 tprintf(", %lu, ", len);
161 print_keyring_serial_number(id2);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000162}
163
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000164static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000165keyctl_negate_key(struct tcb *tcp, key_serial_t id1, unsigned timeout,
166 key_serial_t id2)
167{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000168 print_keyring_serial_number(id1);
169 tprintf(", %u, ", timeout);
170 print_keyring_serial_number(id2);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000171}
172
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000173static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000174keyctl_reject_key(struct tcb *tcp, key_serial_t id1, unsigned timeout,
175 unsigned error, key_serial_t id2)
176{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000177 print_keyring_serial_number(id1);
178 tprintf(", %u, %u, ", timeout, error);
179 print_keyring_serial_number(id2);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000180}
181
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000182static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000183keyctl_set_timeout(struct tcb *tcp, key_serial_t id, unsigned timeout)
184{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000185 print_keyring_serial_number(id);
186 tprintf(", %u", timeout);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000187}
188
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000189static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000190keyctl_get_persistent(struct tcb *tcp, int uid, key_serial_t id)
191{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000192 tprintf("%d, ", uid);
193 print_keyring_serial_number(id);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000194}
195
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000196#include "xlat/key_perms.h"
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000197
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000198static void
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000199keyctl_setperm_key(struct tcb *tcp, key_serial_t id, uint32_t perm)
200{
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000201 print_keyring_serial_number(id);
202 tprints(", ");
203 printflags(key_perms, perm, "KEY_???");
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000204}
205
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000206#include "xlat/key_reqkeys.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000207#include "xlat/keyctl_commands.h"
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000208
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000209SYS_FUNC(keyctl)
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000210{
211 int cmd = tcp->u_arg[0];
212
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000213 if (entering(tcp)) {
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000214 printxval(keyctl_commands, cmd, "KEYCTL_???");
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000215 tprints(", ");
216 }
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000217
218 switch (cmd) {
219 case KEYCTL_GET_KEYRING_ID:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000220 keyctl_get_keyring_id(tcp, tcp->u_arg[1], tcp->u_arg[2]);
221 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000222
223 case KEYCTL_JOIN_SESSION_KEYRING:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000224 printstr(tcp, tcp->u_arg[1], -1);
225 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000226
227 case KEYCTL_UPDATE:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000228 keyctl_update_key(tcp, tcp->u_arg[1],
229 tcp->u_arg[2], tcp->u_arg[3]);
230 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000231
232 case KEYCTL_REVOKE:
233 case KEYCTL_CLEAR:
234 case KEYCTL_INVALIDATE:
235 case KEYCTL_ASSUME_AUTHORITY:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000236 print_keyring_serial_number(tcp->u_arg[1]);
237 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000238
239 case KEYCTL_LINK:
240 case KEYCTL_UNLINK:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000241 keyctl_handle_key_key(tcp, tcp->u_arg[1], tcp->u_arg[2]);
242 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000243
244 case KEYCTL_DESCRIBE:
245 case KEYCTL_READ:
246 case KEYCTL_GET_SECURITY:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000247 keyctl_read_key(tcp, tcp->u_arg[1],
248 tcp->u_arg[2], tcp->u_arg[3]);
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000249 return 0;
250
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000251 case KEYCTL_SEARCH:
252 keyctl_keyring_search(tcp, tcp->u_arg[1], tcp->u_arg[2],
253 tcp->u_arg[3], tcp->u_arg[4]);
254 break;
255
256 case KEYCTL_CHOWN:
257 keyctl_chown_key(tcp, tcp->u_arg[1],
258 tcp->u_arg[2], tcp->u_arg[3]);
259 break;
260
261 case KEYCTL_SETPERM:
262 keyctl_setperm_key(tcp, tcp->u_arg[1], tcp->u_arg[2]);
263 break;
264
265 case KEYCTL_INSTANTIATE:
266 keyctl_instantiate_key(tcp, tcp->u_arg[1], tcp->u_arg[2],
267 tcp->u_arg[3], tcp->u_arg[4]);
268 break;
269
270 case KEYCTL_NEGATE:
271 keyctl_negate_key(tcp, tcp->u_arg[1],
272 tcp->u_arg[2], tcp->u_arg[3]);
273 break;
274
275 case KEYCTL_SET_REQKEY_KEYRING:
276 printxval(key_reqkeys, tcp->u_arg[1], "KEY_REQKEY_DEFL_???");
277 break;
278
279 case KEYCTL_SET_TIMEOUT:
280 keyctl_set_timeout(tcp, tcp->u_arg[1], tcp->u_arg[2]);
281 break;
282
283 case KEYCTL_SESSION_TO_PARENT:
284 break;
285
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000286 case KEYCTL_REJECT:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000287 keyctl_reject_key(tcp, tcp->u_arg[1], tcp->u_arg[2],
288 tcp->u_arg[3], tcp->u_arg[4]);
289 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000290
291 case KEYCTL_INSTANTIATE_IOV:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000292 keyctl_instantiate_key_iov(tcp, tcp->u_arg[1],
293 tcp->u_arg[2], tcp->u_arg[3],
294 tcp->u_arg[4]);
295 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000296
297 case KEYCTL_GET_PERSISTENT:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000298 keyctl_get_persistent(tcp, tcp->u_arg[1], tcp->u_arg[2]);
299 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000300
301 default:
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000302 tprintf("%#lx, %#lx, %#lx, %#lx",
303 tcp->u_arg[1], tcp->u_arg[2],
304 tcp->u_arg[3], tcp->u_arg[4]);
305 break;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000306 }
307
Dmitry V. Levin4eef2912015-07-20 00:48:49 +0000308 return RVAL_DECODED;
Dmitry V. Levin3acf4032014-02-05 22:41:45 +0000309}