blob: bb8ec5d4301c07b6775bef03af3075ff23605ebe [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * Kernel declarations.
4 *
5 * Copyright (C) 1997-2003 Erez Zadok
6 * Copyright (C) 2001-2003 Stony Brook University
Michael Halcrowf66e8832008-04-29 00:59:51 -07007 * Copyright (C) 2004-2008 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07008 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
Michael Halcrow88b4a072007-02-12 00:53:43 -08009 * Trevor S. Highland <trevor.highland@gmail.com>
10 * Tyler Hicks <tyhicks@ou.edu>
Michael Halcrow237fead2006-10-04 02:16:22 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
28#ifndef ECRYPTFS_KERNEL_H
29#define ECRYPTFS_KERNEL_H
30
31#include <keys/user-type.h>
32#include <linux/fs.h>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080033#include <linux/fs_stack.h>
Josef "Jeff" Sipekb65d34f2006-12-08 02:36:34 -080034#include <linux/namei.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070035#include <linux/scatterlist.h>
Michael Halcrowdddfa462007-02-12 00:53:44 -080036#include <linux/hash.h>
Michael Halcrow6a3fd922008-04-29 00:59:52 -070037#include <linux/nsproxy.h>
Jens Axboe9df9c8b2010-04-22 12:22:04 +020038#include <linux/backing-dev.h>
Roberto Sassuf8f85272011-06-27 13:45:43 +020039#include <linux/ecryptfs.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070040
Michael Halcrow237fead2006-10-04 02:16:22 -070041#define ECRYPTFS_DEFAULT_IV_BYTES 16
Michael Halcrow237fead2006-10-04 02:16:22 -070042#define ECRYPTFS_DEFAULT_EXTENT_SIZE 4096
43#define ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE 8192
Michael Halcrow88b4a072007-02-12 00:53:43 -080044#define ECRYPTFS_DEFAULT_MSG_CTX_ELEMS 32
45#define ECRYPTFS_DEFAULT_SEND_TIMEOUT HZ
46#define ECRYPTFS_MAX_MSG_CTX_TTL (HZ*3)
Michael Halcrow88b4a072007-02-12 00:53:43 -080047#define ECRYPTFS_DEFAULT_NUM_USERS 4
48#define ECRYPTFS_MAX_NUM_USERS 32768
Michael Halcrowdd2a3b72007-02-12 00:53:46 -080049#define ECRYPTFS_XATTR_NAME "user.ecryptfs"
Michael Halcrow237fead2006-10-04 02:16:22 -070050
Michael Halcrow237fead2006-10-04 02:16:22 -070051void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok);
52extern void ecryptfs_to_hex(char *dst, char *src, size_t src_size);
53extern void ecryptfs_from_hex(char *dst, char *src, int dst_size);
54
55struct ecryptfs_key_record {
56 unsigned char type;
57 size_t enc_key_size;
58 unsigned char sig[ECRYPTFS_SIG_SIZE];
59 unsigned char enc_key[ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES];
60};
61
62struct ecryptfs_auth_tok_list {
63 struct ecryptfs_auth_tok *auth_tok;
64 struct list_head list;
65};
66
67struct ecryptfs_crypt_stat;
68struct ecryptfs_mount_crypt_stat;
69
70struct ecryptfs_page_crypt_context {
71 struct page *page;
72#define ECRYPTFS_PREPARE_COMMIT_MODE 0
73#define ECRYPTFS_WRITEPAGE_MODE 1
74 unsigned int mode;
75 union {
76 struct file *lower_file;
77 struct writeback_control *wbc;
78 } param;
79};
80
81static inline struct ecryptfs_auth_tok *
82ecryptfs_get_key_payload_data(struct key *key)
83{
84 return (struct ecryptfs_auth_tok *)
85 (((struct user_key_payload*)key->payload.data)->data);
86}
87
Michael Halcrow237fead2006-10-04 02:16:22 -070088#define ECRYPTFS_MAX_KEYSET_SIZE 1024
89#define ECRYPTFS_MAX_CIPHER_NAME_SIZE 32
90#define ECRYPTFS_MAX_NUM_ENC_KEYS 64
Michael Halcrow237fead2006-10-04 02:16:22 -070091#define ECRYPTFS_MAX_IV_BYTES 16 /* 128 bits */
92#define ECRYPTFS_SALT_BYTES 2
93#define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5
94#define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */
Michael Halcrow45eaab72007-10-16 01:28:05 -070095#define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64))
Tyler Hicks778aeb42011-05-24 04:56:23 -050096#define ECRYPTFS_SIZE_AND_MARKER_BYTES (ECRYPTFS_FILE_SIZE_BYTES \
97 + MAGIC_ECRYPTFS_MARKER_SIZE_BYTES)
Michael Halcrow237fead2006-10-04 02:16:22 -070098#define ECRYPTFS_DEFAULT_CIPHER "aes"
99#define ECRYPTFS_DEFAULT_KEY_BYTES 16
Michael Halcrow565d9722006-10-30 22:07:17 -0800100#define ECRYPTFS_DEFAULT_HASH "md5"
Michael Halcrow9c79f342009-01-06 14:41:57 -0800101#define ECRYPTFS_TAG_70_DIGEST ECRYPTFS_DEFAULT_HASH
Michael Halcrow88b4a072007-02-12 00:53:43 -0800102#define ECRYPTFS_TAG_1_PACKET_TYPE 0x01
Michael Halcrow237fead2006-10-04 02:16:22 -0700103#define ECRYPTFS_TAG_3_PACKET_TYPE 0x8C
104#define ECRYPTFS_TAG_11_PACKET_TYPE 0xED
Michael Halcrow88b4a072007-02-12 00:53:43 -0800105#define ECRYPTFS_TAG_64_PACKET_TYPE 0x40
106#define ECRYPTFS_TAG_65_PACKET_TYPE 0x41
107#define ECRYPTFS_TAG_66_PACKET_TYPE 0x42
108#define ECRYPTFS_TAG_67_PACKET_TYPE 0x43
Michael Halcrow9c79f342009-01-06 14:41:57 -0800109#define ECRYPTFS_TAG_70_PACKET_TYPE 0x46 /* FNEK-encrypted filename
110 * as dentry name */
111#define ECRYPTFS_TAG_71_PACKET_TYPE 0x47 /* FNEK-encrypted filename in
112 * metadata */
113#define ECRYPTFS_TAG_72_PACKET_TYPE 0x48 /* FEK-encrypted filename as
114 * dentry name */
115#define ECRYPTFS_TAG_73_PACKET_TYPE 0x49 /* FEK-encrypted filename as
116 * metadata */
117/* Constraint: ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES >=
118 * ECRYPTFS_MAX_IV_BYTES */
119#define ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 16
120#define ECRYPTFS_NON_NULL 0x42 /* A reasonable substitute for NULL */
Michael Halcrow237fead2006-10-04 02:16:22 -0700121#define MD5_DIGEST_SIZE 16
Michael Halcrow9c79f342009-01-06 14:41:57 -0800122#define ECRYPTFS_TAG_70_DIGEST_SIZE MD5_DIGEST_SIZE
123#define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FEK_ENCRYPTED."
124#define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE 23
125#define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX "ECRYPTFS_FNEK_ENCRYPTED."
126#define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE 24
127#define ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN (18 + 1 + 4 + 1 + 32)
Michael Halcrow237fead2006-10-04 02:16:22 -0700128
Michael Halcrowf4aad162007-10-16 01:27:53 -0700129struct ecryptfs_key_sig {
130 struct list_head crypt_stat_list;
Roberto Sassu7762e232011-03-21 16:00:52 +0100131 char keysig[ECRYPTFS_SIG_SIZE_HEX + 1];
Michael Halcrowf4aad162007-10-16 01:27:53 -0700132};
133
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800134struct ecryptfs_filename {
135 struct list_head crypt_stat_list;
136#define ECRYPTFS_FILENAME_CONTAINS_DECRYPTED 0x00000001
137 u32 flags;
138 u32 seq_no;
139 char *filename;
140 char *encrypted_filename;
141 size_t filename_size;
142 size_t encrypted_filename_size;
143 char fnek_sig[ECRYPTFS_SIG_SIZE_HEX];
144 char dentry_name[ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN + 1];
145};
146
Michael Halcrow237fead2006-10-04 02:16:22 -0700147/**
148 * This is the primary struct associated with each encrypted file.
149 *
150 * TODO: cache align/pack?
151 */
152struct ecryptfs_crypt_stat {
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800153#define ECRYPTFS_STRUCT_INITIALIZED 0x00000001
154#define ECRYPTFS_POLICY_APPLIED 0x00000002
Tyler Hicksfed88592011-02-23 00:54:20 -0600155#define ECRYPTFS_ENCRYPTED 0x00000004
156#define ECRYPTFS_SECURITY_WARNING 0x00000008
157#define ECRYPTFS_ENABLE_HMAC 0x00000010
158#define ECRYPTFS_ENCRYPT_IV_PAGES 0x00000020
159#define ECRYPTFS_KEY_VALID 0x00000040
160#define ECRYPTFS_METADATA_IN_XATTR 0x00000080
161#define ECRYPTFS_VIEW_AS_ENCRYPTED 0x00000100
162#define ECRYPTFS_KEY_SET 0x00000200
163#define ECRYPTFS_ENCRYPT_FILENAMES 0x00000400
164#define ECRYPTFS_ENCFN_USE_MOUNT_FNEK 0x00000800
165#define ECRYPTFS_ENCFN_USE_FEK 0x00001000
166#define ECRYPTFS_UNLINK_SIGS 0x00002000
Tyler Hicks3aeb86e2011-03-15 14:54:00 -0500167#define ECRYPTFS_I_SIZE_INITIALIZED 0x00004000
Michael Halcrow237fead2006-10-04 02:16:22 -0700168 u32 flags;
169 unsigned int file_version;
170 size_t iv_bytes;
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600171 size_t metadata_size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700172 size_t extent_size; /* Data extent size; default is 4096 */
173 size_t key_size;
174 size_t extent_shift;
175 unsigned int extent_mask;
176 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrow8bba0662006-10-30 22:07:18 -0800177 struct crypto_blkcipher *tfm;
Michael Halcrow565d9722006-10-30 22:07:17 -0800178 struct crypto_hash *hash_tfm; /* Crypto context for generating
179 * the initialization vectors */
Michael Halcrow237fead2006-10-04 02:16:22 -0700180 unsigned char cipher[ECRYPTFS_MAX_CIPHER_NAME_SIZE];
181 unsigned char key[ECRYPTFS_MAX_KEY_BYTES];
182 unsigned char root_iv[ECRYPTFS_MAX_IV_BYTES];
Michael Halcrowf4aad162007-10-16 01:27:53 -0700183 struct list_head keysig_list;
184 struct mutex keysig_list_mutex;
Michael Halcrow237fead2006-10-04 02:16:22 -0700185 struct mutex cs_tfm_mutex;
Michael Halcrow565d9722006-10-30 22:07:17 -0800186 struct mutex cs_hash_tfm_mutex;
Michael Halcrow237fead2006-10-04 02:16:22 -0700187 struct mutex cs_mutex;
188};
189
190/* inode private data. */
191struct ecryptfs_inode_info {
192 struct inode vfs_inode;
193 struct inode *wii_inode;
Tyler Hicks332ab162011-04-14 15:35:11 -0500194 struct mutex lower_file_mutex;
195 atomic_t lower_file_count;
Michael Halcrowda0102a2007-10-16 01:28:07 -0700196 struct file *lower_file;
Michael Halcrow237fead2006-10-04 02:16:22 -0700197 struct ecryptfs_crypt_stat crypt_stat;
198};
199
200/* dentry private data. Each dentry must keep track of a lower
201 * vfsmount too. */
202struct ecryptfs_dentry_info {
Josef "Jeff" Sipekb65d34f2006-12-08 02:36:34 -0800203 struct path lower_path;
Michael Halcrow237fead2006-10-04 02:16:22 -0700204 struct ecryptfs_crypt_stat *crypt_stat;
205};
206
Michael Halcrow6c6f57f2007-10-16 01:28:01 -0700207/**
Michael Halcrow45eaab72007-10-16 01:28:05 -0700208 * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint
209 * @flags: Status flags
210 * @mount_crypt_stat_list: These auth_toks hang off the mount-wide
211 * cryptographic context. Every time a new
212 * inode comes into existence, eCryptfs copies
213 * the auth_toks on that list to the set of
214 * auth_toks on the inode's crypt_stat
215 * @global_auth_tok_key: The key from the user's keyring for the sig
216 * @global_auth_tok: The key contents
217 * @sig: The key identifier
218 *
Michael Halcrow6c6f57f2007-10-16 01:28:01 -0700219 * ecryptfs_global_auth_tok structs refer to authentication token keys
220 * in the user keyring that apply to newly created files. A list of
221 * these objects hangs off of the mount_crypt_stat struct for any
222 * given eCryptfs mount. This struct maintains a reference to both the
223 * key contents and the key itself so that the key can be put on
224 * unmount.
225 */
Michael Halcrowf4aad162007-10-16 01:27:53 -0700226struct ecryptfs_global_auth_tok {
227#define ECRYPTFS_AUTH_TOK_INVALID 0x00000001
Tyler Hicks84814d62009-03-13 13:51:59 -0700228#define ECRYPTFS_AUTH_TOK_FNEK 0x00000002
Michael Halcrowf4aad162007-10-16 01:27:53 -0700229 u32 flags;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700230 struct list_head mount_crypt_stat_list;
231 struct key *global_auth_tok_key;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700232 unsigned char sig[ECRYPTFS_SIG_SIZE_HEX + 1];
Michael Halcrowf4aad162007-10-16 01:27:53 -0700233};
234
Michael Halcrow6c6f57f2007-10-16 01:28:01 -0700235/**
Michael Halcrow45eaab72007-10-16 01:28:05 -0700236 * ecryptfs_key_tfm - Persistent key tfm
237 * @key_tfm: crypto API handle to the key
238 * @key_size: Key size in bytes
239 * @key_tfm_mutex: Mutex to ensure only one operation in eCryptfs is
240 * using the persistent TFM at any point in time
241 * @key_tfm_list: Handle to hang this off the module-wide TFM list
242 * @cipher_name: String name for the cipher for this TFM
243 *
Michael Halcrow6c6f57f2007-10-16 01:28:01 -0700244 * Typically, eCryptfs will use the same ciphers repeatedly throughout
245 * the course of its operations. In order to avoid unnecessarily
246 * destroying and initializing the same cipher repeatedly, eCryptfs
247 * keeps a list of crypto API contexts around to use when needed.
248 */
Michael Halcrowf4aad162007-10-16 01:27:53 -0700249struct ecryptfs_key_tfm {
250 struct crypto_blkcipher *key_tfm;
251 size_t key_size;
252 struct mutex key_tfm_mutex;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700253 struct list_head key_tfm_list;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700254 unsigned char cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
255};
256
Eric Sandeenaf440f52008-02-06 01:38:37 -0800257extern struct mutex key_tfm_list_mutex;
258
Michael Halcrow237fead2006-10-04 02:16:22 -0700259/**
260 * This struct is to enable a mount-wide passphrase/salt combo. This
261 * is more or less a stopgap to provide similar functionality to other
262 * crypto filesystems like EncFS or CFS until full policy support is
263 * implemented in eCryptfs.
264 */
265struct ecryptfs_mount_crypt_stat {
266 /* Pointers to memory we do not own, do not free these */
267#define ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED 0x00000001
Michael Halcrow17398952007-02-12 00:53:45 -0800268#define ECRYPTFS_XATTR_METADATA_ENABLED 0x00000002
269#define ECRYPTFS_ENCRYPTED_VIEW_ENABLED 0x00000004
Michael Halcrowf4aad162007-10-16 01:27:53 -0700270#define ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED 0x00000008
Michael Halcrow9c79f342009-01-06 14:41:57 -0800271#define ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES 0x00000010
272#define ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK 0x00000020
273#define ECRYPTFS_GLOBAL_ENCFN_USE_FEK 0x00000040
Roberto Sassuf16feb52010-10-06 18:31:32 +0200274#define ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY 0x00000080
Michael Halcrow237fead2006-10-04 02:16:22 -0700275 u32 flags;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700276 struct list_head global_auth_tok_list;
277 struct mutex global_auth_tok_list_mutex;
Michael Halcrow237fead2006-10-04 02:16:22 -0700278 size_t global_default_cipher_key_size;
Michael Halcrow9c79f342009-01-06 14:41:57 -0800279 size_t global_default_fn_cipher_key_bytes;
Michael Halcrow237fead2006-10-04 02:16:22 -0700280 unsigned char global_default_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE
281 + 1];
Michael Halcrow9c79f342009-01-06 14:41:57 -0800282 unsigned char global_default_fn_cipher_name[
283 ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
284 char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX + 1];
Michael Halcrow237fead2006-10-04 02:16:22 -0700285};
286
287/* superblock private data. */
288struct ecryptfs_sb_info {
289 struct super_block *wsi_sb;
290 struct ecryptfs_mount_crypt_stat mount_crypt_stat;
Jens Axboe9df9c8b2010-04-22 12:22:04 +0200291 struct backing_dev_info bdi;
Michael Halcrow237fead2006-10-04 02:16:22 -0700292};
293
294/* file private data. */
295struct ecryptfs_file_info {
296 struct file *wfi_file;
297 struct ecryptfs_crypt_stat *crypt_stat;
298};
299
300/* auth_tok <=> encrypted_session_key mappings */
301struct ecryptfs_auth_tok_list_item {
302 unsigned char encrypted_session_key[ECRYPTFS_MAX_KEY_BYTES];
303 struct list_head list;
304 struct ecryptfs_auth_tok auth_tok;
305};
306
Michael Halcrow88b4a072007-02-12 00:53:43 -0800307struct ecryptfs_message {
Michael Halcrowf66e8832008-04-29 00:59:51 -0700308 /* Can never be greater than ecryptfs_message_buf_len */
309 /* Used to find the parent msg_ctx */
310 /* Inherits from msg_ctx->index */
Michael Halcrow88b4a072007-02-12 00:53:43 -0800311 u32 index;
312 u32 data_len;
313 u8 data[];
314};
315
316struct ecryptfs_msg_ctx {
Michael Halcrowf66e8832008-04-29 00:59:51 -0700317#define ECRYPTFS_MSG_CTX_STATE_FREE 0x01
318#define ECRYPTFS_MSG_CTX_STATE_PENDING 0x02
319#define ECRYPTFS_MSG_CTX_STATE_DONE 0x03
320#define ECRYPTFS_MSG_CTX_STATE_NO_REPLY 0x04
321 u8 state;
322#define ECRYPTFS_MSG_HELO 100
323#define ECRYPTFS_MSG_QUIT 101
324#define ECRYPTFS_MSG_REQUEST 102
325#define ECRYPTFS_MSG_RESPONSE 103
326 u8 type;
327 u32 index;
328 /* Counter converts to a sequence number. Each message sent
329 * out for which we expect a response has an associated
330 * sequence number. The response must have the same sequence
331 * number as the counter for the msg_stc for the message to be
332 * valid. */
333 u32 counter;
334 size_t msg_size;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800335 struct ecryptfs_message *msg;
336 struct task_struct *task;
337 struct list_head node;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700338 struct list_head daemon_out_list;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800339 struct mutex mux;
340};
341
Michael Halcrowf66e8832008-04-29 00:59:51 -0700342struct ecryptfs_daemon;
343
344struct ecryptfs_daemon {
345#define ECRYPTFS_DAEMON_IN_READ 0x00000001
346#define ECRYPTFS_DAEMON_IN_POLL 0x00000002
347#define ECRYPTFS_DAEMON_ZOMBIE 0x00000004
348#define ECRYPTFS_DAEMON_MISCDEV_OPEN 0x00000008
349 u32 flags;
350 u32 num_queued_msg_ctx;
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700351 struct pid *pid;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700352 uid_t euid;
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700353 struct user_namespace *user_ns;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700354 struct task_struct *task;
355 struct mutex mux;
356 struct list_head msg_ctx_out_queue;
357 wait_queue_head_t wait;
358 struct hlist_node euid_chain;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800359};
360
Michael Halcrowf66e8832008-04-29 00:59:51 -0700361extern struct mutex ecryptfs_daemon_hash_mux;
362
Tyler Hicks157f1072010-02-11 07:10:38 -0600363static inline size_t
364ecryptfs_lower_header_size(struct ecryptfs_crypt_stat *crypt_stat)
365{
366 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
367 return 0;
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600368 return crypt_stat->metadata_size;
Tyler Hicks157f1072010-02-11 07:10:38 -0600369}
370
Michael Halcrow237fead2006-10-04 02:16:22 -0700371static inline struct ecryptfs_file_info *
372ecryptfs_file_to_private(struct file *file)
373{
Joe Perches0c6d7d52010-09-04 18:52:48 -0700374 return file->private_data;
Michael Halcrow237fead2006-10-04 02:16:22 -0700375}
376
377static inline void
378ecryptfs_set_file_private(struct file *file,
379 struct ecryptfs_file_info *file_info)
380{
381 file->private_data = file_info;
382}
383
384static inline struct file *ecryptfs_file_to_lower(struct file *file)
385{
386 return ((struct ecryptfs_file_info *)file->private_data)->wfi_file;
387}
388
389static inline void
390ecryptfs_set_file_lower(struct file *file, struct file *lower_file)
391{
392 ((struct ecryptfs_file_info *)file->private_data)->wfi_file =
393 lower_file;
394}
395
396static inline struct ecryptfs_inode_info *
397ecryptfs_inode_to_private(struct inode *inode)
398{
399 return container_of(inode, struct ecryptfs_inode_info, vfs_inode);
400}
401
402static inline struct inode *ecryptfs_inode_to_lower(struct inode *inode)
403{
404 return ecryptfs_inode_to_private(inode)->wii_inode;
405}
406
407static inline void
408ecryptfs_set_inode_lower(struct inode *inode, struct inode *lower_inode)
409{
410 ecryptfs_inode_to_private(inode)->wii_inode = lower_inode;
411}
412
413static inline struct ecryptfs_sb_info *
414ecryptfs_superblock_to_private(struct super_block *sb)
415{
416 return (struct ecryptfs_sb_info *)sb->s_fs_info;
417}
418
419static inline void
420ecryptfs_set_superblock_private(struct super_block *sb,
421 struct ecryptfs_sb_info *sb_info)
422{
423 sb->s_fs_info = sb_info;
424}
425
426static inline struct super_block *
427ecryptfs_superblock_to_lower(struct super_block *sb)
428{
429 return ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb;
430}
431
432static inline void
433ecryptfs_set_superblock_lower(struct super_block *sb,
434 struct super_block *lower_sb)
435{
436 ((struct ecryptfs_sb_info *)sb->s_fs_info)->wsi_sb = lower_sb;
437}
438
439static inline struct ecryptfs_dentry_info *
440ecryptfs_dentry_to_private(struct dentry *dentry)
441{
442 return (struct ecryptfs_dentry_info *)dentry->d_fsdata;
443}
444
445static inline void
446ecryptfs_set_dentry_private(struct dentry *dentry,
447 struct ecryptfs_dentry_info *dentry_info)
448{
449 dentry->d_fsdata = dentry_info;
450}
451
452static inline struct dentry *
453ecryptfs_dentry_to_lower(struct dentry *dentry)
454{
Josef "Jeff" Sipekb65d34f2006-12-08 02:36:34 -0800455 return ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700456}
457
458static inline void
459ecryptfs_set_dentry_lower(struct dentry *dentry, struct dentry *lower_dentry)
460{
Josef "Jeff" Sipekb65d34f2006-12-08 02:36:34 -0800461 ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.dentry =
Michael Halcrow237fead2006-10-04 02:16:22 -0700462 lower_dentry;
463}
464
465static inline struct vfsmount *
466ecryptfs_dentry_to_lower_mnt(struct dentry *dentry)
467{
Josef "Jeff" Sipekb65d34f2006-12-08 02:36:34 -0800468 return ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.mnt;
Michael Halcrow237fead2006-10-04 02:16:22 -0700469}
470
471static inline void
472ecryptfs_set_dentry_lower_mnt(struct dentry *dentry, struct vfsmount *lower_mnt)
473{
Josef "Jeff" Sipekb65d34f2006-12-08 02:36:34 -0800474 ((struct ecryptfs_dentry_info *)dentry->d_fsdata)->lower_path.mnt =
Michael Halcrow237fead2006-10-04 02:16:22 -0700475 lower_mnt;
476}
477
478#define ecryptfs_printk(type, fmt, arg...) \
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700479 __ecryptfs_printk(type "%s: " fmt, __func__, ## arg);
Joe Perches888d57b2010-11-10 15:46:16 -0800480__attribute__ ((format(printf, 1, 2)))
Michael Halcrow237fead2006-10-04 02:16:22 -0700481void __ecryptfs_printk(const char *fmt, ...);
482
483extern const struct file_operations ecryptfs_main_fops;
484extern const struct file_operations ecryptfs_dir_fops;
Arjan van de Ven754661f2007-02-12 00:55:38 -0800485extern const struct inode_operations ecryptfs_main_iops;
486extern const struct inode_operations ecryptfs_dir_iops;
487extern const struct inode_operations ecryptfs_symlink_iops;
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800488extern const struct super_operations ecryptfs_sops;
Al Viro5a3fd052009-02-20 05:57:52 +0000489extern const struct dentry_operations ecryptfs_dops;
Alexey Dobriyan7f094102009-09-21 17:01:10 -0700490extern const struct address_space_operations ecryptfs_aops;
Michael Halcrow237fead2006-10-04 02:16:22 -0700491extern int ecryptfs_verbosity;
Michael Halcrow88b4a072007-02-12 00:53:43 -0800492extern unsigned int ecryptfs_message_buf_len;
493extern signed long ecryptfs_message_wait_timeout;
494extern unsigned int ecryptfs_number_of_users;
Michael Halcrow237fead2006-10-04 02:16:22 -0700495
496extern struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
497extern struct kmem_cache *ecryptfs_file_info_cache;
498extern struct kmem_cache *ecryptfs_dentry_info_cache;
499extern struct kmem_cache *ecryptfs_inode_info_cache;
500extern struct kmem_cache *ecryptfs_sb_info_cache;
Tyler Hicks30632872011-05-24 05:11:12 -0500501extern struct kmem_cache *ecryptfs_header_cache;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800502extern struct kmem_cache *ecryptfs_xattr_cache;
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800503extern struct kmem_cache *ecryptfs_key_record_cache;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700504extern struct kmem_cache *ecryptfs_key_sig_cache;
505extern struct kmem_cache *ecryptfs_global_auth_tok_cache;
506extern struct kmem_cache *ecryptfs_key_tfm_cache;
Michael Halcrow746f1e52008-07-23 21:30:02 -0700507extern struct kmem_cache *ecryptfs_open_req_cache;
508
509struct ecryptfs_open_req {
510#define ECRYPTFS_REQ_PROCESSED 0x00000001
511#define ECRYPTFS_REQ_DROPPED 0x00000002
512#define ECRYPTFS_REQ_ZOMBIE 0x00000004
513 u32 flags;
514 struct file **lower_file;
515 struct dentry *lower_dentry;
516 struct vfsmount *lower_mnt;
517 wait_queue_head_t wait;
518 struct mutex mux;
519 struct list_head kthread_ctl_list;
520};
Michael Halcrow237fead2006-10-04 02:16:22 -0700521
Tyler Hicksc4f79072011-05-23 21:18:20 -0500522struct inode *ecryptfs_get_inode(struct inode *lower_inode,
523 struct super_block *sb);
Tyler Hicks3aeb86e2011-03-15 14:54:00 -0500524void ecryptfs_i_size_init(const char *page_virt, struct inode *inode);
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800525int ecryptfs_decode_and_decrypt_filename(char **decrypted_name,
526 size_t *decrypted_name_size,
527 struct dentry *ecryptfs_dentry,
528 const char *name, size_t name_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700529int ecryptfs_fill_zeros(struct file *file, loff_t new_length);
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800530int ecryptfs_encrypt_and_encode_filename(
531 char **encoded_name,
532 size_t *encoded_name_size,
533 struct ecryptfs_crypt_stat *crypt_stat,
534 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
535 const char *name, size_t name_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700536struct dentry *ecryptfs_lower_dentry(struct dentry *this_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700537void ecryptfs_dump_hex(char *data, int bytes);
538int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
539 int sg_size);
540int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat);
541void ecryptfs_rotate_iv(unsigned char *iv);
542void ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
Michael Halcrowfcd12832007-10-16 01:28:01 -0700543void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
544void ecryptfs_destroy_mount_crypt_stat(
Michael Halcrow237fead2006-10-04 02:16:22 -0700545 struct ecryptfs_mount_crypt_stat *mount_crypt_stat);
546int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat);
Michael Halcrowb6c1d8f2007-10-16 01:28:13 -0700547int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700548int ecryptfs_encrypt_page(struct page *page);
549int ecryptfs_decrypt_page(struct page *page);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700550int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry);
551int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700552int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry);
Tyler Hicksf4e60e62010-02-11 00:02:32 -0600553void ecryptfs_write_crypt_stat_flags(char *page_virt,
554 struct ecryptfs_crypt_stat *crypt_stat,
555 size_t *written);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500556int ecryptfs_read_and_validate_header_region(struct inode *inode);
557int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500558 struct inode *inode);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800559u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes);
Trevor Highland19e66a62008-02-06 01:38:36 -0800560int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code);
Michael Halcrow237fead2006-10-04 02:16:22 -0700561void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat);
562int ecryptfs_generate_key_packet_set(char *dest_base,
563 struct ecryptfs_crypt_stat *crypt_stat,
564 struct dentry *ecryptfs_dentry,
565 size_t *len, size_t max);
Michael Halcrow237fead2006-10-04 02:16:22 -0700566int
567ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
568 unsigned char *src, struct dentry *ecryptfs_dentry);
569int ecryptfs_truncate(struct dentry *dentry, loff_t new_length);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700570ssize_t
571ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
572 void *value, size_t size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800573int
574ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
575 size_t size, int flags);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700576int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode);
Tyler Hicks624ae522008-10-15 22:02:51 -0700577int ecryptfs_process_helo(uid_t euid, struct user_namespace *user_ns,
578 struct pid *pid);
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700579int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
580 struct pid *pid);
581int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
582 struct user_namespace *user_ns, struct pid *pid,
583 u32 seq);
Tyler Hicks624ae522008-10-15 22:02:51 -0700584int ecryptfs_send_message(char *data, int data_len,
Michael Halcrow88b4a072007-02-12 00:53:43 -0800585 struct ecryptfs_msg_ctx **msg_ctx);
586int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
587 struct ecryptfs_message **emsg);
Tyler Hicks624ae522008-10-15 22:02:51 -0700588int ecryptfs_init_messaging(void);
589void ecryptfs_release_messaging(void);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800590
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800591void
592ecryptfs_write_header_metadata(char *virt,
593 struct ecryptfs_crypt_stat *crypt_stat,
594 size_t *written);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700595int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig);
596int
597ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
Tyler Hicks84814d62009-03-13 13:51:59 -0700598 char *sig, u32 global_auth_tok_flags);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700599int ecryptfs_get_global_auth_tok_for_sig(
600 struct ecryptfs_global_auth_tok **global_auth_tok,
601 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig);
602int
603ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
604 size_t key_size);
605int ecryptfs_init_crypto(void);
Michael Halcrowfcd12832007-10-16 01:28:01 -0700606int ecryptfs_destroy_crypto(void);
Eric Sandeenaf440f52008-02-06 01:38:37 -0800607int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700608int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
609 struct mutex **tfm_mutex,
610 char *cipher_name);
611int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
612 struct ecryptfs_auth_tok **auth_tok,
613 char *sig);
Michael Halcrowda0102a2007-10-16 01:28:07 -0700614int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
615 loff_t offset, size_t size);
616int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
617 struct page *page_for_lower,
618 size_t offset_in_page, size_t size);
Al Viro48c1e442010-05-21 11:09:58 -0400619int ecryptfs_write(struct inode *inode, char *data, loff_t offset, size_t size);
Michael Halcrowda0102a2007-10-16 01:28:07 -0700620int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
621 struct inode *ecryptfs_inode);
622int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
623 pgoff_t page_index,
624 size_t offset_in_page, size_t size,
625 struct inode *ecryptfs_inode);
Al Viro02bd9792010-05-21 11:02:14 -0400626struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index);
Michael Halcrowf66e8832008-04-29 00:59:51 -0700627int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon);
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700628int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
629 struct user_namespace *user_ns);
Michael Halcrowf66e8832008-04-29 00:59:51 -0700630int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
631 size_t *length_size);
632int ecryptfs_write_packet_length(char *dest, size_t size,
633 size_t *packet_size_length);
634int ecryptfs_init_ecryptfs_miscdev(void);
635void ecryptfs_destroy_ecryptfs_miscdev(void);
636int ecryptfs_send_miscdev(char *data, size_t data_size,
637 struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
638 u16 msg_flags, struct ecryptfs_daemon *daemon);
639void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);
640int
Michael Halcrow6a3fd922008-04-29 00:59:52 -0700641ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
642 struct user_namespace *user_ns, struct pid *pid);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700643int ecryptfs_init_kthread(void);
644void ecryptfs_destroy_kthread(void);
645int ecryptfs_privileged_open(struct file **lower_file,
646 struct dentry *lower_dentry,
David Howells745ca242008-11-14 10:39:22 +1100647 struct vfsmount *lower_mnt,
648 const struct cred *cred);
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500649int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500650void ecryptfs_put_lower_file(struct inode *inode);
Michael Halcrow9c79f342009-01-06 14:41:57 -0800651int
652ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
653 size_t *packet_size,
654 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
655 char *filename, size_t filename_size);
656int
657ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
658 size_t *packet_size,
659 struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
660 char *data, size_t max_packet_size);
Michael Halcrowa34f60f2009-01-06 14:41:58 -0800661int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
662 loff_t offset);
Michael Halcrow88b4a072007-02-12 00:53:43 -0800663
Michael Halcrow237fead2006-10-04 02:16:22 -0700664#endif /* #ifndef ECRYPTFS_KERNEL_H */