fsverity.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fs-verity: read-only file-based authenticity protection
  4. *
  5. * This header declares the interface between the fs/verity/ support layer and
  6. * filesystems that support fs-verity.
  7. *
  8. * Copyright 2019 Google LLC
  9. */
  10. #ifndef _LINUX_FSVERITY_H
  11. #define _LINUX_FSVERITY_H
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <crypto/hash_info.h>
  15. #include <crypto/sha2.h>
  16. #include <uapi/linux/fsverity.h>
  17. /*
  18. * Largest digest size among all hash algorithms supported by fs-verity.
  19. * Currently assumed to be <= size of fsverity_descriptor::root_hash.
  20. */
  21. #define FS_VERITY_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
  22. /* Arbitrary limit to bound the kmalloc() size. Can be changed. */
  23. #define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
  24. /* Verity operations for filesystems */
  25. struct fsverity_operations {
  26. /**
  27. * Begin enabling verity on the given file.
  28. *
  29. * @filp: a readonly file descriptor for the file
  30. *
  31. * The filesystem must do any needed filesystem-specific preparations
  32. * for enabling verity, e.g. evicting inline data. It also must return
  33. * -EBUSY if verity is already being enabled on the given file.
  34. *
  35. * i_rwsem is held for write.
  36. *
  37. * Return: 0 on success, -errno on failure
  38. */
  39. int (*begin_enable_verity)(struct file *filp);
  40. /**
  41. * End enabling verity on the given file.
  42. *
  43. * @filp: a readonly file descriptor for the file
  44. * @desc: the verity descriptor to write, or NULL on failure
  45. * @desc_size: size of verity descriptor, or 0 on failure
  46. * @merkle_tree_size: total bytes the Merkle tree took up
  47. *
  48. * If desc == NULL, then enabling verity failed and the filesystem only
  49. * must do any necessary cleanups. Else, it must also store the given
  50. * verity descriptor to a fs-specific location associated with the inode
  51. * and do any fs-specific actions needed to mark the inode as a verity
  52. * inode, e.g. setting a bit in the on-disk inode. The filesystem is
  53. * also responsible for setting the S_VERITY flag in the VFS inode.
  54. *
  55. * i_rwsem is held for write, but it may have been dropped between
  56. * ->begin_enable_verity() and ->end_enable_verity().
  57. *
  58. * Return: 0 on success, -errno on failure
  59. */
  60. int (*end_enable_verity)(struct file *filp, const void *desc,
  61. size_t desc_size, u64 merkle_tree_size);
  62. /**
  63. * Get the verity descriptor of the given inode.
  64. *
  65. * @inode: an inode with the S_VERITY flag set
  66. * @buf: buffer in which to place the verity descriptor
  67. * @bufsize: size of @buf, or 0 to retrieve the size only
  68. *
  69. * If bufsize == 0, then the size of the verity descriptor is returned.
  70. * Otherwise the verity descriptor is written to 'buf' and its actual
  71. * size is returned; -ERANGE is returned if it's too large. This may be
  72. * called by multiple processes concurrently on the same inode.
  73. *
  74. * Return: the size on success, -errno on failure
  75. */
  76. int (*get_verity_descriptor)(struct inode *inode, void *buf,
  77. size_t bufsize);
  78. /**
  79. * Read a Merkle tree page of the given inode.
  80. *
  81. * @inode: the inode
  82. * @index: 0-based index of the page within the Merkle tree
  83. * @num_ra_pages: The number of Merkle tree pages that should be
  84. * prefetched starting at @index if the page at @index
  85. * isn't already cached. Implementations may ignore this
  86. * argument; it's only a performance optimization.
  87. *
  88. * This can be called at any time on an open verity file. It may be
  89. * called by multiple processes concurrently, even with the same page.
  90. *
  91. * Note that this must retrieve a *page*, not necessarily a *block*.
  92. *
  93. * Return: the page on success, ERR_PTR() on failure
  94. */
  95. struct page *(*read_merkle_tree_page)(struct inode *inode,
  96. pgoff_t index,
  97. unsigned long num_ra_pages);
  98. /**
  99. * Write a Merkle tree block to the given inode.
  100. *
  101. * @inode: the inode for which the Merkle tree is being built
  102. * @buf: the Merkle tree block to write
  103. * @pos: the position of the block in the Merkle tree (in bytes)
  104. * @size: the Merkle tree block size (in bytes)
  105. *
  106. * This is only called between ->begin_enable_verity() and
  107. * ->end_enable_verity().
  108. *
  109. * Return: 0 on success, -errno on failure
  110. */
  111. int (*write_merkle_tree_block)(struct inode *inode, const void *buf,
  112. u64 pos, unsigned int size);
  113. };
  114. #ifdef CONFIG_FS_VERITY
  115. static inline struct fsverity_info *fsverity_get_info(const struct inode *inode)
  116. {
  117. /*
  118. * Pairs with the cmpxchg_release() in fsverity_set_info().
  119. * I.e., another task may publish ->i_verity_info concurrently,
  120. * executing a RELEASE barrier. We need to use smp_load_acquire() here
  121. * to safely ACQUIRE the memory the other task published.
  122. */
  123. return smp_load_acquire(&inode->i_verity_info);
  124. }
  125. /* enable.c */
  126. int fsverity_ioctl_enable(struct file *filp, const void __user *arg);
  127. /* measure.c */
  128. int fsverity_ioctl_measure(struct file *filp, void __user *arg);
  129. int fsverity_get_digest(struct inode *inode,
  130. u8 digest[FS_VERITY_MAX_DIGEST_SIZE],
  131. enum hash_algo *alg);
  132. /* open.c */
  133. int __fsverity_file_open(struct inode *inode, struct file *filp);
  134. int __fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr);
  135. void __fsverity_cleanup_inode(struct inode *inode);
  136. /**
  137. * fsverity_cleanup_inode() - free the inode's verity info, if present
  138. * @inode: an inode being evicted
  139. *
  140. * Filesystems must call this on inode eviction to free ->i_verity_info.
  141. */
  142. static inline void fsverity_cleanup_inode(struct inode *inode)
  143. {
  144. if (inode->i_verity_info)
  145. __fsverity_cleanup_inode(inode);
  146. }
  147. /* read_metadata.c */
  148. int fsverity_ioctl_read_metadata(struct file *filp, const void __user *uarg);
  149. /* verify.c */
  150. bool fsverity_verify_blocks(struct folio *folio, size_t len, size_t offset);
  151. void fsverity_verify_bio(struct bio *bio);
  152. void fsverity_enqueue_verify_work(struct work_struct *work);
  153. #else /* !CONFIG_FS_VERITY */
  154. static inline struct fsverity_info *fsverity_get_info(const struct inode *inode)
  155. {
  156. return NULL;
  157. }
  158. /* enable.c */
  159. static inline int fsverity_ioctl_enable(struct file *filp,
  160. const void __user *arg)
  161. {
  162. return -EOPNOTSUPP;
  163. }
  164. /* measure.c */
  165. static inline int fsverity_ioctl_measure(struct file *filp, void __user *arg)
  166. {
  167. return -EOPNOTSUPP;
  168. }
  169. static inline int fsverity_get_digest(struct inode *inode,
  170. u8 digest[FS_VERITY_MAX_DIGEST_SIZE],
  171. enum hash_algo *alg)
  172. {
  173. return -EOPNOTSUPP;
  174. }
  175. /* open.c */
  176. static inline int __fsverity_file_open(struct inode *inode, struct file *filp)
  177. {
  178. return -EOPNOTSUPP;
  179. }
  180. static inline int __fsverity_prepare_setattr(struct dentry *dentry,
  181. struct iattr *attr)
  182. {
  183. return -EOPNOTSUPP;
  184. }
  185. static inline void fsverity_cleanup_inode(struct inode *inode)
  186. {
  187. }
  188. /* read_metadata.c */
  189. static inline int fsverity_ioctl_read_metadata(struct file *filp,
  190. const void __user *uarg)
  191. {
  192. return -EOPNOTSUPP;
  193. }
  194. /* verify.c */
  195. static inline bool fsverity_verify_blocks(struct folio *folio, size_t len,
  196. size_t offset)
  197. {
  198. WARN_ON(1);
  199. return false;
  200. }
  201. static inline void fsverity_verify_bio(struct bio *bio)
  202. {
  203. WARN_ON(1);
  204. }
  205. static inline void fsverity_enqueue_verify_work(struct work_struct *work)
  206. {
  207. WARN_ON(1);
  208. }
  209. #endif /* !CONFIG_FS_VERITY */
  210. static inline bool fsverity_verify_folio(struct folio *folio)
  211. {
  212. return fsverity_verify_blocks(folio, folio_size(folio), 0);
  213. }
  214. static inline bool fsverity_verify_page(struct page *page)
  215. {
  216. return fsverity_verify_blocks(page_folio(page), PAGE_SIZE, 0);
  217. }
  218. /**
  219. * fsverity_active() - do reads from the inode need to go through fs-verity?
  220. * @inode: inode to check
  221. *
  222. * This checks whether ->i_verity_info has been set.
  223. *
  224. * Filesystems call this from ->readahead() to check whether the pages need to
  225. * be verified or not. Don't use IS_VERITY() for this purpose; it's subject to
  226. * a race condition where the file is being read concurrently with
  227. * FS_IOC_ENABLE_VERITY completing. (S_VERITY is set before ->i_verity_info.)
  228. *
  229. * Return: true if reads need to go through fs-verity, otherwise false
  230. */
  231. static inline bool fsverity_active(const struct inode *inode)
  232. {
  233. return fsverity_get_info(inode) != NULL;
  234. }
  235. #ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
  236. int __fsverity_verify_signature(const struct inode *inode, const u8 *signature,
  237. size_t sig_size, const u8 *file_digest,
  238. unsigned int digest_algorithm);
  239. #else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
  240. static inline int __fsverity_verify_signature(const struct inode *inode,
  241. const u8 *signature, size_t sig_size,
  242. const u8 *file_digest,
  243. unsigned int digest_algorithm)
  244. {
  245. return 0;
  246. }
  247. #endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
  248. /**
  249. * fsverity_file_open() - prepare to open a verity file
  250. * @inode: the inode being opened
  251. * @filp: the struct file being set up
  252. *
  253. * When opening a verity file, deny the open if it is for writing. Otherwise,
  254. * set up the inode's ->i_verity_info if not already done.
  255. *
  256. * When combined with fscrypt, this must be called after fscrypt_file_open().
  257. * Otherwise, we won't have the key set up to decrypt the verity metadata.
  258. *
  259. * Return: 0 on success, -errno on failure
  260. */
  261. static inline int fsverity_file_open(struct inode *inode, struct file *filp)
  262. {
  263. if (IS_VERITY(inode))
  264. return __fsverity_file_open(inode, filp);
  265. return 0;
  266. }
  267. /**
  268. * fsverity_prepare_setattr() - prepare to change a verity inode's attributes
  269. * @dentry: dentry through which the inode is being changed
  270. * @attr: attributes to change
  271. *
  272. * Verity files are immutable, so deny truncates. This isn't covered by the
  273. * open-time check because sys_truncate() takes a path, not a file descriptor.
  274. *
  275. * Return: 0 on success, -errno on failure
  276. */
  277. static inline int fsverity_prepare_setattr(struct dentry *dentry,
  278. struct iattr *attr)
  279. {
  280. if (IS_VERITY(d_inode(dentry)))
  281. return __fsverity_prepare_setattr(dentry, attr);
  282. return 0;
  283. }
  284. #endif /* _LINUX_FSVERITY_H */