Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * eCryptfs: Linux filesystem encryption layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 1997-2004 Erez Zadok
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2001-2004 Stony Brook University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2004-2007 International Business Machines Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *   		Michael C. Thompson <mcthomps@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/fs_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "ecryptfs_kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * ecryptfs_read_update_atime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * generic_file_read updates the atime of upper layer inode.  But, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * doesn't give us a chance to update the atime of the lower layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * inode.  This function is a wrapper to generic_file_read.  It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * updates the atime of the lower level inode if generic_file_read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * returns without any errors. This is to be used only for file reads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * The function to be used for directory reads is ecryptfs_read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 				struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	rc = generic_file_read_iter(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (rc >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		touch_atime(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct ecryptfs_getdents_callback {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct dir_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct dir_context *caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int filldir_called;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int entries_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) /* Inspired by generic filldir in fs/readdir.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		 int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct ecryptfs_getdents_callback *buf =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		container_of(ctx, struct ecryptfs_getdents_callback, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	size_t name_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	buf->filldir_called++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 						  buf->sb, lower_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 						  lower_namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		if (rc != -EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			ecryptfs_printk(KERN_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 					"%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 					__func__, lower_name, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		/* Mask -EINVAL errors as these are most likely due a plaintext
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		 * filename present in the lower filesystem despite filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		 * encryption being enabled. One unavoidable example would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		 * the "lost+found" dentry in the root directory of an Ext4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		 * filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	buf->caller->pos = buf->ctx.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		buf->entries_written++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * ecryptfs_readdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @file: The eCryptfs directory file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @ctx: The actor to feed the entries to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct file *lower_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct ecryptfs_getdents_callback buf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		.ctx.actor = ecryptfs_filldir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		.caller = ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		.sb = inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	lower_file = ecryptfs_file_to_lower(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	rc = iterate_dir(lower_file, &buf.ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	ctx->pos = buf.ctx.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (buf.filldir_called && !buf.entries_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		fsstack_copy_attr_atime(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					file_inode(lower_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct kmem_cache *ecryptfs_file_info_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int read_or_initialize_metadata(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct ecryptfs_crypt_stat *crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	mount_crypt_stat = &ecryptfs_superblock_to_private(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 						inode->i_sb)->mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	mutex_lock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	    crypt_stat->flags & ECRYPTFS_KEY_VALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	rc = ecryptfs_read_metadata(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				       | ECRYPTFS_ENCRYPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	    !i_size_read(ecryptfs_inode_to_lower(inode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		rc = ecryptfs_initialize_file(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	mutex_unlock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct file *lower_file = ecryptfs_file_to_lower(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 * Don't allow mmap on top of file systems that don't support it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 * allows recursive mounting, this will need to be extended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!lower_file->f_op->mmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return generic_file_mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * ecryptfs_open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * @inode: inode specifying file to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * @file: Structure to return filled in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * Opens the file specified by inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int ecryptfs_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	/* Private value of ecryptfs_dentry allocated in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	 * ecryptfs_lookup() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct ecryptfs_file_info *file_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	/* Released in ecryptfs_release or end of function if failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	ecryptfs_set_file_private(file, file_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (!file_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		ecryptfs_printk(KERN_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				"Error attempting to allocate memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	mutex_lock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		/* Policy code enabled in future release */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				      | ECRYPTFS_ENCRYPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	mutex_unlock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		printk(KERN_ERR "%s: Error attempting to initialize "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			"the lower file for the dentry with name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			"[%pd]; rc = [%d]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			ecryptfs_dentry, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		rc = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		       "file must hence be opened RO\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	ecryptfs_set_file_lower(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		file, ecryptfs_inode_to_private(inode)->lower_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	rc = read_or_initialize_metadata(ecryptfs_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			(unsigned long long)i_size_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	ecryptfs_put_lower_file(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	kmem_cache_free(ecryptfs_file_info_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			ecryptfs_file_to_private(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * ecryptfs_dir_open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * @inode: inode specifying file to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * @file: Structure to return filled in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * Opens the file specified by inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int ecryptfs_dir_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	/* Private value of ecryptfs_dentry allocated in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * ecryptfs_lookup() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct ecryptfs_file_info *file_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct file *lower_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	/* Released in ecryptfs_release or end of function if failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	ecryptfs_set_file_private(file, file_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (unlikely(!file_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		ecryptfs_printk(KERN_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 				"Error attempting to allocate memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				 file->f_flags, current_cred());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (IS_ERR(lower_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		printk(KERN_ERR "%s: Error attempting to initialize "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			"the lower file for the dentry with name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			"[%pd]; rc = [%ld]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			ecryptfs_dentry, PTR_ERR(lower_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		kmem_cache_free(ecryptfs_file_info_cache, file_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return PTR_ERR(lower_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	ecryptfs_set_file_lower(file, lower_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int ecryptfs_flush(struct file *file, fl_owner_t td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct file *lower_file = ecryptfs_file_to_lower(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (lower_file->f_op->flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		filemap_write_and_wait(file->f_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return lower_file->f_op->flush(lower_file, td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int ecryptfs_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	ecryptfs_put_lower_file(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	kmem_cache_free(ecryptfs_file_info_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			ecryptfs_file_to_private(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int ecryptfs_dir_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	fput(ecryptfs_file_to_lower(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	kmem_cache_free(ecryptfs_file_info_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			ecryptfs_file_to_private(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	rc = file_write_and_wait(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static int ecryptfs_fasync(int fd, struct file *file, int flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct file *lower_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	lower_file = ecryptfs_file_to_lower(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (lower_file->f_op->fasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	struct file *lower_file = ecryptfs_file_to_lower(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	long rc = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (!lower_file->f_op->unlocked_ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	case FITRIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	case FS_IOC_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	case FS_IOC_SETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	case FS_IOC_GETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	case FS_IOC_SETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	struct file *lower_file = ecryptfs_file_to_lower(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	long rc = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (!lower_file->f_op->compat_ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	case FITRIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	case FS_IOC32_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	case FS_IOC32_SETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	case FS_IOC32_GETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	case FS_IOC32_SETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) const struct file_operations ecryptfs_dir_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	.iterate_shared = ecryptfs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	.read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	.compat_ioctl = ecryptfs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	.open = ecryptfs_dir_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	.release = ecryptfs_dir_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	.fsync = ecryptfs_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	.llseek = ecryptfs_dir_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) const struct file_operations ecryptfs_main_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	.llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	.read_iter = ecryptfs_read_update_atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	.write_iter = generic_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	.compat_ioctl = ecryptfs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.mmap = ecryptfs_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	.open = ecryptfs_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	.flush = ecryptfs_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	.release = ecryptfs_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	.fsync = ecryptfs_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	.fasync = ecryptfs_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	.splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) };