file.c 10 KB

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