Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/ceph/ceph_debug.h>
0004 
0005 #include <linux/err.h>
0006 #include <linux/module.h>
0007 #include <linux/random.h>
0008 #include <linux/slab.h>
0009 
0010 #include <linux/ceph/decode.h>
0011 #include <linux/ceph/auth.h>
0012 #include <linux/ceph/ceph_features.h>
0013 #include <linux/ceph/libceph.h>
0014 #include <linux/ceph/messenger.h>
0015 
0016 #include "crypto.h"
0017 #include "auth_x.h"
0018 #include "auth_x_protocol.h"
0019 
0020 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
0021 
0022 static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
0023 {
0024     struct ceph_x_info *xi = ac->private;
0025     int missing;
0026     int need;  /* missing + need renewal */
0027 
0028     ceph_x_validate_tickets(ac, &need);
0029     missing = ac->want_keys & ~xi->have_keys;
0030     WARN_ON((need & missing) != missing);
0031     dout("%s want 0x%x have 0x%x missing 0x%x -> %d\n", __func__,
0032          ac->want_keys, xi->have_keys, missing, !missing);
0033     return !missing;
0034 }
0035 
0036 static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
0037 {
0038     struct ceph_x_info *xi = ac->private;
0039     int need;
0040 
0041     ceph_x_validate_tickets(ac, &need);
0042     dout("%s want 0x%x have 0x%x need 0x%x -> %d\n", __func__,
0043          ac->want_keys, xi->have_keys, need, !!need);
0044     return !!need;
0045 }
0046 
0047 static int ceph_x_encrypt_offset(void)
0048 {
0049     return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
0050 }
0051 
0052 static int ceph_x_encrypt_buflen(int ilen)
0053 {
0054     return ceph_x_encrypt_offset() + ilen + 16;
0055 }
0056 
0057 static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
0058               int buf_len, int plaintext_len)
0059 {
0060     struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
0061     int ciphertext_len;
0062     int ret;
0063 
0064     hdr->struct_v = 1;
0065     hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
0066 
0067     ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
0068              plaintext_len + sizeof(struct ceph_x_encrypt_header),
0069              &ciphertext_len);
0070     if (ret)
0071         return ret;
0072 
0073     ceph_encode_32(&buf, ciphertext_len);
0074     return sizeof(u32) + ciphertext_len;
0075 }
0076 
0077 static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
0078                 int ciphertext_len)
0079 {
0080     struct ceph_x_encrypt_header *hdr = p;
0081     int plaintext_len;
0082     int ret;
0083 
0084     ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
0085              &plaintext_len);
0086     if (ret)
0087         return ret;
0088 
0089     if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
0090         pr_err("%s bad magic\n", __func__);
0091         return -EINVAL;
0092     }
0093 
0094     return plaintext_len - sizeof(*hdr);
0095 }
0096 
0097 static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
0098 {
0099     int ciphertext_len;
0100     int ret;
0101 
0102     ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
0103     ceph_decode_need(p, end, ciphertext_len, e_inval);
0104 
0105     ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
0106     if (ret < 0)
0107         return ret;
0108 
0109     *p += ciphertext_len;
0110     return ret;
0111 
0112 e_inval:
0113     return -EINVAL;
0114 }
0115 
0116 /*
0117  * get existing (or insert new) ticket handler
0118  */
0119 static struct ceph_x_ticket_handler *
0120 get_ticket_handler(struct ceph_auth_client *ac, int service)
0121 {
0122     struct ceph_x_ticket_handler *th;
0123     struct ceph_x_info *xi = ac->private;
0124     struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
0125 
0126     while (*p) {
0127         parent = *p;
0128         th = rb_entry(parent, struct ceph_x_ticket_handler, node);
0129         if (service < th->service)
0130             p = &(*p)->rb_left;
0131         else if (service > th->service)
0132             p = &(*p)->rb_right;
0133         else
0134             return th;
0135     }
0136 
0137     /* add it */
0138     th = kzalloc(sizeof(*th), GFP_NOFS);
0139     if (!th)
0140         return ERR_PTR(-ENOMEM);
0141     th->service = service;
0142     rb_link_node(&th->node, parent, p);
0143     rb_insert_color(&th->node, &xi->ticket_handlers);
0144     return th;
0145 }
0146 
0147 static void remove_ticket_handler(struct ceph_auth_client *ac,
0148                   struct ceph_x_ticket_handler *th)
0149 {
0150     struct ceph_x_info *xi = ac->private;
0151 
0152     dout("remove_ticket_handler %p %d\n", th, th->service);
0153     rb_erase(&th->node, &xi->ticket_handlers);
0154     ceph_crypto_key_destroy(&th->session_key);
0155     if (th->ticket_blob)
0156         ceph_buffer_put(th->ticket_blob);
0157     kfree(th);
0158 }
0159 
0160 static int process_one_ticket(struct ceph_auth_client *ac,
0161                   struct ceph_crypto_key *secret,
0162                   void **p, void *end)
0163 {
0164     struct ceph_x_info *xi = ac->private;
0165     int type;
0166     u8 tkt_struct_v, blob_struct_v;
0167     struct ceph_x_ticket_handler *th;
0168     void *dp, *dend;
0169     int dlen;
0170     char is_enc;
0171     struct timespec64 validity;
0172     void *tp, *tpend;
0173     void **ptp;
0174     struct ceph_crypto_key new_session_key = { 0 };
0175     struct ceph_buffer *new_ticket_blob;
0176     time64_t new_expires, new_renew_after;
0177     u64 new_secret_id;
0178     int ret;
0179 
0180     ceph_decode_need(p, end, sizeof(u32) + 1, bad);
0181 
0182     type = ceph_decode_32(p);
0183     dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
0184 
0185     tkt_struct_v = ceph_decode_8(p);
0186     if (tkt_struct_v != 1)
0187         goto bad;
0188 
0189     th = get_ticket_handler(ac, type);
0190     if (IS_ERR(th)) {
0191         ret = PTR_ERR(th);
0192         goto out;
0193     }
0194 
0195     /* blob for me */
0196     dp = *p + ceph_x_encrypt_offset();
0197     ret = ceph_x_decrypt(secret, p, end);
0198     if (ret < 0)
0199         goto out;
0200     dout(" decrypted %d bytes\n", ret);
0201     dend = dp + ret;
0202 
0203     ceph_decode_8_safe(&dp, dend, tkt_struct_v, bad);
0204     if (tkt_struct_v != 1)
0205         goto bad;
0206 
0207     ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
0208     if (ret)
0209         goto out;
0210 
0211     ceph_decode_need(&dp, dend, sizeof(struct ceph_timespec), bad);
0212     ceph_decode_timespec64(&validity, dp);
0213     dp += sizeof(struct ceph_timespec);
0214     new_expires = ktime_get_real_seconds() + validity.tv_sec;
0215     new_renew_after = new_expires - (validity.tv_sec / 4);
0216     dout(" expires=%llu renew_after=%llu\n", new_expires,
0217          new_renew_after);
0218 
0219     /* ticket blob for service */
0220     ceph_decode_8_safe(p, end, is_enc, bad);
0221     if (is_enc) {
0222         /* encrypted */
0223         tp = *p + ceph_x_encrypt_offset();
0224         ret = ceph_x_decrypt(&th->session_key, p, end);
0225         if (ret < 0)
0226             goto out;
0227         dout(" encrypted ticket, decrypted %d bytes\n", ret);
0228         ptp = &tp;
0229         tpend = tp + ret;
0230     } else {
0231         /* unencrypted */
0232         ptp = p;
0233         tpend = end;
0234     }
0235     ceph_decode_32_safe(ptp, tpend, dlen, bad);
0236     dout(" ticket blob is %d bytes\n", dlen);
0237     ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
0238     blob_struct_v = ceph_decode_8(ptp);
0239     if (blob_struct_v != 1)
0240         goto bad;
0241 
0242     new_secret_id = ceph_decode_64(ptp);
0243     ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
0244     if (ret)
0245         goto out;
0246 
0247     /* all is well, update our ticket */
0248     ceph_crypto_key_destroy(&th->session_key);
0249     if (th->ticket_blob)
0250         ceph_buffer_put(th->ticket_blob);
0251     th->session_key = new_session_key;
0252     th->ticket_blob = new_ticket_blob;
0253     th->secret_id = new_secret_id;
0254     th->expires = new_expires;
0255     th->renew_after = new_renew_after;
0256     th->have_key = true;
0257     dout(" got ticket service %d (%s) secret_id %lld len %d\n",
0258          type, ceph_entity_type_name(type), th->secret_id,
0259          (int)th->ticket_blob->vec.iov_len);
0260     xi->have_keys |= th->service;
0261     return 0;
0262 
0263 bad:
0264     ret = -EINVAL;
0265 out:
0266     ceph_crypto_key_destroy(&new_session_key);
0267     return ret;
0268 }
0269 
0270 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
0271                     struct ceph_crypto_key *secret,
0272                     void **p, void *end)
0273 {
0274     u8 reply_struct_v;
0275     u32 num;
0276     int ret;
0277 
0278     ceph_decode_8_safe(p, end, reply_struct_v, bad);
0279     if (reply_struct_v != 1)
0280         return -EINVAL;
0281 
0282     ceph_decode_32_safe(p, end, num, bad);
0283     dout("%d tickets\n", num);
0284 
0285     while (num--) {
0286         ret = process_one_ticket(ac, secret, p, end);
0287         if (ret)
0288             return ret;
0289     }
0290 
0291     return 0;
0292 
0293 bad:
0294     return -EINVAL;
0295 }
0296 
0297 /*
0298  * Encode and encrypt the second part (ceph_x_authorize_b) of the
0299  * authorizer.  The first part (ceph_x_authorize_a) should already be
0300  * encoded.
0301  */
0302 static int encrypt_authorizer(struct ceph_x_authorizer *au,
0303                   u64 *server_challenge)
0304 {
0305     struct ceph_x_authorize_a *msg_a;
0306     struct ceph_x_authorize_b *msg_b;
0307     void *p, *end;
0308     int ret;
0309 
0310     msg_a = au->buf->vec.iov_base;
0311     WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id));
0312     p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len);
0313     end = au->buf->vec.iov_base + au->buf->vec.iov_len;
0314 
0315     msg_b = p + ceph_x_encrypt_offset();
0316     msg_b->struct_v = 2;
0317     msg_b->nonce = cpu_to_le64(au->nonce);
0318     if (server_challenge) {
0319         msg_b->have_challenge = 1;
0320         msg_b->server_challenge_plus_one =
0321             cpu_to_le64(*server_challenge + 1);
0322     } else {
0323         msg_b->have_challenge = 0;
0324         msg_b->server_challenge_plus_one = 0;
0325     }
0326 
0327     ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
0328     if (ret < 0)
0329         return ret;
0330 
0331     p += ret;
0332     if (server_challenge) {
0333         WARN_ON(p != end);
0334     } else {
0335         WARN_ON(p > end);
0336         au->buf->vec.iov_len = p - au->buf->vec.iov_base;
0337     }
0338 
0339     return 0;
0340 }
0341 
0342 static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
0343 {
0344     ceph_crypto_key_destroy(&au->session_key);
0345     if (au->buf) {
0346         ceph_buffer_put(au->buf);
0347         au->buf = NULL;
0348     }
0349 }
0350 
0351 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
0352                    struct ceph_x_ticket_handler *th,
0353                    struct ceph_x_authorizer *au)
0354 {
0355     int maxlen;
0356     struct ceph_x_authorize_a *msg_a;
0357     struct ceph_x_authorize_b *msg_b;
0358     int ret;
0359     int ticket_blob_len =
0360         (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
0361 
0362     dout("build_authorizer for %s %p\n",
0363          ceph_entity_type_name(th->service), au);
0364 
0365     ceph_crypto_key_destroy(&au->session_key);
0366     ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
0367     if (ret)
0368         goto out_au;
0369 
0370     maxlen = sizeof(*msg_a) + ticket_blob_len +
0371         ceph_x_encrypt_buflen(sizeof(*msg_b));
0372     dout("  need len %d\n", maxlen);
0373     if (au->buf && au->buf->alloc_len < maxlen) {
0374         ceph_buffer_put(au->buf);
0375         au->buf = NULL;
0376     }
0377     if (!au->buf) {
0378         au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
0379         if (!au->buf) {
0380             ret = -ENOMEM;
0381             goto out_au;
0382         }
0383     }
0384     au->service = th->service;
0385     WARN_ON(!th->secret_id);
0386     au->secret_id = th->secret_id;
0387 
0388     msg_a = au->buf->vec.iov_base;
0389     msg_a->struct_v = 1;
0390     msg_a->global_id = cpu_to_le64(ac->global_id);
0391     msg_a->service_id = cpu_to_le32(th->service);
0392     msg_a->ticket_blob.struct_v = 1;
0393     msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
0394     msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
0395     if (ticket_blob_len) {
0396         memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
0397                th->ticket_blob->vec.iov_len);
0398     }
0399     dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
0400          le64_to_cpu(msg_a->ticket_blob.secret_id));
0401 
0402     get_random_bytes(&au->nonce, sizeof(au->nonce));
0403     ret = encrypt_authorizer(au, NULL);
0404     if (ret) {
0405         pr_err("failed to encrypt authorizer: %d", ret);
0406         goto out_au;
0407     }
0408 
0409     dout(" built authorizer nonce %llx len %d\n", au->nonce,
0410          (int)au->buf->vec.iov_len);
0411     return 0;
0412 
0413 out_au:
0414     ceph_x_authorizer_cleanup(au);
0415     return ret;
0416 }
0417 
0418 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
0419                 void **p, void *end)
0420 {
0421     ceph_decode_need(p, end, 1 + sizeof(u64), bad);
0422     ceph_encode_8(p, 1);
0423     ceph_encode_64(p, th->secret_id);
0424     if (th->ticket_blob) {
0425         const char *buf = th->ticket_blob->vec.iov_base;
0426         u32 len = th->ticket_blob->vec.iov_len;
0427 
0428         ceph_encode_32_safe(p, end, len, bad);
0429         ceph_encode_copy_safe(p, end, buf, len, bad);
0430     } else {
0431         ceph_encode_32_safe(p, end, 0, bad);
0432     }
0433 
0434     return 0;
0435 bad:
0436     return -ERANGE;
0437 }
0438 
0439 static bool need_key(struct ceph_x_ticket_handler *th)
0440 {
0441     if (!th->have_key)
0442         return true;
0443 
0444     return ktime_get_real_seconds() >= th->renew_after;
0445 }
0446 
0447 static bool have_key(struct ceph_x_ticket_handler *th)
0448 {
0449     if (th->have_key && ktime_get_real_seconds() >= th->expires) {
0450         dout("ticket %d (%s) secret_id %llu expired\n", th->service,
0451              ceph_entity_type_name(th->service), th->secret_id);
0452         th->have_key = false;
0453     }
0454 
0455     return th->have_key;
0456 }
0457 
0458 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
0459 {
0460     int want = ac->want_keys;
0461     struct ceph_x_info *xi = ac->private;
0462     int service;
0463 
0464     *pneed = ac->want_keys & ~(xi->have_keys);
0465 
0466     for (service = 1; service <= want; service <<= 1) {
0467         struct ceph_x_ticket_handler *th;
0468 
0469         if (!(ac->want_keys & service))
0470             continue;
0471 
0472         if (*pneed & service)
0473             continue;
0474 
0475         th = get_ticket_handler(ac, service);
0476         if (IS_ERR(th)) {
0477             *pneed |= service;
0478             continue;
0479         }
0480 
0481         if (need_key(th))
0482             *pneed |= service;
0483         if (!have_key(th))
0484             xi->have_keys &= ~service;
0485     }
0486 }
0487 
0488 static int ceph_x_build_request(struct ceph_auth_client *ac,
0489                 void *buf, void *end)
0490 {
0491     struct ceph_x_info *xi = ac->private;
0492     int need;
0493     struct ceph_x_request_header *head = buf;
0494     void *p;
0495     int ret;
0496     struct ceph_x_ticket_handler *th =
0497         get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
0498 
0499     if (IS_ERR(th))
0500         return PTR_ERR(th);
0501 
0502     ceph_x_validate_tickets(ac, &need);
0503     dout("%s want 0x%x have 0x%x need 0x%x\n", __func__, ac->want_keys,
0504          xi->have_keys, need);
0505 
0506     if (need & CEPH_ENTITY_TYPE_AUTH) {
0507         struct ceph_x_authenticate *auth = (void *)(head + 1);
0508         void *enc_buf = xi->auth_authorizer.enc_buf;
0509         struct ceph_x_challenge_blob *blob = enc_buf +
0510                             ceph_x_encrypt_offset();
0511         u64 *u;
0512 
0513         p = auth + 1;
0514         if (p > end)
0515             return -ERANGE;
0516 
0517         dout(" get_auth_session_key\n");
0518         head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
0519 
0520         /* encrypt and hash */
0521         get_random_bytes(&auth->client_challenge, sizeof(u64));
0522         blob->client_challenge = auth->client_challenge;
0523         blob->server_challenge = cpu_to_le64(xi->server_challenge);
0524         ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
0525                      sizeof(*blob));
0526         if (ret < 0)
0527             return ret;
0528 
0529         auth->struct_v = 3;  /* nautilus+ */
0530         auth->key = 0;
0531         for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
0532             auth->key ^= *(__le64 *)u;
0533         dout(" server_challenge %llx client_challenge %llx key %llx\n",
0534              xi->server_challenge, le64_to_cpu(auth->client_challenge),
0535              le64_to_cpu(auth->key));
0536 
0537         /* now encode the old ticket if exists */
0538         ret = ceph_x_encode_ticket(th, &p, end);
0539         if (ret < 0)
0540             return ret;
0541 
0542         /* nautilus+: request service tickets at the same time */
0543         need = ac->want_keys & ~CEPH_ENTITY_TYPE_AUTH;
0544         WARN_ON(!need);
0545         ceph_encode_32_safe(&p, end, need, e_range);
0546         return p - buf;
0547     }
0548 
0549     if (need) {
0550         dout(" get_principal_session_key\n");
0551         ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
0552         if (ret)
0553             return ret;
0554 
0555         p = buf;
0556         ceph_encode_16_safe(&p, end, CEPHX_GET_PRINCIPAL_SESSION_KEY,
0557                     e_range);
0558         ceph_encode_copy_safe(&p, end,
0559             xi->auth_authorizer.buf->vec.iov_base,
0560             xi->auth_authorizer.buf->vec.iov_len, e_range);
0561         ceph_encode_8_safe(&p, end, 1, e_range);
0562         ceph_encode_32_safe(&p, end, need, e_range);
0563         return p - buf;
0564     }
0565 
0566     return 0;
0567 
0568 e_range:
0569     return -ERANGE;
0570 }
0571 
0572 static int decode_con_secret(void **p, void *end, u8 *con_secret,
0573                  int *con_secret_len)
0574 {
0575     int len;
0576 
0577     ceph_decode_32_safe(p, end, len, bad);
0578     ceph_decode_need(p, end, len, bad);
0579 
0580     dout("%s len %d\n", __func__, len);
0581     if (con_secret) {
0582         if (len > CEPH_MAX_CON_SECRET_LEN) {
0583             pr_err("connection secret too big %d\n", len);
0584             goto bad_memzero;
0585         }
0586         memcpy(con_secret, *p, len);
0587         *con_secret_len = len;
0588     }
0589     memzero_explicit(*p, len);
0590     *p += len;
0591     return 0;
0592 
0593 bad_memzero:
0594     memzero_explicit(*p, len);
0595 bad:
0596     pr_err("failed to decode connection secret\n");
0597     return -EINVAL;
0598 }
0599 
0600 static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id,
0601                    void **p, void *end,
0602                    u8 *session_key, int *session_key_len,
0603                    u8 *con_secret, int *con_secret_len)
0604 {
0605     struct ceph_x_info *xi = ac->private;
0606     struct ceph_x_ticket_handler *th;
0607     void *dp, *dend;
0608     int len;
0609     int ret;
0610 
0611     /* AUTH ticket */
0612     ret = ceph_x_proc_ticket_reply(ac, &xi->secret, p, end);
0613     if (ret)
0614         return ret;
0615 
0616     ceph_auth_set_global_id(ac, global_id);
0617     if (*p == end) {
0618         /* pre-nautilus (or didn't request service tickets!) */
0619         WARN_ON(session_key || con_secret);
0620         return 0;
0621     }
0622 
0623     th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
0624     if (IS_ERR(th))
0625         return PTR_ERR(th);
0626 
0627     if (session_key) {
0628         memcpy(session_key, th->session_key.key, th->session_key.len);
0629         *session_key_len = th->session_key.len;
0630     }
0631 
0632     /* connection secret */
0633     ceph_decode_32_safe(p, end, len, e_inval);
0634     dout("%s connection secret blob len %d\n", __func__, len);
0635     if (len > 0) {
0636         dp = *p + ceph_x_encrypt_offset();
0637         ret = ceph_x_decrypt(&th->session_key, p, *p + len);
0638         if (ret < 0)
0639             return ret;
0640 
0641         dout("%s decrypted %d bytes\n", __func__, ret);
0642         dend = dp + ret;
0643 
0644         ret = decode_con_secret(&dp, dend, con_secret, con_secret_len);
0645         if (ret)
0646             return ret;
0647     }
0648 
0649     /* service tickets */
0650     ceph_decode_32_safe(p, end, len, e_inval);
0651     dout("%s service tickets blob len %d\n", __func__, len);
0652     if (len > 0) {
0653         ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
0654                            p, *p + len);
0655         if (ret)
0656             return ret;
0657     }
0658 
0659     return 0;
0660 
0661 e_inval:
0662     return -EINVAL;
0663 }
0664 
0665 static int ceph_x_handle_reply(struct ceph_auth_client *ac, u64 global_id,
0666                    void *buf, void *end,
0667                    u8 *session_key, int *session_key_len,
0668                    u8 *con_secret, int *con_secret_len)
0669 {
0670     struct ceph_x_info *xi = ac->private;
0671     struct ceph_x_ticket_handler *th;
0672     int len = end - buf;
0673     int result;
0674     void *p;
0675     int op;
0676     int ret;
0677 
0678     if (xi->starting) {
0679         /* it's a hello */
0680         struct ceph_x_server_challenge *sc = buf;
0681 
0682         if (len != sizeof(*sc))
0683             return -EINVAL;
0684         xi->server_challenge = le64_to_cpu(sc->server_challenge);
0685         dout("handle_reply got server challenge %llx\n",
0686              xi->server_challenge);
0687         xi->starting = false;
0688         xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
0689         return -EAGAIN;
0690     }
0691 
0692     p = buf;
0693     ceph_decode_16_safe(&p, end, op, e_inval);
0694     ceph_decode_32_safe(&p, end, result, e_inval);
0695     dout("handle_reply op %d result %d\n", op, result);
0696     switch (op) {
0697     case CEPHX_GET_AUTH_SESSION_KEY:
0698         /* AUTH ticket + [connection secret] + service tickets */
0699         ret = handle_auth_session_key(ac, global_id, &p, end,
0700                           session_key, session_key_len,
0701                           con_secret, con_secret_len);
0702         break;
0703 
0704     case CEPHX_GET_PRINCIPAL_SESSION_KEY:
0705         th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
0706         if (IS_ERR(th))
0707             return PTR_ERR(th);
0708 
0709         /* service tickets */
0710         ret = ceph_x_proc_ticket_reply(ac, &th->session_key, &p, end);
0711         break;
0712 
0713     default:
0714         return -EINVAL;
0715     }
0716     if (ret)
0717         return ret;
0718     if (ac->want_keys == xi->have_keys)
0719         return 0;
0720     return -EAGAIN;
0721 
0722 e_inval:
0723     return -EINVAL;
0724 }
0725 
0726 static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
0727 {
0728     struct ceph_x_authorizer *au = (void *)a;
0729 
0730     ceph_x_authorizer_cleanup(au);
0731     kfree(au);
0732 }
0733 
0734 static int ceph_x_create_authorizer(
0735     struct ceph_auth_client *ac, int peer_type,
0736     struct ceph_auth_handshake *auth)
0737 {
0738     struct ceph_x_authorizer *au;
0739     struct ceph_x_ticket_handler *th;
0740     int ret;
0741 
0742     th = get_ticket_handler(ac, peer_type);
0743     if (IS_ERR(th))
0744         return PTR_ERR(th);
0745 
0746     au = kzalloc(sizeof(*au), GFP_NOFS);
0747     if (!au)
0748         return -ENOMEM;
0749 
0750     au->base.destroy = ceph_x_destroy_authorizer;
0751 
0752     ret = ceph_x_build_authorizer(ac, th, au);
0753     if (ret) {
0754         kfree(au);
0755         return ret;
0756     }
0757 
0758     auth->authorizer = (struct ceph_authorizer *) au;
0759     auth->authorizer_buf = au->buf->vec.iov_base;
0760     auth->authorizer_buf_len = au->buf->vec.iov_len;
0761     auth->authorizer_reply_buf = au->enc_buf;
0762     auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
0763     auth->sign_message = ac->ops->sign_message;
0764     auth->check_message_signature = ac->ops->check_message_signature;
0765 
0766     return 0;
0767 }
0768 
0769 static int ceph_x_update_authorizer(
0770     struct ceph_auth_client *ac, int peer_type,
0771     struct ceph_auth_handshake *auth)
0772 {
0773     struct ceph_x_authorizer *au;
0774     struct ceph_x_ticket_handler *th;
0775 
0776     th = get_ticket_handler(ac, peer_type);
0777     if (IS_ERR(th))
0778         return PTR_ERR(th);
0779 
0780     au = (struct ceph_x_authorizer *)auth->authorizer;
0781     if (au->secret_id < th->secret_id) {
0782         dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
0783              au->service, au->secret_id, th->secret_id);
0784         return ceph_x_build_authorizer(ac, th, au);
0785     }
0786     return 0;
0787 }
0788 
0789 /*
0790  * CephXAuthorizeChallenge
0791  */
0792 static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret,
0793                     void *challenge, int challenge_len,
0794                     u64 *server_challenge)
0795 {
0796     void *dp, *dend;
0797     int ret;
0798 
0799     /* no leading len */
0800     ret = __ceph_x_decrypt(secret, challenge, challenge_len);
0801     if (ret < 0)
0802         return ret;
0803 
0804     dout("%s decrypted %d bytes\n", __func__, ret);
0805     dp = challenge + sizeof(struct ceph_x_encrypt_header);
0806     dend = dp + ret;
0807 
0808     ceph_decode_skip_8(&dp, dend, e_inval);  /* struct_v */
0809     ceph_decode_64_safe(&dp, dend, *server_challenge, e_inval);
0810     dout("%s server_challenge %llu\n", __func__, *server_challenge);
0811     return 0;
0812 
0813 e_inval:
0814     return -EINVAL;
0815 }
0816 
0817 static int ceph_x_add_authorizer_challenge(struct ceph_auth_client *ac,
0818                        struct ceph_authorizer *a,
0819                        void *challenge, int challenge_len)
0820 {
0821     struct ceph_x_authorizer *au = (void *)a;
0822     u64 server_challenge;
0823     int ret;
0824 
0825     ret = decrypt_authorizer_challenge(&au->session_key, challenge,
0826                        challenge_len, &server_challenge);
0827     if (ret) {
0828         pr_err("failed to decrypt authorize challenge: %d", ret);
0829         return ret;
0830     }
0831 
0832     ret = encrypt_authorizer(au, &server_challenge);
0833     if (ret) {
0834         pr_err("failed to encrypt authorizer w/ challenge: %d", ret);
0835         return ret;
0836     }
0837 
0838     return 0;
0839 }
0840 
0841 /*
0842  * CephXAuthorizeReply
0843  */
0844 static int decrypt_authorizer_reply(struct ceph_crypto_key *secret,
0845                     void **p, void *end, u64 *nonce_plus_one,
0846                     u8 *con_secret, int *con_secret_len)
0847 {
0848     void *dp, *dend;
0849     u8 struct_v;
0850     int ret;
0851 
0852     dp = *p + ceph_x_encrypt_offset();
0853     ret = ceph_x_decrypt(secret, p, end);
0854     if (ret < 0)
0855         return ret;
0856 
0857     dout("%s decrypted %d bytes\n", __func__, ret);
0858     dend = dp + ret;
0859 
0860     ceph_decode_8_safe(&dp, dend, struct_v, e_inval);
0861     ceph_decode_64_safe(&dp, dend, *nonce_plus_one, e_inval);
0862     dout("%s nonce_plus_one %llu\n", __func__, *nonce_plus_one);
0863     if (struct_v >= 2) {
0864         ret = decode_con_secret(&dp, dend, con_secret, con_secret_len);
0865         if (ret)
0866             return ret;
0867     }
0868 
0869     return 0;
0870 
0871 e_inval:
0872     return -EINVAL;
0873 }
0874 
0875 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
0876                       struct ceph_authorizer *a,
0877                       void *reply, int reply_len,
0878                       u8 *session_key, int *session_key_len,
0879                       u8 *con_secret, int *con_secret_len)
0880 {
0881     struct ceph_x_authorizer *au = (void *)a;
0882     u64 nonce_plus_one;
0883     int ret;
0884 
0885     if (session_key) {
0886         memcpy(session_key, au->session_key.key, au->session_key.len);
0887         *session_key_len = au->session_key.len;
0888     }
0889 
0890     ret = decrypt_authorizer_reply(&au->session_key, &reply,
0891                        reply + reply_len, &nonce_plus_one,
0892                        con_secret, con_secret_len);
0893     if (ret)
0894         return ret;
0895 
0896     if (nonce_plus_one != au->nonce + 1) {
0897         pr_err("failed to authenticate server\n");
0898         return -EPERM;
0899     }
0900 
0901     return 0;
0902 }
0903 
0904 static void ceph_x_reset(struct ceph_auth_client *ac)
0905 {
0906     struct ceph_x_info *xi = ac->private;
0907 
0908     dout("reset\n");
0909     xi->starting = true;
0910     xi->server_challenge = 0;
0911 }
0912 
0913 static void ceph_x_destroy(struct ceph_auth_client *ac)
0914 {
0915     struct ceph_x_info *xi = ac->private;
0916     struct rb_node *p;
0917 
0918     dout("ceph_x_destroy %p\n", ac);
0919     ceph_crypto_key_destroy(&xi->secret);
0920 
0921     while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
0922         struct ceph_x_ticket_handler *th =
0923             rb_entry(p, struct ceph_x_ticket_handler, node);
0924         remove_ticket_handler(ac, th);
0925     }
0926 
0927     ceph_x_authorizer_cleanup(&xi->auth_authorizer);
0928 
0929     kfree(ac->private);
0930     ac->private = NULL;
0931 }
0932 
0933 static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
0934 {
0935     struct ceph_x_ticket_handler *th;
0936 
0937     th = get_ticket_handler(ac, peer_type);
0938     if (IS_ERR(th))
0939         return;
0940 
0941     if (th->have_key) {
0942         dout("ticket %d (%s) secret_id %llu invalidated\n",
0943              th->service, ceph_entity_type_name(th->service),
0944              th->secret_id);
0945         th->have_key = false;
0946     }
0947 }
0948 
0949 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
0950                      int peer_type)
0951 {
0952     /*
0953      * We are to invalidate a service ticket in the hopes of
0954      * getting a new, hopefully more valid, one.  But, we won't get
0955      * it unless our AUTH ticket is good, so invalidate AUTH ticket
0956      * as well, just in case.
0957      */
0958     invalidate_ticket(ac, peer_type);
0959     invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
0960 }
0961 
0962 static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
0963               __le64 *psig)
0964 {
0965     void *enc_buf = au->enc_buf;
0966     int ret;
0967 
0968     if (!CEPH_HAVE_FEATURE(msg->con->peer_features, CEPHX_V2)) {
0969         struct {
0970             __le32 len;
0971             __le32 header_crc;
0972             __le32 front_crc;
0973             __le32 middle_crc;
0974             __le32 data_crc;
0975         } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
0976 
0977         sigblock->len = cpu_to_le32(4*sizeof(u32));
0978         sigblock->header_crc = msg->hdr.crc;
0979         sigblock->front_crc = msg->footer.front_crc;
0980         sigblock->middle_crc = msg->footer.middle_crc;
0981         sigblock->data_crc =  msg->footer.data_crc;
0982 
0983         ret = ceph_x_encrypt(&au->session_key, enc_buf,
0984                      CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock));
0985         if (ret < 0)
0986             return ret;
0987 
0988         *psig = *(__le64 *)(enc_buf + sizeof(u32));
0989     } else {
0990         struct {
0991             __le32 header_crc;
0992             __le32 front_crc;
0993             __le32 front_len;
0994             __le32 middle_crc;
0995             __le32 middle_len;
0996             __le32 data_crc;
0997             __le32 data_len;
0998             __le32 seq_lower_word;
0999         } __packed *sigblock = enc_buf;
1000         struct {
1001             __le64 a, b, c, d;
1002         } __packed *penc = enc_buf;
1003         int ciphertext_len;
1004 
1005         sigblock->header_crc = msg->hdr.crc;
1006         sigblock->front_crc = msg->footer.front_crc;
1007         sigblock->front_len = msg->hdr.front_len;
1008         sigblock->middle_crc = msg->footer.middle_crc;
1009         sigblock->middle_len = msg->hdr.middle_len;
1010         sigblock->data_crc =  msg->footer.data_crc;
1011         sigblock->data_len = msg->hdr.data_len;
1012         sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq;
1013 
1014         /* no leading len, no ceph_x_encrypt_header */
1015         ret = ceph_crypt(&au->session_key, true, enc_buf,
1016                  CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock),
1017                  &ciphertext_len);
1018         if (ret)
1019             return ret;
1020 
1021         *psig = penc->a ^ penc->b ^ penc->c ^ penc->d;
1022     }
1023 
1024     return 0;
1025 }
1026 
1027 static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
1028                    struct ceph_msg *msg)
1029 {
1030     __le64 sig;
1031     int ret;
1032 
1033     if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
1034         return 0;
1035 
1036     ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
1037                  msg, &sig);
1038     if (ret)
1039         return ret;
1040 
1041     msg->footer.sig = sig;
1042     msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
1043     return 0;
1044 }
1045 
1046 static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
1047                       struct ceph_msg *msg)
1048 {
1049     __le64 sig_check;
1050     int ret;
1051 
1052     if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
1053         return 0;
1054 
1055     ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
1056                  msg, &sig_check);
1057     if (ret)
1058         return ret;
1059     if (sig_check == msg->footer.sig)
1060         return 0;
1061     if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
1062         dout("ceph_x_check_message_signature %p has signature %llx "
1063              "expect %llx\n", msg, msg->footer.sig, sig_check);
1064     else
1065         dout("ceph_x_check_message_signature %p sender did not set "
1066              "CEPH_MSG_FOOTER_SIGNED\n", msg);
1067     return -EBADMSG;
1068 }
1069 
1070 static const struct ceph_auth_client_ops ceph_x_ops = {
1071     .is_authenticated = ceph_x_is_authenticated,
1072     .should_authenticate = ceph_x_should_authenticate,
1073     .build_request = ceph_x_build_request,
1074     .handle_reply = ceph_x_handle_reply,
1075     .create_authorizer = ceph_x_create_authorizer,
1076     .update_authorizer = ceph_x_update_authorizer,
1077     .add_authorizer_challenge = ceph_x_add_authorizer_challenge,
1078     .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
1079     .invalidate_authorizer = ceph_x_invalidate_authorizer,
1080     .reset =  ceph_x_reset,
1081     .destroy = ceph_x_destroy,
1082     .sign_message = ceph_x_sign_message,
1083     .check_message_signature = ceph_x_check_message_signature,
1084 };
1085 
1086 
1087 int ceph_x_init(struct ceph_auth_client *ac)
1088 {
1089     struct ceph_x_info *xi;
1090     int ret;
1091 
1092     dout("ceph_x_init %p\n", ac);
1093     ret = -ENOMEM;
1094     xi = kzalloc(sizeof(*xi), GFP_NOFS);
1095     if (!xi)
1096         goto out;
1097 
1098     ret = -EINVAL;
1099     if (!ac->key) {
1100         pr_err("no secret set (for auth_x protocol)\n");
1101         goto out_nomem;
1102     }
1103 
1104     ret = ceph_crypto_key_clone(&xi->secret, ac->key);
1105     if (ret < 0) {
1106         pr_err("cannot clone key: %d\n", ret);
1107         goto out_nomem;
1108     }
1109 
1110     xi->starting = true;
1111     xi->ticket_handlers = RB_ROOT;
1112 
1113     ac->protocol = CEPH_AUTH_CEPHX;
1114     ac->private = xi;
1115     ac->ops = &ceph_x_ops;
1116     return 0;
1117 
1118 out_nomem:
1119     kfree(xi);
1120 out:
1121     return ret;
1122 }