inode.c 30 KB

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