Commit 39d2c3b9 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'upstream-4.10-rc1' of git://git.infradead.org/linux-ubifs

Pull ubifs updates from Richard Weinberger:

 - file encryption for UBIFS using the fscrypt framework

 - a fix to honor the dirty_writeback_interval sysctl

 - removal of dead code

* tag 'upstream-4.10-rc1' of git://git.infradead.org/linux-ubifs: (30 commits)
  ubifs: Initialize fstr_real_len
  ubifs: Use fscrypt ioctl() helpers
  ubifs: Use FS_CFLG_OWN_PAGES
  ubifs: Raise write version to 5
  ubifs: Implement UBIFS_FLG_ENCRYPTION
  ubifs: Implement UBIFS_FLG_DOUBLE_HASH
  ubifs: Use a random number for cookies
  ubifs: Add full hash lookup support
  ubifs: Rename tnc_read_node_nm
  ubifs: Add support for encrypted symlinks
  ubifs: Implement encrypted filenames
  ubifs: Make r5 hash binary string aware
  ubifs: Relax checks in ubifs_validate_entry()
  ubifs: Implement encrypt/decrypt for all IO
  ubifs: Constify struct inode pointer in ubifs_crypt_is_encrypted()
  ubifs: Introduce new data node field, compr_size
  ubifs: Enforce crypto policy in mmap
  ubifs: Massage assert in ubifs_xattr_set() wrt. fscrypto
  ubifs: Preload crypto context in ->lookup()
  ubifs: Enforce crypto policy in ->link and ->rename
  ...
parents e18bf801 ba75d570
...@@ -50,3 +50,14 @@ config UBIFS_ATIME_SUPPORT ...@@ -50,3 +50,14 @@ config UBIFS_ATIME_SUPPORT
strictatime is the "heavy", relatime is "lighter", etc. strictatime is the "heavy", relatime is "lighter", etc.
If unsure, say 'N' If unsure, say 'N'
config UBIFS_FS_ENCRYPTION
bool "UBIFS Encryption"
depends on UBIFS_FS
select FS_ENCRYPTION
default n
help
Enable encryption of UBIFS files and directories. This
feature is similar to ecryptfs, but it is more memory
efficient since it avoids caching the encrypted and
decrypted pages in the page cache.
...@@ -5,3 +5,4 @@ ubifs-y += tnc.o master.o scan.o replay.o log.o commit.o gc.o orphan.o ...@@ -5,3 +5,4 @@ ubifs-y += tnc.o master.o scan.o replay.o log.o commit.o gc.o orphan.o
ubifs-y += budget.o find.o tnc_commit.o compress.o lpt.o lprops.o ubifs-y += budget.o find.o tnc_commit.o compress.o lpt.o lprops.o
ubifs-y += recovery.o ioctl.o lpt_commit.o tnc_misc.o xattr.o debug.o ubifs-y += recovery.o ioctl.o lpt_commit.o tnc_misc.o xattr.o debug.o
ubifs-y += misc.o ubifs-y += misc.o
ubifs-$(CONFIG_UBIFS_FS_ENCRYPTION) += crypto.o
#include "ubifs.h"
static int ubifs_crypt_get_context(struct inode *inode, void *ctx, size_t len)
{
return ubifs_xattr_get(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
ctx, len);
}
static int ubifs_crypt_set_context(struct inode *inode, const void *ctx,
size_t len, void *fs_data)
{
return ubifs_xattr_set(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
ctx, len, 0);
}
static bool ubifs_crypt_empty_dir(struct inode *inode)
{
return ubifs_check_dir_empty(inode) == 0;
}
static unsigned int ubifs_crypt_max_namelen(struct inode *inode)
{
if (S_ISLNK(inode->i_mode))
return UBIFS_MAX_INO_DATA;
else
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)
{
struct ubifs_info *c = inode->i_sb->s_fs_info;
void *p = &dn->data;
struct page *ret;
unsigned int pad_len = round_up(in_len, UBIFS_CIPHER_BLOCK_SIZE);
ubifs_assert(pad_len <= *out_len);
dn->compr_size = cpu_to_le16(in_len);
/* pad to full block cipher length */
if (pad_len != in_len)
memset(p + in_len, 0, pad_len - in_len);
ret = fscrypt_encrypt_page(inode, virt_to_page(&dn->data), pad_len,
offset_in_page(&dn->data), block, GFP_NOFS);
if (IS_ERR(ret)) {
ubifs_err(c, "fscrypt_encrypt_page failed: %ld", PTR_ERR(ret));
return PTR_ERR(ret);
}
*out_len = pad_len;
return 0;
}
int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
unsigned int *out_len, int block)
{
struct ubifs_info *c = inode->i_sb->s_fs_info;
int err;
unsigned int clen = le16_to_cpu(dn->compr_size);
unsigned int dlen = *out_len;
if (clen <= 0 || clen > UBIFS_BLOCK_SIZE || clen > dlen) {
ubifs_err(c, "bad compr_size: %i", clen);
return -EINVAL;
}
ubifs_assert(dlen <= UBIFS_BLOCK_SIZE);
err = fscrypt_decrypt_page(inode, virt_to_page(&dn->data), dlen,
offset_in_page(&dn->data), block);
if (err) {
ubifs_err(c, "fscrypt_decrypt_page failed: %i", err);
return err;
}
*out_len = clen;
return 0;
}
struct fscrypt_operations ubifs_crypt_operations = {
.flags = FS_CFLG_OWN_PAGES,
.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,
};
...@@ -233,7 +233,7 @@ static void dump_ch(const struct ubifs_ch *ch) ...@@ -233,7 +233,7 @@ static void dump_ch(const struct ubifs_ch *ch)
void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode) void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
{ {
const struct ubifs_inode *ui = ubifs_inode(inode); const struct ubifs_inode *ui = ubifs_inode(inode);
struct qstr nm = { .name = NULL }; struct fscrypt_name nm = {0};
union ubifs_key key; union ubifs_key key;
struct ubifs_dent_node *dent, *pdent = NULL; struct ubifs_dent_node *dent, *pdent = NULL;
int count = 2; int count = 2;
...@@ -289,8 +289,8 @@ void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode) ...@@ -289,8 +289,8 @@ void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
pr_err("\t%d: %s (%s)\n", pr_err("\t%d: %s (%s)\n",
count++, dent->name, get_dent_type(dent->type)); count++, dent->name, get_dent_type(dent->type));
nm.name = dent->name; fname_name(&nm) = dent->name;
nm.len = le16_to_cpu(dent->nlen); fname_len(&nm) = le16_to_cpu(dent->nlen);
kfree(pdent); kfree(pdent);
pdent = dent; pdent = dent;
key_read(c, &dent->key, &key); key_read(c, &dent->key, &key);
...@@ -1107,7 +1107,7 @@ int dbg_check_dir(struct ubifs_info *c, const struct inode *dir) ...@@ -1107,7 +1107,7 @@ int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
unsigned int nlink = 2; unsigned int nlink = 2;
union ubifs_key key; union ubifs_key key;
struct ubifs_dent_node *dent, *pdent = NULL; struct ubifs_dent_node *dent, *pdent = NULL;
struct qstr nm = { .name = NULL }; struct fscrypt_name nm = {0};
loff_t size = UBIFS_INO_NODE_SZ; loff_t size = UBIFS_INO_NODE_SZ;
if (!dbg_is_chk_gen(c)) if (!dbg_is_chk_gen(c))
...@@ -1128,9 +1128,9 @@ int dbg_check_dir(struct ubifs_info *c, const struct inode *dir) ...@@ -1128,9 +1128,9 @@ int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
return err; return err;
} }
nm.name = dent->name; fname_name(&nm) = dent->name;
nm.len = le16_to_cpu(dent->nlen); fname_len(&nm) = le16_to_cpu(dent->nlen);
size += CALC_DENT_SIZE(nm.len); size += CALC_DENT_SIZE(fname_len(&nm));
if (dent->type == UBIFS_ITYPE_DIR) if (dent->type == UBIFS_ITYPE_DIR)
nlink += 1; nlink += 1;
kfree(pdent); kfree(pdent);
......
This diff is collapsed.
...@@ -78,6 +78,13 @@ static int read_block(struct inode *inode, void *addr, unsigned int block, ...@@ -78,6 +78,13 @@ static int read_block(struct inode *inode, void *addr, unsigned int block,
goto dump; goto dump;
dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
if (ubifs_crypt_is_encrypted(inode)) {
err = ubifs_decrypt(inode, dn, &dlen, block);
if (err)
goto dump;
}
out_len = UBIFS_BLOCK_SIZE; out_len = UBIFS_BLOCK_SIZE;
err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len, err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
le16_to_cpu(dn->compr_type)); le16_to_cpu(dn->compr_type));
...@@ -650,6 +657,13 @@ static int populate_page(struct ubifs_info *c, struct page *page, ...@@ -650,6 +657,13 @@ static int populate_page(struct ubifs_info *c, struct page *page,
dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
out_len = UBIFS_BLOCK_SIZE; out_len = UBIFS_BLOCK_SIZE;
if (ubifs_crypt_is_encrypted(inode)) {
err = ubifs_decrypt(inode, dn, &dlen, page_block);
if (err)
goto out_err;
}
err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len, err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
le16_to_cpu(dn->compr_type)); le16_to_cpu(dn->compr_type));
if (err || len != out_len) if (err || len != out_len)
...@@ -1594,6 +1608,15 @@ static const struct vm_operations_struct ubifs_file_vm_ops = { ...@@ -1594,6 +1608,15 @@ static const struct vm_operations_struct ubifs_file_vm_ops = {
static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma) static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
{ {
int err; int err;
struct inode *inode = file->f_mapping->host;
if (ubifs_crypt_is_encrypted(inode)) {
err = fscrypt_get_encryption_info(inode);
if (err)
return -EACCES;
if (!fscrypt_has_encryption_key(inode))
return -ENOKEY;
}
err = generic_file_mmap(file, vma); err = generic_file_mmap(file, vma);
if (err) if (err)
...@@ -1605,6 +1628,88 @@ static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma) ...@@ -1605,6 +1628,88 @@ static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
return 0; return 0;
} }
static int ubifs_file_open(struct inode *inode, struct file *filp)
{
int ret;
struct dentry *dir;
struct ubifs_info *c = inode->i_sb->s_fs_info;
if (ubifs_crypt_is_encrypted(inode)) {
ret = fscrypt_get_encryption_info(inode);
if (ret)
return -EACCES;
if (!fscrypt_has_encryption_key(inode))
return -ENOKEY;
}
dir = dget_parent(file_dentry(filp));
if (ubifs_crypt_is_encrypted(d_inode(dir)) &&
!fscrypt_has_permitted_context(d_inode(dir), inode)) {
ubifs_err(c, "Inconsistent encryption contexts: %lu/%lu",
(unsigned long) d_inode(dir)->i_ino,
(unsigned long) inode->i_ino);
dput(dir);
ubifs_ro_mode(c, -EPERM);
return -EPERM;
}
dput(dir);
return 0;
}
static const char *ubifs_get_link(struct dentry *dentry,
struct inode *inode,
struct delayed_call *done)
{
int err;
struct fscrypt_symlink_data *sd;
struct ubifs_inode *ui = ubifs_inode(inode);
struct fscrypt_str cstr;
struct fscrypt_str pstr;
if (!ubifs_crypt_is_encrypted(inode))
return ui->data;
if (!dentry)
return ERR_PTR(-ECHILD);
err = fscrypt_get_encryption_info(inode);
if (err)
return ERR_PTR(err);
sd = (struct fscrypt_symlink_data *)ui->data;
cstr.name = sd->encrypted_path;
cstr.len = le16_to_cpu(sd->len);
if (cstr.len == 0)
return ERR_PTR(-ENOENT);
if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > ui->data_len)
return ERR_PTR(-EIO);
err = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
if (err)
return ERR_PTR(err);
err = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
if (err) {
fscrypt_fname_free_buffer(&pstr);
return ERR_PTR(err);
}
pstr.name[pstr.len] = '\0';
// XXX this probably won't happen anymore...
if (pstr.name[0] == '\0') {
fscrypt_fname_free_buffer(&pstr);
return ERR_PTR(-ENOENT);
}
set_delayed_call(done, kfree_link, pstr.name);
return pstr.name;
}
const struct address_space_operations ubifs_file_address_operations = { const struct address_space_operations ubifs_file_address_operations = {
.readpage = ubifs_readpage, .readpage = ubifs_readpage,
.writepage = ubifs_writepage, .writepage = ubifs_writepage,
...@@ -1629,7 +1734,7 @@ const struct inode_operations ubifs_file_inode_operations = { ...@@ -1629,7 +1734,7 @@ const struct inode_operations ubifs_file_inode_operations = {
const struct inode_operations ubifs_symlink_inode_operations = { const struct inode_operations ubifs_symlink_inode_operations = {
.readlink = generic_readlink, .readlink = generic_readlink,
.get_link = simple_get_link, .get_link = ubifs_get_link,
.setattr = ubifs_setattr, .setattr = ubifs_setattr,
.getattr = ubifs_getattr, .getattr = ubifs_getattr,
.listxattr = ubifs_listxattr, .listxattr = ubifs_listxattr,
...@@ -1647,6 +1752,7 @@ const struct file_operations ubifs_file_operations = { ...@@ -1647,6 +1752,7 @@ const struct file_operations ubifs_file_operations = {
.unlocked_ioctl = ubifs_ioctl, .unlocked_ioctl = ubifs_ioctl,
.splice_read = generic_file_splice_read, .splice_read = generic_file_splice_read,
.splice_write = iter_file_splice_write, .splice_write = iter_file_splice_write,
.open = ubifs_file_open,
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
.compat_ioctl = ubifs_compat_ioctl, .compat_ioctl = ubifs_compat_ioctl,
#endif #endif
......
...@@ -846,10 +846,6 @@ int ubifs_gc_start_commit(struct ubifs_info *c) ...@@ -846,10 +846,6 @@ int ubifs_gc_start_commit(struct ubifs_info *c)
*/ */
while (1) { while (1) {
lp = ubifs_fast_find_freeable(c); lp = ubifs_fast_find_freeable(c);
if (IS_ERR(lp)) {
err = PTR_ERR(lp);
goto out;
}
if (!lp) if (!lp)
break; break;
ubifs_assert(!(lp->flags & LPROPS_TAKEN)); ubifs_assert(!(lp->flags & LPROPS_TAKEN));
......
...@@ -452,16 +452,22 @@ static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer) ...@@ -452,16 +452,22 @@ static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
*/ */
static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf) static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
{ {
ktime_t softlimit = ms_to_ktime(dirty_writeback_interval * 10);
unsigned long long delta = dirty_writeback_interval;
/* centi to milli, milli to nano, then 10% */
delta *= 10ULL * NSEC_PER_MSEC / 10ULL;
ubifs_assert(!hrtimer_active(&wbuf->timer)); ubifs_assert(!hrtimer_active(&wbuf->timer));
ubifs_assert(delta <= ULONG_MAX);
if (wbuf->no_timer) if (wbuf->no_timer)
return; return;
dbg_io("set timer for jhead %s, %llu-%llu millisecs", dbg_io("set timer for jhead %s, %llu-%llu millisecs",
dbg_jhead(wbuf->jhead), dbg_jhead(wbuf->jhead),
div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC), div_u64(ktime_to_ns(softlimit), USEC_PER_SEC),
div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta, div_u64(ktime_to_ns(softlimit) + delta, USEC_PER_SEC));
USEC_PER_SEC)); hrtimer_start_range_ns(&wbuf->timer, softlimit, delta,
hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
HRTIMER_MODE_REL); HRTIMER_MODE_REL);
} }
...@@ -1059,10 +1065,6 @@ int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf) ...@@ -1059,10 +1065,6 @@ int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
wbuf->timer.function = wbuf_timer_callback_nolock; wbuf->timer.function = wbuf_timer_callback_nolock;
wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
wbuf->delta *= 1000000000ULL;
ubifs_assert(wbuf->delta <= ULONG_MAX);
return 0; return 0;
} }
......
...@@ -181,6 +181,26 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) ...@@ -181,6 +181,26 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
mnt_drop_write_file(file); mnt_drop_write_file(file);
return err; return err;
} }
case FS_IOC_SET_ENCRYPTION_POLICY: {
#ifdef CONFIG_UBIFS_FS_ENCRYPTION
struct ubifs_info *c = inode->i_sb->s_fs_info;
err = ubifs_enable_encryption(c);
if (err)
return err;
return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
#else
return -EOPNOTSUPP;
#endif
}
case FS_IOC_GET_ENCRYPTION_POLICY: {
#ifdef CONFIG_UBIFS_FS_ENCRYPTION
return fscrypt_ioctl_get_policy(file, (void __user *)arg);
#else
return -EOPNOTSUPP;
#endif
}
default: default:
return -ENOTTY; return -ENOTTY;
......
This diff is collapsed.
...@@ -69,7 +69,7 @@ static inline uint32_t key_r5_hash(const char *s, int len) ...@@ -69,7 +69,7 @@ static inline uint32_t key_r5_hash(const char *s, int len)
uint32_t a = 0; uint32_t a = 0;
const signed char *str = (const signed char *)s; const signed char *str = (const signed char *)s;
while (*str) { while (len--) {
a += *str << 4; a += *str << 4;
a += *str >> 4; a += *str >> 4;
a *= 11; a *= 11;
...@@ -153,13 +153,13 @@ static inline void highest_ino_key(const struct ubifs_info *c, ...@@ -153,13 +153,13 @@ static inline void highest_ino_key(const struct ubifs_info *c,
* @c: UBIFS file-system description object * @c: UBIFS file-system description object
* @key: key to initialize * @key: key to initialize
* @inum: parent inode number * @inum: parent inode number
* @nm: direntry name and length * @nm: direntry name and length. Not a string when encrypted!
*/ */
static inline void dent_key_init(const struct ubifs_info *c, static inline void dent_key_init(const struct ubifs_info *c,
union ubifs_key *key, ino_t inum, union ubifs_key *key, ino_t inum,
const struct qstr *nm) const struct fscrypt_name *nm)
{ {
uint32_t hash = c->key_hash(nm->name, nm->len); uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK)); ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
key->u32[0] = inum; key->u32[0] = inum;
...@@ -191,10 +191,11 @@ static inline void dent_key_init_hash(const struct ubifs_info *c, ...@@ -191,10 +191,11 @@ static inline void dent_key_init_hash(const struct ubifs_info *c,
* @nm: direntry name and length * @nm: direntry name and length
*/ */
static inline void dent_key_init_flash(const struct ubifs_info *c, void *k, static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
ino_t inum, const struct qstr *nm) ino_t inum,
const struct fscrypt_name *nm)
{ {
union ubifs_key *key = k; union ubifs_key *key = k;
uint32_t hash = c->key_hash(nm->name, nm->len); uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK)); ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
key->j32[0] = cpu_to_le32(inum); key->j32[0] = cpu_to_le32(inum);
...@@ -225,9 +226,9 @@ static inline void lowest_dent_key(const struct ubifs_info *c, ...@@ -225,9 +226,9 @@ static inline void lowest_dent_key(const struct ubifs_info *c,
*/ */
static inline void xent_key_init(const struct ubifs_info *c, static inline void xent_key_init(const struct ubifs_info *c,
union ubifs_key *key, ino_t inum, union ubifs_key *key, ino_t inum,
const struct qstr *nm) const struct fscrypt_name *nm)
{ {
uint32_t hash = c->key_hash(nm->name, nm->len); uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK)); ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
key->u32[0] = inum; key->u32[0] = inum;
...@@ -242,10 +243,10 @@ static inline void xent_key_init(const struct ubifs_info *c, ...@@ -242,10 +243,10 @@ static inline void xent_key_init(const struct ubifs_info *c,
* @nm: extended attribute entry name and length * @nm: extended attribute entry name and length
*/ */
static inline void xent_key_init_flash(const struct ubifs_info *c, void *k, static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
ino_t inum, const struct qstr *nm) ino_t inum, const struct fscrypt_name *nm)
{ {
union ubifs_key *key = k; union ubifs_key *key = k;
uint32_t hash = c->key_hash(nm->name, nm->len); uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK)); ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
key->j32[0] = cpu_to_le32(inum); key->j32[0] = cpu_to_le32(inum);
......
...@@ -61,7 +61,7 @@ struct replay_entry { ...@@ -61,7 +61,7 @@ struct replay_entry {
struct list_head list; struct list_head list;
union ubifs_key key; union ubifs_key key;
union { union {
struct qstr nm; struct fscrypt_name nm;
struct { struct {
loff_t old_size; loff_t old_size;
loff_t new_size; loff_t new_size;
...@@ -327,7 +327,7 @@ static void destroy_replay_list(struct ubifs_info *c) ...@@ -327,7 +327,7 @@ static void destroy_replay_list(struct ubifs_info *c)
list_for_each_entry_safe(r, tmp, &c->replay_list, list) { list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
if (is_hash_key(c, &r->key)) if (is_hash_key(c, &r->key))
kfree(r->nm.name); kfree(fname_name(&r->nm));
list_del(&r->list); list_del(&r->list);
kfree(r); kfree(r);
} }
...@@ -430,10 +430,10 @@ static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len, ...@@ -430,10 +430,10 @@ static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
r->deletion = !!deletion; r->deletion = !!deletion;
r->sqnum = sqnum; r->sqnum = sqnum;
key_copy(c, key, &r->key); key_copy(c, key, &r->key);
r->nm.len = nlen; fname_len(&r->nm) = nlen;
memcpy(nbuf, name, nlen); memcpy(nbuf, name, nlen);
nbuf[nlen] = '\0'; nbuf[nlen] = '\0';
r->nm.name = nbuf; fname_name(&r->nm) = nbuf;
list_add_tail(&r->list, &c->replay_list); list_add_tail(&r->list, &c->replay_list);
return 0; return 0;
...@@ -456,7 +456,7 @@ int ubifs_validate_entry(struct ubifs_info *c, ...@@ -456,7 +456,7 @@ int ubifs_validate_entry(struct ubifs_info *c,
if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 || if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
dent->type >= UBIFS_ITYPES_CNT || dent->type >= UBIFS_ITYPES_CNT ||
nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 || nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
strnlen(dent->name, nlen) != nlen || (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
le64_to_cpu(dent->inum) > MAX_INUM) { le64_to_cpu(dent->inum) > MAX_INUM) {
ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ? ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
"directory entry" : "extended attribute entry"); "directory entry" : "extended attribute entry");
......
...@@ -163,6 +163,7 @@ static int create_default_filesystem(struct ubifs_info *c) ...@@ -163,6 +163,7 @@ static int create_default_filesystem(struct ubifs_info *c)
tmp64 = (long long)max_buds * c->leb_size; tmp64 = (long long)max_buds * c->leb_size;
if (big_lpt) if (big_lpt)
sup_flags |= UBIFS_FLG_BIGLPT; sup_flags |= UBIFS_FLG_BIGLPT;
sup_flags |= UBIFS_FLG_DOUBLE_HASH;
sup->ch.node_type = UBIFS_SB_NODE; sup->ch.node_type = UBIFS_SB_NODE;
sup->key_hash = UBIFS_KEY_HASH_R5; sup->key_hash = UBIFS_KEY_HASH_R5;
...@@ -465,6 +466,16 @@ static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup) ...@@ -465,6 +466,16 @@ static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
goto failed; goto failed;
} }
if (!c->double_hash && c->fmt_version >= 5) {
err = 16;
goto failed;
}
if (c->encrypted && c->fmt_version < 5) {
err = 17;
goto failed;
}
return 0; return 0;
failed: failed:
...@@ -620,6 +631,24 @@ int ubifs_read_superblock(struct ubifs_info *c) ...@@ -620,6 +631,24 @@ int ubifs_read_superblock(struct ubifs_info *c)
memcpy(&c->uuid, &sup->uuid, 16); memcpy(&c->uuid, &sup->uuid, 16);
c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT); c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP); c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
ubifs_err(c, "Unknown feature flags found: %#x",
sup_flags & ~UBIFS_FLG_MASK);
err = -EINVAL;
goto out;
}
#ifndef CONFIG_UBIFS_FS_ENCRYPTION
if (c->encrypted) {
ubifs_err(c, "file system contains encrypted files but UBIFS"
" was built without crypto support.");
err = -EINVAL;
goto out;
}
#endif
/* Automatically increase file system size to the maximum size */ /* Automatically increase file system size to the maximum size */
c->old_leb_cnt = c->leb_cnt; c->old_leb_cnt = c->leb_cnt;
...@@ -807,3 +836,33 @@ int ubifs_fixup_free_space(struct ubifs_info *c) ...@@ -807,3 +836,33 @@ int ubifs_fixup_free_space(struct ubifs_info *c)
ubifs_msg(c, "free space fixup complete"); ubifs_msg(c, "free space fixup complete");
return err; return err;
} }
int ubifs_enable_encryption(struct ubifs_info *c)
{
int err;
struct ubifs_sb_node *sup;
if (c->encrypted)
return 0;
if (c->ro_mount || c->ro_media)
return -EROFS;
if (c->fmt_version < 5) {
ubifs_err(c, "on-flash format version 5 is needed for encryption");
return -EINVAL;
}
sup = ubifs_read_sb_node(c);
if (IS_ERR(sup))
return PTR_ERR(sup);
sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
err = ubifs_write_sb_node(c, sup);
if (!err)
c->encrypted = 1;
kfree(sup);
return err;
}
...@@ -198,7 +198,6 @@ struct inode *ubifs_iget(struct super_block *sb, unsigned long inum) ...@@ -198,7 +198,6 @@ struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
} }
memcpy(ui->data, ino->data, ui->data_len); memcpy(ui->data, ino->data, ui->data_len);
((char *)ui->data)[ui->data_len] = '\0'; ((char *)ui->data)[ui->data_len] = '\0';
inode->i_link = ui->data;
break; break;
case S_IFBLK: case S_IFBLK:
case S_IFCHR: case S_IFCHR:
...@@ -380,6 +379,9 @@ static void ubifs_evict_inode(struct inode *inode) ...@@ -380,6 +379,9 @@ static void ubifs_evict_inode(struct inode *inode)
} }
done: done:
clear_inode(inode); clear_inode(inode);
#ifdef CONFIG_UBIFS_FS_ENCRYPTION
fscrypt_put_encryption_info(inode, NULL);
#endif
} }
static void ubifs_dirty_inode(struct inode *inode, int flags) static void ubifs_dirty_inode(struct inode *inode, int flags)
...@@ -1207,7 +1209,8 @@ static int mount_ubifs(struct ubifs_info *c) ...@@ -1207,7 +1209,8 @@ static int mount_ubifs(struct ubifs_info *c)
bu_init(c); bu_init(c);
if (!c->ro_mount) { if (!c->ro_mount) {
c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ, c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
UBIFS_CIPHER_BLOCK_SIZE,
GFP_KERNEL); GFP_KERNEL);
if (!c->write_reserve_buf) if (!c->write_reserve_buf)
goto out_free; goto out_free;
...@@ -1620,7 +1623,8 @@ static int ubifs_remount_rw(struct ubifs_info *c) ...@@ -1620,7 +1623,8 @@ static int ubifs_remount_rw(struct ubifs_info *c)
goto out; goto out;
} }
c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ, GFP_KERNEL); c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
UBIFS_CIPHER_BLOCK_SIZE, GFP_KERNEL);
if (!c->write_reserve_buf) { if (!c->write_reserve_buf) {
err = -ENOMEM; err = -ENOMEM;
goto out; goto out;
...@@ -1995,6 +1999,12 @@ static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi) ...@@ -1995,6 +1999,12 @@ static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi)
return c; return c;
} }
#ifndef CONFIG_UBIFS_FS_ENCRYPTION
struct fscrypt_operations ubifs_crypt_operations = {
.is_encrypted = __ubifs_crypt_is_encrypted,
};
#endif
static int ubifs_fill_super(struct super_block *sb, void *data, int silent) static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
{ {
struct ubifs_info *c = sb->s_fs_info; struct ubifs_info *c = sb->s_fs_info;
...@@ -2041,6 +2051,7 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent) ...@@ -2041,6 +2051,7 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE; sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
sb->s_op = &ubifs_super_operations; sb->s_op = &ubifs_super_operations;
sb->s_xattr = ubifs_xattr_handlers; sb->s_xattr = ubifs_xattr_handlers;
sb->s_cop = &ubifs_crypt_operations;
mutex_lock(&c->umount_mutex); mutex_lock(&c->umount_mutex);
err = mount_ubifs(c); err = mount_ubifs(c);
......
This diff is collapsed.
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
* UBIFS went into mainline kernel with format version 4. The older formats * UBIFS went into mainline kernel with format version 4. The older formats
* were development formats. * were development formats.
*/ */
#define UBIFS_FORMAT_VERSION 4 #define UBIFS_FORMAT_VERSION 5
/* /*
* Read-only compatibility version. If the UBIFS format is changed, older UBIFS * Read-only compatibility version. If the UBIFS format is changed, older UBIFS
...@@ -300,6 +300,13 @@ enum { ...@@ -300,6 +300,13 @@ enum {
/* The largest UBIFS node */ /* The largest UBIFS node */
#define UBIFS_MAX_NODE_SZ UBIFS_MAX_INO_NODE_SZ #define UBIFS_MAX_NODE_SZ UBIFS_MAX_INO_NODE_SZ
/*
* xattr name of UBIFS encryption context, we don't use a prefix
* nor a long name to not waste space on the flash.
*/
#define UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT "c"
/* /*
* On-flash inode flags. * On-flash inode flags.
* *
...@@ -309,6 +316,7 @@ enum { ...@@ -309,6 +316,7 @@ enum {
* UBIFS_APPEND_FL: writes to the inode may only append data * UBIFS_APPEND_FL: writes to the inode may only append data
* UBIFS_DIRSYNC_FL: I/O on this directory inode has to be synchronous * UBIFS_DIRSYNC_FL: I/O on this directory inode has to be synchronous
* UBIFS_XATTR_FL: this inode is the inode for an extended attribute value * UBIFS_XATTR_FL: this inode is the inode for an extended attribute value
* UBIFS_CRYPT_FL: use encryption for this inode
* *
* Note, these are on-flash flags which correspond to ioctl flags * Note, these are on-flash flags which correspond to ioctl flags
* (@FS_COMPR_FL, etc). They have the same values now, but generally, do not * (@FS_COMPR_FL, etc). They have the same values now, but generally, do not
...@@ -321,6 +329,7 @@ enum { ...@@ -321,6 +329,7 @@ enum {
UBIFS_APPEND_FL = 0x08, UBIFS_APPEND_FL = 0x08,
UBIFS_DIRSYNC_FL = 0x10, UBIFS_DIRSYNC_FL = 0x10,
UBIFS_XATTR_FL = 0x20, UBIFS_XATTR_FL = 0x20,
UBIFS_CRYPT_FL = 0x40,
}; };
/* Inode flag bits used by UBIFS */ /* Inode flag bits used by UBIFS */
...@@ -409,12 +418,19 @@ enum { ...@@ -409,12 +418,19 @@ enum {
* *
* UBIFS_FLG_BIGLPT: if "big" LPT model is used if set * UBIFS_FLG_BIGLPT: if "big" LPT model is used if set
* UBIFS_FLG_SPACE_FIXUP: first-mount "fixup" of free space within LEBs needed * UBIFS_FLG_SPACE_FIXUP: first-mount "fixup" of free space within LEBs needed
* UBIFS_FLG_DOUBLE_HASH: store a 32bit cookie in directory entry nodes to
* support 64bit cookies for lookups by hash
* UBIFS_FLG_ENCRYPTION: this filesystem contains encrypted files
*/ */
enum { enum {
UBIFS_FLG_BIGLPT = 0x02, UBIFS_FLG_BIGLPT = 0x02,
UBIFS_FLG_SPACE_FIXUP = 0x04, UBIFS_FLG_SPACE_FIXUP = 0x04,
UBIFS_FLG_DOUBLE_HASH = 0x08,
UBIFS_FLG_ENCRYPTION = 0x10,
}; };
#define UBIFS_FLG_MASK (UBIFS_FLG_BIGLPT|UBIFS_FLG_SPACE_FIXUP|UBIFS_FLG_DOUBLE_HASH|UBIFS_FLG_ENCRYPTION)
/** /**
* struct ubifs_ch - common header node. * struct ubifs_ch - common header node.
* @magic: UBIFS node magic number (%UBIFS_NODE_MAGIC) * @magic: UBIFS node magic number (%UBIFS_NODE_MAGIC)
...@@ -521,7 +537,8 @@ struct ubifs_ino_node { ...@@ -521,7 +537,8 @@ struct ubifs_ino_node {
* @padding1: reserved for future, zeroes * @padding1: reserved for future, zeroes
* @type: type of the target inode (%UBIFS_ITYPE_REG, %UBIFS_ITYPE_DIR, etc) * @type: type of the target inode (%UBIFS_ITYPE_REG, %UBIFS_ITYPE_DIR, etc)
* @nlen: name length * @nlen: name length
* @padding2: reserved for future, zeroes * @cookie: A 32bits random number, used to construct a 64bits
* identifier.
* @name: zero-terminated name * @name: zero-terminated name
* *
* Note, do not forget to amend 'zero_dent_node_unused()' function when * Note, do not forget to amend 'zero_dent_node_unused()' function when
...@@ -534,7 +551,7 @@ struct ubifs_dent_node { ...@@ -534,7 +551,7 @@ struct ubifs_dent_node {
__u8 padding1; __u8 padding1;
__u8 type; __u8 type;
__le16 nlen; __le16 nlen;
__u8 padding2[4]; /* Watch 'zero_dent_node_unused()' if changing! */ __le32 cookie;
__u8 name[]; __u8 name[];
} __packed; } __packed;
...@@ -544,18 +561,16 @@ struct ubifs_dent_node { ...@@ -544,18 +561,16 @@ struct ubifs_dent_node {
* @key: node key * @key: node key
* @size: uncompressed data size in bytes * @size: uncompressed data size in bytes
* @compr_type: compression type (%UBIFS_COMPR_NONE, %UBIFS_COMPR_LZO, etc) * @compr_type: compression type (%UBIFS_COMPR_NONE, %UBIFS_COMPR_LZO, etc)
* @padding: reserved for future, zeroes * @compr_size: compressed data size in bytes, only valid when data is encrypted
* @data: data * @data: data
* *
* Note, do not forget to amend 'zero_data_node_unused()' function when
* changing the padding fields.
*/ */
struct ubifs_data_node { struct ubifs_data_node {
struct ubifs_ch ch; struct ubifs_ch ch;
__u8 key[UBIFS_MAX_KEY_LEN]; __u8 key[UBIFS_MAX_KEY_LEN];
__le32 size; __le32 size;
__le16 compr_type; __le16 compr_type;
__u8 padding[2]; /* Watch 'zero_data_node_unused()' if changing! */ __le16 compr_size;
__u8 data[]; __u8 data[];
} __packed; } __packed;
......
This diff is collapsed.
...@@ -97,7 +97,7 @@ static const struct file_operations empty_fops; ...@@ -97,7 +97,7 @@ static const struct file_operations empty_fops;
* of failure. * of failure.
*/ */
static int create_xattr(struct ubifs_info *c, struct inode *host, static int create_xattr(struct ubifs_info *c, struct inode *host,
const struct qstr *nm, const void *value, int size) const struct fscrypt_name *nm, const void *value, int size)
{ {
int err, names_len; int err, names_len;
struct inode *inode; struct inode *inode;
...@@ -117,7 +117,7 @@ static int create_xattr(struct ubifs_info *c, struct inode *host, ...@@ -117,7 +117,7 @@ static int create_xattr(struct ubifs_info *c, struct inode *host,
* extended attributes if the name list becomes larger. This limitation * extended attributes if the name list becomes larger. This limitation
* is artificial for UBIFS, though. * is artificial for UBIFS, though.
*/ */
names_len = host_ui->xattr_names + host_ui->xattr_cnt + nm->len + 1; names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
if (names_len > XATTR_LIST_MAX) { if (names_len > XATTR_LIST_MAX) {
ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d", ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
host->i_ino, names_len, XATTR_LIST_MAX); host->i_ino, names_len, XATTR_LIST_MAX);
...@@ -154,9 +154,18 @@ static int create_xattr(struct ubifs_info *c, struct inode *host, ...@@ -154,9 +154,18 @@ static int create_xattr(struct ubifs_info *c, struct inode *host,
mutex_lock(&host_ui->ui_mutex); mutex_lock(&host_ui->ui_mutex);
host->i_ctime = ubifs_current_time(host); host->i_ctime = ubifs_current_time(host);
host_ui->xattr_cnt += 1; host_ui->xattr_cnt += 1;
host_ui->xattr_size += CALC_DENT_SIZE(nm->len); host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
host_ui->xattr_size += CALC_XATTR_BYTES(size); host_ui->xattr_size += CALC_XATTR_BYTES(size);
host_ui->xattr_names += nm->len; host_ui->xattr_names += fname_len(nm);
/*
* We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
* have to set the UBIFS_CRYPT_FL flag on the host inode.
* To avoid multiple updates of the same inode in the same operation,
* let's do it here.
*/
if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
host_ui->flags |= UBIFS_CRYPT_FL;
err = ubifs_jnl_update(c, host, nm, inode, 0, 1); err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
if (err) if (err)
...@@ -170,9 +179,10 @@ static int create_xattr(struct ubifs_info *c, struct inode *host, ...@@ -170,9 +179,10 @@ static int create_xattr(struct ubifs_info *c, struct inode *host,
out_cancel: out_cancel:
host_ui->xattr_cnt -= 1; host_ui->xattr_cnt -= 1;
host_ui->xattr_size -= CALC_DENT_SIZE(nm->len); host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
host_ui->xattr_size -= CALC_XATTR_BYTES(size); host_ui->xattr_size -= CALC_XATTR_BYTES(size);
host_ui->xattr_names -= nm->len; host_ui->xattr_names -= fname_len(nm);
host_ui->flags &= ~UBIFS_CRYPT_FL;
mutex_unlock(&host_ui->ui_mutex); mutex_unlock(&host_ui->ui_mutex);
out_free: out_free:
make_bad_inode(inode); make_bad_inode(inode);
...@@ -269,22 +279,28 @@ static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum) ...@@ -269,22 +279,28 @@ static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
static int __ubifs_setxattr(struct inode *host, const char *name, int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
const void *value, size_t size, int flags) size_t size, int flags)
{ {
struct inode *inode; struct inode *inode;
struct ubifs_info *c = host->i_sb->s_fs_info; struct ubifs_info *c = host->i_sb->s_fs_info;
struct qstr nm = QSTR_INIT(name, strlen(name)); struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
struct ubifs_dent_node *xent; struct ubifs_dent_node *xent;
union ubifs_key key; union ubifs_key key;
int err; int err;
/*
* Creating an encryption context is done unlocked since we
* operate on a new inode which is not visible to other users
* at this point.
*/
if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) != 0)
ubifs_assert(inode_is_locked(host)); ubifs_assert(inode_is_locked(host));
if (size > UBIFS_MAX_INO_DATA) if (size > UBIFS_MAX_INO_DATA)
return -ERANGE; return -ERANGE;
if (nm.len > UBIFS_MAX_NLEN) if (fname_len(&nm) > UBIFS_MAX_NLEN)
return -ENAMETOOLONG; return -ENAMETOOLONG;
xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS); xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
...@@ -329,18 +345,18 @@ static int __ubifs_setxattr(struct inode *host, const char *name, ...@@ -329,18 +345,18 @@ static int __ubifs_setxattr(struct inode *host, const char *name,
return err; return err;
} }
static ssize_t __ubifs_getxattr(struct inode *host, const char *name, ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
void *buf, size_t size) size_t size)
{ {
struct inode *inode; struct inode *inode;
struct ubifs_info *c = host->i_sb->s_fs_info; struct ubifs_info *c = host->i_sb->s_fs_info;
struct qstr nm = QSTR_INIT(name, strlen(name)); struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
struct ubifs_inode *ui; struct ubifs_inode *ui;
struct ubifs_dent_node *xent; struct ubifs_dent_node *xent;
union ubifs_key key; union ubifs_key key;
int err; int err;
if (nm.len > UBIFS_MAX_NLEN) if (fname_len(&nm) > UBIFS_MAX_NLEN)
return -ENAMETOOLONG; return -ENAMETOOLONG;
xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS); xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
...@@ -387,6 +403,20 @@ static ssize_t __ubifs_getxattr(struct inode *host, const char *name, ...@@ -387,6 +403,20 @@ static ssize_t __ubifs_getxattr(struct inode *host, const char *name,
return err; return err;
} }
static bool xattr_visible(const char *name)
{
/* File encryption related xattrs are for internal use only */
if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
return false;
/* Show trusted namespace only for "power" users */
if (strncmp(name, XATTR_TRUSTED_PREFIX,
XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
return false;
return true;
}
ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size) ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
{ {
union ubifs_key key; union ubifs_key key;
...@@ -395,7 +425,7 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size) ...@@ -395,7 +425,7 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
struct ubifs_inode *host_ui = ubifs_inode(host); struct ubifs_inode *host_ui = ubifs_inode(host);
struct ubifs_dent_node *xent, *pxent = NULL; struct ubifs_dent_node *xent, *pxent = NULL;
int err, len, written = 0; int err, len, written = 0;
struct qstr nm = { .name = NULL }; struct fscrypt_name nm = {0};
dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino, dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
dentry, size); dentry, size);
...@@ -419,15 +449,12 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size) ...@@ -419,15 +449,12 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
break; break;
} }
nm.name = xent->name; fname_name(&nm) = xent->name;
nm.len = le16_to_cpu(xent->nlen); fname_len(&nm) = le16_to_cpu(xent->nlen);
/* Show trusted namespace only for "power" users */ if (xattr_visible(xent->name)) {
if (strncmp(xent->name, XATTR_TRUSTED_PREFIX, memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
XATTR_TRUSTED_PREFIX_LEN) || written += fname_len(&nm) + 1;
capable(CAP_SYS_ADMIN)) {
memcpy(buffer + written, nm.name, nm.len + 1);
written += nm.len + 1;
} }
kfree(pxent); kfree(pxent);
...@@ -446,7 +473,7 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size) ...@@ -446,7 +473,7 @@ ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
} }
static int remove_xattr(struct ubifs_info *c, struct inode *host, static int remove_xattr(struct ubifs_info *c, struct inode *host,
struct inode *inode, const struct qstr *nm) struct inode *inode, const struct fscrypt_name *nm)
{ {
int err; int err;
struct ubifs_inode *host_ui = ubifs_inode(host); struct ubifs_inode *host_ui = ubifs_inode(host);
...@@ -463,9 +490,9 @@ static int remove_xattr(struct ubifs_info *c, struct inode *host, ...@@ -463,9 +490,9 @@ static int remove_xattr(struct ubifs_info *c, struct inode *host,
mutex_lock(&host_ui->ui_mutex); mutex_lock(&host_ui->ui_mutex);
host->i_ctime = ubifs_current_time(host); host->i_ctime = ubifs_current_time(host);
host_ui->xattr_cnt -= 1; host_ui->xattr_cnt -= 1;
host_ui->xattr_size -= CALC_DENT_SIZE(nm->len); host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len); host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
host_ui->xattr_names -= nm->len; host_ui->xattr_names -= fname_len(nm);
err = ubifs_jnl_delete_xattr(c, host, inode, nm); err = ubifs_jnl_delete_xattr(c, host, inode, nm);
if (err) if (err)
...@@ -477,27 +504,27 @@ static int remove_xattr(struct ubifs_info *c, struct inode *host, ...@@ -477,27 +504,27 @@ static int remove_xattr(struct ubifs_info *c, struct inode *host,
out_cancel: out_cancel:
host_ui->xattr_cnt += 1; host_ui->xattr_cnt += 1;
host_ui->xattr_size += CALC_DENT_SIZE(nm->len); host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len); host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
host_ui->xattr_names += nm->len; host_ui->xattr_names += fname_len(nm);
mutex_unlock(&host_ui->ui_mutex); mutex_unlock(&host_ui->ui_mutex);
ubifs_release_budget(c, &req); ubifs_release_budget(c, &req);
make_bad_inode(inode); make_bad_inode(inode);
return err; return err;
} }
static int __ubifs_removexattr(struct inode *host, const char *name) static int ubifs_xattr_remove(struct inode *host, const char *name)
{ {
struct inode *inode; struct inode *inode;
struct ubifs_info *c = host->i_sb->s_fs_info; struct ubifs_info *c = host->i_sb->s_fs_info;
struct qstr nm = QSTR_INIT(name, strlen(name)); struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
struct ubifs_dent_node *xent; struct ubifs_dent_node *xent;
union ubifs_key key; union ubifs_key key;
int err; int err;
ubifs_assert(inode_is_locked(host)); ubifs_assert(inode_is_locked(host));
if (nm.len > UBIFS_MAX_NLEN) if (fname_len(&nm) > UBIFS_MAX_NLEN)
return -ENAMETOOLONG; return -ENAMETOOLONG;
xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS); xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
...@@ -548,7 +575,8 @@ static int init_xattrs(struct inode *inode, const struct xattr *xattr_array, ...@@ -548,7 +575,8 @@ static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
} }
strcpy(name, XATTR_SECURITY_PREFIX); strcpy(name, XATTR_SECURITY_PREFIX);
strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
err = __ubifs_setxattr(inode, name, xattr->value, xattr->value_len, 0); err = ubifs_xattr_set(inode, name, xattr->value,
xattr->value_len, 0);
kfree(name); kfree(name);
if (err < 0) if (err < 0)
break; break;
...@@ -572,7 +600,7 @@ int ubifs_init_security(struct inode *dentry, struct inode *inode, ...@@ -572,7 +600,7 @@ int ubifs_init_security(struct inode *dentry, struct inode *inode,
return err; return err;
} }
static int ubifs_xattr_get(const struct xattr_handler *handler, static int xattr_get(const struct xattr_handler *handler,
struct dentry *dentry, struct inode *inode, struct dentry *dentry, struct inode *inode,
const char *name, void *buffer, size_t size) const char *name, void *buffer, size_t size)
{ {
...@@ -580,10 +608,10 @@ static int ubifs_xattr_get(const struct xattr_handler *handler, ...@@ -580,10 +608,10 @@ static int ubifs_xattr_get(const struct xattr_handler *handler,
inode->i_ino, dentry, size); inode->i_ino, dentry, size);
name = xattr_full_name(handler, name); name = xattr_full_name(handler, name);
return __ubifs_getxattr(inode, name, buffer, size); return ubifs_xattr_get(inode, name, buffer, size);
} }
static int ubifs_xattr_set(const struct xattr_handler *handler, static int xattr_set(const struct xattr_handler *handler,
struct dentry *dentry, struct inode *inode, struct dentry *dentry, struct inode *inode,
const char *name, const void *value, const char *name, const void *value,
size_t size, int flags) size_t size, int flags)
...@@ -594,27 +622,27 @@ static int ubifs_xattr_set(const struct xattr_handler *handler, ...@@ -594,27 +622,27 @@ static int ubifs_xattr_set(const struct xattr_handler *handler,
name = xattr_full_name(handler, name); name = xattr_full_name(handler, name);
if (value) if (value)
return __ubifs_setxattr(inode, name, value, size, flags); return ubifs_xattr_set(inode, name, value, size, flags);
else else
return __ubifs_removexattr(inode, name); return ubifs_xattr_remove(inode, name);
} }
static const struct xattr_handler ubifs_user_xattr_handler = { static const struct xattr_handler ubifs_user_xattr_handler = {
.prefix = XATTR_USER_PREFIX, .prefix = XATTR_USER_PREFIX,
.get = ubifs_xattr_get, .get = xattr_get,
.set = ubifs_xattr_set, .set = xattr_set,
}; };
static const struct xattr_handler ubifs_trusted_xattr_handler = { static const struct xattr_handler ubifs_trusted_xattr_handler = {
.prefix = XATTR_TRUSTED_PREFIX, .prefix = XATTR_TRUSTED_PREFIX,
.get = ubifs_xattr_get, .get = xattr_get,
.set = ubifs_xattr_set, .set = xattr_set,
}; };
static const struct xattr_handler ubifs_security_xattr_handler = { static const struct xattr_handler ubifs_security_xattr_handler = {
.prefix = XATTR_SECURITY_PREFIX, .prefix = XATTR_SECURITY_PREFIX,
.get = ubifs_xattr_get, .get = xattr_get,
.set = ubifs_xattr_set, .set = xattr_set,
}; };
const struct xattr_handler *ubifs_xattr_handlers[] = { const struct xattr_handler *ubifs_xattr_handlers[] = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment