[PATCH v4 42/46] btrfs: make btrfs_ref_to_path handle encrypted filenames

Josef Bacik
2023-12-01T22:12:50Z

We use this helper for inode-resolve and path resolution in send, so
update this helper to properly decrypt any encrypted names it finds.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
fs/btrfs/backref.c | 45 +++++++++++++++++++++++++++++++++++++++++----
fs/btrfs/fscrypt.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
fs/btrfs/fscrypt.h | 11 +++++++++++
3 files changed, 98 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index f58fe7c745c2..9ed854b9f3fc 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -20,6 +20,7 @@
#include "extent-tree.h"
#include "relocation.h"
#include "tree-checker.h"
+#include "fscrypt.h"

/* Just arbitrary numbers so we can be sure one of these happened. */
#define BACKREF_FOUND_SHARED 6
@@ -2117,6 +2118,42 @@ int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
return ret;
}

+static int copy_resolved_iref_to_buf(struct btrfs_root *fs_root,
+ struct extent_buffer *eb,
+ char *dest, u64 parent,
+ unsigned long name_off, u32 name_len,
+ s64 *bytes_left)
+{
+ struct btrfs_fs_info *fs_info = fs_root->fs_info;
+ struct fscrypt_str fname = FSTR_INIT(NULL, 0);
+ int ret;
+
+ /* No encryption, just copy the name in. */
+ if (!btrfs_fs_incompat(fs_info, ENCRYPT)) {
+ *bytes_left -= name_len;
+ if (*bytes_left >= 0)
+ read_extent_buffer(eb, dest + *bytes_left,
+ name_off, name_len);
+ return 0;
+ }
+
+ ret = fscrypt_fname_alloc_buffer(BTRFS_NAME_LEN, &fname);
+ if (ret)
+ return ret;
+
+ ret = btrfs_decrypt_name(fs_root, eb, name_off, name_len, parent,
+ &fname);
+ if (ret)
+ goto out;
+
+ *bytes_left -= fname.len;
+ if (*bytes_left >= 0)
+ memcpy(dest + *bytes_left, fname.name, fname.len);
+out:
+ fscrypt_fname_free_buffer(&fname);
+ return ret;
+}
+
/*
* this iterates to turn a name (from iref/extref) into a full filesystem path.
* Elements of the path are separated by '/' and the path is guaranteed to be
@@ -2148,10 +2185,10 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
dest[bytes_left] = '\0';

while (1) {
- bytes_left -= name_len;
- if (bytes_left >= 0)
- read_extent_buffer(eb, dest + bytes_left,
- name_off, name_len);
+ ret = copy_resolved_iref_to_buf(fs_root, eb, dest, parent,
+ name_off, name_len, &bytes_left);
+ if (ret)
+ break;
if (eb != eb_in) {
if (!path->skip_locking)
btrfs_tree_read_unlock(eb);
diff --git a/fs/btrfs/fscrypt.c b/fs/btrfs/fscrypt.c
index 2fbceec62dc7..6211fa17bf79 100644
--- a/fs/btrfs/fscrypt.c
+++ b/fs/btrfs/fscrypt.c
@@ -355,6 +355,52 @@ int btrfs_fscrypt_bio_length(struct bio *bio, u64 map_length)
return map_length;
}

+int btrfs_decrypt_name(struct btrfs_root *root, struct extent_buffer *eb,
+ unsigned long name_off, u32 name_len,
+ u64 parent_ino, struct fscrypt_str *name)
+{
+ struct btrfs_fs_info *fs_info = root->fs_info;
+ struct inode *dir;
+ struct fscrypt_str iname = FSTR_INIT(NULL, 0);
+ int ret;
+
+ ASSERT(name_len <= BTRFS_NAME_LEN);
+
+ ret = fscrypt_fname_alloc_buffer(name_len, &iname);
+ if (ret)
+ return ret;
+
+ dir = btrfs_iget(fs_info->sb, parent_ino, root);
+ if (IS_ERR(dir)) {
+ ret = PTR_ERR(dir);
+ goto out;
+ }
+
+ /*
+ * Directory isn't encrypted, the name isn't encrypted, we can just copy
+ * it into the buffer.
+ */
+ if (!IS_ENCRYPTED(dir)) {
+ read_extent_buffer(eb, name->name, name_off, name_len);
+ name->len = name_len;
+ goto out_inode;
+ }
+
+ read_extent_buffer(eb, iname.name, name_off, name_len);
+
+ ret = fscrypt_prepare_readdir(dir);
+ if (ret)
+ goto out_inode;
+
+ ASSERT(dir->i_crypt_info);
+ ret = fscrypt_fname_disk_to_usr(dir, 0, 0, &iname, name);
+out_inode:
+ iput(dir);
+out:
+ fscrypt_fname_free_buffer(&iname);
+ return ret;
+}
+
const struct fscrypt_operations btrfs_fscrypt_ops = {
.has_per_extent_encryption = 1,
.get_context = btrfs_fscrypt_get_context,
diff --git a/fs/btrfs/fscrypt.h b/fs/btrfs/fscrypt.h
index c4a327be0eeb..aba01bacc165 100644
--- a/fs/btrfs/fscrypt.h
+++ b/fs/btrfs/fscrypt.h
@@ -36,6 +36,9 @@ bool btrfs_mergeable_encrypted_bio(struct bio *bio, struct inode *inode,
struct fscrypt_extent_info *fi,
u64 logical_offset);
int btrfs_fscrypt_bio_length(struct bio *bio, u64 map_length);
+int btrfs_decrypt_name(struct btrfs_root *root, struct extent_buffer *eb,
+ unsigned long name_off, u32 name_len,
+ u64 parent_ino, struct fscrypt_str *name);

#else
static inline int btrfs_fscrypt_save_extent_info(struct btrfs_inode *inode,
@@ -94,6 +97,14 @@ static inline u64 btrfs_fscrypt_bio_length(struct bio *bio, u64 map_length)
{
return map_length;
}
+
+static inline int btrfs_decrypt_name(struct btrfs_root *root,
+ struct extent_buffer *eb,
+ unsigned long name_off, u32 name_len,
+ u64 parent_ino, struct fscrypt_str *name)
+{
+ return -EINVAL;
+}
#endif /* CONFIG_FS_ENCRYPTION */

extern const struct fscrypt_operations btrfs_fscrypt_ops;
--
2.41.0