1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /** 3 * eCryptfs: Linux filesystem encryption layer 4 * 5 * Copyright (C) 1997-2004 Erez Zadok 6 * Copyright (C) 2001-2004 Stony Brook University 7 * Copyright (C) 2004-2007 International Business Machines Corp. 8 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 9 * Michael C. Thompsion <mcthomps@us.ibm.com> 10 */ 11 12 #include <linux/file.h> 13 #include <linux/vmalloc.h> 14 #include <linux/pagemap.h> 15 #include <linux/dcache.h> 16 #include <linux/namei.h> 17 #include <linux/mount.h> 18 #include <linux/fs_stack.h> 19 #include <linux/slab.h> 20 #include <linux/xattr.h> 21 #include <linux/fileattr.h> 22 #include <asm/unaligned.h> 23 #include "ecryptfs_kernel.h" 24 25 static struct dentry *lock_parent(struct dentry *dentry) 26 { 27 struct dentry *dir; 28 29 dir = dget_parent(dentry); 30 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); 31 return dir; 32 } 33 34 static void unlock_dir(struct dentry *dir) 35 { 36 inode_unlock(d_inode(dir)); 37 dput(dir); 38 } 39 40 static int ecryptfs_inode_test(struct inode *inode, void *lower_inode) 41 { 42 return ecryptfs_inode_to_lower(inode) == lower_inode; 43 } 44 45 static int ecryptfs_inode_set(struct inode *inode, void *opaque) 46 { 47 struct inode *lower_inode = opaque; 48 49 ecryptfs_set_inode_lower(inode, lower_inode); 50 fsstack_copy_attr_all(inode, lower_inode); 51 /* i_size will be overwritten for encrypted regular files */ 52 fsstack_copy_inode_size(inode, lower_inode); 53 inode->i_ino = lower_inode->i_ino; 54 inode->i_mapping->a_ops = &ecryptfs_aops; 55 56 if (S_ISLNK(inode->i_mode)) 57 inode->i_op = &ecryptfs_symlink_iops; 58 else if (S_ISDIR(inode->i_mode)) 59 inode->i_op = &ecryptfs_dir_iops; 60 else 61 inode->i_op = &ecryptfs_main_iops; 62 63 if (S_ISDIR(inode->i_mode)) 64 inode->i_fop = &ecryptfs_dir_fops; 65 else if (special_file(inode->i_mode)) 66 init_special_inode(inode, inode->i_mode, inode->i_rdev); 67 else 68 inode->i_fop = &ecryptfs_main_fops; 69 70 return 0; 71 } 72 73 static struct inode *__ecryptfs_get_inode(struct inode *lower_inode, 74 struct super_block *sb) 75 { 76 struct inode *inode; 77 78 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) 79 return ERR_PTR(-EXDEV); 80 if (!igrab(lower_inode)) 81 return ERR_PTR(-ESTALE); 82 inode = iget5_locked(sb, (unsigned long)lower_inode, 83 ecryptfs_inode_test, ecryptfs_inode_set, 84 lower_inode); 85 if (!inode) { 86 iput(lower_inode); 87 return ERR_PTR(-EACCES); 88 } 89 if (!(inode->i_state & I_NEW)) 90 iput(lower_inode); 91 92 return inode; 93 } 94 95 struct inode *ecryptfs_get_inode(struct inode *lower_inode, 96 struct super_block *sb) 97 { 98 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb); 99 100 if (!IS_ERR(inode) && (inode->i_state & I_NEW)) 101 unlock_new_inode(inode); 102 103 return inode; 104 } 105 106 /** 107 * ecryptfs_interpose 108 * @lower_dentry: Existing dentry in the lower filesystem 109 * @dentry: ecryptfs' dentry 110 * @sb: ecryptfs's super_block 111 * 112 * Interposes upper and lower dentries. 113 * 114 * Returns zero on success; non-zero otherwise 115 */ 116 static int ecryptfs_interpose(struct dentry *lower_dentry, 117 struct dentry *dentry, struct super_block *sb) 118 { 119 struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb); 120 121 if (IS_ERR(inode)) 122 return PTR_ERR(inode); 123 d_instantiate(dentry, inode); 124 125 return 0; 126 } 127 128 static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry, 129 struct inode *inode) 130 { 131 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 132 struct dentry *lower_dir_dentry; 133 struct inode *lower_dir_inode; 134 int rc; 135 136 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent); 137 lower_dir_inode = d_inode(lower_dir_dentry); 138 inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT); 139 dget(lower_dentry); // don't even try to make the lower negative 140 if (lower_dentry->d_parent != lower_dir_dentry) 141 rc = -EINVAL; 142 else if (d_unhashed(lower_dentry)) 143 rc = -EINVAL; 144 else 145 rc = vfs_unlink(&init_user_ns, lower_dir_inode, lower_dentry, 146 NULL); 147 if (rc) { 148 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc); 149 goto out_unlock; 150 } 151 fsstack_copy_attr_times(dir, lower_dir_inode); 152 set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink); 153 inode->i_ctime = dir->i_ctime; 154 out_unlock: 155 dput(lower_dentry); 156 inode_unlock(lower_dir_inode); 157 if (!rc) 158 d_drop(dentry); 159 return rc; 160 } 161 162 /** 163 * ecryptfs_do_create 164 * @directory_inode: inode of the new file's dentry's parent in ecryptfs 165 * @ecryptfs_dentry: New file's dentry in ecryptfs 166 * @mode: The mode of the new file 167 * 168 * Creates the underlying file and the eCryptfs inode which will link to 169 * it. It will also update the eCryptfs directory inode to mimic the 170 * stat of the lower directory inode. 171 * 172 * Returns the new eCryptfs inode on success; an ERR_PTR on error condition 173 */ 174 static struct inode * 175 ecryptfs_do_create(struct inode *directory_inode, 176 struct dentry *ecryptfs_dentry, umode_t mode) 177 { 178 int rc; 179 struct dentry *lower_dentry; 180 struct dentry *lower_dir_dentry; 181 struct inode *inode; 182 183 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); 184 lower_dir_dentry = lock_parent(lower_dentry); 185 rc = vfs_create(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry, 186 mode, true); 187 if (rc) { 188 printk(KERN_ERR "%s: Failure to create dentry in lower fs; " 189 "rc = [%d]\n", __func__, rc); 190 inode = ERR_PTR(rc); 191 goto out_lock; 192 } 193 inode = __ecryptfs_get_inode(d_inode(lower_dentry), 194 directory_inode->i_sb); 195 if (IS_ERR(inode)) { 196 vfs_unlink(&init_user_ns, d_inode(lower_dir_dentry), 197 lower_dentry, NULL); 198 goto out_lock; 199 } 200 fsstack_copy_attr_times(directory_inode, d_inode(lower_dir_dentry)); 201 fsstack_copy_inode_size(directory_inode, d_inode(lower_dir_dentry)); 202 out_lock: 203 unlock_dir(lower_dir_dentry); 204 return inode; 205 } 206 207 /** 208 * ecryptfs_initialize_file 209 * 210 * Cause the file to be changed from a basic empty file to an ecryptfs 211 * file with a header and first data page. 212 * 213 * Returns zero on success 214 */ 215 int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry, 216 struct inode *ecryptfs_inode) 217 { 218 struct ecryptfs_crypt_stat *crypt_stat = 219 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 220 int rc = 0; 221 222 if (S_ISDIR(ecryptfs_inode->i_mode)) { 223 ecryptfs_printk(KERN_DEBUG, "This is a directory\n"); 224 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 225 goto out; 226 } 227 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n"); 228 rc = ecryptfs_new_file_context(ecryptfs_inode); 229 if (rc) { 230 ecryptfs_printk(KERN_ERR, "Error creating new file " 231 "context; rc = [%d]\n", rc); 232 goto out; 233 } 234 rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode); 235 if (rc) { 236 printk(KERN_ERR "%s: Error attempting to initialize " 237 "the lower file for the dentry with name " 238 "[%pd]; rc = [%d]\n", __func__, 239 ecryptfs_dentry, rc); 240 goto out; 241 } 242 rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode); 243 if (rc) 244 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc); 245 ecryptfs_put_lower_file(ecryptfs_inode); 246 out: 247 return rc; 248 } 249 250 /** 251 * ecryptfs_create 252 * @dir: The inode of the directory in which to create the file. 253 * @dentry: The eCryptfs dentry 254 * @mode: The mode of the new file. 255 * 256 * Creates a new file. 257 * 258 * Returns zero on success; non-zero on error condition 259 */ 260 static int 261 ecryptfs_create(struct user_namespace *mnt_userns, 262 struct inode *directory_inode, struct dentry *ecryptfs_dentry, 263 umode_t mode, bool excl) 264 { 265 struct inode *ecryptfs_inode; 266 int rc; 267 268 ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry, 269 mode); 270 if (IS_ERR(ecryptfs_inode)) { 271 ecryptfs_printk(KERN_WARNING, "Failed to create file in" 272 "lower filesystem\n"); 273 rc = PTR_ERR(ecryptfs_inode); 274 goto out; 275 } 276 /* At this point, a file exists on "disk"; we need to make sure 277 * that this on disk file is prepared to be an ecryptfs file */ 278 rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode); 279 if (rc) { 280 ecryptfs_do_unlink(directory_inode, ecryptfs_dentry, 281 ecryptfs_inode); 282 iget_failed(ecryptfs_inode); 283 goto out; 284 } 285 d_instantiate_new(ecryptfs_dentry, ecryptfs_inode); 286 out: 287 return rc; 288 } 289 290 static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode) 291 { 292 struct ecryptfs_crypt_stat *crypt_stat; 293 int rc; 294 295 rc = ecryptfs_get_lower_file(dentry, inode); 296 if (rc) { 297 printk(KERN_ERR "%s: Error attempting to initialize " 298 "the lower file for the dentry with name " 299 "[%pd]; rc = [%d]\n", __func__, 300 dentry, rc); 301 return rc; 302 } 303 304 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; 305 /* TODO: lock for crypt_stat comparison */ 306 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) 307 ecryptfs_set_default_sizes(crypt_stat); 308 309 rc = ecryptfs_read_and_validate_header_region(inode); 310 ecryptfs_put_lower_file(inode); 311 if (rc) { 312 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode); 313 if (!rc) 314 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 315 } 316 317 /* Must return 0 to allow non-eCryptfs files to be looked up, too */ 318 return 0; 319 } 320 321 /** 322 * ecryptfs_lookup_interpose - Dentry interposition for a lookup 323 */ 324 static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry, 325 struct dentry *lower_dentry) 326 { 327 struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent); 328 struct inode *inode, *lower_inode; 329 struct ecryptfs_dentry_info *dentry_info; 330 int rc = 0; 331 332 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL); 333 if (!dentry_info) { 334 dput(lower_dentry); 335 return ERR_PTR(-ENOMEM); 336 } 337 338 fsstack_copy_attr_atime(d_inode(dentry->d_parent), 339 d_inode(path->dentry)); 340 BUG_ON(!d_count(lower_dentry)); 341 342 ecryptfs_set_dentry_private(dentry, dentry_info); 343 dentry_info->lower_path.mnt = mntget(path->mnt); 344 dentry_info->lower_path.dentry = lower_dentry; 345 346 /* 347 * negative dentry can go positive under us here - its parent is not 348 * locked. That's OK and that could happen just as we return from 349 * ecryptfs_lookup() anyway. Just need to be careful and fetch 350 * ->d_inode only once - it's not stable here. 351 */ 352 lower_inode = READ_ONCE(lower_dentry->d_inode); 353 354 if (!lower_inode) { 355 /* We want to add because we couldn't find in lower */ 356 d_add(dentry, NULL); 357 return NULL; 358 } 359 inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb); 360 if (IS_ERR(inode)) { 361 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n", 362 __func__, PTR_ERR(inode)); 363 return ERR_CAST(inode); 364 } 365 if (S_ISREG(inode->i_mode)) { 366 rc = ecryptfs_i_size_read(dentry, inode); 367 if (rc) { 368 make_bad_inode(inode); 369 return ERR_PTR(rc); 370 } 371 } 372 373 if (inode->i_state & I_NEW) 374 unlock_new_inode(inode); 375 return d_splice_alias(inode, dentry); 376 } 377 378 /** 379 * ecryptfs_lookup 380 * @ecryptfs_dir_inode: The eCryptfs directory inode 381 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up 382 * @flags: lookup flags 383 * 384 * Find a file on disk. If the file does not exist, then we'll add it to the 385 * dentry cache and continue on to read it from the disk. 386 */ 387 static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode, 388 struct dentry *ecryptfs_dentry, 389 unsigned int flags) 390 { 391 char *encrypted_and_encoded_name = NULL; 392 struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 393 struct dentry *lower_dir_dentry, *lower_dentry; 394 const char *name = ecryptfs_dentry->d_name.name; 395 size_t len = ecryptfs_dentry->d_name.len; 396 struct dentry *res; 397 int rc = 0; 398 399 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent); 400 401 mount_crypt_stat = &ecryptfs_superblock_to_private( 402 ecryptfs_dentry->d_sb)->mount_crypt_stat; 403 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) { 404 rc = ecryptfs_encrypt_and_encode_filename( 405 &encrypted_and_encoded_name, &len, 406 mount_crypt_stat, name, len); 407 if (rc) { 408 printk(KERN_ERR "%s: Error attempting to encrypt and encode " 409 "filename; rc = [%d]\n", __func__, rc); 410 return ERR_PTR(rc); 411 } 412 name = encrypted_and_encoded_name; 413 } 414 415 lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len); 416 if (IS_ERR(lower_dentry)) { 417 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned " 418 "[%ld] on lower_dentry = [%s]\n", __func__, 419 PTR_ERR(lower_dentry), 420 name); 421 res = ERR_CAST(lower_dentry); 422 } else { 423 res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry); 424 } 425 kfree(encrypted_and_encoded_name); 426 return res; 427 } 428 429 static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir, 430 struct dentry *new_dentry) 431 { 432 struct dentry *lower_old_dentry; 433 struct dentry *lower_new_dentry; 434 struct dentry *lower_dir_dentry; 435 u64 file_size_save; 436 int rc; 437 438 file_size_save = i_size_read(d_inode(old_dentry)); 439 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry); 440 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry); 441 dget(lower_old_dentry); 442 dget(lower_new_dentry); 443 lower_dir_dentry = lock_parent(lower_new_dentry); 444 rc = vfs_link(lower_old_dentry, &init_user_ns, 445 d_inode(lower_dir_dentry), lower_new_dentry, NULL); 446 if (rc || d_really_is_negative(lower_new_dentry)) 447 goto out_lock; 448 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb); 449 if (rc) 450 goto out_lock; 451 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry)); 452 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry)); 453 set_nlink(d_inode(old_dentry), 454 ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink); 455 i_size_write(d_inode(new_dentry), file_size_save); 456 out_lock: 457 unlock_dir(lower_dir_dentry); 458 dput(lower_new_dentry); 459 dput(lower_old_dentry); 460 return rc; 461 } 462 463 static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry) 464 { 465 return ecryptfs_do_unlink(dir, dentry, d_inode(dentry)); 466 } 467 468 static int ecryptfs_symlink(struct user_namespace *mnt_userns, 469 struct inode *dir, struct dentry *dentry, 470 const char *symname) 471 { 472 int rc; 473 struct dentry *lower_dentry; 474 struct dentry *lower_dir_dentry; 475 char *encoded_symname; 476 size_t encoded_symlen; 477 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL; 478 479 lower_dentry = ecryptfs_dentry_to_lower(dentry); 480 dget(lower_dentry); 481 lower_dir_dentry = lock_parent(lower_dentry); 482 mount_crypt_stat = &ecryptfs_superblock_to_private( 483 dir->i_sb)->mount_crypt_stat; 484 rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname, 485 &encoded_symlen, 486 mount_crypt_stat, symname, 487 strlen(symname)); 488 if (rc) 489 goto out_lock; 490 rc = vfs_symlink(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry, 491 encoded_symname); 492 kfree(encoded_symname); 493 if (rc || d_really_is_negative(lower_dentry)) 494 goto out_lock; 495 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); 496 if (rc) 497 goto out_lock; 498 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry)); 499 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry)); 500 out_lock: 501 unlock_dir(lower_dir_dentry); 502 dput(lower_dentry); 503 if (d_really_is_negative(dentry)) 504 d_drop(dentry); 505 return rc; 506 } 507 508 static int ecryptfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 509 struct dentry *dentry, umode_t mode) 510 { 511 int rc; 512 struct dentry *lower_dentry; 513 struct dentry *lower_dir_dentry; 514 515 lower_dentry = ecryptfs_dentry_to_lower(dentry); 516 lower_dir_dentry = lock_parent(lower_dentry); 517 rc = vfs_mkdir(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry, 518 mode); 519 if (rc || d_really_is_negative(lower_dentry)) 520 goto out; 521 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); 522 if (rc) 523 goto out; 524 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry)); 525 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry)); 526 set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink); 527 out: 528 unlock_dir(lower_dir_dentry); 529 if (d_really_is_negative(dentry)) 530 d_drop(dentry); 531 return rc; 532 } 533 534 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry) 535 { 536 struct dentry *lower_dentry; 537 struct dentry *lower_dir_dentry; 538 struct inode *lower_dir_inode; 539 int rc; 540 541 lower_dentry = ecryptfs_dentry_to_lower(dentry); 542 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent); 543 lower_dir_inode = d_inode(lower_dir_dentry); 544 545 inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT); 546 dget(lower_dentry); // don't even try to make the lower negative 547 if (lower_dentry->d_parent != lower_dir_dentry) 548 rc = -EINVAL; 549 else if (d_unhashed(lower_dentry)) 550 rc = -EINVAL; 551 else 552 rc = vfs_rmdir(&init_user_ns, lower_dir_inode, lower_dentry); 553 if (!rc) { 554 clear_nlink(d_inode(dentry)); 555 fsstack_copy_attr_times(dir, lower_dir_inode); 556 set_nlink(dir, lower_dir_inode->i_nlink); 557 } 558 dput(lower_dentry); 559 inode_unlock(lower_dir_inode); 560 if (!rc) 561 d_drop(dentry); 562 return rc; 563 } 564 565 static int 566 ecryptfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 567 struct dentry *dentry, umode_t mode, dev_t dev) 568 { 569 int rc; 570 struct dentry *lower_dentry; 571 struct dentry *lower_dir_dentry; 572 573 lower_dentry = ecryptfs_dentry_to_lower(dentry); 574 lower_dir_dentry = lock_parent(lower_dentry); 575 rc = vfs_mknod(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry, 576 mode, dev); 577 if (rc || d_really_is_negative(lower_dentry)) 578 goto out; 579 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); 580 if (rc) 581 goto out; 582 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry)); 583 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry)); 584 out: 585 unlock_dir(lower_dir_dentry); 586 if (d_really_is_negative(dentry)) 587 d_drop(dentry); 588 return rc; 589 } 590 591 static int 592 ecryptfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 593 struct dentry *old_dentry, struct inode *new_dir, 594 struct dentry *new_dentry, unsigned int flags) 595 { 596 int rc; 597 struct dentry *lower_old_dentry; 598 struct dentry *lower_new_dentry; 599 struct dentry *lower_old_dir_dentry; 600 struct dentry *lower_new_dir_dentry; 601 struct dentry *trap; 602 struct inode *target_inode; 603 struct renamedata rd = {}; 604 605 if (flags) 606 return -EINVAL; 607 608 lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent); 609 lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent); 610 611 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry); 612 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry); 613 614 target_inode = d_inode(new_dentry); 615 616 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry); 617 dget(lower_new_dentry); 618 rc = -EINVAL; 619 if (lower_old_dentry->d_parent != lower_old_dir_dentry) 620 goto out_lock; 621 if (lower_new_dentry->d_parent != lower_new_dir_dentry) 622 goto out_lock; 623 if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry)) 624 goto out_lock; 625 /* source should not be ancestor of target */ 626 if (trap == lower_old_dentry) 627 goto out_lock; 628 /* target should not be ancestor of source */ 629 if (trap == lower_new_dentry) { 630 rc = -ENOTEMPTY; 631 goto out_lock; 632 } 633 634 rd.old_mnt_userns = &init_user_ns; 635 rd.old_dir = d_inode(lower_old_dir_dentry); 636 rd.old_dentry = lower_old_dentry; 637 rd.new_mnt_userns = &init_user_ns; 638 rd.new_dir = d_inode(lower_new_dir_dentry); 639 rd.new_dentry = lower_new_dentry; 640 rc = vfs_rename(&rd); 641 if (rc) 642 goto out_lock; 643 if (target_inode) 644 fsstack_copy_attr_all(target_inode, 645 ecryptfs_inode_to_lower(target_inode)); 646 fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry)); 647 if (new_dir != old_dir) 648 fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry)); 649 out_lock: 650 dput(lower_new_dentry); 651 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry); 652 return rc; 653 } 654 655 static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz) 656 { 657 DEFINE_DELAYED_CALL(done); 658 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 659 const char *link; 660 char *buf; 661 int rc; 662 663 link = vfs_get_link(lower_dentry, &done); 664 if (IS_ERR(link)) 665 return ERR_CAST(link); 666 667 rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb, 668 link, strlen(link)); 669 do_delayed_call(&done); 670 if (rc) 671 return ERR_PTR(rc); 672 673 return buf; 674 } 675 676 static const char *ecryptfs_get_link(struct dentry *dentry, 677 struct inode *inode, 678 struct delayed_call *done) 679 { 680 size_t len; 681 char *buf; 682 683 if (!dentry) 684 return ERR_PTR(-ECHILD); 685 686 buf = ecryptfs_readlink_lower(dentry, &len); 687 if (IS_ERR(buf)) 688 return buf; 689 fsstack_copy_attr_atime(d_inode(dentry), 690 d_inode(ecryptfs_dentry_to_lower(dentry))); 691 buf[len] = '\0'; 692 set_delayed_call(done, kfree_link, buf); 693 return buf; 694 } 695 696 /** 697 * upper_size_to_lower_size 698 * @crypt_stat: Crypt_stat associated with file 699 * @upper_size: Size of the upper file 700 * 701 * Calculate the required size of the lower file based on the 702 * specified size of the upper file. This calculation is based on the 703 * number of headers in the underlying file and the extent size. 704 * 705 * Returns Calculated size of the lower file. 706 */ 707 static loff_t 708 upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat, 709 loff_t upper_size) 710 { 711 loff_t lower_size; 712 713 lower_size = ecryptfs_lower_header_size(crypt_stat); 714 if (upper_size != 0) { 715 loff_t num_extents; 716 717 num_extents = upper_size >> crypt_stat->extent_shift; 718 if (upper_size & ~crypt_stat->extent_mask) 719 num_extents++; 720 lower_size += (num_extents * crypt_stat->extent_size); 721 } 722 return lower_size; 723 } 724 725 /** 726 * truncate_upper 727 * @dentry: The ecryptfs layer dentry 728 * @ia: Address of the ecryptfs inode's attributes 729 * @lower_ia: Address of the lower inode's attributes 730 * 731 * Function to handle truncations modifying the size of the file. Note 732 * that the file sizes are interpolated. When expanding, we are simply 733 * writing strings of 0's out. When truncating, we truncate the upper 734 * inode and update the lower_ia according to the page index 735 * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return, 736 * the caller must use lower_ia in a call to notify_change() to perform 737 * the truncation of the lower inode. 738 * 739 * Returns zero on success; non-zero otherwise 740 */ 741 static int truncate_upper(struct dentry *dentry, struct iattr *ia, 742 struct iattr *lower_ia) 743 { 744 int rc = 0; 745 struct inode *inode = d_inode(dentry); 746 struct ecryptfs_crypt_stat *crypt_stat; 747 loff_t i_size = i_size_read(inode); 748 loff_t lower_size_before_truncate; 749 loff_t lower_size_after_truncate; 750 751 if (unlikely((ia->ia_size == i_size))) { 752 lower_ia->ia_valid &= ~ATTR_SIZE; 753 return 0; 754 } 755 rc = ecryptfs_get_lower_file(dentry, inode); 756 if (rc) 757 return rc; 758 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat; 759 /* Switch on growing or shrinking file */ 760 if (ia->ia_size > i_size) { 761 char zero[] = { 0x00 }; 762 763 lower_ia->ia_valid &= ~ATTR_SIZE; 764 /* Write a single 0 at the last position of the file; 765 * this triggers code that will fill in 0's throughout 766 * the intermediate portion of the previous end of the 767 * file and the new and of the file */ 768 rc = ecryptfs_write(inode, zero, 769 (ia->ia_size - 1), 1); 770 } else { /* ia->ia_size < i_size_read(inode) */ 771 /* We're chopping off all the pages down to the page 772 * in which ia->ia_size is located. Fill in the end of 773 * that page from (ia->ia_size & ~PAGE_MASK) to 774 * PAGE_SIZE with zeros. */ 775 size_t num_zeros = (PAGE_SIZE 776 - (ia->ia_size & ~PAGE_MASK)); 777 778 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 779 truncate_setsize(inode, ia->ia_size); 780 lower_ia->ia_size = ia->ia_size; 781 lower_ia->ia_valid |= ATTR_SIZE; 782 goto out; 783 } 784 if (num_zeros) { 785 char *zeros_virt; 786 787 zeros_virt = kzalloc(num_zeros, GFP_KERNEL); 788 if (!zeros_virt) { 789 rc = -ENOMEM; 790 goto out; 791 } 792 rc = ecryptfs_write(inode, zeros_virt, 793 ia->ia_size, num_zeros); 794 kfree(zeros_virt); 795 if (rc) { 796 printk(KERN_ERR "Error attempting to zero out " 797 "the remainder of the end page on " 798 "reducing truncate; rc = [%d]\n", rc); 799 goto out; 800 } 801 } 802 truncate_setsize(inode, ia->ia_size); 803 rc = ecryptfs_write_inode_size_to_metadata(inode); 804 if (rc) { 805 printk(KERN_ERR "Problem with " 806 "ecryptfs_write_inode_size_to_metadata; " 807 "rc = [%d]\n", rc); 808 goto out; 809 } 810 /* We are reducing the size of the ecryptfs file, and need to 811 * know if we need to reduce the size of the lower file. */ 812 lower_size_before_truncate = 813 upper_size_to_lower_size(crypt_stat, i_size); 814 lower_size_after_truncate = 815 upper_size_to_lower_size(crypt_stat, ia->ia_size); 816 if (lower_size_after_truncate < lower_size_before_truncate) { 817 lower_ia->ia_size = lower_size_after_truncate; 818 lower_ia->ia_valid |= ATTR_SIZE; 819 } else 820 lower_ia->ia_valid &= ~ATTR_SIZE; 821 } 822 out: 823 ecryptfs_put_lower_file(inode); 824 return rc; 825 } 826 827 static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset) 828 { 829 struct ecryptfs_crypt_stat *crypt_stat; 830 loff_t lower_oldsize, lower_newsize; 831 832 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; 833 lower_oldsize = upper_size_to_lower_size(crypt_stat, 834 i_size_read(inode)); 835 lower_newsize = upper_size_to_lower_size(crypt_stat, offset); 836 if (lower_newsize > lower_oldsize) { 837 /* 838 * The eCryptfs inode and the new *lower* size are mixed here 839 * because we may not have the lower i_mutex held and/or it may 840 * not be appropriate to call inode_newsize_ok() with inodes 841 * from other filesystems. 842 */ 843 return inode_newsize_ok(inode, lower_newsize); 844 } 845 846 return 0; 847 } 848 849 /** 850 * ecryptfs_truncate 851 * @dentry: The ecryptfs layer dentry 852 * @new_length: The length to expand the file to 853 * 854 * Simple function that handles the truncation of an eCryptfs inode and 855 * its corresponding lower inode. 856 * 857 * Returns zero on success; non-zero otherwise 858 */ 859 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length) 860 { 861 struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length }; 862 struct iattr lower_ia = { .ia_valid = 0 }; 863 int rc; 864 865 rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length); 866 if (rc) 867 return rc; 868 869 rc = truncate_upper(dentry, &ia, &lower_ia); 870 if (!rc && lower_ia.ia_valid & ATTR_SIZE) { 871 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 872 873 inode_lock(d_inode(lower_dentry)); 874 rc = notify_change(&init_user_ns, lower_dentry, 875 &lower_ia, NULL); 876 inode_unlock(d_inode(lower_dentry)); 877 } 878 return rc; 879 } 880 881 static int 882 ecryptfs_permission(struct user_namespace *mnt_userns, struct inode *inode, 883 int mask) 884 { 885 return inode_permission(&init_user_ns, 886 ecryptfs_inode_to_lower(inode), mask); 887 } 888 889 /** 890 * ecryptfs_setattr 891 * @dentry: dentry handle to the inode to modify 892 * @ia: Structure with flags of what to change and values 893 * 894 * Updates the metadata of an inode. If the update is to the size 895 * i.e. truncation, then ecryptfs_truncate will handle the size modification 896 * of both the ecryptfs inode and the lower inode. 897 * 898 * All other metadata changes will be passed right to the lower filesystem, 899 * and we will just update our inode to look like the lower. 900 */ 901 static int ecryptfs_setattr(struct user_namespace *mnt_userns, 902 struct dentry *dentry, struct iattr *ia) 903 { 904 int rc = 0; 905 struct dentry *lower_dentry; 906 struct iattr lower_ia; 907 struct inode *inode; 908 struct inode *lower_inode; 909 struct ecryptfs_crypt_stat *crypt_stat; 910 911 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat; 912 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) { 913 rc = ecryptfs_init_crypt_stat(crypt_stat); 914 if (rc) 915 return rc; 916 } 917 inode = d_inode(dentry); 918 lower_inode = ecryptfs_inode_to_lower(inode); 919 lower_dentry = ecryptfs_dentry_to_lower(dentry); 920 mutex_lock(&crypt_stat->cs_mutex); 921 if (d_is_dir(dentry)) 922 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 923 else if (d_is_reg(dentry) 924 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED) 925 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) { 926 struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 927 928 mount_crypt_stat = &ecryptfs_superblock_to_private( 929 dentry->d_sb)->mount_crypt_stat; 930 rc = ecryptfs_get_lower_file(dentry, inode); 931 if (rc) { 932 mutex_unlock(&crypt_stat->cs_mutex); 933 goto out; 934 } 935 rc = ecryptfs_read_metadata(dentry); 936 ecryptfs_put_lower_file(inode); 937 if (rc) { 938 if (!(mount_crypt_stat->flags 939 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) { 940 rc = -EIO; 941 printk(KERN_WARNING "Either the lower file " 942 "is not in a valid eCryptfs format, " 943 "or the key could not be retrieved. " 944 "Plaintext passthrough mode is not " 945 "enabled; returning -EIO\n"); 946 mutex_unlock(&crypt_stat->cs_mutex); 947 goto out; 948 } 949 rc = 0; 950 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED 951 | ECRYPTFS_ENCRYPTED); 952 } 953 } 954 mutex_unlock(&crypt_stat->cs_mutex); 955 956 rc = setattr_prepare(&init_user_ns, dentry, ia); 957 if (rc) 958 goto out; 959 if (ia->ia_valid & ATTR_SIZE) { 960 rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size); 961 if (rc) 962 goto out; 963 } 964 965 memcpy(&lower_ia, ia, sizeof(lower_ia)); 966 if (ia->ia_valid & ATTR_FILE) 967 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file); 968 if (ia->ia_valid & ATTR_SIZE) { 969 rc = truncate_upper(dentry, ia, &lower_ia); 970 if (rc < 0) 971 goto out; 972 } 973 974 /* 975 * mode change is for clearing setuid/setgid bits. Allow lower fs 976 * to interpret this in its own way. 977 */ 978 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) 979 lower_ia.ia_valid &= ~ATTR_MODE; 980 981 inode_lock(d_inode(lower_dentry)); 982 rc = notify_change(&init_user_ns, lower_dentry, &lower_ia, NULL); 983 inode_unlock(d_inode(lower_dentry)); 984 out: 985 fsstack_copy_attr_all(inode, lower_inode); 986 return rc; 987 } 988 989 static int ecryptfs_getattr_link(struct user_namespace *mnt_userns, 990 const struct path *path, struct kstat *stat, 991 u32 request_mask, unsigned int flags) 992 { 993 struct dentry *dentry = path->dentry; 994 struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 995 int rc = 0; 996 997 mount_crypt_stat = &ecryptfs_superblock_to_private( 998 dentry->d_sb)->mount_crypt_stat; 999 generic_fillattr(&init_user_ns, d_inode(dentry), stat); 1000 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) { 1001 char *target; 1002 size_t targetsiz; 1003 1004 target = ecryptfs_readlink_lower(dentry, &targetsiz); 1005 if (!IS_ERR(target)) { 1006 kfree(target); 1007 stat->size = targetsiz; 1008 } else { 1009 rc = PTR_ERR(target); 1010 } 1011 } 1012 return rc; 1013 } 1014 1015 static int ecryptfs_getattr(struct user_namespace *mnt_userns, 1016 const struct path *path, struct kstat *stat, 1017 u32 request_mask, unsigned int flags) 1018 { 1019 struct dentry *dentry = path->dentry; 1020 struct kstat lower_stat; 1021 int rc; 1022 1023 rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat, 1024 request_mask, flags); 1025 if (!rc) { 1026 fsstack_copy_attr_all(d_inode(dentry), 1027 ecryptfs_inode_to_lower(d_inode(dentry))); 1028 generic_fillattr(&init_user_ns, d_inode(dentry), stat); 1029 stat->blocks = lower_stat.blocks; 1030 } 1031 return rc; 1032 } 1033 1034 int 1035 ecryptfs_setxattr(struct dentry *dentry, struct inode *inode, 1036 const char *name, const void *value, 1037 size_t size, int flags) 1038 { 1039 int rc; 1040 struct dentry *lower_dentry; 1041 struct inode *lower_inode; 1042 1043 lower_dentry = ecryptfs_dentry_to_lower(dentry); 1044 lower_inode = d_inode(lower_dentry); 1045 if (!(lower_inode->i_opflags & IOP_XATTR)) { 1046 rc = -EOPNOTSUPP; 1047 goto out; 1048 } 1049 inode_lock(lower_inode); 1050 rc = __vfs_setxattr_locked(&init_user_ns, lower_dentry, name, value, size, flags, NULL); 1051 inode_unlock(lower_inode); 1052 if (!rc && inode) 1053 fsstack_copy_attr_all(inode, lower_inode); 1054 out: 1055 return rc; 1056 } 1057 1058 ssize_t 1059 ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode, 1060 const char *name, void *value, size_t size) 1061 { 1062 int rc; 1063 1064 if (!(lower_inode->i_opflags & IOP_XATTR)) { 1065 rc = -EOPNOTSUPP; 1066 goto out; 1067 } 1068 inode_lock(lower_inode); 1069 rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size); 1070 inode_unlock(lower_inode); 1071 out: 1072 return rc; 1073 } 1074 1075 static ssize_t 1076 ecryptfs_getxattr(struct dentry *dentry, struct inode *inode, 1077 const char *name, void *value, size_t size) 1078 { 1079 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), 1080 ecryptfs_inode_to_lower(inode), 1081 name, value, size); 1082 } 1083 1084 static ssize_t 1085 ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size) 1086 { 1087 int rc = 0; 1088 struct dentry *lower_dentry; 1089 1090 lower_dentry = ecryptfs_dentry_to_lower(dentry); 1091 if (!d_inode(lower_dentry)->i_op->listxattr) { 1092 rc = -EOPNOTSUPP; 1093 goto out; 1094 } 1095 inode_lock(d_inode(lower_dentry)); 1096 rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size); 1097 inode_unlock(d_inode(lower_dentry)); 1098 out: 1099 return rc; 1100 } 1101 1102 static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode, 1103 const char *name) 1104 { 1105 int rc; 1106 struct dentry *lower_dentry; 1107 struct inode *lower_inode; 1108 1109 lower_dentry = ecryptfs_dentry_to_lower(dentry); 1110 lower_inode = ecryptfs_inode_to_lower(inode); 1111 if (!(lower_inode->i_opflags & IOP_XATTR)) { 1112 rc = -EOPNOTSUPP; 1113 goto out; 1114 } 1115 inode_lock(lower_inode); 1116 rc = __vfs_removexattr(&init_user_ns, lower_dentry, name); 1117 inode_unlock(lower_inode); 1118 out: 1119 return rc; 1120 } 1121 1122 static int ecryptfs_fileattr_get(struct dentry *dentry, struct fileattr *fa) 1123 { 1124 return vfs_fileattr_get(ecryptfs_dentry_to_lower(dentry), fa); 1125 } 1126 1127 static int ecryptfs_fileattr_set(struct user_namespace *mnt_userns, 1128 struct dentry *dentry, struct fileattr *fa) 1129 { 1130 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 1131 int rc; 1132 1133 rc = vfs_fileattr_set(&init_user_ns, lower_dentry, fa); 1134 fsstack_copy_attr_all(d_inode(dentry), d_inode(lower_dentry)); 1135 1136 return rc; 1137 } 1138 1139 const struct inode_operations ecryptfs_symlink_iops = { 1140 .get_link = ecryptfs_get_link, 1141 .permission = ecryptfs_permission, 1142 .setattr = ecryptfs_setattr, 1143 .getattr = ecryptfs_getattr_link, 1144 .listxattr = ecryptfs_listxattr, 1145 }; 1146 1147 const struct inode_operations ecryptfs_dir_iops = { 1148 .create = ecryptfs_create, 1149 .lookup = ecryptfs_lookup, 1150 .link = ecryptfs_link, 1151 .unlink = ecryptfs_unlink, 1152 .symlink = ecryptfs_symlink, 1153 .mkdir = ecryptfs_mkdir, 1154 .rmdir = ecryptfs_rmdir, 1155 .mknod = ecryptfs_mknod, 1156 .rename = ecryptfs_rename, 1157 .permission = ecryptfs_permission, 1158 .setattr = ecryptfs_setattr, 1159 .listxattr = ecryptfs_listxattr, 1160 .fileattr_get = ecryptfs_fileattr_get, 1161 .fileattr_set = ecryptfs_fileattr_set, 1162 }; 1163 1164 const struct inode_operations ecryptfs_main_iops = { 1165 .permission = ecryptfs_permission, 1166 .setattr = ecryptfs_setattr, 1167 .getattr = ecryptfs_getattr, 1168 .listxattr = ecryptfs_listxattr, 1169 .fileattr_get = ecryptfs_fileattr_get, 1170 .fileattr_set = ecryptfs_fileattr_set, 1171 }; 1172 1173 static int ecryptfs_xattr_get(const struct xattr_handler *handler, 1174 struct dentry *dentry, struct inode *inode, 1175 const char *name, void *buffer, size_t size) 1176 { 1177 return ecryptfs_getxattr(dentry, inode, name, buffer, size); 1178 } 1179 1180 static int ecryptfs_xattr_set(const struct xattr_handler *handler, 1181 struct user_namespace *mnt_userns, 1182 struct dentry *dentry, struct inode *inode, 1183 const char *name, const void *value, size_t size, 1184 int flags) 1185 { 1186 if (value) 1187 return ecryptfs_setxattr(dentry, inode, name, value, size, flags); 1188 else { 1189 BUG_ON(flags != XATTR_REPLACE); 1190 return ecryptfs_removexattr(dentry, inode, name); 1191 } 1192 } 1193 1194 static const struct xattr_handler ecryptfs_xattr_handler = { 1195 .prefix = "", /* match anything */ 1196 .get = ecryptfs_xattr_get, 1197 .set = ecryptfs_xattr_set, 1198 }; 1199 1200 const struct xattr_handler *ecryptfs_xattr_handlers[] = { 1201 &ecryptfs_xattr_handler, 1202 NULL 1203 }; 1204