inode.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. rc = ecryptfs_write_metadata(ecryptfs_dentry);
  181. if (rc) {
  182. printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
  183. goto out;
  184. }
  185. rc = grow_file(ecryptfs_dentry);
  186. if (rc)
  187. printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
  188. out:
  189. return rc;
  190. }
  191. /**
  192. * ecryptfs_create
  193. * @dir: The inode of the directory in which to create the file.
  194. * @dentry: The eCryptfs dentry
  195. * @mode: The mode of the new file.
  196. * @nd: nameidata
  197. *
  198. * Creates a new file.
  199. *
  200. * Returns zero on success; non-zero on error condition
  201. */
  202. static int
  203. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  204. int mode, struct nameidata *nd)
  205. {
  206. int rc;
  207. /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
  208. * the crypt_stat->lower_file (persistent file) */
  209. rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
  210. if (unlikely(rc)) {
  211. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  212. "lower filesystem\n");
  213. goto out;
  214. }
  215. /* At this point, a file exists on "disk"; we need to make sure
  216. * that this on disk file is prepared to be an ecryptfs file */
  217. rc = ecryptfs_initialize_file(ecryptfs_dentry);
  218. out:
  219. return rc;
  220. }
  221. /**
  222. * ecryptfs_lookup
  223. * @dir: inode
  224. * @dentry: The dentry
  225. * @nd: nameidata, may be NULL
  226. *
  227. * Find a file on disk. If the file does not exist, then we'll add it to the
  228. * dentry cache and continue on to read it from the disk.
  229. */
  230. static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
  231. struct nameidata *nd)
  232. {
  233. int rc = 0;
  234. struct dentry *lower_dir_dentry;
  235. struct dentry *lower_dentry;
  236. struct vfsmount *lower_mnt;
  237. char *encoded_name;
  238. int encoded_namelen;
  239. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  240. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  241. char *page_virt = NULL;
  242. struct inode *lower_inode;
  243. u64 file_size;
  244. lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
  245. dentry->d_op = &ecryptfs_dops;
  246. if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
  247. || (dentry->d_name.len == 2
  248. && !strcmp(dentry->d_name.name, ".."))) {
  249. d_drop(dentry);
  250. goto out;
  251. }
  252. encoded_namelen = ecryptfs_encode_filename(crypt_stat,
  253. dentry->d_name.name,
  254. dentry->d_name.len,
  255. &encoded_name);
  256. if (encoded_namelen < 0) {
  257. rc = encoded_namelen;
  258. d_drop(dentry);
  259. goto out;
  260. }
  261. ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
  262. "= [%d]\n", encoded_name, encoded_namelen);
  263. lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
  264. encoded_namelen - 1);
  265. kfree(encoded_name);
  266. if (IS_ERR(lower_dentry)) {
  267. ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
  268. rc = PTR_ERR(lower_dentry);
  269. d_drop(dentry);
  270. goto out;
  271. }
  272. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
  273. ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
  274. "d_name.name = [%s]\n", lower_dentry,
  275. lower_dentry->d_name.name);
  276. lower_inode = lower_dentry->d_inode;
  277. fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
  278. BUG_ON(!atomic_read(&lower_dentry->d_count));
  279. ecryptfs_set_dentry_private(dentry,
  280. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  281. GFP_KERNEL));
  282. if (!ecryptfs_dentry_to_private(dentry)) {
  283. rc = -ENOMEM;
  284. ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
  285. "to allocate ecryptfs_dentry_info struct\n");
  286. goto out_dput;
  287. }
  288. ecryptfs_set_dentry_lower(dentry, lower_dentry);
  289. ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
  290. if (!lower_dentry->d_inode) {
  291. /* We want to add because we couldn't find in lower */
  292. d_add(dentry, NULL);
  293. goto out;
  294. }
  295. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
  296. ECRYPTFS_INTERPOSE_FLAG_D_ADD);
  297. if (rc) {
  298. ecryptfs_printk(KERN_ERR, "Error interposing\n");
  299. goto out_dput;
  300. }
  301. if (S_ISDIR(lower_inode->i_mode)) {
  302. ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
  303. goto out;
  304. }
  305. if (S_ISLNK(lower_inode->i_mode)) {
  306. ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
  307. goto out;
  308. }
  309. if (special_file(lower_inode->i_mode)) {
  310. ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
  311. goto out;
  312. }
  313. if (!nd) {
  314. ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
  315. "as we *think* we are about to unlink\n");
  316. goto out;
  317. }
  318. /* Released in this function */
  319. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
  320. GFP_USER);
  321. if (!page_virt) {
  322. rc = -ENOMEM;
  323. ecryptfs_printk(KERN_ERR,
  324. "Cannot ecryptfs_kmalloc a page\n");
  325. goto out_dput;
  326. }
  327. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  328. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  329. ecryptfs_set_default_sizes(crypt_stat);
  330. rc = ecryptfs_read_and_validate_header_region(page_virt,
  331. dentry->d_inode);
  332. if (rc) {
  333. rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
  334. if (rc) {
  335. printk(KERN_DEBUG "Valid metadata not found in header "
  336. "region or xattr region; treating file as "
  337. "unencrypted\n");
  338. rc = 0;
  339. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  340. goto out;
  341. }
  342. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  343. }
  344. mount_crypt_stat = &ecryptfs_superblock_to_private(
  345. dentry->d_sb)->mount_crypt_stat;
  346. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  347. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  348. file_size = (crypt_stat->num_header_bytes_at_front
  349. + i_size_read(lower_dentry->d_inode));
  350. else
  351. file_size = i_size_read(lower_dentry->d_inode);
  352. } else {
  353. file_size = get_unaligned_be64(page_virt);
  354. }
  355. i_size_write(dentry->d_inode, (loff_t)file_size);
  356. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  357. goto out;
  358. out_dput:
  359. dput(lower_dentry);
  360. d_drop(dentry);
  361. out:
  362. return ERR_PTR(rc);
  363. }
  364. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  365. struct dentry *new_dentry)
  366. {
  367. struct dentry *lower_old_dentry;
  368. struct dentry *lower_new_dentry;
  369. struct dentry *lower_dir_dentry;
  370. u64 file_size_save;
  371. int rc;
  372. file_size_save = i_size_read(old_dentry->d_inode);
  373. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  374. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  375. dget(lower_old_dentry);
  376. dget(lower_new_dentry);
  377. lower_dir_dentry = lock_parent(lower_new_dentry);
  378. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  379. lower_new_dentry);
  380. if (rc || !lower_new_dentry->d_inode)
  381. goto out_lock;
  382. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
  383. if (rc)
  384. goto out_lock;
  385. fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
  386. fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
  387. old_dentry->d_inode->i_nlink =
  388. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
  389. i_size_write(new_dentry->d_inode, file_size_save);
  390. out_lock:
  391. unlock_dir(lower_dir_dentry);
  392. dput(lower_new_dentry);
  393. dput(lower_old_dentry);
  394. d_drop(lower_old_dentry);
  395. d_drop(new_dentry);
  396. d_drop(old_dentry);
  397. return rc;
  398. }
  399. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  400. {
  401. int rc = 0;
  402. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  403. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  404. struct dentry *lower_dir_dentry;
  405. lower_dir_dentry = lock_parent(lower_dentry);
  406. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  407. if (rc) {
  408. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  409. goto out_unlock;
  410. }
  411. fsstack_copy_attr_times(dir, lower_dir_inode);
  412. dentry->d_inode->i_nlink =
  413. ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
  414. dentry->d_inode->i_ctime = dir->i_ctime;
  415. d_drop(dentry);
  416. out_unlock:
  417. unlock_dir(lower_dir_dentry);
  418. return rc;
  419. }
  420. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  421. const char *symname)
  422. {
  423. int rc;
  424. struct dentry *lower_dentry;
  425. struct dentry *lower_dir_dentry;
  426. umode_t mode;
  427. char *encoded_symname;
  428. int encoded_symlen;
  429. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  430. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  431. dget(lower_dentry);
  432. lower_dir_dentry = lock_parent(lower_dentry);
  433. mode = S_IALLUGO;
  434. encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
  435. strlen(symname),
  436. &encoded_symname);
  437. if (encoded_symlen < 0) {
  438. rc = encoded_symlen;
  439. goto out_lock;
  440. }
  441. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  442. encoded_symname, mode);
  443. kfree(encoded_symname);
  444. if (rc || !lower_dentry->d_inode)
  445. goto out_lock;
  446. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  447. if (rc)
  448. goto out_lock;
  449. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  450. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  451. out_lock:
  452. unlock_dir(lower_dir_dentry);
  453. dput(lower_dentry);
  454. if (!dentry->d_inode)
  455. d_drop(dentry);
  456. return rc;
  457. }
  458. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  459. {
  460. int rc;
  461. struct dentry *lower_dentry;
  462. struct dentry *lower_dir_dentry;
  463. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  464. lower_dir_dentry = lock_parent(lower_dentry);
  465. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  466. if (rc || !lower_dentry->d_inode)
  467. goto out;
  468. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  469. if (rc)
  470. goto out;
  471. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  472. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  473. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  474. out:
  475. unlock_dir(lower_dir_dentry);
  476. if (!dentry->d_inode)
  477. d_drop(dentry);
  478. return rc;
  479. }
  480. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  481. {
  482. struct dentry *lower_dentry;
  483. struct dentry *lower_dir_dentry;
  484. int rc;
  485. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  486. dget(dentry);
  487. lower_dir_dentry = lock_parent(lower_dentry);
  488. dget(lower_dentry);
  489. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  490. dput(lower_dentry);
  491. if (!rc)
  492. d_delete(lower_dentry);
  493. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  494. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  495. unlock_dir(lower_dir_dentry);
  496. if (!rc)
  497. d_drop(dentry);
  498. dput(dentry);
  499. return rc;
  500. }
  501. static int
  502. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  503. {
  504. int rc;
  505. struct dentry *lower_dentry;
  506. struct dentry *lower_dir_dentry;
  507. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  508. lower_dir_dentry = lock_parent(lower_dentry);
  509. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  510. if (rc || !lower_dentry->d_inode)
  511. goto out;
  512. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
  513. ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE);
  514. if (rc)
  515. goto out;
  516. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  517. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  518. out:
  519. unlock_dir(lower_dir_dentry);
  520. if (!dentry->d_inode)
  521. d_drop(dentry);
  522. return rc;
  523. }
  524. static int
  525. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  526. struct inode *new_dir, struct dentry *new_dentry)
  527. {
  528. int rc;
  529. struct dentry *lower_old_dentry;
  530. struct dentry *lower_new_dentry;
  531. struct dentry *lower_old_dir_dentry;
  532. struct dentry *lower_new_dir_dentry;
  533. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  534. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  535. dget(lower_old_dentry);
  536. dget(lower_new_dentry);
  537. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  538. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  539. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  540. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  541. lower_new_dir_dentry->d_inode, lower_new_dentry);
  542. if (rc)
  543. goto out_lock;
  544. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
  545. if (new_dir != old_dir)
  546. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
  547. out_lock:
  548. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  549. dput(lower_new_dentry->d_parent);
  550. dput(lower_old_dentry->d_parent);
  551. dput(lower_new_dentry);
  552. dput(lower_old_dentry);
  553. return rc;
  554. }
  555. static int
  556. ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
  557. {
  558. int rc;
  559. struct dentry *lower_dentry;
  560. char *decoded_name;
  561. char *lower_buf;
  562. mm_segment_t old_fs;
  563. struct ecryptfs_crypt_stat *crypt_stat;
  564. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  565. if (!lower_dentry->d_inode->i_op ||
  566. !lower_dentry->d_inode->i_op->readlink) {
  567. rc = -EINVAL;
  568. goto out;
  569. }
  570. /* Released in this function */
  571. lower_buf = kmalloc(bufsiz, GFP_KERNEL);
  572. if (lower_buf == NULL) {
  573. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  574. rc = -ENOMEM;
  575. goto out;
  576. }
  577. old_fs = get_fs();
  578. set_fs(get_ds());
  579. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  580. "lower_dentry->d_name.name = [%s]\n",
  581. lower_dentry->d_name.name);
  582. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  583. (char __user *)lower_buf,
  584. bufsiz);
  585. set_fs(old_fs);
  586. if (rc >= 0) {
  587. crypt_stat = NULL;
  588. rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
  589. &decoded_name);
  590. if (rc == -ENOMEM)
  591. goto out_free_lower_buf;
  592. if (rc > 0) {
  593. ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
  594. "to userspace: [%*s]\n", rc,
  595. decoded_name);
  596. if (copy_to_user(buf, decoded_name, rc))
  597. rc = -EFAULT;
  598. }
  599. kfree(decoded_name);
  600. fsstack_copy_attr_atime(dentry->d_inode,
  601. lower_dentry->d_inode);
  602. }
  603. out_free_lower_buf:
  604. kfree(lower_buf);
  605. out:
  606. return rc;
  607. }
  608. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  609. {
  610. char *buf;
  611. int len = PAGE_SIZE, rc;
  612. mm_segment_t old_fs;
  613. /* Released in ecryptfs_put_link(); only release here on error */
  614. buf = kmalloc(len, GFP_KERNEL);
  615. if (!buf) {
  616. rc = -ENOMEM;
  617. goto out;
  618. }
  619. old_fs = get_fs();
  620. set_fs(get_ds());
  621. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  622. "dentry->d_name.name = [%s]\n", dentry->d_name.name);
  623. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  624. buf[rc] = '\0';
  625. set_fs(old_fs);
  626. if (rc < 0)
  627. goto out_free;
  628. rc = 0;
  629. nd_set_link(nd, buf);
  630. goto out;
  631. out_free:
  632. kfree(buf);
  633. out:
  634. return ERR_PTR(rc);
  635. }
  636. static void
  637. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  638. {
  639. /* Free the char* */
  640. kfree(nd_get_link(nd));
  641. }
  642. /**
  643. * upper_size_to_lower_size
  644. * @crypt_stat: Crypt_stat associated with file
  645. * @upper_size: Size of the upper file
  646. *
  647. * Calculate the required size of the lower file based on the
  648. * specified size of the upper file. This calculation is based on the
  649. * number of headers in the underlying file and the extent size.
  650. *
  651. * Returns Calculated size of the lower file.
  652. */
  653. static loff_t
  654. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  655. loff_t upper_size)
  656. {
  657. loff_t lower_size;
  658. lower_size = crypt_stat->num_header_bytes_at_front;
  659. if (upper_size != 0) {
  660. loff_t num_extents;
  661. num_extents = upper_size >> crypt_stat->extent_shift;
  662. if (upper_size & ~crypt_stat->extent_mask)
  663. num_extents++;
  664. lower_size += (num_extents * crypt_stat->extent_size);
  665. }
  666. return lower_size;
  667. }
  668. /**
  669. * ecryptfs_truncate
  670. * @dentry: The ecryptfs layer dentry
  671. * @new_length: The length to expand the file to
  672. *
  673. * Function to handle truncations modifying the size of the file. Note
  674. * that the file sizes are interpolated. When expanding, we are simply
  675. * writing strings of 0's out. When truncating, we need to modify the
  676. * underlying file size according to the page index interpolations.
  677. *
  678. * Returns zero on success; non-zero otherwise
  679. */
  680. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  681. {
  682. int rc = 0;
  683. struct inode *inode = dentry->d_inode;
  684. struct dentry *lower_dentry;
  685. struct file fake_ecryptfs_file;
  686. struct ecryptfs_crypt_stat *crypt_stat;
  687. loff_t i_size = i_size_read(inode);
  688. loff_t lower_size_before_truncate;
  689. loff_t lower_size_after_truncate;
  690. if (unlikely((new_length == i_size)))
  691. goto out;
  692. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  693. /* Set up a fake ecryptfs file, this is used to interface with
  694. * the file in the underlying filesystem so that the
  695. * truncation has an effect there as well. */
  696. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  697. fake_ecryptfs_file.f_path.dentry = dentry;
  698. /* Released at out_free: label */
  699. ecryptfs_set_file_private(&fake_ecryptfs_file,
  700. kmem_cache_alloc(ecryptfs_file_info_cache,
  701. GFP_KERNEL));
  702. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  703. rc = -ENOMEM;
  704. goto out;
  705. }
  706. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  707. ecryptfs_set_file_lower(
  708. &fake_ecryptfs_file,
  709. ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
  710. /* Switch on growing or shrinking file */
  711. if (new_length > i_size) {
  712. char zero[] = { 0x00 };
  713. /* Write a single 0 at the last position of the file;
  714. * this triggers code that will fill in 0's throughout
  715. * the intermediate portion of the previous end of the
  716. * file and the new and of the file */
  717. rc = ecryptfs_write(&fake_ecryptfs_file, zero,
  718. (new_length - 1), 1);
  719. } else { /* new_length < i_size_read(inode) */
  720. /* We're chopping off all the pages down do the page
  721. * in which new_length is located. Fill in the end of
  722. * that page from (new_length & ~PAGE_CACHE_MASK) to
  723. * PAGE_CACHE_SIZE with zeros. */
  724. size_t num_zeros = (PAGE_CACHE_SIZE
  725. - (new_length & ~PAGE_CACHE_MASK));
  726. if (num_zeros) {
  727. char *zeros_virt;
  728. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  729. if (!zeros_virt) {
  730. rc = -ENOMEM;
  731. goto out_free;
  732. }
  733. rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
  734. new_length, num_zeros);
  735. kfree(zeros_virt);
  736. if (rc) {
  737. printk(KERN_ERR "Error attempting to zero out "
  738. "the remainder of the end page on "
  739. "reducing truncate; rc = [%d]\n", rc);
  740. goto out_free;
  741. }
  742. }
  743. vmtruncate(inode, new_length);
  744. rc = ecryptfs_write_inode_size_to_metadata(inode);
  745. if (rc) {
  746. printk(KERN_ERR "Problem with "
  747. "ecryptfs_write_inode_size_to_metadata; "
  748. "rc = [%d]\n", rc);
  749. goto out_free;
  750. }
  751. /* We are reducing the size of the ecryptfs file, and need to
  752. * know if we need to reduce the size of the lower file. */
  753. lower_size_before_truncate =
  754. upper_size_to_lower_size(crypt_stat, i_size);
  755. lower_size_after_truncate =
  756. upper_size_to_lower_size(crypt_stat, new_length);
  757. if (lower_size_after_truncate < lower_size_before_truncate)
  758. vmtruncate(lower_dentry->d_inode,
  759. lower_size_after_truncate);
  760. }
  761. out_free:
  762. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  763. kmem_cache_free(ecryptfs_file_info_cache,
  764. ecryptfs_file_to_private(&fake_ecryptfs_file));
  765. out:
  766. return rc;
  767. }
  768. static int
  769. ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  770. {
  771. int rc;
  772. if (nd) {
  773. struct vfsmount *vfsmnt_save = nd->path.mnt;
  774. struct dentry *dentry_save = nd->path.dentry;
  775. nd->path.mnt = ecryptfs_dentry_to_lower_mnt(nd->path.dentry);
  776. nd->path.dentry = ecryptfs_dentry_to_lower(nd->path.dentry);
  777. rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
  778. nd->path.mnt = vfsmnt_save;
  779. nd->path.dentry = dentry_save;
  780. } else
  781. rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
  782. return rc;
  783. }
  784. /**
  785. * ecryptfs_setattr
  786. * @dentry: dentry handle to the inode to modify
  787. * @ia: Structure with flags of what to change and values
  788. *
  789. * Updates the metadata of an inode. If the update is to the size
  790. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  791. * of both the ecryptfs inode and the lower inode.
  792. *
  793. * All other metadata changes will be passed right to the lower filesystem,
  794. * and we will just update our inode to look like the lower.
  795. */
  796. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  797. {
  798. int rc = 0;
  799. struct dentry *lower_dentry;
  800. struct inode *inode;
  801. struct inode *lower_inode;
  802. struct ecryptfs_crypt_stat *crypt_stat;
  803. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  804. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  805. ecryptfs_init_crypt_stat(crypt_stat);
  806. inode = dentry->d_inode;
  807. lower_inode = ecryptfs_inode_to_lower(inode);
  808. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  809. mutex_lock(&crypt_stat->cs_mutex);
  810. if (S_ISDIR(dentry->d_inode->i_mode))
  811. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  812. else if (S_ISREG(dentry->d_inode->i_mode)
  813. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  814. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  815. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  816. mount_crypt_stat = &ecryptfs_superblock_to_private(
  817. dentry->d_sb)->mount_crypt_stat;
  818. rc = ecryptfs_read_metadata(dentry);
  819. if (rc) {
  820. if (!(mount_crypt_stat->flags
  821. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  822. rc = -EIO;
  823. printk(KERN_WARNING "Either the lower file "
  824. "is not in a valid eCryptfs format, "
  825. "or the key could not be retrieved. "
  826. "Plaintext passthrough mode is not "
  827. "enabled; returning -EIO\n");
  828. mutex_unlock(&crypt_stat->cs_mutex);
  829. goto out;
  830. }
  831. rc = 0;
  832. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  833. mutex_unlock(&crypt_stat->cs_mutex);
  834. goto out;
  835. }
  836. }
  837. mutex_unlock(&crypt_stat->cs_mutex);
  838. if (ia->ia_valid & ATTR_SIZE) {
  839. ecryptfs_printk(KERN_DEBUG,
  840. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  841. ia->ia_valid, ATTR_SIZE);
  842. rc = ecryptfs_truncate(dentry, ia->ia_size);
  843. /* ecryptfs_truncate handles resizing of the lower file */
  844. ia->ia_valid &= ~ATTR_SIZE;
  845. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  846. ia->ia_valid);
  847. if (rc < 0)
  848. goto out;
  849. }
  850. /*
  851. * mode change is for clearing setuid/setgid bits. Allow lower fs
  852. * to interpret this in its own way.
  853. */
  854. if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  855. ia->ia_valid &= ~ATTR_MODE;
  856. mutex_lock(&lower_dentry->d_inode->i_mutex);
  857. rc = notify_change(lower_dentry, ia);
  858. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  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. };