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