blob: af1a8f01ebacd854789283267c9ce80688f48b64 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2003 Erez Zadok
5 * Copyright (C) 2001-2003 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -08009 * Tyler Hicks <tyhicks@ou.edu>
Michael Halcrow237fead2006-10-04 02:16:22 -070010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 */
26
27#include <linux/dcache.h>
28#include <linux/file.h>
29#include <linux/module.h>
30#include <linux/namei.h>
31#include <linux/skbuff.h>
32#include <linux/crypto.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070033#include <linux/mount.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070034#include <linux/pagemap.h>
35#include <linux/key.h>
36#include <linux/parser.h>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080037#include <linux/fs_stack.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070039#include "ecryptfs_kernel.h"
40
41/**
42 * Module parameter that defines the ecryptfs_verbosity level.
43 */
44int ecryptfs_verbosity = 0;
45
46module_param(ecryptfs_verbosity, int, 0);
47MODULE_PARM_DESC(ecryptfs_verbosity,
48 "Initial verbosity level (0 or 1; defaults to "
49 "0, which is Quiet)");
50
Michael Halcrowdddfa462007-02-12 00:53:44 -080051/**
Tyler Hicks624ae522008-10-15 22:02:51 -070052 * Module parameter that defines the number of message buffer elements
Michael Halcrowdddfa462007-02-12 00:53:44 -080053 */
54unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
55
56module_param(ecryptfs_message_buf_len, uint, 0);
57MODULE_PARM_DESC(ecryptfs_message_buf_len,
58 "Number of message buffer elements");
59
60/**
61 * Module parameter that defines the maximum guaranteed amount of time to wait
Tyler Hicks624ae522008-10-15 22:02:51 -070062 * for a response from ecryptfsd. The actual sleep time will be, more than
Michael Halcrowdddfa462007-02-12 00:53:44 -080063 * likely, a small amount greater than this specified value, but only less if
Tyler Hicks624ae522008-10-15 22:02:51 -070064 * the message successfully arrives.
Michael Halcrowdddfa462007-02-12 00:53:44 -080065 */
66signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
67
68module_param(ecryptfs_message_wait_timeout, long, 0);
69MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
70 "Maximum number of seconds that an operation will "
71 "sleep while waiting for a message response from "
72 "userspace");
73
74/**
75 * Module parameter that is an estimate of the maximum number of users
76 * that will be concurrently using eCryptfs. Set this to the right
77 * value to balance performance and memory use.
78 */
79unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
80
81module_param(ecryptfs_number_of_users, uint, 0);
82MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
83 "concurrent users of eCryptfs");
84
Michael Halcrow237fead2006-10-04 02:16:22 -070085void __ecryptfs_printk(const char *fmt, ...)
86{
87 va_list args;
88 va_start(args, fmt);
89 if (fmt[1] == '7') { /* KERN_DEBUG */
90 if (ecryptfs_verbosity >= 1)
91 vprintk(fmt, args);
92 } else
93 vprintk(fmt, args);
94 va_end(args);
95}
96
97/**
Michael Halcrow4981e082007-10-16 01:28:09 -070098 * ecryptfs_init_persistent_file
99 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
100 * the lower dentry and the lower mount set
101 *
102 * eCryptfs only ever keeps a single open file for every lower
103 * inode. All I/O operations to the lower inode occur through that
104 * file. When the first eCryptfs dentry that interposes with the first
105 * lower dentry for that inode is created, this function creates the
106 * persistent file struct and associates it with the eCryptfs
107 * inode. When the eCryptfs inode is destroyed, the file is closed.
108 *
109 * The persistent file will be opened with read/write permissions, if
110 * possible. Otherwise, it is opened read-only.
111 *
112 * This function does nothing if a lower persistent file is already
113 * associated with the eCryptfs inode.
114 *
115 * Returns zero on success; non-zero otherwise
116 */
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700117int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
Michael Halcrow4981e082007-10-16 01:28:09 -0700118{
David Howells745ca242008-11-14 10:39:22 +1100119 const struct cred *cred = current_cred();
Michael Halcrow4981e082007-10-16 01:28:09 -0700120 struct ecryptfs_inode_info *inode_info =
121 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
122 int rc = 0;
123
124 mutex_lock(&inode_info->lower_file_mutex);
125 if (!inode_info->lower_file) {
126 struct dentry *lower_dentry;
127 struct vfsmount *lower_mnt =
128 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
129
130 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700131 rc = ecryptfs_privileged_open(&inode_info->lower_file,
David Howells745ca242008-11-14 10:39:22 +1100132 lower_dentry, lower_mnt, cred);
Tyler Hicksac22ba22009-08-12 01:06:54 -0500133 if (rc) {
Michael Halcrow4981e082007-10-16 01:28:09 -0700134 printk(KERN_ERR "Error opening lower persistent file "
Michael Halcrow746f1e52008-07-23 21:30:02 -0700135 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
136 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
Michael Halcrow4981e082007-10-16 01:28:09 -0700137 inode_info->lower_file = NULL;
Al Virob65a9cf2009-12-16 06:27:40 -0500138 }
Michael Halcrow4981e082007-10-16 01:28:09 -0700139 }
140 mutex_unlock(&inode_info->lower_file_mutex);
141 return rc;
142}
143
144/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700145 * ecryptfs_interpose
146 * @lower_dentry: Existing dentry in the lower filesystem
147 * @dentry: ecryptfs' dentry
148 * @sb: ecryptfs's super_block
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700149 * @flags: flags to govern behavior of interpose procedure
Michael Halcrow237fead2006-10-04 02:16:22 -0700150 *
151 * Interposes upper and lower dentries.
152 *
153 * Returns zero on success; non-zero otherwise
154 */
155int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700156 struct super_block *sb, u32 flags)
Michael Halcrow237fead2006-10-04 02:16:22 -0700157{
158 struct inode *lower_inode;
159 struct inode *inode;
160 int rc = 0;
161
162 lower_inode = lower_dentry->d_inode;
163 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
164 rc = -EXDEV;
165 goto out;
166 }
167 if (!igrab(lower_inode)) {
168 rc = -ESTALE;
169 goto out;
170 }
171 inode = iget5_locked(sb, (unsigned long)lower_inode,
172 ecryptfs_inode_test, ecryptfs_inode_set,
173 lower_inode);
174 if (!inode) {
175 rc = -EACCES;
176 iput(lower_inode);
177 goto out;
178 }
179 if (inode->i_state & I_NEW)
180 unlock_new_inode(inode);
181 else
182 iput(lower_inode);
183 if (S_ISLNK(lower_inode->i_mode))
184 inode->i_op = &ecryptfs_symlink_iops;
185 else if (S_ISDIR(lower_inode->i_mode))
186 inode->i_op = &ecryptfs_dir_iops;
187 if (S_ISDIR(lower_inode->i_mode))
188 inode->i_fop = &ecryptfs_dir_fops;
Pekka Enberg26da8202006-10-19 23:28:14 -0700189 if (special_file(lower_inode->i_mode))
Michael Halcrow237fead2006-10-04 02:16:22 -0700190 init_special_inode(inode, lower_inode->i_mode,
191 lower_inode->i_rdev);
192 dentry->d_op = &ecryptfs_dops;
Erez Zadok9afa2fb2009-12-02 19:51:54 -0500193 fsstack_copy_attr_all(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700194 /* This size will be overwritten for real files w/ headers and
195 * other metadata */
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800196 fsstack_copy_inode_size(inode, lower_inode);
Tyler Hicksae6e8452009-03-12 00:19:46 -0500197 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
198 d_add(dentry, inode);
199 else
200 d_instantiate(dentry, inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700201out:
202 return rc;
203}
204
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800205enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
206 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
207 ecryptfs_opt_ecryptfs_key_bytes,
Michael Halcrow17398952007-02-12 00:53:45 -0800208 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
Michael Halcrow87c94c42009-01-06 14:42:01 -0800209 ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
210 ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
Tyler Hickse77cc8d2009-04-22 04:08:46 -0500211 ecryptfs_opt_unlink_sigs, ecryptfs_opt_err };
Michael Halcrow237fead2006-10-04 02:16:22 -0700212
Steven Whitehousea447c092008-10-13 10:46:57 +0100213static const match_table_t tokens = {
Michael Halcrow237fead2006-10-04 02:16:22 -0700214 {ecryptfs_opt_sig, "sig=%s"},
215 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700216 {ecryptfs_opt_cipher, "cipher=%s"},
217 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
218 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
219 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800220 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
221 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
Michael Halcrow87c94c42009-01-06 14:42:01 -0800222 {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
223 {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
224 {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
Tyler Hickse77cc8d2009-04-22 04:08:46 -0500225 {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700226 {ecryptfs_opt_err, NULL}
227};
228
Michael Halcrowf4aad162007-10-16 01:27:53 -0700229static int ecryptfs_init_global_auth_toks(
230 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700231{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700232 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -0700233 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700234
Michael Halcrowf4aad162007-10-16 01:27:53 -0700235 list_for_each_entry(global_auth_tok,
236 &mount_crypt_stat->global_auth_tok_list,
237 mount_crypt_stat_list) {
Michael Halcrow5dda6992007-10-16 01:28:06 -0700238 rc = ecryptfs_keyring_auth_tok_for_sig(
239 &global_auth_tok->global_auth_tok_key,
240 &global_auth_tok->global_auth_tok,
241 global_auth_tok->sig);
242 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700243 printk(KERN_ERR "Could not find valid key in user "
244 "session keyring for sig specified in mount "
245 "option: [%s]\n", global_auth_tok->sig);
246 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
Eric Sandeen982363c2008-07-23 21:30:04 -0700247 goto out;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700248 } else
249 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700250 }
Eric Sandeen982363c2008-07-23 21:30:04 -0700251out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700252 return rc;
253}
254
Michael Halcrowf4aad162007-10-16 01:27:53 -0700255static void ecryptfs_init_mount_crypt_stat(
256 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
257{
258 memset((void *)mount_crypt_stat, 0,
259 sizeof(struct ecryptfs_mount_crypt_stat));
260 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
261 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
262 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
263}
264
Michael Halcrow237fead2006-10-04 02:16:22 -0700265/**
266 * ecryptfs_parse_options
267 * @sb: The ecryptfs super block
268 * @options: The options pased to the kernel
269 *
270 * Parse mount options:
271 * debug=N - ecryptfs_verbosity level for debug output
272 * sig=XXX - description(signature) of the key to use
273 *
274 * Returns the dentry object of the lower-level (lower/interposed)
275 * directory; We want to mount our stackable file system on top of
276 * that lower directory.
277 *
278 * The signature of the key to use must be the description of a key
279 * already in the keyring. Mounting will fail if the key can not be
280 * found.
281 *
282 * Returns zero on success; non-zero on error
283 */
284static int ecryptfs_parse_options(struct super_block *sb, char *options)
285{
286 char *p;
287 int rc = 0;
288 int sig_set = 0;
289 int cipher_name_set = 0;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800290 int fn_cipher_name_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700291 int cipher_key_bytes;
292 int cipher_key_bytes_set = 0;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800293 int fn_cipher_key_bytes;
294 int fn_cipher_key_bytes_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700295 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
296 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
297 substring_t args[MAX_OPT_ARGS];
298 int token;
299 char *sig_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700300 char *cipher_name_dst;
301 char *cipher_name_src;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800302 char *fn_cipher_name_dst;
303 char *fn_cipher_name_src;
304 char *fnek_dst;
305 char *fnek_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700306 char *cipher_key_bytes_src;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800307 char *fn_cipher_key_bytes_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700308
309 if (!options) {
310 rc = -EINVAL;
311 goto out;
312 }
Michael Halcrow956159c2007-10-16 01:27:55 -0700313 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700314 while ((p = strsep(&options, ",")) != NULL) {
315 if (!*p)
316 continue;
317 token = match_token(p, tokens, args);
318 switch (token) {
319 case ecryptfs_opt_sig:
320 case ecryptfs_opt_ecryptfs_sig:
321 sig_src = args[0].from;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700322 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
Tyler Hicks84814d62009-03-13 13:51:59 -0700323 sig_src, 0);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700324 if (rc) {
325 printk(KERN_ERR "Error attempting to register "
326 "global sig; rc = [%d]\n", rc);
327 goto out;
328 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700329 sig_set = 1;
330 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700331 case ecryptfs_opt_cipher:
332 case ecryptfs_opt_ecryptfs_cipher:
333 cipher_name_src = args[0].from;
334 cipher_name_dst =
335 mount_crypt_stat->
336 global_default_cipher_name;
337 strncpy(cipher_name_dst, cipher_name_src,
338 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Michael Halcrow87c94c42009-01-06 14:42:01 -0800339 cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
Michael Halcrow237fead2006-10-04 02:16:22 -0700340 cipher_name_set = 1;
341 break;
342 case ecryptfs_opt_ecryptfs_key_bytes:
343 cipher_key_bytes_src = args[0].from;
344 cipher_key_bytes =
345 (int)simple_strtol(cipher_key_bytes_src,
346 &cipher_key_bytes_src, 0);
347 mount_crypt_stat->global_default_cipher_key_size =
348 cipher_key_bytes;
Michael Halcrow237fead2006-10-04 02:16:22 -0700349 cipher_key_bytes_set = 1;
350 break;
351 case ecryptfs_opt_passthrough:
352 mount_crypt_stat->flags |=
353 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
354 break;
Michael Halcrow17398952007-02-12 00:53:45 -0800355 case ecryptfs_opt_xattr_metadata:
356 mount_crypt_stat->flags |=
357 ECRYPTFS_XATTR_METADATA_ENABLED;
358 break;
359 case ecryptfs_opt_encrypted_view:
360 mount_crypt_stat->flags |=
361 ECRYPTFS_XATTR_METADATA_ENABLED;
362 mount_crypt_stat->flags |=
363 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
364 break;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800365 case ecryptfs_opt_fnek_sig:
366 fnek_src = args[0].from;
367 fnek_dst =
368 mount_crypt_stat->global_default_fnek_sig;
369 strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
370 mount_crypt_stat->global_default_fnek_sig[
371 ECRYPTFS_SIG_SIZE_HEX] = '\0';
372 rc = ecryptfs_add_global_auth_tok(
373 mount_crypt_stat,
Tyler Hicks84814d62009-03-13 13:51:59 -0700374 mount_crypt_stat->global_default_fnek_sig,
375 ECRYPTFS_AUTH_TOK_FNEK);
Michael Halcrow87c94c42009-01-06 14:42:01 -0800376 if (rc) {
377 printk(KERN_ERR "Error attempting to register "
378 "global fnek sig [%s]; rc = [%d]\n",
379 mount_crypt_stat->global_default_fnek_sig,
380 rc);
381 goto out;
382 }
383 mount_crypt_stat->flags |=
384 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
385 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
386 break;
387 case ecryptfs_opt_fn_cipher:
388 fn_cipher_name_src = args[0].from;
389 fn_cipher_name_dst =
390 mount_crypt_stat->global_default_fn_cipher_name;
391 strncpy(fn_cipher_name_dst, fn_cipher_name_src,
392 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
393 mount_crypt_stat->global_default_fn_cipher_name[
394 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
395 fn_cipher_name_set = 1;
396 break;
397 case ecryptfs_opt_fn_cipher_key_bytes:
398 fn_cipher_key_bytes_src = args[0].from;
399 fn_cipher_key_bytes =
400 (int)simple_strtol(fn_cipher_key_bytes_src,
401 &fn_cipher_key_bytes_src, 0);
402 mount_crypt_stat->global_default_fn_cipher_key_bytes =
403 fn_cipher_key_bytes;
404 fn_cipher_key_bytes_set = 1;
405 break;
Tyler Hickse77cc8d2009-04-22 04:08:46 -0500406 case ecryptfs_opt_unlink_sigs:
407 mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
408 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700409 case ecryptfs_opt_err:
410 default:
Michael Halcrow87c94c42009-01-06 14:42:01 -0800411 printk(KERN_WARNING
412 "%s: eCryptfs: unrecognized option [%s]\n",
413 __func__, p);
Michael Halcrow237fead2006-10-04 02:16:22 -0700414 }
415 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700416 if (!sig_set) {
417 rc = -EINVAL;
Michael Halcrow956159c2007-10-16 01:27:55 -0700418 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
419 "auth tok signature as a mount "
Michael Halcrow237fead2006-10-04 02:16:22 -0700420 "parameter; see the eCryptfs README\n");
421 goto out;
422 }
423 if (!cipher_name_set) {
Miklos Szeredi8f236802008-07-23 21:30:05 -0700424 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
425
426 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Miklos Szeredi8f236802008-07-23 21:30:05 -0700427 strcpy(mount_crypt_stat->global_default_cipher_name,
428 ECRYPTFS_DEFAULT_CIPHER);
Michael Halcrow237fead2006-10-04 02:16:22 -0700429 }
Michael Halcrow87c94c42009-01-06 14:42:01 -0800430 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
431 && !fn_cipher_name_set)
432 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
433 mount_crypt_stat->global_default_cipher_name);
434 if (!cipher_key_bytes_set)
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800435 mount_crypt_stat->global_default_cipher_key_size = 0;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800436 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
437 && !fn_cipher_key_bytes_set)
438 mount_crypt_stat->global_default_fn_cipher_key_bytes =
439 mount_crypt_stat->global_default_cipher_key_size;
Eric Sandeenaf440f52008-02-06 01:38:37 -0800440 mutex_lock(&key_tfm_list_mutex);
441 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
Michael Halcrow87c94c42009-01-06 14:42:01 -0800442 NULL)) {
Eric Sandeenaf440f52008-02-06 01:38:37 -0800443 rc = ecryptfs_add_new_key_tfm(
444 NULL, mount_crypt_stat->global_default_cipher_name,
445 mount_crypt_stat->global_default_cipher_key_size);
Michael Halcrow87c94c42009-01-06 14:42:01 -0800446 if (rc) {
447 printk(KERN_ERR "Error attempting to initialize "
448 "cipher with name = [%s] and key size = [%td]; "
449 "rc = [%d]\n",
450 mount_crypt_stat->global_default_cipher_name,
451 mount_crypt_stat->global_default_cipher_key_size,
452 rc);
453 rc = -EINVAL;
454 mutex_unlock(&key_tfm_list_mutex);
455 goto out;
456 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700457 }
Michael Halcrow87c94c42009-01-06 14:42:01 -0800458 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
459 && !ecryptfs_tfm_exists(
460 mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
461 rc = ecryptfs_add_new_key_tfm(
462 NULL, mount_crypt_stat->global_default_fn_cipher_name,
463 mount_crypt_stat->global_default_fn_cipher_key_bytes);
464 if (rc) {
465 printk(KERN_ERR "Error attempting to initialize "
466 "cipher with name = [%s] and key size = [%td]; "
467 "rc = [%d]\n",
468 mount_crypt_stat->global_default_fn_cipher_name,
469 mount_crypt_stat->global_default_fn_cipher_key_bytes,
470 rc);
471 rc = -EINVAL;
472 mutex_unlock(&key_tfm_list_mutex);
473 goto out;
474 }
475 }
476 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700477 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
Michael Halcrow87c94c42009-01-06 14:42:01 -0800478 if (rc)
Michael Halcrowf4aad162007-10-16 01:27:53 -0700479 printk(KERN_WARNING "One or more global auth toks could not "
480 "properly register; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700481out:
482 return rc;
483}
484
485struct kmem_cache *ecryptfs_sb_info_cache;
486
487/**
488 * ecryptfs_fill_super
489 * @sb: The ecryptfs super block
490 * @raw_data: The options passed to mount
491 * @silent: Not used but required by function prototype
492 *
493 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
494 *
495 * Returns zero on success; non-zero otherwise
496 */
497static int
498ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
499{
500 int rc = 0;
501
502 /* Released in ecryptfs_put_super() */
503 ecryptfs_set_superblock_private(sb,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800504 kmem_cache_zalloc(ecryptfs_sb_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800505 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700506 if (!ecryptfs_superblock_to_private(sb)) {
507 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
508 rc = -ENOMEM;
509 goto out;
510 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700511 sb->s_op = &ecryptfs_sops;
512 /* Released through deactivate_super(sb) from get_sb_nodev */
513 sb->s_root = d_alloc(NULL, &(const struct qstr) {
514 .hash = 0,.name = "/",.len = 1});
515 if (!sb->s_root) {
516 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
517 rc = -ENOMEM;
518 goto out;
519 }
520 sb->s_root->d_op = &ecryptfs_dops;
521 sb->s_root->d_sb = sb;
522 sb->s_root->d_parent = sb->s_root;
523 /* Released in d_release when dput(sb->s_root) is called */
524 /* through deactivate_super(sb) from get_sb_nodev() */
525 ecryptfs_set_dentry_private(sb->s_root,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800526 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800527 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700528 if (!ecryptfs_dentry_to_private(sb->s_root)) {
529 ecryptfs_printk(KERN_ERR,
530 "dentry_info_cache alloc failed\n");
531 rc = -ENOMEM;
532 goto out;
533 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700534 rc = 0;
535out:
536 /* Should be able to rely on deactivate_super called from
537 * get_sb_nodev */
538 return rc;
539}
540
541/**
542 * ecryptfs_read_super
543 * @sb: The ecryptfs super block
544 * @dev_name: The path to mount over
545 *
546 * Read the super block of the lower filesystem, and use
547 * ecryptfs_interpose to create our initial inode and super block
548 * struct.
549 */
550static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
551{
Al Viro421748e2008-08-02 01:04:36 -0400552 struct path path;
Michael Halcrow237fead2006-10-04 02:16:22 -0700553 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700554
Al Viro421748e2008-08-02 01:04:36 -0400555 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700556 if (rc) {
557 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
Michael Halcrow65dc8142007-02-28 20:12:57 -0800558 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700559 }
Al Viro421748e2008-08-02 01:04:36 -0400560 ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
561 sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
562 sb->s_blocksize = path.dentry->d_sb->s_blocksize;
563 ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
564 ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
565 rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700566 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700567 goto out_free;
568 rc = 0;
569 goto out;
570out_free:
Al Viro421748e2008-08-02 01:04:36 -0400571 path_put(&path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700572out:
573 return rc;
574}
575
576/**
577 * ecryptfs_get_sb
578 * @fs_type
579 * @flags
580 * @dev_name: The path to mount over
581 * @raw_data: The options passed into the kernel
582 *
583 * The whole ecryptfs_get_sb process is broken into 4 functions:
584 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
585 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
586 * with as much information as it can before needing
587 * the lower filesystem.
588 * ecryptfs_read_super(): this accesses the lower filesystem and uses
Erez Zadokfe0fc012010-01-04 18:17:02 -0500589 * ecryptfs_interpose to perform most of the linking
590 * ecryptfs_interpose(): links the lower filesystem into ecryptfs (inode.c)
Michael Halcrow237fead2006-10-04 02:16:22 -0700591 */
592static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
593 const char *dev_name, void *raw_data,
594 struct vfsmount *mnt)
595{
596 int rc;
597 struct super_block *sb;
598
599 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
600 if (rc < 0) {
601 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
602 goto out;
603 }
604 sb = mnt->mnt_sb;
605 rc = ecryptfs_parse_options(sb, raw_data);
606 if (rc) {
607 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
608 goto out_abort;
609 }
610 rc = ecryptfs_read_super(sb, dev_name);
611 if (rc) {
612 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
613 goto out_abort;
614 }
615 goto out;
616out_abort:
Al Viro6f5bbff2009-05-06 01:34:22 -0400617 dput(sb->s_root); /* aka mnt->mnt_root, as set by get_sb_nodev() */
618 deactivate_locked_super(sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700619out:
620 return rc;
621}
622
623/**
624 * ecryptfs_kill_block_super
625 * @sb: The ecryptfs super block
626 *
627 * Used to bring the superblock down and free the private data.
628 * Private data is free'd in ecryptfs_put_super()
629 */
630static void ecryptfs_kill_block_super(struct super_block *sb)
631{
632 generic_shutdown_super(sb);
633}
634
635static struct file_system_type ecryptfs_fs_type = {
636 .owner = THIS_MODULE,
637 .name = "ecryptfs",
638 .get_sb = ecryptfs_get_sb,
639 .kill_sb = ecryptfs_kill_block_super,
640 .fs_flags = 0
641};
642
643/**
644 * inode_info_init_once
645 *
646 * Initializes the ecryptfs_inode_info_cache when it is created
647 */
648static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700649inode_info_init_once(void *vptr)
Michael Halcrow237fead2006-10-04 02:16:22 -0700650{
651 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
652
Christoph Lametera35afb82007-05-16 22:10:57 -0700653 inode_init_once(&ei->vfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700654}
655
656static struct ecryptfs_cache_info {
Christoph Lametere18b8902006-12-06 20:33:20 -0800657 struct kmem_cache **cache;
Michael Halcrow237fead2006-10-04 02:16:22 -0700658 const char *name;
659 size_t size;
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700660 void (*ctor)(void *obj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700661} ecryptfs_cache_infos[] = {
662 {
663 .cache = &ecryptfs_auth_tok_list_item_cache,
664 .name = "ecryptfs_auth_tok_list_item",
665 .size = sizeof(struct ecryptfs_auth_tok_list_item),
666 },
667 {
668 .cache = &ecryptfs_file_info_cache,
669 .name = "ecryptfs_file_cache",
670 .size = sizeof(struct ecryptfs_file_info),
671 },
672 {
673 .cache = &ecryptfs_dentry_info_cache,
674 .name = "ecryptfs_dentry_info_cache",
675 .size = sizeof(struct ecryptfs_dentry_info),
676 },
677 {
678 .cache = &ecryptfs_inode_info_cache,
679 .name = "ecryptfs_inode_cache",
680 .size = sizeof(struct ecryptfs_inode_info),
681 .ctor = inode_info_init_once,
682 },
683 {
684 .cache = &ecryptfs_sb_info_cache,
685 .name = "ecryptfs_sb_cache",
686 .size = sizeof(struct ecryptfs_sb_info),
687 },
688 {
Michael Halcrow237fead2006-10-04 02:16:22 -0700689 .cache = &ecryptfs_header_cache_1,
690 .name = "ecryptfs_headers_1",
691 .size = PAGE_CACHE_SIZE,
692 },
693 {
694 .cache = &ecryptfs_header_cache_2,
695 .name = "ecryptfs_headers_2",
696 .size = PAGE_CACHE_SIZE,
697 },
698 {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800699 .cache = &ecryptfs_xattr_cache,
700 .name = "ecryptfs_xattr_cache",
701 .size = PAGE_CACHE_SIZE,
702 },
703 {
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800704 .cache = &ecryptfs_key_record_cache,
705 .name = "ecryptfs_key_record_cache",
706 .size = sizeof(struct ecryptfs_key_record),
707 },
Michael Halcrow956159c2007-10-16 01:27:55 -0700708 {
709 .cache = &ecryptfs_key_sig_cache,
710 .name = "ecryptfs_key_sig_cache",
711 .size = sizeof(struct ecryptfs_key_sig),
712 },
713 {
714 .cache = &ecryptfs_global_auth_tok_cache,
715 .name = "ecryptfs_global_auth_tok_cache",
716 .size = sizeof(struct ecryptfs_global_auth_tok),
717 },
718 {
719 .cache = &ecryptfs_key_tfm_cache,
720 .name = "ecryptfs_key_tfm_cache",
721 .size = sizeof(struct ecryptfs_key_tfm),
722 },
Michael Halcrow746f1e52008-07-23 21:30:02 -0700723 {
724 .cache = &ecryptfs_open_req_cache,
725 .name = "ecryptfs_open_req_cache",
726 .size = sizeof(struct ecryptfs_open_req),
727 },
Michael Halcrow237fead2006-10-04 02:16:22 -0700728};
729
730static void ecryptfs_free_kmem_caches(void)
731{
732 int i;
733
734 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
735 struct ecryptfs_cache_info *info;
736
737 info = &ecryptfs_cache_infos[i];
738 if (*(info->cache))
739 kmem_cache_destroy(*(info->cache));
740 }
741}
742
743/**
744 * ecryptfs_init_kmem_caches
745 *
746 * Returns zero on success; non-zero otherwise
747 */
748static int ecryptfs_init_kmem_caches(void)
749{
750 int i;
751
752 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
753 struct ecryptfs_cache_info *info;
754
755 info = &ecryptfs_cache_infos[i];
756 *(info->cache) = kmem_cache_create(info->name, info->size,
Paul Mundt20c2df82007-07-20 10:11:58 +0900757 0, SLAB_HWCACHE_ALIGN, info->ctor);
Michael Halcrow237fead2006-10-04 02:16:22 -0700758 if (!*(info->cache)) {
759 ecryptfs_free_kmem_caches();
760 ecryptfs_printk(KERN_WARNING, "%s: "
761 "kmem_cache_create failed\n",
762 info->name);
763 return -ENOMEM;
764 }
765 }
766 return 0;
767}
768
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800769static struct kobject *ecryptfs_kobj;
Michael Halcrow237fead2006-10-04 02:16:22 -0700770
Kay Sievers386f2752007-11-02 13:47:53 +0100771static ssize_t version_show(struct kobject *kobj,
772 struct kobj_attribute *attr, char *buff)
Michael Halcrow237fead2006-10-04 02:16:22 -0700773{
774 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
775}
776
Kay Sievers386f2752007-11-02 13:47:53 +0100777static struct kobj_attribute version_attr = __ATTR_RO(version);
Michael Halcrow237fead2006-10-04 02:16:22 -0700778
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700779static struct attribute *attributes[] = {
780 &version_attr.attr,
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700781 NULL,
782};
783
784static struct attribute_group attr_group = {
785 .attrs = attributes,
786};
Michael Halcrow237fead2006-10-04 02:16:22 -0700787
788static int do_sysfs_registration(void)
789{
790 int rc;
791
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800792 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
793 if (!ecryptfs_kobj) {
Greg Kroah-Hartman917e8652007-10-29 20:13:17 +0100794 printk(KERN_ERR "Unable to create ecryptfs kset\n");
795 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700796 goto out;
797 }
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800798 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
Michael Halcrow237fead2006-10-04 02:16:22 -0700799 if (rc) {
800 printk(KERN_ERR
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700801 "Unable to create ecryptfs version attributes\n");
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800802 kobject_put(ecryptfs_kobj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700803 }
804out:
805 return rc;
806}
807
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700808static void do_sysfs_unregistration(void)
809{
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800810 sysfs_remove_group(ecryptfs_kobj, &attr_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800811 kobject_put(ecryptfs_kobj);
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700812}
813
Michael Halcrow237fead2006-10-04 02:16:22 -0700814static int __init ecryptfs_init(void)
815{
816 int rc;
817
818 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
819 rc = -EINVAL;
820 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
821 "larger than the host's page size, and so "
822 "eCryptfs cannot run on this system. The "
823 "default eCryptfs extent size is [%d] bytes; "
824 "the page size is [%d] bytes.\n",
825 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
826 goto out;
827 }
828 rc = ecryptfs_init_kmem_caches();
829 if (rc) {
830 printk(KERN_ERR
831 "Failed to allocate one or more kmem_cache objects\n");
832 goto out;
833 }
834 rc = register_filesystem(&ecryptfs_fs_type);
835 if (rc) {
836 printk(KERN_ERR "Failed to register filesystem\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700837 goto out_free_kmem_caches;
Michael Halcrow237fead2006-10-04 02:16:22 -0700838 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700839 rc = do_sysfs_registration();
840 if (rc) {
841 printk(KERN_ERR "sysfs registration failed\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700842 goto out_unregister_filesystem;
Michael Halcrow237fead2006-10-04 02:16:22 -0700843 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700844 rc = ecryptfs_init_kthread();
845 if (rc) {
846 printk(KERN_ERR "%s: kthread initialization failed; "
847 "rc = [%d]\n", __func__, rc);
848 goto out_do_sysfs_unregistration;
849 }
Tyler Hicks624ae522008-10-15 22:02:51 -0700850 rc = ecryptfs_init_messaging();
Michael Halcrowdddfa462007-02-12 00:53:44 -0800851 if (rc) {
Michael Halcrow746f1e52008-07-23 21:30:02 -0700852 printk(KERN_ERR "Failure occured while attempting to "
Tyler Hicks624ae522008-10-15 22:02:51 -0700853 "initialize the communications channel to "
854 "ecryptfsd\n");
Michael Halcrow746f1e52008-07-23 21:30:02 -0700855 goto out_destroy_kthread;
Michael Halcrow956159c2007-10-16 01:27:55 -0700856 }
857 rc = ecryptfs_init_crypto();
858 if (rc) {
859 printk(KERN_ERR "Failure whilst attempting to init crypto; "
860 "rc = [%d]\n", rc);
Michael Halcrowcf81f892007-10-16 01:28:07 -0700861 goto out_release_messaging;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800862 }
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800863 if (ecryptfs_verbosity > 0)
864 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
865 "will be written to the syslog!\n", ecryptfs_verbosity);
866
Michael Halcrowcf81f892007-10-16 01:28:07 -0700867 goto out;
868out_release_messaging:
Tyler Hicks624ae522008-10-15 22:02:51 -0700869 ecryptfs_release_messaging();
Michael Halcrow746f1e52008-07-23 21:30:02 -0700870out_destroy_kthread:
871 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700872out_do_sysfs_unregistration:
873 do_sysfs_unregistration();
874out_unregister_filesystem:
875 unregister_filesystem(&ecryptfs_fs_type);
876out_free_kmem_caches:
877 ecryptfs_free_kmem_caches();
Michael Halcrow237fead2006-10-04 02:16:22 -0700878out:
879 return rc;
880}
881
882static void __exit ecryptfs_exit(void)
883{
Michael Halcrowcf81f892007-10-16 01:28:07 -0700884 int rc;
885
886 rc = ecryptfs_destroy_crypto();
887 if (rc)
888 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
889 "rc = [%d]\n", rc);
Tyler Hicks624ae522008-10-15 22:02:51 -0700890 ecryptfs_release_messaging();
Michael Halcrow746f1e52008-07-23 21:30:02 -0700891 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700892 do_sysfs_unregistration();
Michael Halcrow237fead2006-10-04 02:16:22 -0700893 unregister_filesystem(&ecryptfs_fs_type);
894 ecryptfs_free_kmem_caches();
895}
896
897MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
898MODULE_DESCRIPTION("eCryptfs");
899
900MODULE_LICENSE("GPL");
901
902module_init(ecryptfs_init)
903module_exit(ecryptfs_exit)