inode.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. umode_t mode;
  447. char *encoded_symname;
  448. int encoded_symlen;
  449. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  450. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  451. dget(lower_dentry);
  452. lower_dir_dentry = lock_parent(lower_dentry);
  453. mode = S_IALLUGO;
  454. encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
  455. strlen(symname),
  456. &encoded_symname);
  457. if (encoded_symlen < 0) {
  458. rc = encoded_symlen;
  459. goto out_lock;
  460. }
  461. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  462. encoded_symname, mode);
  463. kfree(encoded_symname);
  464. if (rc || !lower_dentry->d_inode)
  465. goto out_lock;
  466. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  467. if (rc)
  468. goto out_lock;
  469. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  470. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  471. out_lock:
  472. unlock_dir(lower_dir_dentry);
  473. dput(lower_dentry);
  474. if (!dentry->d_inode)
  475. d_drop(dentry);
  476. return rc;
  477. }
  478. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  479. {
  480. int rc;
  481. struct dentry *lower_dentry;
  482. struct dentry *lower_dir_dentry;
  483. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  484. lower_dir_dentry = lock_parent(lower_dentry);
  485. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  486. if (rc || !lower_dentry->d_inode)
  487. goto out;
  488. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  489. if (rc)
  490. goto out;
  491. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  492. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  493. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  494. out:
  495. unlock_dir(lower_dir_dentry);
  496. if (!dentry->d_inode)
  497. d_drop(dentry);
  498. return rc;
  499. }
  500. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  501. {
  502. struct dentry *lower_dentry;
  503. struct dentry *lower_dir_dentry;
  504. int rc;
  505. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  506. dget(dentry);
  507. lower_dir_dentry = lock_parent(lower_dentry);
  508. dget(lower_dentry);
  509. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  510. dput(lower_dentry);
  511. if (!rc)
  512. d_delete(lower_dentry);
  513. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  514. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  515. unlock_dir(lower_dir_dentry);
  516. if (!rc)
  517. d_drop(dentry);
  518. dput(dentry);
  519. return rc;
  520. }
  521. static int
  522. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  523. {
  524. int rc;
  525. struct dentry *lower_dentry;
  526. struct dentry *lower_dir_dentry;
  527. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  528. lower_dir_dentry = lock_parent(lower_dentry);
  529. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  530. if (rc || !lower_dentry->d_inode)
  531. goto out;
  532. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  533. if (rc)
  534. goto out;
  535. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  536. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  537. out:
  538. unlock_dir(lower_dir_dentry);
  539. if (!dentry->d_inode)
  540. d_drop(dentry);
  541. return rc;
  542. }
  543. static int
  544. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  545. struct inode *new_dir, struct dentry *new_dentry)
  546. {
  547. int rc;
  548. struct dentry *lower_old_dentry;
  549. struct dentry *lower_new_dentry;
  550. struct dentry *lower_old_dir_dentry;
  551. struct dentry *lower_new_dir_dentry;
  552. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  553. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  554. dget(lower_old_dentry);
  555. dget(lower_new_dentry);
  556. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  557. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  558. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  559. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  560. lower_new_dir_dentry->d_inode, lower_new_dentry);
  561. if (rc)
  562. goto out_lock;
  563. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
  564. if (new_dir != old_dir)
  565. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
  566. out_lock:
  567. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  568. dput(lower_new_dentry->d_parent);
  569. dput(lower_old_dentry->d_parent);
  570. dput(lower_new_dentry);
  571. dput(lower_old_dentry);
  572. return rc;
  573. }
  574. static int
  575. ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
  576. {
  577. int rc;
  578. struct dentry *lower_dentry;
  579. char *decoded_name;
  580. char *lower_buf;
  581. mm_segment_t old_fs;
  582. struct ecryptfs_crypt_stat *crypt_stat;
  583. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  584. if (!lower_dentry->d_inode->i_op ||
  585. !lower_dentry->d_inode->i_op->readlink) {
  586. rc = -EINVAL;
  587. goto out;
  588. }
  589. /* Released in this function */
  590. lower_buf = kmalloc(bufsiz, GFP_KERNEL);
  591. if (lower_buf == NULL) {
  592. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  593. rc = -ENOMEM;
  594. goto out;
  595. }
  596. old_fs = get_fs();
  597. set_fs(get_ds());
  598. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  599. "lower_dentry->d_name.name = [%s]\n",
  600. lower_dentry->d_name.name);
  601. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  602. (char __user *)lower_buf,
  603. bufsiz);
  604. set_fs(old_fs);
  605. if (rc >= 0) {
  606. crypt_stat = NULL;
  607. rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
  608. &decoded_name);
  609. if (rc == -ENOMEM)
  610. goto out_free_lower_buf;
  611. if (rc > 0) {
  612. ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
  613. "to userspace: [%*s]\n", rc,
  614. decoded_name);
  615. if (copy_to_user(buf, decoded_name, rc))
  616. rc = -EFAULT;
  617. }
  618. kfree(decoded_name);
  619. fsstack_copy_attr_atime(dentry->d_inode,
  620. lower_dentry->d_inode);
  621. }
  622. out_free_lower_buf:
  623. kfree(lower_buf);
  624. out:
  625. return rc;
  626. }
  627. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  628. {
  629. char *buf;
  630. int len = PAGE_SIZE, rc;
  631. mm_segment_t old_fs;
  632. /* Released in ecryptfs_put_link(); only release here on error */
  633. buf = kmalloc(len, GFP_KERNEL);
  634. if (!buf) {
  635. rc = -ENOMEM;
  636. goto out;
  637. }
  638. old_fs = get_fs();
  639. set_fs(get_ds());
  640. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  641. "dentry->d_name.name = [%s]\n", dentry->d_name.name);
  642. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  643. buf[rc] = '\0';
  644. set_fs(old_fs);
  645. if (rc < 0)
  646. goto out_free;
  647. rc = 0;
  648. nd_set_link(nd, buf);
  649. goto out;
  650. out_free:
  651. kfree(buf);
  652. out:
  653. return ERR_PTR(rc);
  654. }
  655. static void
  656. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  657. {
  658. /* Free the char* */
  659. kfree(nd_get_link(nd));
  660. }
  661. /**
  662. * upper_size_to_lower_size
  663. * @crypt_stat: Crypt_stat associated with file
  664. * @upper_size: Size of the upper file
  665. *
  666. * Calculate the required size of the lower file based on the
  667. * specified size of the upper file. This calculation is based on the
  668. * number of headers in the underlying file and the extent size.
  669. *
  670. * Returns Calculated size of the lower file.
  671. */
  672. static loff_t
  673. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  674. loff_t upper_size)
  675. {
  676. loff_t lower_size;
  677. lower_size = crypt_stat->num_header_bytes_at_front;
  678. if (upper_size != 0) {
  679. loff_t num_extents;
  680. num_extents = upper_size >> crypt_stat->extent_shift;
  681. if (upper_size & ~crypt_stat->extent_mask)
  682. num_extents++;
  683. lower_size += (num_extents * crypt_stat->extent_size);
  684. }
  685. return lower_size;
  686. }
  687. /**
  688. * ecryptfs_truncate
  689. * @dentry: The ecryptfs layer dentry
  690. * @new_length: The length to expand the file to
  691. *
  692. * Function to handle truncations modifying the size of the file. Note
  693. * that the file sizes are interpolated. When expanding, we are simply
  694. * writing strings of 0's out. When truncating, we need to modify the
  695. * underlying file size according to the page index interpolations.
  696. *
  697. * Returns zero on success; non-zero otherwise
  698. */
  699. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  700. {
  701. int rc = 0;
  702. struct inode *inode = dentry->d_inode;
  703. struct dentry *lower_dentry;
  704. struct file fake_ecryptfs_file;
  705. struct ecryptfs_crypt_stat *crypt_stat;
  706. loff_t i_size = i_size_read(inode);
  707. loff_t lower_size_before_truncate;
  708. loff_t lower_size_after_truncate;
  709. if (unlikely((new_length == i_size)))
  710. goto out;
  711. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  712. /* Set up a fake ecryptfs file, this is used to interface with
  713. * the file in the underlying filesystem so that the
  714. * truncation has an effect there as well. */
  715. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  716. fake_ecryptfs_file.f_path.dentry = dentry;
  717. /* Released at out_free: label */
  718. ecryptfs_set_file_private(&fake_ecryptfs_file,
  719. kmem_cache_alloc(ecryptfs_file_info_cache,
  720. GFP_KERNEL));
  721. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  722. rc = -ENOMEM;
  723. goto out;
  724. }
  725. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  726. ecryptfs_set_file_lower(
  727. &fake_ecryptfs_file,
  728. ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
  729. /* Switch on growing or shrinking file */
  730. if (new_length > i_size) {
  731. char zero[] = { 0x00 };
  732. /* Write a single 0 at the last position of the file;
  733. * this triggers code that will fill in 0's throughout
  734. * the intermediate portion of the previous end of the
  735. * file and the new and of the file */
  736. rc = ecryptfs_write(&fake_ecryptfs_file, zero,
  737. (new_length - 1), 1);
  738. } else { /* new_length < i_size_read(inode) */
  739. /* We're chopping off all the pages down do the page
  740. * in which new_length is located. Fill in the end of
  741. * that page from (new_length & ~PAGE_CACHE_MASK) to
  742. * PAGE_CACHE_SIZE with zeros. */
  743. size_t num_zeros = (PAGE_CACHE_SIZE
  744. - (new_length & ~PAGE_CACHE_MASK));
  745. if (num_zeros) {
  746. char *zeros_virt;
  747. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  748. if (!zeros_virt) {
  749. rc = -ENOMEM;
  750. goto out_free;
  751. }
  752. rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
  753. new_length, num_zeros);
  754. kfree(zeros_virt);
  755. if (rc) {
  756. printk(KERN_ERR "Error attempting to zero out "
  757. "the remainder of the end page on "
  758. "reducing truncate; rc = [%d]\n", rc);
  759. goto out_free;
  760. }
  761. }
  762. vmtruncate(inode, new_length);
  763. rc = ecryptfs_write_inode_size_to_metadata(inode);
  764. if (rc) {
  765. printk(KERN_ERR "Problem with "
  766. "ecryptfs_write_inode_size_to_metadata; "
  767. "rc = [%d]\n", rc);
  768. goto out_free;
  769. }
  770. /* We are reducing the size of the ecryptfs file, and need to
  771. * know if we need to reduce the size of the lower file. */
  772. lower_size_before_truncate =
  773. upper_size_to_lower_size(crypt_stat, i_size);
  774. lower_size_after_truncate =
  775. upper_size_to_lower_size(crypt_stat, new_length);
  776. if (lower_size_after_truncate < lower_size_before_truncate)
  777. vmtruncate(lower_dentry->d_inode,
  778. lower_size_after_truncate);
  779. }
  780. out_free:
  781. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  782. kmem_cache_free(ecryptfs_file_info_cache,
  783. ecryptfs_file_to_private(&fake_ecryptfs_file));
  784. out:
  785. return rc;
  786. }
  787. static int
  788. ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  789. {
  790. int rc;
  791. if (nd) {
  792. struct vfsmount *vfsmnt_save = nd->path.mnt;
  793. struct dentry *dentry_save = nd->path.dentry;
  794. nd->path.mnt = ecryptfs_dentry_to_lower_mnt(nd->path.dentry);
  795. nd->path.dentry = ecryptfs_dentry_to_lower(nd->path.dentry);
  796. rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
  797. nd->path.mnt = vfsmnt_save;
  798. nd->path.dentry = dentry_save;
  799. } else
  800. rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
  801. return rc;
  802. }
  803. /**
  804. * ecryptfs_setattr
  805. * @dentry: dentry handle to the inode to modify
  806. * @ia: Structure with flags of what to change and values
  807. *
  808. * Updates the metadata of an inode. If the update is to the size
  809. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  810. * of both the ecryptfs inode and the lower inode.
  811. *
  812. * All other metadata changes will be passed right to the lower filesystem,
  813. * and we will just update our inode to look like the lower.
  814. */
  815. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  816. {
  817. int rc = 0;
  818. struct dentry *lower_dentry;
  819. struct inode *inode;
  820. struct inode *lower_inode;
  821. struct ecryptfs_crypt_stat *crypt_stat;
  822. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  823. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  824. ecryptfs_init_crypt_stat(crypt_stat);
  825. inode = dentry->d_inode;
  826. lower_inode = ecryptfs_inode_to_lower(inode);
  827. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  828. mutex_lock(&crypt_stat->cs_mutex);
  829. if (S_ISDIR(dentry->d_inode->i_mode))
  830. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  831. else if (S_ISREG(dentry->d_inode->i_mode)
  832. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  833. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  834. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  835. mount_crypt_stat = &ecryptfs_superblock_to_private(
  836. dentry->d_sb)->mount_crypt_stat;
  837. rc = ecryptfs_read_metadata(dentry);
  838. if (rc) {
  839. if (!(mount_crypt_stat->flags
  840. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  841. rc = -EIO;
  842. printk(KERN_WARNING "Either the lower file "
  843. "is not in a valid eCryptfs format, "
  844. "or the key could not be retrieved. "
  845. "Plaintext passthrough mode is not "
  846. "enabled; returning -EIO\n");
  847. mutex_unlock(&crypt_stat->cs_mutex);
  848. goto out;
  849. }
  850. rc = 0;
  851. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  852. mutex_unlock(&crypt_stat->cs_mutex);
  853. goto out;
  854. }
  855. }
  856. mutex_unlock(&crypt_stat->cs_mutex);
  857. if (ia->ia_valid & ATTR_SIZE) {
  858. ecryptfs_printk(KERN_DEBUG,
  859. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  860. ia->ia_valid, ATTR_SIZE);
  861. rc = ecryptfs_truncate(dentry, ia->ia_size);
  862. /* ecryptfs_truncate handles resizing of the lower file */
  863. ia->ia_valid &= ~ATTR_SIZE;
  864. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  865. ia->ia_valid);
  866. if (rc < 0)
  867. goto out;
  868. }
  869. /*
  870. * mode change is for clearing setuid/setgid bits. Allow lower fs
  871. * to interpret this in its own way.
  872. */
  873. if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  874. ia->ia_valid &= ~ATTR_MODE;
  875. mutex_lock(&lower_dentry->d_inode->i_mutex);
  876. rc = notify_change(lower_dentry, ia);
  877. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  878. out:
  879. fsstack_copy_attr_all(inode, lower_inode, NULL);
  880. return rc;
  881. }
  882. int
  883. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  884. size_t size, int flags)
  885. {
  886. int rc = 0;
  887. struct dentry *lower_dentry;
  888. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  889. if (!lower_dentry->d_inode->i_op->setxattr) {
  890. rc = -ENOSYS;
  891. goto out;
  892. }
  893. mutex_lock(&lower_dentry->d_inode->i_mutex);
  894. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  895. size, flags);
  896. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  897. out:
  898. return rc;
  899. }
  900. ssize_t
  901. ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
  902. void *value, size_t size)
  903. {
  904. int rc = 0;
  905. if (!lower_dentry->d_inode->i_op->getxattr) {
  906. rc = -ENOSYS;
  907. goto out;
  908. }
  909. mutex_lock(&lower_dentry->d_inode->i_mutex);
  910. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  911. size);
  912. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  913. out:
  914. return rc;
  915. }
  916. static ssize_t
  917. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  918. size_t size)
  919. {
  920. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
  921. value, size);
  922. }
  923. static ssize_t
  924. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  925. {
  926. int rc = 0;
  927. struct dentry *lower_dentry;
  928. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  929. if (!lower_dentry->d_inode->i_op->listxattr) {
  930. rc = -ENOSYS;
  931. goto out;
  932. }
  933. mutex_lock(&lower_dentry->d_inode->i_mutex);
  934. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  935. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  936. out:
  937. return rc;
  938. }
  939. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  940. {
  941. int rc = 0;
  942. struct dentry *lower_dentry;
  943. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  944. if (!lower_dentry->d_inode->i_op->removexattr) {
  945. rc = -ENOSYS;
  946. goto out;
  947. }
  948. mutex_lock(&lower_dentry->d_inode->i_mutex);
  949. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  950. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  951. out:
  952. return rc;
  953. }
  954. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  955. {
  956. if ((ecryptfs_inode_to_lower(inode)
  957. == (struct inode *)candidate_lower_inode))
  958. return 1;
  959. else
  960. return 0;
  961. }
  962. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  963. {
  964. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  965. return 0;
  966. }
  967. const struct inode_operations ecryptfs_symlink_iops = {
  968. .readlink = ecryptfs_readlink,
  969. .follow_link = ecryptfs_follow_link,
  970. .put_link = ecryptfs_put_link,
  971. .permission = ecryptfs_permission,
  972. .setattr = ecryptfs_setattr,
  973. .setxattr = ecryptfs_setxattr,
  974. .getxattr = ecryptfs_getxattr,
  975. .listxattr = ecryptfs_listxattr,
  976. .removexattr = ecryptfs_removexattr
  977. };
  978. const struct inode_operations ecryptfs_dir_iops = {
  979. .create = ecryptfs_create,
  980. .lookup = ecryptfs_lookup,
  981. .link = ecryptfs_link,
  982. .unlink = ecryptfs_unlink,
  983. .symlink = ecryptfs_symlink,
  984. .mkdir = ecryptfs_mkdir,
  985. .rmdir = ecryptfs_rmdir,
  986. .mknod = ecryptfs_mknod,
  987. .rename = ecryptfs_rename,
  988. .permission = ecryptfs_permission,
  989. .setattr = ecryptfs_setattr,
  990. .setxattr = ecryptfs_setxattr,
  991. .getxattr = ecryptfs_getxattr,
  992. .listxattr = ecryptfs_listxattr,
  993. .removexattr = ecryptfs_removexattr
  994. };
  995. const struct inode_operations ecryptfs_main_iops = {
  996. .permission = ecryptfs_permission,
  997. .setattr = ecryptfs_setattr,
  998. .setxattr = ecryptfs_setxattr,
  999. .getxattr = ecryptfs_getxattr,
  1000. .listxattr = ecryptfs_listxattr,
  1001. .removexattr = ecryptfs_removexattr
  1002. };