file.c 12 KB

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