inode.c 31 KB

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