file.c 9.7 KB

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