file.c 14 KB

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