fscrypt.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fscrypt.h: declarations for per-file encryption
  4. *
  5. * Filesystems that implement per-file encryption must include this header
  6. * file.
  7. *
  8. * Copyright (C) 2015, Google, Inc.
  9. *
  10. * Written by Michael Halcrow, 2015.
  11. * Modified by Jaegeuk Kim, 2015.
  12. */
  13. #ifndef _LINUX_FSCRYPT_H
  14. #define _LINUX_FSCRYPT_H
  15. #include <linux/fs.h>
  16. #include <linux/mm.h>
  17. #include <linux/slab.h>
  18. #include <uapi/linux/fscrypt.h>
  19. #include <linux/android_kabi.h>
  20. /*
  21. * The lengths of all file contents blocks must be divisible by this value.
  22. * This is needed to ensure that all contents encryption modes will work, as
  23. * some of the supported modes don't support arbitrarily byte-aligned messages.
  24. *
  25. * Since the needed alignment is 16 bytes, most filesystems will meet this
  26. * requirement naturally, as typical block sizes are powers of 2. However, if a
  27. * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via
  28. * compression), then it will need to pad to this alignment before encryption.
  29. */
  30. #define FSCRYPT_CONTENTS_ALIGNMENT 16
  31. union fscrypt_policy;
  32. struct fscrypt_info;
  33. struct fs_parameter;
  34. struct seq_file;
  35. struct fscrypt_str {
  36. unsigned char *name;
  37. u32 len;
  38. };
  39. struct fscrypt_name {
  40. const struct qstr *usr_fname;
  41. struct fscrypt_str disk_name;
  42. u32 hash;
  43. u32 minor_hash;
  44. struct fscrypt_str crypto_buf;
  45. bool is_nokey_name;
  46. };
  47. #define FSTR_INIT(n, l) { .name = n, .len = l }
  48. #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
  49. #define fname_name(p) ((p)->disk_name.name)
  50. #define fname_len(p) ((p)->disk_name.len)
  51. /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
  52. #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
  53. #ifdef CONFIG_FS_ENCRYPTION
  54. /*
  55. * If set, the fscrypt bounce page pool won't be allocated (unless another
  56. * filesystem needs it). Set this if the filesystem always uses its own bounce
  57. * pages for writes and therefore won't need the fscrypt bounce page pool.
  58. */
  59. #define FS_CFLG_OWN_PAGES (1U << 1)
  60. /*
  61. * If set, then fs/crypto/ will allow users to select a crypto data unit size
  62. * that is less than the filesystem block size. This is done via the
  63. * log2_data_unit_size field of the fscrypt policy. This flag is not compatible
  64. * with filesystems that encrypt variable-length blocks (i.e. blocks that aren't
  65. * all equal to filesystem's block size), for example as a result of
  66. * compression. It's also not compatible with the
  67. * fscrypt_encrypt_block_inplace() and fscrypt_decrypt_block_inplace()
  68. * functions.
  69. */
  70. #define FS_CFLG_SUPPORTS_SUBBLOCK_DATA_UNITS (1U << 2)
  71. /* Crypto operations for filesystems */
  72. struct fscrypt_operations {
  73. /* Set of optional flags; see above for allowed flags */
  74. unsigned int flags;
  75. /*
  76. * If set, this is a filesystem-specific key description prefix that
  77. * will be accepted for "logon" keys for v1 fscrypt policies, in
  78. * addition to the generic prefix "fscrypt:". This functionality is
  79. * deprecated, so new filesystems shouldn't set this field.
  80. */
  81. const char *key_prefix;
  82. /*
  83. * Get the fscrypt context of the given inode.
  84. *
  85. * @inode: the inode whose context to get
  86. * @ctx: the buffer into which to get the context
  87. * @len: length of the @ctx buffer in bytes
  88. *
  89. * Return: On success, returns the length of the context in bytes; this
  90. * may be less than @len. On failure, returns -ENODATA if the
  91. * inode doesn't have a context, -ERANGE if the context is
  92. * longer than @len, or another -errno code.
  93. */
  94. int (*get_context)(struct inode *inode, void *ctx, size_t len);
  95. /*
  96. * Set an fscrypt context on the given inode.
  97. *
  98. * @inode: the inode whose context to set. The inode won't already have
  99. * an fscrypt context.
  100. * @ctx: the context to set
  101. * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE)
  102. * @fs_data: If called from fscrypt_set_context(), this will be the
  103. * value the filesystem passed to fscrypt_set_context().
  104. * Otherwise (i.e. when called from
  105. * FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL.
  106. *
  107. * i_rwsem will be held for write.
  108. *
  109. * Return: 0 on success, -errno on failure.
  110. */
  111. int (*set_context)(struct inode *inode, const void *ctx, size_t len,
  112. void *fs_data);
  113. /*
  114. * Get the dummy fscrypt policy in use on the filesystem (if any).
  115. *
  116. * Filesystems only need to implement this function if they support the
  117. * test_dummy_encryption mount option.
  118. *
  119. * Return: A pointer to the dummy fscrypt policy, if the filesystem is
  120. * mounted with test_dummy_encryption; otherwise NULL.
  121. */
  122. const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
  123. /*
  124. * Check whether a directory is empty. i_rwsem will be held for write.
  125. */
  126. bool (*empty_dir)(struct inode *inode);
  127. /*
  128. * Check whether the filesystem's inode numbers and UUID are stable,
  129. * meaning that they will never be changed even by offline operations
  130. * such as filesystem shrinking and therefore can be used in the
  131. * encryption without the possibility of files becoming unreadable.
  132. *
  133. * Filesystems only need to implement this function if they want to
  134. * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags. These
  135. * flags are designed to work around the limitations of UFS and eMMC
  136. * inline crypto hardware, and they shouldn't be used in scenarios where
  137. * such hardware isn't being used.
  138. *
  139. * Leaving this NULL is equivalent to always returning false.
  140. */
  141. bool (*has_stable_inodes)(struct super_block *sb);
  142. /*
  143. * Get the number of bits that the filesystem uses to represent inode
  144. * numbers and file logical block numbers.
  145. *
  146. * By default, both of these are assumed to be 64-bit. This function
  147. * can be implemented to declare that either or both of these numbers is
  148. * shorter, which may allow the use of the
  149. * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags and/or the use of
  150. * inline crypto hardware whose maximum DUN length is less than 64 bits
  151. * (e.g., eMMC v5.2 spec compliant hardware). This function only needs
  152. * to be implemented if support for one of these features is needed.
  153. */
  154. void (*get_ino_and_lblk_bits)(struct super_block *sb,
  155. int *ino_bits_ret, int *lblk_bits_ret);
  156. /*
  157. * Return an array of pointers to the block devices to which the
  158. * filesystem may write encrypted file contents, NULL if the filesystem
  159. * only has a single such block device, or an ERR_PTR() on error.
  160. *
  161. * On successful non-NULL return, *num_devs is set to the number of
  162. * devices in the returned array. The caller must free the returned
  163. * array using kfree().
  164. *
  165. * If the filesystem can use multiple block devices (other than block
  166. * devices that aren't used for encrypted file contents, such as
  167. * external journal devices), and wants to support inline encryption,
  168. * then it must implement this function. Otherwise it's not needed.
  169. */
  170. struct block_device **(*get_devices)(struct super_block *sb,
  171. unsigned int *num_devs);
  172. ANDROID_KABI_RESERVE(1);
  173. ANDROID_KABI_RESERVE(2);
  174. ANDROID_KABI_RESERVE(3);
  175. ANDROID_KABI_RESERVE(4);
  176. ANDROID_OEM_DATA_ARRAY(1, 4);
  177. };
  178. static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
  179. {
  180. /*
  181. * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
  182. * I.e., another task may publish ->i_crypt_info concurrently, executing
  183. * a RELEASE barrier. We need to use smp_load_acquire() here to safely
  184. * ACQUIRE the memory the other task published.
  185. */
  186. return smp_load_acquire(&inode->i_crypt_info);
  187. }
  188. /**
  189. * fscrypt_needs_contents_encryption() - check whether an inode needs
  190. * contents encryption
  191. * @inode: the inode to check
  192. *
  193. * Return: %true iff the inode is an encrypted regular file and the kernel was
  194. * built with fscrypt support.
  195. *
  196. * If you need to know whether the encrypt bit is set even when the kernel was
  197. * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
  198. */
  199. static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
  200. {
  201. return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
  202. }
  203. /*
  204. * When d_splice_alias() moves a directory's no-key alias to its plaintext alias
  205. * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be
  206. * cleared. Note that we don't have to support arbitrary moves of this flag
  207. * because fscrypt doesn't allow no-key names to be the source or target of a
  208. * rename().
  209. */
  210. static inline void fscrypt_handle_d_move(struct dentry *dentry)
  211. {
  212. dentry->d_flags &= ~DCACHE_NOKEY_NAME;
  213. }
  214. /**
  215. * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
  216. * @dentry: the dentry to check
  217. *
  218. * This returns true if the dentry is a no-key dentry. A no-key dentry is a
  219. * dentry that was created in an encrypted directory that hasn't had its
  220. * encryption key added yet. Such dentries may be either positive or negative.
  221. *
  222. * When a filesystem is asked to create a new filename in an encrypted directory
  223. * and the new filename's dentry is a no-key dentry, it must fail the operation
  224. * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
  225. * ->rename(), and ->link(). (However, ->rename() and ->link() are already
  226. * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
  227. *
  228. * This is necessary because creating a filename requires the directory's
  229. * encryption key, but just checking for the key on the directory inode during
  230. * the final filesystem operation doesn't guarantee that the key was available
  231. * during the preceding dentry lookup. And the key must have already been
  232. * available during the dentry lookup in order for it to have been checked
  233. * whether the filename already exists in the directory and for the new file's
  234. * dentry not to be invalidated due to it incorrectly having the no-key flag.
  235. *
  236. * Return: %true if the dentry is a no-key name
  237. */
  238. static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
  239. {
  240. return dentry->d_flags & DCACHE_NOKEY_NAME;
  241. }
  242. /* crypto.c */
  243. void fscrypt_enqueue_decrypt_work(struct work_struct *);
  244. struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
  245. unsigned int len,
  246. unsigned int offs,
  247. gfp_t gfp_flags);
  248. int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
  249. unsigned int len, unsigned int offs,
  250. u64 lblk_num, gfp_t gfp_flags);
  251. int fscrypt_decrypt_pagecache_blocks(struct folio *folio, size_t len,
  252. size_t offs);
  253. int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
  254. unsigned int len, unsigned int offs,
  255. u64 lblk_num);
  256. static inline bool fscrypt_is_bounce_page(struct page *page)
  257. {
  258. return page->mapping == NULL;
  259. }
  260. static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
  261. {
  262. return (struct page *)page_private(bounce_page);
  263. }
  264. void fscrypt_free_bounce_page(struct page *bounce_page);
  265. /* policy.c */
  266. int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
  267. int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
  268. int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
  269. int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
  270. int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
  271. int fscrypt_context_for_new_inode(void *ctx, struct inode *inode);
  272. int fscrypt_set_context(struct inode *inode, void *fs_data);
  273. struct fscrypt_dummy_policy {
  274. const union fscrypt_policy *policy;
  275. };
  276. int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
  277. struct fscrypt_dummy_policy *dummy_policy);
  278. bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
  279. const struct fscrypt_dummy_policy *p2);
  280. void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
  281. struct super_block *sb);
  282. static inline bool
  283. fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
  284. {
  285. return dummy_policy->policy != NULL;
  286. }
  287. static inline void
  288. fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
  289. {
  290. kfree(dummy_policy->policy);
  291. dummy_policy->policy = NULL;
  292. }
  293. /* keyring.c */
  294. void fscrypt_destroy_keyring(struct super_block *sb);
  295. int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
  296. int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
  297. int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
  298. int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
  299. /* keysetup.c */
  300. int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
  301. bool *encrypt_ret);
  302. void fscrypt_put_encryption_info(struct inode *inode);
  303. void fscrypt_free_inode(struct inode *inode);
  304. int fscrypt_drop_inode(struct inode *inode);
  305. /* fname.c */
  306. int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
  307. u8 *out, unsigned int olen);
  308. bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
  309. u32 max_len, u32 *encrypted_len_ret);
  310. int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
  311. int lookup, struct fscrypt_name *fname);
  312. static inline void fscrypt_free_filename(struct fscrypt_name *fname)
  313. {
  314. kfree(fname->crypto_buf.name);
  315. }
  316. int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
  317. struct fscrypt_str *crypto_str);
  318. void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
  319. int fscrypt_fname_disk_to_usr(const struct inode *inode,
  320. u32 hash, u32 minor_hash,
  321. const struct fscrypt_str *iname,
  322. struct fscrypt_str *oname);
  323. bool fscrypt_match_name(const struct fscrypt_name *fname,
  324. const u8 *de_name, u32 de_name_len);
  325. u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
  326. int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
  327. /* bio.c */
  328. bool fscrypt_decrypt_bio(struct bio *bio);
  329. int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
  330. sector_t pblk, unsigned int len);
  331. /* hooks.c */
  332. int fscrypt_file_open(struct inode *inode, struct file *filp);
  333. int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
  334. struct dentry *dentry);
  335. int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
  336. struct inode *new_dir, struct dentry *new_dentry,
  337. unsigned int flags);
  338. int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
  339. struct fscrypt_name *fname);
  340. int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry);
  341. int __fscrypt_prepare_readdir(struct inode *dir);
  342. int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
  343. int fscrypt_prepare_setflags(struct inode *inode,
  344. unsigned int oldflags, unsigned int flags);
  345. int fscrypt_prepare_symlink(struct inode *dir, const char *target,
  346. unsigned int len, unsigned int max_len,
  347. struct fscrypt_str *disk_link);
  348. int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
  349. unsigned int len, struct fscrypt_str *disk_link);
  350. const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
  351. unsigned int max_size,
  352. struct delayed_call *done);
  353. int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
  354. static inline void fscrypt_set_ops(struct super_block *sb,
  355. const struct fscrypt_operations *s_cop)
  356. {
  357. sb->s_cop = s_cop;
  358. }
  359. #else /* !CONFIG_FS_ENCRYPTION */
  360. static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
  361. {
  362. return NULL;
  363. }
  364. static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
  365. {
  366. return false;
  367. }
  368. static inline void fscrypt_handle_d_move(struct dentry *dentry)
  369. {
  370. }
  371. static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
  372. {
  373. return false;
  374. }
  375. /* crypto.c */
  376. static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
  377. {
  378. }
  379. static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
  380. unsigned int len,
  381. unsigned int offs,
  382. gfp_t gfp_flags)
  383. {
  384. return ERR_PTR(-EOPNOTSUPP);
  385. }
  386. static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
  387. struct page *page,
  388. unsigned int len,
  389. unsigned int offs, u64 lblk_num,
  390. gfp_t gfp_flags)
  391. {
  392. return -EOPNOTSUPP;
  393. }
  394. static inline int fscrypt_decrypt_pagecache_blocks(struct folio *folio,
  395. size_t len, size_t offs)
  396. {
  397. return -EOPNOTSUPP;
  398. }
  399. static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
  400. struct page *page,
  401. unsigned int len,
  402. unsigned int offs, u64 lblk_num)
  403. {
  404. return -EOPNOTSUPP;
  405. }
  406. static inline bool fscrypt_is_bounce_page(struct page *page)
  407. {
  408. return false;
  409. }
  410. static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
  411. {
  412. WARN_ON_ONCE(1);
  413. return ERR_PTR(-EINVAL);
  414. }
  415. static inline void fscrypt_free_bounce_page(struct page *bounce_page)
  416. {
  417. }
  418. /* policy.c */
  419. static inline int fscrypt_ioctl_set_policy(struct file *filp,
  420. const void __user *arg)
  421. {
  422. return -EOPNOTSUPP;
  423. }
  424. static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
  425. {
  426. return -EOPNOTSUPP;
  427. }
  428. static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
  429. void __user *arg)
  430. {
  431. return -EOPNOTSUPP;
  432. }
  433. static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
  434. {
  435. return -EOPNOTSUPP;
  436. }
  437. static inline int fscrypt_has_permitted_context(struct inode *parent,
  438. struct inode *child)
  439. {
  440. return 0;
  441. }
  442. static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
  443. {
  444. return -EOPNOTSUPP;
  445. }
  446. struct fscrypt_dummy_policy {
  447. };
  448. static inline int
  449. fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
  450. struct fscrypt_dummy_policy *dummy_policy)
  451. {
  452. return -EINVAL;
  453. }
  454. static inline bool
  455. fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
  456. const struct fscrypt_dummy_policy *p2)
  457. {
  458. return true;
  459. }
  460. static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
  461. char sep,
  462. struct super_block *sb)
  463. {
  464. }
  465. static inline bool
  466. fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
  467. {
  468. return false;
  469. }
  470. static inline void
  471. fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
  472. {
  473. }
  474. /* keyring.c */
  475. static inline void fscrypt_destroy_keyring(struct super_block *sb)
  476. {
  477. }
  478. static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
  479. {
  480. return -EOPNOTSUPP;
  481. }
  482. static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
  483. {
  484. return -EOPNOTSUPP;
  485. }
  486. static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
  487. void __user *arg)
  488. {
  489. return -EOPNOTSUPP;
  490. }
  491. static inline int fscrypt_ioctl_get_key_status(struct file *filp,
  492. void __user *arg)
  493. {
  494. return -EOPNOTSUPP;
  495. }
  496. /* keysetup.c */
  497. static inline int fscrypt_prepare_new_inode(struct inode *dir,
  498. struct inode *inode,
  499. bool *encrypt_ret)
  500. {
  501. if (IS_ENCRYPTED(dir))
  502. return -EOPNOTSUPP;
  503. return 0;
  504. }
  505. static inline void fscrypt_put_encryption_info(struct inode *inode)
  506. {
  507. return;
  508. }
  509. static inline void fscrypt_free_inode(struct inode *inode)
  510. {
  511. }
  512. static inline int fscrypt_drop_inode(struct inode *inode)
  513. {
  514. return 0;
  515. }
  516. /* fname.c */
  517. static inline int fscrypt_setup_filename(struct inode *dir,
  518. const struct qstr *iname,
  519. int lookup, struct fscrypt_name *fname)
  520. {
  521. if (IS_ENCRYPTED(dir))
  522. return -EOPNOTSUPP;
  523. memset(fname, 0, sizeof(*fname));
  524. fname->usr_fname = iname;
  525. fname->disk_name.name = (unsigned char *)iname->name;
  526. fname->disk_name.len = iname->len;
  527. return 0;
  528. }
  529. static inline void fscrypt_free_filename(struct fscrypt_name *fname)
  530. {
  531. return;
  532. }
  533. static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
  534. struct fscrypt_str *crypto_str)
  535. {
  536. return -EOPNOTSUPP;
  537. }
  538. static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
  539. {
  540. return;
  541. }
  542. static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
  543. u32 hash, u32 minor_hash,
  544. const struct fscrypt_str *iname,
  545. struct fscrypt_str *oname)
  546. {
  547. return -EOPNOTSUPP;
  548. }
  549. static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
  550. const u8 *de_name, u32 de_name_len)
  551. {
  552. /* Encryption support disabled; use standard comparison */
  553. if (de_name_len != fname->disk_name.len)
  554. return false;
  555. return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
  556. }
  557. static inline u64 fscrypt_fname_siphash(const struct inode *dir,
  558. const struct qstr *name)
  559. {
  560. WARN_ON_ONCE(1);
  561. return 0;
  562. }
  563. static inline int fscrypt_d_revalidate(struct dentry *dentry,
  564. unsigned int flags)
  565. {
  566. return 1;
  567. }
  568. /* bio.c */
  569. static inline bool fscrypt_decrypt_bio(struct bio *bio)
  570. {
  571. return true;
  572. }
  573. static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
  574. sector_t pblk, unsigned int len)
  575. {
  576. return -EOPNOTSUPP;
  577. }
  578. /* hooks.c */
  579. static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
  580. {
  581. if (IS_ENCRYPTED(inode))
  582. return -EOPNOTSUPP;
  583. return 0;
  584. }
  585. static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
  586. struct dentry *dentry)
  587. {
  588. return -EOPNOTSUPP;
  589. }
  590. static inline int __fscrypt_prepare_rename(struct inode *old_dir,
  591. struct dentry *old_dentry,
  592. struct inode *new_dir,
  593. struct dentry *new_dentry,
  594. unsigned int flags)
  595. {
  596. return -EOPNOTSUPP;
  597. }
  598. static inline int __fscrypt_prepare_lookup(struct inode *dir,
  599. struct dentry *dentry,
  600. struct fscrypt_name *fname)
  601. {
  602. return -EOPNOTSUPP;
  603. }
  604. static inline int fscrypt_prepare_lookup_partial(struct inode *dir,
  605. struct dentry *dentry)
  606. {
  607. return -EOPNOTSUPP;
  608. }
  609. static inline int __fscrypt_prepare_readdir(struct inode *dir)
  610. {
  611. return -EOPNOTSUPP;
  612. }
  613. static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
  614. struct iattr *attr)
  615. {
  616. return -EOPNOTSUPP;
  617. }
  618. static inline int fscrypt_prepare_setflags(struct inode *inode,
  619. unsigned int oldflags,
  620. unsigned int flags)
  621. {
  622. return 0;
  623. }
  624. static inline int fscrypt_prepare_symlink(struct inode *dir,
  625. const char *target,
  626. unsigned int len,
  627. unsigned int max_len,
  628. struct fscrypt_str *disk_link)
  629. {
  630. if (IS_ENCRYPTED(dir))
  631. return -EOPNOTSUPP;
  632. disk_link->name = (unsigned char *)target;
  633. disk_link->len = len + 1;
  634. if (disk_link->len > max_len)
  635. return -ENAMETOOLONG;
  636. return 0;
  637. }
  638. static inline int __fscrypt_encrypt_symlink(struct inode *inode,
  639. const char *target,
  640. unsigned int len,
  641. struct fscrypt_str *disk_link)
  642. {
  643. return -EOPNOTSUPP;
  644. }
  645. static inline const char *fscrypt_get_symlink(struct inode *inode,
  646. const void *caddr,
  647. unsigned int max_size,
  648. struct delayed_call *done)
  649. {
  650. return ERR_PTR(-EOPNOTSUPP);
  651. }
  652. static inline int fscrypt_symlink_getattr(const struct path *path,
  653. struct kstat *stat)
  654. {
  655. return -EOPNOTSUPP;
  656. }
  657. static inline void fscrypt_set_ops(struct super_block *sb,
  658. const struct fscrypt_operations *s_cop)
  659. {
  660. }
  661. #endif /* !CONFIG_FS_ENCRYPTION */
  662. /* inline_crypt.c */
  663. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  664. bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
  665. void fscrypt_set_bio_crypt_ctx(struct bio *bio,
  666. const struct inode *inode, u64 first_lblk,
  667. gfp_t gfp_mask);
  668. void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
  669. const struct buffer_head *first_bh,
  670. gfp_t gfp_mask);
  671. bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
  672. u64 next_lblk);
  673. bool fscrypt_mergeable_bio_bh(struct bio *bio,
  674. const struct buffer_head *next_bh);
  675. bool fscrypt_dio_supported(struct inode *inode);
  676. u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
  677. #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
  678. static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
  679. {
  680. return false;
  681. }
  682. static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
  683. const struct inode *inode,
  684. u64 first_lblk, gfp_t gfp_mask) { }
  685. static inline void fscrypt_set_bio_crypt_ctx_bh(
  686. struct bio *bio,
  687. const struct buffer_head *first_bh,
  688. gfp_t gfp_mask) { }
  689. static inline bool fscrypt_mergeable_bio(struct bio *bio,
  690. const struct inode *inode,
  691. u64 next_lblk)
  692. {
  693. return true;
  694. }
  695. static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
  696. const struct buffer_head *next_bh)
  697. {
  698. return true;
  699. }
  700. static inline bool fscrypt_dio_supported(struct inode *inode)
  701. {
  702. return !fscrypt_needs_contents_encryption(inode);
  703. }
  704. static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
  705. u64 nr_blocks)
  706. {
  707. return nr_blocks;
  708. }
  709. #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
  710. #if IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
  711. static inline bool
  712. fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
  713. {
  714. return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
  715. }
  716. #else
  717. static inline bool
  718. fscrypt_inode_should_skip_dm_default_key(const struct inode *inode)
  719. {
  720. return false;
  721. }
  722. #endif
  723. /**
  724. * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
  725. * encryption
  726. * @inode: an inode. If encrypted, its key must be set up.
  727. *
  728. * Return: true if the inode requires file contents encryption and if the
  729. * encryption should be done in the block layer via blk-crypto rather
  730. * than in the filesystem layer.
  731. */
  732. static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
  733. {
  734. return fscrypt_needs_contents_encryption(inode) &&
  735. __fscrypt_inode_uses_inline_crypto(inode);
  736. }
  737. /**
  738. * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
  739. * encryption
  740. * @inode: an inode. If encrypted, its key must be set up.
  741. *
  742. * Return: true if the inode requires file contents encryption and if the
  743. * encryption should be done in the filesystem layer rather than in the
  744. * block layer via blk-crypto.
  745. */
  746. static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
  747. {
  748. return fscrypt_needs_contents_encryption(inode) &&
  749. !__fscrypt_inode_uses_inline_crypto(inode);
  750. }
  751. /**
  752. * fscrypt_has_encryption_key() - check whether an inode has had its key set up
  753. * @inode: the inode to check
  754. *
  755. * Return: %true if the inode has had its encryption key set up, else %false.
  756. *
  757. * Usually this should be preceded by fscrypt_get_encryption_info() to try to
  758. * set up the key first.
  759. */
  760. static inline bool fscrypt_has_encryption_key(const struct inode *inode)
  761. {
  762. return fscrypt_get_info(inode) != NULL;
  763. }
  764. /**
  765. * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
  766. * directory
  767. * @old_dentry: an existing dentry for the inode being linked
  768. * @dir: the target directory
  769. * @dentry: negative dentry for the target filename
  770. *
  771. * A new link can only be added to an encrypted directory if the directory's
  772. * encryption key is available --- since otherwise we'd have no way to encrypt
  773. * the filename.
  774. *
  775. * We also verify that the link will not violate the constraint that all files
  776. * in an encrypted directory tree use the same encryption policy.
  777. *
  778. * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
  779. * -EXDEV if the link would result in an inconsistent encryption policy, or
  780. * another -errno code.
  781. */
  782. static inline int fscrypt_prepare_link(struct dentry *old_dentry,
  783. struct inode *dir,
  784. struct dentry *dentry)
  785. {
  786. if (IS_ENCRYPTED(dir))
  787. return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
  788. return 0;
  789. }
  790. /**
  791. * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
  792. * directories
  793. * @old_dir: source directory
  794. * @old_dentry: dentry for source file
  795. * @new_dir: target directory
  796. * @new_dentry: dentry for target location (may be negative unless exchanging)
  797. * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
  798. *
  799. * Prepare for ->rename() where the source and/or target directories may be
  800. * encrypted. A new link can only be added to an encrypted directory if the
  801. * directory's encryption key is available --- since otherwise we'd have no way
  802. * to encrypt the filename. A rename to an existing name, on the other hand,
  803. * *is* cryptographically possible without the key. However, we take the more
  804. * conservative approach and just forbid all no-key renames.
  805. *
  806. * We also verify that the rename will not violate the constraint that all files
  807. * in an encrypted directory tree use the same encryption policy.
  808. *
  809. * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
  810. * rename would cause inconsistent encryption policies, or another -errno code.
  811. */
  812. static inline int fscrypt_prepare_rename(struct inode *old_dir,
  813. struct dentry *old_dentry,
  814. struct inode *new_dir,
  815. struct dentry *new_dentry,
  816. unsigned int flags)
  817. {
  818. if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
  819. return __fscrypt_prepare_rename(old_dir, old_dentry,
  820. new_dir, new_dentry, flags);
  821. return 0;
  822. }
  823. /**
  824. * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
  825. * directory
  826. * @dir: directory being searched
  827. * @dentry: filename being looked up
  828. * @fname: (output) the name to use to search the on-disk directory
  829. *
  830. * Prepare for ->lookup() in a directory which may be encrypted by determining
  831. * the name that will actually be used to search the directory on-disk. If the
  832. * directory's encryption policy is supported by this kernel and its encryption
  833. * key is available, then the lookup is assumed to be by plaintext name;
  834. * otherwise, it is assumed to be by no-key name.
  835. *
  836. * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
  837. * name. In this case the filesystem must assign the dentry a dentry_operations
  838. * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
  839. * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
  840. * directory's encryption key is later added.
  841. *
  842. * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
  843. * filename isn't a valid no-key name, so a negative dentry should be created;
  844. * or another -errno code.
  845. */
  846. static inline int fscrypt_prepare_lookup(struct inode *dir,
  847. struct dentry *dentry,
  848. struct fscrypt_name *fname)
  849. {
  850. if (IS_ENCRYPTED(dir))
  851. return __fscrypt_prepare_lookup(dir, dentry, fname);
  852. memset(fname, 0, sizeof(*fname));
  853. fname->usr_fname = &dentry->d_name;
  854. fname->disk_name.name = (unsigned char *)dentry->d_name.name;
  855. fname->disk_name.len = dentry->d_name.len;
  856. return 0;
  857. }
  858. /**
  859. * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
  860. * @dir: the directory inode
  861. *
  862. * If the directory is encrypted and it doesn't already have its encryption key
  863. * set up, try to set it up so that the filenames will be listed in plaintext
  864. * form rather than in no-key form.
  865. *
  866. * Return: 0 on success; -errno on error. Note that the encryption key being
  867. * unavailable is not considered an error. It is also not an error if
  868. * the encryption policy is unsupported by this kernel; that is treated
  869. * like the key being unavailable, so that files can still be deleted.
  870. */
  871. static inline int fscrypt_prepare_readdir(struct inode *dir)
  872. {
  873. if (IS_ENCRYPTED(dir))
  874. return __fscrypt_prepare_readdir(dir);
  875. return 0;
  876. }
  877. /**
  878. * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
  879. * attributes
  880. * @dentry: dentry through which the inode is being changed
  881. * @attr: attributes to change
  882. *
  883. * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
  884. * most attribute changes are allowed even without the encryption key. However,
  885. * without the encryption key we do have to forbid truncates. This is needed
  886. * because the size being truncated to may not be a multiple of the filesystem
  887. * block size, and in that case we'd have to decrypt the final block, zero the
  888. * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
  889. * filesystem block boundary, but it's simpler to just forbid all truncates ---
  890. * and we already forbid all other contents modifications without the key.)
  891. *
  892. * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
  893. * if a problem occurred while setting up the encryption key.
  894. */
  895. static inline int fscrypt_prepare_setattr(struct dentry *dentry,
  896. struct iattr *attr)
  897. {
  898. if (IS_ENCRYPTED(d_inode(dentry)))
  899. return __fscrypt_prepare_setattr(dentry, attr);
  900. return 0;
  901. }
  902. /**
  903. * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
  904. * @inode: symlink inode
  905. * @target: plaintext symlink target
  906. * @len: length of @target excluding null terminator
  907. * @disk_link: (in/out) the on-disk symlink target being prepared
  908. *
  909. * If the symlink target needs to be encrypted, then this function encrypts it
  910. * into @disk_link->name. fscrypt_prepare_symlink() must have been called
  911. * previously to compute @disk_link->len. If the filesystem did not allocate a
  912. * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
  913. * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
  914. *
  915. * Return: 0 on success, -errno on failure
  916. */
  917. static inline int fscrypt_encrypt_symlink(struct inode *inode,
  918. const char *target,
  919. unsigned int len,
  920. struct fscrypt_str *disk_link)
  921. {
  922. if (IS_ENCRYPTED(inode))
  923. return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
  924. return 0;
  925. }
  926. /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
  927. static inline void fscrypt_finalize_bounce_page(struct page **pagep)
  928. {
  929. struct page *page = *pagep;
  930. if (fscrypt_is_bounce_page(page)) {
  931. *pagep = fscrypt_pagecache_page(page);
  932. fscrypt_free_bounce_page(page);
  933. }
  934. }
  935. #endif /* _LINUX_FSCRYPT_H */