file.c 12 KB

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