1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/file.h>
27#include <linux/poll.h>
28#include <linux/slab.h>
29#include <linux/mount.h>
30#include <linux/pagemap.h>
31#include <linux/security.h>
32#include <linux/compat.h>
33#include <linux/fs_stack.h>
34#include <linux/aio.h>
35#include "ecryptfs_kernel.h"
36
37
38
39
40
41
42
43
44
45
46
47static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
48 const struct iovec *iov,
49 unsigned long nr_segs, loff_t pos)
50{
51 ssize_t rc;
52 struct path lower;
53 struct file *file = iocb->ki_filp;
54
55 rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
56
57
58
59
60 if (-EIOCBQUEUED == rc)
61 rc = wait_on_sync_kiocb(iocb);
62 if (rc >= 0) {
63 lower.dentry = ecryptfs_dentry_to_lower(file->f_path.dentry);
64 lower.mnt = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
65 touch_atime(&lower);
66 }
67 return rc;
68}
69
70struct ecryptfs_getdents_callback {
71 void *dirent;
72 struct dentry *dentry;
73 filldir_t filldir;
74 int filldir_called;
75 int entries_written;
76};
77
78
79static int
80ecryptfs_filldir(void *dirent, const char *lower_name, int lower_namelen,
81 loff_t offset, u64 ino, unsigned int d_type)
82{
83 struct ecryptfs_getdents_callback *buf =
84 (struct ecryptfs_getdents_callback *)dirent;
85 size_t name_size;
86 char *name;
87 int rc;
88
89 buf->filldir_called++;
90 rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
91 buf->dentry, lower_name,
92 lower_namelen);
93 if (rc) {
94 printk(KERN_ERR "%s: Error attempting to decode and decrypt "
95 "filename [%s]; rc = [%d]\n", __func__, lower_name,
96 rc);
97 goto out;
98 }
99 rc = buf->filldir(buf->dirent, name, name_size, offset, ino, d_type);
100 kfree(name);
101 if (rc >= 0)
102 buf->entries_written++;
103out:
104 return rc;
105}
106
107
108
109
110
111
112
113static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
114{
115 int rc;
116 struct file *lower_file;
117 struct inode *inode;
118 struct ecryptfs_getdents_callback buf;
119
120 lower_file = ecryptfs_file_to_lower(file);
121 lower_file->f_pos = file->f_pos;
122 inode = file_inode(file);
123 memset(&buf, 0, sizeof(buf));
124 buf.dirent = dirent;
125 buf.dentry = file->f_path.dentry;
126 buf.filldir = filldir;
127 buf.filldir_called = 0;
128 buf.entries_written = 0;
129 rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
130 file->f_pos = lower_file->f_pos;
131 if (rc < 0)
132 goto out;
133 if (buf.filldir_called && !buf.entries_written)
134 goto out;
135 if (rc >= 0)
136 fsstack_copy_attr_atime(inode,
137 file_inode(lower_file));
138out:
139 return rc;
140}
141
142struct kmem_cache *ecryptfs_file_info_cache;
143
144static int read_or_initialize_metadata(struct dentry *dentry)
145{
146 struct inode *inode = dentry->d_inode;
147 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
148 struct ecryptfs_crypt_stat *crypt_stat;
149 int rc;
150
151 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
152 mount_crypt_stat = &ecryptfs_superblock_to_private(
153 inode->i_sb)->mount_crypt_stat;
154 mutex_lock(&crypt_stat->cs_mutex);
155
156 if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
157 crypt_stat->flags & ECRYPTFS_KEY_VALID) {
158 rc = 0;
159 goto out;
160 }
161
162 rc = ecryptfs_read_metadata(dentry);
163 if (!rc)
164 goto out;
165
166 if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
167 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
168 | ECRYPTFS_ENCRYPTED);
169 rc = 0;
170 goto out;
171 }
172
173 if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
174 !i_size_read(ecryptfs_inode_to_lower(inode))) {
175 rc = ecryptfs_initialize_file(dentry, inode);
176 if (!rc)
177 goto out;
178 }
179
180 rc = -EIO;
181out:
182 mutex_unlock(&crypt_stat->cs_mutex);
183 return rc;
184}
185
186
187
188
189
190
191
192
193
194
195static int ecryptfs_open(struct inode *inode, struct file *file)
196{
197 int rc = 0;
198 struct ecryptfs_crypt_stat *crypt_stat = NULL;
199 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
200 struct dentry *ecryptfs_dentry = file->f_path.dentry;
201
202
203 struct ecryptfs_file_info *file_info;
204
205 mount_crypt_stat = &ecryptfs_superblock_to_private(
206 ecryptfs_dentry->d_sb)->mount_crypt_stat;
207 if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
208 && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
209 || (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
210 || (file->f_flags & O_APPEND))) {
211 printk(KERN_WARNING "Mount has encrypted view enabled; "
212 "files may only be read\n");
213 rc = -EPERM;
214 goto out;
215 }
216
217 file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
218 ecryptfs_set_file_private(file, file_info);
219 if (!file_info) {
220 ecryptfs_printk(KERN_ERR,
221 "Error attempting to allocate memory\n");
222 rc = -ENOMEM;
223 goto out;
224 }
225 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
226 mutex_lock(&crypt_stat->cs_mutex);
227 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
228 ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
229
230 crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
231 | ECRYPTFS_ENCRYPTED);
232 }
233 mutex_unlock(&crypt_stat->cs_mutex);
234 rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
235 if (rc) {
236 printk(KERN_ERR "%s: Error attempting to initialize "
237 "the lower file for the dentry with name "
238 "[%s]; rc = [%d]\n", __func__,
239 ecryptfs_dentry->d_name.name, rc);
240 goto out_free;
241 }
242 if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
243 == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
244 rc = -EPERM;
245 printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
246 "file must hence be opened RO\n", __func__);
247 goto out_put;
248 }
249 ecryptfs_set_file_lower(
250 file, ecryptfs_inode_to_private(inode)->lower_file);
251 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
252 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
253 mutex_lock(&crypt_stat->cs_mutex);
254 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
255 mutex_unlock(&crypt_stat->cs_mutex);
256 rc = 0;
257 goto out;
258 }
259 rc = read_or_initialize_metadata(ecryptfs_dentry);
260 if (rc)
261 goto out_put;
262 ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
263 "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
264 (unsigned long long)i_size_read(inode));
265 goto out;
266out_put:
267 ecryptfs_put_lower_file(inode);
268out_free:
269 kmem_cache_free(ecryptfs_file_info_cache,
270 ecryptfs_file_to_private(file));
271out:
272 return rc;
273}
274
275static int ecryptfs_flush(struct file *file, fl_owner_t td)
276{
277 struct file *lower_file = ecryptfs_file_to_lower(file);
278
279 if (lower_file->f_op && lower_file->f_op->flush) {
280 filemap_write_and_wait(file->f_mapping);
281 return lower_file->f_op->flush(lower_file, td);
282 }
283
284 return 0;
285}
286
287static int ecryptfs_release(struct inode *inode, struct file *file)
288{
289 ecryptfs_put_lower_file(inode);
290 kmem_cache_free(ecryptfs_file_info_cache,
291 ecryptfs_file_to_private(file));
292 return 0;
293}
294
295static int
296ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
297{
298 int rc;
299
300 rc = filemap_write_and_wait(file->f_mapping);
301 if (rc)
302 return rc;
303
304 return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
305}
306
307static int ecryptfs_fasync(int fd, struct file *file, int flag)
308{
309 int rc = 0;
310 struct file *lower_file = NULL;
311
312 lower_file = ecryptfs_file_to_lower(file);
313 if (lower_file->f_op && lower_file->f_op->fasync)
314 rc = lower_file->f_op->fasync(fd, lower_file, flag);
315 return rc;
316}
317
318static long
319ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
320{
321 struct file *lower_file = NULL;
322 long rc = -ENOTTY;
323
324 if (ecryptfs_file_to_private(file))
325 lower_file = ecryptfs_file_to_lower(file);
326 if (lower_file && lower_file->f_op && lower_file->f_op->unlocked_ioctl)
327 rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
328 return rc;
329}
330
331#ifdef CONFIG_COMPAT
332static long
333ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
334{
335 struct file *lower_file = NULL;
336 long rc = -ENOIOCTLCMD;
337
338 if (ecryptfs_file_to_private(file))
339 lower_file = ecryptfs_file_to_lower(file);
340 if (lower_file && lower_file->f_op && lower_file->f_op->compat_ioctl)
341 rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
342 return rc;
343}
344#endif
345
346const struct file_operations ecryptfs_dir_fops = {
347 .readdir = ecryptfs_readdir,
348 .read = generic_read_dir,
349 .unlocked_ioctl = ecryptfs_unlocked_ioctl,
350#ifdef CONFIG_COMPAT
351 .compat_ioctl = ecryptfs_compat_ioctl,
352#endif
353 .open = ecryptfs_open,
354 .flush = ecryptfs_flush,
355 .release = ecryptfs_release,
356 .fsync = ecryptfs_fsync,
357 .fasync = ecryptfs_fasync,
358 .splice_read = generic_file_splice_read,
359 .llseek = default_llseek,
360};
361
362const struct file_operations ecryptfs_main_fops = {
363 .llseek = generic_file_llseek,
364 .read = do_sync_read,
365 .aio_read = ecryptfs_read_update_atime,
366 .write = do_sync_write,
367 .aio_write = generic_file_aio_write,
368 .readdir = ecryptfs_readdir,
369 .unlocked_ioctl = ecryptfs_unlocked_ioctl,
370#ifdef CONFIG_COMPAT
371 .compat_ioctl = ecryptfs_compat_ioctl,
372#endif
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 .splice_read = generic_file_splice_read,
380};
381