file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <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. /* Released in ecryptfs_release or end of function if failure */
  238. file_info = kmem_cache_alloc(ecryptfs_file_info_cache, GFP_KERNEL);
  239. ecryptfs_set_file_private(file, file_info);
  240. if (!file_info) {
  241. ecryptfs_printk(KERN_ERR,
  242. "Error attempting to allocate memory\n");
  243. rc = -ENOMEM;
  244. goto out;
  245. }
  246. memset(file_info, 0, sizeof(*file_info));
  247. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  248. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  249. mount_crypt_stat = &ecryptfs_superblock_to_private(
  250. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  251. mutex_lock(&crypt_stat->cs_mutex);
  252. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED)) {
  253. ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
  254. /* Policy code enabled in future release */
  255. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED);
  256. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
  257. }
  258. mutex_unlock(&crypt_stat->cs_mutex);
  259. lower_flags = file->f_flags;
  260. if ((lower_flags & O_ACCMODE) == O_WRONLY)
  261. lower_flags = (lower_flags & O_ACCMODE) | O_RDWR;
  262. if (file->f_flags & O_APPEND)
  263. lower_flags &= ~O_APPEND;
  264. lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  265. /* Corresponding fput() in ecryptfs_release() */
  266. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  267. lower_flags))) {
  268. ecryptfs_printk(KERN_ERR, "Error opening lower file\n");
  269. goto out_puts;
  270. }
  271. ecryptfs_set_file_lower(file, lower_file);
  272. /* Isn't this check the same as the one in lookup? */
  273. lower_inode = lower_dentry->d_inode;
  274. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  275. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  276. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
  277. rc = 0;
  278. goto out;
  279. }
  280. mutex_lock(&crypt_stat->cs_mutex);
  281. if (i_size_read(lower_inode) < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
  282. if (!(mount_crypt_stat->flags
  283. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  284. rc = -EIO;
  285. printk(KERN_WARNING "Attempt to read file that is "
  286. "not in a valid eCryptfs format, and plaintext "
  287. "passthrough mode is not enabled; returning "
  288. "-EIO\n");
  289. mutex_unlock(&crypt_stat->cs_mutex);
  290. goto out_puts;
  291. }
  292. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  293. rc = 0;
  294. mutex_unlock(&crypt_stat->cs_mutex);
  295. goto out;
  296. } else if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  297. ECRYPTFS_POLICY_APPLIED)
  298. || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
  299. ECRYPTFS_KEY_VALID)) {
  300. rc = ecryptfs_read_headers(ecryptfs_dentry, lower_file);
  301. if (rc) {
  302. ecryptfs_printk(KERN_DEBUG,
  303. "Valid headers not found\n");
  304. if (!(mount_crypt_stat->flags
  305. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  306. rc = -EIO;
  307. printk(KERN_WARNING "Attempt to read file that "
  308. "is not in a valid eCryptfs format, "
  309. "and plaintext passthrough mode is not "
  310. "enabled; returning -EIO\n");
  311. mutex_unlock(&crypt_stat->cs_mutex);
  312. goto out_puts;
  313. }
  314. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
  315. ECRYPTFS_ENCRYPTED);
  316. rc = 0;
  317. mutex_unlock(&crypt_stat->cs_mutex);
  318. goto out;
  319. }
  320. }
  321. mutex_unlock(&crypt_stat->cs_mutex);
  322. ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] "
  323. "size: [0x%.16x]\n", inode, inode->i_ino,
  324. i_size_read(inode));
  325. ecryptfs_set_file_lower(file, lower_file);
  326. goto out;
  327. out_puts:
  328. mntput(lower_mnt);
  329. dput(lower_dentry);
  330. kmem_cache_free(ecryptfs_file_info_cache,
  331. ecryptfs_file_to_private(file));
  332. out:
  333. return rc;
  334. }
  335. static int ecryptfs_flush(struct file *file, fl_owner_t td)
  336. {
  337. int rc = 0;
  338. struct file *lower_file = NULL;
  339. lower_file = ecryptfs_file_to_lower(file);
  340. if (lower_file->f_op && lower_file->f_op->flush)
  341. rc = lower_file->f_op->flush(lower_file, td);
  342. return rc;
  343. }
  344. static int ecryptfs_release(struct inode *inode, struct file *file)
  345. {
  346. struct file *lower_file = ecryptfs_file_to_lower(file);
  347. struct ecryptfs_file_info *file_info = ecryptfs_file_to_private(file);
  348. struct inode *lower_inode = ecryptfs_inode_to_lower(inode);
  349. int rc;
  350. if ((rc = ecryptfs_close_lower_file(lower_file))) {
  351. printk(KERN_ERR "Error closing lower_file\n");
  352. goto out;
  353. }
  354. inode->i_blocks = lower_inode->i_blocks;
  355. kmem_cache_free(ecryptfs_file_info_cache, file_info);
  356. out:
  357. return rc;
  358. }
  359. static int
  360. ecryptfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  361. {
  362. struct file *lower_file = ecryptfs_file_to_lower(file);
  363. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  364. struct inode *lower_inode = lower_dentry->d_inode;
  365. int rc = -EINVAL;
  366. if (lower_inode->i_fop->fsync) {
  367. mutex_lock(&lower_inode->i_mutex);
  368. rc = lower_inode->i_fop->fsync(lower_file, lower_dentry,
  369. datasync);
  370. mutex_unlock(&lower_inode->i_mutex);
  371. }
  372. return rc;
  373. }
  374. static int ecryptfs_fasync(int fd, struct file *file, int flag)
  375. {
  376. int rc = 0;
  377. struct file *lower_file = NULL;
  378. lower_file = ecryptfs_file_to_lower(file);
  379. if (lower_file->f_op && lower_file->f_op->fasync)
  380. rc = lower_file->f_op->fasync(fd, lower_file, flag);
  381. return rc;
  382. }
  383. static ssize_t ecryptfs_sendfile(struct file *file, loff_t * ppos,
  384. size_t count, read_actor_t actor, void *target)
  385. {
  386. struct file *lower_file = NULL;
  387. int rc = -EINVAL;
  388. lower_file = ecryptfs_file_to_lower(file);
  389. if (lower_file->f_op && lower_file->f_op->sendfile)
  390. rc = lower_file->f_op->sendfile(lower_file, ppos, count,
  391. actor, target);
  392. return rc;
  393. }
  394. static int ecryptfs_ioctl(struct inode *inode, struct file *file,
  395. unsigned int cmd, unsigned long arg);
  396. const struct file_operations ecryptfs_dir_fops = {
  397. .readdir = ecryptfs_readdir,
  398. .ioctl = ecryptfs_ioctl,
  399. .mmap = generic_file_mmap,
  400. .open = ecryptfs_open,
  401. .flush = ecryptfs_flush,
  402. .release = ecryptfs_release,
  403. .fsync = ecryptfs_fsync,
  404. .fasync = ecryptfs_fasync,
  405. .sendfile = ecryptfs_sendfile,
  406. };
  407. const struct file_operations ecryptfs_main_fops = {
  408. .llseek = ecryptfs_llseek,
  409. .read = do_sync_read,
  410. .aio_read = ecryptfs_read_update_atime,
  411. .write = do_sync_write,
  412. .aio_write = generic_file_aio_write,
  413. .readdir = ecryptfs_readdir,
  414. .ioctl = ecryptfs_ioctl,
  415. .mmap = generic_file_mmap,
  416. .open = ecryptfs_open,
  417. .flush = ecryptfs_flush,
  418. .release = ecryptfs_release,
  419. .fsync = ecryptfs_fsync,
  420. .fasync = ecryptfs_fasync,
  421. .sendfile = ecryptfs_sendfile,
  422. };
  423. static int
  424. ecryptfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  425. unsigned long arg)
  426. {
  427. int rc = 0;
  428. struct file *lower_file = NULL;
  429. if (ecryptfs_file_to_private(file))
  430. lower_file = ecryptfs_file_to_lower(file);
  431. if (lower_file && lower_file->f_op && lower_file->f_op->ioctl)
  432. rc = lower_file->f_op->ioctl(ecryptfs_inode_to_lower(inode),
  433. lower_file, cmd, arg);
  434. else
  435. rc = -ENOTTY;
  436. return rc;
  437. }