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