Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * eCryptfs: Linux filesystem encryption layer
0004  *
0005  * Copyright (C) 1997-2003 Erez Zadok
0006  * Copyright (C) 2001-2003 Stony Brook University
0007  * Copyright (C) 2004-2007 International Business Machines Corp.
0008  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
0009  *              Michael C. Thompson <mcthomps@us.ibm.com>
0010  *              Tyler Hicks <code@tyhicks.com>
0011  */
0012 
0013 #include <linux/dcache.h>
0014 #include <linux/file.h>
0015 #include <linux/module.h>
0016 #include <linux/namei.h>
0017 #include <linux/skbuff.h>
0018 #include <linux/mount.h>
0019 #include <linux/pagemap.h>
0020 #include <linux/key.h>
0021 #include <linux/parser.h>
0022 #include <linux/fs_stack.h>
0023 #include <linux/slab.h>
0024 #include <linux/magic.h>
0025 #include "ecryptfs_kernel.h"
0026 
0027 /*
0028  * Module parameter that defines the ecryptfs_verbosity level.
0029  */
0030 int ecryptfs_verbosity = 0;
0031 
0032 module_param(ecryptfs_verbosity, int, 0);
0033 MODULE_PARM_DESC(ecryptfs_verbosity,
0034          "Initial verbosity level (0 or 1; defaults to "
0035          "0, which is Quiet)");
0036 
0037 /*
0038  * Module parameter that defines the number of message buffer elements
0039  */
0040 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
0041 
0042 module_param(ecryptfs_message_buf_len, uint, 0);
0043 MODULE_PARM_DESC(ecryptfs_message_buf_len,
0044          "Number of message buffer elements");
0045 
0046 /*
0047  * Module parameter that defines the maximum guaranteed amount of time to wait
0048  * for a response from ecryptfsd.  The actual sleep time will be, more than
0049  * likely, a small amount greater than this specified value, but only less if
0050  * the message successfully arrives.
0051  */
0052 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
0053 
0054 module_param(ecryptfs_message_wait_timeout, long, 0);
0055 MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
0056          "Maximum number of seconds that an operation will "
0057          "sleep while waiting for a message response from "
0058          "userspace");
0059 
0060 /*
0061  * Module parameter that is an estimate of the maximum number of users
0062  * that will be concurrently using eCryptfs. Set this to the right
0063  * value to balance performance and memory use.
0064  */
0065 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
0066 
0067 module_param(ecryptfs_number_of_users, uint, 0);
0068 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
0069          "concurrent users of eCryptfs");
0070 
0071 void __ecryptfs_printk(const char *fmt, ...)
0072 {
0073     va_list args;
0074     va_start(args, fmt);
0075     if (fmt[1] == '7') { /* KERN_DEBUG */
0076         if (ecryptfs_verbosity >= 1)
0077             vprintk(fmt, args);
0078     } else
0079         vprintk(fmt, args);
0080     va_end(args);
0081 }
0082 
0083 /*
0084  * ecryptfs_init_lower_file
0085  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
0086  *                   the lower dentry and the lower mount set
0087  *
0088  * eCryptfs only ever keeps a single open file for every lower
0089  * inode. All I/O operations to the lower inode occur through that
0090  * file. When the first eCryptfs dentry that interposes with the first
0091  * lower dentry for that inode is created, this function creates the
0092  * lower file struct and associates it with the eCryptfs
0093  * inode. When all eCryptfs files associated with the inode are released, the
0094  * file is closed.
0095  *
0096  * The lower file will be opened with read/write permissions, if
0097  * possible. Otherwise, it is opened read-only.
0098  *
0099  * This function does nothing if a lower file is already
0100  * associated with the eCryptfs inode.
0101  *
0102  * Returns zero on success; non-zero otherwise
0103  */
0104 static int ecryptfs_init_lower_file(struct dentry *dentry,
0105                     struct file **lower_file)
0106 {
0107     const struct cred *cred = current_cred();
0108     struct path *path = ecryptfs_dentry_to_lower_path(dentry);
0109     int rc;
0110 
0111     rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
0112                       cred);
0113     if (rc) {
0114         printk(KERN_ERR "Error opening lower file "
0115                "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
0116                "rc = [%d]\n", path->dentry, path->mnt, rc);
0117         (*lower_file) = NULL;
0118     }
0119     return rc;
0120 }
0121 
0122 int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
0123 {
0124     struct ecryptfs_inode_info *inode_info;
0125     int count, rc = 0;
0126 
0127     inode_info = ecryptfs_inode_to_private(inode);
0128     mutex_lock(&inode_info->lower_file_mutex);
0129     count = atomic_inc_return(&inode_info->lower_file_count);
0130     if (WARN_ON_ONCE(count < 1))
0131         rc = -EINVAL;
0132     else if (count == 1) {
0133         rc = ecryptfs_init_lower_file(dentry,
0134                           &inode_info->lower_file);
0135         if (rc)
0136             atomic_set(&inode_info->lower_file_count, 0);
0137     }
0138     mutex_unlock(&inode_info->lower_file_mutex);
0139     return rc;
0140 }
0141 
0142 void ecryptfs_put_lower_file(struct inode *inode)
0143 {
0144     struct ecryptfs_inode_info *inode_info;
0145 
0146     inode_info = ecryptfs_inode_to_private(inode);
0147     if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
0148                       &inode_info->lower_file_mutex)) {
0149         filemap_write_and_wait(inode->i_mapping);
0150         fput(inode_info->lower_file);
0151         inode_info->lower_file = NULL;
0152         mutex_unlock(&inode_info->lower_file_mutex);
0153     }
0154 }
0155 
0156 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
0157        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
0158        ecryptfs_opt_ecryptfs_key_bytes,
0159        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
0160        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
0161        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
0162        ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
0163        ecryptfs_opt_check_dev_ruid,
0164        ecryptfs_opt_err };
0165 
0166 static const match_table_t tokens = {
0167     {ecryptfs_opt_sig, "sig=%s"},
0168     {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
0169     {ecryptfs_opt_cipher, "cipher=%s"},
0170     {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
0171     {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
0172     {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
0173     {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
0174     {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
0175     {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
0176     {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
0177     {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
0178     {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
0179     {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
0180     {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
0181     {ecryptfs_opt_err, NULL}
0182 };
0183 
0184 static int ecryptfs_init_global_auth_toks(
0185     struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
0186 {
0187     struct ecryptfs_global_auth_tok *global_auth_tok;
0188     struct ecryptfs_auth_tok *auth_tok;
0189     int rc = 0;
0190 
0191     list_for_each_entry(global_auth_tok,
0192                 &mount_crypt_stat->global_auth_tok_list,
0193                 mount_crypt_stat_list) {
0194         rc = ecryptfs_keyring_auth_tok_for_sig(
0195             &global_auth_tok->global_auth_tok_key, &auth_tok,
0196             global_auth_tok->sig);
0197         if (rc) {
0198             printk(KERN_ERR "Could not find valid key in user "
0199                    "session keyring for sig specified in mount "
0200                    "option: [%s]\n", global_auth_tok->sig);
0201             global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
0202             goto out;
0203         } else {
0204             global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
0205             up_write(&(global_auth_tok->global_auth_tok_key)->sem);
0206         }
0207     }
0208 out:
0209     return rc;
0210 }
0211 
0212 static void ecryptfs_init_mount_crypt_stat(
0213     struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
0214 {
0215     memset((void *)mount_crypt_stat, 0,
0216            sizeof(struct ecryptfs_mount_crypt_stat));
0217     INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
0218     mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
0219     mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
0220 }
0221 
0222 /**
0223  * ecryptfs_parse_options
0224  * @sbi: The ecryptfs super block
0225  * @options: The options passed to the kernel
0226  * @check_ruid: set to 1 if device uid should be checked against the ruid
0227  *
0228  * Parse mount options:
0229  * debug=N     - ecryptfs_verbosity level for debug output
0230  * sig=XXX     - description(signature) of the key to use
0231  *
0232  * Returns the dentry object of the lower-level (lower/interposed)
0233  * directory; We want to mount our stackable file system on top of
0234  * that lower directory.
0235  *
0236  * The signature of the key to use must be the description of a key
0237  * already in the keyring. Mounting will fail if the key can not be
0238  * found.
0239  *
0240  * Returns zero on success; non-zero on error
0241  */
0242 static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
0243                   uid_t *check_ruid)
0244 {
0245     char *p;
0246     int rc = 0;
0247     int sig_set = 0;
0248     int cipher_name_set = 0;
0249     int fn_cipher_name_set = 0;
0250     int cipher_key_bytes;
0251     int cipher_key_bytes_set = 0;
0252     int fn_cipher_key_bytes;
0253     int fn_cipher_key_bytes_set = 0;
0254     struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
0255         &sbi->mount_crypt_stat;
0256     substring_t args[MAX_OPT_ARGS];
0257     int token;
0258     char *sig_src;
0259     char *cipher_name_dst;
0260     char *cipher_name_src;
0261     char *fn_cipher_name_dst;
0262     char *fn_cipher_name_src;
0263     char *fnek_dst;
0264     char *fnek_src;
0265     char *cipher_key_bytes_src;
0266     char *fn_cipher_key_bytes_src;
0267     u8 cipher_code;
0268 
0269     *check_ruid = 0;
0270 
0271     if (!options) {
0272         rc = -EINVAL;
0273         goto out;
0274     }
0275     ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
0276     while ((p = strsep(&options, ",")) != NULL) {
0277         if (!*p)
0278             continue;
0279         token = match_token(p, tokens, args);
0280         switch (token) {
0281         case ecryptfs_opt_sig:
0282         case ecryptfs_opt_ecryptfs_sig:
0283             sig_src = args[0].from;
0284             rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
0285                               sig_src, 0);
0286             if (rc) {
0287                 printk(KERN_ERR "Error attempting to register "
0288                        "global sig; rc = [%d]\n", rc);
0289                 goto out;
0290             }
0291             sig_set = 1;
0292             break;
0293         case ecryptfs_opt_cipher:
0294         case ecryptfs_opt_ecryptfs_cipher:
0295             cipher_name_src = args[0].from;
0296             cipher_name_dst =
0297                 mount_crypt_stat->
0298                 global_default_cipher_name;
0299             strncpy(cipher_name_dst, cipher_name_src,
0300                 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
0301             cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
0302             cipher_name_set = 1;
0303             break;
0304         case ecryptfs_opt_ecryptfs_key_bytes:
0305             cipher_key_bytes_src = args[0].from;
0306             cipher_key_bytes =
0307                 (int)simple_strtol(cipher_key_bytes_src,
0308                            &cipher_key_bytes_src, 0);
0309             mount_crypt_stat->global_default_cipher_key_size =
0310                 cipher_key_bytes;
0311             cipher_key_bytes_set = 1;
0312             break;
0313         case ecryptfs_opt_passthrough:
0314             mount_crypt_stat->flags |=
0315                 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
0316             break;
0317         case ecryptfs_opt_xattr_metadata:
0318             mount_crypt_stat->flags |=
0319                 ECRYPTFS_XATTR_METADATA_ENABLED;
0320             break;
0321         case ecryptfs_opt_encrypted_view:
0322             mount_crypt_stat->flags |=
0323                 ECRYPTFS_XATTR_METADATA_ENABLED;
0324             mount_crypt_stat->flags |=
0325                 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
0326             break;
0327         case ecryptfs_opt_fnek_sig:
0328             fnek_src = args[0].from;
0329             fnek_dst =
0330                 mount_crypt_stat->global_default_fnek_sig;
0331             strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
0332             mount_crypt_stat->global_default_fnek_sig[
0333                 ECRYPTFS_SIG_SIZE_HEX] = '\0';
0334             rc = ecryptfs_add_global_auth_tok(
0335                 mount_crypt_stat,
0336                 mount_crypt_stat->global_default_fnek_sig,
0337                 ECRYPTFS_AUTH_TOK_FNEK);
0338             if (rc) {
0339                 printk(KERN_ERR "Error attempting to register "
0340                        "global fnek sig [%s]; rc = [%d]\n",
0341                        mount_crypt_stat->global_default_fnek_sig,
0342                        rc);
0343                 goto out;
0344             }
0345             mount_crypt_stat->flags |=
0346                 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
0347                  | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
0348             break;
0349         case ecryptfs_opt_fn_cipher:
0350             fn_cipher_name_src = args[0].from;
0351             fn_cipher_name_dst =
0352                 mount_crypt_stat->global_default_fn_cipher_name;
0353             strncpy(fn_cipher_name_dst, fn_cipher_name_src,
0354                 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
0355             mount_crypt_stat->global_default_fn_cipher_name[
0356                 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
0357             fn_cipher_name_set = 1;
0358             break;
0359         case ecryptfs_opt_fn_cipher_key_bytes:
0360             fn_cipher_key_bytes_src = args[0].from;
0361             fn_cipher_key_bytes =
0362                 (int)simple_strtol(fn_cipher_key_bytes_src,
0363                            &fn_cipher_key_bytes_src, 0);
0364             mount_crypt_stat->global_default_fn_cipher_key_bytes =
0365                 fn_cipher_key_bytes;
0366             fn_cipher_key_bytes_set = 1;
0367             break;
0368         case ecryptfs_opt_unlink_sigs:
0369             mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
0370             break;
0371         case ecryptfs_opt_mount_auth_tok_only:
0372             mount_crypt_stat->flags |=
0373                 ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
0374             break;
0375         case ecryptfs_opt_check_dev_ruid:
0376             *check_ruid = 1;
0377             break;
0378         case ecryptfs_opt_err:
0379         default:
0380             printk(KERN_WARNING
0381                    "%s: eCryptfs: unrecognized option [%s]\n",
0382                    __func__, p);
0383         }
0384     }
0385     if (!sig_set) {
0386         rc = -EINVAL;
0387         ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
0388                 "auth tok signature as a mount "
0389                 "parameter; see the eCryptfs README\n");
0390         goto out;
0391     }
0392     if (!cipher_name_set) {
0393         int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
0394 
0395         BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
0396         strcpy(mount_crypt_stat->global_default_cipher_name,
0397                ECRYPTFS_DEFAULT_CIPHER);
0398     }
0399     if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
0400         && !fn_cipher_name_set)
0401         strcpy(mount_crypt_stat->global_default_fn_cipher_name,
0402                mount_crypt_stat->global_default_cipher_name);
0403     if (!cipher_key_bytes_set)
0404         mount_crypt_stat->global_default_cipher_key_size = 0;
0405     if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
0406         && !fn_cipher_key_bytes_set)
0407         mount_crypt_stat->global_default_fn_cipher_key_bytes =
0408             mount_crypt_stat->global_default_cipher_key_size;
0409 
0410     cipher_code = ecryptfs_code_for_cipher_string(
0411         mount_crypt_stat->global_default_cipher_name,
0412         mount_crypt_stat->global_default_cipher_key_size);
0413     if (!cipher_code) {
0414         ecryptfs_printk(KERN_ERR,
0415                 "eCryptfs doesn't support cipher: %s\n",
0416                 mount_crypt_stat->global_default_cipher_name);
0417         rc = -EINVAL;
0418         goto out;
0419     }
0420 
0421     mutex_lock(&key_tfm_list_mutex);
0422     if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
0423                  NULL)) {
0424         rc = ecryptfs_add_new_key_tfm(
0425             NULL, mount_crypt_stat->global_default_cipher_name,
0426             mount_crypt_stat->global_default_cipher_key_size);
0427         if (rc) {
0428             printk(KERN_ERR "Error attempting to initialize "
0429                    "cipher with name = [%s] and key size = [%td]; "
0430                    "rc = [%d]\n",
0431                    mount_crypt_stat->global_default_cipher_name,
0432                    mount_crypt_stat->global_default_cipher_key_size,
0433                    rc);
0434             rc = -EINVAL;
0435             mutex_unlock(&key_tfm_list_mutex);
0436             goto out;
0437         }
0438     }
0439     if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
0440         && !ecryptfs_tfm_exists(
0441             mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
0442         rc = ecryptfs_add_new_key_tfm(
0443             NULL, mount_crypt_stat->global_default_fn_cipher_name,
0444             mount_crypt_stat->global_default_fn_cipher_key_bytes);
0445         if (rc) {
0446             printk(KERN_ERR "Error attempting to initialize "
0447                    "cipher with name = [%s] and key size = [%td]; "
0448                    "rc = [%d]\n",
0449                    mount_crypt_stat->global_default_fn_cipher_name,
0450                    mount_crypt_stat->global_default_fn_cipher_key_bytes,
0451                    rc);
0452             rc = -EINVAL;
0453             mutex_unlock(&key_tfm_list_mutex);
0454             goto out;
0455         }
0456     }
0457     mutex_unlock(&key_tfm_list_mutex);
0458     rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
0459     if (rc)
0460         printk(KERN_WARNING "One or more global auth toks could not "
0461                "properly register; rc = [%d]\n", rc);
0462 out:
0463     return rc;
0464 }
0465 
0466 struct kmem_cache *ecryptfs_sb_info_cache;
0467 static struct file_system_type ecryptfs_fs_type;
0468 
0469 /*
0470  * ecryptfs_mount
0471  * @fs_type: The filesystem type that the superblock should belong to
0472  * @flags: The flags associated with the mount
0473  * @dev_name: The path to mount over
0474  * @raw_data: The options passed into the kernel
0475  */
0476 static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
0477             const char *dev_name, void *raw_data)
0478 {
0479     struct super_block *s;
0480     struct ecryptfs_sb_info *sbi;
0481     struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
0482     struct ecryptfs_dentry_info *root_info;
0483     const char *err = "Getting sb failed";
0484     struct inode *inode;
0485     struct path path;
0486     uid_t check_ruid;
0487     int rc;
0488 
0489     sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
0490     if (!sbi) {
0491         rc = -ENOMEM;
0492         goto out;
0493     }
0494 
0495     if (!dev_name) {
0496         rc = -EINVAL;
0497         err = "Device name cannot be null";
0498         goto out;
0499     }
0500 
0501     rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
0502     if (rc) {
0503         err = "Error parsing options";
0504         goto out;
0505     }
0506     mount_crypt_stat = &sbi->mount_crypt_stat;
0507 
0508     s = sget(fs_type, NULL, set_anon_super, flags, NULL);
0509     if (IS_ERR(s)) {
0510         rc = PTR_ERR(s);
0511         goto out;
0512     }
0513 
0514     rc = super_setup_bdi(s);
0515     if (rc)
0516         goto out1;
0517 
0518     ecryptfs_set_superblock_private(s, sbi);
0519 
0520     /* ->kill_sb() will take care of sbi after that point */
0521     sbi = NULL;
0522     s->s_op = &ecryptfs_sops;
0523     s->s_xattr = ecryptfs_xattr_handlers;
0524     s->s_d_op = &ecryptfs_dops;
0525 
0526     err = "Reading sb failed";
0527     rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
0528     if (rc) {
0529         ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
0530         goto out1;
0531     }
0532     if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
0533         rc = -EINVAL;
0534         printk(KERN_ERR "Mount on filesystem of type "
0535             "eCryptfs explicitly disallowed due to "
0536             "known incompatibilities\n");
0537         goto out_free;
0538     }
0539 
0540     if (is_idmapped_mnt(path.mnt)) {
0541         rc = -EINVAL;
0542         printk(KERN_ERR "Mounting on idmapped mounts currently disallowed\n");
0543         goto out_free;
0544     }
0545 
0546     if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
0547         rc = -EPERM;
0548         printk(KERN_ERR "Mount of device (uid: %d) not owned by "
0549                "requested user (uid: %d)\n",
0550             i_uid_read(d_inode(path.dentry)),
0551             from_kuid(&init_user_ns, current_uid()));
0552         goto out_free;
0553     }
0554 
0555     ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
0556 
0557     /**
0558      * Set the POSIX ACL flag based on whether they're enabled in the lower
0559      * mount.
0560      */
0561     s->s_flags = flags & ~SB_POSIXACL;
0562     s->s_flags |= path.dentry->d_sb->s_flags & SB_POSIXACL;
0563 
0564     /**
0565      * Force a read-only eCryptfs mount when:
0566      *   1) The lower mount is ro
0567      *   2) The ecryptfs_encrypted_view mount option is specified
0568      */
0569     if (sb_rdonly(path.dentry->d_sb) || mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
0570         s->s_flags |= SB_RDONLY;
0571 
0572     s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
0573     s->s_blocksize = path.dentry->d_sb->s_blocksize;
0574     s->s_magic = ECRYPTFS_SUPER_MAGIC;
0575     s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
0576 
0577     rc = -EINVAL;
0578     if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
0579         pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
0580         goto out_free;
0581     }
0582 
0583     inode = ecryptfs_get_inode(d_inode(path.dentry), s);
0584     rc = PTR_ERR(inode);
0585     if (IS_ERR(inode))
0586         goto out_free;
0587 
0588     s->s_root = d_make_root(inode);
0589     if (!s->s_root) {
0590         rc = -ENOMEM;
0591         goto out_free;
0592     }
0593 
0594     rc = -ENOMEM;
0595     root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
0596     if (!root_info)
0597         goto out_free;
0598 
0599     /* ->kill_sb() will take care of root_info */
0600     ecryptfs_set_dentry_private(s->s_root, root_info);
0601     root_info->lower_path = path;
0602 
0603     s->s_flags |= SB_ACTIVE;
0604     return dget(s->s_root);
0605 
0606 out_free:
0607     path_put(&path);
0608 out1:
0609     deactivate_locked_super(s);
0610 out:
0611     if (sbi) {
0612         ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
0613         kmem_cache_free(ecryptfs_sb_info_cache, sbi);
0614     }
0615     printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
0616     return ERR_PTR(rc);
0617 }
0618 
0619 /**
0620  * ecryptfs_kill_block_super
0621  * @sb: The ecryptfs super block
0622  *
0623  * Used to bring the superblock down and free the private data.
0624  */
0625 static void ecryptfs_kill_block_super(struct super_block *sb)
0626 {
0627     struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
0628     kill_anon_super(sb);
0629     if (!sb_info)
0630         return;
0631     ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
0632     kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
0633 }
0634 
0635 static struct file_system_type ecryptfs_fs_type = {
0636     .owner = THIS_MODULE,
0637     .name = "ecryptfs",
0638     .mount = ecryptfs_mount,
0639     .kill_sb = ecryptfs_kill_block_super,
0640     .fs_flags = 0
0641 };
0642 MODULE_ALIAS_FS("ecryptfs");
0643 
0644 /*
0645  * inode_info_init_once
0646  *
0647  * Initializes the ecryptfs_inode_info_cache when it is created
0648  */
0649 static void
0650 inode_info_init_once(void *vptr)
0651 {
0652     struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
0653 
0654     inode_init_once(&ei->vfs_inode);
0655 }
0656 
0657 static struct ecryptfs_cache_info {
0658     struct kmem_cache **cache;
0659     const char *name;
0660     size_t size;
0661     slab_flags_t flags;
0662     void (*ctor)(void *obj);
0663 } ecryptfs_cache_infos[] = {
0664     {
0665         .cache = &ecryptfs_auth_tok_list_item_cache,
0666         .name = "ecryptfs_auth_tok_list_item",
0667         .size = sizeof(struct ecryptfs_auth_tok_list_item),
0668     },
0669     {
0670         .cache = &ecryptfs_file_info_cache,
0671         .name = "ecryptfs_file_cache",
0672         .size = sizeof(struct ecryptfs_file_info),
0673     },
0674     {
0675         .cache = &ecryptfs_dentry_info_cache,
0676         .name = "ecryptfs_dentry_info_cache",
0677         .size = sizeof(struct ecryptfs_dentry_info),
0678     },
0679     {
0680         .cache = &ecryptfs_inode_info_cache,
0681         .name = "ecryptfs_inode_cache",
0682         .size = sizeof(struct ecryptfs_inode_info),
0683         .flags = SLAB_ACCOUNT,
0684         .ctor = inode_info_init_once,
0685     },
0686     {
0687         .cache = &ecryptfs_sb_info_cache,
0688         .name = "ecryptfs_sb_cache",
0689         .size = sizeof(struct ecryptfs_sb_info),
0690     },
0691     {
0692         .cache = &ecryptfs_header_cache,
0693         .name = "ecryptfs_headers",
0694         .size = PAGE_SIZE,
0695     },
0696     {
0697         .cache = &ecryptfs_xattr_cache,
0698         .name = "ecryptfs_xattr_cache",
0699         .size = PAGE_SIZE,
0700     },
0701     {
0702         .cache = &ecryptfs_key_record_cache,
0703         .name = "ecryptfs_key_record_cache",
0704         .size = sizeof(struct ecryptfs_key_record),
0705     },
0706     {
0707         .cache = &ecryptfs_key_sig_cache,
0708         .name = "ecryptfs_key_sig_cache",
0709         .size = sizeof(struct ecryptfs_key_sig),
0710     },
0711     {
0712         .cache = &ecryptfs_global_auth_tok_cache,
0713         .name = "ecryptfs_global_auth_tok_cache",
0714         .size = sizeof(struct ecryptfs_global_auth_tok),
0715     },
0716     {
0717         .cache = &ecryptfs_key_tfm_cache,
0718         .name = "ecryptfs_key_tfm_cache",
0719         .size = sizeof(struct ecryptfs_key_tfm),
0720     },
0721 };
0722 
0723 static void ecryptfs_free_kmem_caches(void)
0724 {
0725     int i;
0726 
0727     /*
0728      * Make sure all delayed rcu free inodes are flushed before we
0729      * destroy cache.
0730      */
0731     rcu_barrier();
0732 
0733     for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
0734         struct ecryptfs_cache_info *info;
0735 
0736         info = &ecryptfs_cache_infos[i];
0737         kmem_cache_destroy(*(info->cache));
0738     }
0739 }
0740 
0741 /**
0742  * ecryptfs_init_kmem_caches
0743  *
0744  * Returns zero on success; non-zero otherwise
0745  */
0746 static int ecryptfs_init_kmem_caches(void)
0747 {
0748     int i;
0749 
0750     for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
0751         struct ecryptfs_cache_info *info;
0752 
0753         info = &ecryptfs_cache_infos[i];
0754         *(info->cache) = kmem_cache_create(info->name, info->size, 0,
0755                 SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
0756         if (!*(info->cache)) {
0757             ecryptfs_free_kmem_caches();
0758             ecryptfs_printk(KERN_WARNING, "%s: "
0759                     "kmem_cache_create failed\n",
0760                     info->name);
0761             return -ENOMEM;
0762         }
0763     }
0764     return 0;
0765 }
0766 
0767 static struct kobject *ecryptfs_kobj;
0768 
0769 static ssize_t version_show(struct kobject *kobj,
0770                 struct kobj_attribute *attr, char *buff)
0771 {
0772     return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
0773 }
0774 
0775 static struct kobj_attribute version_attr = __ATTR_RO(version);
0776 
0777 static struct attribute *attributes[] = {
0778     &version_attr.attr,
0779     NULL,
0780 };
0781 
0782 static const struct attribute_group attr_group = {
0783     .attrs = attributes,
0784 };
0785 
0786 static int do_sysfs_registration(void)
0787 {
0788     int rc;
0789 
0790     ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
0791     if (!ecryptfs_kobj) {
0792         printk(KERN_ERR "Unable to create ecryptfs kset\n");
0793         rc = -ENOMEM;
0794         goto out;
0795     }
0796     rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
0797     if (rc) {
0798         printk(KERN_ERR
0799                "Unable to create ecryptfs version attributes\n");
0800         kobject_put(ecryptfs_kobj);
0801     }
0802 out:
0803     return rc;
0804 }
0805 
0806 static void do_sysfs_unregistration(void)
0807 {
0808     sysfs_remove_group(ecryptfs_kobj, &attr_group);
0809     kobject_put(ecryptfs_kobj);
0810 }
0811 
0812 static int __init ecryptfs_init(void)
0813 {
0814     int rc;
0815 
0816     if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
0817         rc = -EINVAL;
0818         ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
0819                 "larger than the host's page size, and so "
0820                 "eCryptfs cannot run on this system. The "
0821                 "default eCryptfs extent size is [%u] bytes; "
0822                 "the page size is [%lu] bytes.\n",
0823                 ECRYPTFS_DEFAULT_EXTENT_SIZE,
0824                 (unsigned long)PAGE_SIZE);
0825         goto out;
0826     }
0827     rc = ecryptfs_init_kmem_caches();
0828     if (rc) {
0829         printk(KERN_ERR
0830                "Failed to allocate one or more kmem_cache objects\n");
0831         goto out;
0832     }
0833     rc = do_sysfs_registration();
0834     if (rc) {
0835         printk(KERN_ERR "sysfs registration failed\n");
0836         goto out_free_kmem_caches;
0837     }
0838     rc = ecryptfs_init_kthread();
0839     if (rc) {
0840         printk(KERN_ERR "%s: kthread initialization failed; "
0841                "rc = [%d]\n", __func__, rc);
0842         goto out_do_sysfs_unregistration;
0843     }
0844     rc = ecryptfs_init_messaging();
0845     if (rc) {
0846         printk(KERN_ERR "Failure occurred while attempting to "
0847                 "initialize the communications channel to "
0848                 "ecryptfsd\n");
0849         goto out_destroy_kthread;
0850     }
0851     rc = ecryptfs_init_crypto();
0852     if (rc) {
0853         printk(KERN_ERR "Failure whilst attempting to init crypto; "
0854                "rc = [%d]\n", rc);
0855         goto out_release_messaging;
0856     }
0857     rc = register_filesystem(&ecryptfs_fs_type);
0858     if (rc) {
0859         printk(KERN_ERR "Failed to register filesystem\n");
0860         goto out_destroy_crypto;
0861     }
0862     if (ecryptfs_verbosity > 0)
0863         printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
0864             "will be written to the syslog!\n", ecryptfs_verbosity);
0865 
0866     goto out;
0867 out_destroy_crypto:
0868     ecryptfs_destroy_crypto();
0869 out_release_messaging:
0870     ecryptfs_release_messaging();
0871 out_destroy_kthread:
0872     ecryptfs_destroy_kthread();
0873 out_do_sysfs_unregistration:
0874     do_sysfs_unregistration();
0875 out_free_kmem_caches:
0876     ecryptfs_free_kmem_caches();
0877 out:
0878     return rc;
0879 }
0880 
0881 static void __exit ecryptfs_exit(void)
0882 {
0883     int rc;
0884 
0885     rc = ecryptfs_destroy_crypto();
0886     if (rc)
0887         printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
0888                "rc = [%d]\n", rc);
0889     ecryptfs_release_messaging();
0890     ecryptfs_destroy_kthread();
0891     do_sysfs_unregistration();
0892     unregister_filesystem(&ecryptfs_fs_type);
0893     ecryptfs_free_kmem_caches();
0894 }
0895 
0896 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
0897 MODULE_DESCRIPTION("eCryptfs");
0898 
0899 MODULE_LICENSE("GPL");
0900 
0901 module_init(ecryptfs_init)
0902 module_exit(ecryptfs_exit)