btrfs: add fscrypt support, PART 1
v8 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:
* v8 - Clean my mistakenly added Signed-off-by:
* 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
Re: [PATCH v7 4/6] btrfs: don't rewrite ret from inode_permission
This patch on it's own looks reasonable and IMHO should be merged ASAP to not overwrite the inode_permisison() return value.
I think also a
would be appropriate
Re: [PATCH v7 4/6] btrfs: don't rewrite ret from inode_permission
Quoted 12 lines · Johannes Thumshirn
This patch on it's own looks reasonable and IMHO should be merged ASAP to not overwrite the
inode_permisison()return value.I think also a
Fixes 23d0b79dfaed ("btrfs: Add unprivileged version of ino_lookup ioctl")would be appropriate
Reviewed-by Johannes Thumshirn <[email protected]>
Forgot to add, this needs your Signed-off-by as well as you've rebased it. Same for the other patches.
Re: [PATCH v7 4/6] btrfs: don't rewrite ret from inode_permission
Quoted 11 lines · Johannes Thumshirn
Quoted 10 lines · Johannes Thumshirn
This patch on it's own looks reasonable and IMHO should be merged ASAP to not overwrite the
inode_permisison()return value.I think also a
Fixes 23d0b79dfaed ("btrfs: Add unprivileged version of ino_lookup ioctl")would be appropriate
Reviewed-by Johannes Thumshirn <[email protected]>
Yes this seems to have effects on user space, though hopefully nothing really bad as the error codes from the permission checks is EACCESS or EPERM, other than the ENOMEM and similar.
Quoted 2 lines
Forgot to add, this needs your Signed-off-by as well as you've rebased it. Same for the other patches.
I'll add it, the final list will be the original author, Daniel as he touched it last and mine. The code hasn't evolved over the revisions so adding everybody who submitted it at some point does not seem adequate. Still I will also add a text to the changelog linking to the latest v5 series and mentioning people who handled the series in the past to give credit.
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
Re: [PATCH v7 5/6] btrfs: move inode_to_path higher in backref.c
Looks good,
Re: [PATCH v7 5/6] btrfs: move inode_to_path higher in backref.c
In reply to Daniel VacekQuoted 5 lines · Daniel Vacek
From: 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.
According to our development notes here:
https://btrfs.readthedocs.io/en/latest/dev/Development-notes.html
Under the "Function declarations" section we have:
"avoid prototypes for static functions, order them in new code in a way that does not need it
but don’t move static functions just to get rid of the prototype"
So what's the motivation for moving the function?
Quoted 97 lines
Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/backref.c | 68 ++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 36 deletions(-)fs/btrfs/backref.c +32
-37@@ -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 -- 2.51.0
Re: [PATCH v7 5/6] btrfs: move inode_to_path higher in backref.c
Quoted 11 lines · Filipe Manana
Quoted 5 lines · Daniel Vacek
From: 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.
According to our development notes here:
https://btrfs.readthedocs.io/en/latest/dev/Development-notes.html
Quoted 8 lines
Under the "Function declarations" section we have:
"avoid prototypes for static functions, order them in new code in a way that does not need it
but don’t move static functions just to get rid of the prototype"
So what's the motivation for moving the function?
Right, I thought the other fscrypt patches needed that for some reason but after checking again they don't, I'll drop the patch. Thanks.
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
Re: [PATCH v7 6/6] btrfs: don't search back for dir inode item in INO_LOOKUP_USER
fs/btrfs/ioctl.c static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap, · line 1892
Quoted 1 line · Daniel Vacek
fs/btrfs/ioctl.c +1
-0+/* Check the read+exec permission of this directory */
I'd put this hunk into 4/6.
and needs your SoB as well.
Otherwise,
Patches 1 to 3 just add dead code without the actual fscrypt support, which you've not even posted anywhere never mind having queued it up. Please don't add this kind of code without the user in the same series.
Quoted 3 lines · Christoph Hellwig
Patches 1 to 3 just add dead code without the actual fscrypt support, which you've not even posted anywhere never mind having queued it up. Please don't add this kind of code without the user in the same series.
The fscrypt series is about 50 patches, last v5 iteration is at [1] and I suggested to pick any independent patches we could ahead of time.
Quoted 7 lines · David Sterba
Quoted 3 lines · Christoph Hellwig
Patches 1 to 3 just add dead code without the actual fscrypt support, which you've not even posted anywhere never mind having queued it up. Please don't add this kind of code without the user in the same series.
The fscrypt series is about 50 patches, last v5 iteration is at [1] and I suggested to pick any independent patches we could ahead of time.
Getting any independent work in first is always a good idea. Patches 4-6 fit that, but patches 1-3 are everything but.
Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · David Sterba
Quoted 3 lines · Christoph Hellwig
Patches 1 to 3 just add dead code without the actual fscrypt support, which you've not even posted anywhere never mind having queued it up. Please don't add this kind of code without the user in the same series.
The fscrypt series is about 50 patches, last v5 iteration is at [1] and I suggested to pick any independent patches we could ahead of time.
Getting any independent work in first is always a good idea. Patches 4-6 fit that, but patches 1-3 are everything but.
We already have fscrypt preparations code that is not yet used, this is not different.
Quoted 13 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:
* v8 - Clean my mistakenly added Signed-off-by:
* 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.
Quoted 2 lines
The remaining part needs further cleanup and a bit of redesign and it will follow later.
Thanks, I've added it to for-next with the following note:
Note: The patch was taken from v5 of fscrypt patchset
(https://lore.kernel.org/linux-btrfs/[email protected]/)
which was handled over time by various people: Omar Sandoval, Sweet Tea Dorminy, Josef Bacik.
And added your signed-off as you're submitting it.
Quoted 17 lines · David Sterba
Quoted 13 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:
* v8 - Clean my mistakenly added Signed-off-by:
* 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.Quoted 2 lines
The remaining part needs further cleanup and a bit of redesign and it will follow later.
Quoted 8 lines
Thanks, I've added it to for-next with the following note:
Note: The patch was taken from v5 of fscrypt patchset
(https://lore.kernel.org/linux-btrfs/[email protected]/)
which was handled over time by various people: Omar Sandoval, Sweet Tea Dorminy, Josef Bacik.And added your signed-off as you're submitting it.
Thanks. I'll do the same with the rest of the series.