ecryptfs_kernel.h 24 KB

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