inode.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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/fs_stack.h>
  32. #include <linux/slab.h>
  33. #include <linux/xattr.h>
  34. #include <asm/unaligned.h>
  35. #include "ecryptfs_kernel.h"
  36. static struct dentry *lock_parent(struct dentry *dentry)
  37. {
  38. struct dentry *dir;
  39. dir = dget_parent(dentry);
  40. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  41. return dir;
  42. }
  43. static void unlock_dir(struct dentry *dir)
  44. {
  45. inode_unlock(d_inode(dir));
  46. dput(dir);
  47. }
  48. static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
  49. {
  50. return ecryptfs_inode_to_lower(inode) == lower_inode;
  51. }
  52. static int ecryptfs_inode_set(struct inode *inode, void *opaque)
  53. {
  54. struct inode *lower_inode = opaque;
  55. ecryptfs_set_inode_lower(inode, lower_inode);
  56. fsstack_copy_attr_all(inode, lower_inode);
  57. /* i_size will be overwritten for encrypted regular files */
  58. fsstack_copy_inode_size(inode, lower_inode);
  59. inode->i_ino = lower_inode->i_ino;
  60. inode->i_mapping->a_ops = &ecryptfs_aops;
  61. if (S_ISLNK(inode->i_mode))
  62. inode->i_op = &ecryptfs_symlink_iops;
  63. else if (S_ISDIR(inode->i_mode))
  64. inode->i_op = &ecryptfs_dir_iops;
  65. else
  66. inode->i_op = &ecryptfs_main_iops;
  67. if (S_ISDIR(inode->i_mode))
  68. inode->i_fop = &ecryptfs_dir_fops;
  69. else if (special_file(inode->i_mode))
  70. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  71. else
  72. inode->i_fop = &ecryptfs_main_fops;
  73. return 0;
  74. }
  75. static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
  76. struct super_block *sb)
  77. {
  78. struct inode *inode;
  79. if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
  80. return ERR_PTR(-EXDEV);
  81. if (!igrab(lower_inode))
  82. return ERR_PTR(-ESTALE);
  83. inode = iget5_locked(sb, (unsigned long)lower_inode,
  84. ecryptfs_inode_test, ecryptfs_inode_set,
  85. lower_inode);
  86. if (!inode) {
  87. iput(lower_inode);
  88. return ERR_PTR(-EACCES);
  89. }
  90. if (!(inode->i_state & I_NEW))
  91. iput(lower_inode);
  92. return inode;
  93. }
  94. struct inode *ecryptfs_get_inode(struct inode *lower_inode,
  95. struct super_block *sb)
  96. {
  97. struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
  98. if (!IS_ERR(inode) && (inode->i_state & I_NEW))
  99. unlock_new_inode(inode);
  100. return inode;
  101. }
  102. /**
  103. * ecryptfs_interpose
  104. * @lower_dentry: Existing dentry in the lower filesystem
  105. * @dentry: ecryptfs' dentry
  106. * @sb: ecryptfs's super_block
  107. *
  108. * Interposes upper and lower dentries.
  109. *
  110. * Returns zero on success; non-zero otherwise
  111. */
  112. static int ecryptfs_interpose(struct dentry *lower_dentry,
  113. struct dentry *dentry, struct super_block *sb)
  114. {
  115. struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
  116. if (IS_ERR(inode))
  117. return PTR_ERR(inode);
  118. d_instantiate(dentry, inode);
  119. return 0;
  120. }
  121. static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
  122. struct inode *inode)
  123. {
  124. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  125. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  126. struct dentry *lower_dir_dentry;
  127. int rc;
  128. dget(lower_dentry);
  129. lower_dir_dentry = lock_parent(lower_dentry);
  130. rc = vfs_unlink(lower_dir_inode, lower_dentry, NULL);
  131. if (rc) {
  132. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  133. goto out_unlock;
  134. }
  135. fsstack_copy_attr_times(dir, lower_dir_inode);
  136. set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
  137. inode->i_ctime = dir->i_ctime;
  138. d_drop(dentry);
  139. out_unlock:
  140. unlock_dir(lower_dir_dentry);
  141. dput(lower_dentry);
  142. return rc;
  143. }
  144. /**
  145. * ecryptfs_do_create
  146. * @directory_inode: inode of the new file's dentry's parent in ecryptfs
  147. * @ecryptfs_dentry: New file's dentry in ecryptfs
  148. * @mode: The mode of the new file
  149. *
  150. * Creates the underlying file and the eCryptfs inode which will link to
  151. * it. It will also update the eCryptfs directory inode to mimic the
  152. * stat of the lower directory inode.
  153. *
  154. * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
  155. */
  156. static struct inode *
  157. ecryptfs_do_create(struct inode *directory_inode,
  158. struct dentry *ecryptfs_dentry, umode_t mode)
  159. {
  160. int rc;
  161. struct dentry *lower_dentry;
  162. struct dentry *lower_dir_dentry;
  163. struct inode *inode;
  164. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  165. lower_dir_dentry = lock_parent(lower_dentry);
  166. rc = vfs_create(d_inode(lower_dir_dentry), lower_dentry, mode, true);
  167. if (rc) {
  168. printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
  169. "rc = [%d]\n", __func__, rc);
  170. inode = ERR_PTR(rc);
  171. goto out_lock;
  172. }
  173. inode = __ecryptfs_get_inode(d_inode(lower_dentry),
  174. directory_inode->i_sb);
  175. if (IS_ERR(inode)) {
  176. vfs_unlink(d_inode(lower_dir_dentry), lower_dentry, NULL);
  177. goto out_lock;
  178. }
  179. fsstack_copy_attr_times(directory_inode, d_inode(lower_dir_dentry));
  180. fsstack_copy_inode_size(directory_inode, d_inode(lower_dir_dentry));
  181. out_lock:
  182. unlock_dir(lower_dir_dentry);
  183. return inode;
  184. }
  185. /**
  186. * ecryptfs_initialize_file
  187. *
  188. * Cause the file to be changed from a basic empty file to an ecryptfs
  189. * file with a header and first data page.
  190. *
  191. * Returns zero on success
  192. */
  193. int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
  194. struct inode *ecryptfs_inode)
  195. {
  196. struct ecryptfs_crypt_stat *crypt_stat =
  197. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  198. int rc = 0;
  199. if (S_ISDIR(ecryptfs_inode->i_mode)) {
  200. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  201. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  202. goto out;
  203. }
  204. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  205. rc = ecryptfs_new_file_context(ecryptfs_inode);
  206. if (rc) {
  207. ecryptfs_printk(KERN_ERR, "Error creating new file "
  208. "context; rc = [%d]\n", rc);
  209. goto out;
  210. }
  211. rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
  212. if (rc) {
  213. printk(KERN_ERR "%s: Error attempting to initialize "
  214. "the lower file for the dentry with name "
  215. "[%pd]; rc = [%d]\n", __func__,
  216. ecryptfs_dentry, rc);
  217. goto out;
  218. }
  219. rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
  220. if (rc)
  221. printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
  222. ecryptfs_put_lower_file(ecryptfs_inode);
  223. out:
  224. return rc;
  225. }
  226. /**
  227. * ecryptfs_create
  228. * @dir: The inode of the directory in which to create the file.
  229. * @dentry: The eCryptfs dentry
  230. * @mode: The mode of the new file.
  231. *
  232. * Creates a new file.
  233. *
  234. * Returns zero on success; non-zero on error condition
  235. */
  236. static int
  237. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  238. umode_t mode, bool excl)
  239. {
  240. struct inode *ecryptfs_inode;
  241. int rc;
  242. ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
  243. mode);
  244. if (IS_ERR(ecryptfs_inode)) {
  245. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  246. "lower filesystem\n");
  247. rc = PTR_ERR(ecryptfs_inode);
  248. goto out;
  249. }
  250. /* At this point, a file exists on "disk"; we need to make sure
  251. * that this on disk file is prepared to be an ecryptfs file */
  252. rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
  253. if (rc) {
  254. ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
  255. ecryptfs_inode);
  256. iget_failed(ecryptfs_inode);
  257. goto out;
  258. }
  259. d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
  260. out:
  261. return rc;
  262. }
  263. static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
  264. {
  265. struct ecryptfs_crypt_stat *crypt_stat;
  266. int rc;
  267. rc = ecryptfs_get_lower_file(dentry, inode);
  268. if (rc) {
  269. printk(KERN_ERR "%s: Error attempting to initialize "
  270. "the lower file for the dentry with name "
  271. "[%pd]; rc = [%d]\n", __func__,
  272. dentry, rc);
  273. return rc;
  274. }
  275. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  276. /* TODO: lock for crypt_stat comparison */
  277. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  278. ecryptfs_set_default_sizes(crypt_stat);
  279. rc = ecryptfs_read_and_validate_header_region(inode);
  280. ecryptfs_put_lower_file(inode);
  281. if (rc) {
  282. rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
  283. if (!rc)
  284. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  285. }
  286. /* Must return 0 to allow non-eCryptfs files to be looked up, too */
  287. return 0;
  288. }
  289. /**
  290. * ecryptfs_lookup_interpose - Dentry interposition for a lookup
  291. */
  292. static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
  293. struct dentry *lower_dentry)
  294. {
  295. struct inode *inode, *lower_inode = d_inode(lower_dentry);
  296. struct ecryptfs_dentry_info *dentry_info;
  297. struct vfsmount *lower_mnt;
  298. int rc = 0;
  299. dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  300. if (!dentry_info) {
  301. dput(lower_dentry);
  302. return ERR_PTR(-ENOMEM);
  303. }
  304. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
  305. fsstack_copy_attr_atime(d_inode(dentry->d_parent),
  306. d_inode(lower_dentry->d_parent));
  307. BUG_ON(!d_count(lower_dentry));
  308. ecryptfs_set_dentry_private(dentry, dentry_info);
  309. dentry_info->lower_path.mnt = lower_mnt;
  310. dentry_info->lower_path.dentry = lower_dentry;
  311. if (d_really_is_negative(lower_dentry)) {
  312. /* We want to add because we couldn't find in lower */
  313. d_add(dentry, NULL);
  314. return NULL;
  315. }
  316. inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
  317. if (IS_ERR(inode)) {
  318. printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
  319. __func__, PTR_ERR(inode));
  320. return ERR_CAST(inode);
  321. }
  322. if (S_ISREG(inode->i_mode)) {
  323. rc = ecryptfs_i_size_read(dentry, inode);
  324. if (rc) {
  325. make_bad_inode(inode);
  326. return ERR_PTR(rc);
  327. }
  328. }
  329. if (inode->i_state & I_NEW)
  330. unlock_new_inode(inode);
  331. return d_splice_alias(inode, dentry);
  332. }
  333. /**
  334. * ecryptfs_lookup
  335. * @ecryptfs_dir_inode: The eCryptfs directory inode
  336. * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
  337. * @flags: lookup flags
  338. *
  339. * Find a file on disk. If the file does not exist, then we'll add it to the
  340. * dentry cache and continue on to read it from the disk.
  341. */
  342. static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
  343. struct dentry *ecryptfs_dentry,
  344. unsigned int flags)
  345. {
  346. char *encrypted_and_encoded_name = NULL;
  347. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  348. struct dentry *lower_dir_dentry, *lower_dentry;
  349. const char *name = ecryptfs_dentry->d_name.name;
  350. size_t len = ecryptfs_dentry->d_name.len;
  351. struct dentry *res;
  352. int rc = 0;
  353. lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
  354. mount_crypt_stat = &ecryptfs_superblock_to_private(
  355. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  356. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
  357. rc = ecryptfs_encrypt_and_encode_filename(
  358. &encrypted_and_encoded_name, &len,
  359. mount_crypt_stat, name, len);
  360. if (rc) {
  361. printk(KERN_ERR "%s: Error attempting to encrypt and encode "
  362. "filename; rc = [%d]\n", __func__, rc);
  363. return ERR_PTR(rc);
  364. }
  365. name = encrypted_and_encoded_name;
  366. }
  367. lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
  368. if (IS_ERR(lower_dentry)) {
  369. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  370. "[%ld] on lower_dentry = [%s]\n", __func__,
  371. PTR_ERR(lower_dentry),
  372. name);
  373. res = ERR_CAST(lower_dentry);
  374. } else {
  375. res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
  376. }
  377. kfree(encrypted_and_encoded_name);
  378. return res;
  379. }
  380. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  381. struct dentry *new_dentry)
  382. {
  383. struct dentry *lower_old_dentry;
  384. struct dentry *lower_new_dentry;
  385. struct dentry *lower_dir_dentry;
  386. u64 file_size_save;
  387. int rc;
  388. file_size_save = i_size_read(d_inode(old_dentry));
  389. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  390. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  391. dget(lower_old_dentry);
  392. dget(lower_new_dentry);
  393. lower_dir_dentry = lock_parent(lower_new_dentry);
  394. rc = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
  395. lower_new_dentry, NULL);
  396. if (rc || d_really_is_negative(lower_new_dentry))
  397. goto out_lock;
  398. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
  399. if (rc)
  400. goto out_lock;
  401. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  402. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  403. set_nlink(d_inode(old_dentry),
  404. ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
  405. i_size_write(d_inode(new_dentry), file_size_save);
  406. out_lock:
  407. unlock_dir(lower_dir_dentry);
  408. dput(lower_new_dentry);
  409. dput(lower_old_dentry);
  410. return rc;
  411. }
  412. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  413. {
  414. return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
  415. }
  416. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  417. const char *symname)
  418. {
  419. int rc;
  420. struct dentry *lower_dentry;
  421. struct dentry *lower_dir_dentry;
  422. char *encoded_symname;
  423. size_t encoded_symlen;
  424. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  425. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  426. dget(lower_dentry);
  427. lower_dir_dentry = lock_parent(lower_dentry);
  428. mount_crypt_stat = &ecryptfs_superblock_to_private(
  429. dir->i_sb)->mount_crypt_stat;
  430. rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
  431. &encoded_symlen,
  432. mount_crypt_stat, symname,
  433. strlen(symname));
  434. if (rc)
  435. goto out_lock;
  436. rc = vfs_symlink(d_inode(lower_dir_dentry), lower_dentry,
  437. encoded_symname);
  438. kfree(encoded_symname);
  439. if (rc || d_really_is_negative(lower_dentry))
  440. goto out_lock;
  441. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  442. if (rc)
  443. goto out_lock;
  444. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  445. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  446. out_lock:
  447. unlock_dir(lower_dir_dentry);
  448. dput(lower_dentry);
  449. if (d_really_is_negative(dentry))
  450. d_drop(dentry);
  451. return rc;
  452. }
  453. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  454. {
  455. int rc;
  456. struct dentry *lower_dentry;
  457. struct dentry *lower_dir_dentry;
  458. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  459. lower_dir_dentry = lock_parent(lower_dentry);
  460. rc = vfs_mkdir(d_inode(lower_dir_dentry), lower_dentry, mode);
  461. if (rc || d_really_is_negative(lower_dentry))
  462. goto out;
  463. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  464. if (rc)
  465. goto out;
  466. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  467. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  468. set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
  469. out:
  470. unlock_dir(lower_dir_dentry);
  471. if (d_really_is_negative(dentry))
  472. d_drop(dentry);
  473. return rc;
  474. }
  475. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  476. {
  477. struct dentry *lower_dentry;
  478. struct dentry *lower_dir_dentry;
  479. int rc;
  480. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  481. dget(dentry);
  482. lower_dir_dentry = lock_parent(lower_dentry);
  483. dget(lower_dentry);
  484. rc = vfs_rmdir(d_inode(lower_dir_dentry), lower_dentry);
  485. dput(lower_dentry);
  486. if (!rc && d_really_is_positive(dentry))
  487. clear_nlink(d_inode(dentry));
  488. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  489. set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
  490. unlock_dir(lower_dir_dentry);
  491. if (!rc)
  492. d_drop(dentry);
  493. dput(dentry);
  494. return rc;
  495. }
  496. static int
  497. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  498. {
  499. int rc;
  500. struct dentry *lower_dentry;
  501. struct dentry *lower_dir_dentry;
  502. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  503. lower_dir_dentry = lock_parent(lower_dentry);
  504. rc = vfs_mknod(d_inode(lower_dir_dentry), lower_dentry, mode, dev);
  505. if (rc || d_really_is_negative(lower_dentry))
  506. goto out;
  507. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  508. if (rc)
  509. goto out;
  510. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  511. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  512. out:
  513. unlock_dir(lower_dir_dentry);
  514. if (d_really_is_negative(dentry))
  515. d_drop(dentry);
  516. return rc;
  517. }
  518. static int
  519. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  520. struct inode *new_dir, struct dentry *new_dentry,
  521. unsigned int flags)
  522. {
  523. int rc;
  524. struct dentry *lower_old_dentry;
  525. struct dentry *lower_new_dentry;
  526. struct dentry *lower_old_dir_dentry;
  527. struct dentry *lower_new_dir_dentry;
  528. struct dentry *trap = NULL;
  529. struct inode *target_inode;
  530. if (flags)
  531. return -EINVAL;
  532. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  533. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  534. dget(lower_old_dentry);
  535. dget(lower_new_dentry);
  536. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  537. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  538. target_inode = d_inode(new_dentry);
  539. trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  540. /* source should not be ancestor of target */
  541. if (trap == lower_old_dentry) {
  542. rc = -EINVAL;
  543. goto out_lock;
  544. }
  545. /* target should not be ancestor of source */
  546. if (trap == lower_new_dentry) {
  547. rc = -ENOTEMPTY;
  548. goto out_lock;
  549. }
  550. rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
  551. d_inode(lower_new_dir_dentry), lower_new_dentry,
  552. NULL, 0);
  553. if (rc)
  554. goto out_lock;
  555. if (target_inode)
  556. fsstack_copy_attr_all(target_inode,
  557. ecryptfs_inode_to_lower(target_inode));
  558. fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
  559. if (new_dir != old_dir)
  560. fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
  561. out_lock:
  562. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  563. dput(lower_new_dir_dentry);
  564. dput(lower_old_dir_dentry);
  565. dput(lower_new_dentry);
  566. dput(lower_old_dentry);
  567. return rc;
  568. }
  569. static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
  570. {
  571. DEFINE_DELAYED_CALL(done);
  572. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  573. const char *link;
  574. char *buf;
  575. int rc;
  576. link = vfs_get_link(lower_dentry, &done);
  577. if (IS_ERR(link))
  578. return ERR_CAST(link);
  579. rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
  580. link, strlen(link));
  581. do_delayed_call(&done);
  582. if (rc)
  583. return ERR_PTR(rc);
  584. return buf;
  585. }
  586. static const char *ecryptfs_get_link(struct dentry *dentry,
  587. struct inode *inode,
  588. struct delayed_call *done)
  589. {
  590. size_t len;
  591. char *buf;
  592. if (!dentry)
  593. return ERR_PTR(-ECHILD);
  594. buf = ecryptfs_readlink_lower(dentry, &len);
  595. if (IS_ERR(buf))
  596. return buf;
  597. fsstack_copy_attr_atime(d_inode(dentry),
  598. d_inode(ecryptfs_dentry_to_lower(dentry)));
  599. buf[len] = '\0';
  600. set_delayed_call(done, kfree_link, buf);
  601. return buf;
  602. }
  603. /**
  604. * upper_size_to_lower_size
  605. * @crypt_stat: Crypt_stat associated with file
  606. * @upper_size: Size of the upper file
  607. *
  608. * Calculate the required size of the lower file based on the
  609. * specified size of the upper file. This calculation is based on the
  610. * number of headers in the underlying file and the extent size.
  611. *
  612. * Returns Calculated size of the lower file.
  613. */
  614. static loff_t
  615. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  616. loff_t upper_size)
  617. {
  618. loff_t lower_size;
  619. lower_size = ecryptfs_lower_header_size(crypt_stat);
  620. if (upper_size != 0) {
  621. loff_t num_extents;
  622. num_extents = upper_size >> crypt_stat->extent_shift;
  623. if (upper_size & ~crypt_stat->extent_mask)
  624. num_extents++;
  625. lower_size += (num_extents * crypt_stat->extent_size);
  626. }
  627. return lower_size;
  628. }
  629. /**
  630. * truncate_upper
  631. * @dentry: The ecryptfs layer dentry
  632. * @ia: Address of the ecryptfs inode's attributes
  633. * @lower_ia: Address of the lower inode's attributes
  634. *
  635. * Function to handle truncations modifying the size of the file. Note
  636. * that the file sizes are interpolated. When expanding, we are simply
  637. * writing strings of 0's out. When truncating, we truncate the upper
  638. * inode and update the lower_ia according to the page index
  639. * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
  640. * the caller must use lower_ia in a call to notify_change() to perform
  641. * the truncation of the lower inode.
  642. *
  643. * Returns zero on success; non-zero otherwise
  644. */
  645. static int truncate_upper(struct dentry *dentry, struct iattr *ia,
  646. struct iattr *lower_ia)
  647. {
  648. int rc = 0;
  649. struct inode *inode = d_inode(dentry);
  650. struct ecryptfs_crypt_stat *crypt_stat;
  651. loff_t i_size = i_size_read(inode);
  652. loff_t lower_size_before_truncate;
  653. loff_t lower_size_after_truncate;
  654. if (unlikely((ia->ia_size == i_size))) {
  655. lower_ia->ia_valid &= ~ATTR_SIZE;
  656. return 0;
  657. }
  658. rc = ecryptfs_get_lower_file(dentry, inode);
  659. if (rc)
  660. return rc;
  661. crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
  662. /* Switch on growing or shrinking file */
  663. if (ia->ia_size > i_size) {
  664. char zero[] = { 0x00 };
  665. lower_ia->ia_valid &= ~ATTR_SIZE;
  666. /* Write a single 0 at the last position of the file;
  667. * this triggers code that will fill in 0's throughout
  668. * the intermediate portion of the previous end of the
  669. * file and the new and of the file */
  670. rc = ecryptfs_write(inode, zero,
  671. (ia->ia_size - 1), 1);
  672. } else { /* ia->ia_size < i_size_read(inode) */
  673. /* We're chopping off all the pages down to the page
  674. * in which ia->ia_size is located. Fill in the end of
  675. * that page from (ia->ia_size & ~PAGE_MASK) to
  676. * PAGE_SIZE with zeros. */
  677. size_t num_zeros = (PAGE_SIZE
  678. - (ia->ia_size & ~PAGE_MASK));
  679. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  680. truncate_setsize(inode, ia->ia_size);
  681. lower_ia->ia_size = ia->ia_size;
  682. lower_ia->ia_valid |= ATTR_SIZE;
  683. goto out;
  684. }
  685. if (num_zeros) {
  686. char *zeros_virt;
  687. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  688. if (!zeros_virt) {
  689. rc = -ENOMEM;
  690. goto out;
  691. }
  692. rc = ecryptfs_write(inode, zeros_virt,
  693. ia->ia_size, num_zeros);
  694. kfree(zeros_virt);
  695. if (rc) {
  696. printk(KERN_ERR "Error attempting to zero out "
  697. "the remainder of the end page on "
  698. "reducing truncate; rc = [%d]\n", rc);
  699. goto out;
  700. }
  701. }
  702. truncate_setsize(inode, ia->ia_size);
  703. rc = ecryptfs_write_inode_size_to_metadata(inode);
  704. if (rc) {
  705. printk(KERN_ERR "Problem with "
  706. "ecryptfs_write_inode_size_to_metadata; "
  707. "rc = [%d]\n", rc);
  708. goto out;
  709. }
  710. /* We are reducing the size of the ecryptfs file, and need to
  711. * know if we need to reduce the size of the lower file. */
  712. lower_size_before_truncate =
  713. upper_size_to_lower_size(crypt_stat, i_size);
  714. lower_size_after_truncate =
  715. upper_size_to_lower_size(crypt_stat, ia->ia_size);
  716. if (lower_size_after_truncate < lower_size_before_truncate) {
  717. lower_ia->ia_size = lower_size_after_truncate;
  718. lower_ia->ia_valid |= ATTR_SIZE;
  719. } else
  720. lower_ia->ia_valid &= ~ATTR_SIZE;
  721. }
  722. out:
  723. ecryptfs_put_lower_file(inode);
  724. return rc;
  725. }
  726. static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
  727. {
  728. struct ecryptfs_crypt_stat *crypt_stat;
  729. loff_t lower_oldsize, lower_newsize;
  730. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  731. lower_oldsize = upper_size_to_lower_size(crypt_stat,
  732. i_size_read(inode));
  733. lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
  734. if (lower_newsize > lower_oldsize) {
  735. /*
  736. * The eCryptfs inode and the new *lower* size are mixed here
  737. * because we may not have the lower i_mutex held and/or it may
  738. * not be appropriate to call inode_newsize_ok() with inodes
  739. * from other filesystems.
  740. */
  741. return inode_newsize_ok(inode, lower_newsize);
  742. }
  743. return 0;
  744. }
  745. /**
  746. * ecryptfs_truncate
  747. * @dentry: The ecryptfs layer dentry
  748. * @new_length: The length to expand the file to
  749. *
  750. * Simple function that handles the truncation of an eCryptfs inode and
  751. * its corresponding lower inode.
  752. *
  753. * Returns zero on success; non-zero otherwise
  754. */
  755. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  756. {
  757. struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
  758. struct iattr lower_ia = { .ia_valid = 0 };
  759. int rc;
  760. rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
  761. if (rc)
  762. return rc;
  763. rc = truncate_upper(dentry, &ia, &lower_ia);
  764. if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
  765. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  766. inode_lock(d_inode(lower_dentry));
  767. rc = notify_change(lower_dentry, &lower_ia, NULL);
  768. inode_unlock(d_inode(lower_dentry));
  769. }
  770. return rc;
  771. }
  772. static int
  773. ecryptfs_permission(struct inode *inode, int mask)
  774. {
  775. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  776. }
  777. /**
  778. * ecryptfs_setattr
  779. * @dentry: dentry handle to the inode to modify
  780. * @ia: Structure with flags of what to change and values
  781. *
  782. * Updates the metadata of an inode. If the update is to the size
  783. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  784. * of both the ecryptfs inode and the lower inode.
  785. *
  786. * All other metadata changes will be passed right to the lower filesystem,
  787. * and we will just update our inode to look like the lower.
  788. */
  789. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  790. {
  791. int rc = 0;
  792. struct dentry *lower_dentry;
  793. struct iattr lower_ia;
  794. struct inode *inode;
  795. struct inode *lower_inode;
  796. struct ecryptfs_crypt_stat *crypt_stat;
  797. crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
  798. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
  799. rc = ecryptfs_init_crypt_stat(crypt_stat);
  800. if (rc)
  801. return rc;
  802. }
  803. inode = d_inode(dentry);
  804. lower_inode = ecryptfs_inode_to_lower(inode);
  805. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  806. mutex_lock(&crypt_stat->cs_mutex);
  807. if (d_is_dir(dentry))
  808. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  809. else if (d_is_reg(dentry)
  810. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  811. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  812. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  813. mount_crypt_stat = &ecryptfs_superblock_to_private(
  814. dentry->d_sb)->mount_crypt_stat;
  815. rc = ecryptfs_get_lower_file(dentry, inode);
  816. if (rc) {
  817. mutex_unlock(&crypt_stat->cs_mutex);
  818. goto out;
  819. }
  820. rc = ecryptfs_read_metadata(dentry);
  821. ecryptfs_put_lower_file(inode);
  822. if (rc) {
  823. if (!(mount_crypt_stat->flags
  824. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  825. rc = -EIO;
  826. printk(KERN_WARNING "Either the lower file "
  827. "is not in a valid eCryptfs format, "
  828. "or the key could not be retrieved. "
  829. "Plaintext passthrough mode is not "
  830. "enabled; returning -EIO\n");
  831. mutex_unlock(&crypt_stat->cs_mutex);
  832. goto out;
  833. }
  834. rc = 0;
  835. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  836. | ECRYPTFS_ENCRYPTED);
  837. }
  838. }
  839. mutex_unlock(&crypt_stat->cs_mutex);
  840. rc = setattr_prepare(dentry, ia);
  841. if (rc)
  842. goto out;
  843. if (ia->ia_valid & ATTR_SIZE) {
  844. rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
  845. if (rc)
  846. goto out;
  847. }
  848. memcpy(&lower_ia, ia, sizeof(lower_ia));
  849. if (ia->ia_valid & ATTR_FILE)
  850. lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
  851. if (ia->ia_valid & ATTR_SIZE) {
  852. rc = truncate_upper(dentry, ia, &lower_ia);
  853. if (rc < 0)
  854. goto out;
  855. }
  856. /*
  857. * mode change is for clearing setuid/setgid bits. Allow lower fs
  858. * to interpret this in its own way.
  859. */
  860. if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  861. lower_ia.ia_valid &= ~ATTR_MODE;
  862. inode_lock(d_inode(lower_dentry));
  863. rc = notify_change(lower_dentry, &lower_ia, NULL);
  864. inode_unlock(d_inode(lower_dentry));
  865. out:
  866. fsstack_copy_attr_all(inode, lower_inode);
  867. return rc;
  868. }
  869. static int ecryptfs_getattr_link(const struct path *path, struct kstat *stat,
  870. u32 request_mask, unsigned int flags)
  871. {
  872. struct dentry *dentry = path->dentry;
  873. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  874. int rc = 0;
  875. mount_crypt_stat = &ecryptfs_superblock_to_private(
  876. dentry->d_sb)->mount_crypt_stat;
  877. generic_fillattr(d_inode(dentry), stat);
  878. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
  879. char *target;
  880. size_t targetsiz;
  881. target = ecryptfs_readlink_lower(dentry, &targetsiz);
  882. if (!IS_ERR(target)) {
  883. kfree(target);
  884. stat->size = targetsiz;
  885. } else {
  886. rc = PTR_ERR(target);
  887. }
  888. }
  889. return rc;
  890. }
  891. static int ecryptfs_getattr(const struct path *path, struct kstat *stat,
  892. u32 request_mask, unsigned int flags)
  893. {
  894. struct dentry *dentry = path->dentry;
  895. struct kstat lower_stat;
  896. int rc;
  897. rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
  898. request_mask, flags);
  899. if (!rc) {
  900. fsstack_copy_attr_all(d_inode(dentry),
  901. ecryptfs_inode_to_lower(d_inode(dentry)));
  902. generic_fillattr(d_inode(dentry), stat);
  903. stat->blocks = lower_stat.blocks;
  904. }
  905. return rc;
  906. }
  907. int
  908. ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
  909. const char *name, const void *value,
  910. size_t size, int flags)
  911. {
  912. int rc;
  913. struct dentry *lower_dentry;
  914. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  915. if (!(d_inode(lower_dentry)->i_opflags & IOP_XATTR)) {
  916. rc = -EOPNOTSUPP;
  917. goto out;
  918. }
  919. rc = vfs_setxattr(lower_dentry, name, value, size, flags);
  920. if (!rc && inode)
  921. fsstack_copy_attr_all(inode, d_inode(lower_dentry));
  922. out:
  923. return rc;
  924. }
  925. ssize_t
  926. ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
  927. const char *name, void *value, size_t size)
  928. {
  929. int rc;
  930. if (!(lower_inode->i_opflags & IOP_XATTR)) {
  931. rc = -EOPNOTSUPP;
  932. goto out;
  933. }
  934. inode_lock(lower_inode);
  935. rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
  936. inode_unlock(lower_inode);
  937. out:
  938. return rc;
  939. }
  940. static ssize_t
  941. ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
  942. const char *name, void *value, size_t size)
  943. {
  944. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
  945. ecryptfs_inode_to_lower(inode),
  946. name, value, size);
  947. }
  948. static ssize_t
  949. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  950. {
  951. int rc = 0;
  952. struct dentry *lower_dentry;
  953. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  954. if (!d_inode(lower_dentry)->i_op->listxattr) {
  955. rc = -EOPNOTSUPP;
  956. goto out;
  957. }
  958. inode_lock(d_inode(lower_dentry));
  959. rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
  960. inode_unlock(d_inode(lower_dentry));
  961. out:
  962. return rc;
  963. }
  964. static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
  965. const char *name)
  966. {
  967. int rc;
  968. struct dentry *lower_dentry;
  969. struct inode *lower_inode;
  970. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  971. lower_inode = ecryptfs_inode_to_lower(inode);
  972. if (!(lower_inode->i_opflags & IOP_XATTR)) {
  973. rc = -EOPNOTSUPP;
  974. goto out;
  975. }
  976. inode_lock(lower_inode);
  977. rc = __vfs_removexattr(lower_dentry, name);
  978. inode_unlock(lower_inode);
  979. out:
  980. return rc;
  981. }
  982. const struct inode_operations ecryptfs_symlink_iops = {
  983. .get_link = ecryptfs_get_link,
  984. .permission = ecryptfs_permission,
  985. .setattr = ecryptfs_setattr,
  986. .getattr = ecryptfs_getattr_link,
  987. .listxattr = ecryptfs_listxattr,
  988. };
  989. const struct inode_operations ecryptfs_dir_iops = {
  990. .create = ecryptfs_create,
  991. .lookup = ecryptfs_lookup,
  992. .link = ecryptfs_link,
  993. .unlink = ecryptfs_unlink,
  994. .symlink = ecryptfs_symlink,
  995. .mkdir = ecryptfs_mkdir,
  996. .rmdir = ecryptfs_rmdir,
  997. .mknod = ecryptfs_mknod,
  998. .rename = ecryptfs_rename,
  999. .permission = ecryptfs_permission,
  1000. .setattr = ecryptfs_setattr,
  1001. .listxattr = ecryptfs_listxattr,
  1002. };
  1003. const struct inode_operations ecryptfs_main_iops = {
  1004. .permission = ecryptfs_permission,
  1005. .setattr = ecryptfs_setattr,
  1006. .getattr = ecryptfs_getattr,
  1007. .listxattr = ecryptfs_listxattr,
  1008. };
  1009. static int ecryptfs_xattr_get(const struct xattr_handler *handler,
  1010. struct dentry *dentry, struct inode *inode,
  1011. const char *name, void *buffer, size_t size)
  1012. {
  1013. return ecryptfs_getxattr(dentry, inode, name, buffer, size);
  1014. }
  1015. static int ecryptfs_xattr_set(const struct xattr_handler *handler,
  1016. struct dentry *dentry, struct inode *inode,
  1017. const char *name, const void *value, size_t size,
  1018. int flags)
  1019. {
  1020. if (value)
  1021. return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
  1022. else {
  1023. BUG_ON(flags != XATTR_REPLACE);
  1024. return ecryptfs_removexattr(dentry, inode, name);
  1025. }
  1026. }
  1027. const struct xattr_handler ecryptfs_xattr_handler = {
  1028. .prefix = "", /* match anything */
  1029. .get = ecryptfs_xattr_get,
  1030. .set = ecryptfs_xattr_set,
  1031. };
  1032. const struct xattr_handler *ecryptfs_xattr_handlers[] = {
  1033. &ecryptfs_xattr_handler,
  1034. NULL
  1035. };