inode.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. buf[rc] = '\0';
  642. set_fs(old_fs);
  643. if (rc < 0)
  644. goto out_free;
  645. rc = 0;
  646. nd_set_link(nd, buf);
  647. goto out;
  648. out_free:
  649. kfree(buf);
  650. out:
  651. return ERR_PTR(rc);
  652. }
  653. static void
  654. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  655. {
  656. /* Free the char* */
  657. kfree(nd_get_link(nd));
  658. }
  659. /**
  660. * upper_size_to_lower_size
  661. * @crypt_stat: Crypt_stat associated with file
  662. * @upper_size: Size of the upper file
  663. *
  664. * Calculate the required size of the lower file based on the
  665. * specified size of the upper file. This calculation is based on the
  666. * number of headers in the underlying file and the extent size.
  667. *
  668. * Returns Calculated size of the lower file.
  669. */
  670. static loff_t
  671. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  672. loff_t upper_size)
  673. {
  674. loff_t lower_size;
  675. lower_size = crypt_stat->num_header_bytes_at_front;
  676. if (upper_size != 0) {
  677. loff_t num_extents;
  678. num_extents = upper_size >> crypt_stat->extent_shift;
  679. if (upper_size & ~crypt_stat->extent_mask)
  680. num_extents++;
  681. lower_size += (num_extents * crypt_stat->extent_size);
  682. }
  683. return lower_size;
  684. }
  685. /**
  686. * ecryptfs_truncate
  687. * @dentry: The ecryptfs layer dentry
  688. * @new_length: The length to expand the file to
  689. *
  690. * Function to handle truncations modifying the size of the file. Note
  691. * that the file sizes are interpolated. When expanding, we are simply
  692. * writing strings of 0's out. When truncating, we need to modify the
  693. * underlying file size according to the page index interpolations.
  694. *
  695. * Returns zero on success; non-zero otherwise
  696. */
  697. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  698. {
  699. int rc = 0;
  700. struct inode *inode = dentry->d_inode;
  701. struct dentry *lower_dentry;
  702. struct file fake_ecryptfs_file;
  703. struct ecryptfs_crypt_stat *crypt_stat;
  704. loff_t i_size = i_size_read(inode);
  705. loff_t lower_size_before_truncate;
  706. loff_t lower_size_after_truncate;
  707. if (unlikely((new_length == i_size)))
  708. goto out;
  709. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  710. /* Set up a fake ecryptfs file, this is used to interface with
  711. * the file in the underlying filesystem so that the
  712. * truncation has an effect there as well. */
  713. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  714. fake_ecryptfs_file.f_path.dentry = dentry;
  715. /* Released at out_free: label */
  716. ecryptfs_set_file_private(&fake_ecryptfs_file,
  717. kmem_cache_alloc(ecryptfs_file_info_cache,
  718. GFP_KERNEL));
  719. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  720. rc = -ENOMEM;
  721. goto out;
  722. }
  723. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  724. ecryptfs_set_file_lower(
  725. &fake_ecryptfs_file,
  726. ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
  727. /* Switch on growing or shrinking file */
  728. if (new_length > i_size) {
  729. char zero[] = { 0x00 };
  730. /* Write a single 0 at the last position of the file;
  731. * this triggers code that will fill in 0's throughout
  732. * the intermediate portion of the previous end of the
  733. * file and the new and of the file */
  734. rc = ecryptfs_write(&fake_ecryptfs_file, zero,
  735. (new_length - 1), 1);
  736. } else { /* new_length < i_size_read(inode) */
  737. /* We're chopping off all the pages down do the page
  738. * in which new_length is located. Fill in the end of
  739. * that page from (new_length & ~PAGE_CACHE_MASK) to
  740. * PAGE_CACHE_SIZE with zeros. */
  741. size_t num_zeros = (PAGE_CACHE_SIZE
  742. - (new_length & ~PAGE_CACHE_MASK));
  743. if (num_zeros) {
  744. char *zeros_virt;
  745. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  746. if (!zeros_virt) {
  747. rc = -ENOMEM;
  748. goto out_free;
  749. }
  750. rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
  751. new_length, num_zeros);
  752. kfree(zeros_virt);
  753. if (rc) {
  754. printk(KERN_ERR "Error attempting to zero out "
  755. "the remainder of the end page on "
  756. "reducing truncate; rc = [%d]\n", rc);
  757. goto out_free;
  758. }
  759. }
  760. vmtruncate(inode, new_length);
  761. rc = ecryptfs_write_inode_size_to_metadata(inode);
  762. if (rc) {
  763. printk(KERN_ERR "Problem with "
  764. "ecryptfs_write_inode_size_to_metadata; "
  765. "rc = [%d]\n", rc);
  766. goto out_free;
  767. }
  768. /* We are reducing the size of the ecryptfs file, and need to
  769. * know if we need to reduce the size of the lower file. */
  770. lower_size_before_truncate =
  771. upper_size_to_lower_size(crypt_stat, i_size);
  772. lower_size_after_truncate =
  773. upper_size_to_lower_size(crypt_stat, new_length);
  774. if (lower_size_after_truncate < lower_size_before_truncate)
  775. vmtruncate(lower_dentry->d_inode,
  776. lower_size_after_truncate);
  777. }
  778. out_free:
  779. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  780. kmem_cache_free(ecryptfs_file_info_cache,
  781. ecryptfs_file_to_private(&fake_ecryptfs_file));
  782. out:
  783. return rc;
  784. }
  785. static int
  786. ecryptfs_permission(struct inode *inode, int mask)
  787. {
  788. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  789. }
  790. /**
  791. * ecryptfs_setattr
  792. * @dentry: dentry handle to the inode to modify
  793. * @ia: Structure with flags of what to change and values
  794. *
  795. * Updates the metadata of an inode. If the update is to the size
  796. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  797. * of both the ecryptfs inode and the lower inode.
  798. *
  799. * All other metadata changes will be passed right to the lower filesystem,
  800. * and we will just update our inode to look like the lower.
  801. */
  802. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  803. {
  804. int rc = 0;
  805. struct dentry *lower_dentry;
  806. struct inode *inode;
  807. struct inode *lower_inode;
  808. struct ecryptfs_crypt_stat *crypt_stat;
  809. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  810. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  811. ecryptfs_init_crypt_stat(crypt_stat);
  812. inode = dentry->d_inode;
  813. lower_inode = ecryptfs_inode_to_lower(inode);
  814. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  815. mutex_lock(&crypt_stat->cs_mutex);
  816. if (S_ISDIR(dentry->d_inode->i_mode))
  817. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  818. else if (S_ISREG(dentry->d_inode->i_mode)
  819. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  820. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  821. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  822. mount_crypt_stat = &ecryptfs_superblock_to_private(
  823. dentry->d_sb)->mount_crypt_stat;
  824. rc = ecryptfs_read_metadata(dentry);
  825. if (rc) {
  826. if (!(mount_crypt_stat->flags
  827. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  828. rc = -EIO;
  829. printk(KERN_WARNING "Either the lower file "
  830. "is not in a valid eCryptfs format, "
  831. "or the key could not be retrieved. "
  832. "Plaintext passthrough mode is not "
  833. "enabled; returning -EIO\n");
  834. mutex_unlock(&crypt_stat->cs_mutex);
  835. goto out;
  836. }
  837. rc = 0;
  838. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  839. mutex_unlock(&crypt_stat->cs_mutex);
  840. goto out;
  841. }
  842. }
  843. mutex_unlock(&crypt_stat->cs_mutex);
  844. if (ia->ia_valid & ATTR_SIZE) {
  845. ecryptfs_printk(KERN_DEBUG,
  846. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  847. ia->ia_valid, ATTR_SIZE);
  848. rc = ecryptfs_truncate(dentry, ia->ia_size);
  849. /* ecryptfs_truncate handles resizing of the lower file */
  850. ia->ia_valid &= ~ATTR_SIZE;
  851. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  852. ia->ia_valid);
  853. if (rc < 0)
  854. goto out;
  855. }
  856. /*
  857. * mode change is for clearing setuid/setgid bits. Allow lower fs
  858. * to interpret this in its own way.
  859. */
  860. if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  861. ia->ia_valid &= ~ATTR_MODE;
  862. mutex_lock(&lower_dentry->d_inode->i_mutex);
  863. rc = notify_change(lower_dentry, ia);
  864. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  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. static 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. };