blob: 8876fe7c76e28e2be30f87d5f478ff0de885785e [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>
33#include <linux/netlink.h>
34#include <linux/mount.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070035#include <linux/pagemap.h>
36#include <linux/key.h>
37#include <linux/parser.h>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080038#include <linux/fs_stack.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/**
52 * Module parameter that defines the number of netlink message buffer
53 * elements
54 */
55unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
56
57module_param(ecryptfs_message_buf_len, uint, 0);
58MODULE_PARM_DESC(ecryptfs_message_buf_len,
59 "Number of message buffer elements");
60
61/**
62 * Module parameter that defines the maximum guaranteed amount of time to wait
63 * for a response through netlink. The actual sleep time will be, more than
64 * likely, a small amount greater than this specified value, but only less if
65 * the netlink message successfully arrives.
66 */
67signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
68
69module_param(ecryptfs_message_wait_timeout, long, 0);
70MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
71 "Maximum number of seconds that an operation will "
72 "sleep while waiting for a message response from "
73 "userspace");
74
75/**
76 * Module parameter that is an estimate of the maximum number of users
77 * that will be concurrently using eCryptfs. Set this to the right
78 * value to balance performance and memory use.
79 */
80unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
81
82module_param(ecryptfs_number_of_users, uint, 0);
83MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
84 "concurrent users of eCryptfs");
85
86unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
87
Michael Halcrow237fead2006-10-04 02:16:22 -070088void __ecryptfs_printk(const char *fmt, ...)
89{
90 va_list args;
91 va_start(args, fmt);
92 if (fmt[1] == '7') { /* KERN_DEBUG */
93 if (ecryptfs_verbosity >= 1)
94 vprintk(fmt, args);
95 } else
96 vprintk(fmt, args);
97 va_end(args);
98}
99
100/**
Michael Halcrow4981e082007-10-16 01:28:09 -0700101 * ecryptfs_init_persistent_file
102 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
103 * the lower dentry and the lower mount set
104 *
105 * eCryptfs only ever keeps a single open file for every lower
106 * inode. All I/O operations to the lower inode occur through that
107 * file. When the first eCryptfs dentry that interposes with the first
108 * lower dentry for that inode is created, this function creates the
109 * persistent file struct and associates it with the eCryptfs
110 * inode. When the eCryptfs inode is destroyed, the file is closed.
111 *
112 * The persistent file will be opened with read/write permissions, if
113 * possible. Otherwise, it is opened read-only.
114 *
115 * This function does nothing if a lower persistent file is already
116 * associated with the eCryptfs inode.
117 *
118 * Returns zero on success; non-zero otherwise
119 */
Adrian Bunk7896b632008-02-06 01:38:32 -0800120static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
Michael Halcrow4981e082007-10-16 01:28:09 -0700121{
122 struct ecryptfs_inode_info *inode_info =
123 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
124 int rc = 0;
125
126 mutex_lock(&inode_info->lower_file_mutex);
127 if (!inode_info->lower_file) {
128 struct dentry *lower_dentry;
129 struct vfsmount *lower_mnt =
130 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
131
132 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700133 rc = ecryptfs_privileged_open(&inode_info->lower_file,
134 lower_dentry, lower_mnt);
135 if (rc || IS_ERR(inode_info->lower_file)) {
Michael Halcrow4981e082007-10-16 01:28:09 -0700136 printk(KERN_ERR "Error opening lower persistent file "
Michael Halcrow746f1e52008-07-23 21:30:02 -0700137 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
138 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
Michael Halcrow4981e082007-10-16 01:28:09 -0700139 rc = PTR_ERR(inode_info->lower_file);
140 inode_info->lower_file = NULL;
141 }
142 }
143 mutex_unlock(&inode_info->lower_file_mutex);
144 return rc;
145}
146
147/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700148 * ecryptfs_interpose
149 * @lower_dentry: Existing dentry in the lower filesystem
150 * @dentry: ecryptfs' dentry
151 * @sb: ecryptfs's super_block
152 * @flag: If set to true, then d_add is called, else d_instantiate is called
153 *
154 * Interposes upper and lower dentries.
155 *
156 * Returns zero on success; non-zero otherwise
157 */
158int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
159 struct super_block *sb, int flag)
160{
161 struct inode *lower_inode;
162 struct inode *inode;
163 int rc = 0;
164
165 lower_inode = lower_dentry->d_inode;
166 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
167 rc = -EXDEV;
168 goto out;
169 }
170 if (!igrab(lower_inode)) {
171 rc = -ESTALE;
172 goto out;
173 }
174 inode = iget5_locked(sb, (unsigned long)lower_inode,
175 ecryptfs_inode_test, ecryptfs_inode_set,
176 lower_inode);
177 if (!inode) {
178 rc = -EACCES;
179 iput(lower_inode);
180 goto out;
181 }
182 if (inode->i_state & I_NEW)
183 unlock_new_inode(inode);
184 else
185 iput(lower_inode);
186 if (S_ISLNK(lower_inode->i_mode))
187 inode->i_op = &ecryptfs_symlink_iops;
188 else if (S_ISDIR(lower_inode->i_mode))
189 inode->i_op = &ecryptfs_dir_iops;
190 if (S_ISDIR(lower_inode->i_mode))
191 inode->i_fop = &ecryptfs_dir_fops;
Pekka Enberg26da8202006-10-19 23:28:14 -0700192 if (special_file(lower_inode->i_mode))
Michael Halcrow237fead2006-10-04 02:16:22 -0700193 init_special_inode(inode, lower_inode->i_mode,
194 lower_inode->i_rdev);
195 dentry->d_op = &ecryptfs_dops;
196 if (flag)
197 d_add(dentry, inode);
198 else
199 d_instantiate(dentry, inode);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800200 fsstack_copy_attr_all(inode, lower_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700201 /* This size will be overwritten for real files w/ headers and
202 * other metadata */
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800203 fsstack_copy_inode_size(inode, lower_inode);
Michael Halcrow4981e082007-10-16 01:28:09 -0700204 rc = ecryptfs_init_persistent_file(dentry);
205 if (rc) {
206 printk(KERN_ERR "%s: Error attempting to initialize the "
207 "persistent file for the dentry with name [%s]; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700208 "rc = [%d]\n", __func__, dentry->d_name.name, rc);
Michael Halcrow4981e082007-10-16 01:28:09 -0700209 goto out;
210 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700211out:
212 return rc;
213}
214
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800215enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
216 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
217 ecryptfs_opt_ecryptfs_key_bytes,
Michael Halcrow17398952007-02-12 00:53:45 -0800218 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
219 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
Michael Halcrow237fead2006-10-04 02:16:22 -0700220
221static match_table_t tokens = {
222 {ecryptfs_opt_sig, "sig=%s"},
223 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700224 {ecryptfs_opt_cipher, "cipher=%s"},
225 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
226 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
227 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800228 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
229 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700230 {ecryptfs_opt_err, NULL}
231};
232
Michael Halcrowf4aad162007-10-16 01:27:53 -0700233static int ecryptfs_init_global_auth_toks(
234 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700235{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700236 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -0700237 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700238
Michael Halcrowf4aad162007-10-16 01:27:53 -0700239 list_for_each_entry(global_auth_tok,
240 &mount_crypt_stat->global_auth_tok_list,
241 mount_crypt_stat_list) {
Michael Halcrow5dda6992007-10-16 01:28:06 -0700242 rc = ecryptfs_keyring_auth_tok_for_sig(
243 &global_auth_tok->global_auth_tok_key,
244 &global_auth_tok->global_auth_tok,
245 global_auth_tok->sig);
246 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700247 printk(KERN_ERR "Could not find valid key in user "
248 "session keyring for sig specified in mount "
249 "option: [%s]\n", global_auth_tok->sig);
250 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
Eric Sandeen982363c2008-07-23 21:30:04 -0700251 goto out;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700252 } else
253 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700254 }
Eric Sandeen982363c2008-07-23 21:30:04 -0700255out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700256 return rc;
257}
258
Michael Halcrowf4aad162007-10-16 01:27:53 -0700259static void ecryptfs_init_mount_crypt_stat(
260 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
261{
262 memset((void *)mount_crypt_stat, 0,
263 sizeof(struct ecryptfs_mount_crypt_stat));
264 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
265 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
266 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
267}
268
Michael Halcrow237fead2006-10-04 02:16:22 -0700269/**
270 * ecryptfs_parse_options
271 * @sb: The ecryptfs super block
272 * @options: The options pased to the kernel
273 *
274 * Parse mount options:
275 * debug=N - ecryptfs_verbosity level for debug output
276 * sig=XXX - description(signature) of the key to use
277 *
278 * Returns the dentry object of the lower-level (lower/interposed)
279 * directory; We want to mount our stackable file system on top of
280 * that lower directory.
281 *
282 * The signature of the key to use must be the description of a key
283 * already in the keyring. Mounting will fail if the key can not be
284 * found.
285 *
286 * Returns zero on success; non-zero on error
287 */
288static int ecryptfs_parse_options(struct super_block *sb, char *options)
289{
290 char *p;
291 int rc = 0;
292 int sig_set = 0;
293 int cipher_name_set = 0;
294 int cipher_key_bytes;
295 int cipher_key_bytes_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700296 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
297 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
298 substring_t args[MAX_OPT_ARGS];
299 int token;
300 char *sig_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700301 char *cipher_name_dst;
302 char *cipher_name_src;
303 char *cipher_key_bytes_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700304 int cipher_name_len;
305
306 if (!options) {
307 rc = -EINVAL;
308 goto out;
309 }
Michael Halcrow956159c2007-10-16 01:27:55 -0700310 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700311 while ((p = strsep(&options, ",")) != NULL) {
312 if (!*p)
313 continue;
314 token = match_token(p, tokens, args);
315 switch (token) {
316 case ecryptfs_opt_sig:
317 case ecryptfs_opt_ecryptfs_sig:
318 sig_src = args[0].from;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700319 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
320 sig_src);
321 if (rc) {
322 printk(KERN_ERR "Error attempting to register "
323 "global sig; rc = [%d]\n", rc);
324 goto out;
325 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700326 sig_set = 1;
327 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700328 case ecryptfs_opt_cipher:
329 case ecryptfs_opt_ecryptfs_cipher:
330 cipher_name_src = args[0].from;
331 cipher_name_dst =
332 mount_crypt_stat->
333 global_default_cipher_name;
334 strncpy(cipher_name_dst, cipher_name_src,
335 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
336 ecryptfs_printk(KERN_DEBUG,
337 "The mount_crypt_stat "
338 "global_default_cipher_name set to: "
339 "[%s]\n", cipher_name_dst);
340 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;
349 ecryptfs_printk(KERN_DEBUG,
350 "The mount_crypt_stat "
351 "global_default_cipher_key_size "
352 "set to: [%d]\n", mount_crypt_stat->
353 global_default_cipher_key_size);
354 cipher_key_bytes_set = 1;
355 break;
356 case ecryptfs_opt_passthrough:
357 mount_crypt_stat->flags |=
358 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
359 break;
Michael Halcrow17398952007-02-12 00:53:45 -0800360 case ecryptfs_opt_xattr_metadata:
361 mount_crypt_stat->flags |=
362 ECRYPTFS_XATTR_METADATA_ENABLED;
363 break;
364 case ecryptfs_opt_encrypted_view:
365 mount_crypt_stat->flags |=
366 ECRYPTFS_XATTR_METADATA_ENABLED;
367 mount_crypt_stat->flags |=
368 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
369 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700370 case ecryptfs_opt_err:
371 default:
372 ecryptfs_printk(KERN_WARNING,
373 "eCryptfs: unrecognized option '%s'\n",
374 p);
375 }
376 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700377 if (!sig_set) {
378 rc = -EINVAL;
Michael Halcrow956159c2007-10-16 01:27:55 -0700379 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
380 "auth tok signature as a mount "
Michael Halcrow237fead2006-10-04 02:16:22 -0700381 "parameter; see the eCryptfs README\n");
382 goto out;
383 }
384 if (!cipher_name_set) {
385 cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
386 if (unlikely(cipher_name_len
387 >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
388 rc = -EINVAL;
389 BUG();
390 goto out;
391 }
392 memcpy(mount_crypt_stat->global_default_cipher_name,
393 ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
394 mount_crypt_stat->global_default_cipher_name[cipher_name_len]
395 = '\0';
396 }
397 if (!cipher_key_bytes_set) {
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800398 mount_crypt_stat->global_default_cipher_key_size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700399 }
Eric Sandeenaf440f52008-02-06 01:38:37 -0800400 mutex_lock(&key_tfm_list_mutex);
401 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
402 NULL))
403 rc = ecryptfs_add_new_key_tfm(
404 NULL, mount_crypt_stat->global_default_cipher_name,
405 mount_crypt_stat->global_default_cipher_key_size);
406 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700407 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700408 printk(KERN_ERR "Error attempting to initialize cipher with "
Andrew Morton81acbcd2007-10-16 01:27:59 -0700409 "name = [%s] and key size = [%td]; rc = [%d]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700410 mount_crypt_stat->global_default_cipher_name,
411 mount_crypt_stat->global_default_cipher_key_size, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700412 rc = -EINVAL;
413 goto out;
414 }
Michael Halcrow5dda6992007-10-16 01:28:06 -0700415 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
416 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700417 printk(KERN_WARNING "One or more global auth toks could not "
418 "properly register; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700419 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700420out:
421 return rc;
422}
423
424struct kmem_cache *ecryptfs_sb_info_cache;
425
426/**
427 * ecryptfs_fill_super
428 * @sb: The ecryptfs super block
429 * @raw_data: The options passed to mount
430 * @silent: Not used but required by function prototype
431 *
432 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
433 *
434 * Returns zero on success; non-zero otherwise
435 */
436static int
437ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
438{
439 int rc = 0;
440
441 /* Released in ecryptfs_put_super() */
442 ecryptfs_set_superblock_private(sb,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800443 kmem_cache_zalloc(ecryptfs_sb_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800444 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700445 if (!ecryptfs_superblock_to_private(sb)) {
446 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
447 rc = -ENOMEM;
448 goto out;
449 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700450 sb->s_op = &ecryptfs_sops;
451 /* Released through deactivate_super(sb) from get_sb_nodev */
452 sb->s_root = d_alloc(NULL, &(const struct qstr) {
453 .hash = 0,.name = "/",.len = 1});
454 if (!sb->s_root) {
455 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
456 rc = -ENOMEM;
457 goto out;
458 }
459 sb->s_root->d_op = &ecryptfs_dops;
460 sb->s_root->d_sb = sb;
461 sb->s_root->d_parent = sb->s_root;
462 /* Released in d_release when dput(sb->s_root) is called */
463 /* through deactivate_super(sb) from get_sb_nodev() */
464 ecryptfs_set_dentry_private(sb->s_root,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800465 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800466 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700467 if (!ecryptfs_dentry_to_private(sb->s_root)) {
468 ecryptfs_printk(KERN_ERR,
469 "dentry_info_cache alloc failed\n");
470 rc = -ENOMEM;
471 goto out;
472 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700473 rc = 0;
474out:
475 /* Should be able to rely on deactivate_super called from
476 * get_sb_nodev */
477 return rc;
478}
479
480/**
481 * ecryptfs_read_super
482 * @sb: The ecryptfs super block
483 * @dev_name: The path to mount over
484 *
485 * Read the super block of the lower filesystem, and use
486 * ecryptfs_interpose to create our initial inode and super block
487 * struct.
488 */
489static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
490{
491 int rc;
492 struct nameidata nd;
493 struct dentry *lower_root;
494 struct vfsmount *lower_mnt;
495
496 memset(&nd, 0, sizeof(struct nameidata));
Dmitriy Monakhov82b165282007-03-05 00:30:46 -0800497 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
Michael Halcrow237fead2006-10-04 02:16:22 -0700498 if (rc) {
499 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
Michael Halcrow65dc8142007-02-28 20:12:57 -0800500 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700501 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800502 lower_root = nd.path.dentry;
503 lower_mnt = nd.path.mnt;
Michael Halcrow237fead2006-10-04 02:16:22 -0700504 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
505 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
Eric Sandeen7c9e70e2007-12-17 16:20:07 -0800506 sb->s_blocksize = lower_root->d_sb->s_blocksize;
Michael Halcrow237fead2006-10-04 02:16:22 -0700507 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
508 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700509 rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
510 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700511 goto out_free;
512 rc = 0;
513 goto out;
514out_free:
Jan Blunck1d957f92008-02-14 19:34:35 -0800515 path_put(&nd.path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700516out:
517 return rc;
518}
519
520/**
521 * ecryptfs_get_sb
522 * @fs_type
523 * @flags
524 * @dev_name: The path to mount over
525 * @raw_data: The options passed into the kernel
526 *
527 * The whole ecryptfs_get_sb process is broken into 4 functions:
528 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
529 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
530 * with as much information as it can before needing
531 * the lower filesystem.
532 * ecryptfs_read_super(): this accesses the lower filesystem and uses
533 * ecryptfs_interpolate to perform most of the linking
534 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
535 */
536static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
537 const char *dev_name, void *raw_data,
538 struct vfsmount *mnt)
539{
540 int rc;
541 struct super_block *sb;
542
543 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
544 if (rc < 0) {
545 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
546 goto out;
547 }
548 sb = mnt->mnt_sb;
549 rc = ecryptfs_parse_options(sb, raw_data);
550 if (rc) {
551 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
552 goto out_abort;
553 }
554 rc = ecryptfs_read_super(sb, dev_name);
555 if (rc) {
556 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
557 goto out_abort;
558 }
559 goto out;
560out_abort:
561 dput(sb->s_root);
562 up_write(&sb->s_umount);
563 deactivate_super(sb);
564out:
565 return rc;
566}
567
568/**
569 * ecryptfs_kill_block_super
570 * @sb: The ecryptfs super block
571 *
572 * Used to bring the superblock down and free the private data.
573 * Private data is free'd in ecryptfs_put_super()
574 */
575static void ecryptfs_kill_block_super(struct super_block *sb)
576{
577 generic_shutdown_super(sb);
578}
579
580static struct file_system_type ecryptfs_fs_type = {
581 .owner = THIS_MODULE,
582 .name = "ecryptfs",
583 .get_sb = ecryptfs_get_sb,
584 .kill_sb = ecryptfs_kill_block_super,
585 .fs_flags = 0
586};
587
588/**
589 * inode_info_init_once
590 *
591 * Initializes the ecryptfs_inode_info_cache when it is created
592 */
593static void
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700594inode_info_init_once(struct kmem_cache *cachep, void *vptr)
Michael Halcrow237fead2006-10-04 02:16:22 -0700595{
596 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
597
Christoph Lametera35afb82007-05-16 22:10:57 -0700598 inode_init_once(&ei->vfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700599}
600
601static struct ecryptfs_cache_info {
Christoph Lametere18b8902006-12-06 20:33:20 -0800602 struct kmem_cache **cache;
Michael Halcrow237fead2006-10-04 02:16:22 -0700603 const char *name;
604 size_t size;
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700605 void (*ctor)(struct kmem_cache *cache, void *obj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700606} ecryptfs_cache_infos[] = {
607 {
608 .cache = &ecryptfs_auth_tok_list_item_cache,
609 .name = "ecryptfs_auth_tok_list_item",
610 .size = sizeof(struct ecryptfs_auth_tok_list_item),
611 },
612 {
613 .cache = &ecryptfs_file_info_cache,
614 .name = "ecryptfs_file_cache",
615 .size = sizeof(struct ecryptfs_file_info),
616 },
617 {
618 .cache = &ecryptfs_dentry_info_cache,
619 .name = "ecryptfs_dentry_info_cache",
620 .size = sizeof(struct ecryptfs_dentry_info),
621 },
622 {
623 .cache = &ecryptfs_inode_info_cache,
624 .name = "ecryptfs_inode_cache",
625 .size = sizeof(struct ecryptfs_inode_info),
626 .ctor = inode_info_init_once,
627 },
628 {
629 .cache = &ecryptfs_sb_info_cache,
630 .name = "ecryptfs_sb_cache",
631 .size = sizeof(struct ecryptfs_sb_info),
632 },
633 {
Michael Halcrow237fead2006-10-04 02:16:22 -0700634 .cache = &ecryptfs_header_cache_1,
635 .name = "ecryptfs_headers_1",
636 .size = PAGE_CACHE_SIZE,
637 },
638 {
639 .cache = &ecryptfs_header_cache_2,
640 .name = "ecryptfs_headers_2",
641 .size = PAGE_CACHE_SIZE,
642 },
643 {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800644 .cache = &ecryptfs_xattr_cache,
645 .name = "ecryptfs_xattr_cache",
646 .size = PAGE_CACHE_SIZE,
647 },
648 {
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800649 .cache = &ecryptfs_key_record_cache,
650 .name = "ecryptfs_key_record_cache",
651 .size = sizeof(struct ecryptfs_key_record),
652 },
Michael Halcrow956159c2007-10-16 01:27:55 -0700653 {
654 .cache = &ecryptfs_key_sig_cache,
655 .name = "ecryptfs_key_sig_cache",
656 .size = sizeof(struct ecryptfs_key_sig),
657 },
658 {
659 .cache = &ecryptfs_global_auth_tok_cache,
660 .name = "ecryptfs_global_auth_tok_cache",
661 .size = sizeof(struct ecryptfs_global_auth_tok),
662 },
663 {
664 .cache = &ecryptfs_key_tfm_cache,
665 .name = "ecryptfs_key_tfm_cache",
666 .size = sizeof(struct ecryptfs_key_tfm),
667 },
Michael Halcrow746f1e52008-07-23 21:30:02 -0700668 {
669 .cache = &ecryptfs_open_req_cache,
670 .name = "ecryptfs_open_req_cache",
671 .size = sizeof(struct ecryptfs_open_req),
672 },
Michael Halcrow237fead2006-10-04 02:16:22 -0700673};
674
675static void ecryptfs_free_kmem_caches(void)
676{
677 int i;
678
679 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
680 struct ecryptfs_cache_info *info;
681
682 info = &ecryptfs_cache_infos[i];
683 if (*(info->cache))
684 kmem_cache_destroy(*(info->cache));
685 }
686}
687
688/**
689 * ecryptfs_init_kmem_caches
690 *
691 * Returns zero on success; non-zero otherwise
692 */
693static int ecryptfs_init_kmem_caches(void)
694{
695 int i;
696
697 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
698 struct ecryptfs_cache_info *info;
699
700 info = &ecryptfs_cache_infos[i];
701 *(info->cache) = kmem_cache_create(info->name, info->size,
Paul Mundt20c2df82007-07-20 10:11:58 +0900702 0, SLAB_HWCACHE_ALIGN, info->ctor);
Michael Halcrow237fead2006-10-04 02:16:22 -0700703 if (!*(info->cache)) {
704 ecryptfs_free_kmem_caches();
705 ecryptfs_printk(KERN_WARNING, "%s: "
706 "kmem_cache_create failed\n",
707 info->name);
708 return -ENOMEM;
709 }
710 }
711 return 0;
712}
713
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800714static struct kobject *ecryptfs_kobj;
Michael Halcrow237fead2006-10-04 02:16:22 -0700715
Kay Sievers386f2752007-11-02 13:47:53 +0100716static ssize_t version_show(struct kobject *kobj,
717 struct kobj_attribute *attr, char *buff)
Michael Halcrow237fead2006-10-04 02:16:22 -0700718{
719 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
720}
721
Kay Sievers386f2752007-11-02 13:47:53 +0100722static struct kobj_attribute version_attr = __ATTR_RO(version);
Michael Halcrow237fead2006-10-04 02:16:22 -0700723
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700724static struct attribute *attributes[] = {
725 &version_attr.attr,
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700726 NULL,
727};
728
729static struct attribute_group attr_group = {
730 .attrs = attributes,
731};
Michael Halcrow237fead2006-10-04 02:16:22 -0700732
733static int do_sysfs_registration(void)
734{
735 int rc;
736
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800737 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
738 if (!ecryptfs_kobj) {
Greg Kroah-Hartman917e8652007-10-29 20:13:17 +0100739 printk(KERN_ERR "Unable to create ecryptfs kset\n");
740 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700741 goto out;
742 }
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800743 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
Michael Halcrow237fead2006-10-04 02:16:22 -0700744 if (rc) {
745 printk(KERN_ERR
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700746 "Unable to create ecryptfs version attributes\n");
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800747 kobject_put(ecryptfs_kobj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700748 }
749out:
750 return rc;
751}
752
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700753static void do_sysfs_unregistration(void)
754{
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800755 sysfs_remove_group(ecryptfs_kobj, &attr_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800756 kobject_put(ecryptfs_kobj);
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700757}
758
Michael Halcrow237fead2006-10-04 02:16:22 -0700759static int __init ecryptfs_init(void)
760{
761 int rc;
762
763 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
764 rc = -EINVAL;
765 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
766 "larger than the host's page size, and so "
767 "eCryptfs cannot run on this system. The "
768 "default eCryptfs extent size is [%d] bytes; "
769 "the page size is [%d] bytes.\n",
770 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
771 goto out;
772 }
773 rc = ecryptfs_init_kmem_caches();
774 if (rc) {
775 printk(KERN_ERR
776 "Failed to allocate one or more kmem_cache objects\n");
777 goto out;
778 }
779 rc = register_filesystem(&ecryptfs_fs_type);
780 if (rc) {
781 printk(KERN_ERR "Failed to register filesystem\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700782 goto out_free_kmem_caches;
Michael Halcrow237fead2006-10-04 02:16:22 -0700783 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700784 rc = do_sysfs_registration();
785 if (rc) {
786 printk(KERN_ERR "sysfs registration failed\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700787 goto out_unregister_filesystem;
Michael Halcrow237fead2006-10-04 02:16:22 -0700788 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700789 rc = ecryptfs_init_kthread();
790 if (rc) {
791 printk(KERN_ERR "%s: kthread initialization failed; "
792 "rc = [%d]\n", __func__, rc);
793 goto out_do_sysfs_unregistration;
794 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800795 rc = ecryptfs_init_messaging(ecryptfs_transport);
796 if (rc) {
Michael Halcrow746f1e52008-07-23 21:30:02 -0700797 printk(KERN_ERR "Failure occured while attempting to "
Michael Halcrowdddfa462007-02-12 00:53:44 -0800798 "initialize the eCryptfs netlink socket\n");
Michael Halcrow746f1e52008-07-23 21:30:02 -0700799 goto out_destroy_kthread;
Michael Halcrow956159c2007-10-16 01:27:55 -0700800 }
801 rc = ecryptfs_init_crypto();
802 if (rc) {
803 printk(KERN_ERR "Failure whilst attempting to init crypto; "
804 "rc = [%d]\n", rc);
Michael Halcrowcf81f892007-10-16 01:28:07 -0700805 goto out_release_messaging;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800806 }
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800807 if (ecryptfs_verbosity > 0)
808 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
809 "will be written to the syslog!\n", ecryptfs_verbosity);
810
Michael Halcrowcf81f892007-10-16 01:28:07 -0700811 goto out;
812out_release_messaging:
813 ecryptfs_release_messaging(ecryptfs_transport);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700814out_destroy_kthread:
815 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700816out_do_sysfs_unregistration:
817 do_sysfs_unregistration();
818out_unregister_filesystem:
819 unregister_filesystem(&ecryptfs_fs_type);
820out_free_kmem_caches:
821 ecryptfs_free_kmem_caches();
Michael Halcrow237fead2006-10-04 02:16:22 -0700822out:
823 return rc;
824}
825
826static void __exit ecryptfs_exit(void)
827{
Michael Halcrowcf81f892007-10-16 01:28:07 -0700828 int rc;
829
830 rc = ecryptfs_destroy_crypto();
831 if (rc)
832 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
833 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800834 ecryptfs_release_messaging(ecryptfs_transport);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700835 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700836 do_sysfs_unregistration();
Michael Halcrow237fead2006-10-04 02:16:22 -0700837 unregister_filesystem(&ecryptfs_fs_type);
838 ecryptfs_free_kmem_caches();
839}
840
841MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
842MODULE_DESCRIPTION("eCryptfs");
843
844MODULE_LICENSE("GPL");
845
846module_init(ecryptfs_init)
847module_exit(ecryptfs_exit)