file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 "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. struct iov_iter *to)
  46. {
  47. ssize_t rc;
  48. struct path *path;
  49. struct file *file = iocb->ki_filp;
  50. rc = generic_file_read_iter(iocb, to);
  51. if (rc >= 0) {
  52. path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
  53. touch_atime(path);
  54. }
  55. return rc;
  56. }
  57. struct ecryptfs_getdents_callback {
  58. struct dir_context ctx;
  59. struct dir_context *caller;
  60. struct super_block *sb;
  61. int filldir_called;
  62. int entries_written;
  63. };
  64. /* Inspired by generic filldir in fs/readdir.c */
  65. static int
  66. ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
  67. int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
  68. {
  69. struct ecryptfs_getdents_callback *buf =
  70. container_of(ctx, struct ecryptfs_getdents_callback, ctx);
  71. size_t name_size;
  72. char *name;
  73. int rc;
  74. buf->filldir_called++;
  75. rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
  76. buf->sb, lower_name,
  77. lower_namelen);
  78. if (rc) {
  79. printk(KERN_ERR "%s: Error attempting to decode and decrypt "
  80. "filename [%s]; rc = [%d]\n", __func__, lower_name,
  81. rc);
  82. goto out;
  83. }
  84. buf->caller->pos = buf->ctx.pos;
  85. rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
  86. kfree(name);
  87. if (!rc)
  88. buf->entries_written++;
  89. out:
  90. return rc;
  91. }
  92. /**
  93. * ecryptfs_readdir
  94. * @file: The eCryptfs directory file
  95. * @ctx: The actor to feed the entries to
  96. */
  97. static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
  98. {
  99. int rc;
  100. struct file *lower_file;
  101. struct inode *inode = file_inode(file);
  102. struct ecryptfs_getdents_callback buf = {
  103. .ctx.actor = ecryptfs_filldir,
  104. .caller = ctx,
  105. .sb = inode->i_sb,
  106. };
  107. lower_file = ecryptfs_file_to_lower(file);
  108. rc = iterate_dir(lower_file, &buf.ctx);
  109. ctx->pos = buf.ctx.pos;
  110. if (rc < 0)
  111. goto out;
  112. if (buf.filldir_called && !buf.entries_written)
  113. goto out;
  114. if (rc >= 0)
  115. fsstack_copy_attr_atime(inode,
  116. file_inode(lower_file));
  117. out:
  118. return rc;
  119. }
  120. struct kmem_cache *ecryptfs_file_info_cache;
  121. static int read_or_initialize_metadata(struct dentry *dentry)
  122. {
  123. struct inode *inode = d_inode(dentry);
  124. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  125. struct ecryptfs_crypt_stat *crypt_stat;
  126. int rc;
  127. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  128. mount_crypt_stat = &ecryptfs_superblock_to_private(
  129. inode->i_sb)->mount_crypt_stat;
  130. mutex_lock(&crypt_stat->cs_mutex);
  131. if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
  132. crypt_stat->flags & ECRYPTFS_KEY_VALID) {
  133. rc = 0;
  134. goto out;
  135. }
  136. rc = ecryptfs_read_metadata(dentry);
  137. if (!rc)
  138. goto out;
  139. if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
  140. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  141. | ECRYPTFS_ENCRYPTED);
  142. rc = 0;
  143. goto out;
  144. }
  145. if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
  146. !i_size_read(ecryptfs_inode_to_lower(inode))) {
  147. rc = ecryptfs_initialize_file(dentry, inode);
  148. if (!rc)
  149. goto out;
  150. }
  151. rc = -EIO;
  152. out:
  153. mutex_unlock(&crypt_stat->cs_mutex);
  154. return rc;
  155. }
  156. static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
  157. {
  158. struct file *lower_file = ecryptfs_file_to_lower(file);
  159. /*
  160. * Don't allow mmap on top of file systems that don't support it
  161. * natively. If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
  162. * allows recursive mounting, this will need to be extended.
  163. */
  164. if (!lower_file->f_op->mmap)
  165. return -ENODEV;
  166. return generic_file_mmap(file, vma);
  167. }
  168. /**
  169. * ecryptfs_open
  170. * @inode: inode specifying file to open
  171. * @file: Structure to return filled in
  172. *
  173. * Opens the file specified by inode.
  174. *
  175. * Returns zero on success; non-zero otherwise
  176. */
  177. static int ecryptfs_open(struct inode *inode, struct file *file)
  178. {
  179. int rc = 0;
  180. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  181. struct dentry *ecryptfs_dentry = file->f_path.dentry;
  182. /* Private value of ecryptfs_dentry allocated in
  183. * ecryptfs_lookup() */
  184. struct ecryptfs_file_info *file_info;
  185. /* Released in ecryptfs_release or end of function if failure */
  186. file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
  187. ecryptfs_set_file_private(file, file_info);
  188. if (!file_info) {
  189. ecryptfs_printk(KERN_ERR,
  190. "Error attempting to allocate memory\n");
  191. rc = -ENOMEM;
  192. goto out;
  193. }
  194. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  195. mutex_lock(&crypt_stat->cs_mutex);
  196. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
  197. ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
  198. /* Policy code enabled in future release */
  199. crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
  200. | ECRYPTFS_ENCRYPTED);
  201. }
  202. mutex_unlock(&crypt_stat->cs_mutex);
  203. rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
  204. if (rc) {
  205. printk(KERN_ERR "%s: Error attempting to initialize "
  206. "the lower file for the dentry with name "
  207. "[%pd]; rc = [%d]\n", __func__,
  208. ecryptfs_dentry, rc);
  209. goto out_free;
  210. }
  211. if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
  212. == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
  213. rc = -EPERM;
  214. printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
  215. "file must hence be opened RO\n", __func__);
  216. goto out_put;
  217. }
  218. ecryptfs_set_file_lower(
  219. file, ecryptfs_inode_to_private(inode)->lower_file);
  220. rc = read_or_initialize_metadata(ecryptfs_dentry);
  221. if (rc)
  222. goto out_put;
  223. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
  224. "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
  225. (unsigned long long)i_size_read(inode));
  226. goto out;
  227. out_put:
  228. ecryptfs_put_lower_file(inode);
  229. out_free:
  230. kmem_cache_free(ecryptfs_file_info_cache,
  231. ecryptfs_file_to_private(file));
  232. out:
  233. return rc;
  234. }
  235. /**
  236. * ecryptfs_dir_open
  237. * @inode: inode specifying file to open
  238. * @file: Structure to return filled in
  239. *
  240. * Opens the file specified by inode.
  241. *
  242. * Returns zero on success; non-zero otherwise
  243. */
  244. static int ecryptfs_dir_open(struct inode *inode, struct file *file)
  245. {
  246. struct dentry *ecryptfs_dentry = file->f_path.dentry;
  247. /* Private value of ecryptfs_dentry allocated in
  248. * ecryptfs_lookup() */
  249. struct ecryptfs_file_info *file_info;
  250. struct file *lower_file;
  251. /* Released in ecryptfs_release or end of function if failure */
  252. file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
  253. ecryptfs_set_file_private(file, file_info);
  254. if (unlikely(!file_info)) {
  255. ecryptfs_printk(KERN_ERR,
  256. "Error attempting to allocate memory\n");
  257. return -ENOMEM;
  258. }
  259. lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
  260. file->f_flags, current_cred());
  261. if (IS_ERR(lower_file)) {
  262. printk(KERN_ERR "%s: Error attempting to initialize "
  263. "the lower file for the dentry with name "
  264. "[%pd]; rc = [%ld]\n", __func__,
  265. ecryptfs_dentry, PTR_ERR(lower_file));
  266. kmem_cache_free(ecryptfs_file_info_cache, file_info);
  267. return PTR_ERR(lower_file);
  268. }
  269. ecryptfs_set_file_lower(file, lower_file);
  270. return 0;
  271. }
  272. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  273. {
  274. struct file *lower_file = ecryptfs_file_to_lower(file);
  275. if (lower_file->f_op->flush) {
  276. filemap_write_and_wait(file->f_mapping);
  277. return lower_file->f_op->flush(lower_file, td);
  278. }
  279. return 0;
  280. }
  281. static int ecryptfs_release(struct inode *inode, struct file *file)
  282. {
  283. ecryptfs_put_lower_file(inode);
  284. kmem_cache_free(ecryptfs_file_info_cache,
  285. ecryptfs_file_to_private(file));
  286. return 0;
  287. }
  288. static int ecryptfs_dir_release(struct inode *inode, struct file *file)
  289. {
  290. fput(ecryptfs_file_to_lower(file));
  291. kmem_cache_free(ecryptfs_file_info_cache,
  292. ecryptfs_file_to_private(file));
  293. return 0;
  294. }
  295. static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
  296. {
  297. return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
  298. }
  299. static int
  300. ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  301. {
  302. int rc;
  303. rc = filemap_write_and_wait(file->f_mapping);
  304. if (rc)
  305. return rc;
  306. return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
  307. }
  308. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  309. {
  310. int rc = 0;
  311. struct file *lower_file = NULL;
  312. lower_file = ecryptfs_file_to_lower(file);
  313. if (lower_file->f_op->fasync)
  314. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  315. return rc;
  316. }
  317. static long
  318. ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  319. {
  320. struct file *lower_file = ecryptfs_file_to_lower(file);
  321. long rc = -ENOTTY;
  322. if (!lower_file->f_op->unlocked_ioctl)
  323. return rc;
  324. switch (cmd) {
  325. case FITRIM:
  326. case FS_IOC_GETFLAGS:
  327. case FS_IOC_SETFLAGS:
  328. case FS_IOC_GETVERSION:
  329. case FS_IOC_SETVERSION:
  330. rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
  331. fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
  332. return rc;
  333. default:
  334. return rc;
  335. }
  336. }
  337. #ifdef CONFIG_COMPAT
  338. static long
  339. ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  340. {
  341. struct file *lower_file = ecryptfs_file_to_lower(file);
  342. long rc = -ENOIOCTLCMD;
  343. if (!lower_file->f_op->compat_ioctl)
  344. return rc;
  345. switch (cmd) {
  346. case FS_IOC32_GETFLAGS:
  347. case FS_IOC32_SETFLAGS:
  348. case FS_IOC32_GETVERSION:
  349. case FS_IOC32_SETVERSION:
  350. rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
  351. fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
  352. return rc;
  353. default:
  354. return rc;
  355. }
  356. }
  357. #endif
  358. const struct file_operations ecryptfs_dir_fops = {
  359. .iterate_shared = ecryptfs_readdir,
  360. .read = generic_read_dir,
  361. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  362. #ifdef CONFIG_COMPAT
  363. .compat_ioctl = ecryptfs_compat_ioctl,
  364. #endif
  365. .open = ecryptfs_dir_open,
  366. .release = ecryptfs_dir_release,
  367. .fsync = ecryptfs_fsync,
  368. .llseek = ecryptfs_dir_llseek,
  369. };
  370. const struct file_operations ecryptfs_main_fops = {
  371. .llseek = generic_file_llseek,
  372. .read_iter = ecryptfs_read_update_atime,
  373. .write_iter = generic_file_write_iter,
  374. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  375. #ifdef CONFIG_COMPAT
  376. .compat_ioctl = ecryptfs_compat_ioctl,
  377. #endif
  378. .mmap = ecryptfs_mmap,
  379. .open = ecryptfs_open,
  380. .flush = ecryptfs_flush,
  381. .release = ecryptfs_release,
  382. .fsync = ecryptfs_fsync,
  383. .fasync = ecryptfs_fasync,
  384. .splice_read = generic_file_splice_read,
  385. };