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