file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/aio.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. ssize_t rc;
  50. struct path *path;
  51. struct file *file = iocb->ki_filp;
  52. rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
  53. /*
  54. * Even though this is a async interface, we need to wait
  55. * for IO to finish to update atime
  56. */
  57. if (-EIOCBQUEUED == rc)
  58. rc = wait_on_sync_kiocb(iocb);
  59. if (rc >= 0) {
  60. path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
  61. touch_atime(path);
  62. }
  63. return rc;
  64. }
  65. struct ecryptfs_getdents_callback {
  66. struct dir_context ctx;
  67. struct dir_context *caller;
  68. struct super_block *sb;
  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->sb, 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. buf->caller->pos = buf->ctx.pos;
  93. rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
  94. kfree(name);
  95. if (!rc)
  96. buf->entries_written++;
  97. out:
  98. return rc;
  99. }
  100. /**
  101. * ecryptfs_readdir
  102. * @file: The eCryptfs directory file
  103. * @ctx: The actor to feed the entries to
  104. */
  105. static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
  106. {
  107. int rc;
  108. struct file *lower_file;
  109. struct inode *inode = file_inode(file);
  110. struct ecryptfs_getdents_callback buf = {
  111. .ctx.actor = ecryptfs_filldir,
  112. .caller = ctx,
  113. .sb = inode->i_sb,
  114. };
  115. lower_file = ecryptfs_file_to_lower(file);
  116. lower_file->f_pos = ctx->pos;
  117. rc = iterate_dir(lower_file, &buf.ctx);
  118. ctx->pos = buf.ctx.pos;
  119. if (rc < 0)
  120. goto out;
  121. if (buf.filldir_called && !buf.entries_written)
  122. goto out;
  123. if (rc >= 0)
  124. fsstack_copy_attr_atime(inode,
  125. file_inode(lower_file));
  126. out:
  127. return rc;
  128. }
  129. struct kmem_cache *ecryptfs_file_info_cache;
  130. static int read_or_initialize_metadata(struct dentry *dentry)
  131. {
  132. struct inode *inode = dentry->d_inode;
  133. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  134. struct ecryptfs_crypt_stat *crypt_stat;
  135. int rc;
  136. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  137. mount_crypt_stat = &ecryptfs_superblock_to_private(
  138. inode->i_sb)->mount_crypt_stat;
  139. mutex_lock(&crypt_stat->cs_mutex);
  140. if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
  141. crypt_stat->flags & ECRYPTFS_KEY_VALID) {
  142. rc = 0;
  143. goto out;
  144. }
  145. rc = ecryptfs_read_metadata(dentry);
  146. if (!rc)
  147. goto out;
  148. if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
  149. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  150. | ECRYPTFS_ENCRYPTED);
  151. rc = 0;
  152. goto out;
  153. }
  154. if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
  155. !i_size_read(ecryptfs_inode_to_lower(inode))) {
  156. rc = ecryptfs_initialize_file(dentry, inode);
  157. if (!rc)
  158. goto out;
  159. }
  160. rc = -EIO;
  161. out:
  162. mutex_unlock(&crypt_stat->cs_mutex);
  163. return rc;
  164. }
  165. /**
  166. * ecryptfs_open
  167. * @inode: inode speciying file to open
  168. * @file: Structure to return filled in
  169. *
  170. * Opens the file specified by inode.
  171. *
  172. * Returns zero on success; non-zero otherwise
  173. */
  174. static int ecryptfs_open(struct inode *inode, struct file *file)
  175. {
  176. int rc = 0;
  177. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  178. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  179. struct dentry *ecryptfs_dentry = file->f_path.dentry;
  180. /* Private value of ecryptfs_dentry allocated in
  181. * ecryptfs_lookup() */
  182. struct ecryptfs_file_info *file_info;
  183. mount_crypt_stat = &ecryptfs_superblock_to_private(
  184. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  185. if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  186. && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
  187. || (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
  188. || (file->f_flags & O_APPEND))) {
  189. printk(KERN_WARNING "Mount has encrypted view enabled; "
  190. "files may only be read\n");
  191. rc = -EPERM;
  192. goto out;
  193. }
  194. /* Released in ecryptfs_release or end of function if failure */
  195. file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
  196. ecryptfs_set_file_private(file, file_info);
  197. if (!file_info) {
  198. ecryptfs_printk(KERN_ERR,
  199. "Error attempting to allocate memory\n");
  200. rc = -ENOMEM;
  201. goto out;
  202. }
  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. | ECRYPTFS_ENCRYPTED);
  210. }
  211. mutex_unlock(&crypt_stat->cs_mutex);
  212. rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
  213. if (rc) {
  214. printk(KERN_ERR "%s: Error attempting to initialize "
  215. "the lower file for the dentry with name "
  216. "[%s]; rc = [%d]\n", __func__,
  217. ecryptfs_dentry->d_name.name, rc);
  218. goto out_free;
  219. }
  220. if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
  221. == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
  222. rc = -EPERM;
  223. printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
  224. "file must hence be opened RO\n", __func__);
  225. goto out_put;
  226. }
  227. ecryptfs_set_file_lower(
  228. file, ecryptfs_inode_to_private(inode)->lower_file);
  229. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  230. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  231. mutex_lock(&crypt_stat->cs_mutex);
  232. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  233. mutex_unlock(&crypt_stat->cs_mutex);
  234. rc = 0;
  235. goto out;
  236. }
  237. rc = read_or_initialize_metadata(ecryptfs_dentry);
  238. if (rc)
  239. goto out_put;
  240. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
  241. "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
  242. (unsigned long long)i_size_read(inode));
  243. goto out;
  244. out_put:
  245. ecryptfs_put_lower_file(inode);
  246. out_free:
  247. kmem_cache_free(ecryptfs_file_info_cache,
  248. ecryptfs_file_to_private(file));
  249. out:
  250. return rc;
  251. }
  252. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  253. {
  254. struct file *lower_file = ecryptfs_file_to_lower(file);
  255. if (lower_file->f_op && lower_file->f_op->flush) {
  256. filemap_write_and_wait(file->f_mapping);
  257. return lower_file->f_op->flush(lower_file, td);
  258. }
  259. return 0;
  260. }
  261. static int ecryptfs_release(struct inode *inode, struct file *file)
  262. {
  263. ecryptfs_put_lower_file(inode);
  264. kmem_cache_free(ecryptfs_file_info_cache,
  265. ecryptfs_file_to_private(file));
  266. return 0;
  267. }
  268. static int
  269. ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  270. {
  271. int rc;
  272. rc = filemap_write_and_wait(file->f_mapping);
  273. if (rc)
  274. return rc;
  275. return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
  276. }
  277. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  278. {
  279. int rc = 0;
  280. struct file *lower_file = NULL;
  281. lower_file = ecryptfs_file_to_lower(file);
  282. if (lower_file->f_op && lower_file->f_op->fasync)
  283. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  284. return rc;
  285. }
  286. static long
  287. ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  288. {
  289. struct file *lower_file = NULL;
  290. long rc = -ENOTTY;
  291. if (ecryptfs_file_to_private(file))
  292. lower_file = ecryptfs_file_to_lower(file);
  293. if (lower_file && lower_file->f_op && lower_file->f_op->unlocked_ioctl)
  294. rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
  295. return rc;
  296. }
  297. #ifdef CONFIG_COMPAT
  298. static long
  299. ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  300. {
  301. struct file *lower_file = NULL;
  302. long rc = -ENOIOCTLCMD;
  303. if (ecryptfs_file_to_private(file))
  304. lower_file = ecryptfs_file_to_lower(file);
  305. if (lower_file && lower_file->f_op && lower_file->f_op->compat_ioctl)
  306. rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
  307. return rc;
  308. }
  309. #endif
  310. const struct file_operations ecryptfs_dir_fops = {
  311. .iterate = ecryptfs_readdir,
  312. .read = generic_read_dir,
  313. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  314. #ifdef CONFIG_COMPAT
  315. .compat_ioctl = ecryptfs_compat_ioctl,
  316. #endif
  317. .open = ecryptfs_open,
  318. .flush = ecryptfs_flush,
  319. .release = ecryptfs_release,
  320. .fsync = ecryptfs_fsync,
  321. .fasync = ecryptfs_fasync,
  322. .splice_read = generic_file_splice_read,
  323. .llseek = default_llseek,
  324. };
  325. const struct file_operations ecryptfs_main_fops = {
  326. .llseek = generic_file_llseek,
  327. .read = do_sync_read,
  328. .aio_read = ecryptfs_read_update_atime,
  329. .write = do_sync_write,
  330. .aio_write = generic_file_aio_write,
  331. .iterate = ecryptfs_readdir,
  332. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  333. #ifdef CONFIG_COMPAT
  334. .compat_ioctl = ecryptfs_compat_ioctl,
  335. #endif
  336. .mmap = generic_file_mmap,
  337. .open = ecryptfs_open,
  338. .flush = ecryptfs_flush,
  339. .release = ecryptfs_release,
  340. .fsync = ecryptfs_fsync,
  341. .fasync = ecryptfs_fasync,
  342. .splice_read = generic_file_splice_read,
  343. };