All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
To: Chris Mason <clm@fb.com>, Josef Bacik <josef@toxicpanda.com>,
	David Sterba <dsterba@suse.com>,
	"Theodore Y . Ts'o " <tytso@mit.edu>,
	Jaegeuk Kim <jaegeuk@kernel.org>,
	Eric Biggers <ebiggers@kernel.org>,
	linux-fscrypt@vger.kernel.org, linux-btrfs@vger.kernel.org,
	kernel-team@fb.com
Cc: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Subject: [PATCH 04/21] fscrypt: add a function for a filesystem to generate an IV
Date: Wed, 17 Aug 2022 10:49:48 -0400	[thread overview]
Message-ID: <40f17d7f64a80e0d2746972b4f6b4b5831d4f455.1660744500.git.sweettea-kernel@dorminy.me> (raw)
In-Reply-To: <cover.1660744500.git.sweettea-kernel@dorminy.me>

Unlike other filesystems, which store all necessary encryption context
in the per-inode fscrypt context, btrfs will need to store an IV per
extent in order to support snapshots and reflinks. To avoid exposing the
internal details of extents to fscrypt, and to centralize IV generation
in fscrypt, this change provides fscrypt_generate_random_iv(), which
will be called for each newly created btrfs extent and will populate a
btrfs-provided buffer with a new IV. Additionally, a function to get
the necessary buffer size, fscrypt_mode_ivsize(), is also necessary.

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 fs/crypto/crypto.c      | 26 ++++++++++++++++++++++++++
 include/linux/fscrypt.h |  3 +++
 2 files changed, 29 insertions(+)

diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c
index e78be66bbf01..e0e30d64837e 100644
--- a/fs/crypto/crypto.c
+++ b/fs/crypto/crypto.c
@@ -23,6 +23,7 @@
 #include <linux/pagemap.h>
 #include <linux/mempool.h>
 #include <linux/module.h>
+#include <linux/random.h>
 #include <linux/scatterlist.h>
 #include <linux/ratelimit.h>
 #include <crypto/skcipher.h>
@@ -69,6 +70,31 @@ void fscrypt_free_bounce_page(struct page *bounce_page)
 }
 EXPORT_SYMBOL(fscrypt_free_bounce_page);
 
+int fscrypt_mode_ivsize(struct inode *inode)
+{
+	struct fscrypt_info *ci;
+
+	if (!fscrypt_needs_contents_encryption(inode))
+		return 0;
+
+	ci = inode->i_crypt_info;
+	if (WARN_ON_ONCE(!ci))
+		return 0;
+	return ci->ci_mode->ivsize;
+}
+EXPORT_SYMBOL(fscrypt_mode_ivsize);
+
+/**
+ * fscrypt_generate_random_iv() - initialize a new iv for an IV_FROM_FS filesystem
+ * @inode: the inode to which the new IV will belong
+ * @iv: an output buffer, long enough for the requisite IV
+ */
+void fscrypt_generate_random_iv(struct inode *inode, u8 *iv)
+{
+	get_random_bytes(iv, fscrypt_mode_ivsize(inode));
+}
+EXPORT_SYMBOL(fscrypt_generate_random_iv);
+
 /*
  * Generate the IV for the given logical block number within the given file.
  * For filenames encryption, lblk_num == 0.
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 1686b25f6d9c..ff572f8a88f8 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -317,6 +317,9 @@ static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
 
 void fscrypt_free_bounce_page(struct page *bounce_page);
 
+int fscrypt_mode_ivsize(struct inode *inode);
+void fscrypt_generate_random_iv(struct inode *inode, u8 *iv);
+
 /* policy.c */
 int fscrypt_have_same_policy(struct inode *inode1, struct inode *inode2);
 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
-- 
2.35.1


  parent reply	other threads:[~2022-08-17 14:50 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-17 14:49 [PATCH 00/21] btrfs: add fscrypt integration Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 01/21] fscrypt: expose fscrypt_nokey_name Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 02/21] fscrypt: add flag allowing partially-encrypted directories Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 03/21] fscrypt: add fscrypt_have_same_policy() to check inode's compatibility Sweet Tea Dorminy
2022-08-17 14:49 ` Sweet Tea Dorminy [this message]
2022-08-17 14:49 ` [PATCH 05/21] fscrypt: add new encryption policy for btrfs Sweet Tea Dorminy
2022-08-17 15:54   ` Sweet Tea Dorminy
2022-08-18 22:07     ` Eric Biggers
2022-08-19  0:22       ` Sweet Tea Dorminy
2022-08-20  0:50         ` Eric Biggers
2022-08-17 14:49 ` [PATCH 06/21] btrfs: store directorys' encryption state Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 07/21] btrfs: use fscrypt_name's instead of name/len everywhere Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 08/21] btrfs: setup fscrypt_names from dentrys using helper Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 09/21] btrfs: factor a fscrypt_name matching method Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 10/21] btrfs: disable various operations on encrypted inodes Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 11/21] btrfs: add fscrypt operation table to superblock Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 12/21] btrfs: start using fscrypt hooks Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 13/21] btrfs: add fscrypt_context items Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 14/21] btrfs: translate btrfs encryption flags and encrypted inode flag Sweet Tea Dorminy
2022-08-17 14:49 ` [PATCH 15/21] btrfs: add iv generation function for fscrypt Sweet Tea Dorminy
2022-08-17 14:50 ` [PATCH 16/21] btrfs: store an IV per encrypted normal file extent Sweet Tea Dorminy
2022-08-17 14:50 ` [PATCH 17/21] btrfs: Add new FEATURE_INCOMPAT_FSCRYPT feature flag Sweet Tea Dorminy
2022-08-17 14:50 ` [PATCH 18/21] btrfs: reuse encrypted filename hash when possible Sweet Tea Dorminy
2022-08-17 14:50 ` [PATCH 19/21] btrfs: adapt directory read and lookup to potentially encrypted filenames Sweet Tea Dorminy
2022-08-17 14:50 ` [PATCH 20/21] btrfs: encrypt normal file extent data if appropriate Sweet Tea Dorminy
2022-08-17 14:50 ` [PATCH 21/21] btrfs: implement fscrypt ioctls Sweet Tea Dorminy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=40f17d7f64a80e0d2746972b4f6b4b5831d4f455.1660744500.git.sweettea-kernel@dorminy.me \
    --to=sweettea-kernel@dorminy.me \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=ebiggers@kernel.org \
    --cc=jaegeuk@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fscrypt@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.