verity.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 Google LLC
  4. */
  5. /*
  6. * fs-verity integration into incfs
  7. *
  8. * Since incfs has its own merkle tree implementation, most of fs-verity code
  9. * is not needed. The key part that is needed is the signature check, since
  10. * that is based on the private /proc/sys/fs/verity/require_signatures value
  11. * and a private keyring. Thus the first change is to modify verity code to
  12. * export a version of fsverity_verify_signature.
  13. *
  14. * fs-verity integration then consists of the following modifications:
  15. *
  16. * 1. Add the (optional) verity signature to the incfs file format
  17. * 2. Add a pointer to the digest of the fs-verity descriptor struct to the
  18. * data_file struct that incfs attaches to each file inode.
  19. * 3. Add the following ioclts:
  20. * - FS_IOC_ENABLE_VERITY
  21. * - FS_IOC_GETFLAGS
  22. * - FS_IOC_MEASURE_VERITY
  23. * 4. When FS_IOC_ENABLE_VERITY is called on a non-verity file, the
  24. * fs-verity descriptor struct is populated and digested. If it passes the
  25. * signature check or the signature is NULL and
  26. * fs.verity.require_signatures=0, then the S_VERITY flag is set and the
  27. * xattr incfs.verity is set. If the signature is non-NULL, an
  28. * INCFS_MD_VERITY_SIGNATURE is added to the backing file containing the
  29. * signature.
  30. * 5. When a file with an incfs.verity xattr's inode is initialized, the
  31. * inode’s S_VERITY flag is set.
  32. * 6. When a file with the S_VERITY flag set on its inode is opened, the
  33. * data_file is checked for its verity digest. If the file doesn’t have a
  34. * digest, the file’s digest is calculated as above, checked, and set, or the
  35. * open is denied if it is not valid.
  36. * 7. FS_IOC_GETFLAGS simply returns the value of the S_VERITY flag
  37. * 8. FS_IOC_MEASURE_VERITY simply returns the cached digest
  38. * 9. The final complication is that if FS_IOC_ENABLE_VERITY is called on a file
  39. * which doesn’t have a merkle tree, the merkle tree is calculated before the
  40. * rest of the process is completed.
  41. */
  42. #include <crypto/hash.h>
  43. #include <crypto/sha.h>
  44. #include <linux/fsverity.h>
  45. #include <linux/mount.h>
  46. #include "verity.h"
  47. #include "data_mgmt.h"
  48. #include "format.h"
  49. #include "integrity.h"
  50. #include "vfs.h"
  51. #define FS_VERITY_MAX_SIGNATURE_SIZE 16128
  52. static int incfs_get_root_hash(struct file *filp, u8 *root_hash)
  53. {
  54. struct data_file *df = get_incfs_data_file(filp);
  55. if (!df)
  56. return -EINVAL;
  57. memcpy(root_hash, df->df_hash_tree->root_hash,
  58. df->df_hash_tree->alg->digest_size);
  59. return 0;
  60. }
  61. static int incfs_end_enable_verity(struct file *filp, u8 *sig, size_t sig_size)
  62. {
  63. struct inode *inode = file_inode(filp);
  64. struct mem_range signature = {
  65. .data = sig,
  66. .len = sig_size,
  67. };
  68. struct data_file *df = get_incfs_data_file(filp);
  69. struct backing_file_context *bfc;
  70. int error;
  71. struct incfs_df_verity_signature *vs = NULL;
  72. loff_t offset;
  73. if (!df || !df->df_backing_file_context)
  74. return -EFSCORRUPTED;
  75. if (sig) {
  76. vs = kzalloc(sizeof(*vs), GFP_NOFS);
  77. if (!vs)
  78. return -ENOMEM;
  79. }
  80. bfc = df->df_backing_file_context;
  81. error = mutex_lock_interruptible(&bfc->bc_mutex);
  82. if (error)
  83. goto out;
  84. error = incfs_write_verity_signature_to_backing_file(bfc, signature,
  85. &offset);
  86. mutex_unlock(&bfc->bc_mutex);
  87. if (error)
  88. goto out;
  89. /*
  90. * Set verity xattr so we can set S_VERITY without opening backing file
  91. */
  92. error = vfs_setxattr(bfc->bc_file->f_path.dentry,
  93. INCFS_XATTR_VERITY_NAME, NULL, 0, XATTR_CREATE);
  94. if (error) {
  95. pr_warn("incfs: error setting verity xattr: %d\n", error);
  96. goto out;
  97. }
  98. if (sig) {
  99. *vs = (struct incfs_df_verity_signature) {
  100. .size = signature.len,
  101. .offset = offset,
  102. };
  103. df->df_verity_signature = vs;
  104. vs = NULL;
  105. }
  106. inode_set_flags(inode, S_VERITY, S_VERITY);
  107. out:
  108. kfree(vs);
  109. return error;
  110. }
  111. static int incfs_compute_file_digest(struct incfs_hash_alg *alg,
  112. struct fsverity_descriptor *desc,
  113. u8 *digest)
  114. {
  115. SHASH_DESC_ON_STACK(d, alg->shash);
  116. d->tfm = alg->shash;
  117. return crypto_shash_digest(d, (u8 *)desc, sizeof(*desc), digest);
  118. }
  119. static enum incfs_hash_tree_algorithm incfs_convert_fsverity_hash_alg(
  120. int hash_alg)
  121. {
  122. switch (hash_alg) {
  123. case FS_VERITY_HASH_ALG_SHA256:
  124. return INCFS_HASH_TREE_SHA256;
  125. default:
  126. return -EINVAL;
  127. }
  128. }
  129. static struct mem_range incfs_get_verity_digest(struct inode *inode)
  130. {
  131. struct inode_info *node = get_incfs_node(inode);
  132. struct data_file *df;
  133. struct mem_range verity_file_digest;
  134. if (!node) {
  135. pr_warn("Invalid inode\n");
  136. return range(NULL, 0);
  137. }
  138. df = node->n_file;
  139. /*
  140. * Pairs with the cmpxchg_release() in incfs_set_verity_digest().
  141. * I.e., another task may publish ->df_verity_file_digest concurrently,
  142. * executing a RELEASE barrier. We need to use smp_load_acquire() here
  143. * to safely ACQUIRE the memory the other task published.
  144. */
  145. verity_file_digest.data = smp_load_acquire(
  146. &df->df_verity_file_digest.data);
  147. verity_file_digest.len = df->df_verity_file_digest.len;
  148. return verity_file_digest;
  149. }
  150. static void incfs_set_verity_digest(struct inode *inode,
  151. struct mem_range verity_file_digest)
  152. {
  153. struct inode_info *node = get_incfs_node(inode);
  154. struct data_file *df;
  155. if (!node) {
  156. pr_warn("Invalid inode\n");
  157. kfree(verity_file_digest.data);
  158. return;
  159. }
  160. df = node->n_file;
  161. df->df_verity_file_digest.len = verity_file_digest.len;
  162. /*
  163. * Multiple tasks may race to set ->df_verity_file_digest.data, so use
  164. * cmpxchg_release(). This pairs with the smp_load_acquire() in
  165. * incfs_get_verity_digest(). I.e., here we publish
  166. * ->df_verity_file_digest.data, with a RELEASE barrier so that other
  167. * tasks can ACQUIRE it.
  168. */
  169. if (cmpxchg_release(&df->df_verity_file_digest.data, NULL,
  170. verity_file_digest.data) != NULL)
  171. /* Lost the race, so free the file_digest we allocated. */
  172. kfree(verity_file_digest.data);
  173. }
  174. /*
  175. * Calculate the digest of the fsverity_descriptor. The signature (if present)
  176. * is also checked.
  177. */
  178. static struct mem_range incfs_calc_verity_digest_from_desc(
  179. const struct inode *inode,
  180. struct fsverity_descriptor *desc,
  181. u8 *signature, size_t sig_size)
  182. {
  183. enum incfs_hash_tree_algorithm incfs_hash_alg;
  184. struct mem_range verity_file_digest;
  185. int err;
  186. struct incfs_hash_alg *hash_alg;
  187. incfs_hash_alg = incfs_convert_fsverity_hash_alg(desc->hash_algorithm);
  188. if (incfs_hash_alg < 0)
  189. return range(ERR_PTR(incfs_hash_alg), 0);
  190. hash_alg = incfs_get_hash_alg(incfs_hash_alg);
  191. if (IS_ERR(hash_alg))
  192. return range((u8 *)hash_alg, 0);
  193. verity_file_digest = range(kzalloc(hash_alg->digest_size, GFP_KERNEL),
  194. hash_alg->digest_size);
  195. if (!verity_file_digest.data)
  196. return range(ERR_PTR(-ENOMEM), 0);
  197. err = incfs_compute_file_digest(hash_alg, desc,
  198. verity_file_digest.data);
  199. if (err) {
  200. pr_err("Error %d computing file digest", err);
  201. goto out;
  202. }
  203. pr_debug("Computed file digest: %s:%*phN\n",
  204. hash_alg->name, (int) verity_file_digest.len,
  205. verity_file_digest.data);
  206. err = __fsverity_verify_signature(inode, signature, sig_size,
  207. verity_file_digest.data,
  208. desc->hash_algorithm);
  209. out:
  210. if (err) {
  211. kfree(verity_file_digest.data);
  212. verity_file_digest = range(ERR_PTR(err), 0);
  213. }
  214. return verity_file_digest;
  215. }
  216. static struct fsverity_descriptor *incfs_get_fsverity_descriptor(
  217. struct file *filp, int hash_algorithm)
  218. {
  219. struct inode *inode = file_inode(filp);
  220. struct fsverity_descriptor *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  221. int err;
  222. if (!desc)
  223. return ERR_PTR(-ENOMEM);
  224. *desc = (struct fsverity_descriptor) {
  225. .version = 1,
  226. .hash_algorithm = hash_algorithm,
  227. .log_blocksize = ilog2(INCFS_DATA_FILE_BLOCK_SIZE),
  228. .data_size = cpu_to_le64(inode->i_size),
  229. };
  230. err = incfs_get_root_hash(filp, desc->root_hash);
  231. if (err) {
  232. kfree(desc);
  233. return ERR_PTR(err);
  234. }
  235. return desc;
  236. }
  237. static struct mem_range incfs_calc_verity_digest(
  238. struct inode *inode, struct file *filp,
  239. u8 *signature, size_t signature_size,
  240. int hash_algorithm)
  241. {
  242. struct fsverity_descriptor *desc = incfs_get_fsverity_descriptor(filp,
  243. hash_algorithm);
  244. struct mem_range verity_file_digest;
  245. if (IS_ERR(desc))
  246. return range((u8 *)desc, 0);
  247. verity_file_digest = incfs_calc_verity_digest_from_desc(inode, desc,
  248. signature, signature_size);
  249. kfree(desc);
  250. return verity_file_digest;
  251. }
  252. static int incfs_build_merkle_tree(struct file *f, struct data_file *df,
  253. struct backing_file_context *bfc,
  254. struct mtree *hash_tree, loff_t hash_offset,
  255. struct incfs_hash_alg *alg, struct mem_range hash)
  256. {
  257. int error = 0;
  258. int limit, lvl, i, result;
  259. struct mem_range buf = {.len = INCFS_DATA_FILE_BLOCK_SIZE};
  260. struct mem_range tmp = {.len = 2 * INCFS_DATA_FILE_BLOCK_SIZE};
  261. buf.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(buf.len));
  262. tmp.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(tmp.len));
  263. if (!buf.data || !tmp.data) {
  264. error = -ENOMEM;
  265. goto out;
  266. }
  267. /*
  268. * lvl - 1 is the level we are reading, lvl the level we are writing
  269. * lvl == -1 means actual blocks
  270. * lvl == hash_tree->depth means root hash
  271. */
  272. limit = df->df_data_block_count;
  273. for (lvl = 0; lvl <= hash_tree->depth; lvl++) {
  274. for (i = 0; i < limit; ++i) {
  275. loff_t hash_level_offset;
  276. struct mem_range partial_buf = buf;
  277. if (lvl == 0)
  278. result = incfs_read_data_file_block(partial_buf,
  279. f, i, tmp, NULL);
  280. else {
  281. hash_level_offset = hash_offset +
  282. hash_tree->hash_level_suboffset[lvl - 1];
  283. result = incfs_kread(bfc, partial_buf.data,
  284. partial_buf.len,
  285. hash_level_offset + i *
  286. INCFS_DATA_FILE_BLOCK_SIZE);
  287. }
  288. if (result < 0) {
  289. error = result;
  290. goto out;
  291. }
  292. partial_buf.len = result;
  293. error = incfs_calc_digest(alg, partial_buf, hash);
  294. if (error)
  295. goto out;
  296. /*
  297. * last level - only one hash to take and it is stored
  298. * in the incfs signature record
  299. */
  300. if (lvl == hash_tree->depth)
  301. break;
  302. hash_level_offset = hash_offset +
  303. hash_tree->hash_level_suboffset[lvl];
  304. result = incfs_kwrite(bfc, hash.data, hash.len,
  305. hash_level_offset + hash.len * i);
  306. if (result < 0) {
  307. error = result;
  308. goto out;
  309. }
  310. if (result != hash.len) {
  311. error = -EIO;
  312. goto out;
  313. }
  314. }
  315. limit = DIV_ROUND_UP(limit,
  316. INCFS_DATA_FILE_BLOCK_SIZE / hash.len);
  317. }
  318. out:
  319. free_pages((unsigned long)tmp.data, get_order(tmp.len));
  320. free_pages((unsigned long)buf.data, get_order(buf.len));
  321. return error;
  322. }
  323. /*
  324. * incfs files have a signature record that is separate from the
  325. * verity_signature record. The signature record does not actually contain a
  326. * signature, rather it contains the size/offset of the hash tree, and a binary
  327. * blob which contains the root hash and potentially a signature.
  328. *
  329. * If the file was created with a signature record, then this function simply
  330. * returns.
  331. *
  332. * Otherwise it will create a signature record with a minimal binary blob as
  333. * defined by the structure below, create space for the hash tree and then
  334. * populate it using incfs_build_merkle_tree
  335. */
  336. static int incfs_add_signature_record(struct file *f)
  337. {
  338. /* See incfs_parse_signature */
  339. struct {
  340. __le32 version;
  341. __le32 size_of_hash_info_section;
  342. struct {
  343. __le32 hash_algorithm;
  344. u8 log2_blocksize;
  345. __le32 salt_size;
  346. u8 salt[0];
  347. __le32 hash_size;
  348. u8 root_hash[32];
  349. } __packed hash_section;
  350. __le32 size_of_signing_info_section;
  351. u8 signing_info_section[0];
  352. } __packed sig = {
  353. .version = cpu_to_le32(INCFS_SIGNATURE_VERSION),
  354. .size_of_hash_info_section =
  355. cpu_to_le32(sizeof(sig.hash_section)),
  356. .hash_section = {
  357. .hash_algorithm = cpu_to_le32(INCFS_HASH_TREE_SHA256),
  358. .log2_blocksize = ilog2(INCFS_DATA_FILE_BLOCK_SIZE),
  359. .hash_size = cpu_to_le32(SHA256_DIGEST_SIZE),
  360. },
  361. };
  362. struct data_file *df = get_incfs_data_file(f);
  363. struct mtree *hash_tree = NULL;
  364. struct backing_file_context *bfc;
  365. int error;
  366. loff_t hash_offset, sig_offset;
  367. struct incfs_hash_alg *alg = incfs_get_hash_alg(INCFS_HASH_TREE_SHA256);
  368. u8 hash_buf[INCFS_MAX_HASH_SIZE];
  369. int hash_size = alg->digest_size;
  370. struct mem_range hash = range(hash_buf, hash_size);
  371. int result;
  372. struct incfs_df_signature *signature = NULL;
  373. if (!df)
  374. return -EINVAL;
  375. if (df->df_header_flags & INCFS_FILE_MAPPED)
  376. return -EINVAL;
  377. /* Already signed? */
  378. if (df->df_signature && df->df_hash_tree)
  379. return 0;
  380. if (df->df_signature || df->df_hash_tree)
  381. return -EFSCORRUPTED;
  382. /* Add signature metadata record to file */
  383. hash_tree = incfs_alloc_mtree(range((u8 *)&sig, sizeof(sig)),
  384. df->df_data_block_count);
  385. if (IS_ERR(hash_tree))
  386. return PTR_ERR(hash_tree);
  387. bfc = df->df_backing_file_context;
  388. if (!bfc) {
  389. error = -EFSCORRUPTED;
  390. goto out;
  391. }
  392. error = mutex_lock_interruptible(&bfc->bc_mutex);
  393. if (error)
  394. goto out;
  395. error = incfs_write_signature_to_backing_file(bfc,
  396. range((u8 *)&sig, sizeof(sig)),
  397. hash_tree->hash_tree_area_size,
  398. &hash_offset, &sig_offset);
  399. mutex_unlock(&bfc->bc_mutex);
  400. if (error)
  401. goto out;
  402. /* Populate merkle tree */
  403. error = incfs_build_merkle_tree(f, df, bfc, hash_tree, hash_offset, alg,
  404. hash);
  405. if (error)
  406. goto out;
  407. /* Update signature metadata record */
  408. memcpy(sig.hash_section.root_hash, hash.data, alg->digest_size);
  409. result = incfs_kwrite(bfc, &sig, sizeof(sig), sig_offset);
  410. if (result < 0) {
  411. error = result;
  412. goto out;
  413. }
  414. if (result != sizeof(sig)) {
  415. error = -EIO;
  416. goto out;
  417. }
  418. /* Update in-memory records */
  419. memcpy(hash_tree->root_hash, hash.data, alg->digest_size);
  420. signature = kzalloc(sizeof(*signature), GFP_NOFS);
  421. if (!signature) {
  422. error = -ENOMEM;
  423. goto out;
  424. }
  425. *signature = (struct incfs_df_signature) {
  426. .hash_offset = hash_offset,
  427. .hash_size = hash_tree->hash_tree_area_size,
  428. .sig_offset = sig_offset,
  429. .sig_size = sizeof(sig),
  430. };
  431. df->df_signature = signature;
  432. signature = NULL;
  433. /*
  434. * Use memory barrier to prevent readpage seeing the hash tree until
  435. * it's fully there
  436. */
  437. smp_store_release(&df->df_hash_tree, hash_tree);
  438. hash_tree = NULL;
  439. out:
  440. kfree(signature);
  441. kfree(hash_tree);
  442. return error;
  443. }
  444. static int incfs_enable_verity(struct file *filp,
  445. const struct fsverity_enable_arg *arg)
  446. {
  447. struct inode *inode = file_inode(filp);
  448. struct data_file *df = get_incfs_data_file(filp);
  449. u8 *signature = NULL;
  450. struct mem_range verity_file_digest = range(NULL, 0);
  451. int err;
  452. if (!df)
  453. return -EFSCORRUPTED;
  454. err = mutex_lock_interruptible(&df->df_enable_verity);
  455. if (err)
  456. return err;
  457. if (IS_VERITY(inode)) {
  458. err = -EEXIST;
  459. goto out;
  460. }
  461. err = incfs_add_signature_record(filp);
  462. if (err)
  463. goto out;
  464. /* Get the signature if the user provided one */
  465. if (arg->sig_size) {
  466. signature = memdup_user(u64_to_user_ptr(arg->sig_ptr),
  467. arg->sig_size);
  468. if (IS_ERR(signature)) {
  469. err = PTR_ERR(signature);
  470. signature = NULL;
  471. goto out;
  472. }
  473. }
  474. verity_file_digest = incfs_calc_verity_digest(inode, filp, signature,
  475. arg->sig_size, arg->hash_algorithm);
  476. if (IS_ERR(verity_file_digest.data)) {
  477. err = PTR_ERR(verity_file_digest.data);
  478. verity_file_digest.data = NULL;
  479. goto out;
  480. }
  481. err = incfs_end_enable_verity(filp, signature, arg->sig_size);
  482. if (err)
  483. goto out;
  484. /* Successfully enabled verity */
  485. incfs_set_verity_digest(inode, verity_file_digest);
  486. verity_file_digest.data = NULL;
  487. out:
  488. mutex_unlock(&df->df_enable_verity);
  489. kfree(signature);
  490. kfree(verity_file_digest.data);
  491. if (err)
  492. pr_err("%s failed with err %d\n", __func__, err);
  493. return err;
  494. }
  495. int incfs_ioctl_enable_verity(struct file *filp, const void __user *uarg)
  496. {
  497. struct inode *inode = file_inode(filp);
  498. struct fsverity_enable_arg arg;
  499. if (copy_from_user(&arg, uarg, sizeof(arg)))
  500. return -EFAULT;
  501. if (arg.version != 1)
  502. return -EINVAL;
  503. if (arg.__reserved1 ||
  504. memchr_inv(arg.__reserved2, 0, sizeof(arg.__reserved2)))
  505. return -EINVAL;
  506. if (arg.hash_algorithm != FS_VERITY_HASH_ALG_SHA256)
  507. return -EINVAL;
  508. if (arg.block_size != PAGE_SIZE)
  509. return -EINVAL;
  510. if (arg.salt_size)
  511. return -EINVAL;
  512. if (arg.sig_size > FS_VERITY_MAX_SIGNATURE_SIZE)
  513. return -EMSGSIZE;
  514. if (S_ISDIR(inode->i_mode))
  515. return -EISDIR;
  516. if (!S_ISREG(inode->i_mode))
  517. return -EINVAL;
  518. return incfs_enable_verity(filp, &arg);
  519. }
  520. static u8 *incfs_get_verity_signature(struct file *filp, size_t *sig_size)
  521. {
  522. struct data_file *df = get_incfs_data_file(filp);
  523. struct incfs_df_verity_signature *vs;
  524. u8 *signature;
  525. int res;
  526. if (!df || !df->df_backing_file_context)
  527. return ERR_PTR(-EFSCORRUPTED);
  528. vs = df->df_verity_signature;
  529. if (!vs) {
  530. *sig_size = 0;
  531. return NULL;
  532. }
  533. if (!vs->size) {
  534. *sig_size = 0;
  535. return ERR_PTR(-EFSCORRUPTED);
  536. }
  537. signature = kzalloc(vs->size, GFP_KERNEL);
  538. if (!signature)
  539. return ERR_PTR(-ENOMEM);
  540. res = incfs_kread(df->df_backing_file_context,
  541. signature, vs->size, vs->offset);
  542. if (res < 0)
  543. goto err_out;
  544. if (res != vs->size) {
  545. res = -EINVAL;
  546. goto err_out;
  547. }
  548. *sig_size = vs->size;
  549. return signature;
  550. err_out:
  551. kfree(signature);
  552. return ERR_PTR(res);
  553. }
  554. /* Ensure data_file->df_verity_file_digest is populated */
  555. static int ensure_verity_info(struct inode *inode, struct file *filp)
  556. {
  557. struct mem_range verity_file_digest;
  558. u8 *signature = NULL;
  559. size_t sig_size;
  560. int err = 0;
  561. /* See if this file's verity file digest is already cached */
  562. verity_file_digest = incfs_get_verity_digest(inode);
  563. if (verity_file_digest.data)
  564. return 0;
  565. signature = incfs_get_verity_signature(filp, &sig_size);
  566. if (IS_ERR(signature))
  567. return PTR_ERR(signature);
  568. verity_file_digest = incfs_calc_verity_digest(inode, filp, signature,
  569. sig_size,
  570. FS_VERITY_HASH_ALG_SHA256);
  571. if (IS_ERR(verity_file_digest.data)) {
  572. err = PTR_ERR(verity_file_digest.data);
  573. goto out;
  574. }
  575. incfs_set_verity_digest(inode, verity_file_digest);
  576. out:
  577. kfree(signature);
  578. return err;
  579. }
  580. /**
  581. * incfs_fsverity_file_open() - prepare to open a file that may be
  582. * verity-enabled
  583. * @inode: the inode being opened
  584. * @filp: the struct file being set up
  585. *
  586. * When opening a verity file, set up data_file->df_verity_file_digest if not
  587. * already done. Note that incfs does not allow opening for writing, so there is
  588. * no need for that check.
  589. *
  590. * Return: 0 on success, -errno on failure
  591. */
  592. int incfs_fsverity_file_open(struct inode *inode, struct file *filp)
  593. {
  594. if (IS_VERITY(inode))
  595. return ensure_verity_info(inode, filp);
  596. return 0;
  597. }
  598. int incfs_ioctl_measure_verity(struct file *filp, void __user *_uarg)
  599. {
  600. struct inode *inode = file_inode(filp);
  601. struct mem_range verity_file_digest = incfs_get_verity_digest(inode);
  602. struct fsverity_digest __user *uarg = _uarg;
  603. struct fsverity_digest arg;
  604. if (!verity_file_digest.data || !verity_file_digest.len)
  605. return -ENODATA; /* not a verity file */
  606. /*
  607. * The user specifies the digest_size their buffer has space for; we can
  608. * return the digest if it fits in the available space. We write back
  609. * the actual size, which may be shorter than the user-specified size.
  610. */
  611. if (get_user(arg.digest_size, &uarg->digest_size))
  612. return -EFAULT;
  613. if (arg.digest_size < verity_file_digest.len)
  614. return -EOVERFLOW;
  615. memset(&arg, 0, sizeof(arg));
  616. arg.digest_algorithm = FS_VERITY_HASH_ALG_SHA256;
  617. arg.digest_size = verity_file_digest.len;
  618. if (copy_to_user(uarg, &arg, sizeof(arg)))
  619. return -EFAULT;
  620. if (copy_to_user(uarg->digest, verity_file_digest.data,
  621. verity_file_digest.len))
  622. return -EFAULT;
  623. return 0;
  624. }
  625. static int incfs_read_merkle_tree(struct file *filp, void __user *buf,
  626. u64 start_offset, int length)
  627. {
  628. struct mem_range tmp_buf;
  629. size_t offset;
  630. int retval = 0;
  631. int err = 0;
  632. struct data_file *df = get_incfs_data_file(filp);
  633. if (!df)
  634. return -EINVAL;
  635. tmp_buf = (struct mem_range) {
  636. .data = kzalloc(INCFS_DATA_FILE_BLOCK_SIZE, GFP_NOFS),
  637. .len = INCFS_DATA_FILE_BLOCK_SIZE,
  638. };
  639. if (!tmp_buf.data)
  640. return -ENOMEM;
  641. for (offset = start_offset; offset < start_offset + length;
  642. offset += tmp_buf.len) {
  643. err = incfs_read_merkle_tree_blocks(tmp_buf, df, offset);
  644. if (err < 0)
  645. break;
  646. if (err != tmp_buf.len)
  647. break;
  648. if (copy_to_user(buf, tmp_buf.data, tmp_buf.len))
  649. break;
  650. buf += tmp_buf.len;
  651. retval += tmp_buf.len;
  652. }
  653. kfree(tmp_buf.data);
  654. return retval ? retval : err;
  655. }
  656. static int incfs_read_descriptor(struct file *filp,
  657. void __user *buf, u64 offset, int length)
  658. {
  659. int err;
  660. struct fsverity_descriptor *desc = incfs_get_fsverity_descriptor(filp,
  661. FS_VERITY_HASH_ALG_SHA256);
  662. if (IS_ERR(desc))
  663. return PTR_ERR(desc);
  664. length = min_t(u64, length, sizeof(*desc));
  665. err = copy_to_user(buf, desc, length);
  666. kfree(desc);
  667. return err ? err : length;
  668. }
  669. static int incfs_read_signature(struct file *filp,
  670. void __user *buf, u64 offset, int length)
  671. {
  672. size_t sig_size;
  673. static u8 *signature;
  674. int err;
  675. signature = incfs_get_verity_signature(filp, &sig_size);
  676. if (IS_ERR(signature))
  677. return PTR_ERR(signature);
  678. if (!signature)
  679. return -ENODATA;
  680. length = min_t(u64, length, sig_size);
  681. err = copy_to_user(buf, signature, length);
  682. kfree(signature);
  683. return err ? err : length;
  684. }
  685. int incfs_ioctl_read_verity_metadata(struct file *filp,
  686. const void __user *uarg)
  687. {
  688. struct fsverity_read_metadata_arg arg;
  689. int length;
  690. void __user *buf;
  691. if (copy_from_user(&arg, uarg, sizeof(arg)))
  692. return -EFAULT;
  693. if (arg.__reserved)
  694. return -EINVAL;
  695. /* offset + length must not overflow. */
  696. if (arg.offset + arg.length < arg.offset)
  697. return -EINVAL;
  698. /* Ensure that the return value will fit in INT_MAX. */
  699. length = min_t(u64, arg.length, INT_MAX);
  700. buf = u64_to_user_ptr(arg.buf_ptr);
  701. switch (arg.metadata_type) {
  702. case FS_VERITY_METADATA_TYPE_MERKLE_TREE:
  703. return incfs_read_merkle_tree(filp, buf, arg.offset, length);
  704. case FS_VERITY_METADATA_TYPE_DESCRIPTOR:
  705. return incfs_read_descriptor(filp, buf, arg.offset, length);
  706. case FS_VERITY_METADATA_TYPE_SIGNATURE:
  707. return incfs_read_signature(filp, buf, arg.offset, length);
  708. default:
  709. return -EINVAL;
  710. }
  711. }