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