btrfs: add fscrypt support, PART 1
v7 cover btrfs: add fscrypt support, PART 1
This is a revive of former work [1] of Omar, Sweet Tea and Josef to bring native encryption support to btrfs.
It will come in more parts. The first part this time is splitting the simple and isolated stuff out first to reduce the size of the final patchset.
Changes:
* v7 - Drop the checksum patch for now. It will make more sense later.
- Drop the btrfs/330 fix. It seems no longer needed after the years.
* v6 vs v5 [1] is mostly rebase to the latest for-next and cleaning up the conflicts.
The remaining part needs further cleanup and a bit of redesign and it will follow later.
[1] https://lore.kernel.org/linux-btrfs/[email protected]/
Josef Bacik (4):
btrfs: add orig_logical to btrfs_bio
btrfs: don't rewrite ret from inode_permission
btrfs: move inode_to_path higher in backref.c
btrfs: don't search back for dir inode item in INO_LOOKUP_USER
Omar Sandoval (1):
btrfs: disable various operations on encrypted inodes
Sweet Tea Dorminy (1):
btrfs: disable verity on encrypted inodes
fs/btrfs/backref.c | 68 +++++++++++++++++++++----------------------- fs/btrfs/bio.c | 10 +++++++ fs/btrfs/bio.h | 2 ++ fs/btrfs/file-item.c | 2 +- fs/btrfs/inode.c | 4 +++ fs/btrfs/ioctl.c | 27 +++--------------- fs/btrfs/reflink.c | 7 +++++ fs/btrfs/verity.c | 3 ++ 8 files changed, 63 insertions(+), 60 deletions(-)
Signature · 1 line
2.51.0
PATCH v7 1/6 btrfs: disable various operations on encrypted inodes
From: Omar Sandoval <[email protected]>
Initially, only normal data extents will be encrypted. This change forbids various other bits:
- allows reflinking only if both inodes have the same encryption status
- disable inline data on encrypted inodes
--
v5 was 'Reviewed-by: Boris Burkov <[email protected]>' but the rebase changed the code a bit so dropping.
https://lore.kernel.org/linux-btrfs/[email protected]/
--- fs/btrfs/inode.c | 4 ++++ fs/btrfs/reflink.c | 7 +++++++ 2 files changed, 11 insertions(+)
fs/btrfs/inode.c +4 -0
@@ -592,6 +592,10 @@ static bool can_cow_file_range_inline(struct btrfs_inode *inode,if (size <i_size_read (&inode->vfs_inode))return false; +/* Encrypted file cannot be inlined. */ +if (IS_ENCRYPTED (&inode->vfs_inode)) +return false; +return true; }
fs/btrfs/reflink.c +7 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/blkdev.h> +#include <linux/fscrypt.h> #include <linux/iversion.h> #include "ctree.h" #include "fs.h"
@@ -789,6 +790,12 @@ static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,ASSERT (inode_in->vfs_inode.i_sb == inode_out->vfs_inode.i_sb); } +/* +* Can only reflink encrypted files if both files are encrypted. +*/ +if (IS_ENCRYPTED (&inode_in->vfs_inode) !=IS_ENCRYPTED (&inode_out->vfs_inode)) +return -EINVAL ; +/* Don't make the dst file partly checksummed */ if ((inode_in->flags &BTRFS_INODE_NODATASUM ) != (inode_out->flags &BTRFS_INODE_NODATASUM )) {
Signature · 1 line
2.51.0
PATCH v7 2/6 btrfs: disable verity on encrypted inodes
In reply to Daniel VacekFrom: Sweet Tea Dorminy <[email protected]>
Right now there isn't a way to encrypt things that aren't either filenames in directories or data on blocks on disk with extent encryption, so for now, disable verity usage with encryption on btrfs.
fscrypt with fsverity should be possible and it can be implemented in the future.
--- fs/btrfs/verity.c | 3 +++ 1 file changed, 3 insertions(+)
fs/btrfs/verity.c +3 -0
@@ -578,6 +578,9 @@ static int btrfs_begin_enable_verity(struct file *filp)btrfs_assert_inode_locked (inode); +if (IS_ENCRYPTED (&inode->vfs_inode)) +return -EOPNOTSUPP ; +if (test_bit (BTRFS_INODE_VERITY_IN_PROGRESS , &inode->runtime_flags))return -EBUSY ;
Signature · 1 line
2.51.0
PATCH v7 3/6 btrfs: add orig_logical to btrfs_bio
In reply to Daniel VacekFrom: Josef Bacik <[email protected]>
When checksumming the encrypted bio on writes we need to know which logical address this checksum is for. At the point where we get the encrypted bio the bi_sector is the physical location on the target disk, so we need to save the original logical offset in the btrfs_bio. Then we can use this when csum'ing the bio instead of the bio->iter.bi_sector.
--- fs/btrfs/bio.c | 10 ++++++++++ fs/btrfs/bio.h | 2 ++ fs/btrfs/file-item.c | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-)
fs/btrfs/bio.c +10 -0
@@ -94,6 +94,8 @@ static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info,if (bbio_has_ordered_extent (bbio)) {refcount_inc (&orig_bbio->ordered->refs); bbio->ordered = orig_bbio->ordered; + bbio->orig_logical = orig_bbio->orig_logical; + orig_bbio->orig_logical += map_length; } bbio->csum_search_commit_root = orig_bbio->csum_search_commit_root;atomic_inc (&orig_bbio->pending_ios);
@@ -765,6 +767,14 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) goto end_bbio; } +/* +* For fscrypt writes we will get the encrypted bio after we've +* remapped our bio to the physical disk location, so we need to +* save the original bytenr so we know what we're checksumming. +*/ +if (bio_op (bio) ==REQ_OP_WRITE &&is_data_bbio (bbio)) + bbio->orig_logical = logical; + map_length =min (map_length, length);if (use_append) map_length =btrfs_append_map_length (bbio, map_length);
fs/btrfs/bio.h +2 -0
@@ -52,6 +52,7 @@ struct btrfs_bio {* - pointer to the checksums for this bio * - original physical address from the allocator * (for zone append only) +* - original logical address, used for checksumming fscrypt bios. */ struct {struct btrfs_ordered_extent *ordered;
@@ -60,6 +61,7 @@ struct btrfs_bio {struct completion csum_done;struct bvec_iter csum_saved_iter;u64 orig_physical; +u64 orig_logical; };/* For metadata reads: parentness verification. */
fs/btrfs/file-item.c +1 -1
@@ -824,7 +824,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)if (!sums)return -ENOMEM ; - sums->logical = bio->bi_iter.bi_sector <<SECTOR_SHIFT ; + sums->logical = bbio->orig_logical; sums->len = bio->bi_iter.bi_size;INIT_LIST_HEAD (&sums->list); bbio->sums = sums;
Signature · 1 line
2.51.0
PATCH v7 4/6 btrfs: don't rewrite ret from inode_permission
In reply to Daniel VacekFrom: Josef Bacik <[email protected]>
In our user safe ino resolve ioctl we'll just turn any ret into -EACCES from inode_permission. This is redundant, and could potentially be wrong if we had an ENOMEM in the security layer or some such other error, so simply return the actual return value.
--- fs/btrfs/ioctl.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/ioctl.c +1 -3
@@ -1910,10 +1910,8 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap, ret =inode_permission (idmap, &temp_inode->vfs_inode,MAY_READ |MAY_EXEC );iput (&temp_inode->vfs_inode); -if (ret) { - ret = -EACCES ; +if (ret) goto out_put; - }if (key.offset == upper_limit)break ;
Signature · 1 line
2.51.0
PATCH v7 5/6 btrfs: move inode_to_path higher in backref.c
In reply to Daniel VacekFrom: Josef Bacik <[email protected]>
We have a prototype and then the definition lower below, we don't need to do this, simply move the function to where the prototype is.
--- fs/btrfs/backref.c | 68 ++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 36 deletions(-)
fs/btrfs/backref.c +32 -36
@@ -2574,8 +2574,39 @@ int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,return iterate_extent_inodes (&walk_ctx, false, build_ino_list, ctx); } +/* +* returns 0 if the path could be dumped (probably truncated) +* returns <0 in case of an error +*/ static int inode_to_path (u64 inum,u32 name_len,unsigned long name_off, -struct extent_buffer *eb,struct inode_fs_paths *ipath); +struct extent_buffer *eb,struct inode_fs_paths *ipath) +{ +char *fspath; +char *fspath_min; +int i = ipath->fspath->elem_cnt; +const int s_ptr =sizeof (char *); +u32 bytes_left; + + bytes_left = ipath->fspath->bytes_left > s_ptr ? ipath->fspath->bytes_left - s_ptr :0 ; + + fspath_min = (char *)ipath->fspath->val + (i +1 ) * s_ptr; + fspath =btrfs_ref_to_path (ipath->fs_root, ipath->btrfs_path, name_len, + name_off, eb, inum, fspath_min, bytes_left); +if (IS_ERR (fspath)) +return PTR_ERR (fspath); + +if (fspath > fspath_min) { + ipath->fspath->val[i] = (u64 )(unsigned long )fspath; + ++ipath->fspath->elem_cnt; + ipath->fspath->bytes_left = fspath - fspath_min; + }else { + ++ipath->fspath->elem_missed; + ipath->fspath->bytes_missing += fspath_min - fspath; + ipath->fspath->bytes_left =0 ; + } + +return 0 ; +}static int iterate_inode_refs (u64 inum,struct inode_fs_paths *ipath) {
@@ -2700,41 +2731,6 @@ static int iterate_inode_extrefs(u64 inum, struct inode_fs_paths *ipath)return ret; } -/* -* returns 0 if the path could be dumped (probably truncated) -* returns <0 in case of an error -*/ -static int inode_to_path (u64 inum,u32 name_len,unsigned long name_off, -struct extent_buffer *eb,struct inode_fs_paths *ipath) -{ -char *fspath; -char *fspath_min; -int i = ipath->fspath->elem_cnt; -const int s_ptr =sizeof (char *); -u32 bytes_left; - - bytes_left = ipath->fspath->bytes_left > s_ptr ? - ipath->fspath->bytes_left - s_ptr :0 ; - - fspath_min = (char *)ipath->fspath->val + (i +1 ) * s_ptr; - fspath =btrfs_ref_to_path (ipath->fs_root, ipath->btrfs_path, name_len, - name_off, eb, inum, fspath_min, bytes_left); -if (IS_ERR (fspath)) -return PTR_ERR (fspath); - -if (fspath > fspath_min) { - ipath->fspath->val[i] = (u64 )(unsigned long )fspath; - ++ipath->fspath->elem_cnt; - ipath->fspath->bytes_left = fspath - fspath_min; - }else { - ++ipath->fspath->elem_missed; - ipath->fspath->bytes_missing += fspath_min - fspath; - ipath->fspath->bytes_left =0 ; - } - -return 0 ; -} -/* * this dumps all file system paths to the inode into the ipath struct, provided * is has been created large enough. each path is zero-terminated and accessed
Signature · 1 line
2.51.0
PATCH v7 6/6 btrfs: don't search back for dir inode item in INO_LOOKUP_USER
In reply to Daniel VacekFrom: Josef Bacik <[email protected]>
We don't need to search back to the inode item, the directory inode number is in key.offset, so simply use that. If we can't find the directory we'll get an ENOENT at the iget.
--- fs/btrfs/ioctl.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-)
fs/btrfs/ioctl.c +3 -20
@@ -1822,7 +1822,7 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,struct btrfs_root_ref *rref;struct btrfs_root *root =NULL ;struct btrfs_path *path; -struct btrfs_key key, key2; +struct btrfs_key key;struct extent_buffer *leaf;char *ptr;int slot;
@@ -1877,24 +1877,6 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,read_extent_buffer (leaf, ptr, (unsigned long )(iref +1 ), len); -/* Check the read+exec permission of this directory */ - ret =btrfs_previous_item (root, path, dirid, -BTRFS_INODE_ITEM_KEY ); -if (ret <0 ) { - goto out_put; - }else if (ret >0 ) { - ret = -ENOENT ; - goto out_put; - } - - leaf = path->nodes[0 ]; - slot = path->slots[0 ]; -btrfs_item_key_to_cpu (leaf, &key2, slot); -if (key2.objectid != dirid) { - ret = -ENOENT ; - goto out_put; - } -/* * We don't need the path anymore, so release it and * avoid deadlocks and lockdep warnings in case
@@ -1902,11 +1884,12 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,* btree and lock the same leaf. */ btrfs_release_path (path); - temp_inode =btrfs_iget (key2.objectid, root); + temp_inode =btrfs_iget (key.offset, root);if (IS_ERR (temp_inode)) { ret =PTR_ERR (temp_inode); goto out_put; } +/* Check the read+exec permission of this directory */ ret =inode_permission (idmap, &temp_inode->vfs_inode,MAY_READ |MAY_EXEC );iput (&temp_inode->vfs_inode);
Signature · 1 line
2.51.0
Please, disregard this v7. I misconfigured my email. I'm sorry about the noise.
--nX
Quoted 43 lines · Daniel Vacek
This is a revive of former work [1] of Omar, Sweet Tea and Josef to bring native encryption support to btrfs.
It will come in more parts. The first part this time is splitting the simple and isolated stuff out first to reduce the size of the final patchset.
Changes:
* v7 - Drop the checksum patch for now. It will make more sense later.
- Drop the btrfs/330 fix. It seems no longer needed after the years.
* v6 vs v5 [1] is mostly rebase to the latest for-next and cleaning up the conflicts.The remaining part needs further cleanup and a bit of redesign and it will follow later.
[1] https://lore.kernel.org/linux-btrfs/[email protected]/
Josef Bacik (4):
btrfs: addorig_logicaltobtrfs_bio
btrfs: don't rewrite ret frominode_permission
btrfs: moveinode_to_pathhigher inbackref.c
btrfs: don't search back for dir inode item inINO_LOOKUP_USEROmar Sandoval (1):
btrfs: disable various operations on encrypted inodesSweet Tea Dorminy (1):
btrfs: disable verity on encrypted inodesfs/btrfs/backref.c | 68 +++++++++++++++++++++----------------------- fs/btrfs/bio.c | 10 +++++++ fs/btrfs/bio.h | 2 ++ fs/btrfs/file-item.c | 2 +- fs/btrfs/inode.c | 4 +++ fs/btrfs/ioctl.c | 27 +++--------------- fs/btrfs/reflink.c | 7 +++++ fs/btrfs/verity.c | 3 ++ 8 files changed, 63 insertions(+), 60 deletions(-)--
2.51.0