ecryptfs_kernel.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /**
  3. * eCryptfs: Linux filesystem encryption layer
  4. * Kernel declarations.
  5. *
  6. * Copyright (C) 1997-2003 Erez Zadok
  7. * Copyright (C) 2001-2003 Stony Brook University
  8. * Copyright (C) 2004-2008 International Business Machines Corp.
  9. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  10. * Trevor S. Highland <trevor.highland@gmail.com>
  11. * Tyler Hicks <code@tyhicks.com>
  12. */
  13. #ifndef ECRYPTFS_KERNEL_H
  14. #define ECRYPTFS_KERNEL_H
  15. #include <crypto/md5.h>
  16. #include <crypto/skcipher.h>
  17. #include <keys/user-type.h>
  18. #include <keys/encrypted-type.h>
  19. #include <linux/kernel.h>
  20. #include <linux/fs.h>
  21. #include <linux/fs_stack.h>
  22. #include <linux/hex.h>
  23. #include <linux/namei.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/hash.h>
  26. #include <linux/nsproxy.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/ecryptfs.h>
  29. #define ECRYPTFS_DEFAULT_IV_BYTES 16
  30. #define ECRYPTFS_DEFAULT_EXTENT_SIZE 4096
  31. #define ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE 8192
  32. #define ECRYPTFS_DEFAULT_MSG_CTX_ELEMS 32
  33. #define ECRYPTFS_DEFAULT_SEND_TIMEOUT HZ
  34. #define ECRYPTFS_MAX_MSG_CTX_TTL (HZ*3)
  35. #define ECRYPTFS_DEFAULT_NUM_USERS 4
  36. #define ECRYPTFS_MAX_NUM_USERS 32768
  37. #define ECRYPTFS_XATTR_NAME "user.ecryptfs"
  38. void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok);
  39. static inline void
  40. ecryptfs_to_hex(char *dst, char *src, size_t src_size)
  41. {
  42. char *end = bin2hex(dst, src, src_size);
  43. *end = '\0';
  44. }
  45. extern void ecryptfs_from_hex(char *dst, char *src, int dst_size);
  46. struct ecryptfs_key_record {
  47. unsigned char type;
  48. size_t enc_key_size;
  49. unsigned char sig[ECRYPTFS_SIG_SIZE];
  50. unsigned char enc_key[ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES];
  51. };
  52. struct ecryptfs_auth_tok_list {
  53. struct ecryptfs_auth_tok *auth_tok;
  54. struct list_head list;
  55. };
  56. struct ecryptfs_crypt_stat;
  57. struct ecryptfs_mount_crypt_stat;
  58. struct ecryptfs_page_crypt_context {
  59. struct page *page;
  60. #define ECRYPTFS_PREPARE_COMMIT_MODE 0
  61. #define ECRYPTFS_WRITEPAGE_MODE 1
  62. unsigned int mode;
  63. union {
  64. struct file *lower_file;
  65. struct writeback_control *wbc;
  66. } param;
  67. };
  68. #if defined(CONFIG_ENCRYPTED_KEYS) || defined(CONFIG_ENCRYPTED_KEYS_MODULE)
  69. static inline struct ecryptfs_auth_tok *
  70. ecryptfs_get_encrypted_key_payload_data(struct key *key)
  71. {
  72. struct encrypted_key_payload *payload;
  73. if (key->type != &key_type_encrypted)
  74. return NULL;
  75. payload = key->payload.data[0];
  76. if (!payload)
  77. return ERR_PTR(-EKEYREVOKED);
  78. return (struct ecryptfs_auth_tok *)payload->payload_data;
  79. }
  80. static inline struct key *ecryptfs_get_encrypted_key(char *sig)
  81. {
  82. return request_key(&key_type_encrypted, sig, NULL);
  83. }
  84. #else
  85. static inline struct ecryptfs_auth_tok *
  86. ecryptfs_get_encrypted_key_payload_data(struct key *key)
  87. {
  88. return NULL;
  89. }
  90. static inline struct key *ecryptfs_get_encrypted_key(char *sig)
  91. {
  92. return ERR_PTR(-ENOKEY);
  93. }
  94. #endif /* CONFIG_ENCRYPTED_KEYS */
  95. static inline struct ecryptfs_auth_tok *
  96. ecryptfs_get_key_payload_data(struct key *key)
  97. {
  98. struct ecryptfs_auth_tok *auth_tok;
  99. struct user_key_payload *ukp;
  100. auth_tok = ecryptfs_get_encrypted_key_payload_data(key);
  101. if (auth_tok)
  102. return auth_tok;
  103. ukp = user_key_payload_locked(key);
  104. if (!ukp)
  105. return ERR_PTR(-EKEYREVOKED);
  106. return (struct ecryptfs_auth_tok *)ukp->data;
  107. }
  108. #define ECRYPTFS_MAX_KEYSET_SIZE 1024
  109. #define ECRYPTFS_MAX_CIPHER_NAME_SIZE 31
  110. #define ECRYPTFS_MAX_NUM_ENC_KEYS 64
  111. #define ECRYPTFS_MAX_IV_BYTES 16 /* 128 bits */
  112. #define ECRYPTFS_SALT_BYTES 2
  113. #define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5
  114. #define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */
  115. #define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64))
  116. #define ECRYPTFS_SIZE_AND_MARKER_BYTES (ECRYPTFS_FILE_SIZE_BYTES \
  117. + MAGIC_ECRYPTFS_MARKER_SIZE_BYTES)
  118. #define ECRYPTFS_DEFAULT_CIPHER "aes"
  119. #define ECRYPTFS_DEFAULT_KEY_BYTES 16
  120. #define ECRYPTFS_TAG_1_PACKET_TYPE 0x01
  121. #define ECRYPTFS_TAG_3_PACKET_TYPE 0x8C
  122. #define ECRYPTFS_TAG_11_PACKET_TYPE 0xED
  123. #define ECRYPTFS_TAG_64_PACKET_TYPE 0x40
  124. #define ECRYPTFS_TAG_65_PACKET_TYPE 0x41
  125. #define ECRYPTFS_TAG_66_PACKET_TYPE 0x42
  126. #define ECRYPTFS_TAG_67_PACKET_TYPE 0x43
  127. #define ECRYPTFS_TAG_70_PACKET_TYPE 0x46 /* FNEK-encrypted filename
  128. * as dentry name */
  129. #define ECRYPTFS_TAG_71_PACKET_TYPE 0x47 /* FNEK-encrypted filename in
  130. * metadata */
  131. #define ECRYPTFS_TAG_72_PACKET_TYPE 0x48 /* FEK-encrypted filename as
  132. * dentry name */
  133. #define ECRYPTFS_TAG_73_PACKET_TYPE 0x49 /* FEK-encrypted filename as
  134. * metadata */
  135. #define ECRYPTFS_MIN_PKT_LEN_SIZE 1 /* Min size to specify packet length */
  136. #define ECRYPTFS_MAX_PKT_LEN_SIZE 2 /* Pass at least this many bytes to
  137. * ecryptfs_parse_packet_length() and
  138. * ecryptfs_write_packet_length()
  139. */
  140. /* Constraint: ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES >=
  141. * ECRYPTFS_MAX_IV_BYTES */
  142. #define ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 16
  143. #define ECRYPTFS_NON_NULL 0x42 /* A reasonable substitute for NULL */
  144. #define ECRYPTFS_TAG_70_MIN_METADATA_SIZE (1 + ECRYPTFS_MIN_PKT_LEN_SIZE \
  145. + ECRYPTFS_SIG_SIZE + 1 + 1)
  146. #define ECRYPTFS_TAG_70_MAX_METADATA_SIZE (1 + ECRYPTFS_MAX_PKT_LEN_SIZE \
  147. + ECRYPTFS_SIG_SIZE + 1 + 1)
  148. #define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FEK_ENCRYPTED."
  149. #define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE 23
  150. #define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FNEK_ENCRYPTED."
  151. #define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 24
  152. #define ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN (18 + 1 + 4 + 1 + 32)
  153. #ifdef CONFIG_ECRYPT_FS_MESSAGING
  154. # define ECRYPTFS_VERSIONING_MASK_MESSAGING (ECRYPTFS_VERSIONING_DEVMISC \
  155. | ECRYPTFS_VERSIONING_PUBKEY)
  156. #else
  157. # define ECRYPTFS_VERSIONING_MASK_MESSAGING 0
  158. #endif
  159. #define ECRYPTFS_VERSIONING_MASK (ECRYPTFS_VERSIONING_PASSPHRASE \
  160. | ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH \
  161. | ECRYPTFS_VERSIONING_XATTR \
  162. | ECRYPTFS_VERSIONING_MULTKEY \
  163. | ECRYPTFS_VERSIONING_MASK_MESSAGING \
  164. | ECRYPTFS_VERSIONING_FILENAME_ENCRYPTION)
  165. struct ecryptfs_key_sig {
  166. struct list_head crypt_stat_list;
  167. char keysig[ECRYPTFS_SIG_SIZE_HEX + 1];
  168. };
  169. struct ecryptfs_filename {
  170. struct list_head crypt_stat_list;
  171. #define ECRYPTFS_FILENAME_CONTAINS_DECRYPTED 0x00000001
  172. u32 flags;
  173. u32 seq_no;
  174. char *filename;
  175. char *encrypted_filename;
  176. size_t filename_size;
  177. size_t encrypted_filename_size;
  178. char fnek_sig[ECRYPTFS_SIG_SIZE_HEX];
  179. char dentry_name[ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN + 1];
  180. };
  181. /**
  182. * This is the primary struct associated with each encrypted file.
  183. *
  184. * TODO: cache align/pack?
  185. */
  186. struct ecryptfs_crypt_stat {
  187. #define ECRYPTFS_STRUCT_INITIALIZED 0x00000001
  188. #define ECRYPTFS_POLICY_APPLIED 0x00000002
  189. #define ECRYPTFS_ENCRYPTED 0x00000004
  190. #define ECRYPTFS_SECURITY_WARNING 0x00000008
  191. #define ECRYPTFS_ENABLE_HMAC 0x00000010
  192. #define ECRYPTFS_ENCRYPT_IV_PAGES 0x00000020
  193. #define ECRYPTFS_KEY_VALID 0x00000040
  194. #define ECRYPTFS_METADATA_IN_XATTR 0x00000080
  195. #define ECRYPTFS_VIEW_AS_ENCRYPTED 0x00000100
  196. #define ECRYPTFS_KEY_SET 0x00000200
  197. #define ECRYPTFS_ENCRYPT_FILENAMES 0x00000400
  198. #define ECRYPTFS_ENCFN_USE_MOUNT_FNEK 0x00000800
  199. #define ECRYPTFS_ENCFN_USE_FEK 0x00001000
  200. #define ECRYPTFS_UNLINK_SIGS 0x00002000
  201. #define ECRYPTFS_I_SIZE_INITIALIZED 0x00004000
  202. u32 flags;
  203. unsigned int file_version;
  204. size_t iv_bytes;
  205. size_t metadata_size;
  206. size_t extent_size; /* Data extent size; default is 4096 */
  207. size_t key_size;
  208. size_t extent_shift;
  209. unsigned int extent_mask;
  210. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  211. struct crypto_skcipher *tfm;
  212. unsigned char cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
  213. unsigned char key[ECRYPTFS_MAX_KEY_BYTES];
  214. unsigned char root_iv[ECRYPTFS_MAX_IV_BYTES];
  215. struct list_head keysig_list;
  216. struct mutex keysig_list_mutex;
  217. struct mutex cs_tfm_mutex;
  218. struct mutex cs_mutex;
  219. };
  220. /* inode private data. */
  221. struct ecryptfs_inode_info {
  222. struct inode vfs_inode;
  223. struct inode *wii_inode;
  224. struct mutex lower_file_mutex;
  225. atomic_t lower_file_count;
  226. struct file *lower_file;
  227. struct ecryptfs_crypt_stat crypt_stat;
  228. };
  229. /**
  230. * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint
  231. * @flags: Status flags
  232. * @mount_crypt_stat_list: These auth_toks hang off the mount-wide
  233. * cryptographic context. Every time a new
  234. * inode comes into existence, eCryptfs copies
  235. * the auth_toks on that list to the set of
  236. * auth_toks on the inode's crypt_stat
  237. * @global_auth_tok_key: The key from the user's keyring for the sig
  238. * @global_auth_tok: The key contents
  239. * @sig: The key identifier
  240. *
  241. * ecryptfs_global_auth_tok structs refer to authentication token keys
  242. * in the user keyring that apply to newly created files. A list of
  243. * these objects hangs off of the mount_crypt_stat struct for any
  244. * given eCryptfs mount. This struct maintains a reference to both the
  245. * key contents and the key itself so that the key can be put on
  246. * unmount.
  247. */
  248. struct ecryptfs_global_auth_tok {
  249. #define ECRYPTFS_AUTH_TOK_INVALID 0x00000001
  250. #define ECRYPTFS_AUTH_TOK_FNEK 0x00000002
  251. u32 flags;
  252. struct list_head mount_crypt_stat_list;
  253. struct key *global_auth_tok_key;
  254. unsigned char sig[ECRYPTFS_SIG_SIZE_HEX + 1];
  255. };
  256. /**
  257. * ecryptfs_key_tfm - Persistent key tfm
  258. * @key_tfm: crypto API handle to the key
  259. * @key_size: Key size in bytes
  260. * @key_tfm_mutex: Mutex to ensure only one operation in eCryptfs is
  261. * using the persistent TFM at any point in time
  262. * @key_tfm_list: Handle to hang this off the module-wide TFM list
  263. * @cipher_name: String name for the cipher for this TFM
  264. *
  265. * Typically, eCryptfs will use the same ciphers repeatedly throughout
  266. * the course of its operations. In order to avoid unnecessarily
  267. * destroying and initializing the same cipher repeatedly, eCryptfs
  268. * keeps a list of crypto API contexts around to use when needed.
  269. */
  270. struct ecryptfs_key_tfm {
  271. struct crypto_skcipher *key_tfm;
  272. size_t key_size;
  273. struct mutex key_tfm_mutex;
  274. struct list_head key_tfm_list;
  275. unsigned char cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
  276. };
  277. extern struct mutex key_tfm_list_mutex;
  278. /**
  279. * This struct is to enable a mount-wide passphrase/salt combo. This
  280. * is more or less a stopgap to provide similar functionality to other
  281. * crypto filesystems like EncFS or CFS until full policy support is
  282. * implemented in eCryptfs.
  283. */
  284. struct ecryptfs_mount_crypt_stat {
  285. /* Pointers to memory we do not own, do not free these */
  286. #define ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED 0x00000001
  287. #define ECRYPTFS_XATTR_METADATA_ENABLED 0x00000002
  288. #define ECRYPTFS_ENCRYPTED_VIEW_ENABLED 0x00000004
  289. #define ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED 0x00000008
  290. #define ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES 0x00000010
  291. #define ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK 0x00000020
  292. #define ECRYPTFS_GLOBAL_ENCFN_USE_FEK 0x00000040
  293. #define ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY 0x00000080
  294. u32 flags;
  295. struct list_head global_auth_tok_list;
  296. struct mutex global_auth_tok_list_mutex;
  297. size_t global_default_cipher_key_size;
  298. size_t global_default_fn_cipher_key_bytes;
  299. unsigned char global_default_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE
  300. + 1];
  301. unsigned char global_default_fn_cipher_name[
  302. ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
  303. char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX + 1];
  304. };
  305. /* superblock private data. */
  306. struct ecryptfs_sb_info {
  307. struct super_block *wsi_sb;
  308. struct vfsmount *lower_mnt;
  309. struct ecryptfs_mount_crypt_stat mount_crypt_stat;
  310. };
  311. /* file private data. */
  312. struct ecryptfs_file_info {
  313. struct file *wfi_file;
  314. struct ecryptfs_crypt_stat *crypt_stat;
  315. };
  316. /* auth_tok <=> encrypted_session_key mappings */
  317. struct ecryptfs_auth_tok_list_item {
  318. unsigned char encrypted_session_key[ECRYPTFS_MAX_KEY_BYTES];
  319. struct list_head list;
  320. struct ecryptfs_auth_tok auth_tok;
  321. };
  322. struct ecryptfs_message {
  323. /* Can never be greater than ecryptfs_message_buf_len */
  324. /* Used to find the parent msg_ctx */
  325. /* Inherits from msg_ctx->index */
  326. u32 index;
  327. u32 data_len;
  328. u8 data[] __counted_by(data_len);
  329. };
  330. struct ecryptfs_msg_ctx {
  331. #define ECRYPTFS_MSG_CTX_STATE_FREE 0x01
  332. #define ECRYPTFS_MSG_CTX_STATE_PENDING 0x02
  333. #define ECRYPTFS_MSG_CTX_STATE_DONE 0x03
  334. #define ECRYPTFS_MSG_CTX_STATE_NO_REPLY 0x04
  335. u8 state;
  336. #define ECRYPTFS_MSG_HELO 100
  337. #define ECRYPTFS_MSG_QUIT 101
  338. #define ECRYPTFS_MSG_REQUEST 102
  339. #define ECRYPTFS_MSG_RESPONSE 103
  340. u8 type;
  341. u32 index;
  342. /* Counter converts to a sequence number. Each message sent
  343. * out for which we expect a response has an associated
  344. * sequence number. The response must have the same sequence
  345. * number as the counter for the msg_stc for the message to be
  346. * valid. */
  347. u32 counter;
  348. size_t msg_size;
  349. struct ecryptfs_message *msg;
  350. struct task_struct *task;
  351. struct list_head node;
  352. struct list_head daemon_out_list;
  353. struct mutex mux;
  354. };
  355. struct ecryptfs_daemon {
  356. #define ECRYPTFS_DAEMON_IN_READ 0x00000001
  357. #define ECRYPTFS_DAEMON_IN_POLL 0x00000002
  358. #define ECRYPTFS_DAEMON_ZOMBIE 0x00000004
  359. #define ECRYPTFS_DAEMON_MISCDEV_OPEN 0x00000008
  360. u32 flags;
  361. u32 num_queued_msg_ctx;
  362. struct file *file;
  363. struct mutex mux;
  364. struct list_head msg_ctx_out_queue;
  365. wait_queue_head_t wait;
  366. struct hlist_node euid_chain;
  367. };
  368. #ifdef CONFIG_ECRYPT_FS_MESSAGING
  369. extern struct mutex ecryptfs_daemon_hash_mux;
  370. #endif
  371. static inline size_t
  372. ecryptfs_lower_header_size(struct ecryptfs_crypt_stat *crypt_stat)
  373. {
  374. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  375. return 0;
  376. return crypt_stat->metadata_size;
  377. }
  378. static inline struct ecryptfs_file_info *
  379. ecryptfs_file_to_private(struct file *file)
  380. {
  381. return file->private_data;
  382. }
  383. static inline void
  384. ecryptfs_set_file_private(struct file *file,
  385. struct ecryptfs_file_info *file_info)
  386. {
  387. file->private_data = file_info;
  388. }
  389. static inline struct file *ecryptfs_file_to_lower(struct file *file)
  390. {
  391. return ((struct ecryptfs_file_info *)file->private_data)->wfi_file;
  392. }
  393. static inline void
  394. ecryptfs_set_file_lower(struct file *file, struct file *lower_file)
  395. {
  396. ((struct ecryptfs_file_info *)file->private_data)->wfi_file =
  397. lower_file;
  398. }
  399. static inline struct ecryptfs_inode_info *
  400. ecryptfs_inode_to_private(struct inode *inode)
  401. {
  402. return container_of(inode, struct ecryptfs_inode_info, vfs_inode);
  403. }
  404. static inline struct inode *ecryptfs_inode_to_lower(struct inode *inode)
  405. {
  406. return ecryptfs_inode_to_private(inode)->wii_inode;
  407. }
  408. static inline void
  409. ecryptfs_set_inode_lower(struct inode *inode, struct inode *lower_inode)
  410. {
  411. ecryptfs_inode_to_private(inode)->wii_inode = lower_inode;
  412. }
  413. static inline struct ecryptfs_sb_info *
  414. ecryptfs_superblock_to_private(struct super_block *sb)
  415. {
  416. return (struct ecryptfs_sb_info *)sb->s_fs_info;
  417. }
  418. static inline void
  419. ecryptfs_set_superblock_private(struct super_block *sb,
  420. struct ecryptfs_sb_info *sb_info)
  421. {
  422. sb->s_fs_info = sb_info;
  423. }
  424. static inline struct super_block *
  425. ecryptfs_superblock_to_lower(struct super_block *sb)
  426. {
  427. return ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb;
  428. }
  429. static inline void
  430. ecryptfs_set_superblock_lower(struct super_block *sb,
  431. struct super_block *lower_sb)
  432. {
  433. ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb = lower_sb;
  434. }
  435. static inline void
  436. ecryptfs_set_dentry_lower(struct dentry *dentry,
  437. struct dentry *lower_dentry)
  438. {
  439. dentry->d_fsdata = lower_dentry;
  440. }
  441. static inline struct dentry *
  442. ecryptfs_dentry_to_lower(struct dentry *dentry)
  443. {
  444. return dentry->d_fsdata;
  445. }
  446. static inline struct path
  447. ecryptfs_lower_path(struct dentry *dentry)
  448. {
  449. return (struct path){
  450. .mnt = ecryptfs_superblock_to_private(dentry->d_sb)->lower_mnt,
  451. .dentry = ecryptfs_dentry_to_lower(dentry)
  452. };
  453. }
  454. #define ecryptfs_printk(type, fmt, arg...) \
  455. __ecryptfs_printk(type "%s: " fmt, __func__, ## arg)
  456. __printf(1, 2)
  457. void __ecryptfs_printk(const char *fmt, ...);
  458. extern const struct file_operations ecryptfs_main_fops;
  459. extern const struct file_operations ecryptfs_dir_fops;
  460. extern const struct inode_operations ecryptfs_main_iops;
  461. extern const struct inode_operations ecryptfs_dir_iops;
  462. extern const struct inode_operations ecryptfs_symlink_iops;
  463. extern const struct super_operations ecryptfs_sops;
  464. extern const struct dentry_operations ecryptfs_dops;
  465. extern const struct address_space_operations ecryptfs_aops;
  466. extern int ecryptfs_verbosity;
  467. extern unsigned int ecryptfs_message_buf_len;
  468. extern signed long ecryptfs_message_wait_timeout;
  469. extern unsigned int ecryptfs_number_of_users;
  470. extern struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
  471. extern struct kmem_cache *ecryptfs_file_info_cache;
  472. extern struct kmem_cache *ecryptfs_inode_info_cache;
  473. extern struct kmem_cache *ecryptfs_sb_info_cache;
  474. extern struct kmem_cache *ecryptfs_header_cache;
  475. extern struct kmem_cache *ecryptfs_xattr_cache;
  476. extern struct kmem_cache *ecryptfs_key_record_cache;
  477. extern struct kmem_cache *ecryptfs_key_sig_cache;
  478. extern struct kmem_cache *ecryptfs_global_auth_tok_cache;
  479. extern struct kmem_cache *ecryptfs_key_tfm_cache;
  480. struct inode *ecryptfs_get_inode(struct inode *lower_inode,
  481. struct super_block *sb);
  482. void ecryptfs_i_size_init(const char *page_virt, struct inode *inode);
  483. int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
  484. struct inode *ecryptfs_inode);
  485. int ecryptfs_decode_and_decrypt_filename(char **decrypted_name,
  486. size_t *decrypted_name_size,
  487. struct super_block *sb,
  488. const char *name, size_t name_size);
  489. int ecryptfs_encrypt_and_encode_filename(
  490. char **encoded_name,
  491. size_t *encoded_name_size,
  492. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  493. const char *name, size_t name_size);
  494. void ecryptfs_dump_hex(char *data, int bytes);
  495. int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
  496. int sg_size);
  497. int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat);
  498. void ecryptfs_rotate_iv(unsigned char *iv);
  499. void ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
  500. void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
  501. void ecryptfs_destroy_mount_crypt_stat(
  502. struct ecryptfs_mount_crypt_stat *mount_crypt_stat);
  503. int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat);
  504. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode);
  505. int ecryptfs_encrypt_page(struct folio *folio);
  506. int ecryptfs_decrypt_page(struct folio *folio);
  507. int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
  508. struct inode *ecryptfs_inode);
  509. int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry);
  510. int ecryptfs_new_file_context(struct inode *ecryptfs_inode);
  511. void ecryptfs_write_crypt_stat_flags(char *page_virt,
  512. struct ecryptfs_crypt_stat *crypt_stat,
  513. size_t *written);
  514. int ecryptfs_read_and_validate_header_region(struct inode *inode);
  515. int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
  516. struct inode *inode);
  517. u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes);
  518. int ecryptfs_cipher_code_to_string(char *str, size_t size, u8 cipher_code);
  519. void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat);
  520. int ecryptfs_generate_key_packet_set(char *dest_base,
  521. struct ecryptfs_crypt_stat *crypt_stat,
  522. struct dentry *ecryptfs_dentry,
  523. size_t *len, size_t max);
  524. int
  525. ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  526. unsigned char *src, struct dentry *ecryptfs_dentry);
  527. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length);
  528. ssize_t
  529. ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
  530. const char *name, void *value, size_t size);
  531. int
  532. ecryptfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
  533. const void *value, size_t size, int flags);
  534. int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode);
  535. #ifdef CONFIG_ECRYPT_FS_MESSAGING
  536. int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
  537. struct ecryptfs_message *msg, u32 seq);
  538. int ecryptfs_send_message(char *data, int data_len,
  539. struct ecryptfs_msg_ctx **msg_ctx);
  540. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  541. struct ecryptfs_message **emsg);
  542. int ecryptfs_init_messaging(void);
  543. void ecryptfs_release_messaging(void);
  544. #else
  545. static inline int ecryptfs_init_messaging(void)
  546. {
  547. return 0;
  548. }
  549. static inline void ecryptfs_release_messaging(void)
  550. { }
  551. static inline int ecryptfs_send_message(char *data, int data_len,
  552. struct ecryptfs_msg_ctx **msg_ctx)
  553. {
  554. return -ENOTCONN;
  555. }
  556. static inline int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  557. struct ecryptfs_message **emsg)
  558. {
  559. return -ENOMSG;
  560. }
  561. #endif
  562. void
  563. ecryptfs_write_header_metadata(char *virt,
  564. struct ecryptfs_crypt_stat *crypt_stat,
  565. size_t *written);
  566. int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig);
  567. int
  568. ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  569. char *sig, u32 global_auth_tok_flags);
  570. int ecryptfs_get_global_auth_tok_for_sig(
  571. struct ecryptfs_global_auth_tok **global_auth_tok,
  572. struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig);
  573. int
  574. ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
  575. size_t key_size);
  576. int ecryptfs_init_crypto(void);
  577. int ecryptfs_destroy_crypto(void);
  578. int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm);
  579. int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
  580. struct mutex **tfm_mutex,
  581. char *cipher_name);
  582. int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
  583. struct ecryptfs_auth_tok **auth_tok,
  584. char *sig);
  585. int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
  586. loff_t offset, size_t size);
  587. int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
  588. struct folio *folio_for_lower,
  589. size_t offset_in_page, size_t size);
  590. int ecryptfs_write(struct inode *inode, char *data, loff_t offset, size_t size);
  591. int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
  592. struct inode *ecryptfs_inode);
  593. int ecryptfs_read_lower_page_segment(struct folio *folio_for_ecryptfs,
  594. pgoff_t page_index,
  595. size_t offset_in_page, size_t size,
  596. struct inode *ecryptfs_inode);
  597. int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
  598. size_t *length_size);
  599. int ecryptfs_write_packet_length(char *dest, size_t size,
  600. size_t *packet_size_length);
  601. #ifdef CONFIG_ECRYPT_FS_MESSAGING
  602. int ecryptfs_init_ecryptfs_miscdev(void);
  603. void ecryptfs_destroy_ecryptfs_miscdev(void);
  604. int ecryptfs_send_miscdev(char *data, size_t data_size,
  605. struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
  606. u16 msg_flags, struct ecryptfs_daemon *daemon);
  607. void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);
  608. int
  609. ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file);
  610. int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon);
  611. int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon);
  612. #endif
  613. int ecryptfs_init_kthread(void);
  614. void ecryptfs_destroy_kthread(void);
  615. int ecryptfs_privileged_open(struct file **lower_file,
  616. struct dentry *lower_dentry,
  617. struct vfsmount *lower_mnt,
  618. const struct cred *cred);
  619. int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode);
  620. void ecryptfs_put_lower_file(struct inode *inode);
  621. int
  622. ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
  623. size_t *packet_size,
  624. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  625. char *filename, size_t filename_size);
  626. int
  627. ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
  628. size_t *packet_size,
  629. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  630. char *data, size_t max_packet_size);
  631. int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
  632. struct ecryptfs_mount_crypt_stat *mount_crypt_stat);
  633. void ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
  634. loff_t offset);
  635. extern const struct xattr_handler * const ecryptfs_xattr_handlers[];
  636. #endif /* #ifndef ECRYPTFS_KERNEL_H */