inode.c 31 KB

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