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