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