file.c 13 KB

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