auth_x.c 27 KB

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