Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 2 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 4 | |
| 5 | #include <linux/err.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 9 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 10 | #include <linux/ceph/decode.h> |
| 11 | #include <linux/ceph/auth.h> |
Ilya Dryomov | cc255c7 | 2018-07-27 19:25:32 +0200 | [diff] [blame^] | 12 | #include <linux/ceph/ceph_features.h> |
Ilya Dryomov | a51983e | 2015-10-28 23:52:06 +0100 | [diff] [blame] | 13 | #include <linux/ceph/libceph.h> |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 14 | #include <linux/ceph/messenger.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 15 | |
| 16 | #include "crypto.h" |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 17 | #include "auth_x.h" |
| 18 | #include "auth_x_protocol.h" |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 19 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 20 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); |
| 21 | |
| 22 | static int ceph_x_is_authenticated(struct ceph_auth_client *ac) |
| 23 | { |
| 24 | struct ceph_x_info *xi = ac->private; |
| 25 | int need; |
| 26 | |
| 27 | ceph_x_validate_tickets(ac, &need); |
| 28 | dout("ceph_x_is_authenticated want=%d need=%d have=%d\n", |
| 29 | ac->want_keys, need, xi->have_keys); |
| 30 | return (ac->want_keys & xi->have_keys) == ac->want_keys; |
| 31 | } |
| 32 | |
Sage Weil | a41359f | 2010-05-25 15:39:06 -0700 | [diff] [blame] | 33 | static int ceph_x_should_authenticate(struct ceph_auth_client *ac) |
| 34 | { |
| 35 | struct ceph_x_info *xi = ac->private; |
| 36 | int need; |
| 37 | |
| 38 | ceph_x_validate_tickets(ac, &need); |
| 39 | dout("ceph_x_should_authenticate want=%d need=%d have=%d\n", |
| 40 | ac->want_keys, need, xi->have_keys); |
| 41 | return need != 0; |
| 42 | } |
| 43 | |
Ilya Dryomov | 55d9cc83 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 44 | static int ceph_x_encrypt_offset(void) |
| 45 | { |
| 46 | return sizeof(u32) + sizeof(struct ceph_x_encrypt_header); |
| 47 | } |
| 48 | |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 49 | static int ceph_x_encrypt_buflen(int ilen) |
| 50 | { |
Ilya Dryomov | 55d9cc83 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 51 | return ceph_x_encrypt_offset() + ilen + 16; |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 54 | static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf, |
| 55 | int buf_len, int plaintext_len) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 56 | { |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 57 | struct ceph_x_encrypt_header *hdr = buf + sizeof(u32); |
| 58 | int ciphertext_len; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 59 | int ret; |
| 60 | |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 61 | hdr->struct_v = 1; |
| 62 | hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC); |
| 63 | |
| 64 | ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32), |
| 65 | plaintext_len + sizeof(struct ceph_x_encrypt_header), |
| 66 | &ciphertext_len); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 67 | if (ret) |
| 68 | return ret; |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 69 | |
| 70 | ceph_encode_32(&buf, ciphertext_len); |
| 71 | return sizeof(u32) + ciphertext_len; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Ilya Dryomov | c571fe2 | 2018-07-26 18:05:43 +0200 | [diff] [blame] | 74 | static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p, |
| 75 | int ciphertext_len) |
| 76 | { |
| 77 | struct ceph_x_encrypt_header *hdr = p; |
| 78 | int plaintext_len; |
| 79 | int ret; |
| 80 | |
| 81 | ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len, |
| 82 | &plaintext_len); |
| 83 | if (ret) |
| 84 | return ret; |
| 85 | |
| 86 | if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) { |
| 87 | pr_err("%s bad magic\n", __func__); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | return plaintext_len - sizeof(*hdr); |
| 92 | } |
| 93 | |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 94 | static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 95 | { |
Ilya Dryomov | c571fe2 | 2018-07-26 18:05:43 +0200 | [diff] [blame] | 96 | int ciphertext_len; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 97 | int ret; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 98 | |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 99 | ceph_decode_32_safe(p, end, ciphertext_len, e_inval); |
| 100 | ceph_decode_need(p, end, ciphertext_len, e_inval); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 101 | |
Ilya Dryomov | c571fe2 | 2018-07-26 18:05:43 +0200 | [diff] [blame] | 102 | ret = __ceph_x_decrypt(secret, *p, ciphertext_len); |
| 103 | if (ret < 0) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 104 | return ret; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 105 | |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 106 | *p += ciphertext_len; |
Ilya Dryomov | c571fe2 | 2018-07-26 18:05:43 +0200 | [diff] [blame] | 107 | return ret; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 108 | |
| 109 | e_inval: |
| 110 | return -EINVAL; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
| 114 | * get existing (or insert new) ticket handler |
| 115 | */ |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 116 | static struct ceph_x_ticket_handler * |
| 117 | get_ticket_handler(struct ceph_auth_client *ac, int service) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 118 | { |
| 119 | struct ceph_x_ticket_handler *th; |
| 120 | struct ceph_x_info *xi = ac->private; |
| 121 | struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node; |
| 122 | |
| 123 | while (*p) { |
| 124 | parent = *p; |
| 125 | th = rb_entry(parent, struct ceph_x_ticket_handler, node); |
| 126 | if (service < th->service) |
| 127 | p = &(*p)->rb_left; |
| 128 | else if (service > th->service) |
| 129 | p = &(*p)->rb_right; |
| 130 | else |
| 131 | return th; |
| 132 | } |
| 133 | |
| 134 | /* add it */ |
| 135 | th = kzalloc(sizeof(*th), GFP_NOFS); |
| 136 | if (!th) |
| 137 | return ERR_PTR(-ENOMEM); |
| 138 | th->service = service; |
| 139 | rb_link_node(&th->node, parent, p); |
| 140 | rb_insert_color(&th->node, &xi->ticket_handlers); |
| 141 | return th; |
| 142 | } |
| 143 | |
| 144 | static void remove_ticket_handler(struct ceph_auth_client *ac, |
| 145 | struct ceph_x_ticket_handler *th) |
| 146 | { |
| 147 | struct ceph_x_info *xi = ac->private; |
| 148 | |
| 149 | dout("remove_ticket_handler %p %d\n", th, th->service); |
| 150 | rb_erase(&th->node, &xi->ticket_handlers); |
| 151 | ceph_crypto_key_destroy(&th->session_key); |
| 152 | if (th->ticket_blob) |
| 153 | ceph_buffer_put(th->ticket_blob); |
| 154 | kfree(th); |
| 155 | } |
| 156 | |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 157 | static int process_one_ticket(struct ceph_auth_client *ac, |
| 158 | struct ceph_crypto_key *secret, |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 159 | void **p, void *end) |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 160 | { |
| 161 | struct ceph_x_info *xi = ac->private; |
| 162 | int type; |
| 163 | u8 tkt_struct_v, blob_struct_v; |
| 164 | struct ceph_x_ticket_handler *th; |
| 165 | void *dp, *dend; |
| 166 | int dlen; |
| 167 | char is_enc; |
Arnd Bergmann | 473bd2d | 2018-07-13 22:18:34 +0200 | [diff] [blame] | 168 | struct timespec64 validity; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 169 | void *tp, *tpend; |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 170 | void **ptp; |
Ilya Dryomov | b51456a | 2017-05-19 14:24:36 +0200 | [diff] [blame] | 171 | struct ceph_crypto_key new_session_key = { 0 }; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 172 | struct ceph_buffer *new_ticket_blob; |
Arnd Bergmann | 473bd2d | 2018-07-13 22:18:34 +0200 | [diff] [blame] | 173 | time64_t new_expires, new_renew_after; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 174 | u64 new_secret_id; |
| 175 | int ret; |
| 176 | |
| 177 | ceph_decode_need(p, end, sizeof(u32) + 1, bad); |
| 178 | |
| 179 | type = ceph_decode_32(p); |
| 180 | dout(" ticket type %d %s\n", type, ceph_entity_type_name(type)); |
| 181 | |
| 182 | tkt_struct_v = ceph_decode_8(p); |
| 183 | if (tkt_struct_v != 1) |
| 184 | goto bad; |
| 185 | |
| 186 | th = get_ticket_handler(ac, type); |
| 187 | if (IS_ERR(th)) { |
| 188 | ret = PTR_ERR(th); |
| 189 | goto out; |
| 190 | } |
| 191 | |
| 192 | /* blob for me */ |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 193 | dp = *p + ceph_x_encrypt_offset(); |
| 194 | ret = ceph_x_decrypt(secret, p, end); |
| 195 | if (ret < 0) |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 196 | goto out; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 197 | dout(" decrypted %d bytes\n", ret); |
| 198 | dend = dp + ret; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 199 | |
| 200 | tkt_struct_v = ceph_decode_8(&dp); |
| 201 | if (tkt_struct_v != 1) |
| 202 | goto bad; |
| 203 | |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 204 | ret = ceph_crypto_key_decode(&new_session_key, &dp, dend); |
| 205 | if (ret) |
| 206 | goto out; |
| 207 | |
Arnd Bergmann | 473bd2d | 2018-07-13 22:18:34 +0200 | [diff] [blame] | 208 | ceph_decode_timespec64(&validity, dp); |
Ilya Dryomov | f6cdb29 | 2016-01-15 13:20:01 +0100 | [diff] [blame] | 209 | dp += sizeof(struct ceph_timespec); |
Arnd Bergmann | 473bd2d | 2018-07-13 22:18:34 +0200 | [diff] [blame] | 210 | new_expires = ktime_get_real_seconds() + validity.tv_sec; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 211 | new_renew_after = new_expires - (validity.tv_sec / 4); |
Arnd Bergmann | 473bd2d | 2018-07-13 22:18:34 +0200 | [diff] [blame] | 212 | dout(" expires=%llu renew_after=%llu\n", new_expires, |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 213 | new_renew_after); |
| 214 | |
| 215 | /* ticket blob for service */ |
| 216 | ceph_decode_8_safe(p, end, is_enc, bad); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 217 | if (is_enc) { |
| 218 | /* encrypted */ |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 219 | tp = *p + ceph_x_encrypt_offset(); |
| 220 | ret = ceph_x_decrypt(&th->session_key, p, end); |
| 221 | if (ret < 0) |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 222 | goto out; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 223 | dout(" encrypted ticket, decrypted %d bytes\n", ret); |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 224 | ptp = &tp; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 225 | tpend = tp + ret; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 226 | } else { |
| 227 | /* unencrypted */ |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 228 | ptp = p; |
| 229 | tpend = end; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 230 | } |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 231 | ceph_decode_32_safe(ptp, tpend, dlen, bad); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 232 | dout(" ticket blob is %d bytes\n", dlen); |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 233 | ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad); |
| 234 | blob_struct_v = ceph_decode_8(ptp); |
Ilya Dryomov | d18a124 | 2017-05-19 12:21:56 +0200 | [diff] [blame] | 235 | if (blob_struct_v != 1) |
| 236 | goto bad; |
| 237 | |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 238 | new_secret_id = ceph_decode_64(ptp); |
| 239 | ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 240 | if (ret) |
| 241 | goto out; |
| 242 | |
| 243 | /* all is well, update our ticket */ |
| 244 | ceph_crypto_key_destroy(&th->session_key); |
| 245 | if (th->ticket_blob) |
| 246 | ceph_buffer_put(th->ticket_blob); |
| 247 | th->session_key = new_session_key; |
| 248 | th->ticket_blob = new_ticket_blob; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 249 | th->secret_id = new_secret_id; |
| 250 | th->expires = new_expires; |
| 251 | th->renew_after = new_renew_after; |
Ilya Dryomov | 6abe097 | 2016-01-14 16:35:35 +0100 | [diff] [blame] | 252 | th->have_key = true; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 253 | dout(" got ticket service %d (%s) secret_id %lld len %d\n", |
| 254 | type, ceph_entity_type_name(type), th->secret_id, |
| 255 | (int)th->ticket_blob->vec.iov_len); |
| 256 | xi->have_keys |= th->service; |
Ilya Dryomov | b51456a | 2017-05-19 14:24:36 +0200 | [diff] [blame] | 257 | return 0; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 258 | |
| 259 | bad: |
| 260 | ret = -EINVAL; |
Ilya Dryomov | b51456a | 2017-05-19 14:24:36 +0200 | [diff] [blame] | 261 | out: |
| 262 | ceph_crypto_key_destroy(&new_session_key); |
| 263 | return ret; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 264 | } |
| 265 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 266 | static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac, |
| 267 | struct ceph_crypto_key *secret, |
| 268 | void *buf, void *end) |
| 269 | { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 270 | void *p = buf; |
Sage Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 271 | u8 reply_struct_v; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 272 | u32 num; |
| 273 | int ret; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 274 | |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 275 | ceph_decode_8_safe(&p, end, reply_struct_v, bad); |
Sage Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 276 | if (reply_struct_v != 1) |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 277 | return -EINVAL; |
| 278 | |
| 279 | ceph_decode_32_safe(&p, end, num, bad); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 280 | dout("%d tickets\n", num); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 281 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 282 | while (num--) { |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 283 | ret = process_one_ticket(ac, secret, &p, end); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 284 | if (ret) |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 285 | return ret; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 288 | return 0; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 289 | |
| 290 | bad: |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 291 | return -EINVAL; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Ilya Dryomov | 149cac4 | 2018-07-27 16:37:54 +0200 | [diff] [blame] | 294 | /* |
| 295 | * Encode and encrypt the second part (ceph_x_authorize_b) of the |
| 296 | * authorizer. The first part (ceph_x_authorize_a) should already be |
| 297 | * encoded. |
| 298 | */ |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 299 | static int encrypt_authorizer(struct ceph_x_authorizer *au, |
| 300 | u64 *server_challenge) |
Ilya Dryomov | 149cac4 | 2018-07-27 16:37:54 +0200 | [diff] [blame] | 301 | { |
| 302 | struct ceph_x_authorize_a *msg_a; |
| 303 | struct ceph_x_authorize_b *msg_b; |
| 304 | void *p, *end; |
| 305 | int ret; |
| 306 | |
| 307 | msg_a = au->buf->vec.iov_base; |
| 308 | WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id)); |
| 309 | p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len); |
| 310 | end = au->buf->vec.iov_base + au->buf->vec.iov_len; |
| 311 | |
| 312 | msg_b = p + ceph_x_encrypt_offset(); |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 313 | msg_b->struct_v = 2; |
Ilya Dryomov | 149cac4 | 2018-07-27 16:37:54 +0200 | [diff] [blame] | 314 | msg_b->nonce = cpu_to_le64(au->nonce); |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 315 | if (server_challenge) { |
| 316 | msg_b->have_challenge = 1; |
| 317 | msg_b->server_challenge_plus_one = |
| 318 | cpu_to_le64(*server_challenge + 1); |
| 319 | } else { |
| 320 | msg_b->have_challenge = 0; |
| 321 | msg_b->server_challenge_plus_one = 0; |
| 322 | } |
Ilya Dryomov | 149cac4 | 2018-07-27 16:37:54 +0200 | [diff] [blame] | 323 | |
| 324 | ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b)); |
| 325 | if (ret < 0) |
| 326 | return ret; |
| 327 | |
| 328 | p += ret; |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 329 | if (server_challenge) { |
| 330 | WARN_ON(p != end); |
| 331 | } else { |
| 332 | WARN_ON(p > end); |
| 333 | au->buf->vec.iov_len = p - au->buf->vec.iov_base; |
| 334 | } |
Ilya Dryomov | 149cac4 | 2018-07-27 16:37:54 +0200 | [diff] [blame] | 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Ilya Dryomov | cbf99a1 | 2015-10-26 11:03:46 +0100 | [diff] [blame] | 339 | static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au) |
| 340 | { |
| 341 | ceph_crypto_key_destroy(&au->session_key); |
| 342 | if (au->buf) { |
| 343 | ceph_buffer_put(au->buf); |
| 344 | au->buf = NULL; |
| 345 | } |
| 346 | } |
| 347 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 348 | static int ceph_x_build_authorizer(struct ceph_auth_client *ac, |
| 349 | struct ceph_x_ticket_handler *th, |
| 350 | struct ceph_x_authorizer *au) |
| 351 | { |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 352 | int maxlen; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 353 | struct ceph_x_authorize_a *msg_a; |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 354 | struct ceph_x_authorize_b *msg_b; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 355 | int ret; |
| 356 | int ticket_blob_len = |
| 357 | (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0); |
| 358 | |
| 359 | dout("build_authorizer for %s %p\n", |
| 360 | ceph_entity_type_name(th->service), au); |
| 361 | |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame] | 362 | ceph_crypto_key_destroy(&au->session_key); |
| 363 | ret = ceph_crypto_key_clone(&au->session_key, &th->session_key); |
| 364 | if (ret) |
Ilya Dryomov | cbf99a1 | 2015-10-26 11:03:46 +0100 | [diff] [blame] | 365 | goto out_au; |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame] | 366 | |
Ilya Dryomov | 36721ec | 2016-12-02 16:35:06 +0100 | [diff] [blame] | 367 | maxlen = sizeof(*msg_a) + ticket_blob_len + |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 368 | ceph_x_encrypt_buflen(sizeof(*msg_b)); |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 369 | dout(" need len %d\n", maxlen); |
| 370 | if (au->buf && au->buf->alloc_len < maxlen) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 371 | ceph_buffer_put(au->buf); |
| 372 | au->buf = NULL; |
| 373 | } |
| 374 | if (!au->buf) { |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 375 | au->buf = ceph_buffer_new(maxlen, GFP_NOFS); |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame] | 376 | if (!au->buf) { |
Ilya Dryomov | cbf99a1 | 2015-10-26 11:03:46 +0100 | [diff] [blame] | 377 | ret = -ENOMEM; |
| 378 | goto out_au; |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame] | 379 | } |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 380 | } |
| 381 | au->service = th->service; |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 382 | au->secret_id = th->secret_id; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 383 | |
| 384 | msg_a = au->buf->vec.iov_base; |
| 385 | msg_a->struct_v = 1; |
| 386 | msg_a->global_id = cpu_to_le64(ac->global_id); |
| 387 | msg_a->service_id = cpu_to_le32(th->service); |
| 388 | msg_a->ticket_blob.struct_v = 1; |
| 389 | msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id); |
| 390 | msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len); |
| 391 | if (ticket_blob_len) { |
| 392 | memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base, |
| 393 | th->ticket_blob->vec.iov_len); |
| 394 | } |
| 395 | dout(" th %p secret_id %lld %lld\n", th, th->secret_id, |
| 396 | le64_to_cpu(msg_a->ticket_blob.secret_id)); |
| 397 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 398 | get_random_bytes(&au->nonce, sizeof(au->nonce)); |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 399 | ret = encrypt_authorizer(au, NULL); |
Ilya Dryomov | 149cac4 | 2018-07-27 16:37:54 +0200 | [diff] [blame] | 400 | if (ret) { |
| 401 | pr_err("failed to encrypt authorizer: %d", ret); |
Ilya Dryomov | cbf99a1 | 2015-10-26 11:03:46 +0100 | [diff] [blame] | 402 | goto out_au; |
Ilya Dryomov | 149cac4 | 2018-07-27 16:37:54 +0200 | [diff] [blame] | 403 | } |
Ilya Dryomov | 36721ec | 2016-12-02 16:35:06 +0100 | [diff] [blame] | 404 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 405 | dout(" built authorizer nonce %llx len %d\n", au->nonce, |
| 406 | (int)au->buf->vec.iov_len); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 407 | return 0; |
| 408 | |
Ilya Dryomov | cbf99a1 | 2015-10-26 11:03:46 +0100 | [diff] [blame] | 409 | out_au: |
| 410 | ceph_x_authorizer_cleanup(au); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th, |
| 415 | void **p, void *end) |
| 416 | { |
| 417 | ceph_decode_need(p, end, 1 + sizeof(u64), bad); |
| 418 | ceph_encode_8(p, 1); |
| 419 | ceph_encode_64(p, th->secret_id); |
| 420 | if (th->ticket_blob) { |
| 421 | const char *buf = th->ticket_blob->vec.iov_base; |
| 422 | u32 len = th->ticket_blob->vec.iov_len; |
| 423 | |
| 424 | ceph_encode_32_safe(p, end, len, bad); |
| 425 | ceph_encode_copy_safe(p, end, buf, len, bad); |
| 426 | } else { |
| 427 | ceph_encode_32_safe(p, end, 0, bad); |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | bad: |
| 432 | return -ERANGE; |
| 433 | } |
| 434 | |
Ilya Dryomov | 6abe097 | 2016-01-14 16:35:35 +0100 | [diff] [blame] | 435 | static bool need_key(struct ceph_x_ticket_handler *th) |
| 436 | { |
| 437 | if (!th->have_key) |
| 438 | return true; |
| 439 | |
Arnd Bergmann | 473bd2d | 2018-07-13 22:18:34 +0200 | [diff] [blame] | 440 | return ktime_get_real_seconds() >= th->renew_after; |
Ilya Dryomov | 6abe097 | 2016-01-14 16:35:35 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static bool have_key(struct ceph_x_ticket_handler *th) |
| 444 | { |
| 445 | if (th->have_key) { |
Arnd Bergmann | 473bd2d | 2018-07-13 22:18:34 +0200 | [diff] [blame] | 446 | if (ktime_get_real_seconds() >= th->expires) |
Ilya Dryomov | 6abe097 | 2016-01-14 16:35:35 +0100 | [diff] [blame] | 447 | th->have_key = false; |
| 448 | } |
| 449 | |
| 450 | return th->have_key; |
| 451 | } |
| 452 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 453 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) |
| 454 | { |
| 455 | int want = ac->want_keys; |
| 456 | struct ceph_x_info *xi = ac->private; |
| 457 | int service; |
| 458 | |
| 459 | *pneed = ac->want_keys & ~(xi->have_keys); |
| 460 | |
| 461 | for (service = 1; service <= want; service <<= 1) { |
| 462 | struct ceph_x_ticket_handler *th; |
| 463 | |
| 464 | if (!(ac->want_keys & service)) |
| 465 | continue; |
| 466 | |
| 467 | if (*pneed & service) |
| 468 | continue; |
| 469 | |
| 470 | th = get_ticket_handler(ac, service); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 471 | if (IS_ERR(th)) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 472 | *pneed |= service; |
| 473 | continue; |
| 474 | } |
| 475 | |
Ilya Dryomov | 6abe097 | 2016-01-14 16:35:35 +0100 | [diff] [blame] | 476 | if (need_key(th)) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 477 | *pneed |= service; |
Ilya Dryomov | 6abe097 | 2016-01-14 16:35:35 +0100 | [diff] [blame] | 478 | if (!have_key(th)) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 479 | xi->have_keys &= ~service; |
| 480 | } |
| 481 | } |
| 482 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 483 | static int ceph_x_build_request(struct ceph_auth_client *ac, |
| 484 | void *buf, void *end) |
| 485 | { |
| 486 | struct ceph_x_info *xi = ac->private; |
| 487 | int need; |
| 488 | struct ceph_x_request_header *head = buf; |
| 489 | int ret; |
| 490 | struct ceph_x_ticket_handler *th = |
| 491 | get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); |
| 492 | |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 493 | if (IS_ERR(th)) |
| 494 | return PTR_ERR(th); |
| 495 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 496 | ceph_x_validate_tickets(ac, &need); |
| 497 | |
| 498 | dout("build_request want %x have %x need %x\n", |
| 499 | ac->want_keys, xi->have_keys, need); |
| 500 | |
| 501 | if (need & CEPH_ENTITY_TYPE_AUTH) { |
| 502 | struct ceph_x_authenticate *auth = (void *)(head + 1); |
| 503 | void *p = auth + 1; |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 504 | void *enc_buf = xi->auth_authorizer.enc_buf; |
| 505 | struct ceph_x_challenge_blob *blob = enc_buf + |
| 506 | ceph_x_encrypt_offset(); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 507 | u64 *u; |
| 508 | |
| 509 | if (p > end) |
| 510 | return -ERANGE; |
| 511 | |
| 512 | dout(" get_auth_session_key\n"); |
| 513 | head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); |
| 514 | |
| 515 | /* encrypt and hash */ |
| 516 | get_random_bytes(&auth->client_challenge, sizeof(u64)); |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 517 | blob->client_challenge = auth->client_challenge; |
| 518 | blob->server_challenge = cpu_to_le64(xi->server_challenge); |
| 519 | ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN, |
| 520 | sizeof(*blob)); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 521 | if (ret < 0) |
| 522 | return ret; |
| 523 | |
| 524 | auth->struct_v = 1; |
| 525 | auth->key = 0; |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 526 | for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++) |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 527 | auth->key ^= *(__le64 *)u; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 528 | dout(" server_challenge %llx client_challenge %llx key %llx\n", |
| 529 | xi->server_challenge, le64_to_cpu(auth->client_challenge), |
| 530 | le64_to_cpu(auth->key)); |
| 531 | |
| 532 | /* now encode the old ticket if exists */ |
| 533 | ret = ceph_x_encode_ticket(th, &p, end); |
| 534 | if (ret < 0) |
| 535 | return ret; |
| 536 | |
| 537 | return p - buf; |
| 538 | } |
| 539 | |
| 540 | if (need) { |
| 541 | void *p = head + 1; |
| 542 | struct ceph_x_service_ticket_request *req; |
| 543 | |
| 544 | if (p > end) |
| 545 | return -ERANGE; |
| 546 | head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY); |
| 547 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 548 | ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); |
| 549 | if (ret) |
| 550 | return ret; |
| 551 | ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base, |
| 552 | xi->auth_authorizer.buf->vec.iov_len); |
| 553 | |
| 554 | req = p; |
| 555 | req->keys = cpu_to_le32(need); |
| 556 | p += sizeof(*req); |
| 557 | return p - buf; |
| 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result, |
| 564 | void *buf, void *end) |
| 565 | { |
| 566 | struct ceph_x_info *xi = ac->private; |
| 567 | struct ceph_x_reply_header *head = buf; |
| 568 | struct ceph_x_ticket_handler *th; |
| 569 | int len = end - buf; |
| 570 | int op; |
| 571 | int ret; |
| 572 | |
| 573 | if (result) |
| 574 | return result; /* XXX hmm? */ |
| 575 | |
| 576 | if (xi->starting) { |
| 577 | /* it's a hello */ |
| 578 | struct ceph_x_server_challenge *sc = buf; |
| 579 | |
| 580 | if (len != sizeof(*sc)) |
| 581 | return -EINVAL; |
| 582 | xi->server_challenge = le64_to_cpu(sc->server_challenge); |
| 583 | dout("handle_reply got server challenge %llx\n", |
| 584 | xi->server_challenge); |
| 585 | xi->starting = false; |
| 586 | xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; |
| 587 | return -EAGAIN; |
| 588 | } |
| 589 | |
Yehuda Sadeh | 0cf5537 | 2010-06-11 15:57:06 -0700 | [diff] [blame] | 590 | op = le16_to_cpu(head->op); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 591 | result = le32_to_cpu(head->result); |
| 592 | dout("handle_reply op %d result %d\n", op, result); |
| 593 | switch (op) { |
| 594 | case CEPHX_GET_AUTH_SESSION_KEY: |
| 595 | /* verify auth key */ |
| 596 | ret = ceph_x_proc_ticket_reply(ac, &xi->secret, |
| 597 | buf + sizeof(*head), end); |
| 598 | break; |
| 599 | |
| 600 | case CEPHX_GET_PRINCIPAL_SESSION_KEY: |
| 601 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 602 | if (IS_ERR(th)) |
| 603 | return PTR_ERR(th); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 604 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key, |
| 605 | buf + sizeof(*head), end); |
| 606 | break; |
| 607 | |
| 608 | default: |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | if (ret) |
| 612 | return ret; |
| 613 | if (ac->want_keys == xi->have_keys) |
| 614 | return 0; |
| 615 | return -EAGAIN; |
| 616 | } |
| 617 | |
Ilya Dryomov | 6c1ea26 | 2016-04-11 19:34:49 +0200 | [diff] [blame] | 618 | static void ceph_x_destroy_authorizer(struct ceph_authorizer *a) |
| 619 | { |
| 620 | struct ceph_x_authorizer *au = (void *)a; |
| 621 | |
| 622 | ceph_x_authorizer_cleanup(au); |
| 623 | kfree(au); |
| 624 | } |
| 625 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 626 | static int ceph_x_create_authorizer( |
| 627 | struct ceph_auth_client *ac, int peer_type, |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 628 | struct ceph_auth_handshake *auth) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 629 | { |
| 630 | struct ceph_x_authorizer *au; |
| 631 | struct ceph_x_ticket_handler *th; |
| 632 | int ret; |
| 633 | |
| 634 | th = get_ticket_handler(ac, peer_type); |
| 635 | if (IS_ERR(th)) |
| 636 | return PTR_ERR(th); |
| 637 | |
| 638 | au = kzalloc(sizeof(*au), GFP_NOFS); |
| 639 | if (!au) |
| 640 | return -ENOMEM; |
| 641 | |
Ilya Dryomov | 6c1ea26 | 2016-04-11 19:34:49 +0200 | [diff] [blame] | 642 | au->base.destroy = ceph_x_destroy_authorizer; |
| 643 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 644 | ret = ceph_x_build_authorizer(ac, th, au); |
| 645 | if (ret) { |
| 646 | kfree(au); |
| 647 | return ret; |
| 648 | } |
| 649 | |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 650 | auth->authorizer = (struct ceph_authorizer *) au; |
| 651 | auth->authorizer_buf = au->buf->vec.iov_base; |
| 652 | auth->authorizer_buf_len = au->buf->vec.iov_len; |
Ilya Dryomov | 7882a26 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 653 | auth->authorizer_reply_buf = au->enc_buf; |
| 654 | auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN; |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 655 | auth->sign_message = ac->ops->sign_message; |
| 656 | auth->check_message_signature = ac->ops->check_message_signature; |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 657 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 661 | static int ceph_x_update_authorizer( |
| 662 | struct ceph_auth_client *ac, int peer_type, |
| 663 | struct ceph_auth_handshake *auth) |
| 664 | { |
| 665 | struct ceph_x_authorizer *au; |
| 666 | struct ceph_x_ticket_handler *th; |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 667 | |
| 668 | th = get_ticket_handler(ac, peer_type); |
| 669 | if (IS_ERR(th)) |
| 670 | return PTR_ERR(th); |
| 671 | |
| 672 | au = (struct ceph_x_authorizer *)auth->authorizer; |
| 673 | if (au->secret_id < th->secret_id) { |
| 674 | dout("ceph_x_update_authorizer service %u secret %llu < %llu\n", |
| 675 | au->service, au->secret_id, th->secret_id); |
| 676 | return ceph_x_build_authorizer(ac, th, au); |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 681 | static int decrypt_authorize_challenge(struct ceph_x_authorizer *au, |
| 682 | void *challenge_buf, |
| 683 | int challenge_buf_len, |
| 684 | u64 *server_challenge) |
| 685 | { |
| 686 | struct ceph_x_authorize_challenge *ch = |
| 687 | challenge_buf + sizeof(struct ceph_x_encrypt_header); |
| 688 | int ret; |
| 689 | |
| 690 | /* no leading len */ |
| 691 | ret = __ceph_x_decrypt(&au->session_key, challenge_buf, |
| 692 | challenge_buf_len); |
| 693 | if (ret < 0) |
| 694 | return ret; |
| 695 | if (ret < sizeof(*ch)) { |
| 696 | pr_err("bad size %d for ceph_x_authorize_challenge\n", ret); |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | |
| 700 | *server_challenge = le64_to_cpu(ch->server_challenge); |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static int ceph_x_add_authorizer_challenge(struct ceph_auth_client *ac, |
| 705 | struct ceph_authorizer *a, |
| 706 | void *challenge_buf, |
| 707 | int challenge_buf_len) |
| 708 | { |
| 709 | struct ceph_x_authorizer *au = (void *)a; |
| 710 | u64 server_challenge; |
| 711 | int ret; |
| 712 | |
| 713 | ret = decrypt_authorize_challenge(au, challenge_buf, challenge_buf_len, |
| 714 | &server_challenge); |
| 715 | if (ret) { |
| 716 | pr_err("failed to decrypt authorize challenge: %d", ret); |
| 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | ret = encrypt_authorizer(au, &server_challenge); |
| 721 | if (ret) { |
| 722 | pr_err("failed to encrypt authorizer w/ challenge: %d", ret); |
| 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 729 | static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, |
Ilya Dryomov | 0dde584 | 2016-12-02 16:35:09 +0100 | [diff] [blame] | 730 | struct ceph_authorizer *a) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 731 | { |
| 732 | struct ceph_x_authorizer *au = (void *)a; |
Ilya Dryomov | 7882a26 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 733 | void *p = au->enc_buf; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 734 | struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset(); |
| 735 | int ret; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 736 | |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 737 | ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 738 | if (ret < 0) |
| 739 | return ret; |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 740 | if (ret != sizeof(*reply)) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 741 | return -EPERM; |
| 742 | |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 743 | if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one)) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 744 | ret = -EPERM; |
| 745 | else |
| 746 | ret = 0; |
| 747 | dout("verify_authorizer_reply nonce %llx got %llx ret %d\n", |
Ilya Dryomov | e15fd0a | 2016-12-02 16:35:08 +0100 | [diff] [blame] | 748 | au->nonce, le64_to_cpu(reply->nonce_plus_one), ret); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 749 | return ret; |
| 750 | } |
| 751 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 752 | static void ceph_x_reset(struct ceph_auth_client *ac) |
| 753 | { |
| 754 | struct ceph_x_info *xi = ac->private; |
| 755 | |
| 756 | dout("reset\n"); |
| 757 | xi->starting = true; |
| 758 | xi->server_challenge = 0; |
| 759 | } |
| 760 | |
| 761 | static void ceph_x_destroy(struct ceph_auth_client *ac) |
| 762 | { |
| 763 | struct ceph_x_info *xi = ac->private; |
| 764 | struct rb_node *p; |
| 765 | |
| 766 | dout("ceph_x_destroy %p\n", ac); |
| 767 | ceph_crypto_key_destroy(&xi->secret); |
| 768 | |
| 769 | while ((p = rb_first(&xi->ticket_handlers)) != NULL) { |
| 770 | struct ceph_x_ticket_handler *th = |
| 771 | rb_entry(p, struct ceph_x_ticket_handler, node); |
| 772 | remove_ticket_handler(ac, th); |
| 773 | } |
| 774 | |
Ilya Dryomov | cbf99a1 | 2015-10-26 11:03:46 +0100 | [diff] [blame] | 775 | ceph_x_authorizer_cleanup(&xi->auth_authorizer); |
Sage Weil | 22b1de0 | 2010-07-05 15:36:49 -0700 | [diff] [blame] | 776 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 777 | kfree(ac->private); |
| 778 | ac->private = NULL; |
| 779 | } |
| 780 | |
Ilya Dryomov | 187d131 | 2016-01-14 17:31:51 +0100 | [diff] [blame] | 781 | static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 782 | { |
| 783 | struct ceph_x_ticket_handler *th; |
| 784 | |
| 785 | th = get_ticket_handler(ac, peer_type); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 786 | if (!IS_ERR(th)) |
Ilya Dryomov | 6abe097 | 2016-01-14 16:35:35 +0100 | [diff] [blame] | 787 | th->have_key = false; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Ilya Dryomov | 187d131 | 2016-01-14 17:31:51 +0100 | [diff] [blame] | 790 | static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, |
| 791 | int peer_type) |
| 792 | { |
| 793 | /* |
| 794 | * We are to invalidate a service ticket in the hopes of |
| 795 | * getting a new, hopefully more valid, one. But, we won't get |
| 796 | * it unless our AUTH ticket is good, so invalidate AUTH ticket |
| 797 | * as well, just in case. |
| 798 | */ |
| 799 | invalidate_ticket(ac, peer_type); |
| 800 | invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH); |
| 801 | } |
| 802 | |
Ilya Dryomov | 4eb4517 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 803 | static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, |
| 804 | __le64 *psig) |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 805 | { |
Ilya Dryomov | d03857c | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 806 | void *enc_buf = au->enc_buf; |
Ilya Dryomov | 4eb4517 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 807 | int ret; |
| 808 | |
Ilya Dryomov | cc255c7 | 2018-07-27 19:25:32 +0200 | [diff] [blame^] | 809 | if (!CEPH_HAVE_FEATURE(msg->con->peer_features, CEPHX_V2)) { |
| 810 | struct { |
| 811 | __le32 len; |
| 812 | __le32 header_crc; |
| 813 | __le32 front_crc; |
| 814 | __le32 middle_crc; |
| 815 | __le32 data_crc; |
| 816 | } __packed *sigblock = enc_buf + ceph_x_encrypt_offset(); |
Ilya Dryomov | 4eb4517 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 817 | |
Ilya Dryomov | cc255c7 | 2018-07-27 19:25:32 +0200 | [diff] [blame^] | 818 | sigblock->len = cpu_to_le32(4*sizeof(u32)); |
| 819 | sigblock->header_crc = msg->hdr.crc; |
| 820 | sigblock->front_crc = msg->footer.front_crc; |
| 821 | sigblock->middle_crc = msg->footer.middle_crc; |
| 822 | sigblock->data_crc = msg->footer.data_crc; |
| 823 | |
| 824 | ret = ceph_x_encrypt(&au->session_key, enc_buf, |
| 825 | CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock)); |
| 826 | if (ret < 0) |
| 827 | return ret; |
| 828 | |
| 829 | *psig = *(__le64 *)(enc_buf + sizeof(u32)); |
| 830 | } else { |
| 831 | struct { |
| 832 | __le32 header_crc; |
| 833 | __le32 front_crc; |
| 834 | __le32 front_len; |
| 835 | __le32 middle_crc; |
| 836 | __le32 middle_len; |
| 837 | __le32 data_crc; |
| 838 | __le32 data_len; |
| 839 | __le32 seq_lower_word; |
| 840 | } __packed *sigblock = enc_buf; |
| 841 | struct { |
| 842 | __le64 a, b, c, d; |
| 843 | } __packed *penc = enc_buf; |
| 844 | int ciphertext_len; |
| 845 | |
| 846 | sigblock->header_crc = msg->hdr.crc; |
| 847 | sigblock->front_crc = msg->footer.front_crc; |
| 848 | sigblock->front_len = msg->hdr.front_len; |
| 849 | sigblock->middle_crc = msg->footer.middle_crc; |
| 850 | sigblock->middle_len = msg->hdr.middle_len; |
| 851 | sigblock->data_crc = msg->footer.data_crc; |
| 852 | sigblock->data_len = msg->hdr.data_len; |
| 853 | sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq; |
| 854 | |
| 855 | /* no leading len, no ceph_x_encrypt_header */ |
| 856 | ret = ceph_crypt(&au->session_key, true, enc_buf, |
| 857 | CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock), |
| 858 | &ciphertext_len); |
| 859 | if (ret) |
| 860 | return ret; |
| 861 | |
| 862 | *psig = penc->a ^ penc->b ^ penc->c ^ penc->d; |
| 863 | } |
| 864 | |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static int ceph_x_sign_message(struct ceph_auth_handshake *auth, |
| 869 | struct ceph_msg *msg) |
| 870 | { |
Ilya Dryomov | 4eb4517 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 871 | __le64 sig; |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 872 | int ret; |
Ilya Dryomov | 4199b8e | 2015-10-27 16:42:49 +0100 | [diff] [blame] | 873 | |
Ilya Dryomov | a51983e | 2015-10-28 23:52:06 +0100 | [diff] [blame] | 874 | if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) |
| 875 | return 0; |
| 876 | |
Ilya Dryomov | 4eb4517 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 877 | ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, |
| 878 | msg, &sig); |
| 879 | if (ret) |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 880 | return ret; |
Ilya Dryomov | 4eb4517 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 881 | |
| 882 | msg->footer.sig = sig; |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 883 | msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED; |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth, |
| 888 | struct ceph_msg *msg) |
| 889 | { |
| 890 | __le64 sig_check; |
| 891 | int ret; |
| 892 | |
Ilya Dryomov | a51983e | 2015-10-28 23:52:06 +0100 | [diff] [blame] | 893 | if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) |
| 894 | return 0; |
| 895 | |
Ilya Dryomov | 4eb4517 | 2016-12-02 16:35:07 +0100 | [diff] [blame] | 896 | ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, |
| 897 | msg, &sig_check); |
| 898 | if (ret) |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 899 | return ret; |
| 900 | if (sig_check == msg->footer.sig) |
| 901 | return 0; |
| 902 | if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED) |
| 903 | dout("ceph_x_check_message_signature %p has signature %llx " |
| 904 | "expect %llx\n", msg, msg->footer.sig, sig_check); |
| 905 | else |
| 906 | dout("ceph_x_check_message_signature %p sender did not set " |
| 907 | "CEPH_MSG_FOOTER_SIGNED\n", msg); |
| 908 | return -EBADMSG; |
| 909 | } |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 910 | |
| 911 | static const struct ceph_auth_client_ops ceph_x_ops = { |
Sage Weil | 559c1e0 | 2010-05-14 09:55:18 -0700 | [diff] [blame] | 912 | .name = "x", |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 913 | .is_authenticated = ceph_x_is_authenticated, |
Sage Weil | a41359f | 2010-05-25 15:39:06 -0700 | [diff] [blame] | 914 | .should_authenticate = ceph_x_should_authenticate, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 915 | .build_request = ceph_x_build_request, |
| 916 | .handle_reply = ceph_x_handle_reply, |
| 917 | .create_authorizer = ceph_x_create_authorizer, |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 918 | .update_authorizer = ceph_x_update_authorizer, |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 919 | .add_authorizer_challenge = ceph_x_add_authorizer_challenge, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 920 | .verify_authorizer_reply = ceph_x_verify_authorizer_reply, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 921 | .invalidate_authorizer = ceph_x_invalidate_authorizer, |
| 922 | .reset = ceph_x_reset, |
| 923 | .destroy = ceph_x_destroy, |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 924 | .sign_message = ceph_x_sign_message, |
| 925 | .check_message_signature = ceph_x_check_message_signature, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 926 | }; |
| 927 | |
| 928 | |
| 929 | int ceph_x_init(struct ceph_auth_client *ac) |
| 930 | { |
| 931 | struct ceph_x_info *xi; |
| 932 | int ret; |
| 933 | |
| 934 | dout("ceph_x_init %p\n", ac); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 935 | ret = -ENOMEM; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 936 | xi = kzalloc(sizeof(*xi), GFP_NOFS); |
| 937 | if (!xi) |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 938 | goto out; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 939 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 940 | ret = -EINVAL; |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 941 | if (!ac->key) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 942 | pr_err("no secret set (for auth_x protocol)\n"); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 943 | goto out_nomem; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 944 | } |
| 945 | |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 946 | ret = ceph_crypto_key_clone(&xi->secret, ac->key); |
| 947 | if (ret < 0) { |
| 948 | pr_err("cannot clone key: %d\n", ret); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 949 | goto out_nomem; |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 950 | } |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 951 | |
| 952 | xi->starting = true; |
| 953 | xi->ticket_handlers = RB_ROOT; |
| 954 | |
| 955 | ac->protocol = CEPH_AUTH_CEPHX; |
| 956 | ac->private = xi; |
| 957 | ac->ops = &ceph_x_ops; |
| 958 | return 0; |
| 959 | |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 960 | out_nomem: |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 961 | kfree(xi); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 962 | out: |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 963 | return ret; |
| 964 | } |