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