file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <linux/smp_lock.h>
  33. #include "ecryptfs_kernel.h"
  34. /**
  35. * ecryptfs_read_update_atime
  36. *
  37. * generic_file_read updates the atime of upper layer inode. But, it
  38. * doesn't give us a chance to update the atime of the lower layer
  39. * inode. This function is a wrapper to generic_file_read. It
  40. * updates the atime of the lower level inode if generic_file_read
  41. * returns without any errors. This is to be used only for file reads.
  42. * The function to be used for directory reads is ecryptfs_read.
  43. */
  44. static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
  45. const struct iovec *iov,
  46. unsigned long nr_segs, loff_t pos)
  47. {
  48. int rc;
  49. struct dentry *lower_dentry;
  50. struct vfsmount *lower_vfsmount;
  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_vfsmount = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
  62. touch_atime(lower_vfsmount, lower_dentry);
  63. }
  64. return rc;
  65. }
  66. struct ecryptfs_getdents_callback {
  67. void *dirent;
  68. struct dentry *dentry;
  69. filldir_t filldir;
  70. int err;
  71. int filldir_called;
  72. int entries_written;
  73. };
  74. /* Inspired by generic filldir in fs/readir.c */
  75. static int
  76. ecryptfs_filldir(void *dirent, const char *name, int namelen, loff_t offset,
  77. u64 ino, unsigned int d_type)
  78. {
  79. struct ecryptfs_crypt_stat *crypt_stat;
  80. struct ecryptfs_getdents_callback *buf =
  81. (struct ecryptfs_getdents_callback *)dirent;
  82. int rc;
  83. int decoded_length;
  84. char *decoded_name;
  85. crypt_stat = ecryptfs_dentry_to_private(buf->dentry)->crypt_stat;
  86. buf->filldir_called++;
  87. decoded_length = ecryptfs_decode_filename(crypt_stat, name, namelen,
  88. &decoded_name);
  89. if (decoded_length < 0) {
  90. rc = decoded_length;
  91. goto out;
  92. }
  93. rc = buf->filldir(buf->dirent, decoded_name, decoded_length, offset,
  94. ino, d_type);
  95. kfree(decoded_name);
  96. if (rc >= 0)
  97. buf->entries_written++;
  98. out:
  99. return rc;
  100. }
  101. /**
  102. * ecryptfs_readdir
  103. * @file: The ecryptfs file struct
  104. * @dirent: Directory entry
  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. retry:
  121. buf.filldir_called = 0;
  122. buf.entries_written = 0;
  123. buf.err = 0;
  124. rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
  125. if (buf.err)
  126. rc = buf.err;
  127. if (buf.filldir_called && !buf.entries_written)
  128. goto retry;
  129. file->f_pos = lower_file->f_pos;
  130. if (rc >= 0)
  131. fsstack_copy_attr_atime(inode, lower_file->f_path.dentry->d_inode);
  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 = ecryptfs_dentry_to_lower(ecryptfs_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->f_flags & O_RDONLY)
  185. && !(file->f_flags & O_RDONLY)) {
  186. rc = -EPERM;
  187. printk(KERN_WARNING "%s: Lower persistent file is RO; eCryptfs "
  188. "file must hence be opened RO\n", __func__);
  189. goto out;
  190. }
  191. if (!ecryptfs_inode_to_private(inode)->lower_file) {
  192. BUG_ON(!(crypt_stat->flags & ECRYPTFS_DELAY_PERSISTENT));
  193. mutex_lock(&crypt_stat->cs_mutex);
  194. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  195. mutex_unlock(&crypt_stat->cs_mutex);
  196. rc = ecryptfs_init_persistent_file(ecryptfs_dentry);
  197. if (rc) {
  198. printk(KERN_ERR "%s: Error attempting to initialize "
  199. "the persistent file for the dentry with name "
  200. "[%s]; rc = [%d]\n", __func__,
  201. ecryptfs_dentry->d_name.name, rc);
  202. goto out;
  203. }
  204. }
  205. ecryptfs_set_file_lower(
  206. file, ecryptfs_inode_to_private(inode)->lower_file);
  207. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  208. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  209. mutex_lock(&crypt_stat->cs_mutex);
  210. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  211. mutex_unlock(&crypt_stat->cs_mutex);
  212. rc = 0;
  213. goto out;
  214. }
  215. mutex_lock(&crypt_stat->cs_mutex);
  216. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  217. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
  218. rc = ecryptfs_read_metadata(ecryptfs_dentry);
  219. if (rc) {
  220. ecryptfs_printk(KERN_DEBUG,
  221. "Valid headers not found\n");
  222. if (!(mount_crypt_stat->flags
  223. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  224. rc = -EIO;
  225. printk(KERN_WARNING "Either the lower file "
  226. "is not in a valid eCryptfs format, "
  227. "or the key could not be retrieved. "
  228. "Plaintext passthrough mode is not "
  229. "enabled; returning -EIO\n");
  230. mutex_unlock(&crypt_stat->cs_mutex);
  231. goto out_free;
  232. }
  233. rc = 0;
  234. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  235. mutex_unlock(&crypt_stat->cs_mutex);
  236. goto out;
  237. }
  238. }
  239. mutex_unlock(&crypt_stat->cs_mutex);
  240. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] "
  241. "size: [0x%.16x]\n", inode, inode->i_ino,
  242. i_size_read(inode));
  243. goto out;
  244. out_free:
  245. kmem_cache_free(ecryptfs_file_info_cache,
  246. ecryptfs_file_to_private(file));
  247. out:
  248. return rc;
  249. }
  250. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  251. {
  252. int rc = 0;
  253. struct file *lower_file = NULL;
  254. lower_file = ecryptfs_file_to_lower(file);
  255. if (lower_file->f_op && lower_file->f_op->flush)
  256. rc = lower_file->f_op->flush(lower_file, td);
  257. return rc;
  258. }
  259. static int ecryptfs_release(struct inode *inode, struct file *file)
  260. {
  261. kmem_cache_free(ecryptfs_file_info_cache,
  262. ecryptfs_file_to_private(file));
  263. return 0;
  264. }
  265. static int
  266. ecryptfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  267. {
  268. struct file *lower_file = ecryptfs_file_to_lower(file);
  269. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  270. struct inode *lower_inode = lower_dentry->d_inode;
  271. int rc = -EINVAL;
  272. if (lower_inode->i_fop->fsync) {
  273. mutex_lock(&lower_inode->i_mutex);
  274. rc = lower_inode->i_fop->fsync(lower_file, lower_dentry,
  275. datasync);
  276. mutex_unlock(&lower_inode->i_mutex);
  277. }
  278. return rc;
  279. }
  280. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  281. {
  282. int rc = 0;
  283. struct file *lower_file = NULL;
  284. lock_kernel();
  285. lower_file = ecryptfs_file_to_lower(file);
  286. if (lower_file->f_op && lower_file->f_op->fasync)
  287. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  288. unlock_kernel();
  289. return rc;
  290. }
  291. static int ecryptfs_ioctl(struct inode *inode, struct file *file,
  292. unsigned int cmd, unsigned long arg);
  293. const struct file_operations ecryptfs_dir_fops = {
  294. .readdir = ecryptfs_readdir,
  295. .ioctl = ecryptfs_ioctl,
  296. .mmap = generic_file_mmap,
  297. .open = ecryptfs_open,
  298. .flush = ecryptfs_flush,
  299. .release = ecryptfs_release,
  300. .fsync = ecryptfs_fsync,
  301. .fasync = ecryptfs_fasync,
  302. .splice_read = generic_file_splice_read,
  303. };
  304. const struct file_operations ecryptfs_main_fops = {
  305. .llseek = generic_file_llseek,
  306. .read = do_sync_read,
  307. .aio_read = ecryptfs_read_update_atime,
  308. .write = do_sync_write,
  309. .aio_write = generic_file_aio_write,
  310. .readdir = ecryptfs_readdir,
  311. .ioctl = ecryptfs_ioctl,
  312. .mmap = generic_file_mmap,
  313. .open = ecryptfs_open,
  314. .flush = ecryptfs_flush,
  315. .release = ecryptfs_release,
  316. .fsync = ecryptfs_fsync,
  317. .fasync = ecryptfs_fasync,
  318. .splice_read = generic_file_splice_read,
  319. };
  320. static int
  321. ecryptfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  322. unsigned long arg)
  323. {
  324. int rc = 0;
  325. struct file *lower_file = NULL;
  326. if (ecryptfs_file_to_private(file))
  327. lower_file = ecryptfs_file_to_lower(file);
  328. if (lower_file && lower_file->f_op && lower_file->f_op->ioctl)
  329. rc = lower_file->f_op->ioctl(ecryptfs_inode_to_lower(inode),
  330. lower_file, cmd, arg);
  331. else
  332. rc = -ENOTTY;
  333. return rc;
  334. }