fscrypt_private.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fscrypt_private.h
  4. *
  5. * Copyright (C) 2015, Google, Inc.
  6. *
  7. * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
  8. * Heavily modified since then.
  9. */
  10. #ifndef _FSCRYPT_PRIVATE_H
  11. #define _FSCRYPT_PRIVATE_H
  12. #include <linux/fscrypt.h>
  13. #include <linux/siphash.h>
  14. #include <crypto/hash.h>
  15. #include <linux/blk-crypto.h>
  16. #define CONST_STRLEN(str) (sizeof(str) - 1)
  17. #define FSCRYPT_FILE_NONCE_SIZE 16
  18. /*
  19. * Minimum size of an fscrypt master key. Note: a longer key will be required
  20. * if ciphers with a 256-bit security strength are used. This is just the
  21. * absolute minimum, which applies when only 128-bit encryption is used.
  22. */
  23. #define FSCRYPT_MIN_KEY_SIZE 16
  24. /*
  25. * This mask is passed as the third argument to the crypto_alloc_*() functions
  26. * to prevent fscrypt from using the Crypto API drivers for non-inline crypto
  27. * engines. Those drivers have been problematic for fscrypt. fscrypt users
  28. * have reported hangs and even incorrect en/decryption with these drivers.
  29. * Since going to the driver, off CPU, and back again is really slow, such
  30. * drivers can be over 50 times slower than the CPU-based code for fscrypt's
  31. * workload. Even on platforms that lack AES instructions on the CPU, using the
  32. * offloads has been shown to be slower, even staying with AES. (Of course,
  33. * Adiantum is faster still, and is the recommended option on such platforms...)
  34. *
  35. * Note that fscrypt also supports inline crypto engines. Those don't use the
  36. * Crypto API and work much better than the old-style (non-inline) engines.
  37. */
  38. #define FSCRYPT_CRYPTOAPI_MASK \
  39. (CRYPTO_ALG_ALLOCATES_MEMORY | CRYPTO_ALG_KERN_DRIVER_ONLY)
  40. #define FSCRYPT_CONTEXT_V1 1
  41. #define FSCRYPT_CONTEXT_V2 2
  42. /* Keep this in sync with include/uapi/linux/fscrypt.h */
  43. #define FSCRYPT_MODE_MAX FSCRYPT_MODE_AES_256_HCTR2
  44. struct fscrypt_context_v1 {
  45. u8 version; /* FSCRYPT_CONTEXT_V1 */
  46. u8 contents_encryption_mode;
  47. u8 filenames_encryption_mode;
  48. u8 flags;
  49. u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
  50. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  51. };
  52. struct fscrypt_context_v2 {
  53. u8 version; /* FSCRYPT_CONTEXT_V2 */
  54. u8 contents_encryption_mode;
  55. u8 filenames_encryption_mode;
  56. u8 flags;
  57. u8 log2_data_unit_size;
  58. u8 __reserved[3];
  59. u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
  60. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  61. };
  62. /*
  63. * fscrypt_context - the encryption context of an inode
  64. *
  65. * This is the on-disk equivalent of an fscrypt_policy, stored alongside each
  66. * encrypted file usually in a hidden extended attribute. It contains the
  67. * fields from the fscrypt_policy, in order to identify the encryption algorithm
  68. * and key with which the file is encrypted. It also contains a nonce that was
  69. * randomly generated by fscrypt itself; this is used as KDF input or as a tweak
  70. * to cause different files to be encrypted differently.
  71. */
  72. union fscrypt_context {
  73. u8 version;
  74. struct fscrypt_context_v1 v1;
  75. struct fscrypt_context_v2 v2;
  76. };
  77. /*
  78. * Return the size expected for the given fscrypt_context based on its version
  79. * number, or 0 if the context version is unrecognized.
  80. */
  81. static inline int fscrypt_context_size(const union fscrypt_context *ctx)
  82. {
  83. switch (ctx->version) {
  84. case FSCRYPT_CONTEXT_V1:
  85. BUILD_BUG_ON(sizeof(ctx->v1) != 28);
  86. return sizeof(ctx->v1);
  87. case FSCRYPT_CONTEXT_V2:
  88. BUILD_BUG_ON(sizeof(ctx->v2) != 40);
  89. return sizeof(ctx->v2);
  90. }
  91. return 0;
  92. }
  93. /* Check whether an fscrypt_context has a recognized version number and size */
  94. static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx,
  95. int ctx_size)
  96. {
  97. return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx);
  98. }
  99. /* Retrieve the context's nonce, assuming the context was already validated */
  100. static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)
  101. {
  102. switch (ctx->version) {
  103. case FSCRYPT_CONTEXT_V1:
  104. return ctx->v1.nonce;
  105. case FSCRYPT_CONTEXT_V2:
  106. return ctx->v2.nonce;
  107. }
  108. WARN_ON_ONCE(1);
  109. return NULL;
  110. }
  111. union fscrypt_policy {
  112. u8 version;
  113. struct fscrypt_policy_v1 v1;
  114. struct fscrypt_policy_v2 v2;
  115. };
  116. /*
  117. * Return the size expected for the given fscrypt_policy based on its version
  118. * number, or 0 if the policy version is unrecognized.
  119. */
  120. static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
  121. {
  122. switch (policy->version) {
  123. case FSCRYPT_POLICY_V1:
  124. return sizeof(policy->v1);
  125. case FSCRYPT_POLICY_V2:
  126. return sizeof(policy->v2);
  127. }
  128. return 0;
  129. }
  130. /* Return the contents encryption mode of a valid encryption policy */
  131. static inline u8
  132. fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
  133. {
  134. switch (policy->version) {
  135. case FSCRYPT_POLICY_V1:
  136. return policy->v1.contents_encryption_mode;
  137. case FSCRYPT_POLICY_V2:
  138. return policy->v2.contents_encryption_mode;
  139. }
  140. BUG();
  141. }
  142. /* Return the filenames encryption mode of a valid encryption policy */
  143. static inline u8
  144. fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
  145. {
  146. switch (policy->version) {
  147. case FSCRYPT_POLICY_V1:
  148. return policy->v1.filenames_encryption_mode;
  149. case FSCRYPT_POLICY_V2:
  150. return policy->v2.filenames_encryption_mode;
  151. }
  152. BUG();
  153. }
  154. /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */
  155. static inline u8
  156. fscrypt_policy_flags(const union fscrypt_policy *policy)
  157. {
  158. switch (policy->version) {
  159. case FSCRYPT_POLICY_V1:
  160. return policy->v1.flags;
  161. case FSCRYPT_POLICY_V2:
  162. return policy->v2.flags;
  163. }
  164. BUG();
  165. }
  166. static inline int
  167. fscrypt_policy_v2_du_bits(const struct fscrypt_policy_v2 *policy,
  168. const struct inode *inode)
  169. {
  170. return policy->log2_data_unit_size ?: inode->i_blkbits;
  171. }
  172. static inline int
  173. fscrypt_policy_du_bits(const union fscrypt_policy *policy,
  174. const struct inode *inode)
  175. {
  176. switch (policy->version) {
  177. case FSCRYPT_POLICY_V1:
  178. return inode->i_blkbits;
  179. case FSCRYPT_POLICY_V2:
  180. return fscrypt_policy_v2_du_bits(&policy->v2, inode);
  181. }
  182. BUG();
  183. }
  184. /*
  185. * For encrypted symlinks, the ciphertext length is stored at the beginning
  186. * of the string in little-endian format.
  187. */
  188. struct fscrypt_symlink_data {
  189. __le16 len;
  190. char encrypted_path[];
  191. } __packed;
  192. /**
  193. * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption
  194. * @tfm: crypto API transform object
  195. * @blk_key: key for blk-crypto
  196. *
  197. * Normally only one of the fields will be non-NULL.
  198. */
  199. struct fscrypt_prepared_key {
  200. struct crypto_skcipher *tfm;
  201. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  202. struct blk_crypto_key *blk_key;
  203. #endif
  204. };
  205. /*
  206. * fscrypt_inode_info - the "encryption key" for an inode
  207. *
  208. * When an encrypted file's key is made available, an instance of this struct is
  209. * allocated and stored in ->i_crypt_info. Once created, it remains until the
  210. * inode is evicted.
  211. */
  212. struct fscrypt_inode_info {
  213. /* The key in a form prepared for actual encryption/decryption */
  214. struct fscrypt_prepared_key ci_enc_key;
  215. /* True if ci_enc_key should be freed when this struct is freed */
  216. u8 ci_owns_key : 1;
  217. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  218. /*
  219. * True if this inode will use inline encryption (blk-crypto) instead of
  220. * the traditional filesystem-layer encryption.
  221. */
  222. u8 ci_inlinecrypt : 1;
  223. #endif
  224. /* True if ci_dirhash_key is initialized */
  225. u8 ci_dirhash_key_initialized : 1;
  226. /*
  227. * log2 of the data unit size (granularity of contents encryption) of
  228. * this file. This is computable from ci_policy and ci_inode but is
  229. * cached here for efficiency. Only used for regular files.
  230. */
  231. u8 ci_data_unit_bits;
  232. /* Cached value: log2 of number of data units per FS block */
  233. u8 ci_data_units_per_block_bits;
  234. /* Hashed inode number. Only set for IV_INO_LBLK_32 */
  235. u32 ci_hashed_ino;
  236. /*
  237. * Encryption mode used for this inode. It corresponds to either the
  238. * contents or filenames encryption mode, depending on the inode type.
  239. */
  240. struct fscrypt_mode *ci_mode;
  241. /* Back-pointer to the inode */
  242. struct inode *ci_inode;
  243. /*
  244. * The master key with which this inode was unlocked (decrypted). This
  245. * will be NULL if the master key was found in a process-subscribed
  246. * keyring rather than in the filesystem-level keyring.
  247. */
  248. struct fscrypt_master_key *ci_master_key;
  249. /*
  250. * Link in list of inodes that were unlocked with the master key.
  251. * Only used when ->ci_master_key is set.
  252. */
  253. struct list_head ci_master_key_link;
  254. /*
  255. * If non-NULL, then encryption is done using the master key directly
  256. * and ci_enc_key will equal ci_direct_key->dk_key.
  257. */
  258. struct fscrypt_direct_key *ci_direct_key;
  259. /*
  260. * This inode's hash key for filenames. This is a 128-bit SipHash-2-4
  261. * key. This is only set for directories that use a keyed dirhash over
  262. * the plaintext filenames -- currently just casefolded directories.
  263. */
  264. siphash_key_t ci_dirhash_key;
  265. /* The encryption policy used by this inode */
  266. union fscrypt_policy ci_policy;
  267. /* This inode's nonce, copied from the fscrypt_context */
  268. u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
  269. };
  270. typedef enum {
  271. FS_DECRYPT = 0,
  272. FS_ENCRYPT,
  273. } fscrypt_direction_t;
  274. /* crypto.c */
  275. extern struct kmem_cache *fscrypt_inode_info_cachep;
  276. int fscrypt_initialize(struct super_block *sb);
  277. int fscrypt_crypt_data_unit(const struct fscrypt_inode_info *ci,
  278. fscrypt_direction_t rw, u64 index,
  279. struct page *src_page, struct page *dest_page,
  280. unsigned int len, unsigned int offs,
  281. gfp_t gfp_flags);
  282. struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
  283. void __printf(3, 4) __cold
  284. fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
  285. #define fscrypt_warn(inode, fmt, ...) \
  286. fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
  287. #define fscrypt_err(inode, fmt, ...) \
  288. fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
  289. #define FSCRYPT_MAX_IV_SIZE 32
  290. union fscrypt_iv {
  291. struct {
  292. /* zero-based index of data unit within the file */
  293. __le64 index;
  294. /* per-file nonce; only set in DIRECT_KEY mode */
  295. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  296. };
  297. u8 raw[FSCRYPT_MAX_IV_SIZE];
  298. __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];
  299. };
  300. void fscrypt_generate_iv(union fscrypt_iv *iv, u64 index,
  301. const struct fscrypt_inode_info *ci);
  302. /*
  303. * Return the number of bits used by the maximum file data unit index that is
  304. * possible on the given filesystem, using the given log2 data unit size.
  305. */
  306. static inline int
  307. fscrypt_max_file_dun_bits(const struct super_block *sb, int du_bits)
  308. {
  309. return fls64(sb->s_maxbytes - 1) - du_bits;
  310. }
  311. /* fname.c */
  312. bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
  313. u32 orig_len, u32 max_len,
  314. u32 *encrypted_len_ret);
  315. /* hkdf.c */
  316. struct fscrypt_hkdf {
  317. struct crypto_shash *hmac_tfm;
  318. };
  319. int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
  320. unsigned int master_key_size);
  321. /*
  322. * The list of contexts in which fscrypt uses HKDF. These values are used as
  323. * the first byte of the HKDF application-specific info string to guarantee that
  324. * info strings are never repeated between contexts. This ensures that all HKDF
  325. * outputs are unique and cryptographically isolated, i.e. knowledge of one
  326. * output doesn't reveal another.
  327. */
  328. #define HKDF_CONTEXT_KEY_IDENTIFIER 1 /* info=<empty> */
  329. #define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */
  330. #define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */
  331. #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */
  332. #define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */
  333. #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */
  334. #define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */
  335. int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
  336. const u8 *info, unsigned int infolen,
  337. u8 *okm, unsigned int okmlen);
  338. void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
  339. /* inline_crypt.c */
  340. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  341. int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci);
  342. static inline bool
  343. fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci)
  344. {
  345. return ci->ci_inlinecrypt;
  346. }
  347. int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
  348. const u8 *raw_key,
  349. const struct fscrypt_inode_info *ci);
  350. void fscrypt_destroy_inline_crypt_key(struct super_block *sb,
  351. struct fscrypt_prepared_key *prep_key);
  352. /*
  353. * Check whether the crypto transform or blk-crypto key has been allocated in
  354. * @prep_key, depending on which encryption implementation the file will use.
  355. */
  356. static inline bool
  357. fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
  358. const struct fscrypt_inode_info *ci)
  359. {
  360. /*
  361. * The two smp_load_acquire()'s here pair with the smp_store_release()'s
  362. * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().
  363. * I.e., in some cases (namely, if this prep_key is a per-mode
  364. * encryption key) another task can publish blk_key or tfm concurrently,
  365. * executing a RELEASE barrier. We need to use smp_load_acquire() here
  366. * to safely ACQUIRE the memory the other task published.
  367. */
  368. if (fscrypt_using_inline_encryption(ci))
  369. return smp_load_acquire(&prep_key->blk_key) != NULL;
  370. return smp_load_acquire(&prep_key->tfm) != NULL;
  371. }
  372. #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
  373. static inline int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci)
  374. {
  375. return 0;
  376. }
  377. static inline bool
  378. fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci)
  379. {
  380. return false;
  381. }
  382. static inline int
  383. fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
  384. const u8 *raw_key,
  385. const struct fscrypt_inode_info *ci)
  386. {
  387. WARN_ON_ONCE(1);
  388. return -EOPNOTSUPP;
  389. }
  390. static inline void
  391. fscrypt_destroy_inline_crypt_key(struct super_block *sb,
  392. struct fscrypt_prepared_key *prep_key)
  393. {
  394. }
  395. static inline bool
  396. fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
  397. const struct fscrypt_inode_info *ci)
  398. {
  399. return smp_load_acquire(&prep_key->tfm) != NULL;
  400. }
  401. #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
  402. /* keyring.c */
  403. /*
  404. * fscrypt_master_key_secret - secret key material of an in-use master key
  405. */
  406. struct fscrypt_master_key_secret {
  407. /*
  408. * For v2 policy keys: HKDF context keyed by this master key.
  409. * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
  410. */
  411. struct fscrypt_hkdf hkdf;
  412. /*
  413. * Size of the raw key in bytes. This remains set even if ->raw was
  414. * zeroized due to no longer being needed. I.e. we still remember the
  415. * size of the key even if we don't need to remember the key itself.
  416. */
  417. u32 size;
  418. /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */
  419. u8 raw[FSCRYPT_MAX_KEY_SIZE];
  420. } __randomize_layout;
  421. /*
  422. * fscrypt_master_key - an in-use master key
  423. *
  424. * This represents a master encryption key which has been added to the
  425. * filesystem. There are three high-level states that a key can be in:
  426. *
  427. * FSCRYPT_KEY_STATUS_PRESENT
  428. * Key is fully usable; it can be used to unlock inodes that are encrypted
  429. * with it (this includes being able to create new inodes). ->mk_present
  430. * indicates whether the key is in this state. ->mk_secret exists, the key
  431. * is in the keyring, and ->mk_active_refs > 0 due to ->mk_present.
  432. *
  433. * FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED
  434. * Removal of this key has been initiated, but some inodes that were
  435. * unlocked with it are still in-use. Like ABSENT, ->mk_secret is wiped,
  436. * and the key can no longer be used to unlock inodes. Unlike ABSENT, the
  437. * key is still in the keyring; ->mk_decrypted_inodes is nonempty; and
  438. * ->mk_active_refs > 0, being equal to the size of ->mk_decrypted_inodes.
  439. *
  440. * This state transitions to ABSENT if ->mk_decrypted_inodes becomes empty,
  441. * or to PRESENT if FS_IOC_ADD_ENCRYPTION_KEY is called again for this key.
  442. *
  443. * FSCRYPT_KEY_STATUS_ABSENT
  444. * Key is fully removed. The key is no longer in the keyring,
  445. * ->mk_decrypted_inodes is empty, ->mk_active_refs == 0, ->mk_secret is
  446. * wiped, and the key can no longer be used to unlock inodes.
  447. */
  448. struct fscrypt_master_key {
  449. /*
  450. * Link in ->s_master_keys->key_hashtable.
  451. * Only valid if ->mk_active_refs > 0.
  452. */
  453. struct hlist_node mk_node;
  454. /* Semaphore that protects ->mk_secret, ->mk_users, and ->mk_present */
  455. struct rw_semaphore mk_sem;
  456. /*
  457. * Active and structural reference counts. An active ref guarantees
  458. * that the struct continues to exist, continues to be in the keyring
  459. * ->s_master_keys, and that any embedded subkeys (e.g.
  460. * ->mk_direct_keys) that have been prepared continue to exist.
  461. * A structural ref only guarantees that the struct continues to exist.
  462. *
  463. * There is one active ref associated with ->mk_present being true, and
  464. * one active ref for each inode in ->mk_decrypted_inodes.
  465. *
  466. * There is one structural ref associated with the active refcount being
  467. * nonzero. Finding a key in the keyring also takes a structural ref,
  468. * which is then held temporarily while the key is operated on.
  469. */
  470. refcount_t mk_active_refs;
  471. refcount_t mk_struct_refs;
  472. struct rcu_head mk_rcu_head;
  473. /*
  474. * The secret key material. Wiped as soon as it is no longer needed;
  475. * for details, see the fscrypt_master_key struct comment.
  476. *
  477. * Locking: protected by ->mk_sem.
  478. */
  479. struct fscrypt_master_key_secret mk_secret;
  480. /*
  481. * For v1 policy keys: an arbitrary key descriptor which was assigned by
  482. * userspace (->descriptor).
  483. *
  484. * For v2 policy keys: a cryptographic hash of this key (->identifier).
  485. */
  486. struct fscrypt_key_specifier mk_spec;
  487. /*
  488. * Keyring which contains a key of type 'key_type_fscrypt_user' for each
  489. * user who has added this key. Normally each key will be added by just
  490. * one user, but it's possible that multiple users share a key, and in
  491. * that case we need to keep track of those users so that one user can't
  492. * remove the key before the others want it removed too.
  493. *
  494. * This is NULL for v1 policy keys; those can only be added by root.
  495. *
  496. * Locking: protected by ->mk_sem. (We don't just rely on the keyrings
  497. * subsystem semaphore ->mk_users->sem, as we need support for atomic
  498. * search+insert along with proper synchronization with other fields.)
  499. */
  500. struct key *mk_users;
  501. /*
  502. * List of inodes that were unlocked using this key. This allows the
  503. * inodes to be evicted efficiently if the key is removed.
  504. */
  505. struct list_head mk_decrypted_inodes;
  506. spinlock_t mk_decrypted_inodes_lock;
  507. /*
  508. * Per-mode encryption keys for the various types of encryption policies
  509. * that use them. Allocated and derived on-demand.
  510. */
  511. struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
  512. struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
  513. struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
  514. /* Hash key for inode numbers. Initialized only when needed. */
  515. siphash_key_t mk_ino_hash_key;
  516. bool mk_ino_hash_key_initialized;
  517. /*
  518. * Whether this key is in the "present" state, i.e. fully usable. For
  519. * details, see the fscrypt_master_key struct comment.
  520. *
  521. * Locking: protected by ->mk_sem, but can be read locklessly using
  522. * READ_ONCE(). Writers must use WRITE_ONCE() when concurrent readers
  523. * are possible.
  524. */
  525. bool mk_present;
  526. } __randomize_layout;
  527. static inline const char *master_key_spec_type(
  528. const struct fscrypt_key_specifier *spec)
  529. {
  530. switch (spec->type) {
  531. case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
  532. return "descriptor";
  533. case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
  534. return "identifier";
  535. }
  536. return "[unknown]";
  537. }
  538. static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
  539. {
  540. switch (spec->type) {
  541. case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
  542. return FSCRYPT_KEY_DESCRIPTOR_SIZE;
  543. case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
  544. return FSCRYPT_KEY_IDENTIFIER_SIZE;
  545. }
  546. return 0;
  547. }
  548. void fscrypt_put_master_key(struct fscrypt_master_key *mk);
  549. void fscrypt_put_master_key_activeref(struct super_block *sb,
  550. struct fscrypt_master_key *mk);
  551. struct fscrypt_master_key *
  552. fscrypt_find_master_key(struct super_block *sb,
  553. const struct fscrypt_key_specifier *mk_spec);
  554. int fscrypt_get_test_dummy_key_identifier(
  555. u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
  556. int fscrypt_add_test_dummy_key(struct super_block *sb,
  557. struct fscrypt_key_specifier *key_spec);
  558. int fscrypt_verify_key_added(struct super_block *sb,
  559. const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
  560. int __init fscrypt_init_keyring(void);
  561. /* keysetup.c */
  562. struct fscrypt_mode {
  563. const char *friendly_name;
  564. const char *cipher_str;
  565. int keysize; /* key size in bytes */
  566. int security_strength; /* security strength in bytes */
  567. int ivsize; /* IV size in bytes */
  568. int logged_cryptoapi_impl;
  569. int logged_blk_crypto_native;
  570. int logged_blk_crypto_fallback;
  571. enum blk_crypto_mode_num blk_crypto_mode;
  572. };
  573. extern struct fscrypt_mode fscrypt_modes[];
  574. int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
  575. const u8 *raw_key, const struct fscrypt_inode_info *ci);
  576. void fscrypt_destroy_prepared_key(struct super_block *sb,
  577. struct fscrypt_prepared_key *prep_key);
  578. int fscrypt_set_per_file_enc_key(struct fscrypt_inode_info *ci,
  579. const u8 *raw_key);
  580. int fscrypt_derive_dirhash_key(struct fscrypt_inode_info *ci,
  581. const struct fscrypt_master_key *mk);
  582. void fscrypt_hash_inode_number(struct fscrypt_inode_info *ci,
  583. const struct fscrypt_master_key *mk);
  584. int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
  585. /**
  586. * fscrypt_require_key() - require an inode's encryption key
  587. * @inode: the inode we need the key for
  588. *
  589. * If the inode is encrypted, set up its encryption key if not already done.
  590. * Then require that the key be present and return -ENOKEY otherwise.
  591. *
  592. * No locks are needed, and the key will live as long as the struct inode --- so
  593. * it won't go away from under you.
  594. *
  595. * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
  596. * if a problem occurred while setting up the encryption key.
  597. */
  598. static inline int fscrypt_require_key(struct inode *inode)
  599. {
  600. if (IS_ENCRYPTED(inode)) {
  601. int err = fscrypt_get_encryption_info(inode, false);
  602. if (err)
  603. return err;
  604. if (!fscrypt_has_encryption_key(inode))
  605. return -ENOKEY;
  606. }
  607. return 0;
  608. }
  609. /* keysetup_v1.c */
  610. void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
  611. int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
  612. const u8 *raw_master_key);
  613. int fscrypt_setup_v1_file_key_via_subscribed_keyrings(
  614. struct fscrypt_inode_info *ci);
  615. /* policy.c */
  616. bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
  617. const union fscrypt_policy *policy2);
  618. int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
  619. struct fscrypt_key_specifier *key_spec);
  620. const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb);
  621. bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
  622. const struct inode *inode);
  623. int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
  624. const union fscrypt_context *ctx_u,
  625. int ctx_size);
  626. const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
  627. #endif /* _FSCRYPT_PRIVATE_H */