1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * eCryptfs: Linux filesystem encryption layer
4 *
5 * Copyright (C) 1997-2003 Erez Zadok
6 * Copyright (C) 2001-2003 Stony Brook University
7 * Copyright (C) 2004-2007 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
10 * Tyler Hicks <code@tyhicks.com>
11 */
12
13 #include <linux/dcache.h>
14 #include <linux/file.h>
15 #include <linux/fips.h>
16 #include <linux/module.h>
17 #include <linux/namei.h>
18 #include <linux/skbuff.h>
19 #include <linux/pagemap.h>
20 #include <linux/key.h>
21 #include <linux/fs_context.h>
22 #include <linux/fs_parser.h>
23 #include <linux/fs_stack.h>
24 #include <linux/sysfs.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/magic.h>
28 #include "ecryptfs_kernel.h"
29
30 /*
31 * Module parameter that defines the ecryptfs_verbosity level.
32 */
33 int ecryptfs_verbosity = 0;
34
35 module_param(ecryptfs_verbosity, int, 0);
36 MODULE_PARM_DESC(ecryptfs_verbosity,
37 "Initial verbosity level (0 or 1; defaults to "
38 "0, which is Quiet)");
39
40 /*
41 * Module parameter that defines the number of message buffer elements
42 */
43 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
44
45 module_param(ecryptfs_message_buf_len, uint, 0);
46 MODULE_PARM_DESC(ecryptfs_message_buf_len,
47 "Number of message buffer elements");
48
49 /*
50 * Module parameter that defines the maximum guaranteed amount of time to wait
51 * for a response from ecryptfsd. The actual sleep time will be, more than
52 * likely, a small amount greater than this specified value, but only less if
53 * the message successfully arrives.
54 */
55 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
56
57 module_param(ecryptfs_message_wait_timeout, long, 0);
58 MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
59 "Maximum number of seconds that an operation will "
60 "sleep while waiting for a message response from "
61 "userspace");
62
63 /*
64 * Module parameter that is an estimate of the maximum number of users
65 * that will be concurrently using eCryptfs. Set this to the right
66 * value to balance performance and memory use.
67 */
68 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
69
70 module_param(ecryptfs_number_of_users, uint, 0);
71 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
72 "concurrent users of eCryptfs");
73
__ecryptfs_printk(const char * fmt,...)74 void __ecryptfs_printk(const char *fmt, ...)
75 {
76 va_list args;
77 va_start(args, fmt);
78 if (fmt[1] == '7') { /* KERN_DEBUG */
79 if (ecryptfs_verbosity >= 1)
80 vprintk(fmt, args);
81 } else
82 vprintk(fmt, args);
83 va_end(args);
84 }
85
86 /*
87 * ecryptfs_init_lower_file
88 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
89 * the lower dentry and the lower mount set
90 *
91 * eCryptfs only ever keeps a single open file for every lower
92 * inode. All I/O operations to the lower inode occur through that
93 * file. When the first eCryptfs dentry that interposes with the first
94 * lower dentry for that inode is created, this function creates the
95 * lower file struct and associates it with the eCryptfs
96 * inode. When all eCryptfs files associated with the inode are released, the
97 * file is closed.
98 *
99 * The lower file will be opened with read/write permissions, if
100 * possible. Otherwise, it is opened read-only.
101 *
102 * This function does nothing if a lower file is already
103 * associated with the eCryptfs inode.
104 *
105 * Returns zero on success; non-zero otherwise
106 */
ecryptfs_init_lower_file(struct dentry * dentry,struct file ** lower_file)107 static int ecryptfs_init_lower_file(struct dentry *dentry,
108 struct file **lower_file)
109 {
110 const struct cred *cred = current_cred();
111 struct path path = ecryptfs_lower_path(dentry);
112 int rc;
113
114 rc = ecryptfs_privileged_open(lower_file, path.dentry, path.mnt, cred);
115 if (rc) {
116 printk(KERN_ERR "Error opening lower file "
117 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
118 "rc = [%d]\n", path.dentry, path.mnt, rc);
119 (*lower_file) = NULL;
120 }
121 return rc;
122 }
123
ecryptfs_get_lower_file(struct dentry * dentry,struct inode * inode)124 int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
125 {
126 struct ecryptfs_inode_info *inode_info;
127 int count, rc = 0;
128
129 inode_info = ecryptfs_inode_to_private(inode);
130 mutex_lock(&inode_info->lower_file_mutex);
131 count = atomic_inc_return(&inode_info->lower_file_count);
132 if (WARN_ON_ONCE(count < 1))
133 rc = -EINVAL;
134 else if (count == 1) {
135 rc = ecryptfs_init_lower_file(dentry,
136 &inode_info->lower_file);
137 if (rc)
138 atomic_set(&inode_info->lower_file_count, 0);
139 }
140 mutex_unlock(&inode_info->lower_file_mutex);
141 return rc;
142 }
143
ecryptfs_put_lower_file(struct inode * inode)144 void ecryptfs_put_lower_file(struct inode *inode)
145 {
146 struct ecryptfs_inode_info *inode_info;
147
148 inode_info = ecryptfs_inode_to_private(inode);
149 if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
150 &inode_info->lower_file_mutex)) {
151 filemap_write_and_wait(inode->i_mapping);
152 fput(inode_info->lower_file);
153 inode_info->lower_file = NULL;
154 mutex_unlock(&inode_info->lower_file_mutex);
155 }
156 }
157
158 enum {
159 Opt_sig, Opt_ecryptfs_sig, Opt_cipher, Opt_ecryptfs_cipher,
160 Opt_ecryptfs_key_bytes, Opt_passthrough, Opt_xattr_metadata,
161 Opt_encrypted_view, Opt_fnek_sig, Opt_fn_cipher,
162 Opt_fn_cipher_key_bytes, Opt_unlink_sigs, Opt_mount_auth_tok_only,
163 Opt_check_dev_ruid
164 };
165
166 static const struct fs_parameter_spec ecryptfs_fs_param_spec[] = {
167 fsparam_string ("sig", Opt_sig),
168 fsparam_string ("ecryptfs_sig", Opt_ecryptfs_sig),
169 fsparam_string ("cipher", Opt_cipher),
170 fsparam_string ("ecryptfs_cipher", Opt_ecryptfs_cipher),
171 fsparam_u32 ("ecryptfs_key_bytes", Opt_ecryptfs_key_bytes),
172 fsparam_flag ("ecryptfs_passthrough", Opt_passthrough),
173 fsparam_flag ("ecryptfs_xattr_metadata", Opt_xattr_metadata),
174 fsparam_flag ("ecryptfs_encrypted_view", Opt_encrypted_view),
175 fsparam_string ("ecryptfs_fnek_sig", Opt_fnek_sig),
176 fsparam_string ("ecryptfs_fn_cipher", Opt_fn_cipher),
177 fsparam_u32 ("ecryptfs_fn_key_bytes", Opt_fn_cipher_key_bytes),
178 fsparam_flag ("ecryptfs_unlink_sigs", Opt_unlink_sigs),
179 fsparam_flag ("ecryptfs_mount_auth_tok_only", Opt_mount_auth_tok_only),
180 fsparam_flag ("ecryptfs_check_dev_ruid", Opt_check_dev_ruid),
181 {}
182 };
183
ecryptfs_init_global_auth_toks(struct ecryptfs_mount_crypt_stat * mount_crypt_stat)184 static int ecryptfs_init_global_auth_toks(
185 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
186 {
187 struct ecryptfs_global_auth_tok *global_auth_tok;
188 struct ecryptfs_auth_tok *auth_tok;
189 int rc = 0;
190
191 list_for_each_entry(global_auth_tok,
192 &mount_crypt_stat->global_auth_tok_list,
193 mount_crypt_stat_list) {
194 rc = ecryptfs_keyring_auth_tok_for_sig(
195 &global_auth_tok->global_auth_tok_key, &auth_tok,
196 global_auth_tok->sig);
197 if (rc) {
198 printk(KERN_ERR "Could not find valid key in user "
199 "session keyring for sig specified in mount "
200 "option: [%s]\n", global_auth_tok->sig);
201 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
202 goto out;
203 } else {
204 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
205 up_write(&(global_auth_tok->global_auth_tok_key)->sem);
206 }
207 }
208 out:
209 return rc;
210 }
211
ecryptfs_init_mount_crypt_stat(struct ecryptfs_mount_crypt_stat * mount_crypt_stat)212 static void ecryptfs_init_mount_crypt_stat(
213 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
214 {
215 memset((void *)mount_crypt_stat, 0,
216 sizeof(struct ecryptfs_mount_crypt_stat));
217 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
218 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
219 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
220 }
221
222 struct ecryptfs_fs_context {
223 /* Mount option status trackers */
224 bool check_ruid;
225 bool sig_set;
226 bool cipher_name_set;
227 bool cipher_key_bytes_set;
228 bool fn_cipher_name_set;
229 bool fn_cipher_key_bytes_set;
230 };
231
232 /**
233 * ecryptfs_parse_param
234 * @fc: The ecryptfs filesystem context
235 * @param: The mount parameter to parse
236 *
237 * The signature of the key to use must be the description of a key
238 * already in the keyring. Mounting will fail if the key can not be
239 * found.
240 *
241 * Returns zero on success; non-zero on error
242 */
ecryptfs_parse_param(struct fs_context * fc,struct fs_parameter * param)243 static int ecryptfs_parse_param(
244 struct fs_context *fc,
245 struct fs_parameter *param)
246 {
247 int rc;
248 int opt;
249 struct fs_parse_result result;
250 struct ecryptfs_fs_context *ctx = fc->fs_private;
251 struct ecryptfs_sb_info *sbi = fc->s_fs_info;
252 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
253 &sbi->mount_crypt_stat;
254
255 opt = fs_parse(fc, ecryptfs_fs_param_spec, param, &result);
256 if (opt < 0)
257 return opt;
258
259 switch (opt) {
260 case Opt_sig:
261 case Opt_ecryptfs_sig:
262 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
263 param->string, 0);
264 if (rc) {
265 printk(KERN_ERR "Error attempting to register "
266 "global sig; rc = [%d]\n", rc);
267 return rc;
268 }
269 ctx->sig_set = 1;
270 break;
271 case Opt_cipher:
272 case Opt_ecryptfs_cipher:
273 strscpy(mount_crypt_stat->global_default_cipher_name,
274 param->string);
275 ctx->cipher_name_set = 1;
276 break;
277 case Opt_ecryptfs_key_bytes:
278 mount_crypt_stat->global_default_cipher_key_size =
279 result.uint_32;
280 ctx->cipher_key_bytes_set = 1;
281 break;
282 case Opt_passthrough:
283 mount_crypt_stat->flags |=
284 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
285 break;
286 case Opt_xattr_metadata:
287 mount_crypt_stat->flags |= ECRYPTFS_XATTR_METADATA_ENABLED;
288 break;
289 case Opt_encrypted_view:
290 mount_crypt_stat->flags |= ECRYPTFS_XATTR_METADATA_ENABLED;
291 mount_crypt_stat->flags |= ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
292 break;
293 case Opt_fnek_sig:
294 strscpy(mount_crypt_stat->global_default_fnek_sig,
295 param->string);
296 rc = ecryptfs_add_global_auth_tok(
297 mount_crypt_stat,
298 mount_crypt_stat->global_default_fnek_sig,
299 ECRYPTFS_AUTH_TOK_FNEK);
300 if (rc) {
301 printk(KERN_ERR "Error attempting to register "
302 "global fnek sig [%s]; rc = [%d]\n",
303 mount_crypt_stat->global_default_fnek_sig, rc);
304 return rc;
305 }
306 mount_crypt_stat->flags |=
307 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
308 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
309 break;
310 case Opt_fn_cipher:
311 strscpy(mount_crypt_stat->global_default_fn_cipher_name,
312 param->string);
313 ctx->fn_cipher_name_set = 1;
314 break;
315 case Opt_fn_cipher_key_bytes:
316 mount_crypt_stat->global_default_fn_cipher_key_bytes =
317 result.uint_32;
318 ctx->fn_cipher_key_bytes_set = 1;
319 break;
320 case Opt_unlink_sigs:
321 mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
322 break;
323 case Opt_mount_auth_tok_only:
324 mount_crypt_stat->flags |= ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
325 break;
326 case Opt_check_dev_ruid:
327 ctx->check_ruid = 1;
328 break;
329 default:
330 return -EINVAL;
331 }
332
333 return 0;
334 }
335
ecryptfs_validate_options(struct fs_context * fc)336 static int ecryptfs_validate_options(struct fs_context *fc)
337 {
338 int rc = 0;
339 u8 cipher_code;
340 struct ecryptfs_fs_context *ctx = fc->fs_private;
341 struct ecryptfs_sb_info *sbi = fc->s_fs_info;
342 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
343
344
345 mount_crypt_stat = &sbi->mount_crypt_stat;
346
347 if (!ctx->sig_set) {
348 rc = -EINVAL;
349 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
350 "auth tok signature as a mount "
351 "parameter; see the eCryptfs README\n");
352 goto out;
353 }
354 if (!ctx->cipher_name_set) {
355 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
356
357 BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
358 strscpy(mount_crypt_stat->global_default_cipher_name,
359 ECRYPTFS_DEFAULT_CIPHER);
360 }
361 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
362 && !ctx->fn_cipher_name_set)
363 strscpy(mount_crypt_stat->global_default_fn_cipher_name,
364 mount_crypt_stat->global_default_cipher_name);
365 if (!ctx->cipher_key_bytes_set)
366 mount_crypt_stat->global_default_cipher_key_size = 0;
367 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
368 && !ctx->fn_cipher_key_bytes_set)
369 mount_crypt_stat->global_default_fn_cipher_key_bytes =
370 mount_crypt_stat->global_default_cipher_key_size;
371
372 cipher_code = ecryptfs_code_for_cipher_string(
373 mount_crypt_stat->global_default_cipher_name,
374 mount_crypt_stat->global_default_cipher_key_size);
375 if (!cipher_code) {
376 ecryptfs_printk(KERN_ERR,
377 "eCryptfs doesn't support cipher: %s\n",
378 mount_crypt_stat->global_default_cipher_name);
379 rc = -EINVAL;
380 goto out;
381 }
382
383 mutex_lock(&key_tfm_list_mutex);
384 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
385 NULL)) {
386 rc = ecryptfs_add_new_key_tfm(
387 NULL, mount_crypt_stat->global_default_cipher_name,
388 mount_crypt_stat->global_default_cipher_key_size);
389 if (rc) {
390 printk(KERN_ERR "Error attempting to initialize "
391 "cipher with name = [%s] and key size = [%td]; "
392 "rc = [%d]\n",
393 mount_crypt_stat->global_default_cipher_name,
394 mount_crypt_stat->global_default_cipher_key_size,
395 rc);
396 rc = -EINVAL;
397 mutex_unlock(&key_tfm_list_mutex);
398 goto out;
399 }
400 }
401 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
402 && !ecryptfs_tfm_exists(
403 mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
404 rc = ecryptfs_add_new_key_tfm(
405 NULL, mount_crypt_stat->global_default_fn_cipher_name,
406 mount_crypt_stat->global_default_fn_cipher_key_bytes);
407 if (rc) {
408 printk(KERN_ERR "Error attempting to initialize "
409 "cipher with name = [%s] and key size = [%td]; "
410 "rc = [%d]\n",
411 mount_crypt_stat->global_default_fn_cipher_name,
412 mount_crypt_stat->global_default_fn_cipher_key_bytes,
413 rc);
414 rc = -EINVAL;
415 mutex_unlock(&key_tfm_list_mutex);
416 goto out;
417 }
418 }
419 mutex_unlock(&key_tfm_list_mutex);
420 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
421 if (rc)
422 printk(KERN_WARNING "One or more global auth toks could not "
423 "properly register; rc = [%d]\n", rc);
424 out:
425 return rc;
426 }
427
428 struct kmem_cache *ecryptfs_sb_info_cache;
429 static struct file_system_type ecryptfs_fs_type;
430
431 /*
432 * ecryptfs_get_tree
433 * @fc: The filesystem context
434 */
ecryptfs_get_tree(struct fs_context * fc)435 static int ecryptfs_get_tree(struct fs_context *fc)
436 {
437 struct super_block *s;
438 struct ecryptfs_fs_context *ctx = fc->fs_private;
439 struct ecryptfs_sb_info *sbi = fc->s_fs_info;
440 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
441 const char *err = "Getting sb failed";
442 struct inode *inode;
443 struct path path;
444 int rc;
445
446 if (!fc->source) {
447 rc = -EINVAL;
448 err = "Device name cannot be null";
449 goto out;
450 }
451
452 mount_crypt_stat = &sbi->mount_crypt_stat;
453 rc = ecryptfs_validate_options(fc);
454 if (rc) {
455 err = "Error validating options";
456 goto out;
457 }
458
459 if (fips_enabled) {
460 rc = -EINVAL;
461 err = "eCryptfs support is disabled due to FIPS";
462 goto out;
463 }
464
465 s = sget_fc(fc, NULL, set_anon_super_fc);
466 if (IS_ERR(s)) {
467 rc = PTR_ERR(s);
468 goto out;
469 }
470
471 rc = super_setup_bdi(s);
472 if (rc)
473 goto out1;
474
475 ecryptfs_set_superblock_private(s, sbi);
476
477 /* ->kill_sb() will take care of sbi after that point */
478 sbi = NULL;
479 s->s_op = &ecryptfs_sops;
480 s->s_xattr = ecryptfs_xattr_handlers;
481 set_default_d_op(s, &ecryptfs_dops);
482
483 err = "Reading sb failed";
484 rc = kern_path(fc->source, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
485 if (rc) {
486 ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
487 goto out1;
488 }
489 if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
490 rc = -EINVAL;
491 printk(KERN_ERR "Mount on filesystem of type "
492 "eCryptfs explicitly disallowed due to "
493 "known incompatibilities\n");
494 goto out_free;
495 }
496
497 if (is_idmapped_mnt(path.mnt)) {
498 rc = -EINVAL;
499 printk(KERN_ERR "Mounting on idmapped mounts currently disallowed\n");
500 goto out_free;
501 }
502
503 if (ctx->check_ruid &&
504 !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
505 rc = -EPERM;
506 printk(KERN_ERR "Mount of device (uid: %d) not owned by "
507 "requested user (uid: %d)\n",
508 i_uid_read(d_inode(path.dentry)),
509 from_kuid(&init_user_ns, current_uid()));
510 goto out_free;
511 }
512
513 ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
514
515 /**
516 * Set the POSIX ACL flag based on whether they're enabled in the lower
517 * mount.
518 */
519 s->s_flags = fc->sb_flags & ~SB_POSIXACL;
520 s->s_flags |= path.dentry->d_sb->s_flags & SB_POSIXACL;
521
522 /**
523 * Force a read-only eCryptfs mount when:
524 * 1) The lower mount is ro
525 * 2) The ecryptfs_encrypted_view mount option is specified
526 */
527 if (sb_rdonly(path.dentry->d_sb) || mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
528 s->s_flags |= SB_RDONLY;
529
530 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
531 s->s_blocksize = path.dentry->d_sb->s_blocksize;
532 s->s_magic = ECRYPTFS_SUPER_MAGIC;
533 s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
534
535 rc = -EINVAL;
536 if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
537 pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
538 goto out_free;
539 }
540
541 inode = ecryptfs_get_inode(d_inode(path.dentry), s);
542 rc = PTR_ERR(inode);
543 if (IS_ERR(inode))
544 goto out_free;
545
546 s->s_root = d_make_root(inode);
547 if (!s->s_root) {
548 rc = -ENOMEM;
549 goto out_free;
550 }
551
552 ecryptfs_set_dentry_lower(s->s_root, path.dentry);
553 ecryptfs_superblock_to_private(s)->lower_mnt = path.mnt;
554
555 s->s_flags |= SB_ACTIVE;
556 fc->root = dget(s->s_root);
557 return 0;
558
559 out_free:
560 path_put(&path);
561 out1:
562 deactivate_locked_super(s);
563 out:
564 if (sbi)
565 ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
566
567 printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
568 return rc;
569 }
570
571 /**
572 * ecryptfs_kill_block_super
573 * @sb: The ecryptfs super block
574 *
575 * Used to bring the superblock down and free the private data.
576 */
ecryptfs_kill_block_super(struct super_block * sb)577 static void ecryptfs_kill_block_super(struct super_block *sb)
578 {
579 struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
580 kill_anon_super(sb);
581 if (!sb_info)
582 return;
583 mntput(sb_info->lower_mnt);
584 ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
585 kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
586 }
587
ecryptfs_free_fc(struct fs_context * fc)588 static void ecryptfs_free_fc(struct fs_context *fc)
589 {
590 struct ecryptfs_fs_context *ctx = fc->fs_private;
591 struct ecryptfs_sb_info *sbi = fc->s_fs_info;
592
593 kfree(ctx);
594
595 if (sbi) {
596 ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
597 kmem_cache_free(ecryptfs_sb_info_cache, sbi);
598 }
599 }
600
601 static const struct fs_context_operations ecryptfs_context_ops = {
602 .free = ecryptfs_free_fc,
603 .parse_param = ecryptfs_parse_param,
604 .get_tree = ecryptfs_get_tree,
605 .reconfigure = NULL,
606 };
607
ecryptfs_init_fs_context(struct fs_context * fc)608 static int ecryptfs_init_fs_context(struct fs_context *fc)
609 {
610 struct ecryptfs_fs_context *ctx;
611 struct ecryptfs_sb_info *sbi = NULL;
612
613 ctx = kzalloc_obj(struct ecryptfs_fs_context);
614 if (!ctx)
615 return -ENOMEM;
616 sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
617 if (!sbi) {
618 kfree(ctx);
619 ctx = NULL;
620 return -ENOMEM;
621 }
622
623 ecryptfs_init_mount_crypt_stat(&sbi->mount_crypt_stat);
624
625 fc->fs_private = ctx;
626 fc->s_fs_info = sbi;
627 fc->ops = &ecryptfs_context_ops;
628 return 0;
629 }
630
631 static struct file_system_type ecryptfs_fs_type = {
632 .owner = THIS_MODULE,
633 .name = "ecryptfs",
634 .init_fs_context = ecryptfs_init_fs_context,
635 .parameters = ecryptfs_fs_param_spec,
636 .kill_sb = ecryptfs_kill_block_super,
637 .fs_flags = 0
638 };
639 MODULE_ALIAS_FS("ecryptfs");
640
641 /*
642 * inode_info_init_once
643 *
644 * Initializes the ecryptfs_inode_info_cache when it is created
645 */
646 static void
inode_info_init_once(void * vptr)647 inode_info_init_once(void *vptr)
648 {
649 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
650
651 inode_init_once(&ei->vfs_inode);
652 }
653
654 static struct ecryptfs_cache_info {
655 struct kmem_cache **cache;
656 const char *name;
657 size_t size;
658 slab_flags_t flags;
659 void (*ctor)(void *obj);
660 } ecryptfs_cache_infos[] = {
661 {
662 .cache = &ecryptfs_auth_tok_list_item_cache,
663 .name = "ecryptfs_auth_tok_list_item",
664 .size = sizeof(struct ecryptfs_auth_tok_list_item),
665 },
666 {
667 .cache = &ecryptfs_file_info_cache,
668 .name = "ecryptfs_file_cache",
669 .size = sizeof(struct ecryptfs_file_info),
670 },
671 {
672 .cache = &ecryptfs_inode_info_cache,
673 .name = "ecryptfs_inode_cache",
674 .size = sizeof(struct ecryptfs_inode_info),
675 .flags = SLAB_ACCOUNT,
676 .ctor = inode_info_init_once,
677 },
678 {
679 .cache = &ecryptfs_sb_info_cache,
680 .name = "ecryptfs_sb_cache",
681 .size = sizeof(struct ecryptfs_sb_info),
682 },
683 {
684 .cache = &ecryptfs_header_cache,
685 .name = "ecryptfs_headers",
686 .size = PAGE_SIZE,
687 },
688 {
689 .cache = &ecryptfs_xattr_cache,
690 .name = "ecryptfs_xattr_cache",
691 .size = PAGE_SIZE,
692 },
693 {
694 .cache = &ecryptfs_key_record_cache,
695 .name = "ecryptfs_key_record_cache",
696 .size = sizeof(struct ecryptfs_key_record),
697 },
698 {
699 .cache = &ecryptfs_key_sig_cache,
700 .name = "ecryptfs_key_sig_cache",
701 .size = sizeof(struct ecryptfs_key_sig),
702 },
703 {
704 .cache = &ecryptfs_global_auth_tok_cache,
705 .name = "ecryptfs_global_auth_tok_cache",
706 .size = sizeof(struct ecryptfs_global_auth_tok),
707 },
708 {
709 .cache = &ecryptfs_key_tfm_cache,
710 .name = "ecryptfs_key_tfm_cache",
711 .size = sizeof(struct ecryptfs_key_tfm),
712 },
713 };
714
ecryptfs_free_kmem_caches(void)715 static void ecryptfs_free_kmem_caches(void)
716 {
717 int i;
718
719 /*
720 * Make sure all delayed rcu free inodes are flushed before we
721 * destroy cache.
722 */
723 rcu_barrier();
724
725 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
726 struct ecryptfs_cache_info *info;
727
728 info = &ecryptfs_cache_infos[i];
729 kmem_cache_destroy(*(info->cache));
730 }
731 }
732
733 /**
734 * ecryptfs_init_kmem_caches
735 *
736 * Returns zero on success; non-zero otherwise
737 */
ecryptfs_init_kmem_caches(void)738 static int ecryptfs_init_kmem_caches(void)
739 {
740 int i;
741
742 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
743 struct ecryptfs_cache_info *info;
744
745 info = &ecryptfs_cache_infos[i];
746 *(info->cache) = kmem_cache_create(info->name, info->size, 0,
747 SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
748 if (!*(info->cache)) {
749 ecryptfs_free_kmem_caches();
750 ecryptfs_printk(KERN_WARNING, "%s: "
751 "kmem_cache_create failed\n",
752 info->name);
753 return -ENOMEM;
754 }
755 }
756 return 0;
757 }
758
759 static struct kobject *ecryptfs_kobj;
760
version_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)761 static ssize_t version_show(struct kobject *kobj,
762 struct kobj_attribute *attr, char *buff)
763 {
764 return sysfs_emit(buff, "%d\n", ECRYPTFS_VERSIONING_MASK);
765 }
766
767 static struct kobj_attribute version_attr = __ATTR_RO(version);
768
769 static struct attribute *attributes[] = {
770 &version_attr.attr,
771 NULL,
772 };
773
774 static const struct attribute_group attr_group = {
775 .attrs = attributes,
776 };
777
do_sysfs_registration(void)778 static int do_sysfs_registration(void)
779 {
780 int rc;
781
782 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
783 if (!ecryptfs_kobj) {
784 printk(KERN_ERR "Unable to create ecryptfs kset\n");
785 rc = -ENOMEM;
786 goto out;
787 }
788 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
789 if (rc) {
790 printk(KERN_ERR
791 "Unable to create ecryptfs version attributes\n");
792 kobject_put(ecryptfs_kobj);
793 }
794 out:
795 return rc;
796 }
797
do_sysfs_unregistration(void)798 static void do_sysfs_unregistration(void)
799 {
800 sysfs_remove_group(ecryptfs_kobj, &attr_group);
801 kobject_put(ecryptfs_kobj);
802 }
803
ecryptfs_init(void)804 static int __init ecryptfs_init(void)
805 {
806 int rc;
807
808 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
809 rc = -EINVAL;
810 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
811 "larger than the host's page size, and so "
812 "eCryptfs cannot run on this system. The "
813 "default eCryptfs extent size is [%u] bytes; "
814 "the page size is [%lu] bytes.\n",
815 ECRYPTFS_DEFAULT_EXTENT_SIZE,
816 (unsigned long)PAGE_SIZE);
817 goto out;
818 }
819 rc = ecryptfs_init_kmem_caches();
820 if (rc) {
821 printk(KERN_ERR
822 "Failed to allocate one or more kmem_cache objects\n");
823 goto out;
824 }
825 rc = do_sysfs_registration();
826 if (rc) {
827 printk(KERN_ERR "sysfs registration failed\n");
828 goto out_free_kmem_caches;
829 }
830 rc = ecryptfs_init_kthread();
831 if (rc) {
832 printk(KERN_ERR "%s: kthread initialization failed; "
833 "rc = [%d]\n", __func__, rc);
834 goto out_do_sysfs_unregistration;
835 }
836 rc = ecryptfs_init_messaging();
837 if (rc) {
838 printk(KERN_ERR "Failure occurred while attempting to "
839 "initialize the communications channel to "
840 "ecryptfsd\n");
841 goto out_destroy_kthread;
842 }
843 rc = ecryptfs_init_crypto();
844 if (rc) {
845 printk(KERN_ERR "Failure whilst attempting to init crypto; "
846 "rc = [%d]\n", rc);
847 goto out_release_messaging;
848 }
849 rc = register_filesystem(&ecryptfs_fs_type);
850 if (rc) {
851 printk(KERN_ERR "Failed to register filesystem\n");
852 goto out_destroy_crypto;
853 }
854 if (ecryptfs_verbosity > 0)
855 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
856 "will be written to the syslog!\n", ecryptfs_verbosity);
857
858 goto out;
859 out_destroy_crypto:
860 ecryptfs_destroy_crypto();
861 out_release_messaging:
862 ecryptfs_release_messaging();
863 out_destroy_kthread:
864 ecryptfs_destroy_kthread();
865 out_do_sysfs_unregistration:
866 do_sysfs_unregistration();
867 out_free_kmem_caches:
868 ecryptfs_free_kmem_caches();
869 out:
870 return rc;
871 }
872
ecryptfs_exit(void)873 static void __exit ecryptfs_exit(void)
874 {
875 int rc;
876
877 rc = ecryptfs_destroy_crypto();
878 if (rc)
879 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
880 "rc = [%d]\n", rc);
881 ecryptfs_release_messaging();
882 ecryptfs_destroy_kthread();
883 do_sysfs_unregistration();
884 unregister_filesystem(&ecryptfs_fs_type);
885 ecryptfs_free_kmem_caches();
886 }
887
888 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
889 MODULE_DESCRIPTION("eCryptfs");
890
891 MODULE_LICENSE("GPL");
892
893 module_init(ecryptfs_init)
894 module_exit(ecryptfs_exit)
895