PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ecryptfs/inode.c

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