file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/slab.h>
  28. #include <linux/mount.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/security.h>
  31. #include <linux/compat.h>
  32. #include <linux/fs_stack.h>
  33. #include "ecryptfs_kernel.h"
  34. /**
  35. * ecryptfs_read_update_atime
  36. *
  37. * generic_file_read updates the atime of upper layer inode. But, it
  38. * doesn't give us a chance to update the atime of the lower layer
  39. * inode. This function is a wrapper to generic_file_read. It
  40. * updates the atime of the lower level inode if generic_file_read
  41. * returns without any errors. This is to be used only for file reads.
  42. * The function to be used for directory reads is ecryptfs_read.
  43. */
  44. static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
  45. const struct iovec *iov,
  46. unsigned long nr_segs, loff_t pos)
  47. {
  48. ssize_t rc;
  49. struct path lower;
  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.mnt = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
  61. touch_atime(&lower);
  62. }
  63. return rc;
  64. }
  65. struct ecryptfs_getdents_callback {
  66. void *dirent;
  67. struct dentry *dentry;
  68. filldir_t filldir;
  69. int filldir_called;
  70. int entries_written;
  71. };
  72. /* Inspired by generic filldir in fs/readdir.c */
  73. static int
  74. ecryptfs_filldir(void *dirent, const char *lower_name, int lower_namelen,
  75. loff_t offset, u64 ino, unsigned int d_type)
  76. {
  77. struct ecryptfs_getdents_callback *buf =
  78. (struct ecryptfs_getdents_callback *)dirent;
  79. size_t name_size;
  80. char *name;
  81. int rc;
  82. buf->filldir_called++;
  83. rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
  84. buf->dentry, lower_name,
  85. lower_namelen);
  86. if (rc) {
  87. printk(KERN_ERR "%s: Error attempting to decode and decrypt "
  88. "filename [%s]; rc = [%d]\n", __func__, lower_name,
  89. rc);
  90. goto out;
  91. }
  92. rc = buf->filldir(buf->dirent, name, name_size, offset, ino, d_type);
  93. kfree(name);
  94. if (rc >= 0)
  95. buf->entries_written++;
  96. out:
  97. return rc;
  98. }
  99. /**
  100. * ecryptfs_readdir
  101. * @file: The eCryptfs directory file
  102. * @dirent: Directory entry handle
  103. * @filldir: The filldir callback function
  104. */
  105. static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  106. {
  107. int rc;
  108. struct file *lower_file;
  109. struct inode *inode;
  110. struct ecryptfs_getdents_callback buf;
  111. lower_file = ecryptfs_file_to_lower(file);
  112. lower_file->f_pos = file->f_pos;
  113. inode = file->f_path.dentry->d_inode;
  114. memset(&buf, 0, sizeof(buf));
  115. buf.dirent = dirent;
  116. buf.dentry = file->f_path.dentry;
  117. buf.filldir = filldir;
  118. buf.filldir_called = 0;
  119. buf.entries_written = 0;
  120. rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
  121. file->f_pos = lower_file->f_pos;
  122. if (rc < 0)
  123. goto out;
  124. if (buf.filldir_called && !buf.entries_written)
  125. goto out;
  126. if (rc >= 0)
  127. fsstack_copy_attr_atime(inode,
  128. lower_file->f_path.dentry->d_inode);
  129. out:
  130. return rc;
  131. }
  132. static void ecryptfs_vma_close(struct vm_area_struct *vma)
  133. {
  134. filemap_write_and_wait(vma->vm_file->f_mapping);
  135. }
  136. static const struct vm_operations_struct ecryptfs_file_vm_ops = {
  137. .close = ecryptfs_vma_close,
  138. .fault = filemap_fault,
  139. };
  140. static int ecryptfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  141. {
  142. int rc;
  143. rc = generic_file_mmap(file, vma);
  144. if (!rc)
  145. vma->vm_ops = &ecryptfs_file_vm_ops;
  146. return rc;
  147. }
  148. struct kmem_cache *ecryptfs_file_info_cache;
  149. /**
  150. * ecryptfs_open
  151. * @inode: inode speciying file to open
  152. * @file: Structure to return filled in
  153. *
  154. * Opens the file specified by inode.
  155. *
  156. * Returns zero on success; non-zero otherwise
  157. */
  158. static int ecryptfs_open(struct inode *inode, struct file *file)
  159. {
  160. int rc = 0;
  161. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  162. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  163. struct dentry *ecryptfs_dentry = file->f_path.dentry;
  164. /* Private value of ecryptfs_dentry allocated in
  165. * ecryptfs_lookup() */
  166. struct dentry *lower_dentry;
  167. struct ecryptfs_file_info *file_info;
  168. mount_crypt_stat = &ecryptfs_superblock_to_private(
  169. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  170. if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  171. && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
  172. || (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
  173. || (file->f_flags & O_APPEND))) {
  174. printk(KERN_WARNING "Mount has encrypted view enabled; "
  175. "files may only be read\n");
  176. rc = -EPERM;
  177. goto out;
  178. }
  179. /* Released in ecryptfs_release or end of function if failure */
  180. file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
  181. ecryptfs_set_file_private(file, file_info);
  182. if (!file_info) {
  183. ecryptfs_printk(KERN_ERR,
  184. "Error attempting to allocate memory\n");
  185. rc = -ENOMEM;
  186. goto out;
  187. }
  188. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  189. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  190. mutex_lock(&crypt_stat->cs_mutex);
  191. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
  192. ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
  193. /* Policy code enabled in future release */
  194. crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
  195. | ECRYPTFS_ENCRYPTED);
  196. }
  197. mutex_unlock(&crypt_stat->cs_mutex);
  198. rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
  199. if (rc) {
  200. printk(KERN_ERR "%s: Error attempting to initialize "
  201. "the lower file for the dentry with name "
  202. "[%s]; rc = [%d]\n", __func__,
  203. ecryptfs_dentry->d_name.name, rc);
  204. goto out_free;
  205. }
  206. if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
  207. == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
  208. rc = -EPERM;
  209. printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
  210. "file must hence be opened RO\n", __func__);
  211. goto out_put;
  212. }
  213. ecryptfs_set_file_lower(
  214. file, ecryptfs_inode_to_private(inode)->lower_file);
  215. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  216. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  217. mutex_lock(&crypt_stat->cs_mutex);
  218. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  219. mutex_unlock(&crypt_stat->cs_mutex);
  220. rc = 0;
  221. goto out;
  222. }
  223. mutex_lock(&crypt_stat->cs_mutex);
  224. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  225. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  226. rc = ecryptfs_read_metadata(ecryptfs_dentry);
  227. if (rc) {
  228. ecryptfs_printk(KERN_DEBUG,
  229. "Valid headers not found\n");
  230. if (!(mount_crypt_stat->flags
  231. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  232. rc = -EIO;
  233. printk(KERN_WARNING "Either the lower file "
  234. "is not in a valid eCryptfs format, "
  235. "or the key could not be retrieved. "
  236. "Plaintext passthrough mode is not "
  237. "enabled; returning -EIO\n");
  238. mutex_unlock(&crypt_stat->cs_mutex);
  239. goto out_put;
  240. }
  241. rc = 0;
  242. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  243. | ECRYPTFS_ENCRYPTED);
  244. mutex_unlock(&crypt_stat->cs_mutex);
  245. goto out;
  246. }
  247. }
  248. mutex_unlock(&crypt_stat->cs_mutex);
  249. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
  250. "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
  251. (unsigned long long)i_size_read(inode));
  252. goto out;
  253. out_put:
  254. ecryptfs_put_lower_file(inode);
  255. out_free:
  256. kmem_cache_free(ecryptfs_file_info_cache,
  257. ecryptfs_file_to_private(file));
  258. out:
  259. return rc;
  260. }
  261. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  262. {
  263. return file->f_mode & FMODE_WRITE
  264. ? filemap_write_and_wait(file->f_mapping) : 0;
  265. }
  266. static int ecryptfs_release(struct inode *inode, struct file *file)
  267. {
  268. ecryptfs_put_lower_file(inode);
  269. kmem_cache_free(ecryptfs_file_info_cache,
  270. ecryptfs_file_to_private(file));
  271. return 0;
  272. }
  273. static int
  274. ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  275. {
  276. int rc = 0;
  277. rc = generic_file_fsync(file, start, end, datasync);
  278. if (rc)
  279. goto out;
  280. rc = vfs_fsync_range(ecryptfs_file_to_lower(file), start, end,
  281. datasync);
  282. out:
  283. return rc;
  284. }
  285. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  286. {
  287. int rc = 0;
  288. struct file *lower_file = NULL;
  289. lower_file = ecryptfs_file_to_lower(file);
  290. if (lower_file->f_op && lower_file->f_op->fasync)
  291. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  292. return rc;
  293. }
  294. static long
  295. ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  296. {
  297. struct file *lower_file = NULL;
  298. long rc = -ENOTTY;
  299. if (ecryptfs_file_to_private(file))
  300. lower_file = ecryptfs_file_to_lower(file);
  301. if (lower_file && lower_file->f_op && lower_file->f_op->unlocked_ioctl)
  302. rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
  303. return rc;
  304. }
  305. #ifdef CONFIG_COMPAT
  306. static long
  307. ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  308. {
  309. struct file *lower_file = NULL;
  310. long rc = -ENOIOCTLCMD;
  311. if (ecryptfs_file_to_private(file))
  312. lower_file = ecryptfs_file_to_lower(file);
  313. if (lower_file && lower_file->f_op && lower_file->f_op->compat_ioctl)
  314. rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
  315. return rc;
  316. }
  317. #endif
  318. const struct file_operations ecryptfs_dir_fops = {
  319. .readdir = ecryptfs_readdir,
  320. .read = generic_read_dir,
  321. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  322. #ifdef CONFIG_COMPAT
  323. .compat_ioctl = ecryptfs_compat_ioctl,
  324. #endif
  325. .open = ecryptfs_open,
  326. .flush = ecryptfs_flush,
  327. .release = ecryptfs_release,
  328. .fsync = ecryptfs_fsync,
  329. .fasync = ecryptfs_fasync,
  330. .splice_read = generic_file_splice_read,
  331. .llseek = default_llseek,
  332. };
  333. const struct file_operations ecryptfs_main_fops = {
  334. .llseek = generic_file_llseek,
  335. .read = do_sync_read,
  336. .aio_read = ecryptfs_read_update_atime,
  337. .write = do_sync_write,
  338. .aio_write = generic_file_aio_write,
  339. .readdir = ecryptfs_readdir,
  340. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  341. #ifdef CONFIG_COMPAT
  342. .compat_ioctl = ecryptfs_compat_ioctl,
  343. #endif
  344. .mmap = ecryptfs_file_mmap,
  345. .open = ecryptfs_open,
  346. .flush = ecryptfs_flush,
  347. .release = ecryptfs_release,
  348. .fsync = ecryptfs_fsync,
  349. .fasync = ecryptfs_fasync,
  350. .splice_read = generic_file_splice_read,
  351. };