file.c 10.2 KB
Newer Older
1 2 3 4 5
/**
 * eCryptfs: Linux filesystem encryption layer
 *
 * Copyright (C) 1997-2004 Erez Zadok
 * Copyright (C) 2001-2004 Stony Brook University
6
 * Copyright (C) 2004-2007 International Business Machines Corp.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
 *   		Michael C. Thompson <mcthomps@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <linux/file.h>
#include <linux/poll.h>
28
#include <linux/slab.h>
29 30 31 32
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/security.h>
#include <linux/compat.h>
33
#include <linux/fs_stack.h>
34
#include <linux/aio.h>
35 36 37 38 39 40 41 42 43 44 45 46 47
#include "ecryptfs_kernel.h"

/**
 * ecryptfs_read_update_atime
 *
 * generic_file_read updates the atime of upper layer inode.  But, it
 * doesn't give us a chance to update the atime of the lower layer
 * inode.  This function is a wrapper to generic_file_read.  It
 * updates the atime of the lower level inode if generic_file_read
 * returns without any errors. This is to be used only for file reads.
 * The function to be used for directory reads is ecryptfs_read.
 */
static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
A
Al Viro 已提交
48
				struct iov_iter *to)
49
{
50
	ssize_t rc;
51
	struct path *path;
52 53
	struct file *file = iocb->ki_filp;

A
Al Viro 已提交
54
	rc = generic_file_read_iter(iocb, to);
55 56 57 58 59 60 61
	/*
	 * Even though this is a async interface, we need to wait
	 * for IO to finish to update atime
	 */
	if (-EIOCBQUEUED == rc)
		rc = wait_on_sync_kiocb(iocb);
	if (rc >= 0) {
62 63
		path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
		touch_atime(path);
64 65 66 67 68
	}
	return rc;
}

struct ecryptfs_getdents_callback {
69
	struct dir_context ctx;
A
Al Viro 已提交
70
	struct dir_context *caller;
71
	struct super_block *sb;
72 73 74 75
	int filldir_called;
	int entries_written;
};

76
/* Inspired by generic filldir in fs/readdir.c */
77
static int
78 79
ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
		 int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
80 81
{
	struct ecryptfs_getdents_callback *buf =
82
		container_of(ctx, struct ecryptfs_getdents_callback, ctx);
83
	size_t name_size;
84
	char *name;
85 86 87
	int rc;

	buf->filldir_called++;
88
	rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
89
						  buf->sb, lower_name,
90 91 92 93 94
						  lower_namelen);
	if (rc) {
		printk(KERN_ERR "%s: Error attempting to decode and decrypt "
		       "filename [%s]; rc = [%d]\n", __func__, lower_name,
		       rc);
95 96
		goto out;
	}
A
Al Viro 已提交
97 98
	buf->caller->pos = buf->ctx.pos;
	rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
99
	kfree(name);
A
Al Viro 已提交
100
	if (!rc)
101 102 103 104 105 106 107
		buf->entries_written++;
out:
	return rc;
}

/**
 * ecryptfs_readdir
108
 * @file: The eCryptfs directory file
A
Al Viro 已提交
109
 * @ctx: The actor to feed the entries to
110
 */
A
Al Viro 已提交
111
static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
112 113 114
{
	int rc;
	struct file *lower_file;
115
	struct inode *inode = file_inode(file);
A
Al Viro 已提交
116 117 118
	struct ecryptfs_getdents_callback buf = {
		.ctx.actor = ecryptfs_filldir,
		.caller = ctx,
119
		.sb = inode->i_sb,
A
Al Viro 已提交
120
	};
121
	lower_file = ecryptfs_file_to_lower(file);
A
Al Viro 已提交
122
	lower_file->f_pos = ctx->pos;
123
	rc = iterate_dir(lower_file, &buf.ctx);
A
Al Viro 已提交
124
	ctx->pos = buf.ctx.pos;
125 126 127 128
	if (rc < 0)
		goto out;
	if (buf.filldir_called && !buf.entries_written)
		goto out;
129
	if (rc >= 0)
130
		fsstack_copy_attr_atime(inode,
A
Al Viro 已提交
131
					file_inode(lower_file));
132
out:
133 134 135 136 137
	return rc;
}

struct kmem_cache *ecryptfs_file_info_cache;

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static int read_or_initialize_metadata(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
	struct ecryptfs_crypt_stat *crypt_stat;
	int rc;

	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
	mount_crypt_stat = &ecryptfs_superblock_to_private(
						inode->i_sb)->mount_crypt_stat;
	mutex_lock(&crypt_stat->cs_mutex);

	if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
	    crypt_stat->flags & ECRYPTFS_KEY_VALID) {
		rc = 0;
		goto out;
	}

	rc = ecryptfs_read_metadata(dentry);
	if (!rc)
		goto out;

	if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
		crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
				       | ECRYPTFS_ENCRYPTED);
		rc = 0;
		goto out;
	}

	if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
	    !i_size_read(ecryptfs_inode_to_lower(inode))) {
		rc = ecryptfs_initialize_file(dentry, inode);
		if (!rc)
			goto out;
	}

	rc = -EIO;
out:
	mutex_unlock(&crypt_stat->cs_mutex);
	return rc;
}

180 181 182 183 184 185 186 187 188 189 190 191 192
/**
 * ecryptfs_open
 * @inode: inode speciying file to open
 * @file: Structure to return filled in
 *
 * Opens the file specified by inode.
 *
 * Returns zero on success; non-zero otherwise
 */
static int ecryptfs_open(struct inode *inode, struct file *file)
{
	int rc = 0;
	struct ecryptfs_crypt_stat *crypt_stat = NULL;
193
	struct dentry *ecryptfs_dentry = file->f_path.dentry;
194 195 196 197 198
	/* Private value of ecryptfs_dentry allocated in
	 * ecryptfs_lookup() */
	struct ecryptfs_file_info *file_info;

	/* Released in ecryptfs_release or end of function if failure */
199
	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
200 201 202 203 204 205 206 207 208
	ecryptfs_set_file_private(file, file_info);
	if (!file_info) {
		ecryptfs_printk(KERN_ERR,
				"Error attempting to allocate memory\n");
		rc = -ENOMEM;
		goto out;
	}
	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
	mutex_lock(&crypt_stat->cs_mutex);
209
	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
210 211
		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
		/* Policy code enabled in future release */
212 213
		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
				      | ECRYPTFS_ENCRYPTED);
214 215
	}
	mutex_unlock(&crypt_stat->cs_mutex);
216
	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
217 218
	if (rc) {
		printk(KERN_ERR "%s: Error attempting to initialize "
219
			"the lower file for the dentry with name "
D
David Howells 已提交
220 221
			"[%pd]; rc = [%d]\n", __func__,
			ecryptfs_dentry, rc);
222
		goto out_free;
223
	}
224 225
	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
226
		rc = -EPERM;
227
		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
228
		       "file must hence be opened RO\n", __func__);
229
		goto out_put;
230
	}
231 232
	ecryptfs_set_file_lower(
		file, ecryptfs_inode_to_private(inode)->lower_file);
233
	if (d_is_dir(ecryptfs_dentry)) {
234
		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
235
		mutex_lock(&crypt_stat->cs_mutex);
236
		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
237
		mutex_unlock(&crypt_stat->cs_mutex);
238 239 240
		rc = 0;
		goto out;
	}
241 242 243
	rc = read_or_initialize_metadata(ecryptfs_dentry);
	if (rc)
		goto out_put;
244 245 246
	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
			(unsigned long long)i_size_read(inode));
247
	goto out;
248 249
out_put:
	ecryptfs_put_lower_file(inode);
250
out_free:
251 252 253 254 255 256 257 258
	kmem_cache_free(ecryptfs_file_info_cache,
			ecryptfs_file_to_private(file));
out:
	return rc;
}

static int ecryptfs_flush(struct file *file, fl_owner_t td)
{
259 260
	struct file *lower_file = ecryptfs_file_to_lower(file);

A
Al Viro 已提交
261
	if (lower_file->f_op->flush) {
262 263 264 265 266
		filemap_write_and_wait(file->f_mapping);
		return lower_file->f_op->flush(lower_file, td);
	}

	return 0;
267 268 269 270
}

static int ecryptfs_release(struct inode *inode, struct file *file)
{
271
	ecryptfs_put_lower_file(inode);
272 273 274
	kmem_cache_free(ecryptfs_file_info_cache,
			ecryptfs_file_to_private(file));
	return 0;
275 276 277
}

static int
278
ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
279
{
280 281 282 283 284 285
	int rc;

	rc = filemap_write_and_wait(file->f_mapping);
	if (rc)
		return rc;

286
	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
287 288 289 290 291 292 293 294
}

static int ecryptfs_fasync(int fd, struct file *file, int flag)
{
	int rc = 0;
	struct file *lower_file = NULL;

	lower_file = ecryptfs_file_to_lower(file);
A
Al Viro 已提交
295
	if (lower_file->f_op->fasync)
296 297 298 299
		rc = lower_file->f_op->fasync(fd, lower_file, flag);
	return rc;
}

300 301 302
static long
ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
303
	struct file *lower_file = ecryptfs_file_to_lower(file);
304 305
	long rc = -ENOTTY;

306 307 308 309 310 311 312 313 314
	if (!lower_file->f_op->unlocked_ioctl)
		return rc;

	switch (cmd) {
	case FITRIM:
	case FS_IOC_GETFLAGS:
	case FS_IOC_SETFLAGS:
	case FS_IOC_GETVERSION:
	case FS_IOC_SETVERSION:
315
		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
316 317 318 319 320 321
		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));

		return rc;
	default:
		return rc;
	}
322 323 324 325 326 327
}

#ifdef CONFIG_COMPAT
static long
ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
328
	struct file *lower_file = ecryptfs_file_to_lower(file);
329 330
	long rc = -ENOIOCTLCMD;

331 332 333 334 335 336 337 338 339
	if (!lower_file->f_op->compat_ioctl)
		return rc;

	switch (cmd) {
	case FITRIM:
	case FS_IOC32_GETFLAGS:
	case FS_IOC32_SETFLAGS:
	case FS_IOC32_GETVERSION:
	case FS_IOC32_SETVERSION:
340
		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
341 342 343 344 345 346
		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));

		return rc;
	default:
		return rc;
	}
347 348
}
#endif
349 350

const struct file_operations ecryptfs_dir_fops = {
A
Al Viro 已提交
351
	.iterate = ecryptfs_readdir,
352
	.read = generic_read_dir,
353 354 355 356
	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = ecryptfs_compat_ioctl,
#endif
357 358 359 360 361
	.open = ecryptfs_open,
	.flush = ecryptfs_flush,
	.release = ecryptfs_release,
	.fsync = ecryptfs_fsync,
	.fasync = ecryptfs_fasync,
362
	.splice_read = generic_file_splice_read,
363
	.llseek = default_llseek,
364 365 366
};

const struct file_operations ecryptfs_main_fops = {
367
	.llseek = generic_file_llseek,
A
Al Viro 已提交
368 369
	.read = new_sync_read,
	.read_iter = ecryptfs_read_update_atime,
370 371
	.write = new_sync_write,
	.write_iter = generic_file_write_iter,
A
Al Viro 已提交
372
	.iterate = ecryptfs_readdir,
373 374 375 376
	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = ecryptfs_compat_ioctl,
#endif
377
	.mmap = generic_file_mmap,
378 379 380 381 382
	.open = ecryptfs_open,
	.flush = ecryptfs_flush,
	.release = ecryptfs_release,
	.fsync = ecryptfs_fsync,
	.fasync = ecryptfs_fasync,
383
	.splice_read = generic_file_splice_read,
384
};