file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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-2006 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/smp_lock.h>
  31. #include <linux/compat.h>
  32. #include "ecryptfs_kernel.h"
  33. /**
  34. * ecryptfs_llseek
  35. * @file: File we are seeking in
  36. * @offset: The offset to seek to
  37. * @origin: 2 - offset from i_size; 1 - offset from f_pos
  38. *
  39. * Returns the position we have seeked to, or negative on error
  40. */
  41. static loff_t ecryptfs_llseek(struct file *file, loff_t offset, int origin)
  42. {
  43. loff_t rv;
  44. loff_t new_end_pos;
  45. int rc;
  46. int expanding_file = 0;
  47. struct inode *inode = file->f_mapping->host;
  48. /* If our offset is past the end of our file, we're going to
  49. * need to grow it so we have a valid length of 0's */
  50. new_end_pos = offset;
  51. switch (origin) {
  52. case 2:
  53. new_end_pos += i_size_read(inode);
  54. expanding_file = 1;
  55. break;
  56. case 1:
  57. new_end_pos += file->f_pos;
  58. if (new_end_pos > i_size_read(inode)) {
  59. ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) "
  60. "> i_size_read(inode)(=[0x%.16x])\n",
  61. new_end_pos, i_size_read(inode));
  62. expanding_file = 1;
  63. }
  64. break;
  65. default:
  66. if (new_end_pos > i_size_read(inode)) {
  67. ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) "
  68. "> i_size_read(inode)(=[0x%.16x])\n",
  69. new_end_pos, i_size_read(inode));
  70. expanding_file = 1;
  71. }
  72. }
  73. ecryptfs_printk(KERN_DEBUG, "new_end_pos = [0x%.16x]\n", new_end_pos);
  74. if (expanding_file) {
  75. rc = ecryptfs_truncate(file->f_dentry, new_end_pos);
  76. if (rc) {
  77. rv = rc;
  78. ecryptfs_printk(KERN_ERR, "Error on attempt to "
  79. "truncate to (higher) offset [0x%.16x];"
  80. " rc = [%d]\n", new_end_pos, rc);
  81. goto out;
  82. }
  83. }
  84. rv = generic_file_llseek(file, offset, origin);
  85. out:
  86. return rv;
  87. }
  88. /**
  89. * ecryptfs_read_update_atime
  90. *
  91. * generic_file_read updates the atime of upper layer inode. But, it
  92. * doesn't give us a chance to update the atime of the lower layer
  93. * inode. This function is a wrapper to generic_file_read. It
  94. * updates the atime of the lower level inode if generic_file_read
  95. * returns without any errors. This is to be used only for file reads.
  96. * The function to be used for directory reads is ecryptfs_read.
  97. */
  98. static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
  99. const struct iovec *iov,
  100. unsigned long nr_segs, loff_t pos)
  101. {
  102. int rc;
  103. struct dentry *lower_dentry;
  104. struct vfsmount *lower_vfsmount;
  105. struct file *file = iocb->ki_filp;
  106. rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
  107. /*
  108. * Even though this is a async interface, we need to wait
  109. * for IO to finish to update atime
  110. */
  111. if (-EIOCBQUEUED == rc)
  112. rc = wait_on_sync_kiocb(iocb);
  113. if (rc >= 0) {
  114. lower_dentry = ecryptfs_dentry_to_lower(file->f_dentry);
  115. lower_vfsmount = ecryptfs_dentry_to_lower_mnt(file->f_dentry);
  116. touch_atime(lower_vfsmount, lower_dentry);
  117. }
  118. return rc;
  119. }
  120. struct ecryptfs_getdents_callback {
  121. void *dirent;
  122. struct dentry *dentry;
  123. filldir_t filldir;
  124. int err;
  125. int filldir_called;
  126. int entries_written;
  127. };
  128. /* Inspired by generic filldir in fs/readir.c */
  129. static int
  130. ecryptfs_filldir(void *dirent, const char *name, int namelen, loff_t offset,
  131. u64 ino, unsigned int d_type)
  132. {
  133. struct ecryptfs_crypt_stat *crypt_stat;
  134. struct ecryptfs_getdents_callback *buf =
  135. (struct ecryptfs_getdents_callback *)dirent;
  136. int rc;
  137. int decoded_length;
  138. char *decoded_name;
  139. crypt_stat = ecryptfs_dentry_to_private(buf->dentry)->crypt_stat;
  140. buf->filldir_called++;
  141. decoded_length = ecryptfs_decode_filename(crypt_stat, name, namelen,
  142. &decoded_name);
  143. if (decoded_length < 0) {
  144. rc = decoded_length;
  145. goto out;
  146. }
  147. rc = buf->filldir(buf->dirent, decoded_name, decoded_length, offset,
  148. ino, d_type);
  149. kfree(decoded_name);
  150. if (rc >= 0)
  151. buf->entries_written++;
  152. out:
  153. return rc;
  154. }
  155. /**
  156. * ecryptfs_readdir
  157. * @file: The ecryptfs file struct
  158. * @dirent: Directory entry
  159. * @filldir: The filldir callback function
  160. */
  161. static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  162. {
  163. int rc;
  164. struct file *lower_file;
  165. struct inode *inode;
  166. struct ecryptfs_getdents_callback buf;
  167. lower_file = ecryptfs_file_to_lower(file);
  168. lower_file->f_pos = file->f_pos;
  169. inode = file->f_dentry->d_inode;
  170. memset(&buf, 0, sizeof(buf));
  171. buf.dirent = dirent;
  172. buf.dentry = file->f_dentry;
  173. buf.filldir = filldir;
  174. retry:
  175. buf.filldir_called = 0;
  176. buf.entries_written = 0;
  177. buf.err = 0;
  178. rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
  179. if (buf.err)
  180. rc = buf.err;
  181. if (buf.filldir_called && !buf.entries_written)
  182. goto retry;
  183. file->f_pos = lower_file->f_pos;
  184. if (rc >= 0)
  185. ecryptfs_copy_attr_atime(inode, lower_file->f_dentry->d_inode);
  186. return rc;
  187. }
  188. struct kmem_cache *ecryptfs_file_info_cache;
  189. /**
  190. * ecryptfs_open
  191. * @inode: inode speciying file to open
  192. * @file: Structure to return filled in
  193. *
  194. * Opens the file specified by inode.
  195. *
  196. * Returns zero on success; non-zero otherwise
  197. */
  198. static int ecryptfs_open(struct inode *inode, struct file *file)
  199. {
  200. int rc = 0;
  201. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  202. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  203. struct dentry *ecryptfs_dentry = file->f_dentry;
  204. /* Private value of ecryptfs_dentry allocated in
  205. * ecryptfs_lookup() */
  206. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  207. struct inode *lower_inode = NULL;
  208. struct file *lower_file = NULL;
  209. struct vfsmount *lower_mnt;
  210. struct ecryptfs_file_info *file_info;
  211. int lower_flags;
  212. /* Released in ecryptfs_release or end of function if failure */
  213. file_info = kmem_cache_alloc(ecryptfs_file_info_cache, SLAB_KERNEL);
  214. ecryptfs_set_file_private(file, file_info);
  215. if (!file_info) {
  216. ecryptfs_printk(KERN_ERR,
  217. "Error attempting to allocate memory\n");
  218. rc = -ENOMEM;
  219. goto out;
  220. }
  221. memset(file_info, 0, sizeof(*file_info));
  222. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  223. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  224. mount_crypt_stat = &ecryptfs_superblock_to_private(
  225. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  226. mutex_lock(&crypt_stat->cs_mutex);
  227. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED)) {
  228. ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
  229. /* Policy code enabled in future release */
  230. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED);
  231. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
  232. }
  233. mutex_unlock(&crypt_stat->cs_mutex);
  234. /* This mntget & dget is undone via fput when the file is released */
  235. dget(lower_dentry);
  236. lower_flags = file->f_flags;
  237. if ((lower_flags & O_ACCMODE) == O_WRONLY)
  238. lower_flags = (lower_flags & O_ACCMODE) | O_RDWR;
  239. if (file->f_flags & O_APPEND)
  240. lower_flags &= ~O_APPEND;
  241. lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  242. mntget(lower_mnt);
  243. /* Corresponding fput() in ecryptfs_release() */
  244. lower_file = dentry_open(lower_dentry, lower_mnt, lower_flags);
  245. if (IS_ERR(lower_file)) {
  246. rc = PTR_ERR(lower_file);
  247. ecryptfs_printk(KERN_ERR, "Error opening lower file\n");
  248. goto out_puts;
  249. }
  250. ecryptfs_set_file_lower(file, lower_file);
  251. /* Isn't this check the same as the one in lookup? */
  252. lower_inode = lower_dentry->d_inode;
  253. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  254. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  255. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
  256. rc = 0;
  257. goto out;
  258. }
  259. mutex_lock(&crypt_stat->cs_mutex);
  260. if (i_size_read(lower_inode) < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
  261. if (!(mount_crypt_stat->flags
  262. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  263. rc = -EIO;
  264. printk(KERN_WARNING "Attempt to read file that is "
  265. "not in a valid eCryptfs format, and plaintext "
  266. "passthrough mode is not enabled; returning "
  267. "-EIO\n");
  268. mutex_unlock(&crypt_stat->cs_mutex);
  269. goto out_puts;
  270. }
  271. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  272. rc = 0;
  273. mutex_unlock(&crypt_stat->cs_mutex);
  274. goto out;
  275. } else if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  276. ECRYPTFS_POLICY_APPLIED)
  277. || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  278. ECRYPTFS_KEY_VALID)) {
  279. rc = ecryptfs_read_headers(ecryptfs_dentry, lower_file);
  280. if (rc) {
  281. ecryptfs_printk(KERN_DEBUG,
  282. "Valid headers not found\n");
  283. if (!(mount_crypt_stat->flags
  284. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  285. rc = -EIO;
  286. printk(KERN_WARNING "Attempt to read file that "
  287. "is not in a valid eCryptfs format, "
  288. "and plaintext passthrough mode is not "
  289. "enabled; returning -EIO\n");
  290. mutex_unlock(&crypt_stat->cs_mutex);
  291. goto out_puts;
  292. }
  293. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
  294. ECRYPTFS_ENCRYPTED);
  295. rc = 0;
  296. mutex_unlock(&crypt_stat->cs_mutex);
  297. goto out;
  298. }
  299. }
  300. mutex_unlock(&crypt_stat->cs_mutex);
  301. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] "
  302. "size: [0x%.16x]\n", inode, inode->i_ino,
  303. i_size_read(inode));
  304. ecryptfs_set_file_lower(file, lower_file);
  305. goto out;
  306. out_puts:
  307. mntput(lower_mnt);
  308. dput(lower_dentry);
  309. kmem_cache_free(ecryptfs_file_info_cache,
  310. ecryptfs_file_to_private(file));
  311. out:
  312. return rc;
  313. }
  314. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  315. {
  316. int rc = 0;
  317. struct file *lower_file = NULL;
  318. lower_file = ecryptfs_file_to_lower(file);
  319. if (lower_file->f_op && lower_file->f_op->flush)
  320. rc = lower_file->f_op->flush(lower_file, td);
  321. return rc;
  322. }
  323. static int ecryptfs_release(struct inode *inode, struct file *file)
  324. {
  325. struct file *lower_file = ecryptfs_file_to_lower(file);
  326. struct ecryptfs_file_info *file_info = ecryptfs_file_to_private(file);
  327. struct inode *lower_inode = ecryptfs_inode_to_lower(inode);
  328. fput(lower_file);
  329. inode->i_blocks = lower_inode->i_blocks;
  330. kmem_cache_free(ecryptfs_file_info_cache, file_info);
  331. return 0;
  332. }
  333. static int
  334. ecryptfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  335. {
  336. struct file *lower_file = ecryptfs_file_to_lower(file);
  337. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  338. struct inode *lower_inode = lower_dentry->d_inode;
  339. int rc = -EINVAL;
  340. if (lower_inode->i_fop->fsync) {
  341. mutex_lock(&lower_inode->i_mutex);
  342. rc = lower_inode->i_fop->fsync(lower_file, lower_dentry,
  343. datasync);
  344. mutex_unlock(&lower_inode->i_mutex);
  345. }
  346. return rc;
  347. }
  348. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  349. {
  350. int rc = 0;
  351. struct file *lower_file = NULL;
  352. lower_file = ecryptfs_file_to_lower(file);
  353. if (lower_file->f_op && lower_file->f_op->fasync)
  354. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  355. return rc;
  356. }
  357. static ssize_t ecryptfs_sendfile(struct file *file, loff_t * ppos,
  358. size_t count, read_actor_t actor, void *target)
  359. {
  360. struct file *lower_file = NULL;
  361. int rc = -EINVAL;
  362. lower_file = ecryptfs_file_to_lower(file);
  363. if (lower_file->f_op && lower_file->f_op->sendfile)
  364. rc = lower_file->f_op->sendfile(lower_file, ppos, count,
  365. actor, target);
  366. return rc;
  367. }
  368. static int ecryptfs_ioctl(struct inode *inode, struct file *file,
  369. unsigned int cmd, unsigned long arg);
  370. const struct file_operations ecryptfs_dir_fops = {
  371. .readdir = ecryptfs_readdir,
  372. .ioctl = ecryptfs_ioctl,
  373. .mmap = generic_file_mmap,
  374. .open = ecryptfs_open,
  375. .flush = ecryptfs_flush,
  376. .release = ecryptfs_release,
  377. .fsync = ecryptfs_fsync,
  378. .fasync = ecryptfs_fasync,
  379. .sendfile = ecryptfs_sendfile,
  380. };
  381. const struct file_operations ecryptfs_main_fops = {
  382. .llseek = ecryptfs_llseek,
  383. .read = do_sync_read,
  384. .aio_read = ecryptfs_read_update_atime,
  385. .write = do_sync_write,
  386. .aio_write = generic_file_aio_write,
  387. .readdir = ecryptfs_readdir,
  388. .ioctl = ecryptfs_ioctl,
  389. .mmap = generic_file_mmap,
  390. .open = ecryptfs_open,
  391. .flush = ecryptfs_flush,
  392. .release = ecryptfs_release,
  393. .fsync = ecryptfs_fsync,
  394. .fasync = ecryptfs_fasync,
  395. .sendfile = ecryptfs_sendfile,
  396. };
  397. static int
  398. ecryptfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  399. unsigned long arg)
  400. {
  401. int rc = 0;
  402. struct file *lower_file = NULL;
  403. if (ecryptfs_file_to_private(file))
  404. lower_file = ecryptfs_file_to_lower(file);
  405. if (lower_file && lower_file->f_op && lower_file->f_op->ioctl)
  406. rc = lower_file->f_op->ioctl(ecryptfs_inode_to_lower(inode),
  407. lower_file, cmd, arg);
  408. else
  409. rc = -ENOTTY;
  410. return rc;
  411. }