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