18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/** 38c2ecf20Sopenharmony_ci * eCryptfs: Linux filesystem encryption layer 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1997-2003 Erez Zadok 68c2ecf20Sopenharmony_ci * Copyright (C) 2001-2003 Stony Brook University 78c2ecf20Sopenharmony_ci * Copyright (C) 2004-2006 International Business Machines Corp. 88c2ecf20Sopenharmony_ci * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 98c2ecf20Sopenharmony_ci * Michael C. Thompson <mcthomps@us.ibm.com> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/fs.h> 138c2ecf20Sopenharmony_ci#include <linux/mount.h> 148c2ecf20Sopenharmony_ci#include <linux/key.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 178c2ecf20Sopenharmony_ci#include <linux/file.h> 188c2ecf20Sopenharmony_ci#include <linux/statfs.h> 198c2ecf20Sopenharmony_ci#include <linux/magic.h> 208c2ecf20Sopenharmony_ci#include "ecryptfs_kernel.h" 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistruct kmem_cache *ecryptfs_inode_info_cache; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/** 258c2ecf20Sopenharmony_ci * ecryptfs_alloc_inode - allocate an ecryptfs inode 268c2ecf20Sopenharmony_ci * @sb: Pointer to the ecryptfs super block 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * Called to bring an inode into existence. 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci * Only handle allocation, setting up structures should be done in 318c2ecf20Sopenharmony_ci * ecryptfs_read_inode. This is because the kernel, between now and 328c2ecf20Sopenharmony_ci * then, will 0 out the private data pointer. 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * Returns a pointer to a newly allocated inode, NULL otherwise 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_cistatic struct inode *ecryptfs_alloc_inode(struct super_block *sb) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci struct ecryptfs_inode_info *inode_info; 398c2ecf20Sopenharmony_ci struct inode *inode = NULL; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci inode_info = kmem_cache_alloc(ecryptfs_inode_info_cache, GFP_KERNEL); 428c2ecf20Sopenharmony_ci if (unlikely(!inode_info)) 438c2ecf20Sopenharmony_ci goto out; 448c2ecf20Sopenharmony_ci if (ecryptfs_init_crypt_stat(&inode_info->crypt_stat)) { 458c2ecf20Sopenharmony_ci kmem_cache_free(ecryptfs_inode_info_cache, inode_info); 468c2ecf20Sopenharmony_ci goto out; 478c2ecf20Sopenharmony_ci } 488c2ecf20Sopenharmony_ci mutex_init(&inode_info->lower_file_mutex); 498c2ecf20Sopenharmony_ci atomic_set(&inode_info->lower_file_count, 0); 508c2ecf20Sopenharmony_ci inode_info->lower_file = NULL; 518c2ecf20Sopenharmony_ci inode = &inode_info->vfs_inode; 528c2ecf20Sopenharmony_ciout: 538c2ecf20Sopenharmony_ci return inode; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic void ecryptfs_free_inode(struct inode *inode) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct ecryptfs_inode_info *inode_info; 598c2ecf20Sopenharmony_ci inode_info = ecryptfs_inode_to_private(inode); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci kmem_cache_free(ecryptfs_inode_info_cache, inode_info); 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/** 658c2ecf20Sopenharmony_ci * ecryptfs_destroy_inode 668c2ecf20Sopenharmony_ci * @inode: The ecryptfs inode 678c2ecf20Sopenharmony_ci * 688c2ecf20Sopenharmony_ci * This is used during the final destruction of the inode. All 698c2ecf20Sopenharmony_ci * allocation of memory related to the inode, including allocated 708c2ecf20Sopenharmony_ci * memory in the crypt_stat struct, will be released here. 718c2ecf20Sopenharmony_ci * There should be no chance that this deallocation will be missed. 728c2ecf20Sopenharmony_ci */ 738c2ecf20Sopenharmony_cistatic void ecryptfs_destroy_inode(struct inode *inode) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci struct ecryptfs_inode_info *inode_info; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci inode_info = ecryptfs_inode_to_private(inode); 788c2ecf20Sopenharmony_ci BUG_ON(inode_info->lower_file); 798c2ecf20Sopenharmony_ci ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci/** 838c2ecf20Sopenharmony_ci * ecryptfs_statfs 848c2ecf20Sopenharmony_ci * @sb: The ecryptfs super block 858c2ecf20Sopenharmony_ci * @buf: The struct kstatfs to fill in with stats 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * Get the filesystem statistics. Currently, we let this pass right through 888c2ecf20Sopenharmony_ci * to the lower filesystem and take no action ourselves. 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_cistatic int ecryptfs_statfs(struct dentry *dentry, struct kstatfs *buf) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 938c2ecf20Sopenharmony_ci int rc; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci if (!lower_dentry->d_sb->s_op->statfs) 968c2ecf20Sopenharmony_ci return -ENOSYS; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci rc = lower_dentry->d_sb->s_op->statfs(lower_dentry, buf); 998c2ecf20Sopenharmony_ci if (rc) 1008c2ecf20Sopenharmony_ci return rc; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci buf->f_type = ECRYPTFS_SUPER_MAGIC; 1038c2ecf20Sopenharmony_ci rc = ecryptfs_set_f_namelen(&buf->f_namelen, buf->f_namelen, 1048c2ecf20Sopenharmony_ci &ecryptfs_superblock_to_private(dentry->d_sb)->mount_crypt_stat); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci return rc; 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/** 1108c2ecf20Sopenharmony_ci * ecryptfs_evict_inode 1118c2ecf20Sopenharmony_ci * @inode - The ecryptfs inode 1128c2ecf20Sopenharmony_ci * 1138c2ecf20Sopenharmony_ci * Called by iput() when the inode reference count reached zero 1148c2ecf20Sopenharmony_ci * and the inode is not hashed anywhere. Used to clear anything 1158c2ecf20Sopenharmony_ci * that needs to be, before the inode is completely destroyed and put 1168c2ecf20Sopenharmony_ci * on the inode free list. We use this to drop out reference to the 1178c2ecf20Sopenharmony_ci * lower inode. 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_cistatic void ecryptfs_evict_inode(struct inode *inode) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci truncate_inode_pages_final(&inode->i_data); 1228c2ecf20Sopenharmony_ci clear_inode(inode); 1238c2ecf20Sopenharmony_ci iput(ecryptfs_inode_to_lower(inode)); 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/** 1278c2ecf20Sopenharmony_ci * ecryptfs_show_options 1288c2ecf20Sopenharmony_ci * 1298c2ecf20Sopenharmony_ci * Prints the mount options for a given superblock. 1308c2ecf20Sopenharmony_ci * Returns zero; does not fail. 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_cistatic int ecryptfs_show_options(struct seq_file *m, struct dentry *root) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci struct super_block *sb = root->d_sb; 1358c2ecf20Sopenharmony_ci struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1368c2ecf20Sopenharmony_ci &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; 1378c2ecf20Sopenharmony_ci struct ecryptfs_global_auth_tok *walker; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 1408c2ecf20Sopenharmony_ci list_for_each_entry(walker, 1418c2ecf20Sopenharmony_ci &mount_crypt_stat->global_auth_tok_list, 1428c2ecf20Sopenharmony_ci mount_crypt_stat_list) { 1438c2ecf20Sopenharmony_ci if (walker->flags & ECRYPTFS_AUTH_TOK_FNEK) 1448c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_fnek_sig=%s", walker->sig); 1458c2ecf20Sopenharmony_ci else 1468c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_sig=%s", walker->sig); 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_cipher=%s", 1518c2ecf20Sopenharmony_ci mount_crypt_stat->global_default_cipher_name); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci if (mount_crypt_stat->global_default_cipher_key_size) 1548c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_key_bytes=%zd", 1558c2ecf20Sopenharmony_ci mount_crypt_stat->global_default_cipher_key_size); 1568c2ecf20Sopenharmony_ci if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) 1578c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_passthrough"); 1588c2ecf20Sopenharmony_ci if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) 1598c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_xattr_metadata"); 1608c2ecf20Sopenharmony_ci if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) 1618c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_encrypted_view"); 1628c2ecf20Sopenharmony_ci if (mount_crypt_stat->flags & ECRYPTFS_UNLINK_SIGS) 1638c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_unlink_sigs"); 1648c2ecf20Sopenharmony_ci if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) 1658c2ecf20Sopenharmony_ci seq_printf(m, ",ecryptfs_mount_auth_tok_only"); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci return 0; 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciconst struct super_operations ecryptfs_sops = { 1718c2ecf20Sopenharmony_ci .alloc_inode = ecryptfs_alloc_inode, 1728c2ecf20Sopenharmony_ci .destroy_inode = ecryptfs_destroy_inode, 1738c2ecf20Sopenharmony_ci .free_inode = ecryptfs_free_inode, 1748c2ecf20Sopenharmony_ci .statfs = ecryptfs_statfs, 1758c2ecf20Sopenharmony_ci .remount_fs = NULL, 1768c2ecf20Sopenharmony_ci .evict_inode = ecryptfs_evict_inode, 1778c2ecf20Sopenharmony_ci .show_options = ecryptfs_show_options 1788c2ecf20Sopenharmony_ci}; 179