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