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