inode.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 <asm/unaligned.h>
  34. #include "ecryptfs_kernel.h"
  35. static struct dentry *lock_parent(struct dentry *dentry)
  36. {
  37. struct dentry *dir;
  38. dir = dget_parent(dentry);
  39. mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
  40. return dir;
  41. }
  42. static void unlock_dir(struct dentry *dir)
  43. {
  44. mutex_unlock(&dir->d_inode->i_mutex);
  45. dput(dir);
  46. }
  47. /**
  48. * ecryptfs_create_underlying_file
  49. * @lower_dir_inode: inode of the parent in the lower fs of the new file
  50. * @dentry: New file's dentry
  51. * @mode: The mode of the new file
  52. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  53. *
  54. * Creates the file in the lower file system.
  55. *
  56. * Returns zero on success; non-zero on error condition
  57. */
  58. static int
  59. ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
  60. struct dentry *dentry, int mode,
  61. struct nameidata *nd)
  62. {
  63. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  64. struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  65. struct dentry *dentry_save;
  66. struct vfsmount *vfsmount_save;
  67. int rc;
  68. dentry_save = nd->path.dentry;
  69. vfsmount_save = nd->path.mnt;
  70. nd->path.dentry = lower_dentry;
  71. nd->path.mnt = lower_mnt;
  72. rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
  73. nd->path.dentry = dentry_save;
  74. nd->path.mnt = vfsmount_save;
  75. return rc;
  76. }
  77. /**
  78. * ecryptfs_do_create
  79. * @directory_inode: inode of the new file's dentry's parent in ecryptfs
  80. * @ecryptfs_dentry: New file's dentry in ecryptfs
  81. * @mode: The mode of the new file
  82. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  83. *
  84. * Creates the underlying file and the eCryptfs inode which will link to
  85. * it. It will also update the eCryptfs directory inode to mimic the
  86. * stat of the lower directory inode.
  87. *
  88. * Returns zero on success; non-zero on error condition
  89. */
  90. static int
  91. ecryptfs_do_create(struct inode *directory_inode,
  92. struct dentry *ecryptfs_dentry, int mode,
  93. struct nameidata *nd)
  94. {
  95. int rc;
  96. struct dentry *lower_dentry;
  97. struct dentry *lower_dir_dentry;
  98. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  99. lower_dir_dentry = lock_parent(lower_dentry);
  100. if (IS_ERR(lower_dir_dentry)) {
  101. ecryptfs_printk(KERN_ERR, "Error locking directory of "
  102. "dentry\n");
  103. rc = PTR_ERR(lower_dir_dentry);
  104. goto out;
  105. }
  106. rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
  107. ecryptfs_dentry, mode, nd);
  108. if (rc) {
  109. printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
  110. "rc = [%d]\n", __func__, rc);
  111. goto out_lock;
  112. }
  113. rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
  114. directory_inode->i_sb, 0);
  115. if (rc) {
  116. ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
  117. goto out_lock;
  118. }
  119. fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
  120. fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
  121. out_lock:
  122. unlock_dir(lower_dir_dentry);
  123. out:
  124. return rc;
  125. }
  126. /**
  127. * grow_file
  128. * @ecryptfs_dentry: the eCryptfs dentry
  129. *
  130. * This is the code which will grow the file to its correct size.
  131. */
  132. static int grow_file(struct dentry *ecryptfs_dentry)
  133. {
  134. struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
  135. struct file fake_file;
  136. struct ecryptfs_file_info tmp_file_info;
  137. char zero_virt[] = { 0x00 };
  138. int rc = 0;
  139. memset(&fake_file, 0, sizeof(fake_file));
  140. fake_file.f_path.dentry = ecryptfs_dentry;
  141. memset(&tmp_file_info, 0, sizeof(tmp_file_info));
  142. ecryptfs_set_file_private(&fake_file, &tmp_file_info);
  143. ecryptfs_set_file_lower(
  144. &fake_file,
  145. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file);
  146. rc = ecryptfs_write(&fake_file, zero_virt, 0, 1);
  147. i_size_write(ecryptfs_inode, 0);
  148. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  149. ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
  150. ECRYPTFS_NEW_FILE;
  151. return rc;
  152. }
  153. /**
  154. * ecryptfs_initialize_file
  155. *
  156. * Cause the file to be changed from a basic empty file to an ecryptfs
  157. * file with a header and first data page.
  158. *
  159. * Returns zero on success
  160. */
  161. static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
  162. {
  163. struct ecryptfs_crypt_stat *crypt_stat =
  164. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  165. int rc = 0;
  166. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  167. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  168. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  169. goto out;
  170. }
  171. crypt_stat->flags |= ECRYPTFS_NEW_FILE;
  172. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  173. rc = ecryptfs_new_file_context(ecryptfs_dentry);
  174. if (rc) {
  175. ecryptfs_printk(KERN_ERR, "Error creating new file "
  176. "context; rc = [%d]\n", rc);
  177. goto out;
  178. }
  179. if (!ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->lower_file) {
  180. rc = ecryptfs_init_persistent_file(ecryptfs_dentry);
  181. if (rc) {
  182. printk(KERN_ERR "%s: Error attempting to initialize "
  183. "the persistent file for the dentry with name "
  184. "[%s]; rc = [%d]\n", __func__,
  185. ecryptfs_dentry->d_name.name, rc);
  186. goto out;
  187. }
  188. }
  189. rc = ecryptfs_write_metadata(ecryptfs_dentry);
  190. if (rc) {
  191. printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
  192. goto out;
  193. }
  194. rc = grow_file(ecryptfs_dentry);
  195. if (rc)
  196. printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
  197. out:
  198. return rc;
  199. }
  200. /**
  201. * ecryptfs_create
  202. * @dir: The inode of the directory in which to create the file.
  203. * @dentry: The eCryptfs dentry
  204. * @mode: The mode of the new file.
  205. * @nd: nameidata
  206. *
  207. * Creates a new file.
  208. *
  209. * Returns zero on success; non-zero on error condition
  210. */
  211. static int
  212. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  213. int mode, struct nameidata *nd)
  214. {
  215. int rc;
  216. /* ecryptfs_do_create() calls ecryptfs_interpose() */
  217. rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
  218. if (unlikely(rc)) {
  219. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  220. "lower filesystem\n");
  221. goto out;
  222. }
  223. /* At this point, a file exists on "disk"; we need to make sure
  224. * that this on disk file is prepared to be an ecryptfs file */
  225. rc = ecryptfs_initialize_file(ecryptfs_dentry);
  226. out:
  227. return rc;
  228. }
  229. /**
  230. * ecryptfs_lookup_and_interpose_lower - Perform a lookup
  231. */
  232. int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry,
  233. struct dentry *lower_dentry,
  234. struct inode *ecryptfs_dir_inode,
  235. struct nameidata *ecryptfs_nd)
  236. {
  237. struct dentry *lower_dir_dentry;
  238. struct vfsmount *lower_mnt;
  239. struct inode *lower_inode;
  240. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  241. struct ecryptfs_crypt_stat *crypt_stat;
  242. char *page_virt = NULL;
  243. u64 file_size;
  244. int rc = 0;
  245. lower_dir_dentry = lower_dentry->d_parent;
  246. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(
  247. ecryptfs_dentry->d_parent));
  248. lower_inode = lower_dentry->d_inode;
  249. fsstack_copy_attr_atime(ecryptfs_dir_inode, lower_dir_dentry->d_inode);
  250. BUG_ON(!atomic_read(&lower_dentry->d_count));
  251. ecryptfs_set_dentry_private(ecryptfs_dentry,
  252. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  253. GFP_KERNEL));
  254. if (!ecryptfs_dentry_to_private(ecryptfs_dentry)) {
  255. rc = -ENOMEM;
  256. printk(KERN_ERR "%s: Out of memory whilst attempting "
  257. "to allocate ecryptfs_dentry_info struct\n",
  258. __func__);
  259. goto out_dput;
  260. }
  261. ecryptfs_set_dentry_lower(ecryptfs_dentry, lower_dentry);
  262. ecryptfs_set_dentry_lower_mnt(ecryptfs_dentry, lower_mnt);
  263. if (!lower_dentry->d_inode) {
  264. /* We want to add because we couldn't find in lower */
  265. d_add(ecryptfs_dentry, NULL);
  266. goto out;
  267. }
  268. rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
  269. ecryptfs_dir_inode->i_sb, 1);
  270. if (rc) {
  271. printk(KERN_ERR "%s: Error interposing; rc = [%d]\n",
  272. __func__, rc);
  273. goto out;
  274. }
  275. if (S_ISDIR(lower_inode->i_mode))
  276. goto out;
  277. if (S_ISLNK(lower_inode->i_mode))
  278. goto out;
  279. if (special_file(lower_inode->i_mode))
  280. goto out;
  281. if (!ecryptfs_nd)
  282. goto out;
  283. /* Released in this function */
  284. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER);
  285. if (!page_virt) {
  286. printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n",
  287. __func__);
  288. rc = -ENOMEM;
  289. goto out;
  290. }
  291. if (!ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->lower_file) {
  292. rc = ecryptfs_init_persistent_file(ecryptfs_dentry);
  293. if (rc) {
  294. printk(KERN_ERR "%s: Error attempting to initialize "
  295. "the persistent file for the dentry with name "
  296. "[%s]; rc = [%d]\n", __func__,
  297. ecryptfs_dentry->d_name.name, rc);
  298. goto out_free_kmem;
  299. }
  300. }
  301. crypt_stat = &ecryptfs_inode_to_private(
  302. ecryptfs_dentry->d_inode)->crypt_stat;
  303. /* TODO: lock for crypt_stat comparison */
  304. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  305. ecryptfs_set_default_sizes(crypt_stat);
  306. rc = ecryptfs_read_and_validate_header_region(page_virt,
  307. ecryptfs_dentry->d_inode);
  308. if (rc) {
  309. rc = ecryptfs_read_and_validate_xattr_region(page_virt,
  310. ecryptfs_dentry);
  311. if (rc) {
  312. rc = 0;
  313. goto out_free_kmem;
  314. }
  315. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  316. }
  317. mount_crypt_stat = &ecryptfs_superblock_to_private(
  318. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  319. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  320. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  321. file_size = (crypt_stat->num_header_bytes_at_front
  322. + i_size_read(lower_dentry->d_inode));
  323. else
  324. file_size = i_size_read(lower_dentry->d_inode);
  325. } else {
  326. file_size = get_unaligned_be64(page_virt);
  327. }
  328. i_size_write(ecryptfs_dentry->d_inode, (loff_t)file_size);
  329. out_free_kmem:
  330. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  331. goto out;
  332. out_dput:
  333. dput(lower_dentry);
  334. d_drop(ecryptfs_dentry);
  335. out:
  336. return rc;
  337. }
  338. /**
  339. * ecryptfs_lookup
  340. * @ecryptfs_dir_inode: The eCryptfs directory inode
  341. * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
  342. * @ecryptfs_nd: nameidata; may be NULL
  343. *
  344. * Find a file on disk. If the file does not exist, then we'll add it to the
  345. * dentry cache and continue on to read it from the disk.
  346. */
  347. static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
  348. struct dentry *ecryptfs_dentry,
  349. struct nameidata *ecryptfs_nd)
  350. {
  351. char *encrypted_and_encoded_name = NULL;
  352. size_t encrypted_and_encoded_name_size;
  353. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  354. struct dentry *lower_dir_dentry, *lower_dentry;
  355. int rc = 0;
  356. ecryptfs_dentry->d_op = &ecryptfs_dops;
  357. if ((ecryptfs_dentry->d_name.len == 1
  358. && !strcmp(ecryptfs_dentry->d_name.name, "."))
  359. || (ecryptfs_dentry->d_name.len == 2
  360. && !strcmp(ecryptfs_dentry->d_name.name, ".."))) {
  361. goto out_d_drop;
  362. }
  363. lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
  364. lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
  365. lower_dir_dentry,
  366. ecryptfs_dentry->d_name.len);
  367. if (IS_ERR(lower_dentry)) {
  368. rc = PTR_ERR(lower_dentry);
  369. printk(KERN_ERR "%s: lookup_one_len() returned [%d] on "
  370. "lower_dentry = [%s]\n", __func__, rc,
  371. ecryptfs_dentry->d_name.name);
  372. goto out_d_drop;
  373. }
  374. if (lower_dentry->d_inode)
  375. goto lookup_and_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 lookup_and_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. lower_dentry = lookup_one_len(encrypted_and_encoded_name,
  392. lower_dir_dentry,
  393. encrypted_and_encoded_name_size - 1);
  394. if (IS_ERR(lower_dentry)) {
  395. rc = PTR_ERR(lower_dentry);
  396. printk(KERN_ERR "%s: lookup_one_len() returned [%d] on "
  397. "lower_dentry = [%s]\n", __func__, rc,
  398. encrypted_and_encoded_name);
  399. goto out_d_drop;
  400. }
  401. lookup_and_interpose:
  402. rc = ecryptfs_lookup_and_interpose_lower(ecryptfs_dentry, lower_dentry,
  403. ecryptfs_dir_inode,
  404. ecryptfs_nd);
  405. goto out;
  406. out_d_drop:
  407. d_drop(ecryptfs_dentry);
  408. out:
  409. kfree(encrypted_and_encoded_name);
  410. return ERR_PTR(rc);
  411. }
  412. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  413. struct dentry *new_dentry)
  414. {
  415. struct dentry *lower_old_dentry;
  416. struct dentry *lower_new_dentry;
  417. struct dentry *lower_dir_dentry;
  418. u64 file_size_save;
  419. int rc;
  420. file_size_save = i_size_read(old_dentry->d_inode);
  421. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  422. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  423. dget(lower_old_dentry);
  424. dget(lower_new_dentry);
  425. lower_dir_dentry = lock_parent(lower_new_dentry);
  426. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  427. lower_new_dentry);
  428. if (rc || !lower_new_dentry->d_inode)
  429. goto out_lock;
  430. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
  431. if (rc)
  432. goto out_lock;
  433. fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
  434. fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
  435. old_dentry->d_inode->i_nlink =
  436. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
  437. i_size_write(new_dentry->d_inode, file_size_save);
  438. out_lock:
  439. unlock_dir(lower_dir_dentry);
  440. dput(lower_new_dentry);
  441. dput(lower_old_dentry);
  442. d_drop(lower_old_dentry);
  443. d_drop(new_dentry);
  444. d_drop(old_dentry);
  445. return rc;
  446. }
  447. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  448. {
  449. int rc = 0;
  450. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  451. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  452. struct dentry *lower_dir_dentry;
  453. lower_dir_dentry = lock_parent(lower_dentry);
  454. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  455. if (rc) {
  456. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  457. goto out_unlock;
  458. }
  459. fsstack_copy_attr_times(dir, lower_dir_inode);
  460. dentry->d_inode->i_nlink =
  461. ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
  462. dentry->d_inode->i_ctime = dir->i_ctime;
  463. d_drop(dentry);
  464. out_unlock:
  465. unlock_dir(lower_dir_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, 0);
  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, 0);
  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)
  540. d_delete(lower_dentry);
  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, 0);
  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. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  581. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  582. dget(lower_old_dentry);
  583. dget(lower_new_dentry);
  584. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  585. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  586. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  587. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  588. lower_new_dir_dentry->d_inode, lower_new_dentry);
  589. if (rc)
  590. goto out_lock;
  591. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
  592. if (new_dir != old_dir)
  593. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
  594. out_lock:
  595. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  596. dput(lower_new_dentry->d_parent);
  597. dput(lower_old_dentry->d_parent);
  598. dput(lower_new_dentry);
  599. dput(lower_old_dentry);
  600. return rc;
  601. }
  602. static int
  603. ecryptfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  604. {
  605. char *lower_buf;
  606. struct dentry *lower_dentry;
  607. struct ecryptfs_crypt_stat *crypt_stat;
  608. char *plaintext_name;
  609. size_t plaintext_name_size;
  610. mm_segment_t old_fs;
  611. int rc;
  612. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  613. if (!lower_dentry->d_inode->i_op->readlink) {
  614. rc = -EINVAL;
  615. goto out;
  616. }
  617. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  618. /* Released in this function */
  619. lower_buf = kmalloc(bufsiz, GFP_KERNEL);
  620. if (lower_buf == NULL) {
  621. printk(KERN_ERR "%s: Out of memory whilst attempting to "
  622. "kmalloc [%d] bytes\n", __func__, bufsiz);
  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. bufsiz);
  631. set_fs(old_fs);
  632. if (rc >= 0) {
  633. rc = ecryptfs_decode_and_decrypt_filename(&plaintext_name,
  634. &plaintext_name_size,
  635. dentry, lower_buf,
  636. rc);
  637. if (rc) {
  638. printk(KERN_ERR "%s: Error attempting to decode and "
  639. "decrypt filename; rc = [%d]\n", __func__,
  640. rc);
  641. goto out_free_lower_buf;
  642. }
  643. rc = copy_to_user(buf, plaintext_name, plaintext_name_size);
  644. if (rc)
  645. rc = -EFAULT;
  646. else
  647. rc = plaintext_name_size;
  648. kfree(plaintext_name);
  649. fsstack_copy_attr_atime(dentry->d_inode, lower_dentry->d_inode);
  650. }
  651. out_free_lower_buf:
  652. kfree(lower_buf);
  653. out:
  654. return rc;
  655. }
  656. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  657. {
  658. char *buf;
  659. int len = PAGE_SIZE, rc;
  660. mm_segment_t old_fs;
  661. /* Released in ecryptfs_put_link(); only release here on error */
  662. buf = kmalloc(len, GFP_KERNEL);
  663. if (!buf) {
  664. rc = -ENOMEM;
  665. goto out;
  666. }
  667. old_fs = get_fs();
  668. set_fs(get_ds());
  669. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  670. set_fs(old_fs);
  671. if (rc < 0)
  672. goto out_free;
  673. else
  674. buf[rc] = '\0';
  675. rc = 0;
  676. nd_set_link(nd, buf);
  677. goto out;
  678. out_free:
  679. kfree(buf);
  680. out:
  681. return ERR_PTR(rc);
  682. }
  683. static void
  684. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  685. {
  686. /* Free the char* */
  687. kfree(nd_get_link(nd));
  688. }
  689. /**
  690. * upper_size_to_lower_size
  691. * @crypt_stat: Crypt_stat associated with file
  692. * @upper_size: Size of the upper file
  693. *
  694. * Calculate the required size of the lower file based on the
  695. * specified size of the upper file. This calculation is based on the
  696. * number of headers in the underlying file and the extent size.
  697. *
  698. * Returns Calculated size of the lower file.
  699. */
  700. static loff_t
  701. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  702. loff_t upper_size)
  703. {
  704. loff_t lower_size;
  705. lower_size = crypt_stat->num_header_bytes_at_front;
  706. if (upper_size != 0) {
  707. loff_t num_extents;
  708. num_extents = upper_size >> crypt_stat->extent_shift;
  709. if (upper_size & ~crypt_stat->extent_mask)
  710. num_extents++;
  711. lower_size += (num_extents * crypt_stat->extent_size);
  712. }
  713. return lower_size;
  714. }
  715. /**
  716. * ecryptfs_truncate
  717. * @dentry: The ecryptfs layer dentry
  718. * @new_length: The length to expand the file to
  719. *
  720. * Function to handle truncations modifying the size of the file. Note
  721. * that the file sizes are interpolated. When expanding, we are simply
  722. * writing strings of 0's out. When truncating, we need to modify the
  723. * underlying file size according to the page index interpolations.
  724. *
  725. * Returns zero on success; non-zero otherwise
  726. */
  727. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  728. {
  729. int rc = 0;
  730. struct inode *inode = dentry->d_inode;
  731. struct dentry *lower_dentry;
  732. struct file fake_ecryptfs_file;
  733. struct ecryptfs_crypt_stat *crypt_stat;
  734. loff_t i_size = i_size_read(inode);
  735. loff_t lower_size_before_truncate;
  736. loff_t lower_size_after_truncate;
  737. if (unlikely((new_length == i_size)))
  738. goto out;
  739. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  740. /* Set up a fake ecryptfs file, this is used to interface with
  741. * the file in the underlying filesystem so that the
  742. * truncation has an effect there as well. */
  743. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  744. fake_ecryptfs_file.f_path.dentry = dentry;
  745. /* Released at out_free: label */
  746. ecryptfs_set_file_private(&fake_ecryptfs_file,
  747. kmem_cache_alloc(ecryptfs_file_info_cache,
  748. GFP_KERNEL));
  749. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  750. rc = -ENOMEM;
  751. goto out;
  752. }
  753. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  754. ecryptfs_set_file_lower(
  755. &fake_ecryptfs_file,
  756. ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
  757. /* Switch on growing or shrinking file */
  758. if (new_length > i_size) {
  759. char zero[] = { 0x00 };
  760. /* Write a single 0 at the last position of the file;
  761. * this triggers code that will fill in 0's throughout
  762. * the intermediate portion of the previous end of the
  763. * file and the new and of the file */
  764. rc = ecryptfs_write(&fake_ecryptfs_file, zero,
  765. (new_length - 1), 1);
  766. } else { /* new_length < i_size_read(inode) */
  767. /* We're chopping off all the pages down do the page
  768. * in which new_length is located. Fill in the end of
  769. * that page from (new_length & ~PAGE_CACHE_MASK) to
  770. * PAGE_CACHE_SIZE with zeros. */
  771. size_t num_zeros = (PAGE_CACHE_SIZE
  772. - (new_length & ~PAGE_CACHE_MASK));
  773. if (num_zeros) {
  774. char *zeros_virt;
  775. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  776. if (!zeros_virt) {
  777. rc = -ENOMEM;
  778. goto out_free;
  779. }
  780. rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
  781. new_length, num_zeros);
  782. kfree(zeros_virt);
  783. if (rc) {
  784. printk(KERN_ERR "Error attempting to zero out "
  785. "the remainder of the end page on "
  786. "reducing truncate; rc = [%d]\n", rc);
  787. goto out_free;
  788. }
  789. }
  790. vmtruncate(inode, new_length);
  791. rc = ecryptfs_write_inode_size_to_metadata(inode);
  792. if (rc) {
  793. printk(KERN_ERR "Problem with "
  794. "ecryptfs_write_inode_size_to_metadata; "
  795. "rc = [%d]\n", rc);
  796. goto out_free;
  797. }
  798. /* We are reducing the size of the ecryptfs file, and need to
  799. * know if we need to reduce the size of the lower file. */
  800. lower_size_before_truncate =
  801. upper_size_to_lower_size(crypt_stat, i_size);
  802. lower_size_after_truncate =
  803. upper_size_to_lower_size(crypt_stat, new_length);
  804. if (lower_size_after_truncate < lower_size_before_truncate)
  805. vmtruncate(lower_dentry->d_inode,
  806. lower_size_after_truncate);
  807. }
  808. out_free:
  809. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  810. kmem_cache_free(ecryptfs_file_info_cache,
  811. ecryptfs_file_to_private(&fake_ecryptfs_file));
  812. out:
  813. return rc;
  814. }
  815. static int
  816. ecryptfs_permission(struct inode *inode, int mask)
  817. {
  818. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  819. }
  820. /**
  821. * ecryptfs_setattr
  822. * @dentry: dentry handle to the inode to modify
  823. * @ia: Structure with flags of what to change and values
  824. *
  825. * Updates the metadata of an inode. If the update is to the size
  826. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  827. * of both the ecryptfs inode and the lower inode.
  828. *
  829. * All other metadata changes will be passed right to the lower filesystem,
  830. * and we will just update our inode to look like the lower.
  831. */
  832. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  833. {
  834. int rc = 0;
  835. struct dentry *lower_dentry;
  836. struct inode *inode;
  837. struct inode *lower_inode;
  838. struct ecryptfs_crypt_stat *crypt_stat;
  839. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  840. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  841. ecryptfs_init_crypt_stat(crypt_stat);
  842. inode = dentry->d_inode;
  843. lower_inode = ecryptfs_inode_to_lower(inode);
  844. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  845. mutex_lock(&crypt_stat->cs_mutex);
  846. if (S_ISDIR(dentry->d_inode->i_mode))
  847. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  848. else if (S_ISREG(dentry->d_inode->i_mode)
  849. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  850. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  851. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  852. mount_crypt_stat = &ecryptfs_superblock_to_private(
  853. dentry->d_sb)->mount_crypt_stat;
  854. rc = ecryptfs_read_metadata(dentry);
  855. if (rc) {
  856. if (!(mount_crypt_stat->flags
  857. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  858. rc = -EIO;
  859. printk(KERN_WARNING "Either the lower file "
  860. "is not in a valid eCryptfs format, "
  861. "or the key could not be retrieved. "
  862. "Plaintext passthrough mode is not "
  863. "enabled; returning -EIO\n");
  864. mutex_unlock(&crypt_stat->cs_mutex);
  865. goto out;
  866. }
  867. rc = 0;
  868. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  869. mutex_unlock(&crypt_stat->cs_mutex);
  870. goto out;
  871. }
  872. }
  873. mutex_unlock(&crypt_stat->cs_mutex);
  874. if (ia->ia_valid & ATTR_SIZE) {
  875. ecryptfs_printk(KERN_DEBUG,
  876. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  877. ia->ia_valid, ATTR_SIZE);
  878. rc = ecryptfs_truncate(dentry, ia->ia_size);
  879. /* ecryptfs_truncate handles resizing of the lower file */
  880. ia->ia_valid &= ~ATTR_SIZE;
  881. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  882. ia->ia_valid);
  883. if (rc < 0)
  884. goto out;
  885. }
  886. /*
  887. * mode change is for clearing setuid/setgid bits. Allow lower fs
  888. * to interpret this in its own way.
  889. */
  890. if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  891. ia->ia_valid &= ~ATTR_MODE;
  892. mutex_lock(&lower_dentry->d_inode->i_mutex);
  893. rc = notify_change(lower_dentry, ia);
  894. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  895. out:
  896. fsstack_copy_attr_all(inode, lower_inode, NULL);
  897. return rc;
  898. }
  899. int
  900. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  901. size_t size, int flags)
  902. {
  903. int rc = 0;
  904. struct dentry *lower_dentry;
  905. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  906. if (!lower_dentry->d_inode->i_op->setxattr) {
  907. rc = -ENOSYS;
  908. goto out;
  909. }
  910. mutex_lock(&lower_dentry->d_inode->i_mutex);
  911. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  912. size, flags);
  913. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  914. out:
  915. return rc;
  916. }
  917. ssize_t
  918. ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
  919. void *value, size_t size)
  920. {
  921. int rc = 0;
  922. if (!lower_dentry->d_inode->i_op->getxattr) {
  923. rc = -ENOSYS;
  924. goto out;
  925. }
  926. mutex_lock(&lower_dentry->d_inode->i_mutex);
  927. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  928. size);
  929. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  930. out:
  931. return rc;
  932. }
  933. static ssize_t
  934. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  935. size_t size)
  936. {
  937. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
  938. value, size);
  939. }
  940. static ssize_t
  941. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  942. {
  943. int rc = 0;
  944. struct dentry *lower_dentry;
  945. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  946. if (!lower_dentry->d_inode->i_op->listxattr) {
  947. rc = -ENOSYS;
  948. goto out;
  949. }
  950. mutex_lock(&lower_dentry->d_inode->i_mutex);
  951. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  952. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  953. out:
  954. return rc;
  955. }
  956. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  957. {
  958. int rc = 0;
  959. struct dentry *lower_dentry;
  960. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  961. if (!lower_dentry->d_inode->i_op->removexattr) {
  962. rc = -ENOSYS;
  963. goto out;
  964. }
  965. mutex_lock(&lower_dentry->d_inode->i_mutex);
  966. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  967. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  968. out:
  969. return rc;
  970. }
  971. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  972. {
  973. if ((ecryptfs_inode_to_lower(inode)
  974. == (struct inode *)candidate_lower_inode))
  975. return 1;
  976. else
  977. return 0;
  978. }
  979. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  980. {
  981. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  982. return 0;
  983. }
  984. const struct inode_operations ecryptfs_symlink_iops = {
  985. .readlink = ecryptfs_readlink,
  986. .follow_link = ecryptfs_follow_link,
  987. .put_link = ecryptfs_put_link,
  988. .permission = ecryptfs_permission,
  989. .setattr = ecryptfs_setattr,
  990. .setxattr = ecryptfs_setxattr,
  991. .getxattr = ecryptfs_getxattr,
  992. .listxattr = ecryptfs_listxattr,
  993. .removexattr = ecryptfs_removexattr
  994. };
  995. const struct inode_operations ecryptfs_dir_iops = {
  996. .create = ecryptfs_create,
  997. .lookup = ecryptfs_lookup,
  998. .link = ecryptfs_link,
  999. .unlink = ecryptfs_unlink,
  1000. .symlink = ecryptfs_symlink,
  1001. .mkdir = ecryptfs_mkdir,
  1002. .rmdir = ecryptfs_rmdir,
  1003. .mknod = ecryptfs_mknod,
  1004. .rename = ecryptfs_rename,
  1005. .permission = ecryptfs_permission,
  1006. .setattr = ecryptfs_setattr,
  1007. .setxattr = ecryptfs_setxattr,
  1008. .getxattr = ecryptfs_getxattr,
  1009. .listxattr = ecryptfs_listxattr,
  1010. .removexattr = ecryptfs_removexattr
  1011. };
  1012. const struct inode_operations ecryptfs_main_iops = {
  1013. .permission = ecryptfs_permission,
  1014. .setattr = ecryptfs_setattr,
  1015. .setxattr = ecryptfs_setxattr,
  1016. .getxattr = ecryptfs_getxattr,
  1017. .listxattr = ecryptfs_listxattr,
  1018. .removexattr = ecryptfs_removexattr
  1019. };