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-2007 International Business Machines Corp. 7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 8 * Michael C. Thompson <mcthomps@us.ibm.com> 9 * Tyler Hicks <tyhicks@ou.edu> 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/pagemap.h> 36 #include <linux/key.h> 37 #include <linux/parser.h> 38 #include <linux/fs_stack.h> 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 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 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 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 rc = ecryptfs_privileged_open(&inode_info->lower_file, 134 lower_dentry, lower_mnt); 135 if (rc || IS_ERR(inode_info->lower_file)) { 136 printk(KERN_ERR "Error opening lower persistent file " 137 "for lower_dentry [0x%p] and lower_mnt [0x%p]; " 138 "rc = [%d]\n", lower_dentry, lower_mnt, rc); 139 rc = PTR_ERR(inode_info->lower_file); 140 inode_info->lower_file = NULL; 141 } 142 } 143 mutex_unlock(&inode_info->lower_file_mutex); 144 return rc; 145 } 146 147 /** 148 * ecryptfs_interpose 149 * @lower_dentry: Existing dentry in the lower filesystem 150 * @dentry: ecryptfs' dentry 151 * @sb: ecryptfs's super_block 152 * @flags: flags to govern behavior of interpose procedure 153 * 154 * Interposes upper and lower dentries. 155 * 156 * Returns zero on success; non-zero otherwise 157 */ 158 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry, 159 struct super_block *sb, u32 flags) 160 { 161 struct inode *lower_inode; 162 struct inode *inode; 163 int rc = 0; 164 165 lower_inode = lower_dentry->d_inode; 166 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) { 167 rc = -EXDEV; 168 goto out; 169 } 170 if (!igrab(lower_inode)) { 171 rc = -ESTALE; 172 goto out; 173 } 174 inode = iget5_locked(sb, (unsigned long)lower_inode, 175 ecryptfs_inode_test, ecryptfs_inode_set, 176 lower_inode); 177 if (!inode) { 178 rc = -EACCES; 179 iput(lower_inode); 180 goto out; 181 } 182 if (inode->i_state & I_NEW) 183 unlock_new_inode(inode); 184 else 185 iput(lower_inode); 186 if (S_ISLNK(lower_inode->i_mode)) 187 inode->i_op = &ecryptfs_symlink_iops; 188 else if (S_ISDIR(lower_inode->i_mode)) 189 inode->i_op = &ecryptfs_dir_iops; 190 if (S_ISDIR(lower_inode->i_mode)) 191 inode->i_fop = &ecryptfs_dir_fops; 192 if (special_file(lower_inode->i_mode)) 193 init_special_inode(inode, lower_inode->i_mode, 194 lower_inode->i_rdev); 195 dentry->d_op = &ecryptfs_dops; 196 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD) 197 d_add(dentry, inode); 198 else 199 d_instantiate(dentry, inode); 200 fsstack_copy_attr_all(inode, lower_inode, NULL); 201 /* This size will be overwritten for real files w/ headers and 202 * other metadata */ 203 fsstack_copy_inode_size(inode, lower_inode); 204 if (!(flags & ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE)) { 205 rc = ecryptfs_init_persistent_file(dentry); 206 if (rc) { 207 printk(KERN_ERR "%s: Error attempting to initialize " 208 "the persistent file for the dentry with name " 209 "[%s]; rc = [%d]\n", __func__, 210 dentry->d_name.name, rc); 211 goto out; 212 } 213 } else { 214 struct ecryptfs_inode_info *inode_info = 215 ecryptfs_inode_to_private(dentry->d_inode); 216 217 inode_info->lower_file = NULL; 218 inode_info->crypt_stat.flags |= ECRYPTFS_DELAY_PERSISTENT; 219 } 220 out: 221 return rc; 222 } 223 224 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, 225 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher, 226 ecryptfs_opt_ecryptfs_key_bytes, 227 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata, 228 ecryptfs_opt_encrypted_view, ecryptfs_opt_err }; 229 230 static match_table_t tokens = { 231 {ecryptfs_opt_sig, "sig=%s"}, 232 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"}, 233 {ecryptfs_opt_cipher, "cipher=%s"}, 234 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"}, 235 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"}, 236 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"}, 237 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"}, 238 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"}, 239 {ecryptfs_opt_err, NULL} 240 }; 241 242 static int ecryptfs_init_global_auth_toks( 243 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 244 { 245 struct ecryptfs_global_auth_tok *global_auth_tok; 246 int rc = 0; 247 248 list_for_each_entry(global_auth_tok, 249 &mount_crypt_stat->global_auth_tok_list, 250 mount_crypt_stat_list) { 251 rc = ecryptfs_keyring_auth_tok_for_sig( 252 &global_auth_tok->global_auth_tok_key, 253 &global_auth_tok->global_auth_tok, 254 global_auth_tok->sig); 255 if (rc) { 256 printk(KERN_ERR "Could not find valid key in user " 257 "session keyring for sig specified in mount " 258 "option: [%s]\n", global_auth_tok->sig); 259 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; 260 goto out; 261 } else 262 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; 263 } 264 out: 265 return rc; 266 } 267 268 static void ecryptfs_init_mount_crypt_stat( 269 struct ecryptfs_mount_crypt_stat *mount_crypt_stat) 270 { 271 memset((void *)mount_crypt_stat, 0, 272 sizeof(struct ecryptfs_mount_crypt_stat)); 273 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list); 274 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex); 275 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED; 276 } 277 278 /** 279 * ecryptfs_parse_options 280 * @sb: The ecryptfs super block 281 * @options: The options pased to the kernel 282 * 283 * Parse mount options: 284 * debug=N - ecryptfs_verbosity level for debug output 285 * sig=XXX - description(signature) of the key to use 286 * 287 * Returns the dentry object of the lower-level (lower/interposed) 288 * directory; We want to mount our stackable file system on top of 289 * that lower directory. 290 * 291 * The signature of the key to use must be the description of a key 292 * already in the keyring. Mounting will fail if the key can not be 293 * found. 294 * 295 * Returns zero on success; non-zero on error 296 */ 297 static int ecryptfs_parse_options(struct super_block *sb, char *options) 298 { 299 char *p; 300 int rc = 0; 301 int sig_set = 0; 302 int cipher_name_set = 0; 303 int cipher_key_bytes; 304 int cipher_key_bytes_set = 0; 305 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 306 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; 307 substring_t args[MAX_OPT_ARGS]; 308 int token; 309 char *sig_src; 310 char *cipher_name_dst; 311 char *cipher_name_src; 312 char *cipher_key_bytes_src; 313 314 if (!options) { 315 rc = -EINVAL; 316 goto out; 317 } 318 ecryptfs_init_mount_crypt_stat(mount_crypt_stat); 319 while ((p = strsep(&options, ",")) != NULL) { 320 if (!*p) 321 continue; 322 token = match_token(p, tokens, args); 323 switch (token) { 324 case ecryptfs_opt_sig: 325 case ecryptfs_opt_ecryptfs_sig: 326 sig_src = args[0].from; 327 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat, 328 sig_src); 329 if (rc) { 330 printk(KERN_ERR "Error attempting to register " 331 "global sig; rc = [%d]\n", rc); 332 goto out; 333 } 334 sig_set = 1; 335 break; 336 case ecryptfs_opt_cipher: 337 case ecryptfs_opt_ecryptfs_cipher: 338 cipher_name_src = args[0].from; 339 cipher_name_dst = 340 mount_crypt_stat-> 341 global_default_cipher_name; 342 strncpy(cipher_name_dst, cipher_name_src, 343 ECRYPTFS_MAX_CIPHER_NAME_SIZE); 344 ecryptfs_printk(KERN_DEBUG, 345 "The mount_crypt_stat " 346 "global_default_cipher_name set to: " 347 "[%s]\n", cipher_name_dst); 348 cipher_name_set = 1; 349 break; 350 case ecryptfs_opt_ecryptfs_key_bytes: 351 cipher_key_bytes_src = args[0].from; 352 cipher_key_bytes = 353 (int)simple_strtol(cipher_key_bytes_src, 354 &cipher_key_bytes_src, 0); 355 mount_crypt_stat->global_default_cipher_key_size = 356 cipher_key_bytes; 357 ecryptfs_printk(KERN_DEBUG, 358 "The mount_crypt_stat " 359 "global_default_cipher_key_size " 360 "set to: [%d]\n", mount_crypt_stat-> 361 global_default_cipher_key_size); 362 cipher_key_bytes_set = 1; 363 break; 364 case ecryptfs_opt_passthrough: 365 mount_crypt_stat->flags |= 366 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED; 367 break; 368 case ecryptfs_opt_xattr_metadata: 369 mount_crypt_stat->flags |= 370 ECRYPTFS_XATTR_METADATA_ENABLED; 371 break; 372 case ecryptfs_opt_encrypted_view: 373 mount_crypt_stat->flags |= 374 ECRYPTFS_XATTR_METADATA_ENABLED; 375 mount_crypt_stat->flags |= 376 ECRYPTFS_ENCRYPTED_VIEW_ENABLED; 377 break; 378 case ecryptfs_opt_err: 379 default: 380 ecryptfs_printk(KERN_WARNING, 381 "eCryptfs: unrecognized option '%s'\n", 382 p); 383 } 384 } 385 if (!sig_set) { 386 rc = -EINVAL; 387 ecryptfs_printk(KERN_ERR, "You must supply at least one valid " 388 "auth tok signature as a mount " 389 "parameter; see the eCryptfs README\n"); 390 goto out; 391 } 392 if (!cipher_name_set) { 393 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER); 394 395 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE); 396 397 strcpy(mount_crypt_stat->global_default_cipher_name, 398 ECRYPTFS_DEFAULT_CIPHER); 399 } 400 if (!cipher_key_bytes_set) { 401 mount_crypt_stat->global_default_cipher_key_size = 0; 402 } 403 mutex_lock(&key_tfm_list_mutex); 404 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name, 405 NULL)) 406 rc = ecryptfs_add_new_key_tfm( 407 NULL, mount_crypt_stat->global_default_cipher_name, 408 mount_crypt_stat->global_default_cipher_key_size); 409 mutex_unlock(&key_tfm_list_mutex); 410 if (rc) { 411 printk(KERN_ERR "Error attempting to initialize cipher with " 412 "name = [%s] and key size = [%td]; rc = [%d]\n", 413 mount_crypt_stat->global_default_cipher_name, 414 mount_crypt_stat->global_default_cipher_key_size, rc); 415 rc = -EINVAL; 416 goto out; 417 } 418 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat); 419 if (rc) { 420 printk(KERN_WARNING "One or more global auth toks could not " 421 "properly register; rc = [%d]\n", rc); 422 } 423 out: 424 return rc; 425 } 426 427 struct kmem_cache *ecryptfs_sb_info_cache; 428 429 /** 430 * ecryptfs_fill_super 431 * @sb: The ecryptfs super block 432 * @raw_data: The options passed to mount 433 * @silent: Not used but required by function prototype 434 * 435 * Sets up what we can of the sb, rest is done in ecryptfs_read_super 436 * 437 * Returns zero on success; non-zero otherwise 438 */ 439 static int 440 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent) 441 { 442 int rc = 0; 443 444 /* Released in ecryptfs_put_super() */ 445 ecryptfs_set_superblock_private(sb, 446 kmem_cache_zalloc(ecryptfs_sb_info_cache, 447 GFP_KERNEL)); 448 if (!ecryptfs_superblock_to_private(sb)) { 449 ecryptfs_printk(KERN_WARNING, "Out of memory\n"); 450 rc = -ENOMEM; 451 goto out; 452 } 453 sb->s_op = &ecryptfs_sops; 454 /* Released through deactivate_super(sb) from get_sb_nodev */ 455 sb->s_root = d_alloc(NULL, &(const struct qstr) { 456 .hash = 0,.name = "/",.len = 1}); 457 if (!sb->s_root) { 458 ecryptfs_printk(KERN_ERR, "d_alloc failed\n"); 459 rc = -ENOMEM; 460 goto out; 461 } 462 sb->s_root->d_op = &ecryptfs_dops; 463 sb->s_root->d_sb = sb; 464 sb->s_root->d_parent = sb->s_root; 465 /* Released in d_release when dput(sb->s_root) is called */ 466 /* through deactivate_super(sb) from get_sb_nodev() */ 467 ecryptfs_set_dentry_private(sb->s_root, 468 kmem_cache_zalloc(ecryptfs_dentry_info_cache, 469 GFP_KERNEL)); 470 if (!ecryptfs_dentry_to_private(sb->s_root)) { 471 ecryptfs_printk(KERN_ERR, 472 "dentry_info_cache alloc failed\n"); 473 rc = -ENOMEM; 474 goto out; 475 } 476 rc = 0; 477 out: 478 /* Should be able to rely on deactivate_super called from 479 * get_sb_nodev */ 480 return rc; 481 } 482 483 /** 484 * ecryptfs_read_super 485 * @sb: The ecryptfs super block 486 * @dev_name: The path to mount over 487 * 488 * Read the super block of the lower filesystem, and use 489 * ecryptfs_interpose to create our initial inode and super block 490 * struct. 491 */ 492 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name) 493 { 494 int rc; 495 struct nameidata nd; 496 struct dentry *lower_root; 497 struct vfsmount *lower_mnt; 498 499 memset(&nd, 0, sizeof(struct nameidata)); 500 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd); 501 if (rc) { 502 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n"); 503 goto out; 504 } 505 lower_root = nd.path.dentry; 506 lower_mnt = nd.path.mnt; 507 ecryptfs_set_superblock_lower(sb, lower_root->d_sb); 508 sb->s_maxbytes = lower_root->d_sb->s_maxbytes; 509 sb->s_blocksize = lower_root->d_sb->s_blocksize; 510 ecryptfs_set_dentry_lower(sb->s_root, lower_root); 511 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt); 512 rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0); 513 if (rc) 514 goto out_free; 515 rc = 0; 516 goto out; 517 out_free: 518 path_put(&nd.path); 519 out: 520 return rc; 521 } 522 523 /** 524 * ecryptfs_get_sb 525 * @fs_type 526 * @flags 527 * @dev_name: The path to mount over 528 * @raw_data: The options passed into the kernel 529 * 530 * The whole ecryptfs_get_sb process is broken into 4 functions: 531 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any 532 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block 533 * with as much information as it can before needing 534 * the lower filesystem. 535 * ecryptfs_read_super(): this accesses the lower filesystem and uses 536 * ecryptfs_interpolate to perform most of the linking 537 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs 538 */ 539 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags, 540 const char *dev_name, void *raw_data, 541 struct vfsmount *mnt) 542 { 543 int rc; 544 struct super_block *sb; 545 546 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt); 547 if (rc < 0) { 548 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc); 549 goto out; 550 } 551 sb = mnt->mnt_sb; 552 rc = ecryptfs_parse_options(sb, raw_data); 553 if (rc) { 554 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc); 555 goto out_abort; 556 } 557 rc = ecryptfs_read_super(sb, dev_name); 558 if (rc) { 559 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc); 560 goto out_abort; 561 } 562 goto out; 563 out_abort: 564 dput(sb->s_root); 565 up_write(&sb->s_umount); 566 deactivate_super(sb); 567 out: 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 * Private data is free'd in ecryptfs_put_super() 577 */ 578 static void ecryptfs_kill_block_super(struct super_block *sb) 579 { 580 generic_shutdown_super(sb); 581 } 582 583 static struct file_system_type ecryptfs_fs_type = { 584 .owner = THIS_MODULE, 585 .name = "ecryptfs", 586 .get_sb = ecryptfs_get_sb, 587 .kill_sb = ecryptfs_kill_block_super, 588 .fs_flags = 0 589 }; 590 591 /** 592 * inode_info_init_once 593 * 594 * Initializes the ecryptfs_inode_info_cache when it is created 595 */ 596 static void 597 inode_info_init_once(struct kmem_cache *cachep, void *vptr) 598 { 599 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; 600 601 inode_init_once(&ei->vfs_inode); 602 } 603 604 static struct ecryptfs_cache_info { 605 struct kmem_cache **cache; 606 const char *name; 607 size_t size; 608 void (*ctor)(struct kmem_cache *cache, void *obj); 609 } ecryptfs_cache_infos[] = { 610 { 611 .cache = &ecryptfs_auth_tok_list_item_cache, 612 .name = "ecryptfs_auth_tok_list_item", 613 .size = sizeof(struct ecryptfs_auth_tok_list_item), 614 }, 615 { 616 .cache = &ecryptfs_file_info_cache, 617 .name = "ecryptfs_file_cache", 618 .size = sizeof(struct ecryptfs_file_info), 619 }, 620 { 621 .cache = &ecryptfs_dentry_info_cache, 622 .name = "ecryptfs_dentry_info_cache", 623 .size = sizeof(struct ecryptfs_dentry_info), 624 }, 625 { 626 .cache = &ecryptfs_inode_info_cache, 627 .name = "ecryptfs_inode_cache", 628 .size = sizeof(struct ecryptfs_inode_info), 629 .ctor = inode_info_init_once, 630 }, 631 { 632 .cache = &ecryptfs_sb_info_cache, 633 .name = "ecryptfs_sb_cache", 634 .size = sizeof(struct ecryptfs_sb_info), 635 }, 636 { 637 .cache = &ecryptfs_header_cache_1, 638 .name = "ecryptfs_headers_1", 639 .size = PAGE_CACHE_SIZE, 640 }, 641 { 642 .cache = &ecryptfs_header_cache_2, 643 .name = "ecryptfs_headers_2", 644 .size = PAGE_CACHE_SIZE, 645 }, 646 { 647 .cache = &ecryptfs_xattr_cache, 648 .name = "ecryptfs_xattr_cache", 649 .size = PAGE_CACHE_SIZE, 650 }, 651 { 652 .cache = &ecryptfs_key_record_cache, 653 .name = "ecryptfs_key_record_cache", 654 .size = sizeof(struct ecryptfs_key_record), 655 }, 656 { 657 .cache = &ecryptfs_key_sig_cache, 658 .name = "ecryptfs_key_sig_cache", 659 .size = sizeof(struct ecryptfs_key_sig), 660 }, 661 { 662 .cache = &ecryptfs_global_auth_tok_cache, 663 .name = "ecryptfs_global_auth_tok_cache", 664 .size = sizeof(struct ecryptfs_global_auth_tok), 665 }, 666 { 667 .cache = &ecryptfs_key_tfm_cache, 668 .name = "ecryptfs_key_tfm_cache", 669 .size = sizeof(struct ecryptfs_key_tfm), 670 }, 671 { 672 .cache = &ecryptfs_open_req_cache, 673 .name = "ecryptfs_open_req_cache", 674 .size = sizeof(struct ecryptfs_open_req), 675 }, 676 }; 677 678 static void ecryptfs_free_kmem_caches(void) 679 { 680 int i; 681 682 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { 683 struct ecryptfs_cache_info *info; 684 685 info = &ecryptfs_cache_infos[i]; 686 if (*(info->cache)) 687 kmem_cache_destroy(*(info->cache)); 688 } 689 } 690 691 /** 692 * ecryptfs_init_kmem_caches 693 * 694 * Returns zero on success; non-zero otherwise 695 */ 696 static int ecryptfs_init_kmem_caches(void) 697 { 698 int i; 699 700 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { 701 struct ecryptfs_cache_info *info; 702 703 info = &ecryptfs_cache_infos[i]; 704 *(info->cache) = kmem_cache_create(info->name, info->size, 705 0, SLAB_HWCACHE_ALIGN, info->ctor); 706 if (!*(info->cache)) { 707 ecryptfs_free_kmem_caches(); 708 ecryptfs_printk(KERN_WARNING, "%s: " 709 "kmem_cache_create failed\n", 710 info->name); 711 return -ENOMEM; 712 } 713 } 714 return 0; 715 } 716 717 static struct kobject *ecryptfs_kobj; 718 719 static ssize_t version_show(struct kobject *kobj, 720 struct kobj_attribute *attr, char *buff) 721 { 722 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK); 723 } 724 725 static struct kobj_attribute version_attr = __ATTR_RO(version); 726 727 static struct attribute *attributes[] = { 728 &version_attr.attr, 729 NULL, 730 }; 731 732 static struct attribute_group attr_group = { 733 .attrs = attributes, 734 }; 735 736 static int do_sysfs_registration(void) 737 { 738 int rc; 739 740 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj); 741 if (!ecryptfs_kobj) { 742 printk(KERN_ERR "Unable to create ecryptfs kset\n"); 743 rc = -ENOMEM; 744 goto out; 745 } 746 rc = sysfs_create_group(ecryptfs_kobj, &attr_group); 747 if (rc) { 748 printk(KERN_ERR 749 "Unable to create ecryptfs version attributes\n"); 750 kobject_put(ecryptfs_kobj); 751 } 752 out: 753 return rc; 754 } 755 756 static void do_sysfs_unregistration(void) 757 { 758 sysfs_remove_group(ecryptfs_kobj, &attr_group); 759 kobject_put(ecryptfs_kobj); 760 } 761 762 static int __init ecryptfs_init(void) 763 { 764 int rc; 765 766 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { 767 rc = -EINVAL; 768 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " 769 "larger than the host's page size, and so " 770 "eCryptfs cannot run on this system. The " 771 "default eCryptfs extent size is [%d] bytes; " 772 "the page size is [%d] bytes.\n", 773 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE); 774 goto out; 775 } 776 rc = ecryptfs_init_kmem_caches(); 777 if (rc) { 778 printk(KERN_ERR 779 "Failed to allocate one or more kmem_cache objects\n"); 780 goto out; 781 } 782 rc = register_filesystem(&ecryptfs_fs_type); 783 if (rc) { 784 printk(KERN_ERR "Failed to register filesystem\n"); 785 goto out_free_kmem_caches; 786 } 787 rc = do_sysfs_registration(); 788 if (rc) { 789 printk(KERN_ERR "sysfs registration failed\n"); 790 goto out_unregister_filesystem; 791 } 792 rc = ecryptfs_init_kthread(); 793 if (rc) { 794 printk(KERN_ERR "%s: kthread initialization failed; " 795 "rc = [%d]\n", __func__, rc); 796 goto out_do_sysfs_unregistration; 797 } 798 rc = ecryptfs_init_messaging(ecryptfs_transport); 799 if (rc) { 800 printk(KERN_ERR "Failure occured while attempting to " 801 "initialize the eCryptfs netlink socket\n"); 802 goto out_destroy_kthread; 803 } 804 rc = ecryptfs_init_crypto(); 805 if (rc) { 806 printk(KERN_ERR "Failure whilst attempting to init crypto; " 807 "rc = [%d]\n", rc); 808 goto out_release_messaging; 809 } 810 if (ecryptfs_verbosity > 0) 811 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values " 812 "will be written to the syslog!\n", ecryptfs_verbosity); 813 814 goto out; 815 out_release_messaging: 816 ecryptfs_release_messaging(ecryptfs_transport); 817 out_destroy_kthread: 818 ecryptfs_destroy_kthread(); 819 out_do_sysfs_unregistration: 820 do_sysfs_unregistration(); 821 out_unregister_filesystem: 822 unregister_filesystem(&ecryptfs_fs_type); 823 out_free_kmem_caches: 824 ecryptfs_free_kmem_caches(); 825 out: 826 return rc; 827 } 828 829 static void __exit ecryptfs_exit(void) 830 { 831 int rc; 832 833 rc = ecryptfs_destroy_crypto(); 834 if (rc) 835 printk(KERN_ERR "Failure whilst attempting to destroy crypto; " 836 "rc = [%d]\n", rc); 837 ecryptfs_release_messaging(ecryptfs_transport); 838 ecryptfs_destroy_kthread(); 839 do_sysfs_unregistration(); 840 unregister_filesystem(&ecryptfs_fs_type); 841 ecryptfs_free_kmem_caches(); 842 } 843 844 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>"); 845 MODULE_DESCRIPTION("eCryptfs"); 846 847 MODULE_LICENSE("GPL"); 848 849 module_init(ecryptfs_init) 850 module_exit(ecryptfs_exit) 851