1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun * eCryptfs: Linux filesystem encryption layer
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1997-2003 Erez Zadok
6*4882a593Smuzhiyun * Copyright (C) 2001-2003 Stony Brook University
7*4882a593Smuzhiyun * Copyright (C) 2004-2006 International Business Machines Corp.
8*4882a593Smuzhiyun * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9*4882a593Smuzhiyun * Michael C. Thompson <mcthomps@us.ibm.com>
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/mount.h>
14*4882a593Smuzhiyun #include <linux/key.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/seq_file.h>
17*4882a593Smuzhiyun #include <linux/file.h>
18*4882a593Smuzhiyun #include <linux/statfs.h>
19*4882a593Smuzhiyun #include <linux/magic.h>
20*4882a593Smuzhiyun #include "ecryptfs_kernel.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun struct kmem_cache *ecryptfs_inode_info_cache;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun * ecryptfs_alloc_inode - allocate an ecryptfs inode
26*4882a593Smuzhiyun * @sb: Pointer to the ecryptfs super block
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * Called to bring an inode into existence.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Only handle allocation, setting up structures should be done in
31*4882a593Smuzhiyun * ecryptfs_read_inode. This is because the kernel, between now and
32*4882a593Smuzhiyun * then, will 0 out the private data pointer.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * Returns a pointer to a newly allocated inode, NULL otherwise
35*4882a593Smuzhiyun */
ecryptfs_alloc_inode(struct super_block * sb)36*4882a593Smuzhiyun static struct inode *ecryptfs_alloc_inode(struct super_block *sb)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct ecryptfs_inode_info *inode_info;
39*4882a593Smuzhiyun struct inode *inode = NULL;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun inode_info = kmem_cache_alloc(ecryptfs_inode_info_cache, GFP_KERNEL);
42*4882a593Smuzhiyun if (unlikely(!inode_info))
43*4882a593Smuzhiyun goto out;
44*4882a593Smuzhiyun if (ecryptfs_init_crypt_stat(&inode_info->crypt_stat)) {
45*4882a593Smuzhiyun kmem_cache_free(ecryptfs_inode_info_cache, inode_info);
46*4882a593Smuzhiyun goto out;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun mutex_init(&inode_info->lower_file_mutex);
49*4882a593Smuzhiyun atomic_set(&inode_info->lower_file_count, 0);
50*4882a593Smuzhiyun inode_info->lower_file = NULL;
51*4882a593Smuzhiyun inode = &inode_info->vfs_inode;
52*4882a593Smuzhiyun out:
53*4882a593Smuzhiyun return inode;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
ecryptfs_free_inode(struct inode * inode)56*4882a593Smuzhiyun static void ecryptfs_free_inode(struct inode *inode)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun struct ecryptfs_inode_info *inode_info;
59*4882a593Smuzhiyun inode_info = ecryptfs_inode_to_private(inode);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun kmem_cache_free(ecryptfs_inode_info_cache, inode_info);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /**
65*4882a593Smuzhiyun * ecryptfs_destroy_inode
66*4882a593Smuzhiyun * @inode: The ecryptfs inode
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * This is used during the final destruction of the inode. All
69*4882a593Smuzhiyun * allocation of memory related to the inode, including allocated
70*4882a593Smuzhiyun * memory in the crypt_stat struct, will be released here.
71*4882a593Smuzhiyun * There should be no chance that this deallocation will be missed.
72*4882a593Smuzhiyun */
ecryptfs_destroy_inode(struct inode * inode)73*4882a593Smuzhiyun static void ecryptfs_destroy_inode(struct inode *inode)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun struct ecryptfs_inode_info *inode_info;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun inode_info = ecryptfs_inode_to_private(inode);
78*4882a593Smuzhiyun BUG_ON(inode_info->lower_file);
79*4882a593Smuzhiyun ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /**
83*4882a593Smuzhiyun * ecryptfs_statfs
84*4882a593Smuzhiyun * @sb: The ecryptfs super block
85*4882a593Smuzhiyun * @buf: The struct kstatfs to fill in with stats
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * Get the filesystem statistics. Currently, we let this pass right through
88*4882a593Smuzhiyun * to the lower filesystem and take no action ourselves.
89*4882a593Smuzhiyun */
ecryptfs_statfs(struct dentry * dentry,struct kstatfs * buf)90*4882a593Smuzhiyun static int ecryptfs_statfs(struct dentry *dentry, struct kstatfs *buf)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
93*4882a593Smuzhiyun int rc;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun if (!lower_dentry->d_sb->s_op->statfs)
96*4882a593Smuzhiyun return -ENOSYS;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun rc = lower_dentry->d_sb->s_op->statfs(lower_dentry, buf);
99*4882a593Smuzhiyun if (rc)
100*4882a593Smuzhiyun return rc;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun buf->f_type = ECRYPTFS_SUPER_MAGIC;
103*4882a593Smuzhiyun rc = ecryptfs_set_f_namelen(&buf->f_namelen, buf->f_namelen,
104*4882a593Smuzhiyun &ecryptfs_superblock_to_private(dentry->d_sb)->mount_crypt_stat);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun return rc;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /**
110*4882a593Smuzhiyun * ecryptfs_evict_inode
111*4882a593Smuzhiyun * @inode - The ecryptfs inode
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * Called by iput() when the inode reference count reached zero
114*4882a593Smuzhiyun * and the inode is not hashed anywhere. Used to clear anything
115*4882a593Smuzhiyun * that needs to be, before the inode is completely destroyed and put
116*4882a593Smuzhiyun * on the inode free list. We use this to drop out reference to the
117*4882a593Smuzhiyun * lower inode.
118*4882a593Smuzhiyun */
ecryptfs_evict_inode(struct inode * inode)119*4882a593Smuzhiyun static void ecryptfs_evict_inode(struct inode *inode)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun truncate_inode_pages_final(&inode->i_data);
122*4882a593Smuzhiyun clear_inode(inode);
123*4882a593Smuzhiyun iput(ecryptfs_inode_to_lower(inode));
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun * ecryptfs_show_options
128*4882a593Smuzhiyun *
129*4882a593Smuzhiyun * Prints the mount options for a given superblock.
130*4882a593Smuzhiyun * Returns zero; does not fail.
131*4882a593Smuzhiyun */
ecryptfs_show_options(struct seq_file * m,struct dentry * root)132*4882a593Smuzhiyun static int ecryptfs_show_options(struct seq_file *m, struct dentry *root)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun struct super_block *sb = root->d_sb;
135*4882a593Smuzhiyun struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
136*4882a593Smuzhiyun &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
137*4882a593Smuzhiyun struct ecryptfs_global_auth_tok *walker;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
140*4882a593Smuzhiyun list_for_each_entry(walker,
141*4882a593Smuzhiyun &mount_crypt_stat->global_auth_tok_list,
142*4882a593Smuzhiyun mount_crypt_stat_list) {
143*4882a593Smuzhiyun if (walker->flags & ECRYPTFS_AUTH_TOK_FNEK)
144*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_fnek_sig=%s", walker->sig);
145*4882a593Smuzhiyun else
146*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_sig=%s", walker->sig);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_cipher=%s",
151*4882a593Smuzhiyun mount_crypt_stat->global_default_cipher_name);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (mount_crypt_stat->global_default_cipher_key_size)
154*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_key_bytes=%zd",
155*4882a593Smuzhiyun mount_crypt_stat->global_default_cipher_key_size);
156*4882a593Smuzhiyun if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)
157*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_passthrough");
158*4882a593Smuzhiyun if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
159*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_xattr_metadata");
160*4882a593Smuzhiyun if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
161*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_encrypted_view");
162*4882a593Smuzhiyun if (mount_crypt_stat->flags & ECRYPTFS_UNLINK_SIGS)
163*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_unlink_sigs");
164*4882a593Smuzhiyun if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY)
165*4882a593Smuzhiyun seq_printf(m, ",ecryptfs_mount_auth_tok_only");
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun const struct super_operations ecryptfs_sops = {
171*4882a593Smuzhiyun .alloc_inode = ecryptfs_alloc_inode,
172*4882a593Smuzhiyun .destroy_inode = ecryptfs_destroy_inode,
173*4882a593Smuzhiyun .free_inode = ecryptfs_free_inode,
174*4882a593Smuzhiyun .statfs = ecryptfs_statfs,
175*4882a593Smuzhiyun .remount_fs = NULL,
176*4882a593Smuzhiyun .evict_inode = ecryptfs_evict_inode,
177*4882a593Smuzhiyun .show_options = ecryptfs_show_options
178*4882a593Smuzhiyun };
179