2015-04-11 19:48:01 +08:00
|
|
|
/*
|
|
|
|
* linux/fs/ext4/ext4_crypto.h
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015, Google, Inc.
|
|
|
|
*
|
|
|
|
* This contains encryption header content for ext4
|
|
|
|
*
|
|
|
|
* Written by Michael Halcrow, 2015.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _EXT4_CRYPTO_H
|
|
|
|
#define _EXT4_CRYPTO_H
|
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
#define EXT4_KEY_DESCRIPTOR_SIZE 8
|
|
|
|
|
|
|
|
/* Policy provided via an ioctl on the topmost directory */
|
|
|
|
struct ext4_encryption_policy {
|
|
|
|
char version;
|
|
|
|
char contents_encryption_mode;
|
|
|
|
char filenames_encryption_mode;
|
2015-05-02 04:56:50 +08:00
|
|
|
char flags;
|
2015-04-11 19:48:01 +08:00
|
|
|
char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
|
|
|
|
} __attribute__((__packed__));
|
|
|
|
|
|
|
|
#define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
|
|
|
|
#define EXT4_KEY_DERIVATION_NONCE_SIZE 16
|
|
|
|
|
2015-05-02 04:56:50 +08:00
|
|
|
#define EXT4_POLICY_FLAGS_PAD_4 0x00
|
|
|
|
#define EXT4_POLICY_FLAGS_PAD_8 0x01
|
|
|
|
#define EXT4_POLICY_FLAGS_PAD_16 0x02
|
|
|
|
#define EXT4_POLICY_FLAGS_PAD_32 0x03
|
|
|
|
#define EXT4_POLICY_FLAGS_PAD_MASK 0x03
|
|
|
|
#define EXT4_POLICY_FLAGS_VALID 0x03
|
|
|
|
|
2015-04-11 19:48:01 +08:00
|
|
|
/**
|
|
|
|
* Encryption context for inode
|
|
|
|
*
|
|
|
|
* Protector format:
|
|
|
|
* 1 byte: Protector format (1 = this version)
|
|
|
|
* 1 byte: File contents encryption mode
|
|
|
|
* 1 byte: File names encryption mode
|
|
|
|
* 1 byte: Reserved
|
|
|
|
* 8 bytes: Master Key descriptor
|
|
|
|
* 16 bytes: Encryption Key derivation nonce
|
|
|
|
*/
|
|
|
|
struct ext4_encryption_context {
|
|
|
|
char format;
|
|
|
|
char contents_encryption_mode;
|
|
|
|
char filenames_encryption_mode;
|
2015-05-02 04:56:50 +08:00
|
|
|
char flags;
|
2015-04-11 19:48:01 +08:00
|
|
|
char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
|
|
|
|
char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
|
|
|
|
} __attribute__((__packed__));
|
|
|
|
|
2015-04-12 12:43:56 +08:00
|
|
|
/* Encryption parameters */
|
|
|
|
#define EXT4_XTS_TWEAK_SIZE 16
|
|
|
|
#define EXT4_AES_128_ECB_KEY_SIZE 16
|
|
|
|
#define EXT4_AES_256_GCM_KEY_SIZE 32
|
|
|
|
#define EXT4_AES_256_CBC_KEY_SIZE 32
|
|
|
|
#define EXT4_AES_256_CTS_KEY_SIZE 32
|
|
|
|
#define EXT4_AES_256_XTS_KEY_SIZE 64
|
|
|
|
#define EXT4_MAX_KEY_SIZE 64
|
|
|
|
|
2015-04-12 12:55:06 +08:00
|
|
|
#define EXT4_KEY_DESC_PREFIX "ext4:"
|
|
|
|
#define EXT4_KEY_DESC_PREFIX_SIZE 5
|
|
|
|
|
2015-05-19 01:16:47 +08:00
|
|
|
/* This is passed in from userspace into the kernel keyring */
|
2015-04-12 12:43:56 +08:00
|
|
|
struct ext4_encryption_key {
|
2015-05-19 01:16:47 +08:00
|
|
|
__u32 mode;
|
|
|
|
char raw[EXT4_MAX_KEY_SIZE];
|
|
|
|
__u32 size;
|
|
|
|
} __attribute__((__packed__));
|
|
|
|
|
|
|
|
struct ext4_crypt_info {
|
|
|
|
unsigned char ci_size;
|
ext4 crypto: reorganize how we store keys in the inode
This is a pretty massive patch which does a number of different things:
1) The per-inode encryption information is now stored in an allocated
data structure, ext4_crypt_info, instead of directly in the node.
This reduces the size usage of an in-memory inode when it is not
using encryption.
2) We drop the ext4_fname_crypto_ctx entirely, and use the per-inode
encryption structure instead. This remove an unnecessary memory
allocation and free for the fname_crypto_ctx as well as allowing us
to reuse the ctfm in a directory for multiple lookups and file
creations.
3) We also cache the inode's policy information in the ext4_crypt_info
structure so we don't have to continually read it out of the
extended attributes.
4) We now keep the keyring key in the inode's encryption structure
instead of releasing it after we are done using it to derive the
per-inode key. This allows us to test to see if the key has been
revoked; if it has, we prevent the use of the derived key and free
it.
5) When an inode is released (or when the derived key is freed), we
will use memset_explicit() to zero out the derived key, so it's not
left hanging around in memory. This implies that when a user logs
out, it is important to first revoke the key, and then unlink it,
and then finally, to use "echo 3 > /proc/sys/vm/drop_caches" to
release any decrypted pages and dcache entries from the system
caches.
6) All this, and we also shrink the number of lines of code by around
100. :-)
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2015-05-19 01:17:47 +08:00
|
|
|
char ci_data_mode;
|
|
|
|
char ci_filename_mode;
|
|
|
|
char ci_flags;
|
|
|
|
struct crypto_ablkcipher *ci_ctfm;
|
|
|
|
struct key *ci_keyring_key;
|
2015-05-19 01:16:47 +08:00
|
|
|
char ci_raw[EXT4_MAX_KEY_SIZE];
|
ext4 crypto: reorganize how we store keys in the inode
This is a pretty massive patch which does a number of different things:
1) The per-inode encryption information is now stored in an allocated
data structure, ext4_crypt_info, instead of directly in the node.
This reduces the size usage of an in-memory inode when it is not
using encryption.
2) We drop the ext4_fname_crypto_ctx entirely, and use the per-inode
encryption structure instead. This remove an unnecessary memory
allocation and free for the fname_crypto_ctx as well as allowing us
to reuse the ctfm in a directory for multiple lookups and file
creations.
3) We also cache the inode's policy information in the ext4_crypt_info
structure so we don't have to continually read it out of the
extended attributes.
4) We now keep the keyring key in the inode's encryption structure
instead of releasing it after we are done using it to derive the
per-inode key. This allows us to test to see if the key has been
revoked; if it has, we prevent the use of the derived key and free
it.
5) When an inode is released (or when the derived key is freed), we
will use memset_explicit() to zero out the derived key, so it's not
left hanging around in memory. This implies that when a user logs
out, it is important to first revoke the key, and then unlink it,
and then finally, to use "echo 3 > /proc/sys/vm/drop_caches" to
release any decrypted pages and dcache entries from the system
caches.
6) All this, and we also shrink the number of lines of code by around
100. :-)
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2015-05-19 01:17:47 +08:00
|
|
|
char ci_master_key[EXT4_KEY_DESCRIPTOR_SIZE];
|
2015-04-12 12:43:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
|
|
|
|
#define EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL 0x00000002
|
|
|
|
|
|
|
|
struct ext4_crypto_ctx {
|
|
|
|
struct crypto_tfm *tfm; /* Crypto API context */
|
|
|
|
struct page *bounce_page; /* Ciphertext page on write path */
|
|
|
|
struct page *control_page; /* Original page on write path */
|
|
|
|
struct bio *bio; /* The bio for this context */
|
|
|
|
struct work_struct work; /* Work queue for read complete path */
|
|
|
|
struct list_head free_list; /* Free list */
|
|
|
|
int flags; /* Flags */
|
|
|
|
int mode; /* Encryption mode for tfm */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ext4_completion_result {
|
|
|
|
struct completion completion;
|
|
|
|
int res;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECLARE_EXT4_COMPLETION_RESULT(ecr) \
|
|
|
|
struct ext4_completion_result ecr = { \
|
|
|
|
COMPLETION_INITIALIZER((ecr).completion), 0 }
|
|
|
|
|
|
|
|
static inline int ext4_encryption_key_size(int mode)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case EXT4_ENCRYPTION_MODE_AES_256_XTS:
|
|
|
|
return EXT4_AES_256_XTS_KEY_SIZE;
|
|
|
|
case EXT4_ENCRYPTION_MODE_AES_256_GCM:
|
|
|
|
return EXT4_AES_256_GCM_KEY_SIZE;
|
|
|
|
case EXT4_ENCRYPTION_MODE_AES_256_CBC:
|
|
|
|
return EXT4_AES_256_CBC_KEY_SIZE;
|
|
|
|
case EXT4_ENCRYPTION_MODE_AES_256_CTS:
|
|
|
|
return EXT4_AES_256_CTS_KEY_SIZE;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-12 12:56:17 +08:00
|
|
|
#define EXT4_FNAME_NUM_SCATTER_ENTRIES 4
|
|
|
|
#define EXT4_CRYPTO_BLOCK_SIZE 16
|
|
|
|
#define EXT4_FNAME_CRYPTO_DIGEST_SIZE 32
|
|
|
|
|
|
|
|
struct ext4_str {
|
|
|
|
unsigned char *name;
|
|
|
|
u32 len;
|
|
|
|
};
|
|
|
|
|
2015-04-16 13:55:00 +08:00
|
|
|
/**
|
|
|
|
* For encrypted symlinks, the ciphertext length is stored at the beginning
|
|
|
|
* of the string in little-endian format.
|
|
|
|
*/
|
|
|
|
struct ext4_encrypted_symlink_data {
|
|
|
|
__le16 len;
|
|
|
|
char encrypted_path[1];
|
|
|
|
} __attribute__((__packed__));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used to calculate the disk space required to
|
|
|
|
* store a filename of length l in encrypted symlink format.
|
|
|
|
*/
|
|
|
|
static inline u32 encrypted_symlink_data_len(u32 l)
|
|
|
|
{
|
|
|
|
if (l < EXT4_CRYPTO_BLOCK_SIZE)
|
|
|
|
l = EXT4_CRYPTO_BLOCK_SIZE;
|
|
|
|
return (l + sizeof(struct ext4_encrypted_symlink_data) - 1);
|
|
|
|
}
|
|
|
|
|
2015-04-11 19:48:01 +08:00
|
|
|
#endif /* _EXT4_CRYPTO_H */
|