11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2*1c6675caSLee Jones /* 3237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 4237fead6SMichael Halcrow * 5237fead6SMichael Halcrow * Copyright (C) 1997-2003 Erez Zadok 6237fead6SMichael Halcrow * Copyright (C) 2001-2003 Stony Brook University 7237fead6SMichael Halcrow * Copyright (C) 2004-2006 International Business Machines Corp. 8237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 9237fead6SMichael Halcrow * Michael C. Thompson <mcthomps@us.ibm.com> 10237fead6SMichael Halcrow */ 11237fead6SMichael Halcrow 12237fead6SMichael Halcrow #include <linux/fs.h> 13237fead6SMichael Halcrow #include <linux/mount.h> 14237fead6SMichael Halcrow #include <linux/key.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 16237fead6SMichael Halcrow #include <linux/seq_file.h> 174981e081SMichael Halcrow #include <linux/file.h> 184a26620dSTyler Hicks #include <linux/statfs.h> 194a26620dSTyler Hicks #include <linux/magic.h> 20237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 21237fead6SMichael Halcrow 22237fead6SMichael Halcrow struct kmem_cache *ecryptfs_inode_info_cache; 23237fead6SMichael Halcrow 24237fead6SMichael Halcrow /** 25237fead6SMichael Halcrow * ecryptfs_alloc_inode - allocate an ecryptfs inode 26237fead6SMichael Halcrow * @sb: Pointer to the ecryptfs super block 27237fead6SMichael Halcrow * 28237fead6SMichael Halcrow * Called to bring an inode into existence. 29237fead6SMichael Halcrow * 30237fead6SMichael Halcrow * Only handle allocation, setting up structures should be done in 31237fead6SMichael Halcrow * ecryptfs_read_inode. This is because the kernel, between now and 32237fead6SMichael Halcrow * then, will 0 out the private data pointer. 33237fead6SMichael Halcrow * 34237fead6SMichael Halcrow * Returns a pointer to a newly allocated inode, NULL otherwise 35237fead6SMichael Halcrow */ 36237fead6SMichael Halcrow static struct inode *ecryptfs_alloc_inode(struct super_block *sb) 37237fead6SMichael Halcrow { 38035241d3SMichael Halcrow struct ecryptfs_inode_info *inode_info; 39237fead6SMichael Halcrow struct inode *inode = NULL; 40237fead6SMichael Halcrow 41035241d3SMichael Halcrow inode_info = kmem_cache_alloc(ecryptfs_inode_info_cache, GFP_KERNEL); 42035241d3SMichael Halcrow if (unlikely(!inode_info)) 43237fead6SMichael Halcrow goto out; 44e81f3340SHerbert Xu if (ecryptfs_init_crypt_stat(&inode_info->crypt_stat)) { 45e81f3340SHerbert Xu kmem_cache_free(ecryptfs_inode_info_cache, inode_info); 46e81f3340SHerbert Xu goto out; 47e81f3340SHerbert Xu } 48332ab16fSTyler Hicks mutex_init(&inode_info->lower_file_mutex); 49332ab16fSTyler Hicks atomic_set(&inode_info->lower_file_count, 0); 50035241d3SMichael Halcrow inode_info->lower_file = NULL; 51035241d3SMichael Halcrow inode = &inode_info->vfs_inode; 52237fead6SMichael Halcrow out: 53237fead6SMichael Halcrow return inode; 54237fead6SMichael Halcrow } 55237fead6SMichael Halcrow 56586a94fdSAl Viro static void ecryptfs_free_inode(struct inode *inode) 57fa0d7e3dSNick Piggin { 58fa0d7e3dSNick Piggin struct ecryptfs_inode_info *inode_info; 59fa0d7e3dSNick Piggin inode_info = ecryptfs_inode_to_private(inode); 60fa0d7e3dSNick Piggin 61fa0d7e3dSNick Piggin kmem_cache_free(ecryptfs_inode_info_cache, inode_info); 62fa0d7e3dSNick Piggin } 63fa0d7e3dSNick Piggin 64237fead6SMichael Halcrow /** 65237fead6SMichael Halcrow * ecryptfs_destroy_inode 66237fead6SMichael Halcrow * @inode: The ecryptfs inode 67237fead6SMichael Halcrow * 684981e081SMichael Halcrow * This is used during the final destruction of the inode. All 694981e081SMichael Halcrow * allocation of memory related to the inode, including allocated 70332ab16fSTyler Hicks * memory in the crypt_stat struct, will be released here. 71237fead6SMichael Halcrow * There should be no chance that this deallocation will be missed. 72237fead6SMichael Halcrow */ 73237fead6SMichael Halcrow static void ecryptfs_destroy_inode(struct inode *inode) 74237fead6SMichael Halcrow { 75237fead6SMichael Halcrow struct ecryptfs_inode_info *inode_info; 76237fead6SMichael Halcrow 77237fead6SMichael Halcrow inode_info = ecryptfs_inode_to_private(inode); 78332ab16fSTyler Hicks BUG_ON(inode_info->lower_file); 79fcd12835SMichael Halcrow ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat); 80237fead6SMichael Halcrow } 81237fead6SMichael Halcrow 82237fead6SMichael Halcrow /** 83237fead6SMichael Halcrow * ecryptfs_statfs 84*1c6675caSLee Jones * @dentry: The ecryptfs dentry 85237fead6SMichael Halcrow * @buf: The struct kstatfs to fill in with stats 86237fead6SMichael Halcrow * 87237fead6SMichael Halcrow * Get the filesystem statistics. Currently, we let this pass right through 88237fead6SMichael Halcrow * to the lower filesystem and take no action ourselves. 89237fead6SMichael Halcrow */ 90237fead6SMichael Halcrow static int ecryptfs_statfs(struct dentry *dentry, struct kstatfs *buf) 91237fead6SMichael Halcrow { 92ebabe9a9SChristoph Hellwig struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 934a26620dSTyler Hicks int rc; 94ebabe9a9SChristoph Hellwig 95ebabe9a9SChristoph Hellwig if (!lower_dentry->d_sb->s_op->statfs) 96ebabe9a9SChristoph Hellwig return -ENOSYS; 974a26620dSTyler Hicks 984a26620dSTyler Hicks rc = lower_dentry->d_sb->s_op->statfs(lower_dentry, buf); 994a26620dSTyler Hicks if (rc) 1004a26620dSTyler Hicks return rc; 1014a26620dSTyler Hicks 1024a26620dSTyler Hicks buf->f_type = ECRYPTFS_SUPER_MAGIC; 1034a26620dSTyler Hicks rc = ecryptfs_set_f_namelen(&buf->f_namelen, buf->f_namelen, 1044a26620dSTyler Hicks &ecryptfs_superblock_to_private(dentry->d_sb)->mount_crypt_stat); 1054a26620dSTyler Hicks 1064a26620dSTyler Hicks return rc; 107237fead6SMichael Halcrow } 108237fead6SMichael Halcrow 109237fead6SMichael Halcrow /** 110b57922d9SAl Viro * ecryptfs_evict_inode 111*1c6675caSLee Jones * @inode: The ecryptfs inode 112237fead6SMichael Halcrow * 113237fead6SMichael Halcrow * Called by iput() when the inode reference count reached zero 114237fead6SMichael Halcrow * and the inode is not hashed anywhere. Used to clear anything 115237fead6SMichael Halcrow * that needs to be, before the inode is completely destroyed and put 116237fead6SMichael Halcrow * on the inode free list. We use this to drop out reference to the 117237fead6SMichael Halcrow * lower inode. 118237fead6SMichael Halcrow */ 119b57922d9SAl Viro static void ecryptfs_evict_inode(struct inode *inode) 120237fead6SMichael Halcrow { 12191b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 122dbd5768fSJan Kara clear_inode(inode); 123237fead6SMichael Halcrow iput(ecryptfs_inode_to_lower(inode)); 124237fead6SMichael Halcrow } 125237fead6SMichael Halcrow 126*1c6675caSLee Jones /* 127237fead6SMichael Halcrow * ecryptfs_show_options 128237fead6SMichael Halcrow * 12999db6e4aSEric Sandeen * Prints the mount options for a given superblock. 13099db6e4aSEric Sandeen * Returns zero; does not fail. 131237fead6SMichael Halcrow */ 13234c80b1dSAl Viro static int ecryptfs_show_options(struct seq_file *m, struct dentry *root) 133237fead6SMichael Halcrow { 13434c80b1dSAl Viro struct super_block *sb = root->d_sb; 13599db6e4aSEric Sandeen struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 13699db6e4aSEric Sandeen &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; 13799db6e4aSEric Sandeen struct ecryptfs_global_auth_tok *walker; 138237fead6SMichael Halcrow 13999db6e4aSEric Sandeen mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 14099db6e4aSEric Sandeen list_for_each_entry(walker, 14199db6e4aSEric Sandeen &mount_crypt_stat->global_auth_tok_list, 14299db6e4aSEric Sandeen mount_crypt_stat_list) { 1433a5203abSTyler Hicks if (walker->flags & ECRYPTFS_AUTH_TOK_FNEK) 1443a5203abSTyler Hicks seq_printf(m, ",ecryptfs_fnek_sig=%s", walker->sig); 1453a5203abSTyler Hicks else 14699db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_sig=%s", walker->sig); 147237fead6SMichael Halcrow } 14899db6e4aSEric Sandeen mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 14999db6e4aSEric Sandeen 15099db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_cipher=%s", 15199db6e4aSEric Sandeen mount_crypt_stat->global_default_cipher_name); 15299db6e4aSEric Sandeen 15399db6e4aSEric Sandeen if (mount_crypt_stat->global_default_cipher_key_size) 15499db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_key_bytes=%zd", 15599db6e4aSEric Sandeen mount_crypt_stat->global_default_cipher_key_size); 15699db6e4aSEric Sandeen if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) 15799db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_passthrough"); 15899db6e4aSEric Sandeen if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) 15999db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_xattr_metadata"); 16099db6e4aSEric Sandeen if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) 16199db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_encrypted_view"); 162e77cc8d2STyler Hicks if (mount_crypt_stat->flags & ECRYPTFS_UNLINK_SIGS) 163e77cc8d2STyler Hicks seq_printf(m, ",ecryptfs_unlink_sigs"); 1648747f954STyler Hicks if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) 1658747f954STyler Hicks seq_printf(m, ",ecryptfs_mount_auth_tok_only"); 16699db6e4aSEric Sandeen 16799db6e4aSEric Sandeen return 0; 168237fead6SMichael Halcrow } 169237fead6SMichael Halcrow 170ee9b6d61SJosef 'Jeff' Sipek const struct super_operations ecryptfs_sops = { 171237fead6SMichael Halcrow .alloc_inode = ecryptfs_alloc_inode, 172237fead6SMichael Halcrow .destroy_inode = ecryptfs_destroy_inode, 173586a94fdSAl Viro .free_inode = ecryptfs_free_inode, 174237fead6SMichael Halcrow .statfs = ecryptfs_statfs, 175237fead6SMichael Halcrow .remount_fs = NULL, 176b57922d9SAl Viro .evict_inode = ecryptfs_evict_inode, 177237fead6SMichael Halcrow .show_options = ecryptfs_show_options 178237fead6SMichael Halcrow }; 179