2015-05-15 17:26:10 -06:00
|
|
|
/*
|
|
|
|
* General per-file encryption definition
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015, Google, Inc.
|
|
|
|
*
|
|
|
|
* Written by Michael Halcrow, 2015.
|
|
|
|
* Modified by Jaegeuk Kim, 2015.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_FSCRYPTO_H
|
|
|
|
#define _LINUX_FSCRYPTO_H
|
|
|
|
|
|
|
|
#include <linux/key.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/dcache.h>
|
2016-03-21 12:03:02 -06:00
|
|
|
#include <crypto/skcipher.h>
|
2015-05-15 17:26:10 -06:00
|
|
|
#include <uapi/linux/fs.h>
|
|
|
|
|
2016-11-26 20:05:18 -07:00
|
|
|
#define FS_CRYPTO_BLOCK_SIZE 16
|
2015-05-15 17:26:10 -06:00
|
|
|
|
2016-11-26 20:05:18 -07:00
|
|
|
struct fscrypt_info;
|
2015-05-15 17:26:10 -06:00
|
|
|
|
|
|
|
struct fscrypt_ctx {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
struct page *bounce_page; /* Ciphertext page */
|
|
|
|
struct page *control_page; /* Original page */
|
|
|
|
} w;
|
|
|
|
struct {
|
|
|
|
struct bio *bio;
|
|
|
|
struct work_struct work;
|
|
|
|
} r;
|
|
|
|
struct list_head free_list; /* Free list */
|
|
|
|
};
|
|
|
|
u8 flags; /* Flags */
|
|
|
|
u8 mode; /* Encryption mode for tfm */
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For encrypted symlinks, the ciphertext length is stored at the beginning
|
|
|
|
* of the string in little-endian format.
|
|
|
|
*/
|
|
|
|
struct fscrypt_symlink_data {
|
|
|
|
__le16 len;
|
|
|
|
char encrypted_path[1];
|
|
|
|
} __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 fscrypt_symlink_data_len(u32 l)
|
|
|
|
{
|
|
|
|
if (l < FS_CRYPTO_BLOCK_SIZE)
|
|
|
|
l = FS_CRYPTO_BLOCK_SIZE;
|
|
|
|
return (l + sizeof(struct fscrypt_symlink_data) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fscrypt_str {
|
|
|
|
unsigned char *name;
|
|
|
|
u32 len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fscrypt_name {
|
|
|
|
const struct qstr *usr_fname;
|
|
|
|
struct fscrypt_str disk_name;
|
|
|
|
u32 hash;
|
|
|
|
u32 minor_hash;
|
|
|
|
struct fscrypt_str crypto_buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FSTR_INIT(n, l) { .name = n, .len = l }
|
|
|
|
#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
|
|
|
|
#define fname_name(p) ((p)->disk_name.name)
|
|
|
|
#define fname_len(p) ((p)->disk_name.len)
|
|
|
|
|
2016-11-13 14:20:44 -07:00
|
|
|
/*
|
|
|
|
* fscrypt superblock flags
|
|
|
|
*/
|
2016-12-06 15:53:56 -07:00
|
|
|
#define FS_CFLG_OWN_PAGES (1U << 1)
|
2016-11-13 14:20:44 -07:00
|
|
|
|
2015-05-15 17:26:10 -06:00
|
|
|
/*
|
|
|
|
* crypto opertions for filesystems
|
|
|
|
*/
|
|
|
|
struct fscrypt_operations {
|
2016-11-13 14:20:44 -07:00
|
|
|
unsigned int flags;
|
2015-05-15 17:26:10 -06:00
|
|
|
int (*get_context)(struct inode *, void *, size_t);
|
2016-05-04 23:05:01 -06:00
|
|
|
int (*key_prefix)(struct inode *, u8 **);
|
2015-05-15 17:26:10 -06:00
|
|
|
int (*prepare_context)(struct inode *);
|
|
|
|
int (*set_context)(struct inode *, const void *, size_t, void *);
|
|
|
|
int (*dummy_context)(struct inode *);
|
|
|
|
bool (*is_encrypted)(struct inode *);
|
|
|
|
bool (*empty_dir)(struct inode *);
|
|
|
|
unsigned (*max_namelen)(struct inode *);
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
|
|
|
|
{
|
|
|
|
if (inode->i_sb->s_cop->dummy_context &&
|
|
|
|
inode->i_sb->s_cop->dummy_context(inode))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
|
|
|
|
{
|
|
|
|
return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
|
|
|
|
{
|
|
|
|
return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
|
|
|
|
{
|
|
|
|
if (str->len == 1 && str->name[0] == '.')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct page *fscrypt_control_page(struct page *page)
|
|
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
|
|
|
|
return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
|
|
|
|
#else
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:20:47 -07:00
|
|
|
static inline int fscrypt_has_encryption_key(const struct inode *inode)
|
2015-05-15 17:26:10 -06:00
|
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
|
|
|
|
return (inode->i_crypt_info != NULL);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
|
|
|
|
spin_lock(&dentry->d_lock);
|
|
|
|
dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
|
|
|
|
spin_unlock(&dentry->d_lock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
|
|
|
|
extern const struct dentry_operations fscrypt_d_ops;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void fscrypt_set_d_op(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
|
|
|
|
d_set_d_op(dentry, &fscrypt_d_ops);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
|
|
|
|
/* crypto.c */
|
|
|
|
extern struct kmem_cache *fscrypt_info_cachep;
|
2016-11-13 14:20:47 -07:00
|
|
|
extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
|
2015-05-15 17:26:10 -06:00
|
|
|
extern void fscrypt_release_ctx(struct fscrypt_ctx *);
|
2016-11-13 14:20:47 -07:00
|
|
|
extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
|
2016-11-13 14:20:46 -07:00
|
|
|
unsigned int, unsigned int,
|
2016-12-06 15:53:55 -07:00
|
|
|
u64, gfp_t);
|
2016-11-13 14:20:47 -07:00
|
|
|
extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
|
2016-12-06 15:53:55 -07:00
|
|
|
unsigned int, u64);
|
2015-05-15 17:26:10 -06:00
|
|
|
extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
|
|
|
|
extern void fscrypt_pullback_bio_page(struct page **, bool);
|
|
|
|
extern void fscrypt_restore_control_page(struct page *);
|
2016-11-13 14:20:47 -07:00
|
|
|
extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
|
2015-05-15 17:26:10 -06:00
|
|
|
unsigned int);
|
|
|
|
/* policy.c */
|
2016-11-26 17:07:49 -07:00
|
|
|
extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
|
|
|
|
extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
|
2015-05-15 17:26:10 -06:00
|
|
|
extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
|
|
|
|
extern int fscrypt_inherit_context(struct inode *, struct inode *,
|
|
|
|
void *, bool);
|
|
|
|
/* keyinfo.c */
|
|
|
|
extern int fscrypt_get_encryption_info(struct inode *);
|
|
|
|
extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
|
|
|
|
|
|
|
|
/* fname.c */
|
|
|
|
extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
|
|
|
|
int lookup, struct fscrypt_name *);
|
|
|
|
extern void fscrypt_free_filename(struct fscrypt_name *);
|
2016-11-13 14:20:47 -07:00
|
|
|
extern u32 fscrypt_fname_encrypted_size(const struct inode *, u32);
|
|
|
|
extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
|
2015-05-15 17:26:10 -06:00
|
|
|
struct fscrypt_str *);
|
|
|
|
extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
|
|
|
|
extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
|
|
|
|
const struct fscrypt_str *, struct fscrypt_str *);
|
|
|
|
extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
|
|
|
|
struct fscrypt_str *);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* crypto.c */
|
2016-11-13 14:20:47 -07:00
|
|
|
static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(const struct inode *i,
|
2016-04-11 16:51:57 -06:00
|
|
|
gfp_t f)
|
2015-05-15 17:26:10 -06:00
|
|
|
{
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:20:47 -07:00
|
|
|
static inline struct page *fscrypt_notsupp_encrypt_page(const struct inode *i,
|
2016-11-13 14:20:46 -07:00
|
|
|
struct page *p,
|
|
|
|
unsigned int len,
|
|
|
|
unsigned int offs,
|
2016-12-06 15:53:55 -07:00
|
|
|
u64 lblk_num, gfp_t f)
|
2015-05-15 17:26:10 -06:00
|
|
|
{
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:20:47 -07:00
|
|
|
static inline int fscrypt_notsupp_decrypt_page(const struct inode *i, struct page *p,
|
2016-11-13 14:20:48 -07:00
|
|
|
unsigned int len, unsigned int offs,
|
2016-12-06 15:53:55 -07:00
|
|
|
u64 lblk_num)
|
2015-05-15 17:26:10 -06:00
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
|
|
|
|
struct bio *b)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_notsupp_restore_control_page(struct page *p)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:20:47 -07:00
|
|
|
static inline int fscrypt_notsupp_zeroout_range(const struct inode *i, pgoff_t p,
|
2015-05-15 17:26:10 -06:00
|
|
|
sector_t s, unsigned int f)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* policy.c */
|
2016-11-26 17:07:49 -07:00
|
|
|
static inline int fscrypt_notsupp_ioctl_set_policy(struct file *f,
|
|
|
|
const void __user *arg)
|
2015-05-15 17:26:10 -06:00
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2016-11-26 17:07:49 -07:00
|
|
|
static inline int fscrypt_notsupp_ioctl_get_policy(struct file *f,
|
|
|
|
void __user *arg)
|
2015-05-15 17:26:10 -06:00
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
|
|
|
|
struct inode *i)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fscrypt_notsupp_inherit_context(struct inode *p,
|
|
|
|
struct inode *i, void *v, bool b)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* keyinfo.c */
|
|
|
|
static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
|
|
|
|
struct fscrypt_info *f)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fname.c */
|
|
|
|
static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
|
|
|
|
const struct qstr *iname,
|
|
|
|
int lookup, struct fscrypt_name *fname)
|
|
|
|
{
|
|
|
|
if (dir->i_sb->s_cop->is_encrypted(dir))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
memset(fname, 0, sizeof(struct fscrypt_name));
|
|
|
|
fname->usr_fname = iname;
|
|
|
|
fname->disk_name.name = (unsigned char *)iname->name;
|
|
|
|
fname->disk_name.len = iname->len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
|
|
|
|
{
|
|
|
|
/* never happens */
|
|
|
|
WARN_ON(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
|
|
|
|
u32 ilen, struct fscrypt_str *crypto_str)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode,
|
|
|
|
u32 hash, u32 minor_hash,
|
|
|
|
const struct fscrypt_str *iname,
|
|
|
|
struct fscrypt_str *oname)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
|
|
|
|
const struct qstr *iname,
|
|
|
|
struct fscrypt_str *oname)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
#endif /* _LINUX_FSCRYPTO_H */
|