iov_iter: transparently handle compat iovecs in import_iovec
[linux-2.6-block.git] / security / keys / compat.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* 32-bit compatibility syscall for 64-bit systems
3  *
4  * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #include <linux/syscalls.h>
9 #include <linux/keyctl.h>
10 #include <linux/compat.h>
11 #include <linux/slab.h>
12 #include "internal.h"
13
14 /*
15  * Instantiate a key with the specified compatibility multipart payload and
16  * link the key into the destination keyring if one is given.
17  *
18  * The caller must have the appropriate instantiation permit set for this to
19  * work (see keyctl_assume_authority).  No other permissions are required.
20  *
21  * If successful, 0 will be returned.
22  */
23 static long compat_keyctl_instantiate_key_iov(
24         key_serial_t id,
25         const struct compat_iovec __user *_payload_iov,
26         unsigned ioc,
27         key_serial_t ringid)
28 {
29         struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
30         struct iov_iter from;
31         long ret;
32
33         if (!_payload_iov)
34                 ioc = 0;
35
36         ret = import_iovec(WRITE, (const struct iovec __user *)_payload_iov,
37                            ioc, ARRAY_SIZE(iovstack), &iov, &from);
38         if (ret < 0)
39                 return ret;
40
41         ret = keyctl_instantiate_key_common(id, &from, ringid);
42         kfree(iov);
43         return ret;
44 }
45
46 /*
47  * The key control system call, 32-bit compatibility version for 64-bit archs
48  */
49 COMPAT_SYSCALL_DEFINE5(keyctl, u32, option,
50                        u32, arg2, u32, arg3, u32, arg4, u32, arg5)
51 {
52         switch (option) {
53         case KEYCTL_GET_KEYRING_ID:
54                 return keyctl_get_keyring_ID(arg2, arg3);
55
56         case KEYCTL_JOIN_SESSION_KEYRING:
57                 return keyctl_join_session_keyring(compat_ptr(arg2));
58
59         case KEYCTL_UPDATE:
60                 return keyctl_update_key(arg2, compat_ptr(arg3), arg4);
61
62         case KEYCTL_REVOKE:
63                 return keyctl_revoke_key(arg2);
64
65         case KEYCTL_DESCRIBE:
66                 return keyctl_describe_key(arg2, compat_ptr(arg3), arg4);
67
68         case KEYCTL_CLEAR:
69                 return keyctl_keyring_clear(arg2);
70
71         case KEYCTL_LINK:
72                 return keyctl_keyring_link(arg2, arg3);
73
74         case KEYCTL_UNLINK:
75                 return keyctl_keyring_unlink(arg2, arg3);
76
77         case KEYCTL_SEARCH:
78                 return keyctl_keyring_search(arg2, compat_ptr(arg3),
79                                              compat_ptr(arg4), arg5);
80
81         case KEYCTL_READ:
82                 return keyctl_read_key(arg2, compat_ptr(arg3), arg4);
83
84         case KEYCTL_CHOWN:
85                 return keyctl_chown_key(arg2, arg3, arg4);
86
87         case KEYCTL_SETPERM:
88                 return keyctl_setperm_key(arg2, arg3);
89
90         case KEYCTL_INSTANTIATE:
91                 return keyctl_instantiate_key(arg2, compat_ptr(arg3), arg4,
92                                               arg5);
93
94         case KEYCTL_NEGATE:
95                 return keyctl_negate_key(arg2, arg3, arg4);
96
97         case KEYCTL_SET_REQKEY_KEYRING:
98                 return keyctl_set_reqkey_keyring(arg2);
99
100         case KEYCTL_SET_TIMEOUT:
101                 return keyctl_set_timeout(arg2, arg3);
102
103         case KEYCTL_ASSUME_AUTHORITY:
104                 return keyctl_assume_authority(arg2);
105
106         case KEYCTL_GET_SECURITY:
107                 return keyctl_get_security(arg2, compat_ptr(arg3), arg4);
108
109         case KEYCTL_SESSION_TO_PARENT:
110                 return keyctl_session_to_parent();
111
112         case KEYCTL_REJECT:
113                 return keyctl_reject_key(arg2, arg3, arg4, arg5);
114
115         case KEYCTL_INSTANTIATE_IOV:
116                 return compat_keyctl_instantiate_key_iov(
117                         arg2, compat_ptr(arg3), arg4, arg5);
118
119         case KEYCTL_INVALIDATE:
120                 return keyctl_invalidate_key(arg2);
121
122         case KEYCTL_GET_PERSISTENT:
123                 return keyctl_get_persistent(arg2, arg3);
124
125         case KEYCTL_DH_COMPUTE:
126                 return compat_keyctl_dh_compute(compat_ptr(arg2),
127                                                 compat_ptr(arg3),
128                                                 arg4, compat_ptr(arg5));
129
130         case KEYCTL_RESTRICT_KEYRING:
131                 return keyctl_restrict_keyring(arg2, compat_ptr(arg3),
132                                                compat_ptr(arg4));
133
134         case KEYCTL_PKEY_QUERY:
135                 if (arg3 != 0)
136                         return -EINVAL;
137                 return keyctl_pkey_query(arg2,
138                                          compat_ptr(arg4),
139                                          compat_ptr(arg5));
140
141         case KEYCTL_PKEY_ENCRYPT:
142         case KEYCTL_PKEY_DECRYPT:
143         case KEYCTL_PKEY_SIGN:
144                 return keyctl_pkey_e_d_s(option,
145                                          compat_ptr(arg2), compat_ptr(arg3),
146                                          compat_ptr(arg4), compat_ptr(arg5));
147
148         case KEYCTL_PKEY_VERIFY:
149                 return keyctl_pkey_verify(compat_ptr(arg2), compat_ptr(arg3),
150                                           compat_ptr(arg4), compat_ptr(arg5));
151
152         case KEYCTL_MOVE:
153                 return keyctl_keyring_move(arg2, arg3, arg4, arg5);
154
155         case KEYCTL_CAPABILITIES:
156                 return keyctl_capabilities(compat_ptr(arg2), arg3);
157
158         case KEYCTL_WATCH_KEY:
159                 return keyctl_watch_key(arg2, arg3, arg4);
160
161         default:
162                 return -EOPNOTSUPP;
163         }
164 }