fscrypt.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fscrypt.h: declarations for per-file encryption
  4. *
  5. * Filesystems that implement per-file encryption include this header
  6. * file with the __FS_HAS_ENCRYPTION set according to whether that filesystem
  7. * is being built with encryption support or not.
  8. *
  9. * Copyright (C) 2015, Google, Inc.
  10. *
  11. * Written by Michael Halcrow, 2015.
  12. * Modified by Jaegeuk Kim, 2015.
  13. */
  14. #ifndef _LINUX_FSCRYPT_H
  15. #define _LINUX_FSCRYPT_H
  16. #include <linux/fs.h>
  17. #define FS_CRYPTO_BLOCK_SIZE 16
  18. struct fscrypt_ctx;
  19. struct fscrypt_info;
  20. struct fscrypt_str {
  21. unsigned char *name;
  22. u32 len;
  23. };
  24. struct fscrypt_name {
  25. const struct qstr *usr_fname;
  26. struct fscrypt_str disk_name;
  27. u32 hash;
  28. u32 minor_hash;
  29. struct fscrypt_str crypto_buf;
  30. };
  31. #define FSTR_INIT(n, l) { .name = n, .len = l }
  32. #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
  33. #define fname_name(p) ((p)->disk_name.name)
  34. #define fname_len(p) ((p)->disk_name.len)
  35. /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
  36. #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
  37. #if __FS_HAS_ENCRYPTION
  38. #include <linux/fscrypt_supp.h>
  39. #else
  40. #include <linux/fscrypt_notsupp.h>
  41. #endif
  42. /**
  43. * fscrypt_require_key - require an inode's encryption key
  44. * @inode: the inode we need the key for
  45. *
  46. * If the inode is encrypted, set up its encryption key if not already done.
  47. * Then require that the key be present and return -ENOKEY otherwise.
  48. *
  49. * No locks are needed, and the key will live as long as the struct inode --- so
  50. * it won't go away from under you.
  51. *
  52. * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
  53. * if a problem occurred while setting up the encryption key.
  54. */
  55. static inline int fscrypt_require_key(struct inode *inode)
  56. {
  57. if (IS_ENCRYPTED(inode)) {
  58. int err = fscrypt_get_encryption_info(inode);
  59. if (err)
  60. return err;
  61. if (!fscrypt_has_encryption_key(inode))
  62. return -ENOKEY;
  63. }
  64. return 0;
  65. }
  66. /**
  67. * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
  68. * @old_dentry: an existing dentry for the inode being linked
  69. * @dir: the target directory
  70. * @dentry: negative dentry for the target filename
  71. *
  72. * A new link can only be added to an encrypted directory if the directory's
  73. * encryption key is available --- since otherwise we'd have no way to encrypt
  74. * the filename. Therefore, we first set up the directory's encryption key (if
  75. * not already done) and return an error if it's unavailable.
  76. *
  77. * We also verify that the link will not violate the constraint that all files
  78. * in an encrypted directory tree use the same encryption policy.
  79. *
  80. * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
  81. * -EPERM if the link would result in an inconsistent encryption policy, or
  82. * another -errno code.
  83. */
  84. static inline int fscrypt_prepare_link(struct dentry *old_dentry,
  85. struct inode *dir,
  86. struct dentry *dentry)
  87. {
  88. if (IS_ENCRYPTED(dir))
  89. return __fscrypt_prepare_link(d_inode(old_dentry), dir);
  90. return 0;
  91. }
  92. /**
  93. * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
  94. * @old_dir: source directory
  95. * @old_dentry: dentry for source file
  96. * @new_dir: target directory
  97. * @new_dentry: dentry for target location (may be negative unless exchanging)
  98. * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
  99. *
  100. * Prepare for ->rename() where the source and/or target directories may be
  101. * encrypted. A new link can only be added to an encrypted directory if the
  102. * directory's encryption key is available --- since otherwise we'd have no way
  103. * to encrypt the filename. A rename to an existing name, on the other hand,
  104. * *is* cryptographically possible without the key. However, we take the more
  105. * conservative approach and just forbid all no-key renames.
  106. *
  107. * We also verify that the rename will not violate the constraint that all files
  108. * in an encrypted directory tree use the same encryption policy.
  109. *
  110. * Return: 0 on success, -ENOKEY if an encryption key is missing, -EPERM if the
  111. * rename would cause inconsistent encryption policies, or another -errno code.
  112. */
  113. static inline int fscrypt_prepare_rename(struct inode *old_dir,
  114. struct dentry *old_dentry,
  115. struct inode *new_dir,
  116. struct dentry *new_dentry,
  117. unsigned int flags)
  118. {
  119. if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
  120. return __fscrypt_prepare_rename(old_dir, old_dentry,
  121. new_dir, new_dentry, flags);
  122. return 0;
  123. }
  124. /**
  125. * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
  126. * @dir: directory being searched
  127. * @dentry: filename being looked up
  128. * @flags: lookup flags
  129. *
  130. * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be
  131. * done with or without the directory's encryption key; without the key,
  132. * filenames are presented in encrypted form. Therefore, we'll try to set up
  133. * the directory's encryption key, but even without it the lookup can continue.
  134. *
  135. * To allow invalidating stale dentries if the directory's encryption key is
  136. * added later, we also install a custom ->d_revalidate() method and use the
  137. * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a
  138. * plaintext name (flag set) or a ciphertext name (flag cleared).
  139. *
  140. * Return: 0 on success, -errno if a problem occurred while setting up the
  141. * encryption key
  142. */
  143. static inline int fscrypt_prepare_lookup(struct inode *dir,
  144. struct dentry *dentry,
  145. unsigned int flags)
  146. {
  147. if (IS_ENCRYPTED(dir))
  148. return __fscrypt_prepare_lookup(dir, dentry);
  149. return 0;
  150. }
  151. /**
  152. * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
  153. * @dentry: dentry through which the inode is being changed
  154. * @attr: attributes to change
  155. *
  156. * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
  157. * most attribute changes are allowed even without the encryption key. However,
  158. * without the encryption key we do have to forbid truncates. This is needed
  159. * because the size being truncated to may not be a multiple of the filesystem
  160. * block size, and in that case we'd have to decrypt the final block, zero the
  161. * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
  162. * filesystem block boundary, but it's simpler to just forbid all truncates ---
  163. * and we already forbid all other contents modifications without the key.)
  164. *
  165. * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
  166. * if a problem occurred while setting up the encryption key.
  167. */
  168. static inline int fscrypt_prepare_setattr(struct dentry *dentry,
  169. struct iattr *attr)
  170. {
  171. if (attr->ia_valid & ATTR_SIZE)
  172. return fscrypt_require_key(d_inode(dentry));
  173. return 0;
  174. }
  175. /**
  176. * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
  177. * @dir: directory in which the symlink is being created
  178. * @target: plaintext symlink target
  179. * @len: length of @target excluding null terminator
  180. * @max_len: space the filesystem has available to store the symlink target
  181. * @disk_link: (out) the on-disk symlink target being prepared
  182. *
  183. * This function computes the size the symlink target will require on-disk,
  184. * stores it in @disk_link->len, and validates it against @max_len. An
  185. * encrypted symlink may be longer than the original.
  186. *
  187. * Additionally, @disk_link->name is set to @target if the symlink will be
  188. * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
  189. * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
  190. * on-disk target later. (The reason for the two-step process is that some
  191. * filesystems need to know the size of the symlink target before creating the
  192. * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
  193. *
  194. * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
  195. * -ENOKEY if the encryption key is missing, or another -errno code if a problem
  196. * occurred while setting up the encryption key.
  197. */
  198. static inline int fscrypt_prepare_symlink(struct inode *dir,
  199. const char *target,
  200. unsigned int len,
  201. unsigned int max_len,
  202. struct fscrypt_str *disk_link)
  203. {
  204. if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
  205. return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
  206. disk_link->name = (unsigned char *)target;
  207. disk_link->len = len + 1;
  208. if (disk_link->len > max_len)
  209. return -ENAMETOOLONG;
  210. return 0;
  211. }
  212. /**
  213. * fscrypt_encrypt_symlink - encrypt the symlink target if needed
  214. * @inode: symlink inode
  215. * @target: plaintext symlink target
  216. * @len: length of @target excluding null terminator
  217. * @disk_link: (in/out) the on-disk symlink target being prepared
  218. *
  219. * If the symlink target needs to be encrypted, then this function encrypts it
  220. * into @disk_link->name. fscrypt_prepare_symlink() must have been called
  221. * previously to compute @disk_link->len. If the filesystem did not allocate a
  222. * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
  223. * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
  224. *
  225. * Return: 0 on success, -errno on failure
  226. */
  227. static inline int fscrypt_encrypt_symlink(struct inode *inode,
  228. const char *target,
  229. unsigned int len,
  230. struct fscrypt_str *disk_link)
  231. {
  232. if (IS_ENCRYPTED(inode))
  233. return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
  234. return 0;
  235. }
  236. #endif /* _LINUX_FSCRYPT_H */