linux/fs/ecryptfs/ecryptfs_kernel.h

/* SPDX-License-Identifier: GPL-2.0-or-later */
/**
 * eCryptfs: Linux filesystem encryption layer
 * Kernel declarations.
 *
 * Copyright (C) 1997-2003 Erez Zadok
 * Copyright (C) 2001-2003 Stony Brook University
 * Copyright (C) 2004-2008 International Business Machines Corp.
 *   Author(s): Michael A. Halcrow <[email protected]>
 *              Trevor S. Highland <[email protected]>
 *              Tyler Hicks <[email protected]>
 */

#ifndef ECRYPTFS_KERNEL_H
#define ECRYPTFS_KERNEL_H

#include <crypto/skcipher.h>
#include <keys/user-type.h>
#include <keys/encrypted-type.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/fs_stack.h>
#include <linux/namei.h>
#include <linux/scatterlist.h>
#include <linux/hash.h>
#include <linux/nsproxy.h>
#include <linux/backing-dev.h>
#include <linux/ecryptfs.h>

#define ECRYPTFS_DEFAULT_IV_BYTES
#define ECRYPTFS_DEFAULT_EXTENT_SIZE
#define ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
#define ECRYPTFS_DEFAULT_MSG_CTX_ELEMS
#define ECRYPTFS_DEFAULT_SEND_TIMEOUT
#define ECRYPTFS_MAX_MSG_CTX_TTL
#define ECRYPTFS_DEFAULT_NUM_USERS
#define ECRYPTFS_MAX_NUM_USERS
#define ECRYPTFS_XATTR_NAME

void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok);
static inline void
ecryptfs_to_hex(char *dst, char *src, size_t src_size)
{}

extern void ecryptfs_from_hex(char *dst, char *src, int dst_size);

struct ecryptfs_key_record {};

struct ecryptfs_auth_tok_list {};

struct ecryptfs_crypt_stat;
struct ecryptfs_mount_crypt_stat;

struct ecryptfs_page_crypt_context {};

#if defined(CONFIG_ENCRYPTED_KEYS) || defined(CONFIG_ENCRYPTED_KEYS_MODULE)
static inline struct ecryptfs_auth_tok *
ecryptfs_get_encrypted_key_payload_data(struct key *key)
{}

static inline struct key *ecryptfs_get_encrypted_key(char *sig)
{}

#else
static inline struct ecryptfs_auth_tok *
ecryptfs_get_encrypted_key_payload_data(struct key *key)
{
	return NULL;
}

static inline struct key *ecryptfs_get_encrypted_key(char *sig)
{
	return ERR_PTR(-ENOKEY);
}

#endif /* CONFIG_ENCRYPTED_KEYS */

static inline struct ecryptfs_auth_tok *
ecryptfs_get_key_payload_data(struct key *key)
{}

#define ECRYPTFS_MAX_KEYSET_SIZE
#define ECRYPTFS_MAX_CIPHER_NAME_SIZE
#define ECRYPTFS_MAX_NUM_ENC_KEYS
#define ECRYPTFS_MAX_IV_BYTES
#define ECRYPTFS_SALT_BYTES
#define MAGIC_ECRYPTFS_MARKER
#define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
#define ECRYPTFS_FILE_SIZE_BYTES
#define ECRYPTFS_SIZE_AND_MARKER_BYTES
#define ECRYPTFS_DEFAULT_CIPHER
#define ECRYPTFS_DEFAULT_KEY_BYTES
#define ECRYPTFS_DEFAULT_HASH
#define ECRYPTFS_TAG_70_DIGEST
#define ECRYPTFS_TAG_1_PACKET_TYPE
#define ECRYPTFS_TAG_3_PACKET_TYPE
#define ECRYPTFS_TAG_11_PACKET_TYPE
#define ECRYPTFS_TAG_64_PACKET_TYPE
#define ECRYPTFS_TAG_65_PACKET_TYPE
#define ECRYPTFS_TAG_66_PACKET_TYPE
#define ECRYPTFS_TAG_67_PACKET_TYPE
#define ECRYPTFS_TAG_70_PACKET_TYPE
#define ECRYPTFS_TAG_71_PACKET_TYPE
#define ECRYPTFS_TAG_72_PACKET_TYPE
#define ECRYPTFS_TAG_73_PACKET_TYPE
#define ECRYPTFS_MIN_PKT_LEN_SIZE
#define ECRYPTFS_MAX_PKT_LEN_SIZE
/* Constraint: ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES >=
 * ECRYPTFS_MAX_IV_BYTES */
#define ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
#define ECRYPTFS_NON_NULL
#define MD5_DIGEST_SIZE
#define ECRYPTFS_TAG_70_DIGEST_SIZE
#define ECRYPTFS_TAG_70_MIN_METADATA_SIZE
#define ECRYPTFS_TAG_70_MAX_METADATA_SIZE
#define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX
#define ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
#define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX
#define ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
#define ECRYPTFS_ENCRYPTED_DENTRY_NAME_LEN

#ifdef CONFIG_ECRYPT_FS_MESSAGING
#define ECRYPTFS_VERSIONING_MASK_MESSAGING
#else
#define ECRYPTFS_VERSIONING_MASK_MESSAGING
#endif

#define ECRYPTFS_VERSIONING_MASK
struct ecryptfs_key_sig {};

struct ecryptfs_filename {};

/**
 * This is the primary struct associated with each encrypted file.
 *
 * TODO: cache align/pack?
 */
struct ecryptfs_crypt_stat {};

/* inode private data. */
struct ecryptfs_inode_info {};

/* dentry private data. Each dentry must keep track of a lower
 * vfsmount too. */
struct ecryptfs_dentry_info {};

/**
 * ecryptfs_global_auth_tok - A key used to encrypt all new files under the mountpoint
 * @flags: Status flags
 * @mount_crypt_stat_list: These auth_toks hang off the mount-wide
 *                         cryptographic context. Every time a new
 *                         inode comes into existence, eCryptfs copies
 *                         the auth_toks on that list to the set of
 *                         auth_toks on the inode's crypt_stat
 * @global_auth_tok_key: The key from the user's keyring for the sig
 * @global_auth_tok: The key contents
 * @sig: The key identifier
 *
 * ecryptfs_global_auth_tok structs refer to authentication token keys
 * in the user keyring that apply to newly created files. A list of
 * these objects hangs off of the mount_crypt_stat struct for any
 * given eCryptfs mount. This struct maintains a reference to both the
 * key contents and the key itself so that the key can be put on
 * unmount.
 */
struct ecryptfs_global_auth_tok {};

/**
 * ecryptfs_key_tfm - Persistent key tfm
 * @key_tfm: crypto API handle to the key
 * @key_size: Key size in bytes
 * @key_tfm_mutex: Mutex to ensure only one operation in eCryptfs is
 *                 using the persistent TFM at any point in time
 * @key_tfm_list: Handle to hang this off the module-wide TFM list
 * @cipher_name: String name for the cipher for this TFM
 *
 * Typically, eCryptfs will use the same ciphers repeatedly throughout
 * the course of its operations. In order to avoid unnecessarily
 * destroying and initializing the same cipher repeatedly, eCryptfs
 * keeps a list of crypto API contexts around to use when needed.
 */
struct ecryptfs_key_tfm {};

extern struct mutex key_tfm_list_mutex;

/**
 * This struct is to enable a mount-wide passphrase/salt combo. This
 * is more or less a stopgap to provide similar functionality to other
 * crypto filesystems like EncFS or CFS until full policy support is
 * implemented in eCryptfs.
 */
struct ecryptfs_mount_crypt_stat {};

/* superblock private data. */
struct ecryptfs_sb_info {};

/* file private data. */
struct ecryptfs_file_info {};

/* auth_tok <=> encrypted_session_key mappings */
struct ecryptfs_auth_tok_list_item {};

struct ecryptfs_message {};

struct ecryptfs_msg_ctx {};

struct ecryptfs_daemon {};

#ifdef CONFIG_ECRYPT_FS_MESSAGING
extern struct mutex ecryptfs_daemon_hash_mux;
#endif

static inline size_t
ecryptfs_lower_header_size(struct ecryptfs_crypt_stat *crypt_stat)
{}

static inline struct ecryptfs_file_info *
ecryptfs_file_to_private(struct file *file)
{}

static inline void
ecryptfs_set_file_private(struct file *file,
			  struct ecryptfs_file_info *file_info)
{}

static inline struct file *ecryptfs_file_to_lower(struct file *file)
{}

static inline void
ecryptfs_set_file_lower(struct file *file, struct file *lower_file)
{}

static inline struct ecryptfs_inode_info *
ecryptfs_inode_to_private(struct inode *inode)
{}

static inline struct inode *ecryptfs_inode_to_lower(struct inode *inode)
{}

static inline void
ecryptfs_set_inode_lower(struct inode *inode, struct inode *lower_inode)
{}

static inline struct ecryptfs_sb_info *
ecryptfs_superblock_to_private(struct super_block *sb)
{}

static inline void
ecryptfs_set_superblock_private(struct super_block *sb,
				struct ecryptfs_sb_info *sb_info)
{}

static inline struct super_block *
ecryptfs_superblock_to_lower(struct super_block *sb)
{}

static inline void
ecryptfs_set_superblock_lower(struct super_block *sb,
			      struct super_block *lower_sb)
{}

static inline void
ecryptfs_set_dentry_private(struct dentry *dentry,
			    struct ecryptfs_dentry_info *dentry_info)
{}

static inline struct dentry *
ecryptfs_dentry_to_lower(struct dentry *dentry)
{}

static inline const struct path *
ecryptfs_dentry_to_lower_path(struct dentry *dentry)
{}

#define ecryptfs_printk(type, fmt, arg...)
__printf(1, 2)
void __ecryptfs_printk(const char *fmt, ...);

extern const struct file_operations ecryptfs_main_fops;
extern const struct file_operations ecryptfs_dir_fops;
extern const struct inode_operations ecryptfs_main_iops;
extern const struct inode_operations ecryptfs_dir_iops;
extern const struct inode_operations ecryptfs_symlink_iops;
extern const struct super_operations ecryptfs_sops;
extern const struct dentry_operations ecryptfs_dops;
extern const struct address_space_operations ecryptfs_aops;
extern int ecryptfs_verbosity;
extern unsigned int ecryptfs_message_buf_len;
extern signed long ecryptfs_message_wait_timeout;
extern unsigned int ecryptfs_number_of_users;

extern struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
extern struct kmem_cache *ecryptfs_file_info_cache;
extern struct kmem_cache *ecryptfs_dentry_info_cache;
extern struct kmem_cache *ecryptfs_inode_info_cache;
extern struct kmem_cache *ecryptfs_sb_info_cache;
extern struct kmem_cache *ecryptfs_header_cache;
extern struct kmem_cache *ecryptfs_xattr_cache;
extern struct kmem_cache *ecryptfs_key_record_cache;
extern struct kmem_cache *ecryptfs_key_sig_cache;
extern struct kmem_cache *ecryptfs_global_auth_tok_cache;
extern struct kmem_cache *ecryptfs_key_tfm_cache;

struct inode *ecryptfs_get_inode(struct inode *lower_inode,
				 struct super_block *sb);
void ecryptfs_i_size_init(const char *page_virt, struct inode *inode);
int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
			     struct inode *ecryptfs_inode);
int ecryptfs_decode_and_decrypt_filename(char **decrypted_name,
					 size_t *decrypted_name_size,
					 struct super_block *sb,
					 const char *name, size_t name_size);
int ecryptfs_fill_zeros(struct file *file, loff_t new_length);
int ecryptfs_encrypt_and_encode_filename(
	char **encoded_name,
	size_t *encoded_name_size,
	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
	const char *name, size_t name_size);
struct dentry *ecryptfs_lower_dentry(struct dentry *this_dentry);
void ecryptfs_dump_hex(char *data, int bytes);
int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
			int sg_size);
int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat);
void ecryptfs_rotate_iv(unsigned char *iv);
int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
void ecryptfs_destroy_mount_crypt_stat(
	struct ecryptfs_mount_crypt_stat *mount_crypt_stat);
int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat);
int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode);
int ecryptfs_encrypt_page(struct page *page);
int ecryptfs_decrypt_page(struct page *page);
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
			    struct inode *ecryptfs_inode);
int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry);
int ecryptfs_new_file_context(struct inode *ecryptfs_inode);
void ecryptfs_write_crypt_stat_flags(char *page_virt,
				     struct ecryptfs_crypt_stat *crypt_stat,
				     size_t *written);
int ecryptfs_read_and_validate_header_region(struct inode *inode);
int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
					    struct inode *inode);
u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes);
int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code);
void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat);
int ecryptfs_generate_key_packet_set(char *dest_base,
				     struct ecryptfs_crypt_stat *crypt_stat,
				     struct dentry *ecryptfs_dentry,
				     size_t *len, size_t max);
int
ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
			  unsigned char *src, struct dentry *ecryptfs_dentry);
int ecryptfs_truncate(struct dentry *dentry, loff_t new_length);
ssize_t
ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
			const char *name, void *value, size_t size);
int
ecryptfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
		  const void *value, size_t size, int flags);
int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode);
#ifdef CONFIG_ECRYPT_FS_MESSAGING
int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
			      struct ecryptfs_message *msg, u32 seq);
int ecryptfs_send_message(char *data, int data_len,
			  struct ecryptfs_msg_ctx **msg_ctx);
int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
			       struct ecryptfs_message **emsg);
int ecryptfs_init_messaging(void);
void ecryptfs_release_messaging(void);
#else
static inline int ecryptfs_init_messaging(void)
{
	return 0;
}
static inline void ecryptfs_release_messaging(void)
{ }
static inline int ecryptfs_send_message(char *data, int data_len,
					struct ecryptfs_msg_ctx **msg_ctx)
{
	return -ENOTCONN;
}
static inline int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
					     struct ecryptfs_message **emsg)
{
	return -ENOMSG;
}
#endif

void
ecryptfs_write_header_metadata(char *virt,
			       struct ecryptfs_crypt_stat *crypt_stat,
			       size_t *written);
int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig);
int
ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
			   char *sig, u32 global_auth_tok_flags);
int ecryptfs_get_global_auth_tok_for_sig(
	struct ecryptfs_global_auth_tok **global_auth_tok,
	struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig);
int
ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
			 size_t key_size);
int ecryptfs_init_crypto(void);
int ecryptfs_destroy_crypto(void);
int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm);
int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
					       struct mutex **tfm_mutex,
					       char *cipher_name);
int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
				      struct ecryptfs_auth_tok **auth_tok,
				      char *sig);
int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
			 loff_t offset, size_t size);
int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
				      struct page *page_for_lower,
				      size_t offset_in_page, size_t size);
int ecryptfs_write(struct inode *inode, char *data, loff_t offset, size_t size);
int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
			struct inode *ecryptfs_inode);
int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
				     pgoff_t page_index,
				     size_t offset_in_page, size_t size,
				     struct inode *ecryptfs_inode);
struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index);
int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
				 size_t *length_size);
int ecryptfs_write_packet_length(char *dest, size_t size,
				 size_t *packet_size_length);
#ifdef CONFIG_ECRYPT_FS_MESSAGING
int ecryptfs_init_ecryptfs_miscdev(void);
void ecryptfs_destroy_ecryptfs_miscdev(void);
int ecryptfs_send_miscdev(char *data, size_t data_size,
			  struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
			  u16 msg_flags, struct ecryptfs_daemon *daemon);
void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx);
int
ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file);
int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon);
int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon);
#endif
int ecryptfs_init_kthread(void);
void ecryptfs_destroy_kthread(void);
int ecryptfs_privileged_open(struct file **lower_file,
			     struct dentry *lower_dentry,
			     struct vfsmount *lower_mnt,
			     const struct cred *cred);
int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode);
void ecryptfs_put_lower_file(struct inode *inode);
int
ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
			     size_t *packet_size,
			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
			     char *filename, size_t filename_size);
int
ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
			     size_t *packet_size,
			     struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
			     char *data, size_t max_packet_size);
int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
			   struct ecryptfs_mount_crypt_stat *mount_crypt_stat);
int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
		       loff_t offset);

extern const struct xattr_handler * const ecryptfs_xattr_handlers[];

#endif /* #ifndef ECRYPTFS_KERNEL_H */