file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * eCryptfs: Linux filesystem encryption layer
  4. *
  5. * Copyright (C) 1997-2004 Erez Zadok
  6. * Copyright (C) 2001-2004 Stony Brook University
  7. * Copyright (C) 2004-2007 International Business Machines Corp.
  8. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  9. * Michael C. Thompson <mcthomps@us.ibm.com>
  10. */
  11. #include <linux/file.h>
  12. #include <linux/poll.h>
  13. #include <linux/slab.h>
  14. #include <linux/mount.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/security.h>
  17. #include <linux/compat.h>
  18. #include <linux/fs_stack.h>
  19. #include "ecryptfs_kernel.h"
  20. /*
  21. * ecryptfs_read_update_atime
  22. *
  23. * generic_file_read updates the atime of upper layer inode. But, it
  24. * doesn't give us a chance to update the atime of the lower layer
  25. * inode. This function is a wrapper to generic_file_read. It
  26. * updates the atime of the lower level inode if generic_file_read
  27. * returns without any errors. This is to be used only for file reads.
  28. * The function to be used for directory reads is ecryptfs_read.
  29. */
  30. static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
  31. struct iov_iter *to)
  32. {
  33. ssize_t rc;
  34. const struct path *path;
  35. struct file *file = iocb->ki_filp;
  36. rc = generic_file_read_iter(iocb, to);
  37. if (rc >= 0) {
  38. path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
  39. touch_atime(path);
  40. }
  41. return rc;
  42. }
  43. /*
  44. * ecryptfs_splice_read_update_atime
  45. *
  46. * filemap_splice_read updates the atime of upper layer inode. But, it
  47. * doesn't give us a chance to update the atime of the lower layer inode. This
  48. * function is a wrapper to generic_file_read. It updates the atime of the
  49. * lower level inode if generic_file_read returns without any errors. This is
  50. * to be used only for file reads. The function to be used for directory reads
  51. * is ecryptfs_read.
  52. */
  53. static ssize_t ecryptfs_splice_read_update_atime(struct file *in, loff_t *ppos,
  54. struct pipe_inode_info *pipe,
  55. size_t len, unsigned int flags)
  56. {
  57. ssize_t rc;
  58. const struct path *path;
  59. rc = filemap_splice_read(in, ppos, pipe, len, flags);
  60. if (rc >= 0) {
  61. path = ecryptfs_dentry_to_lower_path(in->f_path.dentry);
  62. touch_atime(path);
  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 bool
  75. ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
  76. int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
  77. {
  78. struct ecryptfs_getdents_callback *buf =
  79. container_of(ctx, struct ecryptfs_getdents_callback, ctx);
  80. size_t name_size;
  81. char *name;
  82. int err;
  83. bool res;
  84. buf->filldir_called++;
  85. err = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
  86. buf->sb, lower_name,
  87. lower_namelen);
  88. if (err) {
  89. if (err != -EINVAL) {
  90. ecryptfs_printk(KERN_DEBUG,
  91. "%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
  92. __func__, lower_name, err);
  93. return false;
  94. }
  95. /* Mask -EINVAL errors as these are most likely due a plaintext
  96. * filename present in the lower filesystem despite filename
  97. * encryption being enabled. One unavoidable example would be
  98. * the "lost+found" dentry in the root directory of an Ext4
  99. * filesystem.
  100. */
  101. return true;
  102. }
  103. buf->caller->pos = buf->ctx.pos;
  104. res = dir_emit(buf->caller, name, name_size, ino, d_type);
  105. kfree(name);
  106. if (res)
  107. buf->entries_written++;
  108. return res;
  109. }
  110. /**
  111. * ecryptfs_readdir
  112. * @file: The eCryptfs directory file
  113. * @ctx: The actor to feed the entries to
  114. */
  115. static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
  116. {
  117. int rc;
  118. struct file *lower_file;
  119. struct inode *inode = file_inode(file);
  120. struct ecryptfs_getdents_callback buf = {
  121. .ctx.actor = ecryptfs_filldir,
  122. .caller = ctx,
  123. .sb = inode->i_sb,
  124. };
  125. lower_file = ecryptfs_file_to_lower(file);
  126. rc = iterate_dir(lower_file, &buf.ctx);
  127. ctx->pos = buf.ctx.pos;
  128. if (rc >= 0 && (buf.entries_written || !buf.filldir_called))
  129. fsstack_copy_attr_atime(inode, file_inode(lower_file));
  130. return rc;
  131. }
  132. struct kmem_cache *ecryptfs_file_info_cache;
  133. static int read_or_initialize_metadata(struct dentry *dentry)
  134. {
  135. struct inode *inode = d_inode(dentry);
  136. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  137. struct ecryptfs_crypt_stat *crypt_stat;
  138. int rc;
  139. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  140. mount_crypt_stat = &ecryptfs_superblock_to_private(
  141. inode->i_sb)->mount_crypt_stat;
  142. mutex_lock(&crypt_stat->cs_mutex);
  143. if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
  144. crypt_stat->flags & ECRYPTFS_KEY_VALID) {
  145. rc = 0;
  146. goto out;
  147. }
  148. rc = ecryptfs_read_metadata(dentry);
  149. if (!rc)
  150. goto out;
  151. if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
  152. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  153. | ECRYPTFS_ENCRYPTED);
  154. rc = 0;
  155. goto out;
  156. }
  157. if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
  158. !i_size_read(ecryptfs_inode_to_lower(inode))) {
  159. rc = ecryptfs_initialize_file(dentry, inode);
  160. if (!rc)
  161. goto out;
  162. }
  163. rc = -EIO;
  164. out:
  165. mutex_unlock(&crypt_stat->cs_mutex);
  166. return rc;
  167. }
  168. static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
  169. {
  170. struct file *lower_file = ecryptfs_file_to_lower(file);
  171. /*
  172. * Don't allow mmap on top of file systems that don't support it
  173. * natively. If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
  174. * allows recursive mounting, this will need to be extended.
  175. */
  176. if (!lower_file->f_op->mmap)
  177. return -ENODEV;
  178. return generic_file_mmap(file, vma);
  179. }
  180. /**
  181. * ecryptfs_open
  182. * @inode: inode specifying file to open
  183. * @file: Structure to return filled in
  184. *
  185. * Opens the file specified by inode.
  186. *
  187. * Returns zero on success; non-zero otherwise
  188. */
  189. static int ecryptfs_open(struct inode *inode, struct file *file)
  190. {
  191. int rc = 0;
  192. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  193. struct dentry *ecryptfs_dentry = file->f_path.dentry;
  194. /* Private value of ecryptfs_dentry allocated in
  195. * ecryptfs_lookup() */
  196. struct ecryptfs_file_info *file_info;
  197. /* Released in ecryptfs_release or end of function if failure */
  198. file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
  199. ecryptfs_set_file_private(file, file_info);
  200. if (!file_info) {
  201. ecryptfs_printk(KERN_ERR,
  202. "Error attempting to allocate memory\n");
  203. rc = -ENOMEM;
  204. goto out;
  205. }
  206. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  207. mutex_lock(&crypt_stat->cs_mutex);
  208. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
  209. ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
  210. /* Policy code enabled in future release */
  211. crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
  212. | ECRYPTFS_ENCRYPTED);
  213. }
  214. mutex_unlock(&crypt_stat->cs_mutex);
  215. rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
  216. if (rc) {
  217. printk(KERN_ERR "%s: Error attempting to initialize "
  218. "the lower file for the dentry with name "
  219. "[%pd]; rc = [%d]\n", __func__,
  220. ecryptfs_dentry, rc);
  221. goto out_free;
  222. }
  223. if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
  224. == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
  225. rc = -EPERM;
  226. printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
  227. "file must hence be opened RO\n", __func__);
  228. goto out_put;
  229. }
  230. ecryptfs_set_file_lower(
  231. file, ecryptfs_inode_to_private(inode)->lower_file);
  232. rc = read_or_initialize_metadata(ecryptfs_dentry);
  233. if (rc)
  234. goto out_put;
  235. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
  236. "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
  237. (unsigned long long)i_size_read(inode));
  238. goto out;
  239. out_put:
  240. ecryptfs_put_lower_file(inode);
  241. out_free:
  242. kmem_cache_free(ecryptfs_file_info_cache,
  243. ecryptfs_file_to_private(file));
  244. out:
  245. return rc;
  246. }
  247. /**
  248. * ecryptfs_dir_open
  249. * @inode: inode specifying file to open
  250. * @file: Structure to return filled in
  251. *
  252. * Opens the file specified by inode.
  253. *
  254. * Returns zero on success; non-zero otherwise
  255. */
  256. static int ecryptfs_dir_open(struct inode *inode, struct file *file)
  257. {
  258. struct dentry *ecryptfs_dentry = file->f_path.dentry;
  259. /* Private value of ecryptfs_dentry allocated in
  260. * ecryptfs_lookup() */
  261. struct ecryptfs_file_info *file_info;
  262. struct file *lower_file;
  263. /* Released in ecryptfs_release or end of function if failure */
  264. file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
  265. ecryptfs_set_file_private(file, file_info);
  266. if (unlikely(!file_info)) {
  267. ecryptfs_printk(KERN_ERR,
  268. "Error attempting to allocate memory\n");
  269. return -ENOMEM;
  270. }
  271. lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
  272. file->f_flags, current_cred());
  273. if (IS_ERR(lower_file)) {
  274. printk(KERN_ERR "%s: Error attempting to initialize "
  275. "the lower file for the dentry with name "
  276. "[%pd]; rc = [%ld]\n", __func__,
  277. ecryptfs_dentry, PTR_ERR(lower_file));
  278. kmem_cache_free(ecryptfs_file_info_cache, file_info);
  279. return PTR_ERR(lower_file);
  280. }
  281. ecryptfs_set_file_lower(file, lower_file);
  282. return 0;
  283. }
  284. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  285. {
  286. struct file *lower_file = ecryptfs_file_to_lower(file);
  287. if (lower_file->f_op->flush) {
  288. filemap_write_and_wait(file->f_mapping);
  289. return lower_file->f_op->flush(lower_file, td);
  290. }
  291. return 0;
  292. }
  293. static int ecryptfs_release(struct inode *inode, struct file *file)
  294. {
  295. ecryptfs_put_lower_file(inode);
  296. kmem_cache_free(ecryptfs_file_info_cache,
  297. ecryptfs_file_to_private(file));
  298. return 0;
  299. }
  300. static int ecryptfs_dir_release(struct inode *inode, struct file *file)
  301. {
  302. fput(ecryptfs_file_to_lower(file));
  303. kmem_cache_free(ecryptfs_file_info_cache,
  304. ecryptfs_file_to_private(file));
  305. return 0;
  306. }
  307. static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
  308. {
  309. return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
  310. }
  311. static int
  312. ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  313. {
  314. int rc;
  315. rc = file_write_and_wait(file);
  316. if (rc)
  317. return rc;
  318. return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
  319. }
  320. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  321. {
  322. int rc = 0;
  323. struct file *lower_file = NULL;
  324. lower_file = ecryptfs_file_to_lower(file);
  325. if (lower_file->f_op->fasync)
  326. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  327. return rc;
  328. }
  329. static long
  330. ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  331. {
  332. struct file *lower_file = ecryptfs_file_to_lower(file);
  333. long rc = -ENOTTY;
  334. if (!lower_file->f_op->unlocked_ioctl)
  335. return rc;
  336. switch (cmd) {
  337. case FITRIM:
  338. case FS_IOC_GETFLAGS:
  339. case FS_IOC_SETFLAGS:
  340. case FS_IOC_GETVERSION:
  341. case FS_IOC_SETVERSION:
  342. rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
  343. fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
  344. return rc;
  345. default:
  346. return rc;
  347. }
  348. }
  349. #ifdef CONFIG_COMPAT
  350. static long
  351. ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  352. {
  353. struct file *lower_file = ecryptfs_file_to_lower(file);
  354. long rc = -ENOIOCTLCMD;
  355. if (!lower_file->f_op->compat_ioctl)
  356. return rc;
  357. switch (cmd) {
  358. case FITRIM:
  359. case FS_IOC32_GETFLAGS:
  360. case FS_IOC32_SETFLAGS:
  361. case FS_IOC32_GETVERSION:
  362. case FS_IOC32_SETVERSION:
  363. rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
  364. fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
  365. return rc;
  366. default:
  367. return rc;
  368. }
  369. }
  370. #endif
  371. const struct file_operations ecryptfs_dir_fops = {
  372. .iterate_shared = ecryptfs_readdir,
  373. .read = generic_read_dir,
  374. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  375. #ifdef CONFIG_COMPAT
  376. .compat_ioctl = ecryptfs_compat_ioctl,
  377. #endif
  378. .open = ecryptfs_dir_open,
  379. .release = ecryptfs_dir_release,
  380. .fsync = ecryptfs_fsync,
  381. .llseek = ecryptfs_dir_llseek,
  382. };
  383. const struct file_operations ecryptfs_main_fops = {
  384. .llseek = generic_file_llseek,
  385. .read_iter = ecryptfs_read_update_atime,
  386. .write_iter = generic_file_write_iter,
  387. .unlocked_ioctl = ecryptfs_unlocked_ioctl,
  388. #ifdef CONFIG_COMPAT
  389. .compat_ioctl = ecryptfs_compat_ioctl,
  390. #endif
  391. .mmap = ecryptfs_mmap,
  392. .open = ecryptfs_open,
  393. .flush = ecryptfs_flush,
  394. .release = ecryptfs_release,
  395. .fsync = ecryptfs_fsync,
  396. .fasync = ecryptfs_fasync,
  397. .splice_read = ecryptfs_splice_read_update_atime,
  398. };