auth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2018 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  6. */
  7. /*
  8. * This file implements various helper functions for UBIFS authentication support
  9. */
  10. #include <linux/crypto.h>
  11. #include <linux/verification.h>
  12. #include <crypto/hash.h>
  13. #include <crypto/sha.h>
  14. #include <crypto/algapi.h>
  15. #include <keys/user-type.h>
  16. #include <keys/asymmetric-type.h>
  17. #include "ubifs.h"
  18. /**
  19. * ubifs_node_calc_hash - calculate the hash of a UBIFS node
  20. * @c: UBIFS file-system description object
  21. * @node: the node to calculate a hash for
  22. * @hash: the returned hash
  23. *
  24. * Returns 0 for success or a negative error code otherwise.
  25. */
  26. int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *node,
  27. u8 *hash)
  28. {
  29. const struct ubifs_ch *ch = node;
  30. return crypto_shash_tfm_digest(c->hash_tfm, node, le32_to_cpu(ch->len),
  31. hash);
  32. }
  33. /**
  34. * ubifs_hash_calc_hmac - calculate a HMAC from a hash
  35. * @c: UBIFS file-system description object
  36. * @hash: the node to calculate a HMAC for
  37. * @hmac: the returned HMAC
  38. *
  39. * Returns 0 for success or a negative error code otherwise.
  40. */
  41. static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
  42. u8 *hmac)
  43. {
  44. return crypto_shash_tfm_digest(c->hmac_tfm, hash, c->hash_len, hmac);
  45. }
  46. /**
  47. * ubifs_prepare_auth_node - Prepare an authentication node
  48. * @c: UBIFS file-system description object
  49. * @node: the node to calculate a hash for
  50. * @inhash: input hash of previous nodes
  51. *
  52. * This function prepares an authentication node for writing onto flash.
  53. * It creates a HMAC from the given input hash and writes it to the node.
  54. *
  55. * Returns 0 for success or a negative error code otherwise.
  56. */
  57. int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
  58. struct shash_desc *inhash)
  59. {
  60. struct ubifs_auth_node *auth = node;
  61. u8 hash[UBIFS_HASH_ARR_SZ];
  62. int err;
  63. {
  64. SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
  65. hash_desc->tfm = c->hash_tfm;
  66. ubifs_shash_copy_state(c, inhash, hash_desc);
  67. err = crypto_shash_final(hash_desc, hash);
  68. if (err)
  69. return err;
  70. }
  71. err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
  72. if (err)
  73. return err;
  74. auth->ch.node_type = UBIFS_AUTH_NODE;
  75. ubifs_prepare_node(c, auth, ubifs_auth_node_sz(c), 0);
  76. return 0;
  77. }
  78. static struct shash_desc *ubifs_get_desc(const struct ubifs_info *c,
  79. struct crypto_shash *tfm)
  80. {
  81. struct shash_desc *desc;
  82. int err;
  83. if (!ubifs_authenticated(c))
  84. return NULL;
  85. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
  86. if (!desc)
  87. return ERR_PTR(-ENOMEM);
  88. desc->tfm = tfm;
  89. err = crypto_shash_init(desc);
  90. if (err) {
  91. kfree(desc);
  92. return ERR_PTR(err);
  93. }
  94. return desc;
  95. }
  96. /**
  97. * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
  98. * @c: UBIFS file-system description object
  99. *
  100. * This function returns a descriptor suitable for hashing a node. Free after use
  101. * with kfree.
  102. */
  103. struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c)
  104. {
  105. return ubifs_get_desc(c, c->hash_tfm);
  106. }
  107. /**
  108. * ubifs_bad_hash - Report hash mismatches
  109. * @c: UBIFS file-system description object
  110. * @node: the node
  111. * @hash: the expected hash
  112. * @lnum: the LEB @node was read from
  113. * @offs: offset in LEB @node was read from
  114. *
  115. * This function reports a hash mismatch when a node has a different hash than
  116. * expected.
  117. */
  118. void ubifs_bad_hash(const struct ubifs_info *c, const void *node, const u8 *hash,
  119. int lnum, int offs)
  120. {
  121. int len = min(c->hash_len, 20);
  122. int cropped = len != c->hash_len;
  123. const char *cont = cropped ? "..." : "";
  124. u8 calc[UBIFS_HASH_ARR_SZ];
  125. __ubifs_node_calc_hash(c, node, calc);
  126. ubifs_err(c, "hash mismatch on node at LEB %d:%d", lnum, offs);
  127. ubifs_err(c, "hash expected: %*ph%s", len, hash, cont);
  128. ubifs_err(c, "hash calculated: %*ph%s", len, calc, cont);
  129. }
  130. /**
  131. * __ubifs_node_check_hash - check the hash of a node against given hash
  132. * @c: UBIFS file-system description object
  133. * @node: the node
  134. * @expected: the expected hash
  135. *
  136. * This function calculates a hash over a node and compares it to the given hash.
  137. * Returns 0 if both hashes are equal or authentication is disabled, otherwise a
  138. * negative error code is returned.
  139. */
  140. int __ubifs_node_check_hash(const struct ubifs_info *c, const void *node,
  141. const u8 *expected)
  142. {
  143. u8 calc[UBIFS_HASH_ARR_SZ];
  144. int err;
  145. err = __ubifs_node_calc_hash(c, node, calc);
  146. if (err)
  147. return err;
  148. if (ubifs_check_hash(c, expected, calc))
  149. return -EPERM;
  150. return 0;
  151. }
  152. /**
  153. * ubifs_sb_verify_signature - verify the signature of a superblock
  154. * @c: UBIFS file-system description object
  155. * @sup: The superblock node
  156. *
  157. * To support offline signed images the superblock can be signed with a
  158. * PKCS#7 signature. The signature is placed directly behind the superblock
  159. * node in an ubifs_sig_node.
  160. *
  161. * Returns 0 when the signature can be successfully verified or a negative
  162. * error code if not.
  163. */
  164. int ubifs_sb_verify_signature(struct ubifs_info *c,
  165. const struct ubifs_sb_node *sup)
  166. {
  167. int err;
  168. struct ubifs_scan_leb *sleb;
  169. struct ubifs_scan_node *snod;
  170. const struct ubifs_sig_node *signode;
  171. sleb = ubifs_scan(c, UBIFS_SB_LNUM, UBIFS_SB_NODE_SZ, c->sbuf, 0);
  172. if (IS_ERR(sleb)) {
  173. err = PTR_ERR(sleb);
  174. return err;
  175. }
  176. if (sleb->nodes_cnt == 0) {
  177. ubifs_err(c, "Unable to find signature node");
  178. err = -EINVAL;
  179. goto out_destroy;
  180. }
  181. snod = list_first_entry(&sleb->nodes, struct ubifs_scan_node, list);
  182. if (snod->type != UBIFS_SIG_NODE) {
  183. ubifs_err(c, "Signature node is of wrong type");
  184. err = -EINVAL;
  185. goto out_destroy;
  186. }
  187. signode = snod->node;
  188. if (le32_to_cpu(signode->len) > snod->len + sizeof(struct ubifs_sig_node)) {
  189. ubifs_err(c, "invalid signature len %d", le32_to_cpu(signode->len));
  190. err = -EINVAL;
  191. goto out_destroy;
  192. }
  193. if (le32_to_cpu(signode->type) != UBIFS_SIGNATURE_TYPE_PKCS7) {
  194. ubifs_err(c, "Signature type %d is not supported\n",
  195. le32_to_cpu(signode->type));
  196. err = -EINVAL;
  197. goto out_destroy;
  198. }
  199. err = verify_pkcs7_signature(sup, sizeof(struct ubifs_sb_node),
  200. signode->sig, le32_to_cpu(signode->len),
  201. NULL, VERIFYING_UNSPECIFIED_SIGNATURE,
  202. NULL, NULL);
  203. if (err)
  204. ubifs_err(c, "Failed to verify signature");
  205. else
  206. ubifs_msg(c, "Successfully verified super block signature");
  207. out_destroy:
  208. ubifs_scan_destroy(sleb);
  209. return err;
  210. }
  211. /**
  212. * ubifs_init_authentication - initialize UBIFS authentication support
  213. * @c: UBIFS file-system description object
  214. *
  215. * This function returns 0 for success or a negative error code otherwise.
  216. */
  217. int ubifs_init_authentication(struct ubifs_info *c)
  218. {
  219. struct key *keyring_key;
  220. const struct user_key_payload *ukp;
  221. int err;
  222. char hmac_name[CRYPTO_MAX_ALG_NAME];
  223. if (!c->auth_hash_name) {
  224. ubifs_err(c, "authentication hash name needed with authentication");
  225. return -EINVAL;
  226. }
  227. c->auth_hash_algo = match_string(hash_algo_name, HASH_ALGO__LAST,
  228. c->auth_hash_name);
  229. if ((int)c->auth_hash_algo < 0) {
  230. ubifs_err(c, "Unknown hash algo %s specified",
  231. c->auth_hash_name);
  232. return -EINVAL;
  233. }
  234. snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
  235. c->auth_hash_name);
  236. keyring_key = request_key(&key_type_logon, c->auth_key_name, NULL);
  237. if (IS_ERR(keyring_key)) {
  238. ubifs_err(c, "Failed to request key: %ld",
  239. PTR_ERR(keyring_key));
  240. return PTR_ERR(keyring_key);
  241. }
  242. down_read(&keyring_key->sem);
  243. if (keyring_key->type != &key_type_logon) {
  244. ubifs_err(c, "key type must be logon");
  245. err = -ENOKEY;
  246. goto out;
  247. }
  248. ukp = user_key_payload_locked(keyring_key);
  249. if (!ukp) {
  250. /* key was revoked before we acquired its semaphore */
  251. err = -EKEYREVOKED;
  252. goto out;
  253. }
  254. c->hash_tfm = crypto_alloc_shash(c->auth_hash_name, 0, 0);
  255. if (IS_ERR(c->hash_tfm)) {
  256. err = PTR_ERR(c->hash_tfm);
  257. ubifs_err(c, "Can not allocate %s: %d",
  258. c->auth_hash_name, err);
  259. goto out;
  260. }
  261. c->hash_len = crypto_shash_digestsize(c->hash_tfm);
  262. if (c->hash_len > UBIFS_HASH_ARR_SZ) {
  263. ubifs_err(c, "hash %s is bigger than maximum allowed hash size (%d > %d)",
  264. c->auth_hash_name, c->hash_len, UBIFS_HASH_ARR_SZ);
  265. err = -EINVAL;
  266. goto out_free_hash;
  267. }
  268. c->hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0);
  269. if (IS_ERR(c->hmac_tfm)) {
  270. err = PTR_ERR(c->hmac_tfm);
  271. ubifs_err(c, "Can not allocate %s: %d", hmac_name, err);
  272. goto out_free_hash;
  273. }
  274. c->hmac_desc_len = crypto_shash_digestsize(c->hmac_tfm);
  275. if (c->hmac_desc_len > UBIFS_HMAC_ARR_SZ) {
  276. ubifs_err(c, "hmac %s is bigger than maximum allowed hmac size (%d > %d)",
  277. hmac_name, c->hmac_desc_len, UBIFS_HMAC_ARR_SZ);
  278. err = -EINVAL;
  279. goto out_free_hmac;
  280. }
  281. err = crypto_shash_setkey(c->hmac_tfm, ukp->data, ukp->datalen);
  282. if (err)
  283. goto out_free_hmac;
  284. c->authenticated = true;
  285. c->log_hash = ubifs_hash_get_desc(c);
  286. if (IS_ERR(c->log_hash)) {
  287. err = PTR_ERR(c->log_hash);
  288. goto out_free_hmac;
  289. }
  290. err = 0;
  291. out_free_hmac:
  292. if (err)
  293. crypto_free_shash(c->hmac_tfm);
  294. out_free_hash:
  295. if (err)
  296. crypto_free_shash(c->hash_tfm);
  297. out:
  298. up_read(&keyring_key->sem);
  299. key_put(keyring_key);
  300. return err;
  301. }
  302. /**
  303. * __ubifs_exit_authentication - release resource
  304. * @c: UBIFS file-system description object
  305. *
  306. * This function releases the authentication related resources.
  307. */
  308. void __ubifs_exit_authentication(struct ubifs_info *c)
  309. {
  310. if (!ubifs_authenticated(c))
  311. return;
  312. crypto_free_shash(c->hmac_tfm);
  313. crypto_free_shash(c->hash_tfm);
  314. kfree(c->log_hash);
  315. }
  316. /**
  317. * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node
  318. * @c: UBIFS file-system description object
  319. * @node: the node to insert a HMAC into.
  320. * @len: the length of the node
  321. * @ofs_hmac: the offset in the node where the HMAC is inserted
  322. * @hmac: returned HMAC
  323. *
  324. * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be
  325. * embedded into the node, so this area is not covered by the HMAC. Also not
  326. * covered is the UBIFS_NODE_MAGIC and the CRC of the node.
  327. */
  328. static int ubifs_node_calc_hmac(const struct ubifs_info *c, const void *node,
  329. int len, int ofs_hmac, void *hmac)
  330. {
  331. SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
  332. int hmac_len = c->hmac_desc_len;
  333. int err;
  334. ubifs_assert(c, ofs_hmac > 8);
  335. ubifs_assert(c, ofs_hmac + hmac_len < len);
  336. shash->tfm = c->hmac_tfm;
  337. err = crypto_shash_init(shash);
  338. if (err)
  339. return err;
  340. /* behind common node header CRC up to HMAC begin */
  341. err = crypto_shash_update(shash, node + 8, ofs_hmac - 8);
  342. if (err < 0)
  343. return err;
  344. /* behind HMAC, if any */
  345. if (len - ofs_hmac - hmac_len > 0) {
  346. err = crypto_shash_update(shash, node + ofs_hmac + hmac_len,
  347. len - ofs_hmac - hmac_len);
  348. if (err < 0)
  349. return err;
  350. }
  351. return crypto_shash_final(shash, hmac);
  352. }
  353. /**
  354. * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node
  355. * @c: UBIFS file-system description object
  356. * @node: the node to insert a HMAC into.
  357. * @len: the length of the node
  358. * @ofs_hmac: the offset in the node where the HMAC is inserted
  359. *
  360. * This function inserts a HMAC at offset @ofs_hmac into the node given in
  361. * @node.
  362. *
  363. * This function returns 0 for success or a negative error code otherwise.
  364. */
  365. int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *node, int len,
  366. int ofs_hmac)
  367. {
  368. return ubifs_node_calc_hmac(c, node, len, ofs_hmac, node + ofs_hmac);
  369. }
  370. /**
  371. * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node
  372. * @c: UBIFS file-system description object
  373. * @node: the node to insert a HMAC into.
  374. * @len: the length of the node
  375. * @ofs_hmac: the offset in the node where the HMAC is inserted
  376. *
  377. * This function verifies the HMAC at offset @ofs_hmac of the node given in
  378. * @node. Returns 0 if successful or a negative error code otherwise.
  379. */
  380. int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *node,
  381. int len, int ofs_hmac)
  382. {
  383. int hmac_len = c->hmac_desc_len;
  384. u8 *hmac;
  385. int err;
  386. hmac = kmalloc(hmac_len, GFP_NOFS);
  387. if (!hmac)
  388. return -ENOMEM;
  389. err = ubifs_node_calc_hmac(c, node, len, ofs_hmac, hmac);
  390. if (err) {
  391. kfree(hmac);
  392. return err;
  393. }
  394. err = crypto_memneq(hmac, node + ofs_hmac, hmac_len);
  395. kfree(hmac);
  396. if (!err)
  397. return 0;
  398. return -EPERM;
  399. }
  400. int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src,
  401. struct shash_desc *target)
  402. {
  403. u8 *state;
  404. int err;
  405. state = kmalloc(crypto_shash_descsize(src->tfm), GFP_NOFS);
  406. if (!state)
  407. return -ENOMEM;
  408. err = crypto_shash_export(src, state);
  409. if (err)
  410. goto out;
  411. err = crypto_shash_import(target, state);
  412. out:
  413. kfree(state);
  414. return err;
  415. }
  416. /**
  417. * ubifs_hmac_wkm - Create a HMAC of the well known message
  418. * @c: UBIFS file-system description object
  419. * @hmac: The HMAC of the well known message
  420. *
  421. * This function creates a HMAC of a well known message. This is used
  422. * to check if the provided key is suitable to authenticate a UBIFS
  423. * image. This is only a convenience to the user to provide a better
  424. * error message when the wrong key is provided.
  425. *
  426. * This function returns 0 for success or a negative error code otherwise.
  427. */
  428. int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac)
  429. {
  430. SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
  431. int err;
  432. const char well_known_message[] = "UBIFS";
  433. if (!ubifs_authenticated(c))
  434. return 0;
  435. shash->tfm = c->hmac_tfm;
  436. err = crypto_shash_init(shash);
  437. if (err)
  438. return err;
  439. err = crypto_shash_update(shash, well_known_message,
  440. sizeof(well_known_message) - 1);
  441. if (err < 0)
  442. return err;
  443. err = crypto_shash_final(shash, hmac);
  444. if (err)
  445. return err;
  446. return 0;
  447. }
  448. /*
  449. * ubifs_hmac_zero - test if a HMAC is zero
  450. * @c: UBIFS file-system description object
  451. * @hmac: the HMAC to test
  452. *
  453. * This function tests if a HMAC is zero and returns true if it is
  454. * and false otherwise.
  455. */
  456. bool ubifs_hmac_zero(struct ubifs_info *c, const u8 *hmac)
  457. {
  458. return !memchr_inv(hmac, 0, c->hmac_desc_len);
  459. }