fscrypt_private.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. /* Maximum size of a standard fscrypt master key */
  25. #define FSCRYPT_MAX_STANDARD_KEY_SIZE 64
  26. /* Maximum size of a hardware-wrapped fscrypt master key */
  27. #define FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE
  28. /*
  29. * Maximum size of an fscrypt master key across both key types.
  30. * This should just use max(), but max() doesn't work in a struct definition.
  31. */
  32. #define FSCRYPT_MAX_ANY_KEY_SIZE \
  33. (FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE > FSCRYPT_MAX_STANDARD_KEY_SIZE ? \
  34. FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE : FSCRYPT_MAX_STANDARD_KEY_SIZE)
  35. /*
  36. * FSCRYPT_MAX_KEY_SIZE is defined in the UAPI header, but the addition of
  37. * hardware-wrapped keys has made it misleading as it's only for standard keys.
  38. * Don't use it in kernel code; use one of the above constants instead.
  39. */
  40. #undef FSCRYPT_MAX_KEY_SIZE
  41. #define FSCRYPT_CONTEXT_V1 1
  42. #define FSCRYPT_CONTEXT_V2 2
  43. /* Keep this in sync with include/uapi/linux/fscrypt.h */
  44. #define FSCRYPT_MODE_MAX FSCRYPT_MODE_AES_256_HCTR2
  45. struct fscrypt_context_v1 {
  46. u8 version; /* FSCRYPT_CONTEXT_V1 */
  47. u8 contents_encryption_mode;
  48. u8 filenames_encryption_mode;
  49. u8 flags;
  50. u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
  51. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  52. };
  53. struct fscrypt_context_v2 {
  54. u8 version; /* FSCRYPT_CONTEXT_V2 */
  55. u8 contents_encryption_mode;
  56. u8 filenames_encryption_mode;
  57. u8 flags;
  58. u8 log2_data_unit_size;
  59. u8 __reserved[3];
  60. u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
  61. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  62. };
  63. /*
  64. * fscrypt_context - the encryption context of an inode
  65. *
  66. * This is the on-disk equivalent of an fscrypt_policy, stored alongside each
  67. * encrypted file usually in a hidden extended attribute. It contains the
  68. * fields from the fscrypt_policy, in order to identify the encryption algorithm
  69. * and key with which the file is encrypted. It also contains a nonce that was
  70. * randomly generated by fscrypt itself; this is used as KDF input or as a tweak
  71. * to cause different files to be encrypted differently.
  72. */
  73. union fscrypt_context {
  74. u8 version;
  75. struct fscrypt_context_v1 v1;
  76. struct fscrypt_context_v2 v2;
  77. };
  78. /*
  79. * Return the size expected for the given fscrypt_context based on its version
  80. * number, or 0 if the context version is unrecognized.
  81. */
  82. static inline int fscrypt_context_size(const union fscrypt_context *ctx)
  83. {
  84. switch (ctx->version) {
  85. case FSCRYPT_CONTEXT_V1:
  86. BUILD_BUG_ON(sizeof(ctx->v1) != 28);
  87. return sizeof(ctx->v1);
  88. case FSCRYPT_CONTEXT_V2:
  89. BUILD_BUG_ON(sizeof(ctx->v2) != 40);
  90. return sizeof(ctx->v2);
  91. }
  92. return 0;
  93. }
  94. /* Check whether an fscrypt_context has a recognized version number and size */
  95. static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx,
  96. int ctx_size)
  97. {
  98. return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx);
  99. }
  100. /* Retrieve the context's nonce, assuming the context was already validated */
  101. static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)
  102. {
  103. switch (ctx->version) {
  104. case FSCRYPT_CONTEXT_V1:
  105. return ctx->v1.nonce;
  106. case FSCRYPT_CONTEXT_V2:
  107. return ctx->v2.nonce;
  108. }
  109. WARN_ON_ONCE(1);
  110. return NULL;
  111. }
  112. union fscrypt_policy {
  113. u8 version;
  114. struct fscrypt_policy_v1 v1;
  115. struct fscrypt_policy_v2 v2;
  116. };
  117. /*
  118. * Return the size expected for the given fscrypt_policy based on its version
  119. * number, or 0 if the policy version is unrecognized.
  120. */
  121. static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
  122. {
  123. switch (policy->version) {
  124. case FSCRYPT_POLICY_V1:
  125. return sizeof(policy->v1);
  126. case FSCRYPT_POLICY_V2:
  127. return sizeof(policy->v2);
  128. }
  129. return 0;
  130. }
  131. /* Return the contents encryption mode of a valid encryption policy */
  132. static inline u8
  133. fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
  134. {
  135. switch (policy->version) {
  136. case FSCRYPT_POLICY_V1:
  137. return policy->v1.contents_encryption_mode;
  138. case FSCRYPT_POLICY_V2:
  139. return policy->v2.contents_encryption_mode;
  140. }
  141. BUG();
  142. }
  143. /* Return the filenames encryption mode of a valid encryption policy */
  144. static inline u8
  145. fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
  146. {
  147. switch (policy->version) {
  148. case FSCRYPT_POLICY_V1:
  149. return policy->v1.filenames_encryption_mode;
  150. case FSCRYPT_POLICY_V2:
  151. return policy->v2.filenames_encryption_mode;
  152. }
  153. BUG();
  154. }
  155. /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */
  156. static inline u8
  157. fscrypt_policy_flags(const union fscrypt_policy *policy)
  158. {
  159. switch (policy->version) {
  160. case FSCRYPT_POLICY_V1:
  161. return policy->v1.flags;
  162. case FSCRYPT_POLICY_V2:
  163. return policy->v2.flags;
  164. }
  165. BUG();
  166. }
  167. static inline int
  168. fscrypt_policy_v2_du_bits(const struct fscrypt_policy_v2 *policy,
  169. const struct inode *inode)
  170. {
  171. return policy->log2_data_unit_size ?: inode->i_blkbits;
  172. }
  173. static inline int
  174. fscrypt_policy_du_bits(const union fscrypt_policy *policy,
  175. const struct inode *inode)
  176. {
  177. switch (policy->version) {
  178. case FSCRYPT_POLICY_V1:
  179. return inode->i_blkbits;
  180. case FSCRYPT_POLICY_V2:
  181. return fscrypt_policy_v2_du_bits(&policy->v2, inode);
  182. }
  183. BUG();
  184. }
  185. /*
  186. * For encrypted symlinks, the ciphertext length is stored at the beginning
  187. * of the string in little-endian format.
  188. */
  189. struct fscrypt_symlink_data {
  190. __le16 len;
  191. char encrypted_path[1];
  192. } __packed;
  193. /**
  194. * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption
  195. * @tfm: crypto API transform object
  196. * @blk_key: key for blk-crypto
  197. *
  198. * Normally only one of the fields will be non-NULL.
  199. */
  200. struct fscrypt_prepared_key {
  201. struct crypto_skcipher *tfm;
  202. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  203. struct blk_crypto_key *blk_key;
  204. #endif
  205. };
  206. /*
  207. * fscrypt_info - the "encryption key" for an inode
  208. *
  209. * When an encrypted file's key is made available, an instance of this struct is
  210. * allocated and stored in ->i_crypt_info. Once created, it remains until the
  211. * inode is evicted.
  212. */
  213. struct fscrypt_info {
  214. /* The key in a form prepared for actual encryption/decryption */
  215. struct fscrypt_prepared_key ci_enc_key;
  216. /* True if ci_enc_key should be freed when this fscrypt_info is freed */
  217. bool ci_owns_key;
  218. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  219. /*
  220. * True if this inode will use inline encryption (blk-crypto) instead of
  221. * the traditional filesystem-layer encryption.
  222. */
  223. bool ci_inlinecrypt;
  224. #endif
  225. /*
  226. * log2 of the data unit size (granularity of contents encryption) of
  227. * this file. This is computable from ci_policy and ci_inode but is
  228. * cached here for efficiency. Only used for regular files.
  229. */
  230. u8 ci_data_unit_bits;
  231. /* Cached value: log2 of number of data units per FS block */
  232. u8 ci_data_units_per_block_bits;
  233. /*
  234. * Encryption mode used for this inode. It corresponds to either the
  235. * contents or filenames encryption mode, depending on the inode type.
  236. */
  237. struct fscrypt_mode *ci_mode;
  238. /* Back-pointer to the inode */
  239. struct inode *ci_inode;
  240. /*
  241. * The master key with which this inode was unlocked (decrypted). This
  242. * will be NULL if the master key was found in a process-subscribed
  243. * keyring rather than in the filesystem-level keyring.
  244. */
  245. struct fscrypt_master_key *ci_master_key;
  246. /*
  247. * Link in list of inodes that were unlocked with the master key.
  248. * Only used when ->ci_master_key is set.
  249. */
  250. struct list_head ci_master_key_link;
  251. /*
  252. * If non-NULL, then encryption is done using the master key directly
  253. * and ci_enc_key will equal ci_direct_key->dk_key.
  254. */
  255. struct fscrypt_direct_key *ci_direct_key;
  256. /*
  257. * This inode's hash key for filenames. This is a 128-bit SipHash-2-4
  258. * key. This is only set for directories that use a keyed dirhash over
  259. * the plaintext filenames -- currently just casefolded directories.
  260. */
  261. siphash_key_t ci_dirhash_key;
  262. bool ci_dirhash_key_initialized;
  263. /* The encryption policy used by this inode */
  264. union fscrypt_policy ci_policy;
  265. /* This inode's nonce, copied from the fscrypt_context */
  266. u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
  267. /* Hashed inode number. Only set for IV_INO_LBLK_32 */
  268. u32 ci_hashed_ino;
  269. };
  270. typedef enum {
  271. FS_DECRYPT = 0,
  272. FS_ENCRYPT,
  273. } fscrypt_direction_t;
  274. /* crypto.c */
  275. extern struct kmem_cache *fscrypt_info_cachep;
  276. int fscrypt_initialize(struct super_block *sb);
  277. int fscrypt_crypt_data_unit(const struct fscrypt_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_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_info *ci,
  342. bool is_hw_wrapped_key);
  343. static inline bool
  344. fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
  345. {
  346. return ci->ci_inlinecrypt;
  347. }
  348. int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
  349. const u8 *raw_key, size_t raw_key_size,
  350. bool is_hw_wrapped,
  351. const struct fscrypt_info *ci);
  352. void fscrypt_destroy_inline_crypt_key(struct super_block *sb,
  353. struct fscrypt_prepared_key *prep_key);
  354. int fscrypt_derive_sw_secret(struct super_block *sb,
  355. const u8 *wrapped_key, size_t wrapped_key_size,
  356. u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]);
  357. /*
  358. * Check whether the crypto transform or blk-crypto key has been allocated in
  359. * @prep_key, depending on which encryption implementation the file will use.
  360. */
  361. static inline bool
  362. fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
  363. const struct fscrypt_info *ci)
  364. {
  365. /*
  366. * The two smp_load_acquire()'s here pair with the smp_store_release()'s
  367. * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().
  368. * I.e., in some cases (namely, if this prep_key is a per-mode
  369. * encryption key) another task can publish blk_key or tfm concurrently,
  370. * executing a RELEASE barrier. We need to use smp_load_acquire() here
  371. * to safely ACQUIRE the memory the other task published.
  372. */
  373. if (fscrypt_using_inline_encryption(ci))
  374. return smp_load_acquire(&prep_key->blk_key) != NULL;
  375. return smp_load_acquire(&prep_key->tfm) != NULL;
  376. }
  377. #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
  378. static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
  379. bool is_hw_wrapped_key)
  380. {
  381. return 0;
  382. }
  383. static inline bool
  384. fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
  385. {
  386. return false;
  387. }
  388. static inline int
  389. fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
  390. const u8 *raw_key, size_t raw_key_size,
  391. bool is_hw_wrapped,
  392. const struct fscrypt_info *ci)
  393. {
  394. WARN_ON_ONCE(1);
  395. return -EOPNOTSUPP;
  396. }
  397. static inline void
  398. fscrypt_destroy_inline_crypt_key(struct super_block *sb,
  399. struct fscrypt_prepared_key *prep_key)
  400. {
  401. }
  402. static inline int
  403. fscrypt_derive_sw_secret(struct super_block *sb,
  404. const u8 *wrapped_key, size_t wrapped_key_size,
  405. u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])
  406. {
  407. fscrypt_warn(NULL, "kernel doesn't support hardware-wrapped keys");
  408. return -EOPNOTSUPP;
  409. }
  410. static inline bool
  411. fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
  412. const struct fscrypt_info *ci)
  413. {
  414. return smp_load_acquire(&prep_key->tfm) != NULL;
  415. }
  416. #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
  417. /* keyring.c */
  418. /*
  419. * fscrypt_master_key_secret - secret key material of an in-use master key
  420. */
  421. struct fscrypt_master_key_secret {
  422. /*
  423. * The KDF with which subkeys of this key can be derived.
  424. *
  425. * For v1 policy keys, this isn't applicable and won't be set.
  426. * Otherwise, this KDF will be keyed by this master key if
  427. * ->is_hw_wrapped=false, or by the "software secret" that hardware
  428. * derived from this master key if ->is_hw_wrapped=true.
  429. */
  430. struct fscrypt_hkdf hkdf;
  431. /*
  432. * True if this key is a hardware-wrapped key; false if this key is a
  433. * standard key (i.e. a "software key"). For v1 policy keys this will
  434. * always be false, as v1 policy support is a legacy feature which
  435. * doesn't support newer functionality such as hardware-wrapped keys.
  436. */
  437. bool is_hw_wrapped;
  438. /*
  439. * Size of the raw key in bytes. This remains set even if ->raw was
  440. * zeroized due to no longer being needed. I.e. we still remember the
  441. * size of the key even if we don't need to remember the key itself.
  442. */
  443. u32 size;
  444. /*
  445. * The raw key which userspace provided, when still needed. This can be
  446. * either a standard key or a hardware-wrapped key, as indicated by
  447. * ->is_hw_wrapped. In the case of a standard, v2 policy key, there is
  448. * no need to remember the raw key separately from ->hkdf so this field
  449. * will be zeroized as soon as ->hkdf is initialized.
  450. */
  451. u8 raw[FSCRYPT_MAX_ANY_KEY_SIZE];
  452. } __randomize_layout;
  453. /*
  454. * fscrypt_master_key - an in-use master key
  455. *
  456. * This represents a master encryption key which has been added to the
  457. * filesystem and can be used to "unlock" the encrypted files which were
  458. * encrypted with it.
  459. */
  460. struct fscrypt_master_key {
  461. /*
  462. * Link in ->s_master_keys->key_hashtable.
  463. * Only valid if ->mk_active_refs > 0.
  464. */
  465. struct hlist_node mk_node;
  466. /* Semaphore that protects ->mk_secret and ->mk_users */
  467. struct rw_semaphore mk_sem;
  468. /*
  469. * Active and structural reference counts. An active ref guarantees
  470. * that the struct continues to exist, continues to be in the keyring
  471. * ->s_master_keys, and that any embedded subkeys (e.g.
  472. * ->mk_direct_keys) that have been prepared continue to exist.
  473. * A structural ref only guarantees that the struct continues to exist.
  474. *
  475. * There is one active ref associated with ->mk_secret being present,
  476. * and one active ref for each inode in ->mk_decrypted_inodes.
  477. *
  478. * There is one structural ref associated with the active refcount being
  479. * nonzero. Finding a key in the keyring also takes a structural ref,
  480. * which is then held temporarily while the key is operated on.
  481. */
  482. refcount_t mk_active_refs;
  483. refcount_t mk_struct_refs;
  484. struct rcu_head mk_rcu_head;
  485. /*
  486. * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is
  487. * executed, this is wiped and no new inodes can be unlocked with this
  488. * key; however, there may still be inodes in ->mk_decrypted_inodes
  489. * which could not be evicted. As long as some inodes still remain,
  490. * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
  491. * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
  492. *
  493. * While ->mk_secret is present, one ref in ->mk_active_refs is held.
  494. *
  495. * Locking: protected by ->mk_sem. The manipulation of ->mk_active_refs
  496. * associated with this field is protected by ->mk_sem as well.
  497. */
  498. struct fscrypt_master_key_secret mk_secret;
  499. /*
  500. * For v1 policy keys: an arbitrary key descriptor which was assigned by
  501. * userspace (->descriptor).
  502. *
  503. * For v2 policy keys: a cryptographic hash of this key (->identifier).
  504. */
  505. struct fscrypt_key_specifier mk_spec;
  506. /*
  507. * Keyring which contains a key of type 'key_type_fscrypt_user' for each
  508. * user who has added this key. Normally each key will be added by just
  509. * one user, but it's possible that multiple users share a key, and in
  510. * that case we need to keep track of those users so that one user can't
  511. * remove the key before the others want it removed too.
  512. *
  513. * This is NULL for v1 policy keys; those can only be added by root.
  514. *
  515. * Locking: protected by ->mk_sem. (We don't just rely on the keyrings
  516. * subsystem semaphore ->mk_users->sem, as we need support for atomic
  517. * search+insert along with proper synchronization with ->mk_secret.)
  518. */
  519. struct key *mk_users;
  520. /*
  521. * List of inodes that were unlocked using this key. This allows the
  522. * inodes to be evicted efficiently if the key is removed.
  523. */
  524. struct list_head mk_decrypted_inodes;
  525. spinlock_t mk_decrypted_inodes_lock;
  526. /*
  527. * Per-mode encryption keys for the various types of encryption policies
  528. * that use them. Allocated and derived on-demand.
  529. */
  530. struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
  531. struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
  532. struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
  533. /* Hash key for inode numbers. Initialized only when needed. */
  534. siphash_key_t mk_ino_hash_key;
  535. bool mk_ino_hash_key_initialized;
  536. } __randomize_layout;
  537. static inline bool
  538. is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
  539. {
  540. /*
  541. * The READ_ONCE() is only necessary for fscrypt_drop_inode().
  542. * fscrypt_drop_inode() runs in atomic context, so it can't take the key
  543. * semaphore and thus 'secret' can change concurrently which would be a
  544. * data race. But fscrypt_drop_inode() only need to know whether the
  545. * secret *was* present at the time of check, so READ_ONCE() suffices.
  546. */
  547. return READ_ONCE(secret->size) != 0;
  548. }
  549. static inline const char *master_key_spec_type(
  550. const struct fscrypt_key_specifier *spec)
  551. {
  552. switch (spec->type) {
  553. case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
  554. return "descriptor";
  555. case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
  556. return "identifier";
  557. }
  558. return "[unknown]";
  559. }
  560. static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
  561. {
  562. switch (spec->type) {
  563. case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
  564. return FSCRYPT_KEY_DESCRIPTOR_SIZE;
  565. case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
  566. return FSCRYPT_KEY_IDENTIFIER_SIZE;
  567. }
  568. return 0;
  569. }
  570. void fscrypt_put_master_key(struct fscrypt_master_key *mk);
  571. void fscrypt_put_master_key_activeref(struct super_block *sb,
  572. struct fscrypt_master_key *mk);
  573. struct fscrypt_master_key *
  574. fscrypt_find_master_key(struct super_block *sb,
  575. const struct fscrypt_key_specifier *mk_spec);
  576. int fscrypt_get_test_dummy_key_identifier(
  577. u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
  578. int fscrypt_add_test_dummy_key(struct super_block *sb,
  579. struct fscrypt_key_specifier *key_spec);
  580. int fscrypt_verify_key_added(struct super_block *sb,
  581. const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
  582. int __init fscrypt_init_keyring(void);
  583. /* keysetup.c */
  584. struct fscrypt_mode {
  585. const char *friendly_name;
  586. const char *cipher_str;
  587. int keysize; /* key size in bytes */
  588. int security_strength; /* security strength in bytes */
  589. int ivsize; /* IV size in bytes */
  590. int logged_cryptoapi_impl;
  591. int logged_blk_crypto_native;
  592. int logged_blk_crypto_fallback;
  593. enum blk_crypto_mode_num blk_crypto_mode;
  594. };
  595. extern struct fscrypt_mode fscrypt_modes[];
  596. int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
  597. const u8 *raw_key, const struct fscrypt_info *ci);
  598. void fscrypt_destroy_prepared_key(struct super_block *sb,
  599. struct fscrypt_prepared_key *prep_key);
  600. int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
  601. int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
  602. const struct fscrypt_master_key *mk);
  603. void fscrypt_hash_inode_number(struct fscrypt_info *ci,
  604. const struct fscrypt_master_key *mk);
  605. int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
  606. /**
  607. * fscrypt_require_key() - require an inode's encryption key
  608. * @inode: the inode we need the key for
  609. *
  610. * If the inode is encrypted, set up its encryption key if not already done.
  611. * Then require that the key be present and return -ENOKEY otherwise.
  612. *
  613. * No locks are needed, and the key will live as long as the struct inode --- so
  614. * it won't go away from under you.
  615. *
  616. * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
  617. * if a problem occurred while setting up the encryption key.
  618. */
  619. static inline int fscrypt_require_key(struct inode *inode)
  620. {
  621. if (IS_ENCRYPTED(inode)) {
  622. int err = fscrypt_get_encryption_info(inode, false);
  623. if (err)
  624. return err;
  625. if (!fscrypt_has_encryption_key(inode))
  626. return -ENOKEY;
  627. }
  628. return 0;
  629. }
  630. /* keysetup_v1.c */
  631. void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
  632. int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
  633. const u8 *raw_master_key);
  634. int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci);
  635. /* policy.c */
  636. bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
  637. const union fscrypt_policy *policy2);
  638. int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
  639. struct fscrypt_key_specifier *key_spec);
  640. const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb);
  641. bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
  642. const struct inode *inode);
  643. int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
  644. const union fscrypt_context *ctx_u,
  645. int ctx_size);
  646. const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
  647. #endif /* _FSCRYPT_PRIVATE_H */