]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ceph/auth_x.c
ceph: fix authenticator buffer size calculation
[net-next-2.6.git] / fs / ceph / auth_x.c
CommitLineData
ec0994e4
SW
1
2#include "ceph_debug.h"
3
4#include <linux/err.h>
5#include <linux/module.h>
6#include <linux/random.h>
7
8#include "auth_x.h"
9#include "auth_x_protocol.h"
10#include "crypto.h"
11#include "auth.h"
12#include "decode.h"
13
14struct kmem_cache *ceph_x_ticketbuf_cachep;
15
16#define TEMP_TICKET_BUF_LEN 256
17
18static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
19
20static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
21{
22 struct ceph_x_info *xi = ac->private;
23 int need;
24
25 ceph_x_validate_tickets(ac, &need);
26 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
27 ac->want_keys, need, xi->have_keys);
28 return (ac->want_keys & xi->have_keys) == ac->want_keys;
29}
30
807c86e2
SW
31static int ceph_x_encrypt_buflen(int ilen)
32{
33 return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
34 sizeof(u32);
35}
36
ec0994e4
SW
37static int ceph_x_encrypt(struct ceph_crypto_key *secret,
38 void *ibuf, int ilen, void *obuf, size_t olen)
39{
40 struct ceph_x_encrypt_header head = {
41 .struct_v = 1,
42 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
43 };
44 size_t len = olen - sizeof(u32);
45 int ret;
46
47 ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
48 &head, sizeof(head), ibuf, ilen);
49 if (ret)
50 return ret;
51 ceph_encode_32(&obuf, len);
52 return len + sizeof(u32);
53}
54
55static int ceph_x_decrypt(struct ceph_crypto_key *secret,
56 void **p, void *end, void *obuf, size_t olen)
57{
58 struct ceph_x_encrypt_header head;
59 size_t head_len = sizeof(head);
60 int len, ret;
61
62 len = ceph_decode_32(p);
63 if (*p + len > end)
64 return -EINVAL;
65
66 dout("ceph_x_decrypt len %d\n", len);
67 ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen,
68 *p, len);
69 if (ret)
70 return ret;
71 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
72 return -EPERM;
73 *p += len;
74 return olen;
75}
76
77/*
78 * get existing (or insert new) ticket handler
79 */
80struct ceph_x_ticket_handler *get_ticket_handler(struct ceph_auth_client *ac,
81 int service)
82{
83 struct ceph_x_ticket_handler *th;
84 struct ceph_x_info *xi = ac->private;
85 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
86
87 while (*p) {
88 parent = *p;
89 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
90 if (service < th->service)
91 p = &(*p)->rb_left;
92 else if (service > th->service)
93 p = &(*p)->rb_right;
94 else
95 return th;
96 }
97
98 /* add it */
99 th = kzalloc(sizeof(*th), GFP_NOFS);
100 if (!th)
101 return ERR_PTR(-ENOMEM);
102 th->service = service;
103 rb_link_node(&th->node, parent, p);
104 rb_insert_color(&th->node, &xi->ticket_handlers);
105 return th;
106}
107
108static void remove_ticket_handler(struct ceph_auth_client *ac,
109 struct ceph_x_ticket_handler *th)
110{
111 struct ceph_x_info *xi = ac->private;
112
113 dout("remove_ticket_handler %p %d\n", th, th->service);
114 rb_erase(&th->node, &xi->ticket_handlers);
115 ceph_crypto_key_destroy(&th->session_key);
116 if (th->ticket_blob)
117 ceph_buffer_put(th->ticket_blob);
118 kfree(th);
119}
120
121static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
122 struct ceph_crypto_key *secret,
123 void *buf, void *end)
124{
125 struct ceph_x_info *xi = ac->private;
126 int num;
127 void *p = buf;
128 int ret;
129 char *dbuf;
130 char *ticket_buf;
131 u8 struct_v;
132
133 dbuf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, GFP_NOFS | GFP_ATOMIC);
134 if (!dbuf)
135 return -ENOMEM;
136
137 ret = -ENOMEM;
138 ticket_buf = kmem_cache_alloc(ceph_x_ticketbuf_cachep,
139 GFP_NOFS | GFP_ATOMIC);
140 if (!ticket_buf)
141 goto out_dbuf;
142
143 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
144 struct_v = ceph_decode_8(&p);
145 if (struct_v != 1)
146 goto bad;
147 num = ceph_decode_32(&p);
148 dout("%d tickets\n", num);
149 while (num--) {
150 int type;
151 u8 struct_v;
152 struct ceph_x_ticket_handler *th;
153 void *dp, *dend;
154 int dlen;
155 char is_enc;
156 struct timespec validity;
157 struct ceph_crypto_key old_key;
158 void *tp, *tpend;
159
160 ceph_decode_need(&p, end, sizeof(u32) + 1, bad);
161
162 type = ceph_decode_32(&p);
163 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
164
165 struct_v = ceph_decode_8(&p);
166 if (struct_v != 1)
167 goto bad;
168
169 th = get_ticket_handler(ac, type);
170 if (IS_ERR(th)) {
171 ret = PTR_ERR(th);
172 goto out;
173 }
174
175 /* blob for me */
176 dlen = ceph_x_decrypt(secret, &p, end, dbuf,
177 TEMP_TICKET_BUF_LEN);
178 if (dlen <= 0) {
179 ret = dlen;
180 goto out;
181 }
182 dout(" decrypted %d bytes\n", dlen);
183 dend = dbuf + dlen;
184 dp = dbuf;
185
186 struct_v = ceph_decode_8(&dp);
187 if (struct_v != 1)
188 goto bad;
189
190 memcpy(&old_key, &th->session_key, sizeof(old_key));
191 ret = ceph_crypto_key_decode(&th->session_key, &dp, dend);
192 if (ret)
193 goto out;
194
195 ceph_decode_copy(&dp, &th->validity, sizeof(th->validity));
196 ceph_decode_timespec(&validity, &th->validity);
197 th->expires = get_seconds() + validity.tv_sec;
198 th->renew_after = th->expires - (validity.tv_sec / 4);
199 dout(" expires=%lu renew_after=%lu\n", th->expires,
200 th->renew_after);
201
202 /* ticket blob for service */
203 ceph_decode_8_safe(&p, end, is_enc, bad);
204 tp = ticket_buf;
205 if (is_enc) {
206 /* encrypted */
207 dout(" encrypted ticket\n");
208 dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf,
209 TEMP_TICKET_BUF_LEN);
210 if (dlen < 0) {
211 ret = dlen;
212 goto out;
213 }
214 dlen = ceph_decode_32(&tp);
215 } else {
216 /* unencrypted */
217 ceph_decode_32_safe(&p, end, dlen, bad);
218 ceph_decode_need(&p, end, dlen, bad);
219 ceph_decode_copy(&p, ticket_buf, dlen);
220 }
221 tpend = tp + dlen;
222 dout(" ticket blob is %d bytes\n", dlen);
223 ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
224 struct_v = ceph_decode_8(&tp);
225 th->secret_id = ceph_decode_64(&tp);
226 ret = ceph_decode_buffer(&th->ticket_blob, &tp, tpend);
227 if (ret)
228 goto out;
229 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
230 type, ceph_entity_type_name(type), th->secret_id,
231 (int)th->ticket_blob->vec.iov_len);
232 xi->have_keys |= th->service;
233 }
234
235 ret = 0;
236out:
237 kmem_cache_free(ceph_x_ticketbuf_cachep, ticket_buf);
238out_dbuf:
239 kmem_cache_free(ceph_x_ticketbuf_cachep, dbuf);
240 return ret;
241
242bad:
243 ret = -EINVAL;
244 goto out;
245}
246
247static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
248 struct ceph_x_ticket_handler *th,
249 struct ceph_x_authorizer *au)
250{
807c86e2 251 int maxlen;
ec0994e4
SW
252 struct ceph_x_authorize_a *msg_a;
253 struct ceph_x_authorize_b msg_b;
254 void *p, *end;
255 int ret;
256 int ticket_blob_len =
257 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
258
259 dout("build_authorizer for %s %p\n",
260 ceph_entity_type_name(th->service), au);
261
807c86e2
SW
262 maxlen = sizeof(*msg_a) + sizeof(msg_b) +
263 ceph_x_encrypt_buflen(ticket_blob_len);
264 dout(" need len %d\n", maxlen);
265 if (au->buf && au->buf->alloc_len < maxlen) {
ec0994e4
SW
266 ceph_buffer_put(au->buf);
267 au->buf = NULL;
268 }
269 if (!au->buf) {
807c86e2 270 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
ec0994e4
SW
271 if (!au->buf)
272 return -ENOMEM;
273 }
274 au->service = th->service;
275
276 msg_a = au->buf->vec.iov_base;
277 msg_a->struct_v = 1;
278 msg_a->global_id = cpu_to_le64(ac->global_id);
279 msg_a->service_id = cpu_to_le32(th->service);
280 msg_a->ticket_blob.struct_v = 1;
281 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
282 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
283 if (ticket_blob_len) {
284 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
285 th->ticket_blob->vec.iov_len);
286 }
287 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
288 le64_to_cpu(msg_a->ticket_blob.secret_id));
289
290 p = msg_a + 1;
291 p += ticket_blob_len;
292 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
293
294 get_random_bytes(&au->nonce, sizeof(au->nonce));
295 msg_b.struct_v = 1;
296 msg_b.nonce = cpu_to_le64(au->nonce);
297 ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
298 p, end - p);
299 if (ret < 0)
300 goto out_buf;
301 p += ret;
302 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
303 dout(" built authorizer nonce %llx len %d\n", au->nonce,
304 (int)au->buf->vec.iov_len);
807c86e2 305 BUG_ON(au->buf->vec.iov_len > maxlen);
ec0994e4
SW
306 return 0;
307
308out_buf:
309 ceph_buffer_put(au->buf);
310 au->buf = NULL;
311 return ret;
312}
313
314static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
315 void **p, void *end)
316{
317 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
318 ceph_encode_8(p, 1);
319 ceph_encode_64(p, th->secret_id);
320 if (th->ticket_blob) {
321 const char *buf = th->ticket_blob->vec.iov_base;
322 u32 len = th->ticket_blob->vec.iov_len;
323
324 ceph_encode_32_safe(p, end, len, bad);
325 ceph_encode_copy_safe(p, end, buf, len, bad);
326 } else {
327 ceph_encode_32_safe(p, end, 0, bad);
328 }
329
330 return 0;
331bad:
332 return -ERANGE;
333}
334
335static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
336{
337 int want = ac->want_keys;
338 struct ceph_x_info *xi = ac->private;
339 int service;
340
341 *pneed = ac->want_keys & ~(xi->have_keys);
342
343 for (service = 1; service <= want; service <<= 1) {
344 struct ceph_x_ticket_handler *th;
345
346 if (!(ac->want_keys & service))
347 continue;
348
349 if (*pneed & service)
350 continue;
351
352 th = get_ticket_handler(ac, service);
353
354 if (!th) {
355 *pneed |= service;
356 continue;
357 }
358
359 if (get_seconds() >= th->renew_after)
360 *pneed |= service;
361 if (get_seconds() >= th->expires)
362 xi->have_keys &= ~service;
363 }
364}
365
366
367static int ceph_x_build_request(struct ceph_auth_client *ac,
368 void *buf, void *end)
369{
370 struct ceph_x_info *xi = ac->private;
371 int need;
372 struct ceph_x_request_header *head = buf;
373 int ret;
374 struct ceph_x_ticket_handler *th =
375 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
376
377 ceph_x_validate_tickets(ac, &need);
378
379 dout("build_request want %x have %x need %x\n",
380 ac->want_keys, xi->have_keys, need);
381
382 if (need & CEPH_ENTITY_TYPE_AUTH) {
383 struct ceph_x_authenticate *auth = (void *)(head + 1);
384 void *p = auth + 1;
385 struct ceph_x_challenge_blob tmp;
386 char tmp_enc[40];
387 u64 *u;
388
389 if (p > end)
390 return -ERANGE;
391
392 dout(" get_auth_session_key\n");
393 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
394
395 /* encrypt and hash */
396 get_random_bytes(&auth->client_challenge, sizeof(u64));
397 tmp.client_challenge = auth->client_challenge;
398 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
399 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
400 tmp_enc, sizeof(tmp_enc));
401 if (ret < 0)
402 return ret;
403
404 auth->struct_v = 1;
405 auth->key = 0;
406 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
407 auth->key ^= *u;
408 dout(" server_challenge %llx client_challenge %llx key %llx\n",
409 xi->server_challenge, le64_to_cpu(auth->client_challenge),
410 le64_to_cpu(auth->key));
411
412 /* now encode the old ticket if exists */
413 ret = ceph_x_encode_ticket(th, &p, end);
414 if (ret < 0)
415 return ret;
416
417 return p - buf;
418 }
419
420 if (need) {
421 void *p = head + 1;
422 struct ceph_x_service_ticket_request *req;
423
424 if (p > end)
425 return -ERANGE;
426 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
427
428 BUG_ON(!th);
429 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
430 if (ret)
431 return ret;
432 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
433 xi->auth_authorizer.buf->vec.iov_len);
434
435 req = p;
436 req->keys = cpu_to_le32(need);
437 p += sizeof(*req);
438 return p - buf;
439 }
440
441 return 0;
442}
443
444static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
445 void *buf, void *end)
446{
447 struct ceph_x_info *xi = ac->private;
448 struct ceph_x_reply_header *head = buf;
449 struct ceph_x_ticket_handler *th;
450 int len = end - buf;
451 int op;
452 int ret;
453
454 if (result)
455 return result; /* XXX hmm? */
456
457 if (xi->starting) {
458 /* it's a hello */
459 struct ceph_x_server_challenge *sc = buf;
460
461 if (len != sizeof(*sc))
462 return -EINVAL;
463 xi->server_challenge = le64_to_cpu(sc->server_challenge);
464 dout("handle_reply got server challenge %llx\n",
465 xi->server_challenge);
466 xi->starting = false;
467 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
468 return -EAGAIN;
469 }
470
471 op = le32_to_cpu(head->op);
472 result = le32_to_cpu(head->result);
473 dout("handle_reply op %d result %d\n", op, result);
474 switch (op) {
475 case CEPHX_GET_AUTH_SESSION_KEY:
476 /* verify auth key */
477 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
478 buf + sizeof(*head), end);
479 break;
480
481 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
482 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
483 BUG_ON(!th);
484 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
485 buf + sizeof(*head), end);
486 break;
487
488 default:
489 return -EINVAL;
490 }
491 if (ret)
492 return ret;
493 if (ac->want_keys == xi->have_keys)
494 return 0;
495 return -EAGAIN;
496}
497
498static int ceph_x_create_authorizer(
499 struct ceph_auth_client *ac, int peer_type,
500 struct ceph_authorizer **a,
501 void **buf, size_t *len,
502 void **reply_buf, size_t *reply_len)
503{
504 struct ceph_x_authorizer *au;
505 struct ceph_x_ticket_handler *th;
506 int ret;
507
508 th = get_ticket_handler(ac, peer_type);
509 if (IS_ERR(th))
510 return PTR_ERR(th);
511
512 au = kzalloc(sizeof(*au), GFP_NOFS);
513 if (!au)
514 return -ENOMEM;
515
516 ret = ceph_x_build_authorizer(ac, th, au);
517 if (ret) {
518 kfree(au);
519 return ret;
520 }
521
522 *a = (struct ceph_authorizer *)au;
523 *buf = au->buf->vec.iov_base;
524 *len = au->buf->vec.iov_len;
525 *reply_buf = au->reply_buf;
526 *reply_len = sizeof(au->reply_buf);
527 return 0;
528}
529
530static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
531 struct ceph_authorizer *a, size_t len)
532{
533 struct ceph_x_authorizer *au = (void *)a;
534 struct ceph_x_ticket_handler *th;
535 int ret = 0;
536 struct ceph_x_authorize_reply reply;
537 void *p = au->reply_buf;
538 void *end = p + sizeof(au->reply_buf);
539
540 th = get_ticket_handler(ac, au->service);
541 if (!th)
542 return -EIO; /* hrm! */
543 ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply));
544 if (ret < 0)
545 return ret;
546 if (ret != sizeof(reply))
547 return -EPERM;
548
549 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
550 ret = -EPERM;
551 else
552 ret = 0;
553 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
554 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
555 return ret;
556}
557
558static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
559 struct ceph_authorizer *a)
560{
561 struct ceph_x_authorizer *au = (void *)a;
562
563 ceph_buffer_put(au->buf);
564 kfree(au);
565}
566
567
568static void ceph_x_reset(struct ceph_auth_client *ac)
569{
570 struct ceph_x_info *xi = ac->private;
571
572 dout("reset\n");
573 xi->starting = true;
574 xi->server_challenge = 0;
575}
576
577static void ceph_x_destroy(struct ceph_auth_client *ac)
578{
579 struct ceph_x_info *xi = ac->private;
580 struct rb_node *p;
581
582 dout("ceph_x_destroy %p\n", ac);
583 ceph_crypto_key_destroy(&xi->secret);
584
585 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
586 struct ceph_x_ticket_handler *th =
587 rb_entry(p, struct ceph_x_ticket_handler, node);
588 remove_ticket_handler(ac, th);
589 }
590
591 kmem_cache_destroy(ceph_x_ticketbuf_cachep);
592
593 kfree(ac->private);
594 ac->private = NULL;
595}
596
597static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
598 int peer_type)
599{
600 struct ceph_x_ticket_handler *th;
601
602 th = get_ticket_handler(ac, peer_type);
603 if (th && !IS_ERR(th))
604 remove_ticket_handler(ac, th);
605}
606
607
608static const struct ceph_auth_client_ops ceph_x_ops = {
609 .is_authenticated = ceph_x_is_authenticated,
610 .build_request = ceph_x_build_request,
611 .handle_reply = ceph_x_handle_reply,
612 .create_authorizer = ceph_x_create_authorizer,
613 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
614 .destroy_authorizer = ceph_x_destroy_authorizer,
615 .invalidate_authorizer = ceph_x_invalidate_authorizer,
616 .reset = ceph_x_reset,
617 .destroy = ceph_x_destroy,
618};
619
620
621int ceph_x_init(struct ceph_auth_client *ac)
622{
623 struct ceph_x_info *xi;
624 int ret;
625
626 dout("ceph_x_init %p\n", ac);
627 xi = kzalloc(sizeof(*xi), GFP_NOFS);
628 if (!xi)
629 return -ENOMEM;
630
631 ret = -ENOMEM;
632 ceph_x_ticketbuf_cachep = kmem_cache_create("ceph_x_ticketbuf",
633 TEMP_TICKET_BUF_LEN, 8,
634 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
635 NULL);
636 if (!ceph_x_ticketbuf_cachep)
637 goto done_nomem;
638 ret = -EINVAL;
639 if (!ac->secret) {
640 pr_err("no secret set (for auth_x protocol)\n");
641 goto done_nomem;
642 }
643
644 ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret);
645 if (ret)
646 goto done_nomem;
647
648 xi->starting = true;
649 xi->ticket_handlers = RB_ROOT;
650
651 ac->protocol = CEPH_AUTH_CEPHX;
652 ac->private = xi;
653 ac->ops = &ceph_x_ops;
654 return 0;
655
656done_nomem:
657 kfree(xi);
658 if (ceph_x_ticketbuf_cachep)
659 kmem_cache_destroy(ceph_x_ticketbuf_cachep);
660 return ret;
661}
662
663