file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <mhalcrow@us.ibm.com>
  8. * Michael C. Thompson <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/poll.h>
  27. #include <linux/mount.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/security.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/compat.h>
  32. #include <linux/fs_stack.h>
  33. #include "ecryptfs_kernel.h"
  34. /**
  35. * ecryptfs_llseek
  36. * @file: File we are seeking in
  37. * @offset: The offset to seek to
  38. * @origin: 2 - offset from i_size; 1 - offset from f_pos
  39. *
  40. * Returns the position we have seeked to, or negative on error
  41. */
  42. static loff_t ecryptfs_llseek(struct file *file, loff_t offset, int origin)
  43. {
  44. loff_t rv;
  45. loff_t new_end_pos;
  46. int rc;
  47. int expanding_file = 0;
  48. struct inode *inode = file->f_mapping->host;
  49. /* If our offset is past the end of our file, we're going to
  50. * need to grow it so we have a valid length of 0's */
  51. new_end_pos = offset;
  52. switch (origin) {
  53. case 2:
  54. new_end_pos += i_size_read(inode);
  55. expanding_file = 1;
  56. break;
  57. case 1:
  58. new_end_pos += file->f_pos;
  59. if (new_end_pos > i_size_read(inode)) {
  60. ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) "
  61. "> i_size_read(inode)(=[0x%.16x])\n",
  62. new_end_pos, i_size_read(inode));
  63. expanding_file = 1;
  64. }
  65. break;
  66. default:
  67. if (new_end_pos > i_size_read(inode)) {
  68. ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) "
  69. "> i_size_read(inode)(=[0x%.16x])\n",
  70. new_end_pos, i_size_read(inode));
  71. expanding_file = 1;
  72. }
  73. }
  74. ecryptfs_printk(KERN_DEBUG, "new_end_pos = [0x%.16x]\n", new_end_pos);
  75. if (expanding_file) {
  76. rc = ecryptfs_truncate(file->f_path.dentry, new_end_pos);
  77. if (rc) {
  78. rv = rc;
  79. ecryptfs_printk(KERN_ERR, "Error on attempt to "
  80. "truncate to (higher) offset [0x%.16x];"
  81. " rc = [%d]\n", new_end_pos, rc);
  82. goto out;
  83. }
  84. }
  85. rv = generic_file_llseek(file, offset, origin);
  86. out:
  87. return rv;
  88. }
  89. /**
  90. * ecryptfs_read_update_atime
  91. *
  92. * generic_file_read updates the atime of upper layer inode. But, it
  93. * doesn't give us a chance to update the atime of the lower layer
  94. * inode. This function is a wrapper to generic_file_read. It
  95. * updates the atime of the lower level inode if generic_file_read
  96. * returns without any errors. This is to be used only for file reads.
  97. * The function to be used for directory reads is ecryptfs_read.
  98. */
  99. static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
  100. const struct iovec *iov,
  101. unsigned long nr_segs, loff_t pos)
  102. {
  103. int rc;
  104. struct dentry *lower_dentry;
  105. struct vfsmount *lower_vfsmount;
  106. struct file *file = iocb->ki_filp;
  107. rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
  108. /*
  109. * Even though this is a async interface, we need to wait
  110. * for IO to finish to update atime
  111. */
  112. if (-EIOCBQUEUED == rc)
  113. rc = wait_on_sync_kiocb(iocb);
  114. if (rc >= 0) {
  115. lower_dentry = ecryptfs_dentry_to_lower(file->f_path.dentry);
  116. lower_vfsmount = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
  117. touch_atime(lower_vfsmount, lower_dentry);
  118. }
  119. return rc;
  120. }
  121. struct ecryptfs_getdents_callback {
  122. void *dirent;
  123. struct dentry *dentry;
  124. filldir_t filldir;
  125. int err;
  126. int filldir_called;
  127. int entries_written;
  128. };
  129. /* Inspired by generic filldir in fs/readir.c */
  130. static int
  131. ecryptfs_filldir(void *dirent, const char *name, int namelen, loff_t offset,
  132. u64 ino, unsigned int d_type)
  133. {
  134. struct ecryptfs_crypt_stat *crypt_stat;
  135. struct ecryptfs_getdents_callback *buf =
  136. (struct ecryptfs_getdents_callback *)dirent;
  137. int rc;
  138. int decoded_length;
  139. char *decoded_name;
  140. crypt_stat = ecryptfs_dentry_to_private(buf->dentry)->crypt_stat;
  141. buf->filldir_called++;
  142. decoded_length = ecryptfs_decode_filename(crypt_stat, name, namelen,
  143. &decoded_name);
  144. if (decoded_length < 0) {
  145. rc = decoded_length;
  146. goto out;
  147. }
  148. rc = buf->filldir(buf->dirent, decoded_name, decoded_length, offset,
  149. ino, d_type);
  150. kfree(decoded_name);
  151. if (rc >= 0)
  152. buf->entries_written++;
  153. out:
  154. return rc;
  155. }
  156. /**
  157. * ecryptfs_readdir
  158. * @file: The ecryptfs file struct
  159. * @dirent: Directory entry
  160. * @filldir: The filldir callback function
  161. */
  162. static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  163. {
  164. int rc;
  165. struct file *lower_file;
  166. struct inode *inode;
  167. struct ecryptfs_getdents_callback buf;
  168. lower_file = ecryptfs_file_to_lower(file);
  169. lower_file->f_pos = file->f_pos;
  170. inode = file->f_path.dentry->d_inode;
  171. memset(&buf, 0, sizeof(buf));
  172. buf.dirent = dirent;
  173. buf.dentry = file->f_path.dentry;
  174. buf.filldir = filldir;
  175. retry:
  176. buf.filldir_called = 0;
  177. buf.entries_written = 0;
  178. buf.err = 0;
  179. rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
  180. if (buf.err)
  181. rc = buf.err;
  182. if (buf.filldir_called && !buf.entries_written)
  183. goto retry;
  184. file->f_pos = lower_file->f_pos;
  185. if (rc >= 0)
  186. fsstack_copy_attr_atime(inode, lower_file->f_path.dentry->d_inode);
  187. return rc;
  188. }
  189. struct kmem_cache *ecryptfs_file_info_cache;
  190. int ecryptfs_open_lower_file(struct file **lower_file,
  191. struct dentry *lower_dentry,
  192. struct vfsmount *lower_mnt, int flags)
  193. {
  194. int rc = 0;
  195. flags |= O_LARGEFILE;
  196. dget(lower_dentry);
  197. mntget(lower_mnt);
  198. *lower_file = dentry_open(lower_dentry, lower_mnt, flags);
  199. if (IS_ERR(*lower_file)) {
  200. printk(KERN_ERR "Error opening lower file for lower_dentry "
  201. "[0x%p], lower_mnt [0x%p], and flags [0x%x]\n",
  202. lower_dentry, lower_mnt, flags);
  203. rc = PTR_ERR(*lower_file);
  204. *lower_file = NULL;
  205. goto out;
  206. }
  207. out:
  208. return rc;
  209. }
  210. int ecryptfs_close_lower_file(struct file *lower_file)
  211. {
  212. fput(lower_file);
  213. return 0;
  214. }
  215. /**
  216. * ecryptfs_open
  217. * @inode: inode speciying file to open
  218. * @file: Structure to return filled in
  219. *
  220. * Opens the file specified by inode.
  221. *
  222. * Returns zero on success; non-zero otherwise
  223. */
  224. static int ecryptfs_open(struct inode *inode, struct file *file)
  225. {
  226. int rc = 0;
  227. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  228. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  229. struct dentry *ecryptfs_dentry = file->f_path.dentry;
  230. /* Private value of ecryptfs_dentry allocated in
  231. * ecryptfs_lookup() */
  232. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  233. struct inode *lower_inode = NULL;
  234. struct file *lower_file = NULL;
  235. struct vfsmount *lower_mnt;
  236. struct ecryptfs_file_info *file_info;
  237. int lower_flags;
  238. mount_crypt_stat = &ecryptfs_superblock_to_private(
  239. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  240. if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  241. && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
  242. || (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
  243. || (file->f_flags & O_APPEND))) {
  244. printk(KERN_WARNING "Mount has encrypted view enabled; "
  245. "files may only be read\n");
  246. rc = -EPERM;
  247. goto out;
  248. }
  249. /* Released in ecryptfs_release or end of function if failure */
  250. file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
  251. ecryptfs_set_file_private(file, file_info);
  252. if (!file_info) {
  253. ecryptfs_printk(KERN_ERR,
  254. "Error attempting to allocate memory\n");
  255. rc = -ENOMEM;
  256. goto out;
  257. }
  258. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  259. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  260. mutex_lock(&crypt_stat->cs_mutex);
  261. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
  262. ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
  263. /* Policy code enabled in future release */
  264. crypt_stat->flags |= ECRYPTFS_POLICY_APPLIED;
  265. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  266. }
  267. mutex_unlock(&crypt_stat->cs_mutex);
  268. lower_flags = file->f_flags;
  269. if ((lower_flags & O_ACCMODE) == O_WRONLY)
  270. lower_flags = (lower_flags & O_ACCMODE) | O_RDWR;
  271. if (file->f_flags & O_APPEND)
  272. lower_flags &= ~O_APPEND;
  273. lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  274. /* Corresponding fput() in ecryptfs_release() */
  275. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  276. lower_flags))) {
  277. ecryptfs_printk(KERN_ERR, "Error opening lower file\n");
  278. goto out_puts;
  279. }
  280. ecryptfs_set_file_lower(file, lower_file);
  281. /* Isn't this check the same as the one in lookup? */
  282. lower_inode = lower_dentry->d_inode;
  283. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  284. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  285. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  286. rc = 0;
  287. goto out;
  288. }
  289. mutex_lock(&crypt_stat->cs_mutex);
  290. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  291. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  292. rc = ecryptfs_read_metadata(ecryptfs_dentry, lower_file);
  293. if (rc) {
  294. ecryptfs_printk(KERN_DEBUG,
  295. "Valid headers not found\n");
  296. if (!(mount_crypt_stat->flags
  297. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  298. rc = -EIO;
  299. printk(KERN_WARNING "Attempt to read file that "
  300. "is not in a valid eCryptfs format, "
  301. "and plaintext passthrough mode is not "
  302. "enabled; returning -EIO\n");
  303. mutex_unlock(&crypt_stat->cs_mutex);
  304. goto out_puts;
  305. }
  306. rc = 0;
  307. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  308. mutex_unlock(&crypt_stat->cs_mutex);
  309. goto out;
  310. }
  311. }
  312. mutex_unlock(&crypt_stat->cs_mutex);
  313. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] "
  314. "size: [0x%.16x]\n", inode, inode->i_ino,
  315. i_size_read(inode));
  316. ecryptfs_set_file_lower(file, lower_file);
  317. goto out;
  318. out_puts:
  319. mntput(lower_mnt);
  320. dput(lower_dentry);
  321. kmem_cache_free(ecryptfs_file_info_cache,
  322. ecryptfs_file_to_private(file));
  323. out:
  324. return rc;
  325. }
  326. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  327. {
  328. int rc = 0;
  329. struct file *lower_file = NULL;
  330. lower_file = ecryptfs_file_to_lower(file);
  331. if (lower_file->f_op && lower_file->f_op->flush)
  332. rc = lower_file->f_op->flush(lower_file, td);
  333. return rc;
  334. }
  335. static int ecryptfs_release(struct inode *inode, struct file *file)
  336. {
  337. struct file *lower_file = ecryptfs_file_to_lower(file);
  338. struct ecryptfs_file_info *file_info = ecryptfs_file_to_private(file);
  339. struct inode *lower_inode = ecryptfs_inode_to_lower(inode);
  340. int rc;
  341. if ((rc = ecryptfs_close_lower_file(lower_file))) {
  342. printk(KERN_ERR "Error closing lower_file\n");
  343. goto out;
  344. }
  345. inode->i_blocks = lower_inode->i_blocks;
  346. kmem_cache_free(ecryptfs_file_info_cache, file_info);
  347. out:
  348. return rc;
  349. }
  350. static int
  351. ecryptfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  352. {
  353. struct file *lower_file = ecryptfs_file_to_lower(file);
  354. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  355. struct inode *lower_inode = lower_dentry->d_inode;
  356. int rc = -EINVAL;
  357. if (lower_inode->i_fop->fsync) {
  358. mutex_lock(&lower_inode->i_mutex);
  359. rc = lower_inode->i_fop->fsync(lower_file, lower_dentry,
  360. datasync);
  361. mutex_unlock(&lower_inode->i_mutex);
  362. }
  363. return rc;
  364. }
  365. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  366. {
  367. int rc = 0;
  368. struct file *lower_file = NULL;
  369. lower_file = ecryptfs_file_to_lower(file);
  370. if (lower_file->f_op && lower_file->f_op->fasync)
  371. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  372. return rc;
  373. }
  374. static ssize_t ecryptfs_sendfile(struct file *file, loff_t * ppos,
  375. size_t count, read_actor_t actor, void *target)
  376. {
  377. struct file *lower_file = NULL;
  378. int rc = -EINVAL;
  379. lower_file = ecryptfs_file_to_lower(file);
  380. if (lower_file->f_op && lower_file->f_op->sendfile)
  381. rc = lower_file->f_op->sendfile(lower_file, ppos, count,
  382. actor, target);
  383. return rc;
  384. }
  385. static int ecryptfs_ioctl(struct inode *inode, struct file *file,
  386. unsigned int cmd, unsigned long arg);
  387. const struct file_operations ecryptfs_dir_fops = {
  388. .readdir = ecryptfs_readdir,
  389. .ioctl = ecryptfs_ioctl,
  390. .mmap = generic_file_mmap,
  391. .open = ecryptfs_open,
  392. .flush = ecryptfs_flush,
  393. .release = ecryptfs_release,
  394. .fsync = ecryptfs_fsync,
  395. .fasync = ecryptfs_fasync,
  396. .sendfile = ecryptfs_sendfile,
  397. };
  398. const struct file_operations ecryptfs_main_fops = {
  399. .llseek = ecryptfs_llseek,
  400. .read = do_sync_read,
  401. .aio_read = ecryptfs_read_update_atime,
  402. .write = do_sync_write,
  403. .aio_write = generic_file_aio_write,
  404. .readdir = ecryptfs_readdir,
  405. .ioctl = ecryptfs_ioctl,
  406. .mmap = generic_file_mmap,
  407. .open = ecryptfs_open,
  408. .flush = ecryptfs_flush,
  409. .release = ecryptfs_release,
  410. .fsync = ecryptfs_fsync,
  411. .fasync = ecryptfs_fasync,
  412. .sendfile = ecryptfs_sendfile,
  413. };
  414. static int
  415. ecryptfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  416. unsigned long arg)
  417. {
  418. int rc = 0;
  419. struct file *lower_file = NULL;
  420. if (ecryptfs_file_to_private(file))
  421. lower_file = ecryptfs_file_to_lower(file);
  422. if (lower_file && lower_file->f_op && lower_file->f_op->ioctl)
  423. rc = lower_file->f_op->ioctl(ecryptfs_inode_to_lower(inode),
  424. lower_file, cmd, arg);
  425. else
  426. rc = -ENOTTY;
  427. return rc;
  428. }