blob: 64d2ba980df43b28d646fb18efc9842e9b380488 [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>
Michael Halcrow237fead2006-10-04 02:16:22 -070038#include "ecryptfs_kernel.h"
39
40/**
41 * Module parameter that defines the ecryptfs_verbosity level.
42 */
43int ecryptfs_verbosity = 0;
44
45module_param(ecryptfs_verbosity, int, 0);
46MODULE_PARM_DESC(ecryptfs_verbosity,
47 "Initial verbosity level (0 or 1; defaults to "
48 "0, which is Quiet)");
49
Michael Halcrowdddfa462007-02-12 00:53:44 -080050/**
Tyler Hicks624ae522008-10-15 22:02:51 -070051 * Module parameter that defines the number of message buffer elements
Michael Halcrowdddfa462007-02-12 00:53:44 -080052 */
53unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
54
55module_param(ecryptfs_message_buf_len, uint, 0);
56MODULE_PARM_DESC(ecryptfs_message_buf_len,
57 "Number of message buffer elements");
58
59/**
60 * Module parameter that defines the maximum guaranteed amount of time to wait
Tyler Hicks624ae522008-10-15 22:02:51 -070061 * for a response from ecryptfsd. The actual sleep time will be, more than
Michael Halcrowdddfa462007-02-12 00:53:44 -080062 * likely, a small amount greater than this specified value, but only less if
Tyler Hicks624ae522008-10-15 22:02:51 -070063 * the message successfully arrives.
Michael Halcrowdddfa462007-02-12 00:53:44 -080064 */
65signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
66
67module_param(ecryptfs_message_wait_timeout, long, 0);
68MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
69 "Maximum number of seconds that an operation will "
70 "sleep while waiting for a message response from "
71 "userspace");
72
73/**
74 * Module parameter that is an estimate of the maximum number of users
75 * that will be concurrently using eCryptfs. Set this to the right
76 * value to balance performance and memory use.
77 */
78unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
79
80module_param(ecryptfs_number_of_users, uint, 0);
81MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
82 "concurrent users of eCryptfs");
83
Michael Halcrow237fead2006-10-04 02:16:22 -070084void __ecryptfs_printk(const char *fmt, ...)
85{
86 va_list args;
87 va_start(args, fmt);
88 if (fmt[1] == '7') { /* KERN_DEBUG */
89 if (ecryptfs_verbosity >= 1)
90 vprintk(fmt, args);
91 } else
92 vprintk(fmt, args);
93 va_end(args);
94}
95
96/**
Michael Halcrow4981e082007-10-16 01:28:09 -070097 * ecryptfs_init_persistent_file
98 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
99 * the lower dentry and the lower mount set
100 *
101 * eCryptfs only ever keeps a single open file for every lower
102 * inode. All I/O operations to the lower inode occur through that
103 * file. When the first eCryptfs dentry that interposes with the first
104 * lower dentry for that inode is created, this function creates the
105 * persistent file struct and associates it with the eCryptfs
106 * inode. When the eCryptfs inode is destroyed, the file is closed.
107 *
108 * The persistent file will be opened with read/write permissions, if
109 * possible. Otherwise, it is opened read-only.
110 *
111 * This function does nothing if a lower persistent file is already
112 * associated with the eCryptfs inode.
113 *
114 * Returns zero on success; non-zero otherwise
115 */
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700116int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
Michael Halcrow4981e082007-10-16 01:28:09 -0700117{
118 struct ecryptfs_inode_info *inode_info =
119 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
120 int rc = 0;
121
122 mutex_lock(&inode_info->lower_file_mutex);
123 if (!inode_info->lower_file) {
124 struct dentry *lower_dentry;
125 struct vfsmount *lower_mnt =
126 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
127
128 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700129 rc = ecryptfs_privileged_open(&inode_info->lower_file,
130 lower_dentry, lower_mnt);
131 if (rc || IS_ERR(inode_info->lower_file)) {
Michael Halcrow4981e082007-10-16 01:28:09 -0700132 printk(KERN_ERR "Error opening lower persistent file "
Michael Halcrow746f1e52008-07-23 21:30:02 -0700133 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
134 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
Michael Halcrow4981e082007-10-16 01:28:09 -0700135 rc = PTR_ERR(inode_info->lower_file);
136 inode_info->lower_file = NULL;
137 }
138 }
139 mutex_unlock(&inode_info->lower_file_mutex);
140 return rc;
141}
142
143/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700144 * ecryptfs_interpose
145 * @lower_dentry: Existing dentry in the lower filesystem
146 * @dentry: ecryptfs' dentry
147 * @sb: ecryptfs's super_block
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700148 * @flags: flags to govern behavior of interpose procedure
Michael Halcrow237fead2006-10-04 02:16:22 -0700149 *
150 * Interposes upper and lower dentries.
151 *
152 * Returns zero on success; non-zero otherwise
153 */
154int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700155 struct super_block *sb, u32 flags)
Michael Halcrow237fead2006-10-04 02:16:22 -0700156{
157 struct inode *lower_inode;
158 struct inode *inode;
159 int rc = 0;
160
161 lower_inode = lower_dentry->d_inode;
162 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
163 rc = -EXDEV;
164 goto out;
165 }
166 if (!igrab(lower_inode)) {
167 rc = -ESTALE;
168 goto out;
169 }
170 inode = iget5_locked(sb, (unsigned long)lower_inode,
171 ecryptfs_inode_test, ecryptfs_inode_set,
172 lower_inode);
173 if (!inode) {
174 rc = -EACCES;
175 iput(lower_inode);
176 goto out;
177 }
178 if (inode->i_state & I_NEW)
179 unlock_new_inode(inode);
180 else
181 iput(lower_inode);
182 if (S_ISLNK(lower_inode->i_mode))
183 inode->i_op = &ecryptfs_symlink_iops;
184 else if (S_ISDIR(lower_inode->i_mode))
185 inode->i_op = &ecryptfs_dir_iops;
186 if (S_ISDIR(lower_inode->i_mode))
187 inode->i_fop = &ecryptfs_dir_fops;
Pekka Enberg26da8202006-10-19 23:28:14 -0700188 if (special_file(lower_inode->i_mode))
Michael Halcrow237fead2006-10-04 02:16:22 -0700189 init_special_inode(inode, lower_inode->i_mode,
190 lower_inode->i_rdev);
191 dentry->d_op = &ecryptfs_dops;
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700192 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
Michael Halcrow237fead2006-10-04 02:16:22 -0700193 d_add(dentry, inode);
194 else
195 d_instantiate(dentry, inode);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800196 fsstack_copy_attr_all(inode, lower_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700197 /* This size will be overwritten for real files w/ headers and
198 * other metadata */
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800199 fsstack_copy_inode_size(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700200out:
201 return rc;
202}
203
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800204enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
205 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
206 ecryptfs_opt_ecryptfs_key_bytes,
Michael Halcrow17398952007-02-12 00:53:45 -0800207 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
208 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
Michael Halcrow237fead2006-10-04 02:16:22 -0700209
Steven Whitehousea447c092008-10-13 10:46:57 +0100210static const match_table_t tokens = {
Michael Halcrow237fead2006-10-04 02:16:22 -0700211 {ecryptfs_opt_sig, "sig=%s"},
212 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700213 {ecryptfs_opt_cipher, "cipher=%s"},
214 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
215 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
216 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800217 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
218 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700219 {ecryptfs_opt_err, NULL}
220};
221
Michael Halcrowf4aad162007-10-16 01:27:53 -0700222static int ecryptfs_init_global_auth_toks(
223 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700224{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700225 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -0700226 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700227
Michael Halcrowf4aad162007-10-16 01:27:53 -0700228 list_for_each_entry(global_auth_tok,
229 &mount_crypt_stat->global_auth_tok_list,
230 mount_crypt_stat_list) {
Michael Halcrow5dda6992007-10-16 01:28:06 -0700231 rc = ecryptfs_keyring_auth_tok_for_sig(
232 &global_auth_tok->global_auth_tok_key,
233 &global_auth_tok->global_auth_tok,
234 global_auth_tok->sig);
235 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700236 printk(KERN_ERR "Could not find valid key in user "
237 "session keyring for sig specified in mount "
238 "option: [%s]\n", global_auth_tok->sig);
239 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
Eric Sandeen982363c2008-07-23 21:30:04 -0700240 goto out;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700241 } else
242 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700243 }
Eric Sandeen982363c2008-07-23 21:30:04 -0700244out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700245 return rc;
246}
247
Michael Halcrowf4aad162007-10-16 01:27:53 -0700248static void ecryptfs_init_mount_crypt_stat(
249 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
250{
251 memset((void *)mount_crypt_stat, 0,
252 sizeof(struct ecryptfs_mount_crypt_stat));
253 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
254 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
255 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
256}
257
Michael Halcrow237fead2006-10-04 02:16:22 -0700258/**
259 * ecryptfs_parse_options
260 * @sb: The ecryptfs super block
261 * @options: The options pased to the kernel
262 *
263 * Parse mount options:
264 * debug=N - ecryptfs_verbosity level for debug output
265 * sig=XXX - description(signature) of the key to use
266 *
267 * Returns the dentry object of the lower-level (lower/interposed)
268 * directory; We want to mount our stackable file system on top of
269 * that lower directory.
270 *
271 * The signature of the key to use must be the description of a key
272 * already in the keyring. Mounting will fail if the key can not be
273 * found.
274 *
275 * Returns zero on success; non-zero on error
276 */
277static int ecryptfs_parse_options(struct super_block *sb, char *options)
278{
279 char *p;
280 int rc = 0;
281 int sig_set = 0;
282 int cipher_name_set = 0;
283 int cipher_key_bytes;
284 int cipher_key_bytes_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700285 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
286 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
287 substring_t args[MAX_OPT_ARGS];
288 int token;
289 char *sig_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700290 char *cipher_name_dst;
291 char *cipher_name_src;
292 char *cipher_key_bytes_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700293
294 if (!options) {
295 rc = -EINVAL;
296 goto out;
297 }
Michael Halcrow956159c2007-10-16 01:27:55 -0700298 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700299 while ((p = strsep(&options, ",")) != NULL) {
300 if (!*p)
301 continue;
302 token = match_token(p, tokens, args);
303 switch (token) {
304 case ecryptfs_opt_sig:
305 case ecryptfs_opt_ecryptfs_sig:
306 sig_src = args[0].from;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700307 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
308 sig_src);
309 if (rc) {
310 printk(KERN_ERR "Error attempting to register "
311 "global sig; rc = [%d]\n", rc);
312 goto out;
313 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700314 sig_set = 1;
315 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700316 case ecryptfs_opt_cipher:
317 case ecryptfs_opt_ecryptfs_cipher:
318 cipher_name_src = args[0].from;
319 cipher_name_dst =
320 mount_crypt_stat->
321 global_default_cipher_name;
322 strncpy(cipher_name_dst, cipher_name_src,
323 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
324 ecryptfs_printk(KERN_DEBUG,
325 "The mount_crypt_stat "
326 "global_default_cipher_name set to: "
327 "[%s]\n", cipher_name_dst);
328 cipher_name_set = 1;
329 break;
330 case ecryptfs_opt_ecryptfs_key_bytes:
331 cipher_key_bytes_src = args[0].from;
332 cipher_key_bytes =
333 (int)simple_strtol(cipher_key_bytes_src,
334 &cipher_key_bytes_src, 0);
335 mount_crypt_stat->global_default_cipher_key_size =
336 cipher_key_bytes;
337 ecryptfs_printk(KERN_DEBUG,
338 "The mount_crypt_stat "
339 "global_default_cipher_key_size "
340 "set to: [%d]\n", mount_crypt_stat->
341 global_default_cipher_key_size);
342 cipher_key_bytes_set = 1;
343 break;
344 case ecryptfs_opt_passthrough:
345 mount_crypt_stat->flags |=
346 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
347 break;
Michael Halcrow17398952007-02-12 00:53:45 -0800348 case ecryptfs_opt_xattr_metadata:
349 mount_crypt_stat->flags |=
350 ECRYPTFS_XATTR_METADATA_ENABLED;
351 break;
352 case ecryptfs_opt_encrypted_view:
353 mount_crypt_stat->flags |=
354 ECRYPTFS_XATTR_METADATA_ENABLED;
355 mount_crypt_stat->flags |=
356 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
357 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700358 case ecryptfs_opt_err:
359 default:
360 ecryptfs_printk(KERN_WARNING,
361 "eCryptfs: unrecognized option '%s'\n",
362 p);
363 }
364 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700365 if (!sig_set) {
366 rc = -EINVAL;
Michael Halcrow956159c2007-10-16 01:27:55 -0700367 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
368 "auth tok signature as a mount "
Michael Halcrow237fead2006-10-04 02:16:22 -0700369 "parameter; see the eCryptfs README\n");
370 goto out;
371 }
372 if (!cipher_name_set) {
Miklos Szeredi8f236802008-07-23 21:30:05 -0700373 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
374
375 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
376
377 strcpy(mount_crypt_stat->global_default_cipher_name,
378 ECRYPTFS_DEFAULT_CIPHER);
Michael Halcrow237fead2006-10-04 02:16:22 -0700379 }
380 if (!cipher_key_bytes_set) {
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800381 mount_crypt_stat->global_default_cipher_key_size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700382 }
Eric Sandeenaf440f52008-02-06 01:38:37 -0800383 mutex_lock(&key_tfm_list_mutex);
384 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
385 NULL))
386 rc = ecryptfs_add_new_key_tfm(
387 NULL, mount_crypt_stat->global_default_cipher_name,
388 mount_crypt_stat->global_default_cipher_key_size);
389 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700390 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700391 printk(KERN_ERR "Error attempting to initialize cipher with "
Andrew Morton81acbcd2007-10-16 01:27:59 -0700392 "name = [%s] and key size = [%td]; rc = [%d]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700393 mount_crypt_stat->global_default_cipher_name,
394 mount_crypt_stat->global_default_cipher_key_size, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700395 rc = -EINVAL;
396 goto out;
397 }
Michael Halcrow5dda6992007-10-16 01:28:06 -0700398 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
399 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700400 printk(KERN_WARNING "One or more global auth toks could not "
401 "properly register; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700402 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700403out:
404 return rc;
405}
406
407struct kmem_cache *ecryptfs_sb_info_cache;
408
409/**
410 * ecryptfs_fill_super
411 * @sb: The ecryptfs super block
412 * @raw_data: The options passed to mount
413 * @silent: Not used but required by function prototype
414 *
415 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
416 *
417 * Returns zero on success; non-zero otherwise
418 */
419static int
420ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
421{
422 int rc = 0;
423
424 /* Released in ecryptfs_put_super() */
425 ecryptfs_set_superblock_private(sb,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800426 kmem_cache_zalloc(ecryptfs_sb_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800427 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700428 if (!ecryptfs_superblock_to_private(sb)) {
429 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
430 rc = -ENOMEM;
431 goto out;
432 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700433 sb->s_op = &ecryptfs_sops;
434 /* Released through deactivate_super(sb) from get_sb_nodev */
435 sb->s_root = d_alloc(NULL, &(const struct qstr) {
436 .hash = 0,.name = "/",.len = 1});
437 if (!sb->s_root) {
438 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
439 rc = -ENOMEM;
440 goto out;
441 }
442 sb->s_root->d_op = &ecryptfs_dops;
443 sb->s_root->d_sb = sb;
444 sb->s_root->d_parent = sb->s_root;
445 /* Released in d_release when dput(sb->s_root) is called */
446 /* through deactivate_super(sb) from get_sb_nodev() */
447 ecryptfs_set_dentry_private(sb->s_root,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800448 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800449 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700450 if (!ecryptfs_dentry_to_private(sb->s_root)) {
451 ecryptfs_printk(KERN_ERR,
452 "dentry_info_cache alloc failed\n");
453 rc = -ENOMEM;
454 goto out;
455 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700456 rc = 0;
457out:
458 /* Should be able to rely on deactivate_super called from
459 * get_sb_nodev */
460 return rc;
461}
462
463/**
464 * ecryptfs_read_super
465 * @sb: The ecryptfs super block
466 * @dev_name: The path to mount over
467 *
468 * Read the super block of the lower filesystem, and use
469 * ecryptfs_interpose to create our initial inode and super block
470 * struct.
471 */
472static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
473{
Al Viro421748e2008-08-02 01:04:36 -0400474 struct path path;
Michael Halcrow237fead2006-10-04 02:16:22 -0700475 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700476
Al Viro421748e2008-08-02 01:04:36 -0400477 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700478 if (rc) {
479 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
Michael Halcrow65dc8142007-02-28 20:12:57 -0800480 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700481 }
Al Viro421748e2008-08-02 01:04:36 -0400482 ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
483 sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
484 sb->s_blocksize = path.dentry->d_sb->s_blocksize;
485 ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
486 ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
487 rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700488 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700489 goto out_free;
490 rc = 0;
491 goto out;
492out_free:
Al Viro421748e2008-08-02 01:04:36 -0400493 path_put(&path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700494out:
495 return rc;
496}
497
498/**
499 * ecryptfs_get_sb
500 * @fs_type
501 * @flags
502 * @dev_name: The path to mount over
503 * @raw_data: The options passed into the kernel
504 *
505 * The whole ecryptfs_get_sb process is broken into 4 functions:
506 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
507 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
508 * with as much information as it can before needing
509 * the lower filesystem.
510 * ecryptfs_read_super(): this accesses the lower filesystem and uses
511 * ecryptfs_interpolate to perform most of the linking
512 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
513 */
514static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
515 const char *dev_name, void *raw_data,
516 struct vfsmount *mnt)
517{
518 int rc;
519 struct super_block *sb;
520
521 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
522 if (rc < 0) {
523 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
524 goto out;
525 }
526 sb = mnt->mnt_sb;
527 rc = ecryptfs_parse_options(sb, raw_data);
528 if (rc) {
529 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
530 goto out_abort;
531 }
532 rc = ecryptfs_read_super(sb, dev_name);
533 if (rc) {
534 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
535 goto out_abort;
536 }
537 goto out;
538out_abort:
539 dput(sb->s_root);
540 up_write(&sb->s_umount);
541 deactivate_super(sb);
542out:
543 return rc;
544}
545
546/**
547 * ecryptfs_kill_block_super
548 * @sb: The ecryptfs super block
549 *
550 * Used to bring the superblock down and free the private data.
551 * Private data is free'd in ecryptfs_put_super()
552 */
553static void ecryptfs_kill_block_super(struct super_block *sb)
554{
555 generic_shutdown_super(sb);
556}
557
558static struct file_system_type ecryptfs_fs_type = {
559 .owner = THIS_MODULE,
560 .name = "ecryptfs",
561 .get_sb = ecryptfs_get_sb,
562 .kill_sb = ecryptfs_kill_block_super,
563 .fs_flags = 0
564};
565
566/**
567 * inode_info_init_once
568 *
569 * Initializes the ecryptfs_inode_info_cache when it is created
570 */
571static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700572inode_info_init_once(void *vptr)
Michael Halcrow237fead2006-10-04 02:16:22 -0700573{
574 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
575
Christoph Lametera35afb82007-05-16 22:10:57 -0700576 inode_init_once(&ei->vfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700577}
578
579static struct ecryptfs_cache_info {
Christoph Lametere18b8902006-12-06 20:33:20 -0800580 struct kmem_cache **cache;
Michael Halcrow237fead2006-10-04 02:16:22 -0700581 const char *name;
582 size_t size;
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700583 void (*ctor)(void *obj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700584} ecryptfs_cache_infos[] = {
585 {
586 .cache = &ecryptfs_auth_tok_list_item_cache,
587 .name = "ecryptfs_auth_tok_list_item",
588 .size = sizeof(struct ecryptfs_auth_tok_list_item),
589 },
590 {
591 .cache = &ecryptfs_file_info_cache,
592 .name = "ecryptfs_file_cache",
593 .size = sizeof(struct ecryptfs_file_info),
594 },
595 {
596 .cache = &ecryptfs_dentry_info_cache,
597 .name = "ecryptfs_dentry_info_cache",
598 .size = sizeof(struct ecryptfs_dentry_info),
599 },
600 {
601 .cache = &ecryptfs_inode_info_cache,
602 .name = "ecryptfs_inode_cache",
603 .size = sizeof(struct ecryptfs_inode_info),
604 .ctor = inode_info_init_once,
605 },
606 {
607 .cache = &ecryptfs_sb_info_cache,
608 .name = "ecryptfs_sb_cache",
609 .size = sizeof(struct ecryptfs_sb_info),
610 },
611 {
Michael Halcrow237fead2006-10-04 02:16:22 -0700612 .cache = &ecryptfs_header_cache_1,
613 .name = "ecryptfs_headers_1",
614 .size = PAGE_CACHE_SIZE,
615 },
616 {
617 .cache = &ecryptfs_header_cache_2,
618 .name = "ecryptfs_headers_2",
619 .size = PAGE_CACHE_SIZE,
620 },
621 {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800622 .cache = &ecryptfs_xattr_cache,
623 .name = "ecryptfs_xattr_cache",
624 .size = PAGE_CACHE_SIZE,
625 },
626 {
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800627 .cache = &ecryptfs_key_record_cache,
628 .name = "ecryptfs_key_record_cache",
629 .size = sizeof(struct ecryptfs_key_record),
630 },
Michael Halcrow956159c2007-10-16 01:27:55 -0700631 {
632 .cache = &ecryptfs_key_sig_cache,
633 .name = "ecryptfs_key_sig_cache",
634 .size = sizeof(struct ecryptfs_key_sig),
635 },
636 {
637 .cache = &ecryptfs_global_auth_tok_cache,
638 .name = "ecryptfs_global_auth_tok_cache",
639 .size = sizeof(struct ecryptfs_global_auth_tok),
640 },
641 {
642 .cache = &ecryptfs_key_tfm_cache,
643 .name = "ecryptfs_key_tfm_cache",
644 .size = sizeof(struct ecryptfs_key_tfm),
645 },
Michael Halcrow746f1e52008-07-23 21:30:02 -0700646 {
647 .cache = &ecryptfs_open_req_cache,
648 .name = "ecryptfs_open_req_cache",
649 .size = sizeof(struct ecryptfs_open_req),
650 },
Michael Halcrow237fead2006-10-04 02:16:22 -0700651};
652
653static void ecryptfs_free_kmem_caches(void)
654{
655 int i;
656
657 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
658 struct ecryptfs_cache_info *info;
659
660 info = &ecryptfs_cache_infos[i];
661 if (*(info->cache))
662 kmem_cache_destroy(*(info->cache));
663 }
664}
665
666/**
667 * ecryptfs_init_kmem_caches
668 *
669 * Returns zero on success; non-zero otherwise
670 */
671static int ecryptfs_init_kmem_caches(void)
672{
673 int i;
674
675 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
676 struct ecryptfs_cache_info *info;
677
678 info = &ecryptfs_cache_infos[i];
679 *(info->cache) = kmem_cache_create(info->name, info->size,
Paul Mundt20c2df82007-07-20 10:11:58 +0900680 0, SLAB_HWCACHE_ALIGN, info->ctor);
Michael Halcrow237fead2006-10-04 02:16:22 -0700681 if (!*(info->cache)) {
682 ecryptfs_free_kmem_caches();
683 ecryptfs_printk(KERN_WARNING, "%s: "
684 "kmem_cache_create failed\n",
685 info->name);
686 return -ENOMEM;
687 }
688 }
689 return 0;
690}
691
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800692static struct kobject *ecryptfs_kobj;
Michael Halcrow237fead2006-10-04 02:16:22 -0700693
Kay Sievers386f2752007-11-02 13:47:53 +0100694static ssize_t version_show(struct kobject *kobj,
695 struct kobj_attribute *attr, char *buff)
Michael Halcrow237fead2006-10-04 02:16:22 -0700696{
697 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
698}
699
Kay Sievers386f2752007-11-02 13:47:53 +0100700static struct kobj_attribute version_attr = __ATTR_RO(version);
Michael Halcrow237fead2006-10-04 02:16:22 -0700701
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700702static struct attribute *attributes[] = {
703 &version_attr.attr,
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700704 NULL,
705};
706
707static struct attribute_group attr_group = {
708 .attrs = attributes,
709};
Michael Halcrow237fead2006-10-04 02:16:22 -0700710
711static int do_sysfs_registration(void)
712{
713 int rc;
714
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800715 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
716 if (!ecryptfs_kobj) {
Greg Kroah-Hartman917e8652007-10-29 20:13:17 +0100717 printk(KERN_ERR "Unable to create ecryptfs kset\n");
718 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700719 goto out;
720 }
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800721 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 if (rc) {
723 printk(KERN_ERR
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700724 "Unable to create ecryptfs version attributes\n");
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800725 kobject_put(ecryptfs_kobj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700726 }
727out:
728 return rc;
729}
730
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700731static void do_sysfs_unregistration(void)
732{
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800733 sysfs_remove_group(ecryptfs_kobj, &attr_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800734 kobject_put(ecryptfs_kobj);
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700735}
736
Michael Halcrow237fead2006-10-04 02:16:22 -0700737static int __init ecryptfs_init(void)
738{
739 int rc;
740
741 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
742 rc = -EINVAL;
743 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
744 "larger than the host's page size, and so "
745 "eCryptfs cannot run on this system. The "
746 "default eCryptfs extent size is [%d] bytes; "
747 "the page size is [%d] bytes.\n",
748 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
749 goto out;
750 }
751 rc = ecryptfs_init_kmem_caches();
752 if (rc) {
753 printk(KERN_ERR
754 "Failed to allocate one or more kmem_cache objects\n");
755 goto out;
756 }
757 rc = register_filesystem(&ecryptfs_fs_type);
758 if (rc) {
759 printk(KERN_ERR "Failed to register filesystem\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700760 goto out_free_kmem_caches;
Michael Halcrow237fead2006-10-04 02:16:22 -0700761 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700762 rc = do_sysfs_registration();
763 if (rc) {
764 printk(KERN_ERR "sysfs registration failed\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700765 goto out_unregister_filesystem;
Michael Halcrow237fead2006-10-04 02:16:22 -0700766 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700767 rc = ecryptfs_init_kthread();
768 if (rc) {
769 printk(KERN_ERR "%s: kthread initialization failed; "
770 "rc = [%d]\n", __func__, rc);
771 goto out_do_sysfs_unregistration;
772 }
Tyler Hicks624ae522008-10-15 22:02:51 -0700773 rc = ecryptfs_init_messaging();
Michael Halcrowdddfa462007-02-12 00:53:44 -0800774 if (rc) {
Michael Halcrow746f1e52008-07-23 21:30:02 -0700775 printk(KERN_ERR "Failure occured while attempting to "
Tyler Hicks624ae522008-10-15 22:02:51 -0700776 "initialize the communications channel to "
777 "ecryptfsd\n");
Michael Halcrow746f1e52008-07-23 21:30:02 -0700778 goto out_destroy_kthread;
Michael Halcrow956159c2007-10-16 01:27:55 -0700779 }
780 rc = ecryptfs_init_crypto();
781 if (rc) {
782 printk(KERN_ERR "Failure whilst attempting to init crypto; "
783 "rc = [%d]\n", rc);
Michael Halcrowcf81f892007-10-16 01:28:07 -0700784 goto out_release_messaging;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800785 }
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800786 if (ecryptfs_verbosity > 0)
787 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
788 "will be written to the syslog!\n", ecryptfs_verbosity);
789
Michael Halcrowcf81f892007-10-16 01:28:07 -0700790 goto out;
791out_release_messaging:
Tyler Hicks624ae522008-10-15 22:02:51 -0700792 ecryptfs_release_messaging();
Michael Halcrow746f1e52008-07-23 21:30:02 -0700793out_destroy_kthread:
794 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700795out_do_sysfs_unregistration:
796 do_sysfs_unregistration();
797out_unregister_filesystem:
798 unregister_filesystem(&ecryptfs_fs_type);
799out_free_kmem_caches:
800 ecryptfs_free_kmem_caches();
Michael Halcrow237fead2006-10-04 02:16:22 -0700801out:
802 return rc;
803}
804
805static void __exit ecryptfs_exit(void)
806{
Michael Halcrowcf81f892007-10-16 01:28:07 -0700807 int rc;
808
809 rc = ecryptfs_destroy_crypto();
810 if (rc)
811 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
812 "rc = [%d]\n", rc);
Tyler Hicks624ae522008-10-15 22:02:51 -0700813 ecryptfs_release_messaging();
Michael Halcrow746f1e52008-07-23 21:30:02 -0700814 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700815 do_sysfs_unregistration();
Michael Halcrow237fead2006-10-04 02:16:22 -0700816 unregister_filesystem(&ecryptfs_fs_type);
817 ecryptfs_free_kmem_caches();
818}
819
820MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
821MODULE_DESCRIPTION("eCryptfs");
822
823MODULE_LICENSE("GPL");
824
825module_init(ecryptfs_init)
826module_exit(ecryptfs_exit)