blob: ee4f84b204106aa3b2c4ce6827212f754650b663 [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 */
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700120int 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
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700152 * @flags: flags to govern behavior of interpose procedure
Michael Halcrow237fead2006-10-04 02:16:22 -0700153 *
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,
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700159 struct super_block *sb, u32 flags)
Michael Halcrow237fead2006-10-04 02:16:22 -0700160{
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;
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700196 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
Michael Halcrow237fead2006-10-04 02:16:22 -0700197 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 Halcrow72b55ff2008-07-23 21:30:07 -0700204 if (!(flags & ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE)) {
205 rc = ecryptfs_init_persistent_file(dentry);
206 if (rc) {
207 printk(KERN_ERR "%s: Error attempting to initialize "
208 "the persistent file for the dentry with name "
209 "[%s]; rc = [%d]\n", __func__,
210 dentry->d_name.name, rc);
211 goto out;
212 }
213 } else {
214 struct ecryptfs_inode_info *inode_info =
215 ecryptfs_inode_to_private(dentry->d_inode);
216
217 inode_info->lower_file = NULL;
218 inode_info->crypt_stat.flags |= ECRYPTFS_DELAY_PERSISTENT;
Michael Halcrow4981e082007-10-16 01:28:09 -0700219 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700220out:
221 return rc;
222}
223
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800224enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
225 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
226 ecryptfs_opt_ecryptfs_key_bytes,
Michael Halcrow17398952007-02-12 00:53:45 -0800227 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
228 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
Michael Halcrow237fead2006-10-04 02:16:22 -0700229
230static match_table_t tokens = {
231 {ecryptfs_opt_sig, "sig=%s"},
232 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700233 {ecryptfs_opt_cipher, "cipher=%s"},
234 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
235 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
236 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800237 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
238 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700239 {ecryptfs_opt_err, NULL}
240};
241
Michael Halcrowf4aad162007-10-16 01:27:53 -0700242static int ecryptfs_init_global_auth_toks(
243 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700244{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700245 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -0700246 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700247
Michael Halcrowf4aad162007-10-16 01:27:53 -0700248 list_for_each_entry(global_auth_tok,
249 &mount_crypt_stat->global_auth_tok_list,
250 mount_crypt_stat_list) {
Michael Halcrow5dda6992007-10-16 01:28:06 -0700251 rc = ecryptfs_keyring_auth_tok_for_sig(
252 &global_auth_tok->global_auth_tok_key,
253 &global_auth_tok->global_auth_tok,
254 global_auth_tok->sig);
255 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700256 printk(KERN_ERR "Could not find valid key in user "
257 "session keyring for sig specified in mount "
258 "option: [%s]\n", global_auth_tok->sig);
259 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
Eric Sandeen982363c2008-07-23 21:30:04 -0700260 goto out;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700261 } else
262 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700263 }
Eric Sandeen982363c2008-07-23 21:30:04 -0700264out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700265 return rc;
266}
267
Michael Halcrowf4aad162007-10-16 01:27:53 -0700268static void ecryptfs_init_mount_crypt_stat(
269 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
270{
271 memset((void *)mount_crypt_stat, 0,
272 sizeof(struct ecryptfs_mount_crypt_stat));
273 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
274 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
275 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
276}
277
Michael Halcrow237fead2006-10-04 02:16:22 -0700278/**
279 * ecryptfs_parse_options
280 * @sb: The ecryptfs super block
281 * @options: The options pased to the kernel
282 *
283 * Parse mount options:
284 * debug=N - ecryptfs_verbosity level for debug output
285 * sig=XXX - description(signature) of the key to use
286 *
287 * Returns the dentry object of the lower-level (lower/interposed)
288 * directory; We want to mount our stackable file system on top of
289 * that lower directory.
290 *
291 * The signature of the key to use must be the description of a key
292 * already in the keyring. Mounting will fail if the key can not be
293 * found.
294 *
295 * Returns zero on success; non-zero on error
296 */
297static int ecryptfs_parse_options(struct super_block *sb, char *options)
298{
299 char *p;
300 int rc = 0;
301 int sig_set = 0;
302 int cipher_name_set = 0;
303 int cipher_key_bytes;
304 int cipher_key_bytes_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700305 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
306 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
307 substring_t args[MAX_OPT_ARGS];
308 int token;
309 char *sig_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700310 char *cipher_name_dst;
311 char *cipher_name_src;
312 char *cipher_key_bytes_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700313
314 if (!options) {
315 rc = -EINVAL;
316 goto out;
317 }
Michael Halcrow956159c2007-10-16 01:27:55 -0700318 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700319 while ((p = strsep(&options, ",")) != NULL) {
320 if (!*p)
321 continue;
322 token = match_token(p, tokens, args);
323 switch (token) {
324 case ecryptfs_opt_sig:
325 case ecryptfs_opt_ecryptfs_sig:
326 sig_src = args[0].from;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700327 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
328 sig_src);
329 if (rc) {
330 printk(KERN_ERR "Error attempting to register "
331 "global sig; rc = [%d]\n", rc);
332 goto out;
333 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700334 sig_set = 1;
335 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700336 case ecryptfs_opt_cipher:
337 case ecryptfs_opt_ecryptfs_cipher:
338 cipher_name_src = args[0].from;
339 cipher_name_dst =
340 mount_crypt_stat->
341 global_default_cipher_name;
342 strncpy(cipher_name_dst, cipher_name_src,
343 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
344 ecryptfs_printk(KERN_DEBUG,
345 "The mount_crypt_stat "
346 "global_default_cipher_name set to: "
347 "[%s]\n", cipher_name_dst);
348 cipher_name_set = 1;
349 break;
350 case ecryptfs_opt_ecryptfs_key_bytes:
351 cipher_key_bytes_src = args[0].from;
352 cipher_key_bytes =
353 (int)simple_strtol(cipher_key_bytes_src,
354 &cipher_key_bytes_src, 0);
355 mount_crypt_stat->global_default_cipher_key_size =
356 cipher_key_bytes;
357 ecryptfs_printk(KERN_DEBUG,
358 "The mount_crypt_stat "
359 "global_default_cipher_key_size "
360 "set to: [%d]\n", mount_crypt_stat->
361 global_default_cipher_key_size);
362 cipher_key_bytes_set = 1;
363 break;
364 case ecryptfs_opt_passthrough:
365 mount_crypt_stat->flags |=
366 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
367 break;
Michael Halcrow17398952007-02-12 00:53:45 -0800368 case ecryptfs_opt_xattr_metadata:
369 mount_crypt_stat->flags |=
370 ECRYPTFS_XATTR_METADATA_ENABLED;
371 break;
372 case ecryptfs_opt_encrypted_view:
373 mount_crypt_stat->flags |=
374 ECRYPTFS_XATTR_METADATA_ENABLED;
375 mount_crypt_stat->flags |=
376 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
377 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700378 case ecryptfs_opt_err:
379 default:
380 ecryptfs_printk(KERN_WARNING,
381 "eCryptfs: unrecognized option '%s'\n",
382 p);
383 }
384 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700385 if (!sig_set) {
386 rc = -EINVAL;
Michael Halcrow956159c2007-10-16 01:27:55 -0700387 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
388 "auth tok signature as a mount "
Michael Halcrow237fead2006-10-04 02:16:22 -0700389 "parameter; see the eCryptfs README\n");
390 goto out;
391 }
392 if (!cipher_name_set) {
Miklos Szeredi8f236802008-07-23 21:30:05 -0700393 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
394
395 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
396
397 strcpy(mount_crypt_stat->global_default_cipher_name,
398 ECRYPTFS_DEFAULT_CIPHER);
Michael Halcrow237fead2006-10-04 02:16:22 -0700399 }
400 if (!cipher_key_bytes_set) {
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800401 mount_crypt_stat->global_default_cipher_key_size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700402 }
Eric Sandeenaf440f52008-02-06 01:38:37 -0800403 mutex_lock(&key_tfm_list_mutex);
404 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
405 NULL))
406 rc = ecryptfs_add_new_key_tfm(
407 NULL, mount_crypt_stat->global_default_cipher_name,
408 mount_crypt_stat->global_default_cipher_key_size);
409 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700410 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700411 printk(KERN_ERR "Error attempting to initialize cipher with "
Andrew Morton81acbcd2007-10-16 01:27:59 -0700412 "name = [%s] and key size = [%td]; rc = [%d]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700413 mount_crypt_stat->global_default_cipher_name,
414 mount_crypt_stat->global_default_cipher_key_size, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700415 rc = -EINVAL;
416 goto out;
417 }
Michael Halcrow5dda6992007-10-16 01:28:06 -0700418 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
419 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700420 printk(KERN_WARNING "One or more global auth toks could not "
421 "properly register; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700422 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700423out:
424 return rc;
425}
426
427struct kmem_cache *ecryptfs_sb_info_cache;
428
429/**
430 * ecryptfs_fill_super
431 * @sb: The ecryptfs super block
432 * @raw_data: The options passed to mount
433 * @silent: Not used but required by function prototype
434 *
435 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
436 *
437 * Returns zero on success; non-zero otherwise
438 */
439static int
440ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
441{
442 int rc = 0;
443
444 /* Released in ecryptfs_put_super() */
445 ecryptfs_set_superblock_private(sb,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800446 kmem_cache_zalloc(ecryptfs_sb_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800447 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700448 if (!ecryptfs_superblock_to_private(sb)) {
449 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
450 rc = -ENOMEM;
451 goto out;
452 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700453 sb->s_op = &ecryptfs_sops;
454 /* Released through deactivate_super(sb) from get_sb_nodev */
455 sb->s_root = d_alloc(NULL, &(const struct qstr) {
456 .hash = 0,.name = "/",.len = 1});
457 if (!sb->s_root) {
458 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
459 rc = -ENOMEM;
460 goto out;
461 }
462 sb->s_root->d_op = &ecryptfs_dops;
463 sb->s_root->d_sb = sb;
464 sb->s_root->d_parent = sb->s_root;
465 /* Released in d_release when dput(sb->s_root) is called */
466 /* through deactivate_super(sb) from get_sb_nodev() */
467 ecryptfs_set_dentry_private(sb->s_root,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800468 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800469 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700470 if (!ecryptfs_dentry_to_private(sb->s_root)) {
471 ecryptfs_printk(KERN_ERR,
472 "dentry_info_cache alloc failed\n");
473 rc = -ENOMEM;
474 goto out;
475 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700476 rc = 0;
477out:
478 /* Should be able to rely on deactivate_super called from
479 * get_sb_nodev */
480 return rc;
481}
482
483/**
484 * ecryptfs_read_super
485 * @sb: The ecryptfs super block
486 * @dev_name: The path to mount over
487 *
488 * Read the super block of the lower filesystem, and use
489 * ecryptfs_interpose to create our initial inode and super block
490 * struct.
491 */
492static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
493{
494 int rc;
495 struct nameidata nd;
496 struct dentry *lower_root;
497 struct vfsmount *lower_mnt;
498
499 memset(&nd, 0, sizeof(struct nameidata));
Dmitriy Monakhov82b16522007-03-05 00:30:46 -0800500 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
Michael Halcrow237fead2006-10-04 02:16:22 -0700501 if (rc) {
502 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
Michael Halcrow65dc8142007-02-28 20:12:57 -0800503 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700504 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800505 lower_root = nd.path.dentry;
506 lower_mnt = nd.path.mnt;
Michael Halcrow237fead2006-10-04 02:16:22 -0700507 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
508 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
Eric Sandeen7c9e70e2007-12-17 16:20:07 -0800509 sb->s_blocksize = lower_root->d_sb->s_blocksize;
Michael Halcrow237fead2006-10-04 02:16:22 -0700510 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
511 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700512 rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
513 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700514 goto out_free;
515 rc = 0;
516 goto out;
517out_free:
Jan Blunck1d957f92008-02-14 19:34:35 -0800518 path_put(&nd.path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700519out:
520 return rc;
521}
522
523/**
524 * ecryptfs_get_sb
525 * @fs_type
526 * @flags
527 * @dev_name: The path to mount over
528 * @raw_data: The options passed into the kernel
529 *
530 * The whole ecryptfs_get_sb process is broken into 4 functions:
531 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
532 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
533 * with as much information as it can before needing
534 * the lower filesystem.
535 * ecryptfs_read_super(): this accesses the lower filesystem and uses
536 * ecryptfs_interpolate to perform most of the linking
537 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
538 */
539static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
540 const char *dev_name, void *raw_data,
541 struct vfsmount *mnt)
542{
543 int rc;
544 struct super_block *sb;
545
546 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
547 if (rc < 0) {
548 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
549 goto out;
550 }
551 sb = mnt->mnt_sb;
552 rc = ecryptfs_parse_options(sb, raw_data);
553 if (rc) {
554 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
555 goto out_abort;
556 }
557 rc = ecryptfs_read_super(sb, dev_name);
558 if (rc) {
559 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
560 goto out_abort;
561 }
562 goto out;
563out_abort:
564 dput(sb->s_root);
565 up_write(&sb->s_umount);
566 deactivate_super(sb);
567out:
568 return rc;
569}
570
571/**
572 * ecryptfs_kill_block_super
573 * @sb: The ecryptfs super block
574 *
575 * Used to bring the superblock down and free the private data.
576 * Private data is free'd in ecryptfs_put_super()
577 */
578static void ecryptfs_kill_block_super(struct super_block *sb)
579{
580 generic_shutdown_super(sb);
581}
582
583static struct file_system_type ecryptfs_fs_type = {
584 .owner = THIS_MODULE,
585 .name = "ecryptfs",
586 .get_sb = ecryptfs_get_sb,
587 .kill_sb = ecryptfs_kill_block_super,
588 .fs_flags = 0
589};
590
591/**
592 * inode_info_init_once
593 *
594 * Initializes the ecryptfs_inode_info_cache when it is created
595 */
596static void
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700597inode_info_init_once(struct kmem_cache *cachep, void *vptr)
Michael Halcrow237fead2006-10-04 02:16:22 -0700598{
599 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
600
Christoph Lametera35afb82007-05-16 22:10:57 -0700601 inode_init_once(&ei->vfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700602}
603
604static struct ecryptfs_cache_info {
Christoph Lametere18b8902006-12-06 20:33:20 -0800605 struct kmem_cache **cache;
Michael Halcrow237fead2006-10-04 02:16:22 -0700606 const char *name;
607 size_t size;
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700608 void (*ctor)(struct kmem_cache *cache, void *obj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700609} ecryptfs_cache_infos[] = {
610 {
611 .cache = &ecryptfs_auth_tok_list_item_cache,
612 .name = "ecryptfs_auth_tok_list_item",
613 .size = sizeof(struct ecryptfs_auth_tok_list_item),
614 },
615 {
616 .cache = &ecryptfs_file_info_cache,
617 .name = "ecryptfs_file_cache",
618 .size = sizeof(struct ecryptfs_file_info),
619 },
620 {
621 .cache = &ecryptfs_dentry_info_cache,
622 .name = "ecryptfs_dentry_info_cache",
623 .size = sizeof(struct ecryptfs_dentry_info),
624 },
625 {
626 .cache = &ecryptfs_inode_info_cache,
627 .name = "ecryptfs_inode_cache",
628 .size = sizeof(struct ecryptfs_inode_info),
629 .ctor = inode_info_init_once,
630 },
631 {
632 .cache = &ecryptfs_sb_info_cache,
633 .name = "ecryptfs_sb_cache",
634 .size = sizeof(struct ecryptfs_sb_info),
635 },
636 {
Michael Halcrow237fead2006-10-04 02:16:22 -0700637 .cache = &ecryptfs_header_cache_1,
638 .name = "ecryptfs_headers_1",
639 .size = PAGE_CACHE_SIZE,
640 },
641 {
642 .cache = &ecryptfs_header_cache_2,
643 .name = "ecryptfs_headers_2",
644 .size = PAGE_CACHE_SIZE,
645 },
646 {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800647 .cache = &ecryptfs_xattr_cache,
648 .name = "ecryptfs_xattr_cache",
649 .size = PAGE_CACHE_SIZE,
650 },
651 {
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800652 .cache = &ecryptfs_key_record_cache,
653 .name = "ecryptfs_key_record_cache",
654 .size = sizeof(struct ecryptfs_key_record),
655 },
Michael Halcrow956159c2007-10-16 01:27:55 -0700656 {
657 .cache = &ecryptfs_key_sig_cache,
658 .name = "ecryptfs_key_sig_cache",
659 .size = sizeof(struct ecryptfs_key_sig),
660 },
661 {
662 .cache = &ecryptfs_global_auth_tok_cache,
663 .name = "ecryptfs_global_auth_tok_cache",
664 .size = sizeof(struct ecryptfs_global_auth_tok),
665 },
666 {
667 .cache = &ecryptfs_key_tfm_cache,
668 .name = "ecryptfs_key_tfm_cache",
669 .size = sizeof(struct ecryptfs_key_tfm),
670 },
Michael Halcrow746f1e52008-07-23 21:30:02 -0700671 {
672 .cache = &ecryptfs_open_req_cache,
673 .name = "ecryptfs_open_req_cache",
674 .size = sizeof(struct ecryptfs_open_req),
675 },
Michael Halcrow237fead2006-10-04 02:16:22 -0700676};
677
678static void ecryptfs_free_kmem_caches(void)
679{
680 int i;
681
682 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
683 struct ecryptfs_cache_info *info;
684
685 info = &ecryptfs_cache_infos[i];
686 if (*(info->cache))
687 kmem_cache_destroy(*(info->cache));
688 }
689}
690
691/**
692 * ecryptfs_init_kmem_caches
693 *
694 * Returns zero on success; non-zero otherwise
695 */
696static int ecryptfs_init_kmem_caches(void)
697{
698 int i;
699
700 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
701 struct ecryptfs_cache_info *info;
702
703 info = &ecryptfs_cache_infos[i];
704 *(info->cache) = kmem_cache_create(info->name, info->size,
Paul Mundt20c2df82007-07-20 10:11:58 +0900705 0, SLAB_HWCACHE_ALIGN, info->ctor);
Michael Halcrow237fead2006-10-04 02:16:22 -0700706 if (!*(info->cache)) {
707 ecryptfs_free_kmem_caches();
708 ecryptfs_printk(KERN_WARNING, "%s: "
709 "kmem_cache_create failed\n",
710 info->name);
711 return -ENOMEM;
712 }
713 }
714 return 0;
715}
716
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800717static struct kobject *ecryptfs_kobj;
Michael Halcrow237fead2006-10-04 02:16:22 -0700718
Kay Sievers386f2752007-11-02 13:47:53 +0100719static ssize_t version_show(struct kobject *kobj,
720 struct kobj_attribute *attr, char *buff)
Michael Halcrow237fead2006-10-04 02:16:22 -0700721{
722 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
723}
724
Kay Sievers386f2752007-11-02 13:47:53 +0100725static struct kobj_attribute version_attr = __ATTR_RO(version);
Michael Halcrow237fead2006-10-04 02:16:22 -0700726
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700727static struct attribute *attributes[] = {
728 &version_attr.attr,
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700729 NULL,
730};
731
732static struct attribute_group attr_group = {
733 .attrs = attributes,
734};
Michael Halcrow237fead2006-10-04 02:16:22 -0700735
736static int do_sysfs_registration(void)
737{
738 int rc;
739
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800740 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
741 if (!ecryptfs_kobj) {
Greg Kroah-Hartman917e8652007-10-29 20:13:17 +0100742 printk(KERN_ERR "Unable to create ecryptfs kset\n");
743 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700744 goto out;
745 }
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800746 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
Michael Halcrow237fead2006-10-04 02:16:22 -0700747 if (rc) {
748 printk(KERN_ERR
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700749 "Unable to create ecryptfs version attributes\n");
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800750 kobject_put(ecryptfs_kobj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700751 }
752out:
753 return rc;
754}
755
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700756static void do_sysfs_unregistration(void)
757{
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800758 sysfs_remove_group(ecryptfs_kobj, &attr_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800759 kobject_put(ecryptfs_kobj);
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700760}
761
Michael Halcrow237fead2006-10-04 02:16:22 -0700762static int __init ecryptfs_init(void)
763{
764 int rc;
765
766 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
767 rc = -EINVAL;
768 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
769 "larger than the host's page size, and so "
770 "eCryptfs cannot run on this system. The "
771 "default eCryptfs extent size is [%d] bytes; "
772 "the page size is [%d] bytes.\n",
773 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
774 goto out;
775 }
776 rc = ecryptfs_init_kmem_caches();
777 if (rc) {
778 printk(KERN_ERR
779 "Failed to allocate one or more kmem_cache objects\n");
780 goto out;
781 }
782 rc = register_filesystem(&ecryptfs_fs_type);
783 if (rc) {
784 printk(KERN_ERR "Failed to register filesystem\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700785 goto out_free_kmem_caches;
Michael Halcrow237fead2006-10-04 02:16:22 -0700786 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700787 rc = do_sysfs_registration();
788 if (rc) {
789 printk(KERN_ERR "sysfs registration failed\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700790 goto out_unregister_filesystem;
Michael Halcrow237fead2006-10-04 02:16:22 -0700791 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700792 rc = ecryptfs_init_kthread();
793 if (rc) {
794 printk(KERN_ERR "%s: kthread initialization failed; "
795 "rc = [%d]\n", __func__, rc);
796 goto out_do_sysfs_unregistration;
797 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800798 rc = ecryptfs_init_messaging(ecryptfs_transport);
799 if (rc) {
Michael Halcrow746f1e52008-07-23 21:30:02 -0700800 printk(KERN_ERR "Failure occured while attempting to "
Michael Halcrowdddfa462007-02-12 00:53:44 -0800801 "initialize the eCryptfs netlink socket\n");
Michael Halcrow746f1e52008-07-23 21:30:02 -0700802 goto out_destroy_kthread;
Michael Halcrow956159c2007-10-16 01:27:55 -0700803 }
804 rc = ecryptfs_init_crypto();
805 if (rc) {
806 printk(KERN_ERR "Failure whilst attempting to init crypto; "
807 "rc = [%d]\n", rc);
Michael Halcrowcf81f892007-10-16 01:28:07 -0700808 goto out_release_messaging;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800809 }
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800810 if (ecryptfs_verbosity > 0)
811 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
812 "will be written to the syslog!\n", ecryptfs_verbosity);
813
Michael Halcrowcf81f892007-10-16 01:28:07 -0700814 goto out;
815out_release_messaging:
816 ecryptfs_release_messaging(ecryptfs_transport);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700817out_destroy_kthread:
818 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700819out_do_sysfs_unregistration:
820 do_sysfs_unregistration();
821out_unregister_filesystem:
822 unregister_filesystem(&ecryptfs_fs_type);
823out_free_kmem_caches:
824 ecryptfs_free_kmem_caches();
Michael Halcrow237fead2006-10-04 02:16:22 -0700825out:
826 return rc;
827}
828
829static void __exit ecryptfs_exit(void)
830{
Michael Halcrowcf81f892007-10-16 01:28:07 -0700831 int rc;
832
833 rc = ecryptfs_destroy_crypto();
834 if (rc)
835 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
836 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800837 ecryptfs_release_messaging(ecryptfs_transport);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700838 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700839 do_sysfs_unregistration();
Michael Halcrow237fead2006-10-04 02:16:22 -0700840 unregister_filesystem(&ecryptfs_fs_type);
841 ecryptfs_free_kmem_caches();
842}
843
844MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
845MODULE_DESCRIPTION("eCryptfs");
846
847MODULE_LICENSE("GPL");
848
849module_init(ecryptfs_init)
850module_exit(ecryptfs_exit)