blob: a277754da17163ee90521aea4c83f6c1fa3ac14f [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 */
120int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
121{
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);
133 /* Corresponding dput() and mntput() are done when the
134 * persistent file is fput() when the eCryptfs inode
135 * is destroyed. */
136 dget(lower_dentry);
137 mntget(lower_mnt);
138 inode_info->lower_file = dentry_open(lower_dentry,
139 lower_mnt,
140 (O_RDWR | O_LARGEFILE));
141 if (IS_ERR(inode_info->lower_file))
142 inode_info->lower_file = dentry_open(lower_dentry,
143 lower_mnt,
144 (O_RDONLY
145 | O_LARGEFILE));
146 if (IS_ERR(inode_info->lower_file)) {
147 printk(KERN_ERR "Error opening lower persistent file "
148 "for lower_dentry [0x%p] and lower_mnt [0x%p]\n",
149 lower_dentry, lower_mnt);
150 rc = PTR_ERR(inode_info->lower_file);
151 inode_info->lower_file = NULL;
152 }
153 }
154 mutex_unlock(&inode_info->lower_file_mutex);
155 return rc;
156}
157
158/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700159 * ecryptfs_interpose
160 * @lower_dentry: Existing dentry in the lower filesystem
161 * @dentry: ecryptfs' dentry
162 * @sb: ecryptfs's super_block
163 * @flag: If set to true, then d_add is called, else d_instantiate is called
164 *
165 * Interposes upper and lower dentries.
166 *
167 * Returns zero on success; non-zero otherwise
168 */
169int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
170 struct super_block *sb, int flag)
171{
172 struct inode *lower_inode;
173 struct inode *inode;
174 int rc = 0;
175
176 lower_inode = lower_dentry->d_inode;
177 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
178 rc = -EXDEV;
179 goto out;
180 }
181 if (!igrab(lower_inode)) {
182 rc = -ESTALE;
183 goto out;
184 }
185 inode = iget5_locked(sb, (unsigned long)lower_inode,
186 ecryptfs_inode_test, ecryptfs_inode_set,
187 lower_inode);
188 if (!inode) {
189 rc = -EACCES;
190 iput(lower_inode);
191 goto out;
192 }
193 if (inode->i_state & I_NEW)
194 unlock_new_inode(inode);
195 else
196 iput(lower_inode);
197 if (S_ISLNK(lower_inode->i_mode))
198 inode->i_op = &ecryptfs_symlink_iops;
199 else if (S_ISDIR(lower_inode->i_mode))
200 inode->i_op = &ecryptfs_dir_iops;
201 if (S_ISDIR(lower_inode->i_mode))
202 inode->i_fop = &ecryptfs_dir_fops;
Pekka Enberg26da8202006-10-19 23:28:14 -0700203 if (special_file(lower_inode->i_mode))
Michael Halcrow237fead2006-10-04 02:16:22 -0700204 init_special_inode(inode, lower_inode->i_mode,
205 lower_inode->i_rdev);
206 dentry->d_op = &ecryptfs_dops;
207 if (flag)
208 d_add(dentry, inode);
209 else
210 d_instantiate(dentry, inode);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800211 fsstack_copy_attr_all(inode, lower_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700212 /* This size will be overwritten for real files w/ headers and
213 * other metadata */
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800214 fsstack_copy_inode_size(inode, lower_inode);
Michael Halcrow4981e082007-10-16 01:28:09 -0700215 rc = ecryptfs_init_persistent_file(dentry);
216 if (rc) {
217 printk(KERN_ERR "%s: Error attempting to initialize the "
218 "persistent file for the dentry with name [%s]; "
219 "rc = [%d]\n", __FUNCTION__, dentry->d_name.name, rc);
220 goto out;
221 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700222out:
223 return rc;
224}
225
226enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug,
227 ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher,
228 ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes,
Michael Halcrow17398952007-02-12 00:53:45 -0800229 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
230 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
Michael Halcrow237fead2006-10-04 02:16:22 -0700231
232static match_table_t tokens = {
233 {ecryptfs_opt_sig, "sig=%s"},
234 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
235 {ecryptfs_opt_debug, "debug=%u"},
236 {ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"},
237 {ecryptfs_opt_cipher, "cipher=%s"},
238 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
239 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
240 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800241 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
242 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700243 {ecryptfs_opt_err, NULL}
244};
245
Michael Halcrowf4aad162007-10-16 01:27:53 -0700246static int ecryptfs_init_global_auth_toks(
247 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700248{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700249 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -0700250 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700251
Michael Halcrowf4aad162007-10-16 01:27:53 -0700252 list_for_each_entry(global_auth_tok,
253 &mount_crypt_stat->global_auth_tok_list,
254 mount_crypt_stat_list) {
Michael Halcrow5dda6992007-10-16 01:28:06 -0700255 rc = ecryptfs_keyring_auth_tok_for_sig(
256 &global_auth_tok->global_auth_tok_key,
257 &global_auth_tok->global_auth_tok,
258 global_auth_tok->sig);
259 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700260 printk(KERN_ERR "Could not find valid key in user "
261 "session keyring for sig specified in mount "
262 "option: [%s]\n", global_auth_tok->sig);
263 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
264 rc = 0;
265 } else
266 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700267 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700268 return rc;
269}
270
Michael Halcrowf4aad162007-10-16 01:27:53 -0700271static void ecryptfs_init_mount_crypt_stat(
272 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
273{
274 memset((void *)mount_crypt_stat, 0,
275 sizeof(struct ecryptfs_mount_crypt_stat));
276 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
277 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
278 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
279}
280
Michael Halcrow237fead2006-10-04 02:16:22 -0700281/**
282 * ecryptfs_parse_options
283 * @sb: The ecryptfs super block
284 * @options: The options pased to the kernel
285 *
286 * Parse mount options:
287 * debug=N - ecryptfs_verbosity level for debug output
288 * sig=XXX - description(signature) of the key to use
289 *
290 * Returns the dentry object of the lower-level (lower/interposed)
291 * directory; We want to mount our stackable file system on top of
292 * that lower directory.
293 *
294 * The signature of the key to use must be the description of a key
295 * already in the keyring. Mounting will fail if the key can not be
296 * found.
297 *
298 * Returns zero on success; non-zero on error
299 */
300static int ecryptfs_parse_options(struct super_block *sb, char *options)
301{
302 char *p;
303 int rc = 0;
304 int sig_set = 0;
305 int cipher_name_set = 0;
306 int cipher_key_bytes;
307 int cipher_key_bytes_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700308 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
309 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
310 substring_t args[MAX_OPT_ARGS];
311 int token;
312 char *sig_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700313 char *debug_src;
314 char *cipher_name_dst;
315 char *cipher_name_src;
316 char *cipher_key_bytes_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700317 int cipher_name_len;
318
319 if (!options) {
320 rc = -EINVAL;
321 goto out;
322 }
Michael Halcrow956159c2007-10-16 01:27:55 -0700323 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700324 while ((p = strsep(&options, ",")) != NULL) {
325 if (!*p)
326 continue;
327 token = match_token(p, tokens, args);
328 switch (token) {
329 case ecryptfs_opt_sig:
330 case ecryptfs_opt_ecryptfs_sig:
331 sig_src = args[0].from;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700332 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
333 sig_src);
334 if (rc) {
335 printk(KERN_ERR "Error attempting to register "
336 "global sig; rc = [%d]\n", rc);
337 goto out;
338 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700339 sig_set = 1;
340 break;
341 case ecryptfs_opt_debug:
342 case ecryptfs_opt_ecryptfs_debug:
343 debug_src = args[0].from;
344 ecryptfs_verbosity =
345 (int)simple_strtol(debug_src, &debug_src,
346 0);
347 ecryptfs_printk(KERN_DEBUG,
348 "Verbosity set to [%d]" "\n",
349 ecryptfs_verbosity);
350 break;
351 case ecryptfs_opt_cipher:
352 case ecryptfs_opt_ecryptfs_cipher:
353 cipher_name_src = args[0].from;
354 cipher_name_dst =
355 mount_crypt_stat->
356 global_default_cipher_name;
357 strncpy(cipher_name_dst, cipher_name_src,
358 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
359 ecryptfs_printk(KERN_DEBUG,
360 "The mount_crypt_stat "
361 "global_default_cipher_name set to: "
362 "[%s]\n", cipher_name_dst);
363 cipher_name_set = 1;
364 break;
365 case ecryptfs_opt_ecryptfs_key_bytes:
366 cipher_key_bytes_src = args[0].from;
367 cipher_key_bytes =
368 (int)simple_strtol(cipher_key_bytes_src,
369 &cipher_key_bytes_src, 0);
370 mount_crypt_stat->global_default_cipher_key_size =
371 cipher_key_bytes;
372 ecryptfs_printk(KERN_DEBUG,
373 "The mount_crypt_stat "
374 "global_default_cipher_key_size "
375 "set to: [%d]\n", mount_crypt_stat->
376 global_default_cipher_key_size);
377 cipher_key_bytes_set = 1;
378 break;
379 case ecryptfs_opt_passthrough:
380 mount_crypt_stat->flags |=
381 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
382 break;
Michael Halcrow17398952007-02-12 00:53:45 -0800383 case ecryptfs_opt_xattr_metadata:
384 mount_crypt_stat->flags |=
385 ECRYPTFS_XATTR_METADATA_ENABLED;
386 break;
387 case ecryptfs_opt_encrypted_view:
388 mount_crypt_stat->flags |=
389 ECRYPTFS_XATTR_METADATA_ENABLED;
390 mount_crypt_stat->flags |=
391 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
392 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700393 case ecryptfs_opt_err:
394 default:
395 ecryptfs_printk(KERN_WARNING,
396 "eCryptfs: unrecognized option '%s'\n",
397 p);
398 }
399 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700400 if (!sig_set) {
401 rc = -EINVAL;
Michael Halcrow956159c2007-10-16 01:27:55 -0700402 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
403 "auth tok signature as a mount "
Michael Halcrow237fead2006-10-04 02:16:22 -0700404 "parameter; see the eCryptfs README\n");
405 goto out;
406 }
407 if (!cipher_name_set) {
408 cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
409 if (unlikely(cipher_name_len
410 >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
411 rc = -EINVAL;
412 BUG();
413 goto out;
414 }
415 memcpy(mount_crypt_stat->global_default_cipher_name,
416 ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
417 mount_crypt_stat->global_default_cipher_name[cipher_name_len]
418 = '\0';
419 }
420 if (!cipher_key_bytes_set) {
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800421 mount_crypt_stat->global_default_cipher_key_size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700422 }
Michael Halcrow5dda6992007-10-16 01:28:06 -0700423 rc = ecryptfs_add_new_key_tfm(
424 NULL, mount_crypt_stat->global_default_cipher_name,
425 mount_crypt_stat->global_default_cipher_key_size);
426 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700427 printk(KERN_ERR "Error attempting to initialize cipher with "
Andrew Morton81acbcd2007-10-16 01:27:59 -0700428 "name = [%s] and key size = [%td]; rc = [%d]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700429 mount_crypt_stat->global_default_cipher_name,
430 mount_crypt_stat->global_default_cipher_key_size, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700431 rc = -EINVAL;
432 goto out;
433 }
Michael Halcrow5dda6992007-10-16 01:28:06 -0700434 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
435 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700436 printk(KERN_WARNING "One or more global auth toks could not "
437 "properly register; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700438 }
Michael Halcrowf4aad162007-10-16 01:27:53 -0700439 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700440out:
441 return rc;
442}
443
444struct kmem_cache *ecryptfs_sb_info_cache;
445
446/**
447 * ecryptfs_fill_super
448 * @sb: The ecryptfs super block
449 * @raw_data: The options passed to mount
450 * @silent: Not used but required by function prototype
451 *
452 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
453 *
454 * Returns zero on success; non-zero otherwise
455 */
456static int
457ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
458{
459 int rc = 0;
460
461 /* Released in ecryptfs_put_super() */
462 ecryptfs_set_superblock_private(sb,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800463 kmem_cache_zalloc(ecryptfs_sb_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800464 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700465 if (!ecryptfs_superblock_to_private(sb)) {
466 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
467 rc = -ENOMEM;
468 goto out;
469 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700470 sb->s_op = &ecryptfs_sops;
471 /* Released through deactivate_super(sb) from get_sb_nodev */
472 sb->s_root = d_alloc(NULL, &(const struct qstr) {
473 .hash = 0,.name = "/",.len = 1});
474 if (!sb->s_root) {
475 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
476 rc = -ENOMEM;
477 goto out;
478 }
479 sb->s_root->d_op = &ecryptfs_dops;
480 sb->s_root->d_sb = sb;
481 sb->s_root->d_parent = sb->s_root;
482 /* Released in d_release when dput(sb->s_root) is called */
483 /* through deactivate_super(sb) from get_sb_nodev() */
484 ecryptfs_set_dentry_private(sb->s_root,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800485 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800486 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700487 if (!ecryptfs_dentry_to_private(sb->s_root)) {
488 ecryptfs_printk(KERN_ERR,
489 "dentry_info_cache alloc failed\n");
490 rc = -ENOMEM;
491 goto out;
492 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700493 rc = 0;
494out:
495 /* Should be able to rely on deactivate_super called from
496 * get_sb_nodev */
497 return rc;
498}
499
500/**
501 * ecryptfs_read_super
502 * @sb: The ecryptfs super block
503 * @dev_name: The path to mount over
504 *
505 * Read the super block of the lower filesystem, and use
506 * ecryptfs_interpose to create our initial inode and super block
507 * struct.
508 */
509static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
510{
511 int rc;
512 struct nameidata nd;
513 struct dentry *lower_root;
514 struct vfsmount *lower_mnt;
515
516 memset(&nd, 0, sizeof(struct nameidata));
Dmitriy Monakhov82b165282007-03-05 00:30:46 -0800517 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
Michael Halcrow237fead2006-10-04 02:16:22 -0700518 if (rc) {
519 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
Michael Halcrow65dc8142007-02-28 20:12:57 -0800520 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700521 }
522 lower_root = nd.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700523 lower_mnt = nd.mnt;
524 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
525 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
Eric Sandeen7c9e70e2007-12-17 16:20:07 -0800526 sb->s_blocksize = lower_root->d_sb->s_blocksize;
Michael Halcrow237fead2006-10-04 02:16:22 -0700527 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
528 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700529 rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
530 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700531 goto out_free;
532 rc = 0;
533 goto out;
534out_free:
535 path_release(&nd);
536out:
537 return rc;
538}
539
540/**
541 * ecryptfs_get_sb
542 * @fs_type
543 * @flags
544 * @dev_name: The path to mount over
545 * @raw_data: The options passed into the kernel
546 *
547 * The whole ecryptfs_get_sb process is broken into 4 functions:
548 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
549 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
550 * with as much information as it can before needing
551 * the lower filesystem.
552 * ecryptfs_read_super(): this accesses the lower filesystem and uses
553 * ecryptfs_interpolate to perform most of the linking
554 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
555 */
556static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
557 const char *dev_name, void *raw_data,
558 struct vfsmount *mnt)
559{
560 int rc;
561 struct super_block *sb;
562
563 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
564 if (rc < 0) {
565 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
566 goto out;
567 }
568 sb = mnt->mnt_sb;
569 rc = ecryptfs_parse_options(sb, raw_data);
570 if (rc) {
571 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
572 goto out_abort;
573 }
574 rc = ecryptfs_read_super(sb, dev_name);
575 if (rc) {
576 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
577 goto out_abort;
578 }
579 goto out;
580out_abort:
581 dput(sb->s_root);
582 up_write(&sb->s_umount);
583 deactivate_super(sb);
584out:
585 return rc;
586}
587
588/**
589 * ecryptfs_kill_block_super
590 * @sb: The ecryptfs super block
591 *
592 * Used to bring the superblock down and free the private data.
593 * Private data is free'd in ecryptfs_put_super()
594 */
595static void ecryptfs_kill_block_super(struct super_block *sb)
596{
597 generic_shutdown_super(sb);
598}
599
600static struct file_system_type ecryptfs_fs_type = {
601 .owner = THIS_MODULE,
602 .name = "ecryptfs",
603 .get_sb = ecryptfs_get_sb,
604 .kill_sb = ecryptfs_kill_block_super,
605 .fs_flags = 0
606};
607
608/**
609 * inode_info_init_once
610 *
611 * Initializes the ecryptfs_inode_info_cache when it is created
612 */
613static void
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700614inode_info_init_once(struct kmem_cache *cachep, void *vptr)
Michael Halcrow237fead2006-10-04 02:16:22 -0700615{
616 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
617
Christoph Lametera35afb82007-05-16 22:10:57 -0700618 inode_init_once(&ei->vfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700619}
620
621static struct ecryptfs_cache_info {
Christoph Lametere18b8902006-12-06 20:33:20 -0800622 struct kmem_cache **cache;
Michael Halcrow237fead2006-10-04 02:16:22 -0700623 const char *name;
624 size_t size;
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700625 void (*ctor)(struct kmem_cache *cache, void *obj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700626} ecryptfs_cache_infos[] = {
627 {
628 .cache = &ecryptfs_auth_tok_list_item_cache,
629 .name = "ecryptfs_auth_tok_list_item",
630 .size = sizeof(struct ecryptfs_auth_tok_list_item),
631 },
632 {
633 .cache = &ecryptfs_file_info_cache,
634 .name = "ecryptfs_file_cache",
635 .size = sizeof(struct ecryptfs_file_info),
636 },
637 {
638 .cache = &ecryptfs_dentry_info_cache,
639 .name = "ecryptfs_dentry_info_cache",
640 .size = sizeof(struct ecryptfs_dentry_info),
641 },
642 {
643 .cache = &ecryptfs_inode_info_cache,
644 .name = "ecryptfs_inode_cache",
645 .size = sizeof(struct ecryptfs_inode_info),
646 .ctor = inode_info_init_once,
647 },
648 {
649 .cache = &ecryptfs_sb_info_cache,
650 .name = "ecryptfs_sb_cache",
651 .size = sizeof(struct ecryptfs_sb_info),
652 },
653 {
654 .cache = &ecryptfs_header_cache_0,
655 .name = "ecryptfs_headers_0",
656 .size = PAGE_CACHE_SIZE,
657 },
658 {
659 .cache = &ecryptfs_header_cache_1,
660 .name = "ecryptfs_headers_1",
661 .size = PAGE_CACHE_SIZE,
662 },
663 {
664 .cache = &ecryptfs_header_cache_2,
665 .name = "ecryptfs_headers_2",
666 .size = PAGE_CACHE_SIZE,
667 },
668 {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800669 .cache = &ecryptfs_xattr_cache,
670 .name = "ecryptfs_xattr_cache",
671 .size = PAGE_CACHE_SIZE,
672 },
673 {
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800674 .cache = &ecryptfs_key_record_cache,
675 .name = "ecryptfs_key_record_cache",
676 .size = sizeof(struct ecryptfs_key_record),
677 },
Michael Halcrow956159c2007-10-16 01:27:55 -0700678 {
679 .cache = &ecryptfs_key_sig_cache,
680 .name = "ecryptfs_key_sig_cache",
681 .size = sizeof(struct ecryptfs_key_sig),
682 },
683 {
684 .cache = &ecryptfs_global_auth_tok_cache,
685 .name = "ecryptfs_global_auth_tok_cache",
686 .size = sizeof(struct ecryptfs_global_auth_tok),
687 },
688 {
689 .cache = &ecryptfs_key_tfm_cache,
690 .name = "ecryptfs_key_tfm_cache",
691 .size = sizeof(struct ecryptfs_key_tfm),
692 },
Michael Halcrow237fead2006-10-04 02:16:22 -0700693};
694
695static void ecryptfs_free_kmem_caches(void)
696{
697 int i;
698
699 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
700 struct ecryptfs_cache_info *info;
701
702 info = &ecryptfs_cache_infos[i];
703 if (*(info->cache))
704 kmem_cache_destroy(*(info->cache));
705 }
706}
707
708/**
709 * ecryptfs_init_kmem_caches
710 *
711 * Returns zero on success; non-zero otherwise
712 */
713static int ecryptfs_init_kmem_caches(void)
714{
715 int i;
716
717 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
718 struct ecryptfs_cache_info *info;
719
720 info = &ecryptfs_cache_infos[i];
721 *(info->cache) = kmem_cache_create(info->name, info->size,
Paul Mundt20c2df82007-07-20 10:11:58 +0900722 0, SLAB_HWCACHE_ALIGN, info->ctor);
Michael Halcrow237fead2006-10-04 02:16:22 -0700723 if (!*(info->cache)) {
724 ecryptfs_free_kmem_caches();
725 ecryptfs_printk(KERN_WARNING, "%s: "
726 "kmem_cache_create failed\n",
727 info->name);
728 return -ENOMEM;
729 }
730 }
731 return 0;
732}
733
734struct ecryptfs_obj {
735 char *name;
736 struct list_head slot_list;
737 struct kobject kobj;
738};
739
740struct ecryptfs_attribute {
741 struct attribute attr;
742 ssize_t(*show) (struct ecryptfs_obj *, char *);
743 ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
744};
745
746static ssize_t
747ecryptfs_attr_store(struct kobject *kobj,
748 struct attribute *attr, const char *buf, size_t len)
749{
750 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
751 kobj);
752 struct ecryptfs_attribute *attribute =
753 container_of(attr, struct ecryptfs_attribute, attr);
754
755 return (attribute->store ? attribute->store(obj, buf, len) : 0);
756}
757
758static ssize_t
759ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
760{
761 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
762 kobj);
763 struct ecryptfs_attribute *attribute =
764 container_of(attr, struct ecryptfs_attribute, attr);
765
766 return (attribute->show ? attribute->show(obj, buf) : 0);
767}
768
769static struct sysfs_ops ecryptfs_sysfs_ops = {
770 .show = ecryptfs_attr_show,
771 .store = ecryptfs_attr_store
772};
773
774static struct kobj_type ecryptfs_ktype = {
775 .sysfs_ops = &ecryptfs_sysfs_ops
776};
777
778static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
779
780static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
781{
782 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
783}
784
785static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
786
Adrian Bunk8487f2e2006-12-06 20:38:25 -0800787static struct ecryptfs_version_str_map_elem {
Michael Halcrow237fead2006-10-04 02:16:22 -0700788 u32 flag;
789 char *str;
790} ecryptfs_version_str_map[] = {
791 {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
792 {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
793 {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800794 {ECRYPTFS_VERSIONING_POLICY, "policy"},
Michael Halcrow956159c2007-10-16 01:27:55 -0700795 {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"},
796 {ECRYPTFS_VERSIONING_MULTKEY, "multiple keys per file"}
Michael Halcrow237fead2006-10-04 02:16:22 -0700797};
798
799static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
800{
801 int i;
802 int remaining = PAGE_SIZE;
803 int total_written = 0;
804
805 buff[0] = '\0';
806 for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
807 int entry_size;
808
809 if (!(ECRYPTFS_VERSIONING_MASK
810 & ecryptfs_version_str_map[i].flag))
811 continue;
812 entry_size = strlen(ecryptfs_version_str_map[i].str);
813 if ((entry_size + 2) > remaining)
814 goto out;
815 memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
816 buff[entry_size++] = '\n';
817 buff[entry_size] = '\0';
818 buff += entry_size;
819 total_written += entry_size;
820 remaining -= entry_size;
821 }
822out:
823 return total_written;
824}
825
826static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
827
828static int do_sysfs_registration(void)
829{
830 int rc;
831
Michael Halcrow5dda6992007-10-16 01:28:06 -0700832 rc = subsystem_register(&ecryptfs_subsys);
833 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700834 printk(KERN_ERR
835 "Unable to register ecryptfs sysfs subsystem\n");
836 goto out;
837 }
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700838 rc = sysfs_create_file(&ecryptfs_subsys.kobj,
Michael Halcrow237fead2006-10-04 02:16:22 -0700839 &sysfs_attr_version.attr);
840 if (rc) {
841 printk(KERN_ERR
842 "Unable to create ecryptfs version attribute\n");
843 subsystem_unregister(&ecryptfs_subsys);
844 goto out;
845 }
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700846 rc = sysfs_create_file(&ecryptfs_subsys.kobj,
Michael Halcrow237fead2006-10-04 02:16:22 -0700847 &sysfs_attr_version_str.attr);
848 if (rc) {
849 printk(KERN_ERR
850 "Unable to create ecryptfs version_str attribute\n");
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700851 sysfs_remove_file(&ecryptfs_subsys.kobj,
Michael Halcrow237fead2006-10-04 02:16:22 -0700852 &sysfs_attr_version.attr);
853 subsystem_unregister(&ecryptfs_subsys);
854 goto out;
855 }
856out:
857 return rc;
858}
859
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700860static void do_sysfs_unregistration(void)
861{
862 sysfs_remove_file(&ecryptfs_subsys.kobj,
863 &sysfs_attr_version.attr);
864 sysfs_remove_file(&ecryptfs_subsys.kobj,
865 &sysfs_attr_version_str.attr);
866 subsystem_unregister(&ecryptfs_subsys);
867}
868
Michael Halcrow237fead2006-10-04 02:16:22 -0700869static int __init ecryptfs_init(void)
870{
871 int rc;
872
873 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
874 rc = -EINVAL;
875 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
876 "larger than the host's page size, and so "
877 "eCryptfs cannot run on this system. The "
878 "default eCryptfs extent size is [%d] bytes; "
879 "the page size is [%d] bytes.\n",
880 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
881 goto out;
882 }
883 rc = ecryptfs_init_kmem_caches();
884 if (rc) {
885 printk(KERN_ERR
886 "Failed to allocate one or more kmem_cache objects\n");
887 goto out;
888 }
889 rc = register_filesystem(&ecryptfs_fs_type);
890 if (rc) {
891 printk(KERN_ERR "Failed to register filesystem\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700892 goto out_free_kmem_caches;
Michael Halcrow237fead2006-10-04 02:16:22 -0700893 }
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700894 kobj_set_kset_s(&ecryptfs_subsys, fs_subsys);
Michael Halcrow237fead2006-10-04 02:16:22 -0700895 rc = do_sysfs_registration();
896 if (rc) {
897 printk(KERN_ERR "sysfs registration failed\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700898 goto out_unregister_filesystem;
Michael Halcrow237fead2006-10-04 02:16:22 -0700899 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800900 rc = ecryptfs_init_messaging(ecryptfs_transport);
901 if (rc) {
902 ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
903 "initialize the eCryptfs netlink socket\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700904 goto out_do_sysfs_unregistration;
Michael Halcrow956159c2007-10-16 01:27:55 -0700905 }
906 rc = ecryptfs_init_crypto();
907 if (rc) {
908 printk(KERN_ERR "Failure whilst attempting to init crypto; "
909 "rc = [%d]\n", rc);
Michael Halcrowcf81f892007-10-16 01:28:07 -0700910 goto out_release_messaging;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800911 }
Michael Halcrowcf81f892007-10-16 01:28:07 -0700912 goto out;
913out_release_messaging:
914 ecryptfs_release_messaging(ecryptfs_transport);
915out_do_sysfs_unregistration:
916 do_sysfs_unregistration();
917out_unregister_filesystem:
918 unregister_filesystem(&ecryptfs_fs_type);
919out_free_kmem_caches:
920 ecryptfs_free_kmem_caches();
Michael Halcrow237fead2006-10-04 02:16:22 -0700921out:
922 return rc;
923}
924
925static void __exit ecryptfs_exit(void)
926{
Michael Halcrowcf81f892007-10-16 01:28:07 -0700927 int rc;
928
929 rc = ecryptfs_destroy_crypto();
930 if (rc)
931 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
932 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800933 ecryptfs_release_messaging(ecryptfs_transport);
Michael Halcrowcf81f892007-10-16 01:28:07 -0700934 do_sysfs_unregistration();
Michael Halcrow237fead2006-10-04 02:16:22 -0700935 unregister_filesystem(&ecryptfs_fs_type);
936 ecryptfs_free_kmem_caches();
937}
938
939MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
940MODULE_DESCRIPTION("eCryptfs");
941
942MODULE_LICENSE("GPL");
943
944module_init(ecryptfs_init)
945module_exit(ecryptfs_exit)