auth.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020 Hannes Reinecke, SUSE Linux
  4. */
  5. #include <linux/crc32.h>
  6. #include <linux/base64.h>
  7. #include <linux/prandom.h>
  8. #include <linux/unaligned.h>
  9. #include <crypto/hash.h>
  10. #include <crypto/dh.h>
  11. #include "nvme.h"
  12. #include "fabrics.h"
  13. #include <linux/nvme-auth.h>
  14. #define CHAP_BUF_SIZE 4096
  15. static struct kmem_cache *nvme_chap_buf_cache;
  16. static mempool_t *nvme_chap_buf_pool;
  17. struct nvme_dhchap_queue_context {
  18. struct list_head entry;
  19. struct work_struct auth_work;
  20. struct nvme_ctrl *ctrl;
  21. struct crypto_shash *shash_tfm;
  22. struct crypto_kpp *dh_tfm;
  23. struct nvme_dhchap_key *transformed_key;
  24. void *buf;
  25. int qid;
  26. int error;
  27. u32 s1;
  28. u32 s2;
  29. bool bi_directional;
  30. u16 transaction;
  31. u8 status;
  32. u8 dhgroup_id;
  33. u8 hash_id;
  34. size_t hash_len;
  35. u8 c1[64];
  36. u8 c2[64];
  37. u8 response[64];
  38. u8 *ctrl_key;
  39. u8 *host_key;
  40. u8 *sess_key;
  41. int ctrl_key_len;
  42. int host_key_len;
  43. int sess_key_len;
  44. };
  45. static struct workqueue_struct *nvme_auth_wq;
  46. static inline int ctrl_max_dhchaps(struct nvme_ctrl *ctrl)
  47. {
  48. return ctrl->opts->nr_io_queues + ctrl->opts->nr_write_queues +
  49. ctrl->opts->nr_poll_queues + 1;
  50. }
  51. static int nvme_auth_submit(struct nvme_ctrl *ctrl, int qid,
  52. void *data, size_t data_len, bool auth_send)
  53. {
  54. struct nvme_command cmd = {};
  55. nvme_submit_flags_t flags = NVME_SUBMIT_RETRY;
  56. struct request_queue *q = ctrl->fabrics_q;
  57. int ret;
  58. if (qid != 0) {
  59. flags |= NVME_SUBMIT_NOWAIT | NVME_SUBMIT_RESERVED;
  60. q = ctrl->connect_q;
  61. }
  62. cmd.auth_common.opcode = nvme_fabrics_command;
  63. cmd.auth_common.secp = NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER;
  64. cmd.auth_common.spsp0 = 0x01;
  65. cmd.auth_common.spsp1 = 0x01;
  66. if (auth_send) {
  67. cmd.auth_send.fctype = nvme_fabrics_type_auth_send;
  68. cmd.auth_send.tl = cpu_to_le32(data_len);
  69. } else {
  70. cmd.auth_receive.fctype = nvme_fabrics_type_auth_receive;
  71. cmd.auth_receive.al = cpu_to_le32(data_len);
  72. }
  73. ret = __nvme_submit_sync_cmd(q, &cmd, NULL, data, data_len,
  74. qid == 0 ? NVME_QID_ANY : qid, flags);
  75. if (ret > 0)
  76. dev_warn(ctrl->device,
  77. "qid %d auth_send failed with status %d\n", qid, ret);
  78. else if (ret < 0)
  79. dev_err(ctrl->device,
  80. "qid %d auth_send failed with error %d\n", qid, ret);
  81. return ret;
  82. }
  83. static int nvme_auth_receive_validate(struct nvme_ctrl *ctrl, int qid,
  84. struct nvmf_auth_dhchap_failure_data *data,
  85. u16 transaction, u8 expected_msg)
  86. {
  87. dev_dbg(ctrl->device, "%s: qid %d auth_type %d auth_id %x\n",
  88. __func__, qid, data->auth_type, data->auth_id);
  89. if (data->auth_type == NVME_AUTH_COMMON_MESSAGES &&
  90. data->auth_id == NVME_AUTH_DHCHAP_MESSAGE_FAILURE1) {
  91. return data->rescode_exp;
  92. }
  93. if (data->auth_type != NVME_AUTH_DHCHAP_MESSAGES ||
  94. data->auth_id != expected_msg) {
  95. dev_warn(ctrl->device,
  96. "qid %d invalid message %02x/%02x\n",
  97. qid, data->auth_type, data->auth_id);
  98. return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_MESSAGE;
  99. }
  100. if (le16_to_cpu(data->t_id) != transaction) {
  101. dev_warn(ctrl->device,
  102. "qid %d invalid transaction ID %d\n",
  103. qid, le16_to_cpu(data->t_id));
  104. return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_MESSAGE;
  105. }
  106. return 0;
  107. }
  108. static int nvme_auth_set_dhchap_negotiate_data(struct nvme_ctrl *ctrl,
  109. struct nvme_dhchap_queue_context *chap)
  110. {
  111. struct nvmf_auth_dhchap_negotiate_data *data = chap->buf;
  112. size_t size = sizeof(*data) + sizeof(union nvmf_auth_protocol);
  113. if (size > CHAP_BUF_SIZE) {
  114. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  115. return -EINVAL;
  116. }
  117. memset((u8 *)chap->buf, 0, size);
  118. data->auth_type = NVME_AUTH_COMMON_MESSAGES;
  119. data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE;
  120. data->t_id = cpu_to_le16(chap->transaction);
  121. data->sc_c = 0; /* No secure channel concatenation */
  122. data->napd = 1;
  123. data->auth_protocol[0].dhchap.authid = NVME_AUTH_DHCHAP_AUTH_ID;
  124. data->auth_protocol[0].dhchap.halen = 3;
  125. data->auth_protocol[0].dhchap.dhlen = 6;
  126. data->auth_protocol[0].dhchap.idlist[0] = NVME_AUTH_HASH_SHA256;
  127. data->auth_protocol[0].dhchap.idlist[1] = NVME_AUTH_HASH_SHA384;
  128. data->auth_protocol[0].dhchap.idlist[2] = NVME_AUTH_HASH_SHA512;
  129. data->auth_protocol[0].dhchap.idlist[30] = NVME_AUTH_DHGROUP_NULL;
  130. data->auth_protocol[0].dhchap.idlist[31] = NVME_AUTH_DHGROUP_2048;
  131. data->auth_protocol[0].dhchap.idlist[32] = NVME_AUTH_DHGROUP_3072;
  132. data->auth_protocol[0].dhchap.idlist[33] = NVME_AUTH_DHGROUP_4096;
  133. data->auth_protocol[0].dhchap.idlist[34] = NVME_AUTH_DHGROUP_6144;
  134. data->auth_protocol[0].dhchap.idlist[35] = NVME_AUTH_DHGROUP_8192;
  135. return size;
  136. }
  137. static int nvme_auth_process_dhchap_challenge(struct nvme_ctrl *ctrl,
  138. struct nvme_dhchap_queue_context *chap)
  139. {
  140. struct nvmf_auth_dhchap_challenge_data *data = chap->buf;
  141. u16 dhvlen = le16_to_cpu(data->dhvlen);
  142. size_t size = sizeof(*data) + data->hl + dhvlen;
  143. const char *gid_name = nvme_auth_dhgroup_name(data->dhgid);
  144. const char *hmac_name, *kpp_name;
  145. if (size > CHAP_BUF_SIZE) {
  146. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  147. return -EINVAL;
  148. }
  149. hmac_name = nvme_auth_hmac_name(data->hashid);
  150. if (!hmac_name) {
  151. dev_warn(ctrl->device,
  152. "qid %d: invalid HASH ID %d\n",
  153. chap->qid, data->hashid);
  154. chap->status = NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE;
  155. return -EPROTO;
  156. }
  157. if (chap->hash_id == data->hashid && chap->shash_tfm &&
  158. !strcmp(crypto_shash_alg_name(chap->shash_tfm), hmac_name) &&
  159. crypto_shash_digestsize(chap->shash_tfm) == data->hl) {
  160. dev_dbg(ctrl->device,
  161. "qid %d: reuse existing hash %s\n",
  162. chap->qid, hmac_name);
  163. goto select_kpp;
  164. }
  165. /* Reset if hash cannot be reused */
  166. if (chap->shash_tfm) {
  167. crypto_free_shash(chap->shash_tfm);
  168. chap->hash_id = 0;
  169. chap->hash_len = 0;
  170. }
  171. chap->shash_tfm = crypto_alloc_shash(hmac_name, 0,
  172. CRYPTO_ALG_ALLOCATES_MEMORY);
  173. if (IS_ERR(chap->shash_tfm)) {
  174. dev_warn(ctrl->device,
  175. "qid %d: failed to allocate hash %s, error %ld\n",
  176. chap->qid, hmac_name, PTR_ERR(chap->shash_tfm));
  177. chap->shash_tfm = NULL;
  178. chap->status = NVME_AUTH_DHCHAP_FAILURE_FAILED;
  179. return -ENOMEM;
  180. }
  181. if (crypto_shash_digestsize(chap->shash_tfm) != data->hl) {
  182. dev_warn(ctrl->device,
  183. "qid %d: invalid hash length %d\n",
  184. chap->qid, data->hl);
  185. crypto_free_shash(chap->shash_tfm);
  186. chap->shash_tfm = NULL;
  187. chap->status = NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE;
  188. return -EPROTO;
  189. }
  190. chap->hash_id = data->hashid;
  191. chap->hash_len = data->hl;
  192. dev_dbg(ctrl->device, "qid %d: selected hash %s\n",
  193. chap->qid, hmac_name);
  194. select_kpp:
  195. kpp_name = nvme_auth_dhgroup_kpp(data->dhgid);
  196. if (!kpp_name) {
  197. dev_warn(ctrl->device,
  198. "qid %d: invalid DH group id %d\n",
  199. chap->qid, data->dhgid);
  200. chap->status = NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE;
  201. /* Leave previous dh_tfm intact */
  202. return -EPROTO;
  203. }
  204. if (chap->dhgroup_id == data->dhgid &&
  205. (data->dhgid == NVME_AUTH_DHGROUP_NULL || chap->dh_tfm)) {
  206. dev_dbg(ctrl->device,
  207. "qid %d: reuse existing DH group %s\n",
  208. chap->qid, gid_name);
  209. goto skip_kpp;
  210. }
  211. /* Reset dh_tfm if it can't be reused */
  212. if (chap->dh_tfm) {
  213. crypto_free_kpp(chap->dh_tfm);
  214. chap->dh_tfm = NULL;
  215. }
  216. if (data->dhgid != NVME_AUTH_DHGROUP_NULL) {
  217. if (dhvlen == 0) {
  218. dev_warn(ctrl->device,
  219. "qid %d: empty DH value\n",
  220. chap->qid);
  221. chap->status = NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE;
  222. return -EPROTO;
  223. }
  224. chap->dh_tfm = crypto_alloc_kpp(kpp_name, 0, 0);
  225. if (IS_ERR(chap->dh_tfm)) {
  226. int ret = PTR_ERR(chap->dh_tfm);
  227. dev_warn(ctrl->device,
  228. "qid %d: error %d initializing DH group %s\n",
  229. chap->qid, ret, gid_name);
  230. chap->status = NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE;
  231. chap->dh_tfm = NULL;
  232. return ret;
  233. }
  234. dev_dbg(ctrl->device, "qid %d: selected DH group %s\n",
  235. chap->qid, gid_name);
  236. } else if (dhvlen != 0) {
  237. dev_warn(ctrl->device,
  238. "qid %d: invalid DH value for NULL DH\n",
  239. chap->qid);
  240. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  241. return -EPROTO;
  242. }
  243. chap->dhgroup_id = data->dhgid;
  244. skip_kpp:
  245. chap->s1 = le32_to_cpu(data->seqnum);
  246. memcpy(chap->c1, data->cval, chap->hash_len);
  247. if (dhvlen) {
  248. chap->ctrl_key = kmalloc(dhvlen, GFP_KERNEL);
  249. if (!chap->ctrl_key) {
  250. chap->status = NVME_AUTH_DHCHAP_FAILURE_FAILED;
  251. return -ENOMEM;
  252. }
  253. chap->ctrl_key_len = dhvlen;
  254. memcpy(chap->ctrl_key, data->cval + chap->hash_len,
  255. dhvlen);
  256. dev_dbg(ctrl->device, "ctrl public key %*ph\n",
  257. (int)chap->ctrl_key_len, chap->ctrl_key);
  258. }
  259. return 0;
  260. }
  261. static int nvme_auth_set_dhchap_reply_data(struct nvme_ctrl *ctrl,
  262. struct nvme_dhchap_queue_context *chap)
  263. {
  264. struct nvmf_auth_dhchap_reply_data *data = chap->buf;
  265. size_t size = sizeof(*data);
  266. size += 2 * chap->hash_len;
  267. if (chap->host_key_len)
  268. size += chap->host_key_len;
  269. if (size > CHAP_BUF_SIZE) {
  270. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  271. return -EINVAL;
  272. }
  273. memset(chap->buf, 0, size);
  274. data->auth_type = NVME_AUTH_DHCHAP_MESSAGES;
  275. data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_REPLY;
  276. data->t_id = cpu_to_le16(chap->transaction);
  277. data->hl = chap->hash_len;
  278. data->dhvlen = cpu_to_le16(chap->host_key_len);
  279. memcpy(data->rval, chap->response, chap->hash_len);
  280. if (ctrl->ctrl_key) {
  281. chap->bi_directional = true;
  282. get_random_bytes(chap->c2, chap->hash_len);
  283. data->cvalid = 1;
  284. memcpy(data->rval + chap->hash_len, chap->c2,
  285. chap->hash_len);
  286. dev_dbg(ctrl->device, "%s: qid %d ctrl challenge %*ph\n",
  287. __func__, chap->qid, (int)chap->hash_len, chap->c2);
  288. } else {
  289. memset(chap->c2, 0, chap->hash_len);
  290. }
  291. chap->s2 = nvme_auth_get_seqnum();
  292. data->seqnum = cpu_to_le32(chap->s2);
  293. if (chap->host_key_len) {
  294. dev_dbg(ctrl->device, "%s: qid %d host public key %*ph\n",
  295. __func__, chap->qid,
  296. chap->host_key_len, chap->host_key);
  297. memcpy(data->rval + 2 * chap->hash_len, chap->host_key,
  298. chap->host_key_len);
  299. }
  300. return size;
  301. }
  302. static int nvme_auth_process_dhchap_success1(struct nvme_ctrl *ctrl,
  303. struct nvme_dhchap_queue_context *chap)
  304. {
  305. struct nvmf_auth_dhchap_success1_data *data = chap->buf;
  306. size_t size = sizeof(*data) + chap->hash_len;
  307. if (size > CHAP_BUF_SIZE) {
  308. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  309. return -EINVAL;
  310. }
  311. if (data->hl != chap->hash_len) {
  312. dev_warn(ctrl->device,
  313. "qid %d: invalid hash length %u\n",
  314. chap->qid, data->hl);
  315. chap->status = NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE;
  316. return -EPROTO;
  317. }
  318. /* Just print out information for the admin queue */
  319. if (chap->qid == 0)
  320. dev_info(ctrl->device,
  321. "qid 0: authenticated with hash %s dhgroup %s\n",
  322. nvme_auth_hmac_name(chap->hash_id),
  323. nvme_auth_dhgroup_name(chap->dhgroup_id));
  324. if (!data->rvalid)
  325. return 0;
  326. /* Validate controller response */
  327. if (memcmp(chap->response, data->rval, data->hl)) {
  328. dev_dbg(ctrl->device, "%s: qid %d ctrl response %*ph\n",
  329. __func__, chap->qid, (int)chap->hash_len, data->rval);
  330. dev_dbg(ctrl->device, "%s: qid %d host response %*ph\n",
  331. __func__, chap->qid, (int)chap->hash_len,
  332. chap->response);
  333. dev_warn(ctrl->device,
  334. "qid %d: controller authentication failed\n",
  335. chap->qid);
  336. chap->status = NVME_AUTH_DHCHAP_FAILURE_FAILED;
  337. return -ECONNREFUSED;
  338. }
  339. /* Just print out information for the admin queue */
  340. if (chap->qid == 0)
  341. dev_info(ctrl->device,
  342. "qid 0: controller authenticated\n");
  343. return 0;
  344. }
  345. static int nvme_auth_set_dhchap_success2_data(struct nvme_ctrl *ctrl,
  346. struct nvme_dhchap_queue_context *chap)
  347. {
  348. struct nvmf_auth_dhchap_success2_data *data = chap->buf;
  349. size_t size = sizeof(*data);
  350. memset(chap->buf, 0, size);
  351. data->auth_type = NVME_AUTH_DHCHAP_MESSAGES;
  352. data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2;
  353. data->t_id = cpu_to_le16(chap->transaction);
  354. return size;
  355. }
  356. static int nvme_auth_set_dhchap_failure2_data(struct nvme_ctrl *ctrl,
  357. struct nvme_dhchap_queue_context *chap)
  358. {
  359. struct nvmf_auth_dhchap_failure_data *data = chap->buf;
  360. size_t size = sizeof(*data);
  361. memset(chap->buf, 0, size);
  362. data->auth_type = NVME_AUTH_COMMON_MESSAGES;
  363. data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_FAILURE2;
  364. data->t_id = cpu_to_le16(chap->transaction);
  365. data->rescode = NVME_AUTH_DHCHAP_FAILURE_REASON_FAILED;
  366. data->rescode_exp = chap->status;
  367. return size;
  368. }
  369. static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
  370. struct nvme_dhchap_queue_context *chap)
  371. {
  372. SHASH_DESC_ON_STACK(shash, chap->shash_tfm);
  373. u8 buf[4], *challenge = chap->c1;
  374. int ret;
  375. dev_dbg(ctrl->device, "%s: qid %d host response seq %u transaction %d\n",
  376. __func__, chap->qid, chap->s1, chap->transaction);
  377. if (!chap->transformed_key) {
  378. chap->transformed_key = nvme_auth_transform_key(ctrl->host_key,
  379. ctrl->opts->host->nqn);
  380. if (IS_ERR(chap->transformed_key)) {
  381. ret = PTR_ERR(chap->transformed_key);
  382. chap->transformed_key = NULL;
  383. return ret;
  384. }
  385. } else {
  386. dev_dbg(ctrl->device, "%s: qid %d re-using host response\n",
  387. __func__, chap->qid);
  388. }
  389. ret = crypto_shash_setkey(chap->shash_tfm,
  390. chap->transformed_key->key, chap->transformed_key->len);
  391. if (ret) {
  392. dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n",
  393. chap->qid, ret);
  394. goto out;
  395. }
  396. if (chap->dh_tfm) {
  397. challenge = kmalloc(chap->hash_len, GFP_KERNEL);
  398. if (!challenge) {
  399. ret = -ENOMEM;
  400. goto out;
  401. }
  402. ret = nvme_auth_augmented_challenge(chap->hash_id,
  403. chap->sess_key,
  404. chap->sess_key_len,
  405. chap->c1, challenge,
  406. chap->hash_len);
  407. if (ret)
  408. goto out;
  409. }
  410. shash->tfm = chap->shash_tfm;
  411. ret = crypto_shash_init(shash);
  412. if (ret)
  413. goto out;
  414. ret = crypto_shash_update(shash, challenge, chap->hash_len);
  415. if (ret)
  416. goto out;
  417. put_unaligned_le32(chap->s1, buf);
  418. ret = crypto_shash_update(shash, buf, 4);
  419. if (ret)
  420. goto out;
  421. put_unaligned_le16(chap->transaction, buf);
  422. ret = crypto_shash_update(shash, buf, 2);
  423. if (ret)
  424. goto out;
  425. memset(buf, 0, sizeof(buf));
  426. ret = crypto_shash_update(shash, buf, 1);
  427. if (ret)
  428. goto out;
  429. ret = crypto_shash_update(shash, "HostHost", 8);
  430. if (ret)
  431. goto out;
  432. ret = crypto_shash_update(shash, ctrl->opts->host->nqn,
  433. strlen(ctrl->opts->host->nqn));
  434. if (ret)
  435. goto out;
  436. ret = crypto_shash_update(shash, buf, 1);
  437. if (ret)
  438. goto out;
  439. ret = crypto_shash_update(shash, ctrl->opts->subsysnqn,
  440. strlen(ctrl->opts->subsysnqn));
  441. if (ret)
  442. goto out;
  443. ret = crypto_shash_final(shash, chap->response);
  444. out:
  445. if (challenge != chap->c1)
  446. kfree(challenge);
  447. return ret;
  448. }
  449. static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl,
  450. struct nvme_dhchap_queue_context *chap)
  451. {
  452. SHASH_DESC_ON_STACK(shash, chap->shash_tfm);
  453. struct nvme_dhchap_key *transformed_key;
  454. u8 buf[4], *challenge = chap->c2;
  455. int ret;
  456. transformed_key = nvme_auth_transform_key(ctrl->ctrl_key,
  457. ctrl->opts->subsysnqn);
  458. if (IS_ERR(transformed_key)) {
  459. ret = PTR_ERR(transformed_key);
  460. return ret;
  461. }
  462. ret = crypto_shash_setkey(chap->shash_tfm,
  463. transformed_key->key, transformed_key->len);
  464. if (ret) {
  465. dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n",
  466. chap->qid, ret);
  467. goto out;
  468. }
  469. if (chap->dh_tfm) {
  470. challenge = kmalloc(chap->hash_len, GFP_KERNEL);
  471. if (!challenge) {
  472. ret = -ENOMEM;
  473. goto out;
  474. }
  475. ret = nvme_auth_augmented_challenge(chap->hash_id,
  476. chap->sess_key,
  477. chap->sess_key_len,
  478. chap->c2, challenge,
  479. chap->hash_len);
  480. if (ret)
  481. goto out;
  482. }
  483. dev_dbg(ctrl->device, "%s: qid %d ctrl response seq %u transaction %d\n",
  484. __func__, chap->qid, chap->s2, chap->transaction);
  485. dev_dbg(ctrl->device, "%s: qid %d challenge %*ph\n",
  486. __func__, chap->qid, (int)chap->hash_len, challenge);
  487. dev_dbg(ctrl->device, "%s: qid %d subsysnqn %s\n",
  488. __func__, chap->qid, ctrl->opts->subsysnqn);
  489. dev_dbg(ctrl->device, "%s: qid %d hostnqn %s\n",
  490. __func__, chap->qid, ctrl->opts->host->nqn);
  491. shash->tfm = chap->shash_tfm;
  492. ret = crypto_shash_init(shash);
  493. if (ret)
  494. goto out;
  495. ret = crypto_shash_update(shash, challenge, chap->hash_len);
  496. if (ret)
  497. goto out;
  498. put_unaligned_le32(chap->s2, buf);
  499. ret = crypto_shash_update(shash, buf, 4);
  500. if (ret)
  501. goto out;
  502. put_unaligned_le16(chap->transaction, buf);
  503. ret = crypto_shash_update(shash, buf, 2);
  504. if (ret)
  505. goto out;
  506. memset(buf, 0, 4);
  507. ret = crypto_shash_update(shash, buf, 1);
  508. if (ret)
  509. goto out;
  510. ret = crypto_shash_update(shash, "Controller", 10);
  511. if (ret)
  512. goto out;
  513. ret = crypto_shash_update(shash, ctrl->opts->subsysnqn,
  514. strlen(ctrl->opts->subsysnqn));
  515. if (ret)
  516. goto out;
  517. ret = crypto_shash_update(shash, buf, 1);
  518. if (ret)
  519. goto out;
  520. ret = crypto_shash_update(shash, ctrl->opts->host->nqn,
  521. strlen(ctrl->opts->host->nqn));
  522. if (ret)
  523. goto out;
  524. ret = crypto_shash_final(shash, chap->response);
  525. out:
  526. if (challenge != chap->c2)
  527. kfree(challenge);
  528. nvme_auth_free_key(transformed_key);
  529. return ret;
  530. }
  531. static int nvme_auth_dhchap_exponential(struct nvme_ctrl *ctrl,
  532. struct nvme_dhchap_queue_context *chap)
  533. {
  534. int ret;
  535. if (chap->host_key && chap->host_key_len) {
  536. dev_dbg(ctrl->device,
  537. "qid %d: reusing host key\n", chap->qid);
  538. goto gen_sesskey;
  539. }
  540. ret = nvme_auth_gen_privkey(chap->dh_tfm, chap->dhgroup_id);
  541. if (ret < 0) {
  542. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  543. return ret;
  544. }
  545. chap->host_key_len = crypto_kpp_maxsize(chap->dh_tfm);
  546. chap->host_key = kzalloc(chap->host_key_len, GFP_KERNEL);
  547. if (!chap->host_key) {
  548. chap->host_key_len = 0;
  549. chap->status = NVME_AUTH_DHCHAP_FAILURE_FAILED;
  550. return -ENOMEM;
  551. }
  552. ret = nvme_auth_gen_pubkey(chap->dh_tfm,
  553. chap->host_key, chap->host_key_len);
  554. if (ret) {
  555. dev_dbg(ctrl->device,
  556. "failed to generate public key, error %d\n", ret);
  557. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  558. return ret;
  559. }
  560. gen_sesskey:
  561. chap->sess_key_len = chap->host_key_len;
  562. chap->sess_key = kmalloc(chap->sess_key_len, GFP_KERNEL);
  563. if (!chap->sess_key) {
  564. chap->sess_key_len = 0;
  565. chap->status = NVME_AUTH_DHCHAP_FAILURE_FAILED;
  566. return -ENOMEM;
  567. }
  568. ret = nvme_auth_gen_shared_secret(chap->dh_tfm,
  569. chap->ctrl_key, chap->ctrl_key_len,
  570. chap->sess_key, chap->sess_key_len);
  571. if (ret) {
  572. dev_dbg(ctrl->device,
  573. "failed to generate shared secret, error %d\n", ret);
  574. chap->status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
  575. return ret;
  576. }
  577. dev_dbg(ctrl->device, "shared secret %*ph\n",
  578. (int)chap->sess_key_len, chap->sess_key);
  579. return 0;
  580. }
  581. static void nvme_auth_reset_dhchap(struct nvme_dhchap_queue_context *chap)
  582. {
  583. nvme_auth_free_key(chap->transformed_key);
  584. chap->transformed_key = NULL;
  585. kfree_sensitive(chap->host_key);
  586. chap->host_key = NULL;
  587. chap->host_key_len = 0;
  588. kfree_sensitive(chap->ctrl_key);
  589. chap->ctrl_key = NULL;
  590. chap->ctrl_key_len = 0;
  591. kfree_sensitive(chap->sess_key);
  592. chap->sess_key = NULL;
  593. chap->sess_key_len = 0;
  594. chap->status = 0;
  595. chap->error = 0;
  596. chap->s1 = 0;
  597. chap->s2 = 0;
  598. chap->bi_directional = false;
  599. chap->transaction = 0;
  600. memset(chap->c1, 0, sizeof(chap->c1));
  601. memset(chap->c2, 0, sizeof(chap->c2));
  602. mempool_free(chap->buf, nvme_chap_buf_pool);
  603. chap->buf = NULL;
  604. }
  605. static void nvme_auth_free_dhchap(struct nvme_dhchap_queue_context *chap)
  606. {
  607. nvme_auth_reset_dhchap(chap);
  608. if (chap->shash_tfm)
  609. crypto_free_shash(chap->shash_tfm);
  610. if (chap->dh_tfm)
  611. crypto_free_kpp(chap->dh_tfm);
  612. }
  613. static void nvme_queue_auth_work(struct work_struct *work)
  614. {
  615. struct nvme_dhchap_queue_context *chap =
  616. container_of(work, struct nvme_dhchap_queue_context, auth_work);
  617. struct nvme_ctrl *ctrl = chap->ctrl;
  618. size_t tl;
  619. int ret = 0;
  620. /*
  621. * Allocate a large enough buffer for the entire negotiation:
  622. * 4k is enough to ffdhe8192.
  623. */
  624. chap->buf = mempool_alloc(nvme_chap_buf_pool, GFP_KERNEL);
  625. if (!chap->buf) {
  626. chap->error = -ENOMEM;
  627. return;
  628. }
  629. chap->transaction = ctrl->transaction++;
  630. /* DH-HMAC-CHAP Step 1: send negotiate */
  631. dev_dbg(ctrl->device, "%s: qid %d send negotiate\n",
  632. __func__, chap->qid);
  633. ret = nvme_auth_set_dhchap_negotiate_data(ctrl, chap);
  634. if (ret < 0) {
  635. chap->error = ret;
  636. return;
  637. }
  638. tl = ret;
  639. ret = nvme_auth_submit(ctrl, chap->qid, chap->buf, tl, true);
  640. if (ret) {
  641. chap->error = ret;
  642. return;
  643. }
  644. /* DH-HMAC-CHAP Step 2: receive challenge */
  645. dev_dbg(ctrl->device, "%s: qid %d receive challenge\n",
  646. __func__, chap->qid);
  647. memset(chap->buf, 0, CHAP_BUF_SIZE);
  648. ret = nvme_auth_submit(ctrl, chap->qid, chap->buf, CHAP_BUF_SIZE,
  649. false);
  650. if (ret) {
  651. dev_warn(ctrl->device,
  652. "qid %d failed to receive challenge, %s %d\n",
  653. chap->qid, ret < 0 ? "error" : "nvme status", ret);
  654. chap->error = ret;
  655. return;
  656. }
  657. ret = nvme_auth_receive_validate(ctrl, chap->qid, chap->buf, chap->transaction,
  658. NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE);
  659. if (ret) {
  660. chap->status = ret;
  661. chap->error = -EKEYREJECTED;
  662. return;
  663. }
  664. ret = nvme_auth_process_dhchap_challenge(ctrl, chap);
  665. if (ret) {
  666. /* Invalid challenge parameters */
  667. chap->error = ret;
  668. goto fail2;
  669. }
  670. if (chap->ctrl_key_len) {
  671. dev_dbg(ctrl->device,
  672. "%s: qid %d DH exponential\n",
  673. __func__, chap->qid);
  674. ret = nvme_auth_dhchap_exponential(ctrl, chap);
  675. if (ret) {
  676. chap->error = ret;
  677. goto fail2;
  678. }
  679. }
  680. dev_dbg(ctrl->device, "%s: qid %d host response\n",
  681. __func__, chap->qid);
  682. mutex_lock(&ctrl->dhchap_auth_mutex);
  683. ret = nvme_auth_dhchap_setup_host_response(ctrl, chap);
  684. mutex_unlock(&ctrl->dhchap_auth_mutex);
  685. if (ret) {
  686. chap->error = ret;
  687. goto fail2;
  688. }
  689. /* DH-HMAC-CHAP Step 3: send reply */
  690. dev_dbg(ctrl->device, "%s: qid %d send reply\n",
  691. __func__, chap->qid);
  692. ret = nvme_auth_set_dhchap_reply_data(ctrl, chap);
  693. if (ret < 0) {
  694. chap->error = ret;
  695. goto fail2;
  696. }
  697. tl = ret;
  698. ret = nvme_auth_submit(ctrl, chap->qid, chap->buf, tl, true);
  699. if (ret) {
  700. chap->error = ret;
  701. goto fail2;
  702. }
  703. /* DH-HMAC-CHAP Step 4: receive success1 */
  704. dev_dbg(ctrl->device, "%s: qid %d receive success1\n",
  705. __func__, chap->qid);
  706. memset(chap->buf, 0, CHAP_BUF_SIZE);
  707. ret = nvme_auth_submit(ctrl, chap->qid, chap->buf, CHAP_BUF_SIZE,
  708. false);
  709. if (ret) {
  710. dev_warn(ctrl->device,
  711. "qid %d failed to receive success1, %s %d\n",
  712. chap->qid, ret < 0 ? "error" : "nvme status", ret);
  713. chap->error = ret;
  714. return;
  715. }
  716. ret = nvme_auth_receive_validate(ctrl, chap->qid,
  717. chap->buf, chap->transaction,
  718. NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1);
  719. if (ret) {
  720. chap->status = ret;
  721. chap->error = -EKEYREJECTED;
  722. return;
  723. }
  724. mutex_lock(&ctrl->dhchap_auth_mutex);
  725. if (ctrl->ctrl_key) {
  726. dev_dbg(ctrl->device,
  727. "%s: qid %d controller response\n",
  728. __func__, chap->qid);
  729. ret = nvme_auth_dhchap_setup_ctrl_response(ctrl, chap);
  730. if (ret) {
  731. mutex_unlock(&ctrl->dhchap_auth_mutex);
  732. chap->error = ret;
  733. goto fail2;
  734. }
  735. }
  736. mutex_unlock(&ctrl->dhchap_auth_mutex);
  737. ret = nvme_auth_process_dhchap_success1(ctrl, chap);
  738. if (ret) {
  739. /* Controller authentication failed */
  740. chap->error = -EKEYREJECTED;
  741. goto fail2;
  742. }
  743. if (chap->bi_directional) {
  744. /* DH-HMAC-CHAP Step 5: send success2 */
  745. dev_dbg(ctrl->device, "%s: qid %d send success2\n",
  746. __func__, chap->qid);
  747. tl = nvme_auth_set_dhchap_success2_data(ctrl, chap);
  748. ret = nvme_auth_submit(ctrl, chap->qid, chap->buf, tl, true);
  749. if (ret)
  750. chap->error = ret;
  751. }
  752. if (!ret) {
  753. chap->error = 0;
  754. return;
  755. }
  756. fail2:
  757. if (chap->status == 0)
  758. chap->status = NVME_AUTH_DHCHAP_FAILURE_FAILED;
  759. dev_dbg(ctrl->device, "%s: qid %d send failure2, status %x\n",
  760. __func__, chap->qid, chap->status);
  761. tl = nvme_auth_set_dhchap_failure2_data(ctrl, chap);
  762. ret = nvme_auth_submit(ctrl, chap->qid, chap->buf, tl, true);
  763. /*
  764. * only update error if send failure2 failed and no other
  765. * error had been set during authentication.
  766. */
  767. if (ret && !chap->error)
  768. chap->error = ret;
  769. }
  770. int nvme_auth_negotiate(struct nvme_ctrl *ctrl, int qid)
  771. {
  772. struct nvme_dhchap_queue_context *chap;
  773. if (!ctrl->host_key) {
  774. dev_warn(ctrl->device, "qid %d: no key\n", qid);
  775. return -ENOKEY;
  776. }
  777. if (ctrl->opts->dhchap_ctrl_secret && !ctrl->ctrl_key) {
  778. dev_warn(ctrl->device, "qid %d: invalid ctrl key\n", qid);
  779. return -ENOKEY;
  780. }
  781. chap = &ctrl->dhchap_ctxs[qid];
  782. cancel_work_sync(&chap->auth_work);
  783. queue_work(nvme_auth_wq, &chap->auth_work);
  784. return 0;
  785. }
  786. EXPORT_SYMBOL_GPL(nvme_auth_negotiate);
  787. int nvme_auth_wait(struct nvme_ctrl *ctrl, int qid)
  788. {
  789. struct nvme_dhchap_queue_context *chap;
  790. int ret;
  791. chap = &ctrl->dhchap_ctxs[qid];
  792. flush_work(&chap->auth_work);
  793. ret = chap->error;
  794. /* clear sensitive info */
  795. nvme_auth_reset_dhchap(chap);
  796. return ret;
  797. }
  798. EXPORT_SYMBOL_GPL(nvme_auth_wait);
  799. static void nvme_ctrl_auth_work(struct work_struct *work)
  800. {
  801. struct nvme_ctrl *ctrl =
  802. container_of(work, struct nvme_ctrl, dhchap_auth_work);
  803. int ret, q;
  804. /*
  805. * If the ctrl is no connected, bail as reconnect will handle
  806. * authentication.
  807. */
  808. if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
  809. return;
  810. /* Authenticate admin queue first */
  811. ret = nvme_auth_negotiate(ctrl, 0);
  812. if (ret) {
  813. dev_warn(ctrl->device,
  814. "qid 0: error %d setting up authentication\n", ret);
  815. return;
  816. }
  817. ret = nvme_auth_wait(ctrl, 0);
  818. if (ret) {
  819. dev_warn(ctrl->device,
  820. "qid 0: authentication failed\n");
  821. return;
  822. }
  823. for (q = 1; q < ctrl->queue_count; q++) {
  824. ret = nvme_auth_negotiate(ctrl, q);
  825. if (ret) {
  826. dev_warn(ctrl->device,
  827. "qid %d: error %d setting up authentication\n",
  828. q, ret);
  829. break;
  830. }
  831. }
  832. /*
  833. * Failure is a soft-state; credentials remain valid until
  834. * the controller terminates the connection.
  835. */
  836. for (q = 1; q < ctrl->queue_count; q++) {
  837. ret = nvme_auth_wait(ctrl, q);
  838. if (ret)
  839. dev_warn(ctrl->device,
  840. "qid %d: authentication failed\n", q);
  841. }
  842. }
  843. int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
  844. {
  845. struct nvme_dhchap_queue_context *chap;
  846. int i, ret;
  847. mutex_init(&ctrl->dhchap_auth_mutex);
  848. INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
  849. if (!ctrl->opts)
  850. return 0;
  851. ret = nvme_auth_generate_key(ctrl->opts->dhchap_secret,
  852. &ctrl->host_key);
  853. if (ret)
  854. return ret;
  855. ret = nvme_auth_generate_key(ctrl->opts->dhchap_ctrl_secret,
  856. &ctrl->ctrl_key);
  857. if (ret)
  858. goto err_free_dhchap_secret;
  859. if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
  860. return 0;
  861. ctrl->dhchap_ctxs = kvcalloc(ctrl_max_dhchaps(ctrl),
  862. sizeof(*chap), GFP_KERNEL);
  863. if (!ctrl->dhchap_ctxs) {
  864. ret = -ENOMEM;
  865. goto err_free_dhchap_ctrl_secret;
  866. }
  867. for (i = 0; i < ctrl_max_dhchaps(ctrl); i++) {
  868. chap = &ctrl->dhchap_ctxs[i];
  869. chap->qid = i;
  870. chap->ctrl = ctrl;
  871. INIT_WORK(&chap->auth_work, nvme_queue_auth_work);
  872. }
  873. return 0;
  874. err_free_dhchap_ctrl_secret:
  875. nvme_auth_free_key(ctrl->ctrl_key);
  876. ctrl->ctrl_key = NULL;
  877. err_free_dhchap_secret:
  878. nvme_auth_free_key(ctrl->host_key);
  879. ctrl->host_key = NULL;
  880. return ret;
  881. }
  882. EXPORT_SYMBOL_GPL(nvme_auth_init_ctrl);
  883. void nvme_auth_stop(struct nvme_ctrl *ctrl)
  884. {
  885. cancel_work_sync(&ctrl->dhchap_auth_work);
  886. }
  887. EXPORT_SYMBOL_GPL(nvme_auth_stop);
  888. void nvme_auth_free(struct nvme_ctrl *ctrl)
  889. {
  890. int i;
  891. if (ctrl->dhchap_ctxs) {
  892. for (i = 0; i < ctrl_max_dhchaps(ctrl); i++)
  893. nvme_auth_free_dhchap(&ctrl->dhchap_ctxs[i]);
  894. kfree(ctrl->dhchap_ctxs);
  895. }
  896. if (ctrl->host_key) {
  897. nvme_auth_free_key(ctrl->host_key);
  898. ctrl->host_key = NULL;
  899. }
  900. if (ctrl->ctrl_key) {
  901. nvme_auth_free_key(ctrl->ctrl_key);
  902. ctrl->ctrl_key = NULL;
  903. }
  904. }
  905. EXPORT_SYMBOL_GPL(nvme_auth_free);
  906. int __init nvme_init_auth(void)
  907. {
  908. nvme_auth_wq = alloc_workqueue("nvme-auth-wq",
  909. WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
  910. if (!nvme_auth_wq)
  911. return -ENOMEM;
  912. nvme_chap_buf_cache = kmem_cache_create("nvme-chap-buf-cache",
  913. CHAP_BUF_SIZE, 0, SLAB_HWCACHE_ALIGN, NULL);
  914. if (!nvme_chap_buf_cache)
  915. goto err_destroy_workqueue;
  916. nvme_chap_buf_pool = mempool_create(16, mempool_alloc_slab,
  917. mempool_free_slab, nvme_chap_buf_cache);
  918. if (!nvme_chap_buf_pool)
  919. goto err_destroy_chap_buf_cache;
  920. return 0;
  921. err_destroy_chap_buf_cache:
  922. kmem_cache_destroy(nvme_chap_buf_cache);
  923. err_destroy_workqueue:
  924. destroy_workqueue(nvme_auth_wq);
  925. return -ENOMEM;
  926. }
  927. void __exit nvme_exit_auth(void)
  928. {
  929. mempool_destroy(nvme_chap_buf_pool);
  930. kmem_cache_destroy(nvme_chap_buf_cache);
  931. destroy_workqueue(nvme_auth_wq);
  932. }