Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1 | /** |
| 2 | * eCryptfs: Linux filesystem encryption layer |
| 3 | * |
| 4 | * Copyright (C) 1997-2003 Erez Zadok |
| 5 | * Copyright (C) 2001-2003 Stony Brook University |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 7 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
| 8 | * Michael C. Thompson <mcthomps@us.ibm.com> |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 9 | * Tyler Hicks <tyhicks@ou.edu> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 33 | #include <linux/mount.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 34 | #include <linux/pagemap.h> |
| 35 | #include <linux/key.h> |
| 36 | #include <linux/parser.h> |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 37 | #include <linux/fs_stack.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 | |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 51 | /** |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 52 | * Module parameter that defines the number of message buffer elements |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 53 | */ |
| 54 | unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS; |
| 55 | |
| 56 | module_param(ecryptfs_message_buf_len, uint, 0); |
| 57 | MODULE_PARM_DESC(ecryptfs_message_buf_len, |
| 58 | "Number of message buffer elements"); |
| 59 | |
| 60 | /** |
| 61 | * Module parameter that defines the maximum guaranteed amount of time to wait |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 62 | * for a response from ecryptfsd. The actual sleep time will be, more than |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 63 | * likely, a small amount greater than this specified value, but only less if |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 64 | * the message successfully arrives. |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 65 | */ |
| 66 | signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ; |
| 67 | |
| 68 | module_param(ecryptfs_message_wait_timeout, long, 0); |
| 69 | MODULE_PARM_DESC(ecryptfs_message_wait_timeout, |
| 70 | "Maximum number of seconds that an operation will " |
| 71 | "sleep while waiting for a message response from " |
| 72 | "userspace"); |
| 73 | |
| 74 | /** |
| 75 | * Module parameter that is an estimate of the maximum number of users |
| 76 | * that will be concurrently using eCryptfs. Set this to the right |
| 77 | * value to balance performance and memory use. |
| 78 | */ |
| 79 | unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS; |
| 80 | |
| 81 | module_param(ecryptfs_number_of_users, uint, 0); |
| 82 | MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of " |
| 83 | "concurrent users of eCryptfs"); |
| 84 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 85 | void __ecryptfs_printk(const char *fmt, ...) |
| 86 | { |
| 87 | va_list args; |
| 88 | va_start(args, fmt); |
| 89 | if (fmt[1] == '7') { /* KERN_DEBUG */ |
| 90 | if (ecryptfs_verbosity >= 1) |
| 91 | vprintk(fmt, args); |
| 92 | } else |
| 93 | vprintk(fmt, args); |
| 94 | va_end(args); |
| 95 | } |
| 96 | |
| 97 | /** |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 98 | * ecryptfs_init_persistent_file |
| 99 | * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with |
| 100 | * the lower dentry and the lower mount set |
| 101 | * |
| 102 | * eCryptfs only ever keeps a single open file for every lower |
| 103 | * inode. All I/O operations to the lower inode occur through that |
| 104 | * file. When the first eCryptfs dentry that interposes with the first |
| 105 | * lower dentry for that inode is created, this function creates the |
| 106 | * persistent file struct and associates it with the eCryptfs |
| 107 | * inode. When the eCryptfs inode is destroyed, the file is closed. |
| 108 | * |
| 109 | * The persistent file will be opened with read/write permissions, if |
| 110 | * possible. Otherwise, it is opened read-only. |
| 111 | * |
| 112 | * This function does nothing if a lower persistent file is already |
| 113 | * associated with the eCryptfs inode. |
| 114 | * |
| 115 | * Returns zero on success; non-zero otherwise |
| 116 | */ |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 117 | int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 118 | { |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 119 | const struct cred *cred = current_cred(); |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 120 | struct ecryptfs_inode_info *inode_info = |
| 121 | ecryptfs_inode_to_private(ecryptfs_dentry->d_inode); |
| 122 | int rc = 0; |
| 123 | |
| 124 | mutex_lock(&inode_info->lower_file_mutex); |
| 125 | if (!inode_info->lower_file) { |
| 126 | struct dentry *lower_dentry; |
| 127 | struct vfsmount *lower_mnt = |
| 128 | ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry); |
| 129 | |
| 130 | lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 131 | rc = ecryptfs_privileged_open(&inode_info->lower_file, |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 132 | lower_dentry, lower_mnt, cred); |
Tyler Hicks | ac22ba2 | 2009-08-12 01:06:54 -0500 | [diff] [blame] | 133 | if (rc) { |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 134 | printk(KERN_ERR "Error opening lower persistent file " |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 135 | "for lower_dentry [0x%p] and lower_mnt [0x%p]; " |
| 136 | "rc = [%d]\n", lower_dentry, lower_mnt, rc); |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 137 | inode_info->lower_file = NULL; |
Al Viro | b65a9cf | 2009-12-16 06:27:40 -0500 | [diff] [blame] | 138 | } |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 139 | } |
| 140 | mutex_unlock(&inode_info->lower_file_mutex); |
| 141 | return rc; |
| 142 | } |
| 143 | |
| 144 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 145 | * ecryptfs_interpose |
| 146 | * @lower_dentry: Existing dentry in the lower filesystem |
| 147 | * @dentry: ecryptfs' dentry |
| 148 | * @sb: ecryptfs's super_block |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 149 | * @flags: flags to govern behavior of interpose procedure |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 150 | * |
| 151 | * Interposes upper and lower dentries. |
| 152 | * |
| 153 | * Returns zero on success; non-zero otherwise |
| 154 | */ |
| 155 | int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry, |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 156 | struct super_block *sb, u32 flags) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 157 | { |
| 158 | struct inode *lower_inode; |
| 159 | struct inode *inode; |
| 160 | int rc = 0; |
| 161 | |
| 162 | lower_inode = lower_dentry->d_inode; |
| 163 | if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) { |
| 164 | rc = -EXDEV; |
| 165 | goto out; |
| 166 | } |
| 167 | if (!igrab(lower_inode)) { |
| 168 | rc = -ESTALE; |
| 169 | goto out; |
| 170 | } |
| 171 | inode = iget5_locked(sb, (unsigned long)lower_inode, |
| 172 | ecryptfs_inode_test, ecryptfs_inode_set, |
| 173 | lower_inode); |
| 174 | if (!inode) { |
| 175 | rc = -EACCES; |
| 176 | iput(lower_inode); |
| 177 | goto out; |
| 178 | } |
| 179 | if (inode->i_state & I_NEW) |
| 180 | unlock_new_inode(inode); |
| 181 | else |
| 182 | iput(lower_inode); |
| 183 | if (S_ISLNK(lower_inode->i_mode)) |
| 184 | inode->i_op = &ecryptfs_symlink_iops; |
| 185 | else if (S_ISDIR(lower_inode->i_mode)) |
| 186 | inode->i_op = &ecryptfs_dir_iops; |
| 187 | if (S_ISDIR(lower_inode->i_mode)) |
| 188 | inode->i_fop = &ecryptfs_dir_fops; |
Pekka Enberg | 26da820 | 2006-10-19 23:28:14 -0700 | [diff] [blame] | 189 | if (special_file(lower_inode->i_mode)) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 190 | init_special_inode(inode, lower_inode->i_mode, |
| 191 | lower_inode->i_rdev); |
| 192 | dentry->d_op = &ecryptfs_dops; |
Erez Zadok | 9afa2fb | 2009-12-02 19:51:54 -0500 | [diff] [blame] | 193 | fsstack_copy_attr_all(inode, lower_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 194 | /* This size will be overwritten for real files w/ headers and |
| 195 | * other metadata */ |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 196 | fsstack_copy_inode_size(inode, lower_inode); |
Tyler Hicks | ae6e845 | 2009-03-12 00:19:46 -0500 | [diff] [blame] | 197 | if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD) |
| 198 | d_add(dentry, inode); |
| 199 | else |
| 200 | d_instantiate(dentry, inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 201 | out: |
| 202 | return rc; |
| 203 | } |
| 204 | |
Eric Sandeen | 2830bfd | 2008-02-06 01:38:34 -0800 | [diff] [blame] | 205 | enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, |
| 206 | ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher, |
| 207 | ecryptfs_opt_ecryptfs_key_bytes, |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 208 | ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata, |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 209 | ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig, |
| 210 | ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes, |
Tyler Hicks | e77cc8d | 2009-04-22 04:08:46 -0500 | [diff] [blame] | 211 | ecryptfs_opt_unlink_sigs, ecryptfs_opt_err }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 212 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 213 | static const match_table_t tokens = { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 214 | {ecryptfs_opt_sig, "sig=%s"}, |
| 215 | {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"}, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 216 | {ecryptfs_opt_cipher, "cipher=%s"}, |
| 217 | {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"}, |
| 218 | {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"}, |
| 219 | {ecryptfs_opt_passthrough, "ecryptfs_passthrough"}, |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 220 | {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"}, |
| 221 | {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"}, |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 222 | {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"}, |
| 223 | {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"}, |
| 224 | {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"}, |
Tyler Hicks | e77cc8d | 2009-04-22 04:08:46 -0500 | [diff] [blame] | 225 | {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"}, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 226 | {ecryptfs_opt_err, NULL} |
| 227 | }; |
| 228 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 229 | static int ecryptfs_init_global_auth_toks( |
| 230 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 231 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 232 | struct ecryptfs_global_auth_tok *global_auth_tok; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 233 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 234 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 235 | list_for_each_entry(global_auth_tok, |
| 236 | &mount_crypt_stat->global_auth_tok_list, |
| 237 | mount_crypt_stat_list) { |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 238 | rc = ecryptfs_keyring_auth_tok_for_sig( |
| 239 | &global_auth_tok->global_auth_tok_key, |
| 240 | &global_auth_tok->global_auth_tok, |
| 241 | global_auth_tok->sig); |
| 242 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 243 | printk(KERN_ERR "Could not find valid key in user " |
| 244 | "session keyring for sig specified in mount " |
| 245 | "option: [%s]\n", global_auth_tok->sig); |
| 246 | global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; |
Eric Sandeen | 982363c | 2008-07-23 21:30:04 -0700 | [diff] [blame] | 247 | goto out; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 248 | } else |
| 249 | global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 250 | } |
Eric Sandeen | 982363c | 2008-07-23 21:30:04 -0700 | [diff] [blame] | 251 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 252 | return rc; |
| 253 | } |
| 254 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 255 | static void ecryptfs_init_mount_crypt_stat( |
| 256 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 257 | { |
| 258 | memset((void *)mount_crypt_stat, 0, |
| 259 | sizeof(struct ecryptfs_mount_crypt_stat)); |
| 260 | INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list); |
| 261 | mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 262 | mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED; |
| 263 | } |
| 264 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 265 | /** |
| 266 | * ecryptfs_parse_options |
| 267 | * @sb: The ecryptfs super block |
| 268 | * @options: The options pased to the kernel |
| 269 | * |
| 270 | * Parse mount options: |
| 271 | * debug=N - ecryptfs_verbosity level for debug output |
| 272 | * sig=XXX - description(signature) of the key to use |
| 273 | * |
| 274 | * Returns the dentry object of the lower-level (lower/interposed) |
| 275 | * directory; We want to mount our stackable file system on top of |
| 276 | * that lower directory. |
| 277 | * |
| 278 | * The signature of the key to use must be the description of a key |
| 279 | * already in the keyring. Mounting will fail if the key can not be |
| 280 | * found. |
| 281 | * |
| 282 | * Returns zero on success; non-zero on error |
| 283 | */ |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 284 | static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 285 | { |
| 286 | char *p; |
| 287 | int rc = 0; |
| 288 | int sig_set = 0; |
| 289 | int cipher_name_set = 0; |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 290 | int fn_cipher_name_set = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 291 | int cipher_key_bytes; |
| 292 | int cipher_key_bytes_set = 0; |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 293 | int fn_cipher_key_bytes; |
| 294 | int fn_cipher_key_bytes_set = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 295 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 296 | &sbi->mount_crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 297 | substring_t args[MAX_OPT_ARGS]; |
| 298 | int token; |
| 299 | char *sig_src; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 300 | char *cipher_name_dst; |
| 301 | char *cipher_name_src; |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 302 | char *fn_cipher_name_dst; |
| 303 | char *fn_cipher_name_src; |
| 304 | char *fnek_dst; |
| 305 | char *fnek_src; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 306 | char *cipher_key_bytes_src; |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 307 | char *fn_cipher_key_bytes_src; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 308 | |
| 309 | if (!options) { |
| 310 | rc = -EINVAL; |
| 311 | goto out; |
| 312 | } |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 313 | ecryptfs_init_mount_crypt_stat(mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 314 | while ((p = strsep(&options, ",")) != NULL) { |
| 315 | if (!*p) |
| 316 | continue; |
| 317 | token = match_token(p, tokens, args); |
| 318 | switch (token) { |
| 319 | case ecryptfs_opt_sig: |
| 320 | case ecryptfs_opt_ecryptfs_sig: |
| 321 | sig_src = args[0].from; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 322 | rc = ecryptfs_add_global_auth_tok(mount_crypt_stat, |
Tyler Hicks | 84814d6 | 2009-03-13 13:51:59 -0700 | [diff] [blame] | 323 | sig_src, 0); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 324 | if (rc) { |
| 325 | printk(KERN_ERR "Error attempting to register " |
| 326 | "global sig; rc = [%d]\n", rc); |
| 327 | goto out; |
| 328 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 329 | sig_set = 1; |
| 330 | break; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 331 | case ecryptfs_opt_cipher: |
| 332 | case ecryptfs_opt_ecryptfs_cipher: |
| 333 | cipher_name_src = args[0].from; |
| 334 | cipher_name_dst = |
| 335 | mount_crypt_stat-> |
| 336 | global_default_cipher_name; |
| 337 | strncpy(cipher_name_dst, cipher_name_src, |
| 338 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 339 | cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 340 | cipher_name_set = 1; |
| 341 | break; |
| 342 | case ecryptfs_opt_ecryptfs_key_bytes: |
| 343 | cipher_key_bytes_src = args[0].from; |
| 344 | cipher_key_bytes = |
| 345 | (int)simple_strtol(cipher_key_bytes_src, |
| 346 | &cipher_key_bytes_src, 0); |
| 347 | mount_crypt_stat->global_default_cipher_key_size = |
| 348 | cipher_key_bytes; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 349 | cipher_key_bytes_set = 1; |
| 350 | break; |
| 351 | case ecryptfs_opt_passthrough: |
| 352 | mount_crypt_stat->flags |= |
| 353 | ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED; |
| 354 | break; |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 355 | case ecryptfs_opt_xattr_metadata: |
| 356 | mount_crypt_stat->flags |= |
| 357 | ECRYPTFS_XATTR_METADATA_ENABLED; |
| 358 | break; |
| 359 | case ecryptfs_opt_encrypted_view: |
| 360 | mount_crypt_stat->flags |= |
| 361 | ECRYPTFS_XATTR_METADATA_ENABLED; |
| 362 | mount_crypt_stat->flags |= |
| 363 | ECRYPTFS_ENCRYPTED_VIEW_ENABLED; |
| 364 | break; |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 365 | case ecryptfs_opt_fnek_sig: |
| 366 | fnek_src = args[0].from; |
| 367 | fnek_dst = |
| 368 | mount_crypt_stat->global_default_fnek_sig; |
| 369 | strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX); |
| 370 | mount_crypt_stat->global_default_fnek_sig[ |
| 371 | ECRYPTFS_SIG_SIZE_HEX] = '\0'; |
| 372 | rc = ecryptfs_add_global_auth_tok( |
| 373 | mount_crypt_stat, |
Tyler Hicks | 84814d6 | 2009-03-13 13:51:59 -0700 | [diff] [blame] | 374 | mount_crypt_stat->global_default_fnek_sig, |
| 375 | ECRYPTFS_AUTH_TOK_FNEK); |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 376 | if (rc) { |
| 377 | printk(KERN_ERR "Error attempting to register " |
| 378 | "global fnek sig [%s]; rc = [%d]\n", |
| 379 | mount_crypt_stat->global_default_fnek_sig, |
| 380 | rc); |
| 381 | goto out; |
| 382 | } |
| 383 | mount_crypt_stat->flags |= |
| 384 | (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES |
| 385 | | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK); |
| 386 | break; |
| 387 | case ecryptfs_opt_fn_cipher: |
| 388 | fn_cipher_name_src = args[0].from; |
| 389 | fn_cipher_name_dst = |
| 390 | mount_crypt_stat->global_default_fn_cipher_name; |
| 391 | strncpy(fn_cipher_name_dst, fn_cipher_name_src, |
| 392 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
| 393 | mount_crypt_stat->global_default_fn_cipher_name[ |
| 394 | ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0'; |
| 395 | fn_cipher_name_set = 1; |
| 396 | break; |
| 397 | case ecryptfs_opt_fn_cipher_key_bytes: |
| 398 | fn_cipher_key_bytes_src = args[0].from; |
| 399 | fn_cipher_key_bytes = |
| 400 | (int)simple_strtol(fn_cipher_key_bytes_src, |
| 401 | &fn_cipher_key_bytes_src, 0); |
| 402 | mount_crypt_stat->global_default_fn_cipher_key_bytes = |
| 403 | fn_cipher_key_bytes; |
| 404 | fn_cipher_key_bytes_set = 1; |
| 405 | break; |
Tyler Hicks | e77cc8d | 2009-04-22 04:08:46 -0500 | [diff] [blame] | 406 | case ecryptfs_opt_unlink_sigs: |
| 407 | mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS; |
| 408 | break; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 409 | case ecryptfs_opt_err: |
| 410 | default: |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 411 | printk(KERN_WARNING |
| 412 | "%s: eCryptfs: unrecognized option [%s]\n", |
| 413 | __func__, p); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 416 | if (!sig_set) { |
| 417 | rc = -EINVAL; |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 418 | ecryptfs_printk(KERN_ERR, "You must supply at least one valid " |
| 419 | "auth tok signature as a mount " |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 420 | "parameter; see the eCryptfs README\n"); |
| 421 | goto out; |
| 422 | } |
| 423 | if (!cipher_name_set) { |
Miklos Szeredi | 8f23680 | 2008-07-23 21:30:05 -0700 | [diff] [blame] | 424 | int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER); |
| 425 | |
| 426 | BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
Miklos Szeredi | 8f23680 | 2008-07-23 21:30:05 -0700 | [diff] [blame] | 427 | strcpy(mount_crypt_stat->global_default_cipher_name, |
| 428 | ECRYPTFS_DEFAULT_CIPHER); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 429 | } |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 430 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) |
| 431 | && !fn_cipher_name_set) |
| 432 | strcpy(mount_crypt_stat->global_default_fn_cipher_name, |
| 433 | mount_crypt_stat->global_default_cipher_name); |
| 434 | if (!cipher_key_bytes_set) |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 435 | mount_crypt_stat->global_default_cipher_key_size = 0; |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 436 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) |
| 437 | && !fn_cipher_key_bytes_set) |
| 438 | mount_crypt_stat->global_default_fn_cipher_key_bytes = |
| 439 | mount_crypt_stat->global_default_cipher_key_size; |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 440 | mutex_lock(&key_tfm_list_mutex); |
| 441 | if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name, |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 442 | NULL)) { |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 443 | rc = ecryptfs_add_new_key_tfm( |
| 444 | NULL, mount_crypt_stat->global_default_cipher_name, |
| 445 | mount_crypt_stat->global_default_cipher_key_size); |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 446 | if (rc) { |
| 447 | printk(KERN_ERR "Error attempting to initialize " |
| 448 | "cipher with name = [%s] and key size = [%td]; " |
| 449 | "rc = [%d]\n", |
| 450 | mount_crypt_stat->global_default_cipher_name, |
| 451 | mount_crypt_stat->global_default_cipher_key_size, |
| 452 | rc); |
| 453 | rc = -EINVAL; |
| 454 | mutex_unlock(&key_tfm_list_mutex); |
| 455 | goto out; |
| 456 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 457 | } |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 458 | if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) |
| 459 | && !ecryptfs_tfm_exists( |
| 460 | mount_crypt_stat->global_default_fn_cipher_name, NULL)) { |
| 461 | rc = ecryptfs_add_new_key_tfm( |
| 462 | NULL, mount_crypt_stat->global_default_fn_cipher_name, |
| 463 | mount_crypt_stat->global_default_fn_cipher_key_bytes); |
| 464 | if (rc) { |
| 465 | printk(KERN_ERR "Error attempting to initialize " |
| 466 | "cipher with name = [%s] and key size = [%td]; " |
| 467 | "rc = [%d]\n", |
| 468 | mount_crypt_stat->global_default_fn_cipher_name, |
| 469 | mount_crypt_stat->global_default_fn_cipher_key_bytes, |
| 470 | rc); |
| 471 | rc = -EINVAL; |
| 472 | mutex_unlock(&key_tfm_list_mutex); |
| 473 | goto out; |
| 474 | } |
| 475 | } |
| 476 | mutex_unlock(&key_tfm_list_mutex); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 477 | rc = ecryptfs_init_global_auth_toks(mount_crypt_stat); |
Michael Halcrow | 87c94c4 | 2009-01-06 14:42:01 -0800 | [diff] [blame] | 478 | if (rc) |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 479 | printk(KERN_WARNING "One or more global auth toks could not " |
| 480 | "properly register; rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 481 | out: |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | struct kmem_cache *ecryptfs_sb_info_cache; |
Al Viro | 4403158 | 2010-05-17 00:59:46 -0400 | [diff] [blame] | 486 | static struct file_system_type ecryptfs_fs_type; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 487 | |
| 488 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 489 | * ecryptfs_read_super |
| 490 | * @sb: The ecryptfs super block |
| 491 | * @dev_name: The path to mount over |
| 492 | * |
| 493 | * Read the super block of the lower filesystem, and use |
| 494 | * ecryptfs_interpose to create our initial inode and super block |
| 495 | * struct. |
| 496 | */ |
| 497 | static int ecryptfs_read_super(struct super_block *sb, const char *dev_name) |
| 498 | { |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 499 | struct path path; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 500 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 501 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 502 | rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 503 | if (rc) { |
| 504 | ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n"); |
Michael Halcrow | 65dc814 | 2007-02-28 20:12:57 -0800 | [diff] [blame] | 505 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 506 | } |
Al Viro | 4403158 | 2010-05-17 00:59:46 -0400 | [diff] [blame] | 507 | if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) { |
| 508 | rc = -EINVAL; |
| 509 | printk(KERN_ERR "Mount on filesystem of type " |
| 510 | "eCryptfs explicitly disallowed due to " |
| 511 | "known incompatibilities\n"); |
| 512 | goto out_free; |
| 513 | } |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 514 | ecryptfs_set_superblock_lower(sb, path.dentry->d_sb); |
| 515 | sb->s_maxbytes = path.dentry->d_sb->s_maxbytes; |
| 516 | sb->s_blocksize = path.dentry->d_sb->s_blocksize; |
| 517 | ecryptfs_set_dentry_lower(sb->s_root, path.dentry); |
| 518 | ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt); |
| 519 | rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 520 | if (rc) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 521 | goto out_free; |
| 522 | rc = 0; |
| 523 | goto out; |
| 524 | out_free: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 525 | path_put(&path); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 526 | out: |
| 527 | return rc; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * ecryptfs_get_sb |
| 532 | * @fs_type |
| 533 | * @flags |
| 534 | * @dev_name: The path to mount over |
| 535 | * @raw_data: The options passed into the kernel |
| 536 | * |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 537 | * The whole ecryptfs_get_sb process is broken into 3 functions: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 538 | * ecryptfs_parse_options(): handle options passed to ecryptfs, if any |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 539 | * ecryptfs_read_super(): this accesses the lower filesystem and uses |
Erez Zadok | fe0fc01 | 2010-01-04 18:17:02 -0500 | [diff] [blame] | 540 | * ecryptfs_interpose to perform most of the linking |
| 541 | * ecryptfs_interpose(): links the lower filesystem into ecryptfs (inode.c) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 542 | */ |
| 543 | static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags, |
| 544 | const char *dev_name, void *raw_data, |
| 545 | struct vfsmount *mnt) |
| 546 | { |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 547 | struct super_block *s; |
| 548 | struct ecryptfs_sb_info *sbi; |
| 549 | struct ecryptfs_dentry_info *root_info; |
| 550 | const char *err = "Getting sb failed"; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 551 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 552 | |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 553 | sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL); |
| 554 | if (!sbi) { |
| 555 | rc = -ENOMEM; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 556 | goto out; |
| 557 | } |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 558 | |
| 559 | rc = ecryptfs_parse_options(sbi, raw_data); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 560 | if (rc) { |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 561 | err = "Error parsing options"; |
| 562 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 563 | } |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 564 | |
| 565 | s = sget(fs_type, NULL, set_anon_super, NULL); |
| 566 | if (IS_ERR(s)) { |
| 567 | rc = PTR_ERR(s); |
| 568 | goto out; |
| 569 | } |
| 570 | |
| 571 | s->s_flags = flags; |
| 572 | rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 573 | if (rc) { |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 574 | deactivate_locked_super(s); |
| 575 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 576 | } |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 577 | |
| 578 | ecryptfs_set_superblock_private(s, sbi); |
| 579 | s->s_bdi = &sbi->bdi; |
| 580 | |
| 581 | /* ->kill_sb() will take care of sbi after that point */ |
| 582 | sbi = NULL; |
| 583 | s->s_op = &ecryptfs_sops; |
| 584 | |
| 585 | rc = -ENOMEM; |
| 586 | s->s_root = d_alloc(NULL, &(const struct qstr) { |
| 587 | .hash = 0,.name = "/",.len = 1}); |
| 588 | if (!s->s_root) { |
| 589 | deactivate_locked_super(s); |
| 590 | goto out; |
| 591 | } |
| 592 | s->s_root->d_op = &ecryptfs_dops; |
| 593 | s->s_root->d_sb = s; |
| 594 | s->s_root->d_parent = s->s_root; |
| 595 | |
| 596 | root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL); |
| 597 | if (!root_info) { |
| 598 | deactivate_locked_super(s); |
| 599 | goto out; |
| 600 | } |
| 601 | /* ->kill_sb() will take care of root_info */ |
| 602 | ecryptfs_set_dentry_private(s->s_root, root_info); |
| 603 | s->s_flags |= MS_ACTIVE; |
| 604 | rc = ecryptfs_read_super(s, dev_name); |
| 605 | if (rc) { |
| 606 | deactivate_locked_super(s); |
| 607 | err = "Reading sb failed"; |
| 608 | goto out; |
| 609 | } |
| 610 | simple_set_mnt(mnt, s); |
| 611 | return 0; |
| 612 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 613 | out: |
Al Viro | 2ccde7c | 2010-03-21 12:24:29 -0400 | [diff] [blame] | 614 | if (sbi) { |
| 615 | ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat); |
| 616 | kmem_cache_free(ecryptfs_sb_info_cache, sbi); |
| 617 | } |
| 618 | printk(KERN_ERR "%s; rc = [%d]\n", err, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 619 | return rc; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * ecryptfs_kill_block_super |
| 624 | * @sb: The ecryptfs super block |
| 625 | * |
| 626 | * Used to bring the superblock down and free the private data. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 627 | */ |
| 628 | static void ecryptfs_kill_block_super(struct super_block *sb) |
| 629 | { |
Al Viro | decabd6 | 2010-03-20 22:32:26 -0400 | [diff] [blame] | 630 | struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb); |
| 631 | kill_anon_super(sb); |
| 632 | if (!sb_info) |
| 633 | return; |
| 634 | ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat); |
| 635 | bdi_destroy(&sb_info->bdi); |
| 636 | kmem_cache_free(ecryptfs_sb_info_cache, sb_info); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | static struct file_system_type ecryptfs_fs_type = { |
| 640 | .owner = THIS_MODULE, |
| 641 | .name = "ecryptfs", |
| 642 | .get_sb = ecryptfs_get_sb, |
| 643 | .kill_sb = ecryptfs_kill_block_super, |
| 644 | .fs_flags = 0 |
| 645 | }; |
| 646 | |
| 647 | /** |
| 648 | * inode_info_init_once |
| 649 | * |
| 650 | * Initializes the ecryptfs_inode_info_cache when it is created |
| 651 | */ |
| 652 | static void |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 653 | inode_info_init_once(void *vptr) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 654 | { |
| 655 | struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; |
| 656 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 657 | inode_init_once(&ei->vfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static struct ecryptfs_cache_info { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 661 | struct kmem_cache **cache; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 662 | const char *name; |
| 663 | size_t size; |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 664 | void (*ctor)(void *obj); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 665 | } ecryptfs_cache_infos[] = { |
| 666 | { |
| 667 | .cache = &ecryptfs_auth_tok_list_item_cache, |
| 668 | .name = "ecryptfs_auth_tok_list_item", |
| 669 | .size = sizeof(struct ecryptfs_auth_tok_list_item), |
| 670 | }, |
| 671 | { |
| 672 | .cache = &ecryptfs_file_info_cache, |
| 673 | .name = "ecryptfs_file_cache", |
| 674 | .size = sizeof(struct ecryptfs_file_info), |
| 675 | }, |
| 676 | { |
| 677 | .cache = &ecryptfs_dentry_info_cache, |
| 678 | .name = "ecryptfs_dentry_info_cache", |
| 679 | .size = sizeof(struct ecryptfs_dentry_info), |
| 680 | }, |
| 681 | { |
| 682 | .cache = &ecryptfs_inode_info_cache, |
| 683 | .name = "ecryptfs_inode_cache", |
| 684 | .size = sizeof(struct ecryptfs_inode_info), |
| 685 | .ctor = inode_info_init_once, |
| 686 | }, |
| 687 | { |
| 688 | .cache = &ecryptfs_sb_info_cache, |
| 689 | .name = "ecryptfs_sb_cache", |
| 690 | .size = sizeof(struct ecryptfs_sb_info), |
| 691 | }, |
| 692 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 693 | .cache = &ecryptfs_header_cache_1, |
| 694 | .name = "ecryptfs_headers_1", |
| 695 | .size = PAGE_CACHE_SIZE, |
| 696 | }, |
| 697 | { |
| 698 | .cache = &ecryptfs_header_cache_2, |
| 699 | .name = "ecryptfs_headers_2", |
| 700 | .size = PAGE_CACHE_SIZE, |
| 701 | }, |
| 702 | { |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 703 | .cache = &ecryptfs_xattr_cache, |
| 704 | .name = "ecryptfs_xattr_cache", |
| 705 | .size = PAGE_CACHE_SIZE, |
| 706 | }, |
| 707 | { |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 708 | .cache = &ecryptfs_key_record_cache, |
| 709 | .name = "ecryptfs_key_record_cache", |
| 710 | .size = sizeof(struct ecryptfs_key_record), |
| 711 | }, |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 712 | { |
| 713 | .cache = &ecryptfs_key_sig_cache, |
| 714 | .name = "ecryptfs_key_sig_cache", |
| 715 | .size = sizeof(struct ecryptfs_key_sig), |
| 716 | }, |
| 717 | { |
| 718 | .cache = &ecryptfs_global_auth_tok_cache, |
| 719 | .name = "ecryptfs_global_auth_tok_cache", |
| 720 | .size = sizeof(struct ecryptfs_global_auth_tok), |
| 721 | }, |
| 722 | { |
| 723 | .cache = &ecryptfs_key_tfm_cache, |
| 724 | .name = "ecryptfs_key_tfm_cache", |
| 725 | .size = sizeof(struct ecryptfs_key_tfm), |
| 726 | }, |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 727 | { |
| 728 | .cache = &ecryptfs_open_req_cache, |
| 729 | .name = "ecryptfs_open_req_cache", |
| 730 | .size = sizeof(struct ecryptfs_open_req), |
| 731 | }, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | static void ecryptfs_free_kmem_caches(void) |
| 735 | { |
| 736 | int i; |
| 737 | |
| 738 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { |
| 739 | struct ecryptfs_cache_info *info; |
| 740 | |
| 741 | info = &ecryptfs_cache_infos[i]; |
| 742 | if (*(info->cache)) |
| 743 | kmem_cache_destroy(*(info->cache)); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * ecryptfs_init_kmem_caches |
| 749 | * |
| 750 | * Returns zero on success; non-zero otherwise |
| 751 | */ |
| 752 | static int ecryptfs_init_kmem_caches(void) |
| 753 | { |
| 754 | int i; |
| 755 | |
| 756 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { |
| 757 | struct ecryptfs_cache_info *info; |
| 758 | |
| 759 | info = &ecryptfs_cache_infos[i]; |
| 760 | *(info->cache) = kmem_cache_create(info->name, info->size, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 761 | 0, SLAB_HWCACHE_ALIGN, info->ctor); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 762 | if (!*(info->cache)) { |
| 763 | ecryptfs_free_kmem_caches(); |
| 764 | ecryptfs_printk(KERN_WARNING, "%s: " |
| 765 | "kmem_cache_create failed\n", |
| 766 | info->name); |
| 767 | return -ENOMEM; |
| 768 | } |
| 769 | } |
| 770 | return 0; |
| 771 | } |
| 772 | |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 773 | static struct kobject *ecryptfs_kobj; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 774 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 775 | static ssize_t version_show(struct kobject *kobj, |
| 776 | struct kobj_attribute *attr, char *buff) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 777 | { |
| 778 | return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK); |
| 779 | } |
| 780 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 781 | static struct kobj_attribute version_attr = __ATTR_RO(version); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 782 | |
Greg Kroah-Hartman | 30a468b | 2007-10-15 15:01:24 -0700 | [diff] [blame] | 783 | static struct attribute *attributes[] = { |
| 784 | &version_attr.attr, |
Greg Kroah-Hartman | 30a468b | 2007-10-15 15:01:24 -0700 | [diff] [blame] | 785 | NULL, |
| 786 | }; |
| 787 | |
| 788 | static struct attribute_group attr_group = { |
| 789 | .attrs = attributes, |
| 790 | }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 791 | |
| 792 | static int do_sysfs_registration(void) |
| 793 | { |
| 794 | int rc; |
| 795 | |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 796 | ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj); |
| 797 | if (!ecryptfs_kobj) { |
Greg Kroah-Hartman | 917e865 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 798 | printk(KERN_ERR "Unable to create ecryptfs kset\n"); |
| 799 | rc = -ENOMEM; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 800 | goto out; |
| 801 | } |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 802 | rc = sysfs_create_group(ecryptfs_kobj, &attr_group); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 803 | if (rc) { |
| 804 | printk(KERN_ERR |
Greg Kroah-Hartman | 30a468b | 2007-10-15 15:01:24 -0700 | [diff] [blame] | 805 | "Unable to create ecryptfs version attributes\n"); |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 806 | kobject_put(ecryptfs_kobj); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 807 | } |
| 808 | out: |
| 809 | return rc; |
| 810 | } |
| 811 | |
Ryusuke Konishi | a75de1b | 2007-08-10 13:00:56 -0700 | [diff] [blame] | 812 | static void do_sysfs_unregistration(void) |
| 813 | { |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 814 | sysfs_remove_group(ecryptfs_kobj, &attr_group); |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 815 | kobject_put(ecryptfs_kobj); |
Ryusuke Konishi | a75de1b | 2007-08-10 13:00:56 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 818 | static int __init ecryptfs_init(void) |
| 819 | { |
| 820 | int rc; |
| 821 | |
| 822 | if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { |
| 823 | rc = -EINVAL; |
| 824 | ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " |
| 825 | "larger than the host's page size, and so " |
| 826 | "eCryptfs cannot run on this system. The " |
| 827 | "default eCryptfs extent size is [%d] bytes; " |
| 828 | "the page size is [%d] bytes.\n", |
| 829 | ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE); |
| 830 | goto out; |
| 831 | } |
| 832 | rc = ecryptfs_init_kmem_caches(); |
| 833 | if (rc) { |
| 834 | printk(KERN_ERR |
| 835 | "Failed to allocate one or more kmem_cache objects\n"); |
| 836 | goto out; |
| 837 | } |
| 838 | rc = register_filesystem(&ecryptfs_fs_type); |
| 839 | if (rc) { |
| 840 | printk(KERN_ERR "Failed to register filesystem\n"); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 841 | goto out_free_kmem_caches; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 842 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 843 | rc = do_sysfs_registration(); |
| 844 | if (rc) { |
| 845 | printk(KERN_ERR "sysfs registration failed\n"); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 846 | goto out_unregister_filesystem; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 847 | } |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 848 | rc = ecryptfs_init_kthread(); |
| 849 | if (rc) { |
| 850 | printk(KERN_ERR "%s: kthread initialization failed; " |
| 851 | "rc = [%d]\n", __func__, rc); |
| 852 | goto out_do_sysfs_unregistration; |
| 853 | } |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 854 | rc = ecryptfs_init_messaging(); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 855 | if (rc) { |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 856 | printk(KERN_ERR "Failure occured while attempting to " |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 857 | "initialize the communications channel to " |
| 858 | "ecryptfsd\n"); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 859 | goto out_destroy_kthread; |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 860 | } |
| 861 | rc = ecryptfs_init_crypto(); |
| 862 | if (rc) { |
| 863 | printk(KERN_ERR "Failure whilst attempting to init crypto; " |
| 864 | "rc = [%d]\n", rc); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 865 | goto out_release_messaging; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 866 | } |
Eric Sandeen | 2830bfd | 2008-02-06 01:38:34 -0800 | [diff] [blame] | 867 | if (ecryptfs_verbosity > 0) |
| 868 | printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values " |
| 869 | "will be written to the syslog!\n", ecryptfs_verbosity); |
| 870 | |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 871 | goto out; |
| 872 | out_release_messaging: |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 873 | ecryptfs_release_messaging(); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 874 | out_destroy_kthread: |
| 875 | ecryptfs_destroy_kthread(); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 876 | out_do_sysfs_unregistration: |
| 877 | do_sysfs_unregistration(); |
| 878 | out_unregister_filesystem: |
| 879 | unregister_filesystem(&ecryptfs_fs_type); |
| 880 | out_free_kmem_caches: |
| 881 | ecryptfs_free_kmem_caches(); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 882 | out: |
| 883 | return rc; |
| 884 | } |
| 885 | |
| 886 | static void __exit ecryptfs_exit(void) |
| 887 | { |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 888 | int rc; |
| 889 | |
| 890 | rc = ecryptfs_destroy_crypto(); |
| 891 | if (rc) |
| 892 | printk(KERN_ERR "Failure whilst attempting to destroy crypto; " |
| 893 | "rc = [%d]\n", rc); |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 894 | ecryptfs_release_messaging(); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 895 | ecryptfs_destroy_kthread(); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 896 | do_sysfs_unregistration(); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 897 | unregister_filesystem(&ecryptfs_fs_type); |
| 898 | ecryptfs_free_kmem_caches(); |
| 899 | } |
| 900 | |
| 901 | MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>"); |
| 902 | MODULE_DESCRIPTION("eCryptfs"); |
| 903 | |
| 904 | MODULE_LICENSE("GPL"); |
| 905 | |
| 906 | module_init(ecryptfs_init) |
| 907 | module_exit(ecryptfs_exit) |