fscrypt: make fscrypt_operations.key_prefix a string
authorEric Biggers <ebiggers@google.com>
Thu, 5 Jan 2017 21:51:18 +0000 (13:51 -0800)
committerTheodore Ts'o <tytso@mit.edu>
Sun, 8 Jan 2017 06:03:41 +0000 (01:03 -0500)
There was an unnecessary amount of complexity around requesting the
filesystem-specific key prefix.  It was unclear why; perhaps it was
envisioned that different instances of the same filesystem type could
use different key prefixes, or that key prefixes could be binary.
However, neither of those things were implemented or really make sense
at all.  So simplify the code by making key_prefix a const char *.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Richard Weinberger <richard@nod.at>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/crypto/keyinfo.c
fs/ext4/ext4.h
fs/ext4/super.c
fs/f2fs/f2fs.h
fs/f2fs/super.c
fs/ubifs/crypto.c
include/linux/fscrypto.h

index 80f145c..eeb6fd6 100644 (file)
@@ -77,26 +77,22 @@ out:
 
 static int validate_user_key(struct fscrypt_info *crypt_info,
                        struct fscrypt_context *ctx, u8 *raw_key,
-                       u8 *prefix, int prefix_size)
+                       const char *prefix)
 {
-       u8 *full_key_descriptor;
+       char *description;
        struct key *keyring_key;
        struct fscrypt_key *master_key;
        const struct user_key_payload *ukp;
-       int full_key_len = prefix_size + (FS_KEY_DESCRIPTOR_SIZE * 2) + 1;
        int res;
 
-       full_key_descriptor = kmalloc(full_key_len, GFP_NOFS);
-       if (!full_key_descriptor)
+       description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
+                               FS_KEY_DESCRIPTOR_SIZE,
+                               ctx->master_key_descriptor);
+       if (!description)
                return -ENOMEM;
 
-       memcpy(full_key_descriptor, prefix, prefix_size);
-       sprintf(full_key_descriptor + prefix_size,
-                       "%*phN", FS_KEY_DESCRIPTOR_SIZE,
-                       ctx->master_key_descriptor);
-       full_key_descriptor[full_key_len - 1] = '\0';
-       keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
-       kfree(full_key_descriptor);
+       keyring_key = request_key(&key_type_logon, description, NULL);
+       kfree(description);
        if (IS_ERR(keyring_key))
                return PTR_ERR(keyring_key);
 
@@ -251,15 +247,10 @@ retry:
        if (!raw_key)
                goto out;
 
-       res = validate_user_key(crypt_info, &ctx, raw_key,
-                       FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE);
+       res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX);
        if (res && inode->i_sb->s_cop->key_prefix) {
-               u8 *prefix = NULL;
-               int prefix_size, res2;
-
-               prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix);
-               res2 = validate_user_key(crypt_info, &ctx, raw_key,
-                                                       prefix, prefix_size);
+               int res2 = validate_user_key(crypt_info, &ctx, raw_key,
+                                            inode->i_sb->s_cop->key_prefix);
                if (res2) {
                        if (res2 == -ENOKEY)
                                res = -ENOKEY;
index 2163c1e..6bcb962 100644 (file)
@@ -1343,11 +1343,6 @@ struct ext4_super_block {
 /* Number of quota types we support */
 #define EXT4_MAXQUOTAS 3
 
-#ifdef CONFIG_EXT4_FS_ENCRYPTION
-#define EXT4_KEY_DESC_PREFIX "ext4:"
-#define EXT4_KEY_DESC_PREFIX_SIZE 5
-#endif
-
 /*
  * fourth extended-fs super-block data in memory
  */
@@ -1517,12 +1512,6 @@ struct ext4_sb_info {
 
        /* Barrier between changing inodes' journal flags and writepages ops. */
        struct percpu_rw_semaphore s_journal_flag_rwsem;
-
-       /* Encryption support */
-#ifdef CONFIG_EXT4_FS_ENCRYPTION
-       u8 key_prefix[EXT4_KEY_DESC_PREFIX_SIZE];
-       u8 key_prefix_size;
-#endif
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
index 66845a0..9d15a62 100644 (file)
@@ -1100,12 +1100,6 @@ static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
                                 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
 }
 
-static int ext4_key_prefix(struct inode *inode, u8 **key)
-{
-       *key = EXT4_SB(inode->i_sb)->key_prefix;
-       return EXT4_SB(inode->i_sb)->key_prefix_size;
-}
-
 static int ext4_prepare_context(struct inode *inode)
 {
        return ext4_convert_inline_data(inode);
@@ -1180,8 +1174,8 @@ static unsigned ext4_max_namelen(struct inode *inode)
 }
 
 static struct fscrypt_operations ext4_cryptops = {
+       .key_prefix             = "ext4:",
        .get_context            = ext4_get_context,
-       .key_prefix             = ext4_key_prefix,
        .prepare_context        = ext4_prepare_context,
        .set_context            = ext4_set_context,
        .dummy_context          = ext4_dummy_context,
@@ -4218,11 +4212,6 @@ no_journal:
        ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
 
        kfree(orig_data);
-#ifdef CONFIG_EXT4_FS_ENCRYPTION
-       memcpy(sbi->key_prefix, EXT4_KEY_DESC_PREFIX,
-                               EXT4_KEY_DESC_PREFIX_SIZE);
-       sbi->key_prefix_size = EXT4_KEY_DESC_PREFIX_SIZE;
-#endif
        return 0;
 
 cantfind_ext4:
index 2da8c3a..93d38d8 100644 (file)
@@ -760,10 +760,6 @@ enum {
        MAX_TIME,
 };
 
-#ifdef CONFIG_F2FS_FS_ENCRYPTION
-#define F2FS_KEY_DESC_PREFIX "f2fs:"
-#define F2FS_KEY_DESC_PREFIX_SIZE 5
-#endif
 struct f2fs_sb_info {
        struct super_block *sb;                 /* pointer to VFS super block */
        struct proc_dir_entry *s_proc;          /* proc entry */
@@ -771,11 +767,6 @@ struct f2fs_sb_info {
        int valid_super_block;                  /* valid super block no */
        unsigned long s_flag;                           /* flags for sbi */
 
-#ifdef CONFIG_F2FS_FS_ENCRYPTION
-       u8 key_prefix[F2FS_KEY_DESC_PREFIX_SIZE];
-       u8 key_prefix_size;
-#endif
-
 #ifdef CONFIG_BLK_DEV_ZONED
        unsigned int blocks_per_blkz;           /* F2FS blocks per zone */
        unsigned int log_blocks_per_blkz;       /* log2 F2FS blocks per zone */
index 702638e..739192d 100644 (file)
@@ -1156,12 +1156,6 @@ static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
                                ctx, len, NULL);
 }
 
-static int f2fs_key_prefix(struct inode *inode, u8 **key)
-{
-       *key = F2FS_I_SB(inode)->key_prefix;
-       return F2FS_I_SB(inode)->key_prefix_size;
-}
-
 static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
                                                        void *fs_data)
 {
@@ -1177,8 +1171,8 @@ static unsigned f2fs_max_namelen(struct inode *inode)
 }
 
 static struct fscrypt_operations f2fs_cryptops = {
+       .key_prefix     = "f2fs:",
        .get_context    = f2fs_get_context,
-       .key_prefix     = f2fs_key_prefix,
        .set_context    = f2fs_set_context,
        .is_encrypted   = f2fs_encrypted_inode,
        .empty_dir      = f2fs_empty_dir,
@@ -1518,12 +1512,6 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
        mutex_init(&sbi->wio_mutex[NODE]);
        mutex_init(&sbi->wio_mutex[DATA]);
        spin_lock_init(&sbi->cp_lock);
-
-#ifdef CONFIG_F2FS_FS_ENCRYPTION
-       memcpy(sbi->key_prefix, F2FS_KEY_DESC_PREFIX,
-                               F2FS_KEY_DESC_PREFIX_SIZE);
-       sbi->key_prefix_size = F2FS_KEY_DESC_PREFIX_SIZE;
-#endif
 }
 
 static int init_percpu_info(struct f2fs_sb_info *sbi)
index 3402720..6335abc 100644 (file)
@@ -26,15 +26,6 @@ static unsigned int ubifs_crypt_max_namelen(struct inode *inode)
                return UBIFS_MAX_NLEN;
 }
 
-static int ubifs_key_prefix(struct inode *inode, u8 **key)
-{
-       static char prefix[] = "ubifs:";
-
-       *key = prefix;
-
-       return sizeof(prefix) - 1;
-}
-
 int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
                  unsigned int in_len, unsigned int *out_len, int block)
 {
@@ -88,10 +79,10 @@ int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
 
 struct fscrypt_operations ubifs_crypt_operations = {
        .flags                  = FS_CFLG_OWN_PAGES,
+       .key_prefix             = "ubifs:",
        .get_context            = ubifs_crypt_get_context,
        .set_context            = ubifs_crypt_set_context,
        .is_encrypted           = __ubifs_crypt_is_encrypted,
        .empty_dir              = ubifs_crypt_empty_dir,
        .max_namelen            = ubifs_crypt_max_namelen,
-       .key_prefix             = ubifs_key_prefix,
 };
index 8635ea4..715f17b 100644 (file)
@@ -85,8 +85,8 @@ struct fscrypt_name {
  */
 struct fscrypt_operations {
        unsigned int flags;
+       const char *key_prefix;
        int (*get_context)(struct inode *, void *, size_t);
-       int (*key_prefix)(struct inode *, u8 **);
        int (*prepare_context)(struct inode *);
        int (*set_context)(struct inode *, const void *, size_t, void *);
        int (*dummy_context)(struct inode *);