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