blob: 79641c4afee935335a39c002a807cb44b941ec31 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sage Weilec0994e2010-02-02 16:25:35 -08002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/ceph/ceph_debug.h>
Sage Weilec0994e2010-02-02 16:25:35 -08004
5#include <linux/err.h>
6#include <linux/module.h>
7#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Sage Weilec0994e2010-02-02 16:25:35 -08009
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070010#include <linux/ceph/decode.h>
11#include <linux/ceph/auth.h>
Ilya Dryomovcc255c72018-07-27 19:25:32 +020012#include <linux/ceph/ceph_features.h>
Ilya Dryomova51983e2015-10-28 23:52:06 +010013#include <linux/ceph/libceph.h>
Yan, Zheng33d07332014-11-04 16:33:37 +080014#include <linux/ceph/messenger.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015
16#include "crypto.h"
Sage Weilec0994e2010-02-02 16:25:35 -080017#include "auth_x.h"
18#include "auth_x_protocol.h"
Sage Weilec0994e2010-02-02 16:25:35 -080019
Sage Weilec0994e2010-02-02 16:25:35 -080020static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
21
22static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
23{
24 struct ceph_x_info *xi = ac->private;
Ilya Dryomovf79e25b2020-11-27 17:18:27 +010025 int missing;
26 int need; /* missing + need renewal */
Sage Weilec0994e2010-02-02 16:25:35 -080027
28 ceph_x_validate_tickets(ac, &need);
Ilya Dryomovf79e25b2020-11-27 17:18:27 +010029 missing = ac->want_keys & ~xi->have_keys;
30 WARN_ON((need & missing) != missing);
31 dout("%s want 0x%x have 0x%x missing 0x%x -> %d\n", __func__,
32 ac->want_keys, xi->have_keys, missing, !missing);
33 return !missing;
Sage Weilec0994e2010-02-02 16:25:35 -080034}
35
Sage Weila41359f2010-05-25 15:39:06 -070036static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
37{
38 struct ceph_x_info *xi = ac->private;
39 int need;
40
41 ceph_x_validate_tickets(ac, &need);
Ilya Dryomovf79e25b2020-11-27 17:18:27 +010042 dout("%s want 0x%x have 0x%x need 0x%x -> %d\n", __func__,
43 ac->want_keys, xi->have_keys, need, !!need);
44 return !!need;
Sage Weila41359f2010-05-25 15:39:06 -070045}
46
Ilya Dryomov55d9cc832016-12-02 16:35:07 +010047static int ceph_x_encrypt_offset(void)
48{
49 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
50}
51
Sage Weil807c86e2010-03-15 15:52:17 -070052static int ceph_x_encrypt_buflen(int ilen)
53{
Ilya Dryomov55d9cc832016-12-02 16:35:07 +010054 return ceph_x_encrypt_offset() + ilen + 16;
Sage Weil807c86e2010-03-15 15:52:17 -070055}
56
Ilya Dryomovd03857c2016-12-02 16:35:07 +010057static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
58 int buf_len, int plaintext_len)
Sage Weilec0994e2010-02-02 16:25:35 -080059{
Ilya Dryomovd03857c2016-12-02 16:35:07 +010060 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
61 int ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080062 int ret;
63
Ilya Dryomovd03857c2016-12-02 16:35:07 +010064 hdr->struct_v = 1;
65 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
66
67 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
68 plaintext_len + sizeof(struct ceph_x_encrypt_header),
69 &ciphertext_len);
Sage Weilec0994e2010-02-02 16:25:35 -080070 if (ret)
71 return ret;
Ilya Dryomovd03857c2016-12-02 16:35:07 +010072
73 ceph_encode_32(&buf, ciphertext_len);
74 return sizeof(u32) + ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080075}
76
Ilya Dryomovc571fe22018-07-26 18:05:43 +020077static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
78 int ciphertext_len)
79{
80 struct ceph_x_encrypt_header *hdr = p;
81 int plaintext_len;
82 int ret;
83
84 ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
85 &plaintext_len);
86 if (ret)
87 return ret;
88
89 if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
90 pr_err("%s bad magic\n", __func__);
91 return -EINVAL;
92 }
93
94 return plaintext_len - sizeof(*hdr);
95}
96
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010097static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
Sage Weilec0994e2010-02-02 16:25:35 -080098{
Ilya Dryomovc571fe22018-07-26 18:05:43 +020099 int ciphertext_len;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100100 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800101
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100102 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
103 ceph_decode_need(p, end, ciphertext_len, e_inval);
Sage Weilec0994e2010-02-02 16:25:35 -0800104
Ilya Dryomovc571fe22018-07-26 18:05:43 +0200105 ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
106 if (ret < 0)
Sage Weilec0994e2010-02-02 16:25:35 -0800107 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100108
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100109 *p += ciphertext_len;
Ilya Dryomovc571fe22018-07-26 18:05:43 +0200110 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100111
112e_inval:
113 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800114}
115
116/*
117 * get existing (or insert new) ticket handler
118 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700119static struct ceph_x_ticket_handler *
120get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -0800121{
122 struct ceph_x_ticket_handler *th;
123 struct ceph_x_info *xi = ac->private;
124 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
125
126 while (*p) {
127 parent = *p;
128 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
129 if (service < th->service)
130 p = &(*p)->rb_left;
131 else if (service > th->service)
132 p = &(*p)->rb_right;
133 else
134 return th;
135 }
136
137 /* add it */
138 th = kzalloc(sizeof(*th), GFP_NOFS);
139 if (!th)
140 return ERR_PTR(-ENOMEM);
141 th->service = service;
142 rb_link_node(&th->node, parent, p);
143 rb_insert_color(&th->node, &xi->ticket_handlers);
144 return th;
145}
146
147static void remove_ticket_handler(struct ceph_auth_client *ac,
148 struct ceph_x_ticket_handler *th)
149{
150 struct ceph_x_info *xi = ac->private;
151
152 dout("remove_ticket_handler %p %d\n", th, th->service);
153 rb_erase(&th->node, &xi->ticket_handlers);
154 ceph_crypto_key_destroy(&th->session_key);
155 if (th->ticket_blob)
156 ceph_buffer_put(th->ticket_blob);
157 kfree(th);
158}
159
Ilya Dryomov597cda32014-09-08 17:25:34 +0400160static int process_one_ticket(struct ceph_auth_client *ac,
161 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400162 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400163{
164 struct ceph_x_info *xi = ac->private;
165 int type;
166 u8 tkt_struct_v, blob_struct_v;
167 struct ceph_x_ticket_handler *th;
168 void *dp, *dend;
169 int dlen;
170 char is_enc;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200171 struct timespec64 validity;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400172 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400173 void **ptp;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200174 struct ceph_crypto_key new_session_key = { 0 };
Ilya Dryomov597cda32014-09-08 17:25:34 +0400175 struct ceph_buffer *new_ticket_blob;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200176 time64_t new_expires, new_renew_after;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400177 u64 new_secret_id;
178 int ret;
179
180 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
181
182 type = ceph_decode_32(p);
183 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
184
185 tkt_struct_v = ceph_decode_8(p);
186 if (tkt_struct_v != 1)
187 goto bad;
188
189 th = get_ticket_handler(ac, type);
190 if (IS_ERR(th)) {
191 ret = PTR_ERR(th);
192 goto out;
193 }
194
195 /* blob for me */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100196 dp = *p + ceph_x_encrypt_offset();
197 ret = ceph_x_decrypt(secret, p, end);
198 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400199 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100200 dout(" decrypted %d bytes\n", ret);
201 dend = dp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400202
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200203 ceph_decode_8_safe(&dp, dend, tkt_struct_v, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400204 if (tkt_struct_v != 1)
205 goto bad;
206
Ilya Dryomov597cda32014-09-08 17:25:34 +0400207 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
208 if (ret)
209 goto out;
210
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200211 ceph_decode_need(&dp, dend, sizeof(struct ceph_timespec), bad);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200212 ceph_decode_timespec64(&validity, dp);
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100213 dp += sizeof(struct ceph_timespec);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200214 new_expires = ktime_get_real_seconds() + validity.tv_sec;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400215 new_renew_after = new_expires - (validity.tv_sec / 4);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200216 dout(" expires=%llu renew_after=%llu\n", new_expires,
Ilya Dryomov597cda32014-09-08 17:25:34 +0400217 new_renew_after);
218
219 /* ticket blob for service */
220 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400221 if (is_enc) {
222 /* encrypted */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100223 tp = *p + ceph_x_encrypt_offset();
224 ret = ceph_x_decrypt(&th->session_key, p, end);
225 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400226 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100227 dout(" encrypted ticket, decrypted %d bytes\n", ret);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400228 ptp = &tp;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100229 tpend = tp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400230 } else {
231 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400232 ptp = p;
233 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400234 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400235 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400236 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400237 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
238 blob_struct_v = ceph_decode_8(ptp);
Ilya Dryomovd18a1242017-05-19 12:21:56 +0200239 if (blob_struct_v != 1)
240 goto bad;
241
Ilya Dryomove9226d72014-10-22 18:15:37 +0400242 new_secret_id = ceph_decode_64(ptp);
243 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400244 if (ret)
245 goto out;
246
247 /* all is well, update our ticket */
248 ceph_crypto_key_destroy(&th->session_key);
249 if (th->ticket_blob)
250 ceph_buffer_put(th->ticket_blob);
251 th->session_key = new_session_key;
252 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400253 th->secret_id = new_secret_id;
254 th->expires = new_expires;
255 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100256 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400257 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
258 type, ceph_entity_type_name(type), th->secret_id,
259 (int)th->ticket_blob->vec.iov_len);
260 xi->have_keys |= th->service;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200261 return 0;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400262
263bad:
264 ret = -EINVAL;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200265out:
266 ceph_crypto_key_destroy(&new_session_key);
267 return ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400268}
269
Sage Weilec0994e2010-02-02 16:25:35 -0800270static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
271 struct ceph_crypto_key *secret,
Ilya Dryomov285ea342020-10-26 16:47:20 +0100272 void **p, void *end)
Sage Weilec0994e2010-02-02 16:25:35 -0800273{
Sage Weilb736b3d2010-04-30 12:45:02 -0700274 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400275 u32 num;
276 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800277
Ilya Dryomov285ea342020-10-26 16:47:20 +0100278 ceph_decode_8_safe(p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700279 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400280 return -EINVAL;
281
Ilya Dryomov285ea342020-10-26 16:47:20 +0100282 ceph_decode_32_safe(p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800283 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400284
Sage Weilec0994e2010-02-02 16:25:35 -0800285 while (num--) {
Ilya Dryomov285ea342020-10-26 16:47:20 +0100286 ret = process_one_ticket(ac, secret, p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800287 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400288 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800289 }
290
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400291 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800292
293bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400294 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800295}
296
Ilya Dryomov149cac42018-07-27 16:37:54 +0200297/*
298 * Encode and encrypt the second part (ceph_x_authorize_b) of the
299 * authorizer. The first part (ceph_x_authorize_a) should already be
300 * encoded.
301 */
Ilya Dryomov6daca132018-07-27 19:18:34 +0200302static int encrypt_authorizer(struct ceph_x_authorizer *au,
303 u64 *server_challenge)
Ilya Dryomov149cac42018-07-27 16:37:54 +0200304{
305 struct ceph_x_authorize_a *msg_a;
306 struct ceph_x_authorize_b *msg_b;
307 void *p, *end;
308 int ret;
309
310 msg_a = au->buf->vec.iov_base;
311 WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id));
312 p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len);
313 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
314
315 msg_b = p + ceph_x_encrypt_offset();
Ilya Dryomov6daca132018-07-27 19:18:34 +0200316 msg_b->struct_v = 2;
Ilya Dryomov149cac42018-07-27 16:37:54 +0200317 msg_b->nonce = cpu_to_le64(au->nonce);
Ilya Dryomov6daca132018-07-27 19:18:34 +0200318 if (server_challenge) {
319 msg_b->have_challenge = 1;
320 msg_b->server_challenge_plus_one =
321 cpu_to_le64(*server_challenge + 1);
322 } else {
323 msg_b->have_challenge = 0;
324 msg_b->server_challenge_plus_one = 0;
325 }
Ilya Dryomov149cac42018-07-27 16:37:54 +0200326
327 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
328 if (ret < 0)
329 return ret;
330
331 p += ret;
Ilya Dryomov6daca132018-07-27 19:18:34 +0200332 if (server_challenge) {
333 WARN_ON(p != end);
334 } else {
335 WARN_ON(p > end);
336 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
337 }
Ilya Dryomov149cac42018-07-27 16:37:54 +0200338
339 return 0;
340}
341
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100342static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
343{
344 ceph_crypto_key_destroy(&au->session_key);
345 if (au->buf) {
346 ceph_buffer_put(au->buf);
347 au->buf = NULL;
348 }
349}
350
Sage Weilec0994e2010-02-02 16:25:35 -0800351static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
352 struct ceph_x_ticket_handler *th,
353 struct ceph_x_authorizer *au)
354{
Sage Weil807c86e2010-03-15 15:52:17 -0700355 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800356 struct ceph_x_authorize_a *msg_a;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100357 struct ceph_x_authorize_b *msg_b;
Sage Weilec0994e2010-02-02 16:25:35 -0800358 int ret;
359 int ticket_blob_len =
360 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
361
362 dout("build_authorizer for %s %p\n",
363 ceph_entity_type_name(th->service), au);
364
Yan, Zhengae385ea2014-11-04 16:32:35 +0800365 ceph_crypto_key_destroy(&au->session_key);
366 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
367 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100368 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800369
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100370 maxlen = sizeof(*msg_a) + ticket_blob_len +
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100371 ceph_x_encrypt_buflen(sizeof(*msg_b));
Sage Weil807c86e2010-03-15 15:52:17 -0700372 dout(" need len %d\n", maxlen);
373 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800374 ceph_buffer_put(au->buf);
375 au->buf = NULL;
376 }
377 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700378 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800379 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100380 ret = -ENOMEM;
381 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800382 }
Sage Weilec0994e2010-02-02 16:25:35 -0800383 }
384 au->service = th->service;
Ilya Dryomovf79e25b2020-11-27 17:18:27 +0100385 WARN_ON(!th->secret_id);
Sage Weil0bed9b52013-03-25 10:26:01 -0700386 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800387
388 msg_a = au->buf->vec.iov_base;
389 msg_a->struct_v = 1;
390 msg_a->global_id = cpu_to_le64(ac->global_id);
391 msg_a->service_id = cpu_to_le32(th->service);
392 msg_a->ticket_blob.struct_v = 1;
393 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
394 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
395 if (ticket_blob_len) {
396 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
397 th->ticket_blob->vec.iov_len);
398 }
399 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
400 le64_to_cpu(msg_a->ticket_blob.secret_id));
401
Sage Weilec0994e2010-02-02 16:25:35 -0800402 get_random_bytes(&au->nonce, sizeof(au->nonce));
Ilya Dryomov6daca132018-07-27 19:18:34 +0200403 ret = encrypt_authorizer(au, NULL);
Ilya Dryomov149cac42018-07-27 16:37:54 +0200404 if (ret) {
405 pr_err("failed to encrypt authorizer: %d", ret);
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100406 goto out_au;
Ilya Dryomov149cac42018-07-27 16:37:54 +0200407 }
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100408
Sage Weilec0994e2010-02-02 16:25:35 -0800409 dout(" built authorizer nonce %llx len %d\n", au->nonce,
410 (int)au->buf->vec.iov_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800411 return 0;
412
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100413out_au:
414 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800415 return ret;
416}
417
418static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
419 void **p, void *end)
420{
421 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
422 ceph_encode_8(p, 1);
423 ceph_encode_64(p, th->secret_id);
424 if (th->ticket_blob) {
425 const char *buf = th->ticket_blob->vec.iov_base;
426 u32 len = th->ticket_blob->vec.iov_len;
427
428 ceph_encode_32_safe(p, end, len, bad);
429 ceph_encode_copy_safe(p, end, buf, len, bad);
430 } else {
431 ceph_encode_32_safe(p, end, 0, bad);
432 }
433
434 return 0;
435bad:
436 return -ERANGE;
437}
438
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100439static bool need_key(struct ceph_x_ticket_handler *th)
440{
441 if (!th->have_key)
442 return true;
443
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200444 return ktime_get_real_seconds() >= th->renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100445}
446
447static bool have_key(struct ceph_x_ticket_handler *th)
448{
Ilya Dryomovf79e25b2020-11-27 17:18:27 +0100449 if (th->have_key && ktime_get_real_seconds() >= th->expires) {
450 dout("ticket %d (%s) secret_id %llu expired\n", th->service,
451 ceph_entity_type_name(th->service), th->secret_id);
452 th->have_key = false;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100453 }
454
455 return th->have_key;
456}
457
Sage Weilec0994e2010-02-02 16:25:35 -0800458static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
459{
460 int want = ac->want_keys;
461 struct ceph_x_info *xi = ac->private;
462 int service;
463
464 *pneed = ac->want_keys & ~(xi->have_keys);
465
466 for (service = 1; service <= want; service <<= 1) {
467 struct ceph_x_ticket_handler *th;
468
469 if (!(ac->want_keys & service))
470 continue;
471
472 if (*pneed & service)
473 continue;
474
475 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200476 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800477 *pneed |= service;
478 continue;
479 }
480
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100481 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800482 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100483 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800484 xi->have_keys &= ~service;
485 }
486}
487
Sage Weilec0994e2010-02-02 16:25:35 -0800488static int ceph_x_build_request(struct ceph_auth_client *ac,
489 void *buf, void *end)
490{
491 struct ceph_x_info *xi = ac->private;
492 int need;
493 struct ceph_x_request_header *head = buf;
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200494 void *p;
Sage Weilec0994e2010-02-02 16:25:35 -0800495 int ret;
496 struct ceph_x_ticket_handler *th =
497 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
498
Dan Carpenterb5457872010-08-26 11:12:38 +0200499 if (IS_ERR(th))
500 return PTR_ERR(th);
501
Sage Weilec0994e2010-02-02 16:25:35 -0800502 ceph_x_validate_tickets(ac, &need);
Ilya Dryomovf79e25b2020-11-27 17:18:27 +0100503 dout("%s want 0x%x have 0x%x need 0x%x\n", __func__, ac->want_keys,
504 xi->have_keys, need);
Sage Weilec0994e2010-02-02 16:25:35 -0800505
506 if (need & CEPH_ENTITY_TYPE_AUTH) {
507 struct ceph_x_authenticate *auth = (void *)(head + 1);
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100508 void *enc_buf = xi->auth_authorizer.enc_buf;
509 struct ceph_x_challenge_blob *blob = enc_buf +
510 ceph_x_encrypt_offset();
Sage Weilec0994e2010-02-02 16:25:35 -0800511 u64 *u;
512
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200513 p = auth + 1;
Sage Weilec0994e2010-02-02 16:25:35 -0800514 if (p > end)
515 return -ERANGE;
516
517 dout(" get_auth_session_key\n");
518 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
519
520 /* encrypt and hash */
521 get_random_bytes(&auth->client_challenge, sizeof(u64));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100522 blob->client_challenge = auth->client_challenge;
523 blob->server_challenge = cpu_to_le64(xi->server_challenge);
524 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
525 sizeof(*blob));
Sage Weilec0994e2010-02-02 16:25:35 -0800526 if (ret < 0)
527 return ret;
528
Ilya Dryomov7807daf2021-04-14 10:38:40 +0200529 auth->struct_v = 3; /* nautilus+ */
Sage Weilec0994e2010-02-02 16:25:35 -0800530 auth->key = 0;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100531 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700532 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800533 dout(" server_challenge %llx client_challenge %llx key %llx\n",
534 xi->server_challenge, le64_to_cpu(auth->client_challenge),
535 le64_to_cpu(auth->key));
536
537 /* now encode the old ticket if exists */
538 ret = ceph_x_encode_ticket(th, &p, end);
539 if (ret < 0)
540 return ret;
541
Ilya Dryomov285ea342020-10-26 16:47:20 +0100542 /* nautilus+: request service tickets at the same time */
543 need = ac->want_keys & ~CEPH_ENTITY_TYPE_AUTH;
544 WARN_ON(!need);
545 ceph_encode_32_safe(&p, end, need, e_range);
Sage Weilec0994e2010-02-02 16:25:35 -0800546 return p - buf;
547 }
548
549 if (need) {
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200550 dout(" get_principal_session_key\n");
Sage Weilec0994e2010-02-02 16:25:35 -0800551 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
552 if (ret)
553 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800554
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200555 p = buf;
556 ceph_encode_16_safe(&p, end, CEPHX_GET_PRINCIPAL_SESSION_KEY,
557 e_range);
558 ceph_encode_copy_safe(&p, end,
559 xi->auth_authorizer.buf->vec.iov_base,
560 xi->auth_authorizer.buf->vec.iov_len, e_range);
561 ceph_encode_8_safe(&p, end, 1, e_range);
562 ceph_encode_32_safe(&p, end, need, e_range);
Sage Weilec0994e2010-02-02 16:25:35 -0800563 return p - buf;
564 }
565
566 return 0;
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200567
568e_range:
569 return -ERANGE;
Sage Weilec0994e2010-02-02 16:25:35 -0800570}
571
Ilya Dryomov10f42b32020-12-22 19:00:48 +0100572static int decode_con_secret(void **p, void *end, u8 *con_secret,
573 int *con_secret_len)
574{
575 int len;
576
577 ceph_decode_32_safe(p, end, len, bad);
578 ceph_decode_need(p, end, len, bad);
579
580 dout("%s len %d\n", __func__, len);
581 if (con_secret) {
582 if (len > CEPH_MAX_CON_SECRET_LEN) {
583 pr_err("connection secret too big %d\n", len);
584 goto bad_memzero;
585 }
586 memcpy(con_secret, *p, len);
587 *con_secret_len = len;
588 }
589 memzero_explicit(*p, len);
590 *p += len;
591 return 0;
592
593bad_memzero:
594 memzero_explicit(*p, len);
595bad:
596 pr_err("failed to decode connection secret\n");
597 return -EINVAL;
598}
599
Ilya Dryomov285ea342020-10-26 16:47:20 +0100600static int handle_auth_session_key(struct ceph_auth_client *ac,
601 void **p, void *end,
602 u8 *session_key, int *session_key_len,
603 u8 *con_secret, int *con_secret_len)
604{
605 struct ceph_x_info *xi = ac->private;
606 struct ceph_x_ticket_handler *th;
607 void *dp, *dend;
608 int len;
609 int ret;
610
611 /* AUTH ticket */
612 ret = ceph_x_proc_ticket_reply(ac, &xi->secret, p, end);
613 if (ret)
614 return ret;
615
616 if (*p == end) {
617 /* pre-nautilus (or didn't request service tickets!) */
618 WARN_ON(session_key || con_secret);
619 return 0;
620 }
621
622 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
623 if (IS_ERR(th))
624 return PTR_ERR(th);
625
626 if (session_key) {
627 memcpy(session_key, th->session_key.key, th->session_key.len);
628 *session_key_len = th->session_key.len;
629 }
630
631 /* connection secret */
632 ceph_decode_32_safe(p, end, len, e_inval);
633 dout("%s connection secret blob len %d\n", __func__, len);
634 if (len > 0) {
635 dp = *p + ceph_x_encrypt_offset();
636 ret = ceph_x_decrypt(&th->session_key, p, *p + len);
637 if (ret < 0)
638 return ret;
639
640 dout("%s decrypted %d bytes\n", __func__, ret);
641 dend = dp + ret;
642
Ilya Dryomov10f42b32020-12-22 19:00:48 +0100643 ret = decode_con_secret(&dp, dend, con_secret, con_secret_len);
644 if (ret)
645 return ret;
Ilya Dryomov285ea342020-10-26 16:47:20 +0100646 }
647
648 /* service tickets */
649 ceph_decode_32_safe(p, end, len, e_inval);
650 dout("%s service tickets blob len %d\n", __func__, len);
651 if (len > 0) {
652 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
653 p, *p + len);
654 if (ret)
655 return ret;
656 }
657
658 return 0;
659
660e_inval:
661 return -EINVAL;
662}
663
Sage Weilec0994e2010-02-02 16:25:35 -0800664static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
Ilya Dryomov285ea342020-10-26 16:47:20 +0100665 void *buf, void *end,
666 u8 *session_key, int *session_key_len,
667 u8 *con_secret, int *con_secret_len)
Sage Weilec0994e2010-02-02 16:25:35 -0800668{
669 struct ceph_x_info *xi = ac->private;
Sage Weilec0994e2010-02-02 16:25:35 -0800670 struct ceph_x_ticket_handler *th;
671 int len = end - buf;
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200672 void *p;
Sage Weilec0994e2010-02-02 16:25:35 -0800673 int op;
674 int ret;
675
676 if (result)
677 return result; /* XXX hmm? */
678
679 if (xi->starting) {
680 /* it's a hello */
681 struct ceph_x_server_challenge *sc = buf;
682
683 if (len != sizeof(*sc))
684 return -EINVAL;
685 xi->server_challenge = le64_to_cpu(sc->server_challenge);
686 dout("handle_reply got server challenge %llx\n",
687 xi->server_challenge);
688 xi->starting = false;
689 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
690 return -EAGAIN;
691 }
692
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200693 p = buf;
694 ceph_decode_16_safe(&p, end, op, e_inval);
695 ceph_decode_32_safe(&p, end, result, e_inval);
Sage Weilec0994e2010-02-02 16:25:35 -0800696 dout("handle_reply op %d result %d\n", op, result);
697 switch (op) {
698 case CEPHX_GET_AUTH_SESSION_KEY:
Ilya Dryomov285ea342020-10-26 16:47:20 +0100699 /* AUTH ticket + [connection secret] + service tickets */
700 ret = handle_auth_session_key(ac, &p, end, session_key,
701 session_key_len, con_secret,
702 con_secret_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800703 break;
704
705 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
706 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200707 if (IS_ERR(th))
708 return PTR_ERR(th);
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200709
Ilya Dryomov285ea342020-10-26 16:47:20 +0100710 /* service tickets */
711 ret = ceph_x_proc_ticket_reply(ac, &th->session_key, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800712 break;
713
714 default:
715 return -EINVAL;
716 }
717 if (ret)
718 return ret;
719 if (ac->want_keys == xi->have_keys)
720 return 0;
721 return -EAGAIN;
Ilya Dryomov6610fff2020-10-12 15:05:09 +0200722
723e_inval:
724 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800725}
726
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200727static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
728{
729 struct ceph_x_authorizer *au = (void *)a;
730
731 ceph_x_authorizer_cleanup(au);
732 kfree(au);
733}
734
Sage Weilec0994e2010-02-02 16:25:35 -0800735static int ceph_x_create_authorizer(
736 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500737 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800738{
739 struct ceph_x_authorizer *au;
740 struct ceph_x_ticket_handler *th;
741 int ret;
742
743 th = get_ticket_handler(ac, peer_type);
744 if (IS_ERR(th))
745 return PTR_ERR(th);
746
747 au = kzalloc(sizeof(*au), GFP_NOFS);
748 if (!au)
749 return -ENOMEM;
750
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200751 au->base.destroy = ceph_x_destroy_authorizer;
752
Sage Weilec0994e2010-02-02 16:25:35 -0800753 ret = ceph_x_build_authorizer(ac, th, au);
754 if (ret) {
755 kfree(au);
756 return ret;
757 }
758
Alex Elder74f18692012-05-16 15:16:39 -0500759 auth->authorizer = (struct ceph_authorizer *) au;
760 auth->authorizer_buf = au->buf->vec.iov_base;
761 auth->authorizer_buf_len = au->buf->vec.iov_len;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100762 auth->authorizer_reply_buf = au->enc_buf;
763 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
Yan, Zheng33d07332014-11-04 16:33:37 +0800764 auth->sign_message = ac->ops->sign_message;
765 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500766
Sage Weilec0994e2010-02-02 16:25:35 -0800767 return 0;
768}
769
Sage Weil0bed9b52013-03-25 10:26:01 -0700770static int ceph_x_update_authorizer(
771 struct ceph_auth_client *ac, int peer_type,
772 struct ceph_auth_handshake *auth)
773{
774 struct ceph_x_authorizer *au;
775 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700776
777 th = get_ticket_handler(ac, peer_type);
778 if (IS_ERR(th))
779 return PTR_ERR(th);
780
781 au = (struct ceph_x_authorizer *)auth->authorizer;
782 if (au->secret_id < th->secret_id) {
783 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
784 au->service, au->secret_id, th->secret_id);
785 return ceph_x_build_authorizer(ac, th, au);
786 }
787 return 0;
788}
789
Ilya Dryomov285ea342020-10-26 16:47:20 +0100790/*
791 * CephXAuthorizeChallenge
792 */
793static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret,
794 void *challenge, int challenge_len,
795 u64 *server_challenge)
Ilya Dryomov6daca132018-07-27 19:18:34 +0200796{
Ilya Dryomov285ea342020-10-26 16:47:20 +0100797 void *dp, *dend;
Ilya Dryomov6daca132018-07-27 19:18:34 +0200798 int ret;
799
800 /* no leading len */
Ilya Dryomov285ea342020-10-26 16:47:20 +0100801 ret = __ceph_x_decrypt(secret, challenge, challenge_len);
Ilya Dryomov6daca132018-07-27 19:18:34 +0200802 if (ret < 0)
803 return ret;
Ilya Dryomov6daca132018-07-27 19:18:34 +0200804
Ilya Dryomov285ea342020-10-26 16:47:20 +0100805 dout("%s decrypted %d bytes\n", __func__, ret);
806 dp = challenge + sizeof(struct ceph_x_encrypt_header);
807 dend = dp + ret;
808
809 ceph_decode_skip_8(&dp, dend, e_inval); /* struct_v */
810 ceph_decode_64_safe(&dp, dend, *server_challenge, e_inval);
811 dout("%s server_challenge %llu\n", __func__, *server_challenge);
Ilya Dryomov6daca132018-07-27 19:18:34 +0200812 return 0;
Ilya Dryomov285ea342020-10-26 16:47:20 +0100813
814e_inval:
815 return -EINVAL;
Ilya Dryomov6daca132018-07-27 19:18:34 +0200816}
817
818static int ceph_x_add_authorizer_challenge(struct ceph_auth_client *ac,
819 struct ceph_authorizer *a,
Ilya Dryomov285ea342020-10-26 16:47:20 +0100820 void *challenge, int challenge_len)
Ilya Dryomov6daca132018-07-27 19:18:34 +0200821{
822 struct ceph_x_authorizer *au = (void *)a;
823 u64 server_challenge;
824 int ret;
825
Ilya Dryomov285ea342020-10-26 16:47:20 +0100826 ret = decrypt_authorizer_challenge(&au->session_key, challenge,
827 challenge_len, &server_challenge);
Ilya Dryomov6daca132018-07-27 19:18:34 +0200828 if (ret) {
829 pr_err("failed to decrypt authorize challenge: %d", ret);
830 return ret;
831 }
832
833 ret = encrypt_authorizer(au, &server_challenge);
834 if (ret) {
835 pr_err("failed to encrypt authorizer w/ challenge: %d", ret);
836 return ret;
837 }
838
839 return 0;
840}
841
Ilya Dryomov285ea342020-10-26 16:47:20 +0100842/*
843 * CephXAuthorizeReply
844 */
845static int decrypt_authorizer_reply(struct ceph_crypto_key *secret,
846 void **p, void *end, u64 *nonce_plus_one,
847 u8 *con_secret, int *con_secret_len)
Sage Weilec0994e2010-02-02 16:25:35 -0800848{
Ilya Dryomov285ea342020-10-26 16:47:20 +0100849 void *dp, *dend;
850 u8 struct_v;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100851 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800852
Ilya Dryomov285ea342020-10-26 16:47:20 +0100853 dp = *p + ceph_x_encrypt_offset();
854 ret = ceph_x_decrypt(secret, p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800855 if (ret < 0)
856 return ret;
Ilya Dryomov285ea342020-10-26 16:47:20 +0100857
858 dout("%s decrypted %d bytes\n", __func__, ret);
859 dend = dp + ret;
860
861 ceph_decode_8_safe(&dp, dend, struct_v, e_inval);
862 ceph_decode_64_safe(&dp, dend, *nonce_plus_one, e_inval);
863 dout("%s nonce_plus_one %llu\n", __func__, *nonce_plus_one);
864 if (struct_v >= 2) {
Ilya Dryomov10f42b32020-12-22 19:00:48 +0100865 ret = decode_con_secret(&dp, dend, con_secret, con_secret_len);
866 if (ret)
867 return ret;
Ilya Dryomovf1d10e02018-07-27 19:45:36 +0200868 }
Sage Weilec0994e2010-02-02 16:25:35 -0800869
Ilya Dryomov285ea342020-10-26 16:47:20 +0100870 return 0;
871
872e_inval:
873 return -EINVAL;
874}
875
876static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
877 struct ceph_authorizer *a,
878 void *reply, int reply_len,
879 u8 *session_key, int *session_key_len,
880 u8 *con_secret, int *con_secret_len)
881{
882 struct ceph_x_authorizer *au = (void *)a;
883 u64 nonce_plus_one;
884 int ret;
885
886 if (session_key) {
887 memcpy(session_key, au->session_key.key, au->session_key.len);
888 *session_key_len = au->session_key.len;
889 }
890
891 ret = decrypt_authorizer_reply(&au->session_key, &reply,
892 reply + reply_len, &nonce_plus_one,
893 con_secret, con_secret_len);
894 if (ret)
895 return ret;
896
897 if (nonce_plus_one != au->nonce + 1) {
898 pr_err("failed to authenticate server\n");
899 return -EPERM;
900 }
901
902 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800903}
904
Sage Weilec0994e2010-02-02 16:25:35 -0800905static void ceph_x_reset(struct ceph_auth_client *ac)
906{
907 struct ceph_x_info *xi = ac->private;
908
909 dout("reset\n");
910 xi->starting = true;
911 xi->server_challenge = 0;
912}
913
914static void ceph_x_destroy(struct ceph_auth_client *ac)
915{
916 struct ceph_x_info *xi = ac->private;
917 struct rb_node *p;
918
919 dout("ceph_x_destroy %p\n", ac);
920 ceph_crypto_key_destroy(&xi->secret);
921
922 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
923 struct ceph_x_ticket_handler *th =
924 rb_entry(p, struct ceph_x_ticket_handler, node);
925 remove_ticket_handler(ac, th);
926 }
927
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100928 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700929
Sage Weilec0994e2010-02-02 16:25:35 -0800930 kfree(ac->private);
931 ac->private = NULL;
932}
933
Ilya Dryomov187d1312016-01-14 17:31:51 +0100934static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800935{
936 struct ceph_x_ticket_handler *th;
937
938 th = get_ticket_handler(ac, peer_type);
Ilya Dryomovf79e25b2020-11-27 17:18:27 +0100939 if (IS_ERR(th))
940 return;
941
942 if (th->have_key) {
943 dout("ticket %d (%s) secret_id %llu invalidated\n",
944 th->service, ceph_entity_type_name(th->service),
945 th->secret_id);
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100946 th->have_key = false;
Ilya Dryomovf79e25b2020-11-27 17:18:27 +0100947 }
Sage Weilec0994e2010-02-02 16:25:35 -0800948}
949
Ilya Dryomov187d1312016-01-14 17:31:51 +0100950static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
951 int peer_type)
952{
953 /*
954 * We are to invalidate a service ticket in the hopes of
955 * getting a new, hopefully more valid, one. But, we won't get
956 * it unless our AUTH ticket is good, so invalidate AUTH ticket
957 * as well, just in case.
958 */
959 invalidate_ticket(ac, peer_type);
960 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
961}
962
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100963static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
964 __le64 *psig)
Yan, Zheng33d07332014-11-04 16:33:37 +0800965{
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100966 void *enc_buf = au->enc_buf;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100967 int ret;
968
Ilya Dryomovcc255c72018-07-27 19:25:32 +0200969 if (!CEPH_HAVE_FEATURE(msg->con->peer_features, CEPHX_V2)) {
970 struct {
971 __le32 len;
972 __le32 header_crc;
973 __le32 front_crc;
974 __le32 middle_crc;
975 __le32 data_crc;
976 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100977
Ilya Dryomovcc255c72018-07-27 19:25:32 +0200978 sigblock->len = cpu_to_le32(4*sizeof(u32));
979 sigblock->header_crc = msg->hdr.crc;
980 sigblock->front_crc = msg->footer.front_crc;
981 sigblock->middle_crc = msg->footer.middle_crc;
982 sigblock->data_crc = msg->footer.data_crc;
983
984 ret = ceph_x_encrypt(&au->session_key, enc_buf,
985 CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock));
986 if (ret < 0)
987 return ret;
988
989 *psig = *(__le64 *)(enc_buf + sizeof(u32));
990 } else {
991 struct {
992 __le32 header_crc;
993 __le32 front_crc;
994 __le32 front_len;
995 __le32 middle_crc;
996 __le32 middle_len;
997 __le32 data_crc;
998 __le32 data_len;
999 __le32 seq_lower_word;
1000 } __packed *sigblock = enc_buf;
1001 struct {
1002 __le64 a, b, c, d;
1003 } __packed *penc = enc_buf;
1004 int ciphertext_len;
1005
1006 sigblock->header_crc = msg->hdr.crc;
1007 sigblock->front_crc = msg->footer.front_crc;
1008 sigblock->front_len = msg->hdr.front_len;
1009 sigblock->middle_crc = msg->footer.middle_crc;
1010 sigblock->middle_len = msg->hdr.middle_len;
1011 sigblock->data_crc = msg->footer.data_crc;
1012 sigblock->data_len = msg->hdr.data_len;
1013 sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq;
1014
1015 /* no leading len, no ceph_x_encrypt_header */
1016 ret = ceph_crypt(&au->session_key, true, enc_buf,
1017 CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock),
1018 &ciphertext_len);
1019 if (ret)
1020 return ret;
1021
1022 *psig = penc->a ^ penc->b ^ penc->c ^ penc->d;
1023 }
1024
Yan, Zheng33d07332014-11-04 16:33:37 +08001025 return 0;
1026}
1027
1028static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
1029 struct ceph_msg *msg)
1030{
Ilya Dryomov4eb45172016-12-02 16:35:07 +01001031 __le64 sig;
Yan, Zheng33d07332014-11-04 16:33:37 +08001032 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +01001033
Ilya Dryomova51983e2015-10-28 23:52:06 +01001034 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
1035 return 0;
1036
Ilya Dryomov4eb45172016-12-02 16:35:07 +01001037 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
1038 msg, &sig);
1039 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +08001040 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +01001041
1042 msg->footer.sig = sig;
Yan, Zheng33d07332014-11-04 16:33:37 +08001043 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
1044 return 0;
1045}
1046
1047static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
1048 struct ceph_msg *msg)
1049{
1050 __le64 sig_check;
1051 int ret;
1052
Ilya Dryomova51983e2015-10-28 23:52:06 +01001053 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
1054 return 0;
1055
Ilya Dryomov4eb45172016-12-02 16:35:07 +01001056 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
1057 msg, &sig_check);
1058 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +08001059 return ret;
1060 if (sig_check == msg->footer.sig)
1061 return 0;
1062 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
1063 dout("ceph_x_check_message_signature %p has signature %llx "
1064 "expect %llx\n", msg, msg->footer.sig, sig_check);
1065 else
1066 dout("ceph_x_check_message_signature %p sender did not set "
1067 "CEPH_MSG_FOOTER_SIGNED\n", msg);
1068 return -EBADMSG;
1069}
Sage Weilec0994e2010-02-02 16:25:35 -08001070
1071static const struct ceph_auth_client_ops ceph_x_ops = {
1072 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -07001073 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -08001074 .build_request = ceph_x_build_request,
1075 .handle_reply = ceph_x_handle_reply,
1076 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -07001077 .update_authorizer = ceph_x_update_authorizer,
Ilya Dryomov6daca132018-07-27 19:18:34 +02001078 .add_authorizer_challenge = ceph_x_add_authorizer_challenge,
Sage Weilec0994e2010-02-02 16:25:35 -08001079 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -08001080 .invalidate_authorizer = ceph_x_invalidate_authorizer,
1081 .reset = ceph_x_reset,
1082 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +08001083 .sign_message = ceph_x_sign_message,
1084 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -08001085};
1086
1087
1088int ceph_x_init(struct ceph_auth_client *ac)
1089{
1090 struct ceph_x_info *xi;
1091 int ret;
1092
1093 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -07001094 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -08001095 xi = kzalloc(sizeof(*xi), GFP_NOFS);
1096 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -07001097 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -08001098
Sage Weilec0994e2010-02-02 16:25:35 -08001099 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -07001100 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -08001101 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -07001102 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -08001103 }
1104
Tommi Virtanen8323c3a2011-03-25 16:32:57 -07001105 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
1106 if (ret < 0) {
1107 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -07001108 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -07001109 }
Sage Weilec0994e2010-02-02 16:25:35 -08001110
1111 xi->starting = true;
1112 xi->ticket_handlers = RB_ROOT;
1113
1114 ac->protocol = CEPH_AUTH_CEPHX;
1115 ac->private = xi;
1116 ac->ops = &ceph_x_ops;
1117 return 0;
1118
Sage Weilb0930f82010-04-29 13:26:53 -07001119out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -08001120 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -07001121out:
Sage Weilec0994e2010-02-02 16:25:35 -08001122 return ret;
1123}