blob: 789cf2e1be1e65f545ee1e746eab4fdf66ca190a [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2003 Erez Zadok
5 * Copyright (C) 2001-2003 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -08009 * Tyler Hicks <tyhicks@ou.edu>
Michael Halcrow237fead2006-10-04 02:16:22 -070010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 */
26
27#include <linux/dcache.h>
28#include <linux/file.h>
29#include <linux/module.h>
30#include <linux/namei.h>
31#include <linux/skbuff.h>
32#include <linux/crypto.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070033#include <linux/mount.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070034#include <linux/pagemap.h>
35#include <linux/key.h>
36#include <linux/parser.h>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080037#include <linux/fs_stack.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070038#include "ecryptfs_kernel.h"
39
40/**
41 * Module parameter that defines the ecryptfs_verbosity level.
42 */
43int ecryptfs_verbosity = 0;
44
45module_param(ecryptfs_verbosity, int, 0);
46MODULE_PARM_DESC(ecryptfs_verbosity,
47 "Initial verbosity level (0 or 1; defaults to "
48 "0, which is Quiet)");
49
Michael Halcrowdddfa462007-02-12 00:53:44 -080050/**
Tyler Hicks624ae522008-10-15 22:02:51 -070051 * Module parameter that defines the number of message buffer elements
Michael Halcrowdddfa462007-02-12 00:53:44 -080052 */
53unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
54
55module_param(ecryptfs_message_buf_len, uint, 0);
56MODULE_PARM_DESC(ecryptfs_message_buf_len,
57 "Number of message buffer elements");
58
59/**
60 * Module parameter that defines the maximum guaranteed amount of time to wait
Tyler Hicks624ae522008-10-15 22:02:51 -070061 * for a response from ecryptfsd. The actual sleep time will be, more than
Michael Halcrowdddfa462007-02-12 00:53:44 -080062 * likely, a small amount greater than this specified value, but only less if
Tyler Hicks624ae522008-10-15 22:02:51 -070063 * the message successfully arrives.
Michael Halcrowdddfa462007-02-12 00:53:44 -080064 */
65signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
66
67module_param(ecryptfs_message_wait_timeout, long, 0);
68MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
69 "Maximum number of seconds that an operation will "
70 "sleep while waiting for a message response from "
71 "userspace");
72
73/**
74 * Module parameter that is an estimate of the maximum number of users
75 * that will be concurrently using eCryptfs. Set this to the right
76 * value to balance performance and memory use.
77 */
78unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
79
80module_param(ecryptfs_number_of_users, uint, 0);
81MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
82 "concurrent users of eCryptfs");
83
Michael Halcrow237fead2006-10-04 02:16:22 -070084void __ecryptfs_printk(const char *fmt, ...)
85{
86 va_list args;
87 va_start(args, fmt);
88 if (fmt[1] == '7') { /* KERN_DEBUG */
89 if (ecryptfs_verbosity >= 1)
90 vprintk(fmt, args);
91 } else
92 vprintk(fmt, args);
93 va_end(args);
94}
95
96/**
Michael Halcrow4981e082007-10-16 01:28:09 -070097 * ecryptfs_init_persistent_file
98 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
99 * the lower dentry and the lower mount set
100 *
101 * eCryptfs only ever keeps a single open file for every lower
102 * inode. All I/O operations to the lower inode occur through that
103 * file. When the first eCryptfs dentry that interposes with the first
104 * lower dentry for that inode is created, this function creates the
105 * persistent file struct and associates it with the eCryptfs
106 * inode. When the eCryptfs inode is destroyed, the file is closed.
107 *
108 * The persistent file will be opened with read/write permissions, if
109 * possible. Otherwise, it is opened read-only.
110 *
111 * This function does nothing if a lower persistent file is already
112 * associated with the eCryptfs inode.
113 *
114 * Returns zero on success; non-zero otherwise
115 */
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700116int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
Michael Halcrow4981e082007-10-16 01:28:09 -0700117{
David Howells745ca242008-11-14 10:39:22 +1100118 const struct cred *cred = current_cred();
Michael Halcrow4981e082007-10-16 01:28:09 -0700119 struct ecryptfs_inode_info *inode_info =
120 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
121 int rc = 0;
122
123 mutex_lock(&inode_info->lower_file_mutex);
124 if (!inode_info->lower_file) {
125 struct dentry *lower_dentry;
126 struct vfsmount *lower_mnt =
127 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
128
129 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700130 rc = ecryptfs_privileged_open(&inode_info->lower_file,
David Howells745ca242008-11-14 10:39:22 +1100131 lower_dentry, lower_mnt, cred);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700132 if (rc || IS_ERR(inode_info->lower_file)) {
Michael Halcrow4981e082007-10-16 01:28:09 -0700133 printk(KERN_ERR "Error opening lower persistent file "
Michael Halcrow746f1e52008-07-23 21:30:02 -0700134 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
135 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
Michael Halcrow4981e082007-10-16 01:28:09 -0700136 rc = PTR_ERR(inode_info->lower_file);
137 inode_info->lower_file = NULL;
138 }
139 }
140 mutex_unlock(&inode_info->lower_file_mutex);
141 return rc;
142}
143
144/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700145 * ecryptfs_interpose
146 * @lower_dentry: Existing dentry in the lower filesystem
147 * @dentry: ecryptfs' dentry
148 * @sb: ecryptfs's super_block
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700149 * @flags: flags to govern behavior of interpose procedure
Michael Halcrow237fead2006-10-04 02:16:22 -0700150 *
151 * Interposes upper and lower dentries.
152 *
153 * Returns zero on success; non-zero otherwise
154 */
155int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700156 struct super_block *sb, u32 flags)
Michael Halcrow237fead2006-10-04 02:16:22 -0700157{
158 struct inode *lower_inode;
159 struct inode *inode;
160 int rc = 0;
161
162 lower_inode = lower_dentry->d_inode;
163 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
164 rc = -EXDEV;
165 goto out;
166 }
167 if (!igrab(lower_inode)) {
168 rc = -ESTALE;
169 goto out;
170 }
171 inode = iget5_locked(sb, (unsigned long)lower_inode,
172 ecryptfs_inode_test, ecryptfs_inode_set,
173 lower_inode);
174 if (!inode) {
175 rc = -EACCES;
176 iput(lower_inode);
177 goto out;
178 }
179 if (inode->i_state & I_NEW)
180 unlock_new_inode(inode);
181 else
182 iput(lower_inode);
183 if (S_ISLNK(lower_inode->i_mode))
184 inode->i_op = &ecryptfs_symlink_iops;
185 else if (S_ISDIR(lower_inode->i_mode))
186 inode->i_op = &ecryptfs_dir_iops;
187 if (S_ISDIR(lower_inode->i_mode))
188 inode->i_fop = &ecryptfs_dir_fops;
Pekka Enberg26da8202006-10-19 23:28:14 -0700189 if (special_file(lower_inode->i_mode))
Michael Halcrow237fead2006-10-04 02:16:22 -0700190 init_special_inode(inode, lower_inode->i_mode,
191 lower_inode->i_rdev);
192 dentry->d_op = &ecryptfs_dops;
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700193 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
Michael Halcrow237fead2006-10-04 02:16:22 -0700194 d_add(dentry, inode);
195 else
196 d_instantiate(dentry, inode);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800197 fsstack_copy_attr_all(inode, lower_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700198 /* This size will be overwritten for real files w/ headers and
199 * other metadata */
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800200 fsstack_copy_inode_size(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700201out:
202 return rc;
203}
204
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800205enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
206 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
207 ecryptfs_opt_ecryptfs_key_bytes,
Michael Halcrow17398952007-02-12 00:53:45 -0800208 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
Michael Halcrow87c94c42009-01-06 14:42:01 -0800209 ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
210 ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
211 ecryptfs_opt_err };
Michael Halcrow237fead2006-10-04 02:16:22 -0700212
Steven Whitehousea447c092008-10-13 10:46:57 +0100213static const match_table_t tokens = {
Michael Halcrow237fead2006-10-04 02:16:22 -0700214 {ecryptfs_opt_sig, "sig=%s"},
215 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700216 {ecryptfs_opt_cipher, "cipher=%s"},
217 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
218 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
219 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800220 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
221 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
Michael Halcrow87c94c42009-01-06 14:42:01 -0800222 {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
223 {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
224 {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700225 {ecryptfs_opt_err, NULL}
226};
227
Michael Halcrowf4aad162007-10-16 01:27:53 -0700228static int ecryptfs_init_global_auth_toks(
229 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700230{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700231 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -0700232 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700233
Michael Halcrowf4aad162007-10-16 01:27:53 -0700234 list_for_each_entry(global_auth_tok,
235 &mount_crypt_stat->global_auth_tok_list,
236 mount_crypt_stat_list) {
Michael Halcrow5dda6992007-10-16 01:28:06 -0700237 rc = ecryptfs_keyring_auth_tok_for_sig(
238 &global_auth_tok->global_auth_tok_key,
239 &global_auth_tok->global_auth_tok,
240 global_auth_tok->sig);
241 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700242 printk(KERN_ERR "Could not find valid key in user "
243 "session keyring for sig specified in mount "
244 "option: [%s]\n", global_auth_tok->sig);
245 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
Eric Sandeen982363c2008-07-23 21:30:04 -0700246 goto out;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700247 } else
248 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700249 }
Eric Sandeen982363c2008-07-23 21:30:04 -0700250out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700251 return rc;
252}
253
Michael Halcrowf4aad162007-10-16 01:27:53 -0700254static void ecryptfs_init_mount_crypt_stat(
255 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
256{
257 memset((void *)mount_crypt_stat, 0,
258 sizeof(struct ecryptfs_mount_crypt_stat));
259 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
260 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
261 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
262}
263
Michael Halcrow237fead2006-10-04 02:16:22 -0700264/**
265 * ecryptfs_parse_options
266 * @sb: The ecryptfs super block
267 * @options: The options pased to the kernel
268 *
269 * Parse mount options:
270 * debug=N - ecryptfs_verbosity level for debug output
271 * sig=XXX - description(signature) of the key to use
272 *
273 * Returns the dentry object of the lower-level (lower/interposed)
274 * directory; We want to mount our stackable file system on top of
275 * that lower directory.
276 *
277 * The signature of the key to use must be the description of a key
278 * already in the keyring. Mounting will fail if the key can not be
279 * found.
280 *
281 * Returns zero on success; non-zero on error
282 */
283static int ecryptfs_parse_options(struct super_block *sb, char *options)
284{
285 char *p;
286 int rc = 0;
287 int sig_set = 0;
288 int cipher_name_set = 0;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800289 int fn_cipher_name_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700290 int cipher_key_bytes;
291 int cipher_key_bytes_set = 0;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800292 int fn_cipher_key_bytes;
293 int fn_cipher_key_bytes_set = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700294 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
295 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
296 substring_t args[MAX_OPT_ARGS];
297 int token;
298 char *sig_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700299 char *cipher_name_dst;
300 char *cipher_name_src;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800301 char *fn_cipher_name_dst;
302 char *fn_cipher_name_src;
303 char *fnek_dst;
304 char *fnek_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700305 char *cipher_key_bytes_src;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800306 char *fn_cipher_key_bytes_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700307
308 if (!options) {
309 rc = -EINVAL;
310 goto out;
311 }
Michael Halcrow956159c2007-10-16 01:27:55 -0700312 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700313 while ((p = strsep(&options, ",")) != NULL) {
314 if (!*p)
315 continue;
316 token = match_token(p, tokens, args);
317 switch (token) {
318 case ecryptfs_opt_sig:
319 case ecryptfs_opt_ecryptfs_sig:
320 sig_src = args[0].from;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700321 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
322 sig_src);
323 if (rc) {
324 printk(KERN_ERR "Error attempting to register "
325 "global sig; rc = [%d]\n", rc);
326 goto out;
327 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700328 sig_set = 1;
329 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700330 case ecryptfs_opt_cipher:
331 case ecryptfs_opt_ecryptfs_cipher:
332 cipher_name_src = args[0].from;
333 cipher_name_dst =
334 mount_crypt_stat->
335 global_default_cipher_name;
336 strncpy(cipher_name_dst, cipher_name_src,
337 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Michael Halcrow87c94c42009-01-06 14:42:01 -0800338 cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
Michael Halcrow237fead2006-10-04 02:16:22 -0700339 cipher_name_set = 1;
340 break;
341 case ecryptfs_opt_ecryptfs_key_bytes:
342 cipher_key_bytes_src = args[0].from;
343 cipher_key_bytes =
344 (int)simple_strtol(cipher_key_bytes_src,
345 &cipher_key_bytes_src, 0);
346 mount_crypt_stat->global_default_cipher_key_size =
347 cipher_key_bytes;
Michael Halcrow237fead2006-10-04 02:16:22 -0700348 cipher_key_bytes_set = 1;
349 break;
350 case ecryptfs_opt_passthrough:
351 mount_crypt_stat->flags |=
352 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
353 break;
Michael Halcrow17398952007-02-12 00:53:45 -0800354 case ecryptfs_opt_xattr_metadata:
355 mount_crypt_stat->flags |=
356 ECRYPTFS_XATTR_METADATA_ENABLED;
357 break;
358 case ecryptfs_opt_encrypted_view:
359 mount_crypt_stat->flags |=
360 ECRYPTFS_XATTR_METADATA_ENABLED;
361 mount_crypt_stat->flags |=
362 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
363 break;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800364 case ecryptfs_opt_fnek_sig:
365 fnek_src = args[0].from;
366 fnek_dst =
367 mount_crypt_stat->global_default_fnek_sig;
368 strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
369 mount_crypt_stat->global_default_fnek_sig[
370 ECRYPTFS_SIG_SIZE_HEX] = '\0';
371 rc = ecryptfs_add_global_auth_tok(
372 mount_crypt_stat,
373 mount_crypt_stat->global_default_fnek_sig);
374 if (rc) {
375 printk(KERN_ERR "Error attempting to register "
376 "global fnek sig [%s]; rc = [%d]\n",
377 mount_crypt_stat->global_default_fnek_sig,
378 rc);
379 goto out;
380 }
381 mount_crypt_stat->flags |=
382 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
383 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
384 break;
385 case ecryptfs_opt_fn_cipher:
386 fn_cipher_name_src = args[0].from;
387 fn_cipher_name_dst =
388 mount_crypt_stat->global_default_fn_cipher_name;
389 strncpy(fn_cipher_name_dst, fn_cipher_name_src,
390 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
391 mount_crypt_stat->global_default_fn_cipher_name[
392 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
393 fn_cipher_name_set = 1;
394 break;
395 case ecryptfs_opt_fn_cipher_key_bytes:
396 fn_cipher_key_bytes_src = args[0].from;
397 fn_cipher_key_bytes =
398 (int)simple_strtol(fn_cipher_key_bytes_src,
399 &fn_cipher_key_bytes_src, 0);
400 mount_crypt_stat->global_default_fn_cipher_key_bytes =
401 fn_cipher_key_bytes;
402 fn_cipher_key_bytes_set = 1;
403 break;
Michael Halcrow237fead2006-10-04 02:16:22 -0700404 case ecryptfs_opt_err:
405 default:
Michael Halcrow87c94c42009-01-06 14:42:01 -0800406 printk(KERN_WARNING
407 "%s: eCryptfs: unrecognized option [%s]\n",
408 __func__, p);
Michael Halcrow237fead2006-10-04 02:16:22 -0700409 }
410 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700411 if (!sig_set) {
412 rc = -EINVAL;
Michael Halcrow956159c2007-10-16 01:27:55 -0700413 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
414 "auth tok signature as a mount "
Michael Halcrow237fead2006-10-04 02:16:22 -0700415 "parameter; see the eCryptfs README\n");
416 goto out;
417 }
418 if (!cipher_name_set) {
Miklos Szeredi8f236802008-07-23 21:30:05 -0700419 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
420
421 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Miklos Szeredi8f236802008-07-23 21:30:05 -0700422 strcpy(mount_crypt_stat->global_default_cipher_name,
423 ECRYPTFS_DEFAULT_CIPHER);
Michael Halcrow237fead2006-10-04 02:16:22 -0700424 }
Michael Halcrow87c94c42009-01-06 14:42:01 -0800425 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
426 && !fn_cipher_name_set)
427 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
428 mount_crypt_stat->global_default_cipher_name);
429 if (!cipher_key_bytes_set)
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800430 mount_crypt_stat->global_default_cipher_key_size = 0;
Michael Halcrow87c94c42009-01-06 14:42:01 -0800431 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
432 && !fn_cipher_key_bytes_set)
433 mount_crypt_stat->global_default_fn_cipher_key_bytes =
434 mount_crypt_stat->global_default_cipher_key_size;
Eric Sandeenaf440f52008-02-06 01:38:37 -0800435 mutex_lock(&key_tfm_list_mutex);
436 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
Michael Halcrow87c94c42009-01-06 14:42:01 -0800437 NULL)) {
Eric Sandeenaf440f52008-02-06 01:38:37 -0800438 rc = ecryptfs_add_new_key_tfm(
439 NULL, mount_crypt_stat->global_default_cipher_name,
440 mount_crypt_stat->global_default_cipher_key_size);
Michael Halcrow87c94c42009-01-06 14:42:01 -0800441 if (rc) {
442 printk(KERN_ERR "Error attempting to initialize "
443 "cipher with name = [%s] and key size = [%td]; "
444 "rc = [%d]\n",
445 mount_crypt_stat->global_default_cipher_name,
446 mount_crypt_stat->global_default_cipher_key_size,
447 rc);
448 rc = -EINVAL;
449 mutex_unlock(&key_tfm_list_mutex);
450 goto out;
451 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700452 }
Michael Halcrow87c94c42009-01-06 14:42:01 -0800453 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
454 && !ecryptfs_tfm_exists(
455 mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
456 rc = ecryptfs_add_new_key_tfm(
457 NULL, mount_crypt_stat->global_default_fn_cipher_name,
458 mount_crypt_stat->global_default_fn_cipher_key_bytes);
459 if (rc) {
460 printk(KERN_ERR "Error attempting to initialize "
461 "cipher with name = [%s] and key size = [%td]; "
462 "rc = [%d]\n",
463 mount_crypt_stat->global_default_fn_cipher_name,
464 mount_crypt_stat->global_default_fn_cipher_key_bytes,
465 rc);
466 rc = -EINVAL;
467 mutex_unlock(&key_tfm_list_mutex);
468 goto out;
469 }
470 }
471 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700472 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
Michael Halcrow87c94c42009-01-06 14:42:01 -0800473 if (rc)
Michael Halcrowf4aad162007-10-16 01:27:53 -0700474 printk(KERN_WARNING "One or more global auth toks could not "
475 "properly register; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700476out:
477 return rc;
478}
479
480struct kmem_cache *ecryptfs_sb_info_cache;
481
482/**
483 * ecryptfs_fill_super
484 * @sb: The ecryptfs super block
485 * @raw_data: The options passed to mount
486 * @silent: Not used but required by function prototype
487 *
488 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
489 *
490 * Returns zero on success; non-zero otherwise
491 */
492static int
493ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
494{
495 int rc = 0;
496
497 /* Released in ecryptfs_put_super() */
498 ecryptfs_set_superblock_private(sb,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800499 kmem_cache_zalloc(ecryptfs_sb_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800500 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700501 if (!ecryptfs_superblock_to_private(sb)) {
502 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
503 rc = -ENOMEM;
504 goto out;
505 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700506 sb->s_op = &ecryptfs_sops;
507 /* Released through deactivate_super(sb) from get_sb_nodev */
508 sb->s_root = d_alloc(NULL, &(const struct qstr) {
509 .hash = 0,.name = "/",.len = 1});
510 if (!sb->s_root) {
511 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
512 rc = -ENOMEM;
513 goto out;
514 }
515 sb->s_root->d_op = &ecryptfs_dops;
516 sb->s_root->d_sb = sb;
517 sb->s_root->d_parent = sb->s_root;
518 /* Released in d_release when dput(sb->s_root) is called */
519 /* through deactivate_super(sb) from get_sb_nodev() */
520 ecryptfs_set_dentry_private(sb->s_root,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800521 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800522 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700523 if (!ecryptfs_dentry_to_private(sb->s_root)) {
524 ecryptfs_printk(KERN_ERR,
525 "dentry_info_cache alloc failed\n");
526 rc = -ENOMEM;
527 goto out;
528 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700529 rc = 0;
530out:
531 /* Should be able to rely on deactivate_super called from
532 * get_sb_nodev */
533 return rc;
534}
535
536/**
537 * ecryptfs_read_super
538 * @sb: The ecryptfs super block
539 * @dev_name: The path to mount over
540 *
541 * Read the super block of the lower filesystem, and use
542 * ecryptfs_interpose to create our initial inode and super block
543 * struct.
544 */
545static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
546{
Al Viro421748e2008-08-02 01:04:36 -0400547 struct path path;
Michael Halcrow237fead2006-10-04 02:16:22 -0700548 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700549
Al Viro421748e2008-08-02 01:04:36 -0400550 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700551 if (rc) {
552 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
Michael Halcrow65dc8142007-02-28 20:12:57 -0800553 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700554 }
Al Viro421748e2008-08-02 01:04:36 -0400555 ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
556 sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
557 sb->s_blocksize = path.dentry->d_sb->s_blocksize;
558 ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
559 ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
560 rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700561 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700562 goto out_free;
563 rc = 0;
564 goto out;
565out_free:
Al Viro421748e2008-08-02 01:04:36 -0400566 path_put(&path);
Michael Halcrow237fead2006-10-04 02:16:22 -0700567out:
568 return rc;
569}
570
571/**
572 * ecryptfs_get_sb
573 * @fs_type
574 * @flags
575 * @dev_name: The path to mount over
576 * @raw_data: The options passed into the kernel
577 *
578 * The whole ecryptfs_get_sb process is broken into 4 functions:
579 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
580 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
581 * with as much information as it can before needing
582 * the lower filesystem.
583 * ecryptfs_read_super(): this accesses the lower filesystem and uses
584 * ecryptfs_interpolate to perform most of the linking
585 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
586 */
587static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
588 const char *dev_name, void *raw_data,
589 struct vfsmount *mnt)
590{
591 int rc;
592 struct super_block *sb;
593
594 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
595 if (rc < 0) {
596 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
597 goto out;
598 }
599 sb = mnt->mnt_sb;
600 rc = ecryptfs_parse_options(sb, raw_data);
601 if (rc) {
602 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
603 goto out_abort;
604 }
605 rc = ecryptfs_read_super(sb, dev_name);
606 if (rc) {
607 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
608 goto out_abort;
609 }
610 goto out;
611out_abort:
612 dput(sb->s_root);
613 up_write(&sb->s_umount);
614 deactivate_super(sb);
615out:
616 return rc;
617}
618
619/**
620 * ecryptfs_kill_block_super
621 * @sb: The ecryptfs super block
622 *
623 * Used to bring the superblock down and free the private data.
624 * Private data is free'd in ecryptfs_put_super()
625 */
626static void ecryptfs_kill_block_super(struct super_block *sb)
627{
628 generic_shutdown_super(sb);
629}
630
631static struct file_system_type ecryptfs_fs_type = {
632 .owner = THIS_MODULE,
633 .name = "ecryptfs",
634 .get_sb = ecryptfs_get_sb,
635 .kill_sb = ecryptfs_kill_block_super,
636 .fs_flags = 0
637};
638
639/**
640 * inode_info_init_once
641 *
642 * Initializes the ecryptfs_inode_info_cache when it is created
643 */
644static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700645inode_info_init_once(void *vptr)
Michael Halcrow237fead2006-10-04 02:16:22 -0700646{
647 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
648
Christoph Lametera35afb82007-05-16 22:10:57 -0700649 inode_init_once(&ei->vfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700650}
651
652static struct ecryptfs_cache_info {
Christoph Lametere18b8902006-12-06 20:33:20 -0800653 struct kmem_cache **cache;
Michael Halcrow237fead2006-10-04 02:16:22 -0700654 const char *name;
655 size_t size;
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700656 void (*ctor)(void *obj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700657} ecryptfs_cache_infos[] = {
658 {
659 .cache = &ecryptfs_auth_tok_list_item_cache,
660 .name = "ecryptfs_auth_tok_list_item",
661 .size = sizeof(struct ecryptfs_auth_tok_list_item),
662 },
663 {
664 .cache = &ecryptfs_file_info_cache,
665 .name = "ecryptfs_file_cache",
666 .size = sizeof(struct ecryptfs_file_info),
667 },
668 {
669 .cache = &ecryptfs_dentry_info_cache,
670 .name = "ecryptfs_dentry_info_cache",
671 .size = sizeof(struct ecryptfs_dentry_info),
672 },
673 {
674 .cache = &ecryptfs_inode_info_cache,
675 .name = "ecryptfs_inode_cache",
676 .size = sizeof(struct ecryptfs_inode_info),
677 .ctor = inode_info_init_once,
678 },
679 {
680 .cache = &ecryptfs_sb_info_cache,
681 .name = "ecryptfs_sb_cache",
682 .size = sizeof(struct ecryptfs_sb_info),
683 },
684 {
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 .cache = &ecryptfs_header_cache_1,
686 .name = "ecryptfs_headers_1",
687 .size = PAGE_CACHE_SIZE,
688 },
689 {
690 .cache = &ecryptfs_header_cache_2,
691 .name = "ecryptfs_headers_2",
692 .size = PAGE_CACHE_SIZE,
693 },
694 {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800695 .cache = &ecryptfs_xattr_cache,
696 .name = "ecryptfs_xattr_cache",
697 .size = PAGE_CACHE_SIZE,
698 },
699 {
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800700 .cache = &ecryptfs_key_record_cache,
701 .name = "ecryptfs_key_record_cache",
702 .size = sizeof(struct ecryptfs_key_record),
703 },
Michael Halcrow956159c2007-10-16 01:27:55 -0700704 {
705 .cache = &ecryptfs_key_sig_cache,
706 .name = "ecryptfs_key_sig_cache",
707 .size = sizeof(struct ecryptfs_key_sig),
708 },
709 {
710 .cache = &ecryptfs_global_auth_tok_cache,
711 .name = "ecryptfs_global_auth_tok_cache",
712 .size = sizeof(struct ecryptfs_global_auth_tok),
713 },
714 {
715 .cache = &ecryptfs_key_tfm_cache,
716 .name = "ecryptfs_key_tfm_cache",
717 .size = sizeof(struct ecryptfs_key_tfm),
718 },
Michael Halcrow746f1e52008-07-23 21:30:02 -0700719 {
720 .cache = &ecryptfs_open_req_cache,
721 .name = "ecryptfs_open_req_cache",
722 .size = sizeof(struct ecryptfs_open_req),
723 },
Michael Halcrow237fead2006-10-04 02:16:22 -0700724};
725
726static void ecryptfs_free_kmem_caches(void)
727{
728 int i;
729
730 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
731 struct ecryptfs_cache_info *info;
732
733 info = &ecryptfs_cache_infos[i];
734 if (*(info->cache))
735 kmem_cache_destroy(*(info->cache));
736 }
737}
738
739/**
740 * ecryptfs_init_kmem_caches
741 *
742 * Returns zero on success; non-zero otherwise
743 */
744static int ecryptfs_init_kmem_caches(void)
745{
746 int i;
747
748 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
749 struct ecryptfs_cache_info *info;
750
751 info = &ecryptfs_cache_infos[i];
752 *(info->cache) = kmem_cache_create(info->name, info->size,
Paul Mundt20c2df82007-07-20 10:11:58 +0900753 0, SLAB_HWCACHE_ALIGN, info->ctor);
Michael Halcrow237fead2006-10-04 02:16:22 -0700754 if (!*(info->cache)) {
755 ecryptfs_free_kmem_caches();
756 ecryptfs_printk(KERN_WARNING, "%s: "
757 "kmem_cache_create failed\n",
758 info->name);
759 return -ENOMEM;
760 }
761 }
762 return 0;
763}
764
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800765static struct kobject *ecryptfs_kobj;
Michael Halcrow237fead2006-10-04 02:16:22 -0700766
Kay Sievers386f2752007-11-02 13:47:53 +0100767static ssize_t version_show(struct kobject *kobj,
768 struct kobj_attribute *attr, char *buff)
Michael Halcrow237fead2006-10-04 02:16:22 -0700769{
770 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
771}
772
Kay Sievers386f2752007-11-02 13:47:53 +0100773static struct kobj_attribute version_attr = __ATTR_RO(version);
Michael Halcrow237fead2006-10-04 02:16:22 -0700774
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700775static struct attribute *attributes[] = {
776 &version_attr.attr,
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700777 NULL,
778};
779
780static struct attribute_group attr_group = {
781 .attrs = attributes,
782};
Michael Halcrow237fead2006-10-04 02:16:22 -0700783
784static int do_sysfs_registration(void)
785{
786 int rc;
787
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800788 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
789 if (!ecryptfs_kobj) {
Greg Kroah-Hartman917e8652007-10-29 20:13:17 +0100790 printk(KERN_ERR "Unable to create ecryptfs kset\n");
791 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700792 goto out;
793 }
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800794 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
Michael Halcrow237fead2006-10-04 02:16:22 -0700795 if (rc) {
796 printk(KERN_ERR
Greg Kroah-Hartman30a468b2007-10-15 15:01:24 -0700797 "Unable to create ecryptfs version attributes\n");
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800798 kobject_put(ecryptfs_kobj);
Michael Halcrow237fead2006-10-04 02:16:22 -0700799 }
800out:
801 return rc;
802}
803
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700804static void do_sysfs_unregistration(void)
805{
Greg Kroah-Hartman6e90aa92007-11-06 15:08:08 -0800806 sysfs_remove_group(ecryptfs_kobj, &attr_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800807 kobject_put(ecryptfs_kobj);
Ryusuke Konishia75de1b2007-08-10 13:00:56 -0700808}
809
Michael Halcrow237fead2006-10-04 02:16:22 -0700810static int __init ecryptfs_init(void)
811{
812 int rc;
813
814 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
815 rc = -EINVAL;
816 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
817 "larger than the host's page size, and so "
818 "eCryptfs cannot run on this system. The "
819 "default eCryptfs extent size is [%d] bytes; "
820 "the page size is [%d] bytes.\n",
821 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
822 goto out;
823 }
824 rc = ecryptfs_init_kmem_caches();
825 if (rc) {
826 printk(KERN_ERR
827 "Failed to allocate one or more kmem_cache objects\n");
828 goto out;
829 }
830 rc = register_filesystem(&ecryptfs_fs_type);
831 if (rc) {
832 printk(KERN_ERR "Failed to register filesystem\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700833 goto out_free_kmem_caches;
Michael Halcrow237fead2006-10-04 02:16:22 -0700834 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700835 rc = do_sysfs_registration();
836 if (rc) {
837 printk(KERN_ERR "sysfs registration failed\n");
Michael Halcrowcf81f892007-10-16 01:28:07 -0700838 goto out_unregister_filesystem;
Michael Halcrow237fead2006-10-04 02:16:22 -0700839 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700840 rc = ecryptfs_init_kthread();
841 if (rc) {
842 printk(KERN_ERR "%s: kthread initialization failed; "
843 "rc = [%d]\n", __func__, rc);
844 goto out_do_sysfs_unregistration;
845 }
Tyler Hicks624ae522008-10-15 22:02:51 -0700846 rc = ecryptfs_init_messaging();
Michael Halcrowdddfa462007-02-12 00:53:44 -0800847 if (rc) {
Michael Halcrow746f1e52008-07-23 21:30:02 -0700848 printk(KERN_ERR "Failure occured while attempting to "
Tyler Hicks624ae522008-10-15 22:02:51 -0700849 "initialize the communications channel to "
850 "ecryptfsd\n");
Michael Halcrow746f1e52008-07-23 21:30:02 -0700851 goto out_destroy_kthread;
Michael Halcrow956159c2007-10-16 01:27:55 -0700852 }
853 rc = ecryptfs_init_crypto();
854 if (rc) {
855 printk(KERN_ERR "Failure whilst attempting to init crypto; "
856 "rc = [%d]\n", rc);
Michael Halcrowcf81f892007-10-16 01:28:07 -0700857 goto out_release_messaging;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800858 }
Eric Sandeen2830bfd2008-02-06 01:38:34 -0800859 if (ecryptfs_verbosity > 0)
860 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
861 "will be written to the syslog!\n", ecryptfs_verbosity);
862
Michael Halcrowcf81f892007-10-16 01:28:07 -0700863 goto out;
864out_release_messaging:
Tyler Hicks624ae522008-10-15 22:02:51 -0700865 ecryptfs_release_messaging();
Michael Halcrow746f1e52008-07-23 21:30:02 -0700866out_destroy_kthread:
867 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700868out_do_sysfs_unregistration:
869 do_sysfs_unregistration();
870out_unregister_filesystem:
871 unregister_filesystem(&ecryptfs_fs_type);
872out_free_kmem_caches:
873 ecryptfs_free_kmem_caches();
Michael Halcrow237fead2006-10-04 02:16:22 -0700874out:
875 return rc;
876}
877
878static void __exit ecryptfs_exit(void)
879{
Michael Halcrowcf81f892007-10-16 01:28:07 -0700880 int rc;
881
882 rc = ecryptfs_destroy_crypto();
883 if (rc)
884 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
885 "rc = [%d]\n", rc);
Tyler Hicks624ae522008-10-15 22:02:51 -0700886 ecryptfs_release_messaging();
Michael Halcrow746f1e52008-07-23 21:30:02 -0700887 ecryptfs_destroy_kthread();
Michael Halcrowcf81f892007-10-16 01:28:07 -0700888 do_sysfs_unregistration();
Michael Halcrow237fead2006-10-04 02:16:22 -0700889 unregister_filesystem(&ecryptfs_fs_type);
890 ecryptfs_free_kmem_caches();
891}
892
893MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
894MODULE_DESCRIPTION("eCryptfs");
895
896MODULE_LICENSE("GPL");
897
898module_init(ecryptfs_init)
899module_exit(ecryptfs_exit)