1 /** 2 * eCryptfs: Linux filesystem encryption layer 3 * 4 * Copyright (C) 1997-2004 Erez Zadok 5 * Copyright (C) 2001-2004 Stony Brook University 6 * Copyright (C) 2004-2007 International Business Machines Corp. 7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 8 * Michael C. Thompsion <mcthomps@us.ibm.com> 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of the 13 * License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, but 16 * WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23 * 02111-1307, USA. 24 */ 25 26 #include <linux/file.h> 27 #include <linux/vmalloc.h> 28 #include <linux/pagemap.h> 29 #include <linux/dcache.h> 30 #include <linux/namei.h> 31 #include <linux/mount.h> 32 #include <linux/crypto.h> 33 #include <linux/fs_stack.h> 34 #include <asm/unaligned.h> 35 #include "ecryptfs_kernel.h" 36 37 static struct dentry *lock_parent(struct dentry *dentry) 38 { 39 struct dentry *dir; 40 41 dir = dget_parent(dentry); 42 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT); 43 return dir; 44 } 45 46 static void unlock_dir(struct dentry *dir) 47 { 48 mutex_unlock(&dir->d_inode->i_mutex); 49 dput(dir); 50 } 51 52 /** 53 * ecryptfs_create_underlying_file 54 * @lower_dir_inode: inode of the parent in the lower fs of the new file 55 * @lower_dentry: New file's dentry in the lower fs 56 * @ecryptfs_dentry: New file's dentry in ecryptfs 57 * @mode: The mode of the new file 58 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount 59 * 60 * Creates the file in the lower file system. 61 * 62 * Returns zero on success; non-zero on error condition 63 */ 64 static int 65 ecryptfs_create_underlying_file(struct inode *lower_dir_inode, 66 struct dentry *dentry, int mode, 67 struct nameidata *nd) 68 { 69 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 70 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry); 71 struct dentry *dentry_save; 72 struct vfsmount *vfsmount_save; 73 int rc; 74 75 dentry_save = nd->path.dentry; 76 vfsmount_save = nd->path.mnt; 77 nd->path.dentry = lower_dentry; 78 nd->path.mnt = lower_mnt; 79 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd); 80 nd->path.dentry = dentry_save; 81 nd->path.mnt = vfsmount_save; 82 return rc; 83 } 84 85 /** 86 * ecryptfs_do_create 87 * @directory_inode: inode of the new file's dentry's parent in ecryptfs 88 * @ecryptfs_dentry: New file's dentry in ecryptfs 89 * @mode: The mode of the new file 90 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount 91 * 92 * Creates the underlying file and the eCryptfs inode which will link to 93 * it. It will also update the eCryptfs directory inode to mimic the 94 * stat of the lower directory inode. 95 * 96 * Returns zero on success; non-zero on error condition 97 */ 98 static int 99 ecryptfs_do_create(struct inode *directory_inode, 100 struct dentry *ecryptfs_dentry, int mode, 101 struct nameidata *nd) 102 { 103 int rc; 104 struct dentry *lower_dentry; 105 struct dentry *lower_dir_dentry; 106 107 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); 108 lower_dir_dentry = lock_parent(lower_dentry); 109 if (IS_ERR(lower_dir_dentry)) { 110 ecryptfs_printk(KERN_ERR, "Error locking directory of " 111 "dentry\n"); 112 rc = PTR_ERR(lower_dir_dentry); 113 goto out; 114 } 115 rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode, 116 ecryptfs_dentry, mode, nd); 117 if (rc) { 118 printk(KERN_ERR "%s: Failure to create dentry in lower fs; " 119 "rc = [%d]\n", __func__, rc); 120 goto out_lock; 121 } 122 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, 123 directory_inode->i_sb, 0); 124 if (rc) { 125 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n"); 126 goto out_lock; 127 } 128 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode); 129 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode); 130 out_lock: 131 unlock_dir(lower_dir_dentry); 132 out: 133 return rc; 134 } 135 136 /** 137 * grow_file 138 * @ecryptfs_dentry: the eCryptfs dentry 139 * 140 * This is the code which will grow the file to its correct size. 141 */ 142 static int grow_file(struct dentry *ecryptfs_dentry) 143 { 144 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode; 145 struct file fake_file; 146 struct ecryptfs_file_info tmp_file_info; 147 char zero_virt[] = { 0x00 }; 148 int rc = 0; 149 150 memset(&fake_file, 0, sizeof(fake_file)); 151 fake_file.f_path.dentry = ecryptfs_dentry; 152 memset(&tmp_file_info, 0, sizeof(tmp_file_info)); 153 ecryptfs_set_file_private(&fake_file, &tmp_file_info); 154 ecryptfs_set_file_lower( 155 &fake_file, 156 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file); 157 rc = ecryptfs_write(&fake_file, zero_virt, 0, 1); 158 i_size_write(ecryptfs_inode, 0); 159 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); 160 ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |= 161 ECRYPTFS_NEW_FILE; 162 return rc; 163 } 164 165 /** 166 * ecryptfs_initialize_file 167 * 168 * Cause the file to be changed from a basic empty file to an ecryptfs 169 * file with a header and first data page. 170 * 171 * Returns zero on success 172 */ 173 static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry) 174 { 175 struct ecryptfs_crypt_stat *crypt_stat = 176 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; 177 int rc = 0; 178 179 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) { 180 ecryptfs_printk(KERN_DEBUG, "This is a directory\n"); 181 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 182 goto out; 183 } 184 crypt_stat->flags |= ECRYPTFS_NEW_FILE; 185 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n"); 186 rc = ecryptfs_new_file_context(ecryptfs_dentry); 187 if (rc) { 188 ecryptfs_printk(KERN_ERR, "Error creating new file " 189 "context; rc = [%d]\n", rc); 190 goto out; 191 } 192 rc = ecryptfs_write_metadata(ecryptfs_dentry); 193 if (rc) { 194 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc); 195 goto out; 196 } 197 rc = grow_file(ecryptfs_dentry); 198 if (rc) 199 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc); 200 out: 201 return rc; 202 } 203 204 /** 205 * ecryptfs_create 206 * @dir: The inode of the directory in which to create the file. 207 * @dentry: The eCryptfs dentry 208 * @mode: The mode of the new file. 209 * @nd: nameidata 210 * 211 * Creates a new file. 212 * 213 * Returns zero on success; non-zero on error condition 214 */ 215 static int 216 ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry, 217 int mode, struct nameidata *nd) 218 { 219 int rc; 220 221 /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens 222 * the crypt_stat->lower_file (persistent file) */ 223 rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd); 224 if (unlikely(rc)) { 225 ecryptfs_printk(KERN_WARNING, "Failed to create file in" 226 "lower filesystem\n"); 227 goto out; 228 } 229 /* At this point, a file exists on "disk"; we need to make sure 230 * that this on disk file is prepared to be an ecryptfs file */ 231 rc = ecryptfs_initialize_file(ecryptfs_dentry); 232 out: 233 return rc; 234 } 235 236 /** 237 * ecryptfs_lookup 238 * @dir: inode 239 * @dentry: The dentry 240 * @nd: nameidata, may be NULL 241 * 242 * Find a file on disk. If the file does not exist, then we'll add it to the 243 * dentry cache and continue on to read it from the disk. 244 */ 245 static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry, 246 struct nameidata *nd) 247 { 248 int rc = 0; 249 struct dentry *lower_dir_dentry; 250 struct dentry *lower_dentry; 251 struct vfsmount *lower_mnt; 252 char *encoded_name; 253 int encoded_namelen; 254 struct ecryptfs_crypt_stat *crypt_stat = NULL; 255 struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 256 char *page_virt = NULL; 257 struct inode *lower_inode; 258 u64 file_size; 259 260 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent); 261 dentry->d_op = &ecryptfs_dops; 262 if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, ".")) 263 || (dentry->d_name.len == 2 264 && !strcmp(dentry->d_name.name, ".."))) { 265 d_drop(dentry); 266 goto out; 267 } 268 encoded_namelen = ecryptfs_encode_filename(crypt_stat, 269 dentry->d_name.name, 270 dentry->d_name.len, 271 &encoded_name); 272 if (encoded_namelen < 0) { 273 rc = encoded_namelen; 274 d_drop(dentry); 275 goto out; 276 } 277 ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen " 278 "= [%d]\n", encoded_name, encoded_namelen); 279 lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry, 280 encoded_namelen - 1); 281 kfree(encoded_name); 282 if (IS_ERR(lower_dentry)) { 283 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n"); 284 rc = PTR_ERR(lower_dentry); 285 d_drop(dentry); 286 goto out; 287 } 288 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent)); 289 ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->" 290 "d_name.name = [%s]\n", lower_dentry, 291 lower_dentry->d_name.name); 292 lower_inode = lower_dentry->d_inode; 293 fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode); 294 BUG_ON(!atomic_read(&lower_dentry->d_count)); 295 ecryptfs_set_dentry_private(dentry, 296 kmem_cache_alloc(ecryptfs_dentry_info_cache, 297 GFP_KERNEL)); 298 if (!ecryptfs_dentry_to_private(dentry)) { 299 rc = -ENOMEM; 300 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting " 301 "to allocate ecryptfs_dentry_info struct\n"); 302 goto out_dput; 303 } 304 ecryptfs_set_dentry_lower(dentry, lower_dentry); 305 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt); 306 if (!lower_dentry->d_inode) { 307 /* We want to add because we couldn't find in lower */ 308 d_add(dentry, NULL); 309 goto out; 310 } 311 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 312 ECRYPTFS_INTERPOSE_FLAG_D_ADD); 313 if (rc) { 314 ecryptfs_printk(KERN_ERR, "Error interposing\n"); 315 goto out_dput; 316 } 317 if (S_ISDIR(lower_inode->i_mode)) { 318 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n"); 319 goto out; 320 } 321 if (S_ISLNK(lower_inode->i_mode)) { 322 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n"); 323 goto out; 324 } 325 if (special_file(lower_inode->i_mode)) { 326 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n"); 327 goto out; 328 } 329 if (!nd) { 330 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave" 331 "as we *think* we are about to unlink\n"); 332 goto out; 333 } 334 /* Released in this function */ 335 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, 336 GFP_USER); 337 if (!page_virt) { 338 rc = -ENOMEM; 339 ecryptfs_printk(KERN_ERR, 340 "Cannot ecryptfs_kmalloc a page\n"); 341 goto out_dput; 342 } 343 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; 344 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) 345 ecryptfs_set_default_sizes(crypt_stat); 346 rc = ecryptfs_read_and_validate_header_region(page_virt, 347 dentry->d_inode); 348 if (rc) { 349 rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry); 350 if (rc) { 351 printk(KERN_DEBUG "Valid metadata not found in header " 352 "region or xattr region; treating file as " 353 "unencrypted\n"); 354 rc = 0; 355 kmem_cache_free(ecryptfs_header_cache_2, page_virt); 356 goto out; 357 } 358 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 359 } 360 mount_crypt_stat = &ecryptfs_superblock_to_private( 361 dentry->d_sb)->mount_crypt_stat; 362 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) { 363 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 364 file_size = (crypt_stat->num_header_bytes_at_front 365 + i_size_read(lower_dentry->d_inode)); 366 else 367 file_size = i_size_read(lower_dentry->d_inode); 368 } else { 369 file_size = get_unaligned_be64(page_virt); 370 } 371 i_size_write(dentry->d_inode, (loff_t)file_size); 372 kmem_cache_free(ecryptfs_header_cache_2, page_virt); 373 goto out; 374 375 out_dput: 376 dput(lower_dentry); 377 d_drop(dentry); 378 out: 379 return ERR_PTR(rc); 380 } 381 382 static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir, 383 struct dentry *new_dentry) 384 { 385 struct dentry *lower_old_dentry; 386 struct dentry *lower_new_dentry; 387 struct dentry *lower_dir_dentry; 388 u64 file_size_save; 389 int rc; 390 391 file_size_save = i_size_read(old_dentry->d_inode); 392 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry); 393 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry); 394 dget(lower_old_dentry); 395 dget(lower_new_dentry); 396 lower_dir_dentry = lock_parent(lower_new_dentry); 397 rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode, 398 lower_new_dentry); 399 if (rc || !lower_new_dentry->d_inode) 400 goto out_lock; 401 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0); 402 if (rc) 403 goto out_lock; 404 fsstack_copy_attr_times(dir, lower_new_dentry->d_inode); 405 fsstack_copy_inode_size(dir, lower_new_dentry->d_inode); 406 old_dentry->d_inode->i_nlink = 407 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink; 408 i_size_write(new_dentry->d_inode, file_size_save); 409 out_lock: 410 unlock_dir(lower_dir_dentry); 411 dput(lower_new_dentry); 412 dput(lower_old_dentry); 413 d_drop(lower_old_dentry); 414 d_drop(new_dentry); 415 d_drop(old_dentry); 416 return rc; 417 } 418 419 static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry) 420 { 421 int rc = 0; 422 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 423 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir); 424 struct dentry *lower_dir_dentry; 425 426 lower_dir_dentry = lock_parent(lower_dentry); 427 rc = vfs_unlink(lower_dir_inode, lower_dentry); 428 if (rc) { 429 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc); 430 goto out_unlock; 431 } 432 fsstack_copy_attr_times(dir, lower_dir_inode); 433 dentry->d_inode->i_nlink = 434 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink; 435 dentry->d_inode->i_ctime = dir->i_ctime; 436 d_drop(dentry); 437 out_unlock: 438 unlock_dir(lower_dir_dentry); 439 return rc; 440 } 441 442 static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry, 443 const char *symname) 444 { 445 int rc; 446 struct dentry *lower_dentry; 447 struct dentry *lower_dir_dentry; 448 umode_t mode; 449 char *encoded_symname; 450 int encoded_symlen; 451 struct ecryptfs_crypt_stat *crypt_stat = NULL; 452 453 lower_dentry = ecryptfs_dentry_to_lower(dentry); 454 dget(lower_dentry); 455 lower_dir_dentry = lock_parent(lower_dentry); 456 mode = S_IALLUGO; 457 encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname, 458 strlen(symname), 459 &encoded_symname); 460 if (encoded_symlen < 0) { 461 rc = encoded_symlen; 462 goto out_lock; 463 } 464 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry, 465 encoded_symname, mode); 466 kfree(encoded_symname); 467 if (rc || !lower_dentry->d_inode) 468 goto out_lock; 469 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 470 if (rc) 471 goto out_lock; 472 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 473 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode); 474 out_lock: 475 unlock_dir(lower_dir_dentry); 476 dput(lower_dentry); 477 if (!dentry->d_inode) 478 d_drop(dentry); 479 return rc; 480 } 481 482 static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 483 { 484 int rc; 485 struct dentry *lower_dentry; 486 struct dentry *lower_dir_dentry; 487 488 lower_dentry = ecryptfs_dentry_to_lower(dentry); 489 lower_dir_dentry = lock_parent(lower_dentry); 490 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode); 491 if (rc || !lower_dentry->d_inode) 492 goto out; 493 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 494 if (rc) 495 goto out; 496 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 497 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode); 498 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink; 499 out: 500 unlock_dir(lower_dir_dentry); 501 if (!dentry->d_inode) 502 d_drop(dentry); 503 return rc; 504 } 505 506 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry) 507 { 508 struct dentry *lower_dentry; 509 struct dentry *lower_dir_dentry; 510 int rc; 511 512 lower_dentry = ecryptfs_dentry_to_lower(dentry); 513 dget(dentry); 514 lower_dir_dentry = lock_parent(lower_dentry); 515 dget(lower_dentry); 516 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry); 517 dput(lower_dentry); 518 if (!rc) 519 d_delete(lower_dentry); 520 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 521 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink; 522 unlock_dir(lower_dir_dentry); 523 if (!rc) 524 d_drop(dentry); 525 dput(dentry); 526 return rc; 527 } 528 529 static int 530 ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 531 { 532 int rc; 533 struct dentry *lower_dentry; 534 struct dentry *lower_dir_dentry; 535 536 lower_dentry = ecryptfs_dentry_to_lower(dentry); 537 lower_dir_dentry = lock_parent(lower_dentry); 538 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev); 539 if (rc || !lower_dentry->d_inode) 540 goto out; 541 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 542 ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE); 543 if (rc) 544 goto out; 545 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 546 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode); 547 out: 548 unlock_dir(lower_dir_dentry); 549 if (!dentry->d_inode) 550 d_drop(dentry); 551 return rc; 552 } 553 554 static int 555 ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry, 556 struct inode *new_dir, struct dentry *new_dentry) 557 { 558 int rc; 559 struct dentry *lower_old_dentry; 560 struct dentry *lower_new_dentry; 561 struct dentry *lower_old_dir_dentry; 562 struct dentry *lower_new_dir_dentry; 563 564 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry); 565 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry); 566 dget(lower_old_dentry); 567 dget(lower_new_dentry); 568 lower_old_dir_dentry = dget_parent(lower_old_dentry); 569 lower_new_dir_dentry = dget_parent(lower_new_dentry); 570 lock_rename(lower_old_dir_dentry, lower_new_dir_dentry); 571 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry, 572 lower_new_dir_dentry->d_inode, lower_new_dentry); 573 if (rc) 574 goto out_lock; 575 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL); 576 if (new_dir != old_dir) 577 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL); 578 out_lock: 579 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry); 580 dput(lower_new_dentry->d_parent); 581 dput(lower_old_dentry->d_parent); 582 dput(lower_new_dentry); 583 dput(lower_old_dentry); 584 return rc; 585 } 586 587 static int 588 ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz) 589 { 590 int rc; 591 struct dentry *lower_dentry; 592 char *decoded_name; 593 char *lower_buf; 594 mm_segment_t old_fs; 595 struct ecryptfs_crypt_stat *crypt_stat; 596 597 lower_dentry = ecryptfs_dentry_to_lower(dentry); 598 if (!lower_dentry->d_inode->i_op || 599 !lower_dentry->d_inode->i_op->readlink) { 600 rc = -EINVAL; 601 goto out; 602 } 603 /* Released in this function */ 604 lower_buf = kmalloc(bufsiz, GFP_KERNEL); 605 if (lower_buf == NULL) { 606 ecryptfs_printk(KERN_ERR, "Out of memory\n"); 607 rc = -ENOMEM; 608 goto out; 609 } 610 old_fs = get_fs(); 611 set_fs(get_ds()); 612 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ " 613 "lower_dentry->d_name.name = [%s]\n", 614 lower_dentry->d_name.name); 615 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry, 616 (char __user *)lower_buf, 617 bufsiz); 618 set_fs(old_fs); 619 if (rc >= 0) { 620 crypt_stat = NULL; 621 rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc, 622 &decoded_name); 623 if (rc == -ENOMEM) 624 goto out_free_lower_buf; 625 if (rc > 0) { 626 ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes " 627 "to userspace: [%*s]\n", rc, 628 decoded_name); 629 if (copy_to_user(buf, decoded_name, rc)) 630 rc = -EFAULT; 631 } 632 kfree(decoded_name); 633 fsstack_copy_attr_atime(dentry->d_inode, 634 lower_dentry->d_inode); 635 } 636 out_free_lower_buf: 637 kfree(lower_buf); 638 out: 639 return rc; 640 } 641 642 static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd) 643 { 644 char *buf; 645 int len = PAGE_SIZE, rc; 646 mm_segment_t old_fs; 647 648 /* Released in ecryptfs_put_link(); only release here on error */ 649 buf = kmalloc(len, GFP_KERNEL); 650 if (!buf) { 651 rc = -ENOMEM; 652 goto out; 653 } 654 old_fs = get_fs(); 655 set_fs(get_ds()); 656 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ " 657 "dentry->d_name.name = [%s]\n", dentry->d_name.name); 658 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len); 659 buf[rc] = '\0'; 660 set_fs(old_fs); 661 if (rc < 0) 662 goto out_free; 663 rc = 0; 664 nd_set_link(nd, buf); 665 goto out; 666 out_free: 667 kfree(buf); 668 out: 669 return ERR_PTR(rc); 670 } 671 672 static void 673 ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr) 674 { 675 /* Free the char* */ 676 kfree(nd_get_link(nd)); 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 = crypt_stat->num_header_bytes_at_front; 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 * ecryptfs_truncate 710 * @dentry: The ecryptfs layer dentry 711 * @new_length: The length to expand the file to 712 * 713 * Function to handle truncations modifying the size of the file. Note 714 * that the file sizes are interpolated. When expanding, we are simply 715 * writing strings of 0's out. When truncating, we need to modify the 716 * underlying file size according to the page index interpolations. 717 * 718 * Returns zero on success; non-zero otherwise 719 */ 720 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length) 721 { 722 int rc = 0; 723 struct inode *inode = dentry->d_inode; 724 struct dentry *lower_dentry; 725 struct file fake_ecryptfs_file; 726 struct ecryptfs_crypt_stat *crypt_stat; 727 loff_t i_size = i_size_read(inode); 728 loff_t lower_size_before_truncate; 729 loff_t lower_size_after_truncate; 730 731 if (unlikely((new_length == i_size))) 732 goto out; 733 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; 734 /* Set up a fake ecryptfs file, this is used to interface with 735 * the file in the underlying filesystem so that the 736 * truncation has an effect there as well. */ 737 memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file)); 738 fake_ecryptfs_file.f_path.dentry = dentry; 739 /* Released at out_free: label */ 740 ecryptfs_set_file_private(&fake_ecryptfs_file, 741 kmem_cache_alloc(ecryptfs_file_info_cache, 742 GFP_KERNEL)); 743 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) { 744 rc = -ENOMEM; 745 goto out; 746 } 747 lower_dentry = ecryptfs_dentry_to_lower(dentry); 748 ecryptfs_set_file_lower( 749 &fake_ecryptfs_file, 750 ecryptfs_inode_to_private(dentry->d_inode)->lower_file); 751 /* Switch on growing or shrinking file */ 752 if (new_length > i_size) { 753 char zero[] = { 0x00 }; 754 755 /* Write a single 0 at the last position of the file; 756 * this triggers code that will fill in 0's throughout 757 * the intermediate portion of the previous end of the 758 * file and the new and of the file */ 759 rc = ecryptfs_write(&fake_ecryptfs_file, zero, 760 (new_length - 1), 1); 761 } else { /* new_length < i_size_read(inode) */ 762 /* We're chopping off all the pages down do the page 763 * in which new_length is located. Fill in the end of 764 * that page from (new_length & ~PAGE_CACHE_MASK) to 765 * PAGE_CACHE_SIZE with zeros. */ 766 size_t num_zeros = (PAGE_CACHE_SIZE 767 - (new_length & ~PAGE_CACHE_MASK)); 768 769 if (num_zeros) { 770 char *zeros_virt; 771 772 zeros_virt = kzalloc(num_zeros, GFP_KERNEL); 773 if (!zeros_virt) { 774 rc = -ENOMEM; 775 goto out_free; 776 } 777 rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt, 778 new_length, num_zeros); 779 kfree(zeros_virt); 780 if (rc) { 781 printk(KERN_ERR "Error attempting to zero out " 782 "the remainder of the end page on " 783 "reducing truncate; rc = [%d]\n", rc); 784 goto out_free; 785 } 786 } 787 vmtruncate(inode, new_length); 788 rc = ecryptfs_write_inode_size_to_metadata(inode); 789 if (rc) { 790 printk(KERN_ERR "Problem with " 791 "ecryptfs_write_inode_size_to_metadata; " 792 "rc = [%d]\n", rc); 793 goto out_free; 794 } 795 /* We are reducing the size of the ecryptfs file, and need to 796 * know if we need to reduce the size of the lower file. */ 797 lower_size_before_truncate = 798 upper_size_to_lower_size(crypt_stat, i_size); 799 lower_size_after_truncate = 800 upper_size_to_lower_size(crypt_stat, new_length); 801 if (lower_size_after_truncate < lower_size_before_truncate) 802 vmtruncate(lower_dentry->d_inode, 803 lower_size_after_truncate); 804 } 805 out_free: 806 if (ecryptfs_file_to_private(&fake_ecryptfs_file)) 807 kmem_cache_free(ecryptfs_file_info_cache, 808 ecryptfs_file_to_private(&fake_ecryptfs_file)); 809 out: 810 return rc; 811 } 812 813 static int 814 ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd) 815 { 816 int rc; 817 818 if (nd) { 819 struct vfsmount *vfsmnt_save = nd->path.mnt; 820 struct dentry *dentry_save = nd->path.dentry; 821 822 nd->path.mnt = ecryptfs_dentry_to_lower_mnt(nd->path.dentry); 823 nd->path.dentry = ecryptfs_dentry_to_lower(nd->path.dentry); 824 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd); 825 nd->path.mnt = vfsmnt_save; 826 nd->path.dentry = dentry_save; 827 } else 828 rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL); 829 return rc; 830 } 831 832 /** 833 * ecryptfs_setattr 834 * @dentry: dentry handle to the inode to modify 835 * @ia: Structure with flags of what to change and values 836 * 837 * Updates the metadata of an inode. If the update is to the size 838 * i.e. truncation, then ecryptfs_truncate will handle the size modification 839 * of both the ecryptfs inode and the lower inode. 840 * 841 * All other metadata changes will be passed right to the lower filesystem, 842 * and we will just update our inode to look like the lower. 843 */ 844 static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia) 845 { 846 int rc = 0; 847 struct dentry *lower_dentry; 848 struct inode *inode; 849 struct inode *lower_inode; 850 struct ecryptfs_crypt_stat *crypt_stat; 851 852 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; 853 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) 854 ecryptfs_init_crypt_stat(crypt_stat); 855 inode = dentry->d_inode; 856 lower_inode = ecryptfs_inode_to_lower(inode); 857 lower_dentry = ecryptfs_dentry_to_lower(dentry); 858 mutex_lock(&crypt_stat->cs_mutex); 859 if (S_ISDIR(dentry->d_inode->i_mode)) 860 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 861 else if (S_ISREG(dentry->d_inode->i_mode) 862 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED) 863 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) { 864 struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 865 866 mount_crypt_stat = &ecryptfs_superblock_to_private( 867 dentry->d_sb)->mount_crypt_stat; 868 rc = ecryptfs_read_metadata(dentry); 869 if (rc) { 870 if (!(mount_crypt_stat->flags 871 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) { 872 rc = -EIO; 873 printk(KERN_WARNING "Either the lower file " 874 "is not in a valid eCryptfs format, " 875 "or the key could not be retrieved. " 876 "Plaintext passthrough mode is not " 877 "enabled; returning -EIO\n"); 878 mutex_unlock(&crypt_stat->cs_mutex); 879 goto out; 880 } 881 rc = 0; 882 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 883 mutex_unlock(&crypt_stat->cs_mutex); 884 goto out; 885 } 886 } 887 mutex_unlock(&crypt_stat->cs_mutex); 888 if (ia->ia_valid & ATTR_SIZE) { 889 ecryptfs_printk(KERN_DEBUG, 890 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n", 891 ia->ia_valid, ATTR_SIZE); 892 rc = ecryptfs_truncate(dentry, ia->ia_size); 893 /* ecryptfs_truncate handles resizing of the lower file */ 894 ia->ia_valid &= ~ATTR_SIZE; 895 ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n", 896 ia->ia_valid); 897 if (rc < 0) 898 goto out; 899 } 900 901 /* 902 * mode change is for clearing setuid/setgid bits. Allow lower fs 903 * to interpret this in its own way. 904 */ 905 if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) 906 ia->ia_valid &= ~ATTR_MODE; 907 908 mutex_lock(&lower_dentry->d_inode->i_mutex); 909 rc = notify_change(lower_dentry, ia); 910 mutex_unlock(&lower_dentry->d_inode->i_mutex); 911 out: 912 fsstack_copy_attr_all(inode, lower_inode, NULL); 913 return rc; 914 } 915 916 int 917 ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value, 918 size_t size, int flags) 919 { 920 int rc = 0; 921 struct dentry *lower_dentry; 922 923 lower_dentry = ecryptfs_dentry_to_lower(dentry); 924 if (!lower_dentry->d_inode->i_op->setxattr) { 925 rc = -ENOSYS; 926 goto out; 927 } 928 mutex_lock(&lower_dentry->d_inode->i_mutex); 929 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value, 930 size, flags); 931 mutex_unlock(&lower_dentry->d_inode->i_mutex); 932 out: 933 return rc; 934 } 935 936 ssize_t 937 ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name, 938 void *value, size_t size) 939 { 940 int rc = 0; 941 942 if (!lower_dentry->d_inode->i_op->getxattr) { 943 rc = -ENOSYS; 944 goto out; 945 } 946 mutex_lock(&lower_dentry->d_inode->i_mutex); 947 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value, 948 size); 949 mutex_unlock(&lower_dentry->d_inode->i_mutex); 950 out: 951 return rc; 952 } 953 954 static ssize_t 955 ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value, 956 size_t size) 957 { 958 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name, 959 value, size); 960 } 961 962 static ssize_t 963 ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size) 964 { 965 int rc = 0; 966 struct dentry *lower_dentry; 967 968 lower_dentry = ecryptfs_dentry_to_lower(dentry); 969 if (!lower_dentry->d_inode->i_op->listxattr) { 970 rc = -ENOSYS; 971 goto out; 972 } 973 mutex_lock(&lower_dentry->d_inode->i_mutex); 974 rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size); 975 mutex_unlock(&lower_dentry->d_inode->i_mutex); 976 out: 977 return rc; 978 } 979 980 static int ecryptfs_removexattr(struct dentry *dentry, const char *name) 981 { 982 int rc = 0; 983 struct dentry *lower_dentry; 984 985 lower_dentry = ecryptfs_dentry_to_lower(dentry); 986 if (!lower_dentry->d_inode->i_op->removexattr) { 987 rc = -ENOSYS; 988 goto out; 989 } 990 mutex_lock(&lower_dentry->d_inode->i_mutex); 991 rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name); 992 mutex_unlock(&lower_dentry->d_inode->i_mutex); 993 out: 994 return rc; 995 } 996 997 int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode) 998 { 999 if ((ecryptfs_inode_to_lower(inode) 1000 == (struct inode *)candidate_lower_inode)) 1001 return 1; 1002 else 1003 return 0; 1004 } 1005 1006 int ecryptfs_inode_set(struct inode *inode, void *lower_inode) 1007 { 1008 ecryptfs_init_inode(inode, (struct inode *)lower_inode); 1009 return 0; 1010 } 1011 1012 const struct inode_operations ecryptfs_symlink_iops = { 1013 .readlink = ecryptfs_readlink, 1014 .follow_link = ecryptfs_follow_link, 1015 .put_link = ecryptfs_put_link, 1016 .permission = ecryptfs_permission, 1017 .setattr = ecryptfs_setattr, 1018 .setxattr = ecryptfs_setxattr, 1019 .getxattr = ecryptfs_getxattr, 1020 .listxattr = ecryptfs_listxattr, 1021 .removexattr = ecryptfs_removexattr 1022 }; 1023 1024 const struct inode_operations ecryptfs_dir_iops = { 1025 .create = ecryptfs_create, 1026 .lookup = ecryptfs_lookup, 1027 .link = ecryptfs_link, 1028 .unlink = ecryptfs_unlink, 1029 .symlink = ecryptfs_symlink, 1030 .mkdir = ecryptfs_mkdir, 1031 .rmdir = ecryptfs_rmdir, 1032 .mknod = ecryptfs_mknod, 1033 .rename = ecryptfs_rename, 1034 .permission = ecryptfs_permission, 1035 .setattr = ecryptfs_setattr, 1036 .setxattr = ecryptfs_setxattr, 1037 .getxattr = ecryptfs_getxattr, 1038 .listxattr = ecryptfs_listxattr, 1039 .removexattr = ecryptfs_removexattr 1040 }; 1041 1042 const struct inode_operations ecryptfs_main_iops = { 1043 .permission = ecryptfs_permission, 1044 .setattr = ecryptfs_setattr, 1045 .setxattr = ecryptfs_setxattr, 1046 .getxattr = ecryptfs_getxattr, 1047 .listxattr = ecryptfs_listxattr, 1048 .removexattr = ecryptfs_removexattr 1049 }; 1050