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