]>
Commit | Line | Data |
---|---|---|
237fead6 MH |
1 | /** |
2 | * eCryptfs: Linux filesystem encryption layer | |
3 | * | |
4 | * Copyright (C) 1997-2003 Erez Zadok | |
5 | * Copyright (C) 2001-2003 Stony Brook University | |
dd2a3b7a | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
237fead6 MH |
7 | * Author(s): Michael A. Halcrow <[email protected]> |
8 | * Michael C. Thompson <[email protected]> | |
dddfa461 | 9 | * Tyler Hicks <[email protected]> |
237fead6 MH |
10 | * |
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> | |
237fead6 MH |
35 | #include <linux/pagemap.h> |
36 | #include <linux/key.h> | |
37 | #include <linux/parser.h> | |
0cc72dc7 | 38 | #include <linux/fs_stack.h> |
237fead6 MH |
39 | #include "ecryptfs_kernel.h" |
40 | ||
41 | /** | |
42 | * Module parameter that defines the ecryptfs_verbosity level. | |
43 | */ | |
44 | int ecryptfs_verbosity = 0; | |
45 | ||
46 | module_param(ecryptfs_verbosity, int, 0); | |
47 | MODULE_PARM_DESC(ecryptfs_verbosity, | |
48 | "Initial verbosity level (0 or 1; defaults to " | |
49 | "0, which is Quiet)"); | |
50 | ||
dddfa461 MH |
51 | /** |
52 | * Module parameter that defines the number of netlink message buffer | |
53 | * elements | |
54 | */ | |
55 | unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS; | |
56 | ||
57 | module_param(ecryptfs_message_buf_len, uint, 0); | |
58 | MODULE_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 | */ | |
67 | signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ; | |
68 | ||
69 | module_param(ecryptfs_message_wait_timeout, long, 0); | |
70 | MODULE_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 | */ | |
80 | unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS; | |
81 | ||
82 | module_param(ecryptfs_number_of_users, uint, 0); | |
83 | MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of " | |
84 | "concurrent users of eCryptfs"); | |
85 | ||
86 | unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT; | |
87 | ||
237fead6 MH |
88 | void __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 | ||
4981e081 MH |
100 | /** |
101 | * 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 | */ | |
120 | int 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)); | |
16317ec2 ES |
141 | if (IS_ERR(inode_info->lower_file)) { |
142 | dget(lower_dentry); | |
143 | mntget(lower_mnt); | |
4981e081 MH |
144 | inode_info->lower_file = dentry_open(lower_dentry, |
145 | lower_mnt, | |
146 | (O_RDONLY | |
147 | | O_LARGEFILE)); | |
16317ec2 | 148 | } |
4981e081 MH |
149 | if (IS_ERR(inode_info->lower_file)) { |
150 | printk(KERN_ERR "Error opening lower persistent file " | |
151 | "for lower_dentry [0x%p] and lower_mnt [0x%p]\n", | |
152 | lower_dentry, lower_mnt); | |
153 | rc = PTR_ERR(inode_info->lower_file); | |
154 | inode_info->lower_file = NULL; | |
155 | } | |
156 | } | |
157 | mutex_unlock(&inode_info->lower_file_mutex); | |
158 | return rc; | |
159 | } | |
160 | ||
237fead6 MH |
161 | /** |
162 | * ecryptfs_interpose | |
163 | * @lower_dentry: Existing dentry in the lower filesystem | |
164 | * @dentry: ecryptfs' dentry | |
165 | * @sb: ecryptfs's super_block | |
166 | * @flag: If set to true, then d_add is called, else d_instantiate is called | |
167 | * | |
168 | * Interposes upper and lower dentries. | |
169 | * | |
170 | * Returns zero on success; non-zero otherwise | |
171 | */ | |
172 | int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry, | |
173 | struct super_block *sb, int flag) | |
174 | { | |
175 | struct inode *lower_inode; | |
176 | struct inode *inode; | |
177 | int rc = 0; | |
178 | ||
179 | lower_inode = lower_dentry->d_inode; | |
180 | if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) { | |
181 | rc = -EXDEV; | |
182 | goto out; | |
183 | } | |
184 | if (!igrab(lower_inode)) { | |
185 | rc = -ESTALE; | |
186 | goto out; | |
187 | } | |
188 | inode = iget5_locked(sb, (unsigned long)lower_inode, | |
189 | ecryptfs_inode_test, ecryptfs_inode_set, | |
190 | lower_inode); | |
191 | if (!inode) { | |
192 | rc = -EACCES; | |
193 | iput(lower_inode); | |
194 | goto out; | |
195 | } | |
196 | if (inode->i_state & I_NEW) | |
197 | unlock_new_inode(inode); | |
198 | else | |
199 | iput(lower_inode); | |
200 | if (S_ISLNK(lower_inode->i_mode)) | |
201 | inode->i_op = &ecryptfs_symlink_iops; | |
202 | else if (S_ISDIR(lower_inode->i_mode)) | |
203 | inode->i_op = &ecryptfs_dir_iops; | |
204 | if (S_ISDIR(lower_inode->i_mode)) | |
205 | inode->i_fop = &ecryptfs_dir_fops; | |
26da8205 | 206 | if (special_file(lower_inode->i_mode)) |
237fead6 MH |
207 | init_special_inode(inode, lower_inode->i_mode, |
208 | lower_inode->i_rdev); | |
209 | dentry->d_op = &ecryptfs_dops; | |
210 | if (flag) | |
211 | d_add(dentry, inode); | |
212 | else | |
213 | d_instantiate(dentry, inode); | |
0cc72dc7 | 214 | fsstack_copy_attr_all(inode, lower_inode, NULL); |
237fead6 MH |
215 | /* This size will be overwritten for real files w/ headers and |
216 | * other metadata */ | |
0cc72dc7 | 217 | fsstack_copy_inode_size(inode, lower_inode); |
4981e081 MH |
218 | rc = ecryptfs_init_persistent_file(dentry); |
219 | if (rc) { | |
220 | printk(KERN_ERR "%s: Error attempting to initialize the " | |
221 | "persistent file for the dentry with name [%s]; " | |
222 | "rc = [%d]\n", __FUNCTION__, dentry->d_name.name, rc); | |
223 | goto out; | |
224 | } | |
237fead6 MH |
225 | out: |
226 | return rc; | |
227 | } | |
228 | ||
229 | enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug, | |
230 | ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher, | |
231 | ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes, | |
17398957 MH |
232 | ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata, |
233 | ecryptfs_opt_encrypted_view, ecryptfs_opt_err }; | |
237fead6 MH |
234 | |
235 | static match_table_t tokens = { | |
236 | {ecryptfs_opt_sig, "sig=%s"}, | |
237 | {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"}, | |
238 | {ecryptfs_opt_debug, "debug=%u"}, | |
239 | {ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"}, | |
240 | {ecryptfs_opt_cipher, "cipher=%s"}, | |
241 | {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"}, | |
242 | {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"}, | |
243 | {ecryptfs_opt_passthrough, "ecryptfs_passthrough"}, | |
17398957 MH |
244 | {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"}, |
245 | {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"}, | |
237fead6 MH |
246 | {ecryptfs_opt_err, NULL} |
247 | }; | |
248 | ||
f4aad16a MH |
249 | static int ecryptfs_init_global_auth_toks( |
250 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
237fead6 | 251 | { |
f4aad16a | 252 | struct ecryptfs_global_auth_tok *global_auth_tok; |
237fead6 | 253 | int rc = 0; |
237fead6 | 254 | |
f4aad16a MH |
255 | list_for_each_entry(global_auth_tok, |
256 | &mount_crypt_stat->global_auth_tok_list, | |
257 | mount_crypt_stat_list) { | |
5dda6992 MH |
258 | rc = ecryptfs_keyring_auth_tok_for_sig( |
259 | &global_auth_tok->global_auth_tok_key, | |
260 | &global_auth_tok->global_auth_tok, | |
261 | global_auth_tok->sig); | |
262 | if (rc) { | |
f4aad16a MH |
263 | printk(KERN_ERR "Could not find valid key in user " |
264 | "session keyring for sig specified in mount " | |
265 | "option: [%s]\n", global_auth_tok->sig); | |
266 | global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; | |
267 | rc = 0; | |
268 | } else | |
269 | global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; | |
237fead6 | 270 | } |
237fead6 MH |
271 | return rc; |
272 | } | |
273 | ||
f4aad16a MH |
274 | static void ecryptfs_init_mount_crypt_stat( |
275 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) | |
276 | { | |
277 | memset((void *)mount_crypt_stat, 0, | |
278 | sizeof(struct ecryptfs_mount_crypt_stat)); | |
279 | INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list); | |
280 | mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex); | |
281 | mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED; | |
282 | } | |
283 | ||
237fead6 MH |
284 | /** |
285 | * ecryptfs_parse_options | |
286 | * @sb: The ecryptfs super block | |
287 | * @options: The options pased to the kernel | |
288 | * | |
289 | * Parse mount options: | |
290 | * debug=N - ecryptfs_verbosity level for debug output | |
291 | * sig=XXX - description(signature) of the key to use | |
292 | * | |
293 | * Returns the dentry object of the lower-level (lower/interposed) | |
294 | * directory; We want to mount our stackable file system on top of | |
295 | * that lower directory. | |
296 | * | |
297 | * The signature of the key to use must be the description of a key | |
298 | * already in the keyring. Mounting will fail if the key can not be | |
299 | * found. | |
300 | * | |
301 | * Returns zero on success; non-zero on error | |
302 | */ | |
303 | static int ecryptfs_parse_options(struct super_block *sb, char *options) | |
304 | { | |
305 | char *p; | |
306 | int rc = 0; | |
307 | int sig_set = 0; | |
308 | int cipher_name_set = 0; | |
309 | int cipher_key_bytes; | |
310 | int cipher_key_bytes_set = 0; | |
237fead6 MH |
311 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
312 | &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; | |
313 | substring_t args[MAX_OPT_ARGS]; | |
314 | int token; | |
315 | char *sig_src; | |
237fead6 MH |
316 | char *debug_src; |
317 | char *cipher_name_dst; | |
318 | char *cipher_name_src; | |
319 | char *cipher_key_bytes_src; | |
237fead6 MH |
320 | int cipher_name_len; |
321 | ||
322 | if (!options) { | |
323 | rc = -EINVAL; | |
324 | goto out; | |
325 | } | |
956159c3 | 326 | ecryptfs_init_mount_crypt_stat(mount_crypt_stat); |
237fead6 MH |
327 | while ((p = strsep(&options, ",")) != NULL) { |
328 | if (!*p) | |
329 | continue; | |
330 | token = match_token(p, tokens, args); | |
331 | switch (token) { | |
332 | case ecryptfs_opt_sig: | |
333 | case ecryptfs_opt_ecryptfs_sig: | |
334 | sig_src = args[0].from; | |
f4aad16a MH |
335 | rc = ecryptfs_add_global_auth_tok(mount_crypt_stat, |
336 | sig_src); | |
337 | if (rc) { | |
338 | printk(KERN_ERR "Error attempting to register " | |
339 | "global sig; rc = [%d]\n", rc); | |
340 | goto out; | |
341 | } | |
237fead6 MH |
342 | sig_set = 1; |
343 | break; | |
344 | case ecryptfs_opt_debug: | |
345 | case ecryptfs_opt_ecryptfs_debug: | |
346 | debug_src = args[0].from; | |
347 | ecryptfs_verbosity = | |
348 | (int)simple_strtol(debug_src, &debug_src, | |
349 | 0); | |
350 | ecryptfs_printk(KERN_DEBUG, | |
351 | "Verbosity set to [%d]" "\n", | |
352 | ecryptfs_verbosity); | |
353 | break; | |
354 | case ecryptfs_opt_cipher: | |
355 | case ecryptfs_opt_ecryptfs_cipher: | |
356 | cipher_name_src = args[0].from; | |
357 | cipher_name_dst = | |
358 | mount_crypt_stat-> | |
359 | global_default_cipher_name; | |
360 | strncpy(cipher_name_dst, cipher_name_src, | |
361 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); | |
362 | ecryptfs_printk(KERN_DEBUG, | |
363 | "The mount_crypt_stat " | |
364 | "global_default_cipher_name set to: " | |
365 | "[%s]\n", cipher_name_dst); | |
366 | cipher_name_set = 1; | |
367 | break; | |
368 | case ecryptfs_opt_ecryptfs_key_bytes: | |
369 | cipher_key_bytes_src = args[0].from; | |
370 | cipher_key_bytes = | |
371 | (int)simple_strtol(cipher_key_bytes_src, | |
372 | &cipher_key_bytes_src, 0); | |
373 | mount_crypt_stat->global_default_cipher_key_size = | |
374 | cipher_key_bytes; | |
375 | ecryptfs_printk(KERN_DEBUG, | |
376 | "The mount_crypt_stat " | |
377 | "global_default_cipher_key_size " | |
378 | "set to: [%d]\n", mount_crypt_stat-> | |
379 | global_default_cipher_key_size); | |
380 | cipher_key_bytes_set = 1; | |
381 | break; | |
382 | case ecryptfs_opt_passthrough: | |
383 | mount_crypt_stat->flags |= | |
384 | ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED; | |
385 | break; | |
17398957 MH |
386 | case ecryptfs_opt_xattr_metadata: |
387 | mount_crypt_stat->flags |= | |
388 | ECRYPTFS_XATTR_METADATA_ENABLED; | |
389 | break; | |
390 | case ecryptfs_opt_encrypted_view: | |
391 | mount_crypt_stat->flags |= | |
392 | ECRYPTFS_XATTR_METADATA_ENABLED; | |
393 | mount_crypt_stat->flags |= | |
394 | ECRYPTFS_ENCRYPTED_VIEW_ENABLED; | |
395 | break; | |
237fead6 MH |
396 | case ecryptfs_opt_err: |
397 | default: | |
398 | ecryptfs_printk(KERN_WARNING, | |
399 | "eCryptfs: unrecognized option '%s'\n", | |
400 | p); | |
401 | } | |
402 | } | |
237fead6 MH |
403 | if (!sig_set) { |
404 | rc = -EINVAL; | |
956159c3 MH |
405 | ecryptfs_printk(KERN_ERR, "You must supply at least one valid " |
406 | "auth tok signature as a mount " | |
237fead6 MH |
407 | "parameter; see the eCryptfs README\n"); |
408 | goto out; | |
409 | } | |
410 | if (!cipher_name_set) { | |
411 | cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER); | |
412 | if (unlikely(cipher_name_len | |
413 | >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) { | |
414 | rc = -EINVAL; | |
415 | BUG(); | |
416 | goto out; | |
417 | } | |
418 | memcpy(mount_crypt_stat->global_default_cipher_name, | |
419 | ECRYPTFS_DEFAULT_CIPHER, cipher_name_len); | |
420 | mount_crypt_stat->global_default_cipher_name[cipher_name_len] | |
421 | = '\0'; | |
422 | } | |
423 | if (!cipher_key_bytes_set) { | |
e5d9cbde | 424 | mount_crypt_stat->global_default_cipher_key_size = 0; |
237fead6 | 425 | } |
5dda6992 MH |
426 | rc = ecryptfs_add_new_key_tfm( |
427 | NULL, mount_crypt_stat->global_default_cipher_name, | |
428 | mount_crypt_stat->global_default_cipher_key_size); | |
429 | if (rc) { | |
f4aad16a | 430 | printk(KERN_ERR "Error attempting to initialize cipher with " |
81acbcd6 | 431 | "name = [%s] and key size = [%td]; rc = [%d]\n", |
237fead6 MH |
432 | mount_crypt_stat->global_default_cipher_name, |
433 | mount_crypt_stat->global_default_cipher_key_size, rc); | |
237fead6 MH |
434 | rc = -EINVAL; |
435 | goto out; | |
436 | } | |
5dda6992 MH |
437 | rc = ecryptfs_init_global_auth_toks(mount_crypt_stat); |
438 | if (rc) { | |
f4aad16a MH |
439 | printk(KERN_WARNING "One or more global auth toks could not " |
440 | "properly register; rc = [%d]\n", rc); | |
237fead6 | 441 | } |
f4aad16a | 442 | rc = 0; |
237fead6 MH |
443 | out: |
444 | return rc; | |
445 | } | |
446 | ||
447 | struct kmem_cache *ecryptfs_sb_info_cache; | |
448 | ||
449 | /** | |
450 | * ecryptfs_fill_super | |
451 | * @sb: The ecryptfs super block | |
452 | * @raw_data: The options passed to mount | |
453 | * @silent: Not used but required by function prototype | |
454 | * | |
455 | * Sets up what we can of the sb, rest is done in ecryptfs_read_super | |
456 | * | |
457 | * Returns zero on success; non-zero otherwise | |
458 | */ | |
459 | static int | |
460 | ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent) | |
461 | { | |
462 | int rc = 0; | |
463 | ||
464 | /* Released in ecryptfs_put_super() */ | |
465 | ecryptfs_set_superblock_private(sb, | |
c3762229 | 466 | kmem_cache_zalloc(ecryptfs_sb_info_cache, |
e94b1766 | 467 | GFP_KERNEL)); |
237fead6 MH |
468 | if (!ecryptfs_superblock_to_private(sb)) { |
469 | ecryptfs_printk(KERN_WARNING, "Out of memory\n"); | |
470 | rc = -ENOMEM; | |
471 | goto out; | |
472 | } | |
237fead6 MH |
473 | sb->s_op = &ecryptfs_sops; |
474 | /* Released through deactivate_super(sb) from get_sb_nodev */ | |
475 | sb->s_root = d_alloc(NULL, &(const struct qstr) { | |
476 | .hash = 0,.name = "/",.len = 1}); | |
477 | if (!sb->s_root) { | |
478 | ecryptfs_printk(KERN_ERR, "d_alloc failed\n"); | |
479 | rc = -ENOMEM; | |
480 | goto out; | |
481 | } | |
482 | sb->s_root->d_op = &ecryptfs_dops; | |
483 | sb->s_root->d_sb = sb; | |
484 | sb->s_root->d_parent = sb->s_root; | |
485 | /* Released in d_release when dput(sb->s_root) is called */ | |
486 | /* through deactivate_super(sb) from get_sb_nodev() */ | |
487 | ecryptfs_set_dentry_private(sb->s_root, | |
c3762229 | 488 | kmem_cache_zalloc(ecryptfs_dentry_info_cache, |
e94b1766 | 489 | GFP_KERNEL)); |
237fead6 MH |
490 | if (!ecryptfs_dentry_to_private(sb->s_root)) { |
491 | ecryptfs_printk(KERN_ERR, | |
492 | "dentry_info_cache alloc failed\n"); | |
493 | rc = -ENOMEM; | |
494 | goto out; | |
495 | } | |
237fead6 MH |
496 | rc = 0; |
497 | out: | |
498 | /* Should be able to rely on deactivate_super called from | |
499 | * get_sb_nodev */ | |
500 | return rc; | |
501 | } | |
502 | ||
503 | /** | |
504 | * ecryptfs_read_super | |
505 | * @sb: The ecryptfs super block | |
506 | * @dev_name: The path to mount over | |
507 | * | |
508 | * Read the super block of the lower filesystem, and use | |
509 | * ecryptfs_interpose to create our initial inode and super block | |
510 | * struct. | |
511 | */ | |
512 | static int ecryptfs_read_super(struct super_block *sb, const char *dev_name) | |
513 | { | |
514 | int rc; | |
515 | struct nameidata nd; | |
516 | struct dentry *lower_root; | |
517 | struct vfsmount *lower_mnt; | |
518 | ||
519 | memset(&nd, 0, sizeof(struct nameidata)); | |
82b16528 | 520 | rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd); |
237fead6 MH |
521 | if (rc) { |
522 | ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n"); | |
65dc8145 | 523 | goto out; |
237fead6 MH |
524 | } |
525 | lower_root = nd.dentry; | |
237fead6 MH |
526 | lower_mnt = nd.mnt; |
527 | ecryptfs_set_superblock_lower(sb, lower_root->d_sb); | |
528 | sb->s_maxbytes = lower_root->d_sb->s_maxbytes; | |
7c9e70ef | 529 | sb->s_blocksize = lower_root->d_sb->s_blocksize; |
237fead6 MH |
530 | ecryptfs_set_dentry_lower(sb->s_root, lower_root); |
531 | ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt); | |
5dda6992 MH |
532 | rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0); |
533 | if (rc) | |
237fead6 MH |
534 | goto out_free; |
535 | rc = 0; | |
536 | goto out; | |
537 | out_free: | |
538 | path_release(&nd); | |
539 | out: | |
540 | return rc; | |
541 | } | |
542 | ||
543 | /** | |
544 | * ecryptfs_get_sb | |
545 | * @fs_type | |
546 | * @flags | |
547 | * @dev_name: The path to mount over | |
548 | * @raw_data: The options passed into the kernel | |
549 | * | |
550 | * The whole ecryptfs_get_sb process is broken into 4 functions: | |
551 | * ecryptfs_parse_options(): handle options passed to ecryptfs, if any | |
552 | * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block | |
553 | * with as much information as it can before needing | |
554 | * the lower filesystem. | |
555 | * ecryptfs_read_super(): this accesses the lower filesystem and uses | |
556 | * ecryptfs_interpolate to perform most of the linking | |
557 | * ecryptfs_interpolate(): links the lower filesystem into ecryptfs | |
558 | */ | |
559 | static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags, | |
560 | const char *dev_name, void *raw_data, | |
561 | struct vfsmount *mnt) | |
562 | { | |
563 | int rc; | |
564 | struct super_block *sb; | |
565 | ||
566 | rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt); | |
567 | if (rc < 0) { | |
568 | printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc); | |
569 | goto out; | |
570 | } | |
571 | sb = mnt->mnt_sb; | |
572 | rc = ecryptfs_parse_options(sb, raw_data); | |
573 | if (rc) { | |
574 | printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc); | |
575 | goto out_abort; | |
576 | } | |
577 | rc = ecryptfs_read_super(sb, dev_name); | |
578 | if (rc) { | |
579 | printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc); | |
580 | goto out_abort; | |
581 | } | |
582 | goto out; | |
583 | out_abort: | |
584 | dput(sb->s_root); | |
585 | up_write(&sb->s_umount); | |
586 | deactivate_super(sb); | |
587 | out: | |
588 | return rc; | |
589 | } | |
590 | ||
591 | /** | |
592 | * ecryptfs_kill_block_super | |
593 | * @sb: The ecryptfs super block | |
594 | * | |
595 | * Used to bring the superblock down and free the private data. | |
596 | * Private data is free'd in ecryptfs_put_super() | |
597 | */ | |
598 | static void ecryptfs_kill_block_super(struct super_block *sb) | |
599 | { | |
600 | generic_shutdown_super(sb); | |
601 | } | |
602 | ||
603 | static struct file_system_type ecryptfs_fs_type = { | |
604 | .owner = THIS_MODULE, | |
605 | .name = "ecryptfs", | |
606 | .get_sb = ecryptfs_get_sb, | |
607 | .kill_sb = ecryptfs_kill_block_super, | |
608 | .fs_flags = 0 | |
609 | }; | |
610 | ||
611 | /** | |
612 | * inode_info_init_once | |
613 | * | |
614 | * Initializes the ecryptfs_inode_info_cache when it is created | |
615 | */ | |
616 | static void | |
4ba9b9d0 | 617 | inode_info_init_once(struct kmem_cache *cachep, void *vptr) |
237fead6 MH |
618 | { |
619 | struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; | |
620 | ||
a35afb83 | 621 | inode_init_once(&ei->vfs_inode); |
237fead6 MH |
622 | } |
623 | ||
624 | static struct ecryptfs_cache_info { | |
e18b890b | 625 | struct kmem_cache **cache; |
237fead6 MH |
626 | const char *name; |
627 | size_t size; | |
4ba9b9d0 | 628 | void (*ctor)(struct kmem_cache *cache, void *obj); |
237fead6 MH |
629 | } ecryptfs_cache_infos[] = { |
630 | { | |
631 | .cache = &ecryptfs_auth_tok_list_item_cache, | |
632 | .name = "ecryptfs_auth_tok_list_item", | |
633 | .size = sizeof(struct ecryptfs_auth_tok_list_item), | |
634 | }, | |
635 | { | |
636 | .cache = &ecryptfs_file_info_cache, | |
637 | .name = "ecryptfs_file_cache", | |
638 | .size = sizeof(struct ecryptfs_file_info), | |
639 | }, | |
640 | { | |
641 | .cache = &ecryptfs_dentry_info_cache, | |
642 | .name = "ecryptfs_dentry_info_cache", | |
643 | .size = sizeof(struct ecryptfs_dentry_info), | |
644 | }, | |
645 | { | |
646 | .cache = &ecryptfs_inode_info_cache, | |
647 | .name = "ecryptfs_inode_cache", | |
648 | .size = sizeof(struct ecryptfs_inode_info), | |
649 | .ctor = inode_info_init_once, | |
650 | }, | |
651 | { | |
652 | .cache = &ecryptfs_sb_info_cache, | |
653 | .name = "ecryptfs_sb_cache", | |
654 | .size = sizeof(struct ecryptfs_sb_info), | |
655 | }, | |
656 | { | |
657 | .cache = &ecryptfs_header_cache_0, | |
658 | .name = "ecryptfs_headers_0", | |
659 | .size = PAGE_CACHE_SIZE, | |
660 | }, | |
661 | { | |
662 | .cache = &ecryptfs_header_cache_1, | |
663 | .name = "ecryptfs_headers_1", | |
664 | .size = PAGE_CACHE_SIZE, | |
665 | }, | |
666 | { | |
667 | .cache = &ecryptfs_header_cache_2, | |
668 | .name = "ecryptfs_headers_2", | |
669 | .size = PAGE_CACHE_SIZE, | |
670 | }, | |
dd2a3b7a MH |
671 | { |
672 | .cache = &ecryptfs_xattr_cache, | |
673 | .name = "ecryptfs_xattr_cache", | |
674 | .size = PAGE_CACHE_SIZE, | |
675 | }, | |
eb95e7ff MH |
676 | { |
677 | .cache = &ecryptfs_key_record_cache, | |
678 | .name = "ecryptfs_key_record_cache", | |
679 | .size = sizeof(struct ecryptfs_key_record), | |
680 | }, | |
956159c3 MH |
681 | { |
682 | .cache = &ecryptfs_key_sig_cache, | |
683 | .name = "ecryptfs_key_sig_cache", | |
684 | .size = sizeof(struct ecryptfs_key_sig), | |
685 | }, | |
686 | { | |
687 | .cache = &ecryptfs_global_auth_tok_cache, | |
688 | .name = "ecryptfs_global_auth_tok_cache", | |
689 | .size = sizeof(struct ecryptfs_global_auth_tok), | |
690 | }, | |
691 | { | |
692 | .cache = &ecryptfs_key_tfm_cache, | |
693 | .name = "ecryptfs_key_tfm_cache", | |
694 | .size = sizeof(struct ecryptfs_key_tfm), | |
695 | }, | |
237fead6 MH |
696 | }; |
697 | ||
698 | static void ecryptfs_free_kmem_caches(void) | |
699 | { | |
700 | int i; | |
701 | ||
702 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { | |
703 | struct ecryptfs_cache_info *info; | |
704 | ||
705 | info = &ecryptfs_cache_infos[i]; | |
706 | if (*(info->cache)) | |
707 | kmem_cache_destroy(*(info->cache)); | |
708 | } | |
709 | } | |
710 | ||
711 | /** | |
712 | * ecryptfs_init_kmem_caches | |
713 | * | |
714 | * Returns zero on success; non-zero otherwise | |
715 | */ | |
716 | static int ecryptfs_init_kmem_caches(void) | |
717 | { | |
718 | int i; | |
719 | ||
720 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { | |
721 | struct ecryptfs_cache_info *info; | |
722 | ||
723 | info = &ecryptfs_cache_infos[i]; | |
724 | *(info->cache) = kmem_cache_create(info->name, info->size, | |
20c2df83 | 725 | 0, SLAB_HWCACHE_ALIGN, info->ctor); |
237fead6 MH |
726 | if (!*(info->cache)) { |
727 | ecryptfs_free_kmem_caches(); | |
728 | ecryptfs_printk(KERN_WARNING, "%s: " | |
729 | "kmem_cache_create failed\n", | |
730 | info->name); | |
731 | return -ENOMEM; | |
732 | } | |
733 | } | |
734 | return 0; | |
735 | } | |
736 | ||
737 | struct ecryptfs_obj { | |
738 | char *name; | |
739 | struct list_head slot_list; | |
740 | struct kobject kobj; | |
741 | }; | |
742 | ||
743 | struct ecryptfs_attribute { | |
744 | struct attribute attr; | |
745 | ssize_t(*show) (struct ecryptfs_obj *, char *); | |
746 | ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t); | |
747 | }; | |
748 | ||
749 | static ssize_t | |
750 | ecryptfs_attr_store(struct kobject *kobj, | |
751 | struct attribute *attr, const char *buf, size_t len) | |
752 | { | |
753 | struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj, | |
754 | kobj); | |
755 | struct ecryptfs_attribute *attribute = | |
756 | container_of(attr, struct ecryptfs_attribute, attr); | |
757 | ||
758 | return (attribute->store ? attribute->store(obj, buf, len) : 0); | |
759 | } | |
760 | ||
761 | static ssize_t | |
762 | ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) | |
763 | { | |
764 | struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj, | |
765 | kobj); | |
766 | struct ecryptfs_attribute *attribute = | |
767 | container_of(attr, struct ecryptfs_attribute, attr); | |
768 | ||
769 | return (attribute->show ? attribute->show(obj, buf) : 0); | |
770 | } | |
771 | ||
772 | static struct sysfs_ops ecryptfs_sysfs_ops = { | |
773 | .show = ecryptfs_attr_show, | |
774 | .store = ecryptfs_attr_store | |
775 | }; | |
776 | ||
777 | static struct kobj_type ecryptfs_ktype = { | |
778 | .sysfs_ops = &ecryptfs_sysfs_ops | |
779 | }; | |
780 | ||
781 | static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL); | |
782 | ||
783 | static ssize_t version_show(struct ecryptfs_obj *obj, char *buff) | |
784 | { | |
785 | return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK); | |
786 | } | |
787 | ||
788 | static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version); | |
789 | ||
8487f2e4 | 790 | static struct ecryptfs_version_str_map_elem { |
237fead6 MH |
791 | u32 flag; |
792 | char *str; | |
793 | } ecryptfs_version_str_map[] = { | |
794 | {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"}, | |
795 | {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"}, | |
796 | {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"}, | |
17398957 | 797 | {ECRYPTFS_VERSIONING_POLICY, "policy"}, |
956159c3 MH |
798 | {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"}, |
799 | {ECRYPTFS_VERSIONING_MULTKEY, "multiple keys per file"} | |
237fead6 MH |
800 | }; |
801 | ||
802 | static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff) | |
803 | { | |
804 | int i; | |
805 | int remaining = PAGE_SIZE; | |
806 | int total_written = 0; | |
807 | ||
808 | buff[0] = '\0'; | |
809 | for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) { | |
810 | int entry_size; | |
811 | ||
812 | if (!(ECRYPTFS_VERSIONING_MASK | |
813 | & ecryptfs_version_str_map[i].flag)) | |
814 | continue; | |
815 | entry_size = strlen(ecryptfs_version_str_map[i].str); | |
816 | if ((entry_size + 2) > remaining) | |
817 | goto out; | |
818 | memcpy(buff, ecryptfs_version_str_map[i].str, entry_size); | |
819 | buff[entry_size++] = '\n'; | |
820 | buff[entry_size] = '\0'; | |
821 | buff += entry_size; | |
822 | total_written += entry_size; | |
823 | remaining -= entry_size; | |
824 | } | |
825 | out: | |
826 | return total_written; | |
827 | } | |
828 | ||
829 | static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str); | |
830 | ||
831 | static int do_sysfs_registration(void) | |
832 | { | |
833 | int rc; | |
834 | ||
5dda6992 MH |
835 | rc = subsystem_register(&ecryptfs_subsys); |
836 | if (rc) { | |
237fead6 MH |
837 | printk(KERN_ERR |
838 | "Unable to register ecryptfs sysfs subsystem\n"); | |
839 | goto out; | |
840 | } | |
823bccfc | 841 | rc = sysfs_create_file(&ecryptfs_subsys.kobj, |
237fead6 MH |
842 | &sysfs_attr_version.attr); |
843 | if (rc) { | |
844 | printk(KERN_ERR | |
845 | "Unable to create ecryptfs version attribute\n"); | |
846 | subsystem_unregister(&ecryptfs_subsys); | |
847 | goto out; | |
848 | } | |
823bccfc | 849 | rc = sysfs_create_file(&ecryptfs_subsys.kobj, |
237fead6 MH |
850 | &sysfs_attr_version_str.attr); |
851 | if (rc) { | |
852 | printk(KERN_ERR | |
853 | "Unable to create ecryptfs version_str attribute\n"); | |
823bccfc | 854 | sysfs_remove_file(&ecryptfs_subsys.kobj, |
237fead6 MH |
855 | &sysfs_attr_version.attr); |
856 | subsystem_unregister(&ecryptfs_subsys); | |
857 | goto out; | |
858 | } | |
859 | out: | |
860 | return rc; | |
861 | } | |
862 | ||
a75de1b3 RK |
863 | static void do_sysfs_unregistration(void) |
864 | { | |
865 | sysfs_remove_file(&ecryptfs_subsys.kobj, | |
866 | &sysfs_attr_version.attr); | |
867 | sysfs_remove_file(&ecryptfs_subsys.kobj, | |
868 | &sysfs_attr_version_str.attr); | |
869 | subsystem_unregister(&ecryptfs_subsys); | |
870 | } | |
871 | ||
237fead6 MH |
872 | static int __init ecryptfs_init(void) |
873 | { | |
874 | int rc; | |
875 | ||
876 | if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { | |
877 | rc = -EINVAL; | |
878 | ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " | |
879 | "larger than the host's page size, and so " | |
880 | "eCryptfs cannot run on this system. The " | |
881 | "default eCryptfs extent size is [%d] bytes; " | |
882 | "the page size is [%d] bytes.\n", | |
883 | ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE); | |
884 | goto out; | |
885 | } | |
886 | rc = ecryptfs_init_kmem_caches(); | |
887 | if (rc) { | |
888 | printk(KERN_ERR | |
889 | "Failed to allocate one or more kmem_cache objects\n"); | |
890 | goto out; | |
891 | } | |
892 | rc = register_filesystem(&ecryptfs_fs_type); | |
893 | if (rc) { | |
894 | printk(KERN_ERR "Failed to register filesystem\n"); | |
cf81f89d | 895 | goto out_free_kmem_caches; |
237fead6 | 896 | } |
823bccfc | 897 | kobj_set_kset_s(&ecryptfs_subsys, fs_subsys); |
237fead6 MH |
898 | rc = do_sysfs_registration(); |
899 | if (rc) { | |
900 | printk(KERN_ERR "sysfs registration failed\n"); | |
cf81f89d | 901 | goto out_unregister_filesystem; |
237fead6 | 902 | } |
dddfa461 MH |
903 | rc = ecryptfs_init_messaging(ecryptfs_transport); |
904 | if (rc) { | |
905 | ecryptfs_printk(KERN_ERR, "Failure occured while attempting to " | |
906 | "initialize the eCryptfs netlink socket\n"); | |
cf81f89d | 907 | goto out_do_sysfs_unregistration; |
956159c3 MH |
908 | } |
909 | rc = ecryptfs_init_crypto(); | |
910 | if (rc) { | |
911 | printk(KERN_ERR "Failure whilst attempting to init crypto; " | |
912 | "rc = [%d]\n", rc); | |
cf81f89d | 913 | goto out_release_messaging; |
dddfa461 | 914 | } |
cf81f89d MH |
915 | goto out; |
916 | out_release_messaging: | |
917 | ecryptfs_release_messaging(ecryptfs_transport); | |
918 | out_do_sysfs_unregistration: | |
919 | do_sysfs_unregistration(); | |
920 | out_unregister_filesystem: | |
921 | unregister_filesystem(&ecryptfs_fs_type); | |
922 | out_free_kmem_caches: | |
923 | ecryptfs_free_kmem_caches(); | |
237fead6 MH |
924 | out: |
925 | return rc; | |
926 | } | |
927 | ||
928 | static void __exit ecryptfs_exit(void) | |
929 | { | |
cf81f89d MH |
930 | int rc; |
931 | ||
932 | rc = ecryptfs_destroy_crypto(); | |
933 | if (rc) | |
934 | printk(KERN_ERR "Failure whilst attempting to destroy crypto; " | |
935 | "rc = [%d]\n", rc); | |
dddfa461 | 936 | ecryptfs_release_messaging(ecryptfs_transport); |
cf81f89d | 937 | do_sysfs_unregistration(); |
237fead6 MH |
938 | unregister_filesystem(&ecryptfs_fs_type); |
939 | ecryptfs_free_kmem_caches(); | |
940 | } | |
941 | ||
942 | MODULE_AUTHOR("Michael A. Halcrow <[email protected]>"); | |
943 | MODULE_DESCRIPTION("eCryptfs"); | |
944 | ||
945 | MODULE_LICENSE("GPL"); | |
946 | ||
947 | module_init(ecryptfs_init) | |
948 | module_exit(ecryptfs_exit) |