inode.c 30 KB

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