file.c 11 KB

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