blob: cd1118d106a5f5db80bebe6979145225c1d11101 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sage Weilec0994e2010-02-02 16:25:35 -08002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/ceph/ceph_debug.h>
Sage Weilec0994e2010-02-02 16:25:35 -08004
5#include <linux/err.h>
6#include <linux/module.h>
7#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Sage Weilec0994e2010-02-02 16:25:35 -08009
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070010#include <linux/ceph/decode.h>
11#include <linux/ceph/auth.h>
Ilya Dryomova51983e2015-10-28 23:52:06 +010012#include <linux/ceph/libceph.h>
Yan, Zheng33d07332014-11-04 16:33:37 +080013#include <linux/ceph/messenger.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014
15#include "crypto.h"
Sage Weilec0994e2010-02-02 16:25:35 -080016#include "auth_x.h"
17#include "auth_x_protocol.h"
Sage Weilec0994e2010-02-02 16:25:35 -080018
Sage Weilec0994e2010-02-02 16:25:35 -080019static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
20
21static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
22{
23 struct ceph_x_info *xi = ac->private;
24 int need;
25
26 ceph_x_validate_tickets(ac, &need);
27 dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
28 ac->want_keys, need, xi->have_keys);
29 return (ac->want_keys & xi->have_keys) == ac->want_keys;
30}
31
Sage Weila41359f2010-05-25 15:39:06 -070032static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
33{
34 struct ceph_x_info *xi = ac->private;
35 int need;
36
37 ceph_x_validate_tickets(ac, &need);
38 dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
39 ac->want_keys, need, xi->have_keys);
40 return need != 0;
41}
42
Ilya Dryomov55d9cc832016-12-02 16:35:07 +010043static int ceph_x_encrypt_offset(void)
44{
45 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
46}
47
Sage Weil807c86e2010-03-15 15:52:17 -070048static int ceph_x_encrypt_buflen(int ilen)
49{
Ilya Dryomov55d9cc832016-12-02 16:35:07 +010050 return ceph_x_encrypt_offset() + ilen + 16;
Sage Weil807c86e2010-03-15 15:52:17 -070051}
52
Ilya Dryomovd03857c2016-12-02 16:35:07 +010053static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
54 int buf_len, int plaintext_len)
Sage Weilec0994e2010-02-02 16:25:35 -080055{
Ilya Dryomovd03857c2016-12-02 16:35:07 +010056 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
57 int ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080058 int ret;
59
Ilya Dryomovd03857c2016-12-02 16:35:07 +010060 hdr->struct_v = 1;
61 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
62
63 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
64 plaintext_len + sizeof(struct ceph_x_encrypt_header),
65 &ciphertext_len);
Sage Weilec0994e2010-02-02 16:25:35 -080066 if (ret)
67 return ret;
Ilya Dryomovd03857c2016-12-02 16:35:07 +010068
69 ceph_encode_32(&buf, ciphertext_len);
70 return sizeof(u32) + ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080071}
72
Ilya Dryomovc571fe22018-07-26 18:05:43 +020073static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
74 int ciphertext_len)
75{
76 struct ceph_x_encrypt_header *hdr = p;
77 int plaintext_len;
78 int ret;
79
80 ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
81 &plaintext_len);
82 if (ret)
83 return ret;
84
85 if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
86 pr_err("%s bad magic\n", __func__);
87 return -EINVAL;
88 }
89
90 return plaintext_len - sizeof(*hdr);
91}
92
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010093static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
Sage Weilec0994e2010-02-02 16:25:35 -080094{
Ilya Dryomovc571fe22018-07-26 18:05:43 +020095 int ciphertext_len;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010096 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -080097
Ilya Dryomove15fd0a2016-12-02 16:35:08 +010098 ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
99 ceph_decode_need(p, end, ciphertext_len, e_inval);
Sage Weilec0994e2010-02-02 16:25:35 -0800100
Ilya Dryomovc571fe22018-07-26 18:05:43 +0200101 ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
102 if (ret < 0)
Sage Weilec0994e2010-02-02 16:25:35 -0800103 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100104
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100105 *p += ciphertext_len;
Ilya Dryomovc571fe22018-07-26 18:05:43 +0200106 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100107
108e_inval:
109 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800110}
111
112/*
113 * get existing (or insert new) ticket handler
114 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700115static struct ceph_x_ticket_handler *
116get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -0800117{
118 struct ceph_x_ticket_handler *th;
119 struct ceph_x_info *xi = ac->private;
120 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
121
122 while (*p) {
123 parent = *p;
124 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
125 if (service < th->service)
126 p = &(*p)->rb_left;
127 else if (service > th->service)
128 p = &(*p)->rb_right;
129 else
130 return th;
131 }
132
133 /* add it */
134 th = kzalloc(sizeof(*th), GFP_NOFS);
135 if (!th)
136 return ERR_PTR(-ENOMEM);
137 th->service = service;
138 rb_link_node(&th->node, parent, p);
139 rb_insert_color(&th->node, &xi->ticket_handlers);
140 return th;
141}
142
143static void remove_ticket_handler(struct ceph_auth_client *ac,
144 struct ceph_x_ticket_handler *th)
145{
146 struct ceph_x_info *xi = ac->private;
147
148 dout("remove_ticket_handler %p %d\n", th, th->service);
149 rb_erase(&th->node, &xi->ticket_handlers);
150 ceph_crypto_key_destroy(&th->session_key);
151 if (th->ticket_blob)
152 ceph_buffer_put(th->ticket_blob);
153 kfree(th);
154}
155
Ilya Dryomov597cda32014-09-08 17:25:34 +0400156static int process_one_ticket(struct ceph_auth_client *ac,
157 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400158 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400159{
160 struct ceph_x_info *xi = ac->private;
161 int type;
162 u8 tkt_struct_v, blob_struct_v;
163 struct ceph_x_ticket_handler *th;
164 void *dp, *dend;
165 int dlen;
166 char is_enc;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200167 struct timespec64 validity;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400168 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400169 void **ptp;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200170 struct ceph_crypto_key new_session_key = { 0 };
Ilya Dryomov597cda32014-09-08 17:25:34 +0400171 struct ceph_buffer *new_ticket_blob;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200172 time64_t new_expires, new_renew_after;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400173 u64 new_secret_id;
174 int ret;
175
176 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
177
178 type = ceph_decode_32(p);
179 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
180
181 tkt_struct_v = ceph_decode_8(p);
182 if (tkt_struct_v != 1)
183 goto bad;
184
185 th = get_ticket_handler(ac, type);
186 if (IS_ERR(th)) {
187 ret = PTR_ERR(th);
188 goto out;
189 }
190
191 /* blob for me */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100192 dp = *p + ceph_x_encrypt_offset();
193 ret = ceph_x_decrypt(secret, p, end);
194 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400195 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100196 dout(" decrypted %d bytes\n", ret);
197 dend = dp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400198
199 tkt_struct_v = ceph_decode_8(&dp);
200 if (tkt_struct_v != 1)
201 goto bad;
202
Ilya Dryomov597cda32014-09-08 17:25:34 +0400203 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
204 if (ret)
205 goto out;
206
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200207 ceph_decode_timespec64(&validity, dp);
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100208 dp += sizeof(struct ceph_timespec);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200209 new_expires = ktime_get_real_seconds() + validity.tv_sec;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400210 new_renew_after = new_expires - (validity.tv_sec / 4);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200211 dout(" expires=%llu renew_after=%llu\n", new_expires,
Ilya Dryomov597cda32014-09-08 17:25:34 +0400212 new_renew_after);
213
214 /* ticket blob for service */
215 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400216 if (is_enc) {
217 /* encrypted */
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100218 tp = *p + ceph_x_encrypt_offset();
219 ret = ceph_x_decrypt(&th->session_key, p, end);
220 if (ret < 0)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400221 goto out;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100222 dout(" encrypted ticket, decrypted %d bytes\n", ret);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400223 ptp = &tp;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100224 tpend = tp + ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400225 } else {
226 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400227 ptp = p;
228 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400229 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400230 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400231 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400232 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
233 blob_struct_v = ceph_decode_8(ptp);
Ilya Dryomovd18a1242017-05-19 12:21:56 +0200234 if (blob_struct_v != 1)
235 goto bad;
236
Ilya Dryomove9226d72014-10-22 18:15:37 +0400237 new_secret_id = ceph_decode_64(ptp);
238 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400239 if (ret)
240 goto out;
241
242 /* all is well, update our ticket */
243 ceph_crypto_key_destroy(&th->session_key);
244 if (th->ticket_blob)
245 ceph_buffer_put(th->ticket_blob);
246 th->session_key = new_session_key;
247 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400248 th->secret_id = new_secret_id;
249 th->expires = new_expires;
250 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100251 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400252 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
253 type, ceph_entity_type_name(type), th->secret_id,
254 (int)th->ticket_blob->vec.iov_len);
255 xi->have_keys |= th->service;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200256 return 0;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400257
258bad:
259 ret = -EINVAL;
Ilya Dryomovb51456a2017-05-19 14:24:36 +0200260out:
261 ceph_crypto_key_destroy(&new_session_key);
262 return ret;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400263}
264
Sage Weilec0994e2010-02-02 16:25:35 -0800265static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
266 struct ceph_crypto_key *secret,
267 void *buf, void *end)
268{
Sage Weilec0994e2010-02-02 16:25:35 -0800269 void *p = buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700270 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400271 u32 num;
272 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800273
Ilya Dryomov597cda32014-09-08 17:25:34 +0400274 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700275 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400276 return -EINVAL;
277
278 ceph_decode_32_safe(&p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800279 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400280
Sage Weilec0994e2010-02-02 16:25:35 -0800281 while (num--) {
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400282 ret = process_one_ticket(ac, secret, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800283 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400284 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800285 }
286
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400287 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800288
289bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400290 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800291}
292
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100293static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
294{
295 ceph_crypto_key_destroy(&au->session_key);
296 if (au->buf) {
297 ceph_buffer_put(au->buf);
298 au->buf = NULL;
299 }
300}
301
Sage Weilec0994e2010-02-02 16:25:35 -0800302static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
303 struct ceph_x_ticket_handler *th,
304 struct ceph_x_authorizer *au)
305{
Sage Weil807c86e2010-03-15 15:52:17 -0700306 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800307 struct ceph_x_authorize_a *msg_a;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100308 struct ceph_x_authorize_b *msg_b;
Sage Weilec0994e2010-02-02 16:25:35 -0800309 void *p, *end;
310 int ret;
311 int ticket_blob_len =
312 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
313
314 dout("build_authorizer for %s %p\n",
315 ceph_entity_type_name(th->service), au);
316
Yan, Zhengae385ea2014-11-04 16:32:35 +0800317 ceph_crypto_key_destroy(&au->session_key);
318 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
319 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100320 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800321
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100322 maxlen = sizeof(*msg_a) + ticket_blob_len +
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100323 ceph_x_encrypt_buflen(sizeof(*msg_b));
Sage Weil807c86e2010-03-15 15:52:17 -0700324 dout(" need len %d\n", maxlen);
325 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800326 ceph_buffer_put(au->buf);
327 au->buf = NULL;
328 }
329 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700330 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800331 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100332 ret = -ENOMEM;
333 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800334 }
Sage Weilec0994e2010-02-02 16:25:35 -0800335 }
336 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700337 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800338
339 msg_a = au->buf->vec.iov_base;
340 msg_a->struct_v = 1;
341 msg_a->global_id = cpu_to_le64(ac->global_id);
342 msg_a->service_id = cpu_to_le32(th->service);
343 msg_a->ticket_blob.struct_v = 1;
344 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
345 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
346 if (ticket_blob_len) {
347 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
348 th->ticket_blob->vec.iov_len);
349 }
350 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
351 le64_to_cpu(msg_a->ticket_blob.secret_id));
352
353 p = msg_a + 1;
354 p += ticket_blob_len;
355 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
356
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100357 msg_b = p + ceph_x_encrypt_offset();
358 msg_b->struct_v = 1;
Sage Weilec0994e2010-02-02 16:25:35 -0800359 get_random_bytes(&au->nonce, sizeof(au->nonce));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100360 msg_b->nonce = cpu_to_le64(au->nonce);
361 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
Sage Weilec0994e2010-02-02 16:25:35 -0800362 if (ret < 0)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100363 goto out_au;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100364
Sage Weilec0994e2010-02-02 16:25:35 -0800365 p += ret;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100366 WARN_ON(p > end);
Sage Weilec0994e2010-02-02 16:25:35 -0800367 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
368 dout(" built authorizer nonce %llx len %d\n", au->nonce,
369 (int)au->buf->vec.iov_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800370 return 0;
371
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100372out_au:
373 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800374 return ret;
375}
376
377static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
378 void **p, void *end)
379{
380 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
381 ceph_encode_8(p, 1);
382 ceph_encode_64(p, th->secret_id);
383 if (th->ticket_blob) {
384 const char *buf = th->ticket_blob->vec.iov_base;
385 u32 len = th->ticket_blob->vec.iov_len;
386
387 ceph_encode_32_safe(p, end, len, bad);
388 ceph_encode_copy_safe(p, end, buf, len, bad);
389 } else {
390 ceph_encode_32_safe(p, end, 0, bad);
391 }
392
393 return 0;
394bad:
395 return -ERANGE;
396}
397
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100398static bool need_key(struct ceph_x_ticket_handler *th)
399{
400 if (!th->have_key)
401 return true;
402
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200403 return ktime_get_real_seconds() >= th->renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100404}
405
406static bool have_key(struct ceph_x_ticket_handler *th)
407{
408 if (th->have_key) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +0200409 if (ktime_get_real_seconds() >= th->expires)
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100410 th->have_key = false;
411 }
412
413 return th->have_key;
414}
415
Sage Weilec0994e2010-02-02 16:25:35 -0800416static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
417{
418 int want = ac->want_keys;
419 struct ceph_x_info *xi = ac->private;
420 int service;
421
422 *pneed = ac->want_keys & ~(xi->have_keys);
423
424 for (service = 1; service <= want; service <<= 1) {
425 struct ceph_x_ticket_handler *th;
426
427 if (!(ac->want_keys & service))
428 continue;
429
430 if (*pneed & service)
431 continue;
432
433 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200434 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800435 *pneed |= service;
436 continue;
437 }
438
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100439 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800440 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100441 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800442 xi->have_keys &= ~service;
443 }
444}
445
Sage Weilec0994e2010-02-02 16:25:35 -0800446static int ceph_x_build_request(struct ceph_auth_client *ac,
447 void *buf, void *end)
448{
449 struct ceph_x_info *xi = ac->private;
450 int need;
451 struct ceph_x_request_header *head = buf;
452 int ret;
453 struct ceph_x_ticket_handler *th =
454 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
455
Dan Carpenterb5457872010-08-26 11:12:38 +0200456 if (IS_ERR(th))
457 return PTR_ERR(th);
458
Sage Weilec0994e2010-02-02 16:25:35 -0800459 ceph_x_validate_tickets(ac, &need);
460
461 dout("build_request want %x have %x need %x\n",
462 ac->want_keys, xi->have_keys, need);
463
464 if (need & CEPH_ENTITY_TYPE_AUTH) {
465 struct ceph_x_authenticate *auth = (void *)(head + 1);
466 void *p = auth + 1;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100467 void *enc_buf = xi->auth_authorizer.enc_buf;
468 struct ceph_x_challenge_blob *blob = enc_buf +
469 ceph_x_encrypt_offset();
Sage Weilec0994e2010-02-02 16:25:35 -0800470 u64 *u;
471
472 if (p > end)
473 return -ERANGE;
474
475 dout(" get_auth_session_key\n");
476 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
477
478 /* encrypt and hash */
479 get_random_bytes(&auth->client_challenge, sizeof(u64));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100480 blob->client_challenge = auth->client_challenge;
481 blob->server_challenge = cpu_to_le64(xi->server_challenge);
482 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
483 sizeof(*blob));
Sage Weilec0994e2010-02-02 16:25:35 -0800484 if (ret < 0)
485 return ret;
486
487 auth->struct_v = 1;
488 auth->key = 0;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100489 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700490 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800491 dout(" server_challenge %llx client_challenge %llx key %llx\n",
492 xi->server_challenge, le64_to_cpu(auth->client_challenge),
493 le64_to_cpu(auth->key));
494
495 /* now encode the old ticket if exists */
496 ret = ceph_x_encode_ticket(th, &p, end);
497 if (ret < 0)
498 return ret;
499
500 return p - buf;
501 }
502
503 if (need) {
504 void *p = head + 1;
505 struct ceph_x_service_ticket_request *req;
506
507 if (p > end)
508 return -ERANGE;
509 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
510
Sage Weilec0994e2010-02-02 16:25:35 -0800511 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
512 if (ret)
513 return ret;
514 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
515 xi->auth_authorizer.buf->vec.iov_len);
516
517 req = p;
518 req->keys = cpu_to_le32(need);
519 p += sizeof(*req);
520 return p - buf;
521 }
522
523 return 0;
524}
525
526static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
527 void *buf, void *end)
528{
529 struct ceph_x_info *xi = ac->private;
530 struct ceph_x_reply_header *head = buf;
531 struct ceph_x_ticket_handler *th;
532 int len = end - buf;
533 int op;
534 int ret;
535
536 if (result)
537 return result; /* XXX hmm? */
538
539 if (xi->starting) {
540 /* it's a hello */
541 struct ceph_x_server_challenge *sc = buf;
542
543 if (len != sizeof(*sc))
544 return -EINVAL;
545 xi->server_challenge = le64_to_cpu(sc->server_challenge);
546 dout("handle_reply got server challenge %llx\n",
547 xi->server_challenge);
548 xi->starting = false;
549 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
550 return -EAGAIN;
551 }
552
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700553 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800554 result = le32_to_cpu(head->result);
555 dout("handle_reply op %d result %d\n", op, result);
556 switch (op) {
557 case CEPHX_GET_AUTH_SESSION_KEY:
558 /* verify auth key */
559 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
560 buf + sizeof(*head), end);
561 break;
562
563 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
564 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200565 if (IS_ERR(th))
566 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800567 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
568 buf + sizeof(*head), end);
569 break;
570
571 default:
572 return -EINVAL;
573 }
574 if (ret)
575 return ret;
576 if (ac->want_keys == xi->have_keys)
577 return 0;
578 return -EAGAIN;
579}
580
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200581static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
582{
583 struct ceph_x_authorizer *au = (void *)a;
584
585 ceph_x_authorizer_cleanup(au);
586 kfree(au);
587}
588
Sage Weilec0994e2010-02-02 16:25:35 -0800589static int ceph_x_create_authorizer(
590 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500591 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800592{
593 struct ceph_x_authorizer *au;
594 struct ceph_x_ticket_handler *th;
595 int ret;
596
597 th = get_ticket_handler(ac, peer_type);
598 if (IS_ERR(th))
599 return PTR_ERR(th);
600
601 au = kzalloc(sizeof(*au), GFP_NOFS);
602 if (!au)
603 return -ENOMEM;
604
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200605 au->base.destroy = ceph_x_destroy_authorizer;
606
Sage Weilec0994e2010-02-02 16:25:35 -0800607 ret = ceph_x_build_authorizer(ac, th, au);
608 if (ret) {
609 kfree(au);
610 return ret;
611 }
612
Alex Elder74f18692012-05-16 15:16:39 -0500613 auth->authorizer = (struct ceph_authorizer *) au;
614 auth->authorizer_buf = au->buf->vec.iov_base;
615 auth->authorizer_buf_len = au->buf->vec.iov_len;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100616 auth->authorizer_reply_buf = au->enc_buf;
617 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
Yan, Zheng33d07332014-11-04 16:33:37 +0800618 auth->sign_message = ac->ops->sign_message;
619 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500620
Sage Weilec0994e2010-02-02 16:25:35 -0800621 return 0;
622}
623
Sage Weil0bed9b52013-03-25 10:26:01 -0700624static int ceph_x_update_authorizer(
625 struct ceph_auth_client *ac, int peer_type,
626 struct ceph_auth_handshake *auth)
627{
628 struct ceph_x_authorizer *au;
629 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700630
631 th = get_ticket_handler(ac, peer_type);
632 if (IS_ERR(th))
633 return PTR_ERR(th);
634
635 au = (struct ceph_x_authorizer *)auth->authorizer;
636 if (au->secret_id < th->secret_id) {
637 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
638 au->service, au->secret_id, th->secret_id);
639 return ceph_x_build_authorizer(ac, th, au);
640 }
641 return 0;
642}
643
Sage Weilec0994e2010-02-02 16:25:35 -0800644static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
Ilya Dryomov0dde5842016-12-02 16:35:09 +0100645 struct ceph_authorizer *a)
Sage Weilec0994e2010-02-02 16:25:35 -0800646{
647 struct ceph_x_authorizer *au = (void *)a;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100648 void *p = au->enc_buf;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100649 struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
650 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800651
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100652 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
Sage Weilec0994e2010-02-02 16:25:35 -0800653 if (ret < 0)
654 return ret;
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100655 if (ret != sizeof(*reply))
Sage Weilec0994e2010-02-02 16:25:35 -0800656 return -EPERM;
657
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100658 if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
Sage Weilec0994e2010-02-02 16:25:35 -0800659 ret = -EPERM;
660 else
661 ret = 0;
662 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
Ilya Dryomove15fd0a2016-12-02 16:35:08 +0100663 au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
Sage Weilec0994e2010-02-02 16:25:35 -0800664 return ret;
665}
666
Sage Weilec0994e2010-02-02 16:25:35 -0800667static void ceph_x_reset(struct ceph_auth_client *ac)
668{
669 struct ceph_x_info *xi = ac->private;
670
671 dout("reset\n");
672 xi->starting = true;
673 xi->server_challenge = 0;
674}
675
676static void ceph_x_destroy(struct ceph_auth_client *ac)
677{
678 struct ceph_x_info *xi = ac->private;
679 struct rb_node *p;
680
681 dout("ceph_x_destroy %p\n", ac);
682 ceph_crypto_key_destroy(&xi->secret);
683
684 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
685 struct ceph_x_ticket_handler *th =
686 rb_entry(p, struct ceph_x_ticket_handler, node);
687 remove_ticket_handler(ac, th);
688 }
689
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100690 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700691
Sage Weilec0994e2010-02-02 16:25:35 -0800692 kfree(ac->private);
693 ac->private = NULL;
694}
695
Ilya Dryomov187d1312016-01-14 17:31:51 +0100696static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800697{
698 struct ceph_x_ticket_handler *th;
699
700 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200701 if (!IS_ERR(th))
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100702 th->have_key = false;
Sage Weilec0994e2010-02-02 16:25:35 -0800703}
704
Ilya Dryomov187d1312016-01-14 17:31:51 +0100705static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
706 int peer_type)
707{
708 /*
709 * We are to invalidate a service ticket in the hopes of
710 * getting a new, hopefully more valid, one. But, we won't get
711 * it unless our AUTH ticket is good, so invalidate AUTH ticket
712 * as well, just in case.
713 */
714 invalidate_ticket(ac, peer_type);
715 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
716}
717
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100718static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
719 __le64 *psig)
Yan, Zheng33d07332014-11-04 16:33:37 +0800720{
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100721 void *enc_buf = au->enc_buf;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100722 struct {
723 __le32 len;
724 __le32 header_crc;
725 __le32 front_crc;
726 __le32 middle_crc;
727 __le32 data_crc;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100728 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100729 int ret;
730
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100731 sigblock->len = cpu_to_le32(4*sizeof(u32));
732 sigblock->header_crc = msg->hdr.crc;
733 sigblock->front_crc = msg->footer.front_crc;
734 sigblock->middle_crc = msg->footer.middle_crc;
735 sigblock->data_crc = msg->footer.data_crc;
736 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
737 sizeof(*sigblock));
Yan, Zheng33d07332014-11-04 16:33:37 +0800738 if (ret < 0)
739 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100740
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100741 *psig = *(__le64 *)(enc_buf + sizeof(u32));
Yan, Zheng33d07332014-11-04 16:33:37 +0800742 return 0;
743}
744
745static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
746 struct ceph_msg *msg)
747{
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100748 __le64 sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800749 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +0100750
Ilya Dryomova51983e2015-10-28 23:52:06 +0100751 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
752 return 0;
753
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100754 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
755 msg, &sig);
756 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800757 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100758
759 msg->footer.sig = sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800760 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
761 return 0;
762}
763
764static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
765 struct ceph_msg *msg)
766{
767 __le64 sig_check;
768 int ret;
769
Ilya Dryomova51983e2015-10-28 23:52:06 +0100770 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
771 return 0;
772
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100773 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
774 msg, &sig_check);
775 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800776 return ret;
777 if (sig_check == msg->footer.sig)
778 return 0;
779 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
780 dout("ceph_x_check_message_signature %p has signature %llx "
781 "expect %llx\n", msg, msg->footer.sig, sig_check);
782 else
783 dout("ceph_x_check_message_signature %p sender did not set "
784 "CEPH_MSG_FOOTER_SIGNED\n", msg);
785 return -EBADMSG;
786}
Sage Weilec0994e2010-02-02 16:25:35 -0800787
788static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700789 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800790 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700791 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800792 .build_request = ceph_x_build_request,
793 .handle_reply = ceph_x_handle_reply,
794 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700795 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800796 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -0800797 .invalidate_authorizer = ceph_x_invalidate_authorizer,
798 .reset = ceph_x_reset,
799 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800800 .sign_message = ceph_x_sign_message,
801 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800802};
803
804
805int ceph_x_init(struct ceph_auth_client *ac)
806{
807 struct ceph_x_info *xi;
808 int ret;
809
810 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700811 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800812 xi = kzalloc(sizeof(*xi), GFP_NOFS);
813 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700814 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800815
Sage Weilec0994e2010-02-02 16:25:35 -0800816 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700817 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800818 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700819 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800820 }
821
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700822 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
823 if (ret < 0) {
824 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700825 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700826 }
Sage Weilec0994e2010-02-02 16:25:35 -0800827
828 xi->starting = true;
829 xi->ticket_handlers = RB_ROOT;
830
831 ac->protocol = CEPH_AUTH_CEPHX;
832 ac->private = xi;
833 ac->ops = &ceph_x_ops;
834 return 0;
835
Sage Weilb0930f82010-04-29 13:26:53 -0700836out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800837 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700838out:
Sage Weilec0994e2010-02-02 16:25:35 -0800839 return ret;
840}