inode.c 31 KB

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