file.c 10 KB

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