Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 191db760 authored by Jaegeuk Kim's avatar Jaegeuk Kim Committed by Barani Muthukumaran
Browse files

dm-default-key, f2fs, ICE: support dm-default-key with f2fs/ICE



This patch fixes assigning bi_crypt_key for moving data which was previously
encrypted by f2fs.

Note that, dm-default-key should not assign bi_crypt_key, if bi_crypt_skip is
set.

The sceanrios is:

1. write data with user key by f2fs
  -  ENC(KU, IVU, DATA)
2. log out user key
3. read data #1 w/o user key from LBA #a
4. dm-default-key assigns default key
  - DEC(KD, LBA#a, ENC(KU, IVU, DATA))
5. write data #1 w/o user key into LBA #b
6. dm-default-key assigns default key
  - ENC(KD, LBA#b, DEC(KD, LBA#a, ENC(KU, IVU, DATA)))
7. Read DATA out with valid logged-in user key
  - DEC(KU, IVU, ENC(KD, LBA#b, DEC(KD, LBA#a, ENC(KU, IVU, DATA))))

So, this patch introduces bi_crypt_skip to avoid 4. ~ 6 with right flow:
1. write data with user key by f2fs
  -  ENC(KU, IVU, DATA)
2. log out user key
3. read data #1 w/o user key from LBA #a
4. dm-default-key skip to assign default key
  - ENC(KU, IVU, DATA)
5. write data #1 w/o user key into LBA #b
6. dm-default-key skips to assign default key
  - ENC(KU, IVU, DATA)
7. Try to read DATA with valid logged-in user key
  - DEC(KU, IVU, ENC(KU, IVU, DATA))

Change-Id: I4a047ff0d019cd062bdf99e8e0b7cea243721fb7
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@google.com>
Signed-off-by: default avatarShivaprasad Hongal <shongal@codeaurora.org>
Signed-off-by: default avatarBarani Muthukumaran <bmuthuku@codeaurora.org>
parent 77931cca
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -583,8 +583,12 @@ EXPORT_SYMBOL(bio_phys_segments);
static inline void bio_clone_crypt_key(struct bio *dst, const struct bio *src)
{
#ifdef CONFIG_PFK
	dst->bi_crypt_key = src->bi_crypt_key;
	dst->bi_iter.bi_dun = src->bi_iter.bi_dun;
#ifdef CONFIG_DM_DEFAULT_KEY
	dst->bi_crypt_key = src->bi_crypt_key;
	dst->bi_crypt_skip = src->bi_crypt_skip;
#endif
	dst->bi_dio_inode = src->bi_dio_inode;
#endif
}

+14 −1
Original line number Diff line number Diff line
@@ -120,16 +120,29 @@ void fscrypt_set_ice_dun(const struct inode *inode, struct bio *bio, u64 dun)
}
EXPORT_SYMBOL(fscrypt_set_ice_dun);

void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip)
{
#ifdef CONFIG_DM_DEFAULT_KEY
	bio->bi_crypt_skip = bi_crypt_skip;
#endif
}
EXPORT_SYMBOL(fscrypt_set_ice_skip);

/*
 * This function will be used for filesystem when deciding to merge bios.
 * Basic assumption is, if inline_encryption is set, single bio has to
 * guarantee consecutive LBAs as well as ino|pg->index.
 */
bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted)
bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted,
						int bi_crypt_skip)
{
	if (!bio)
		return true;

#ifdef CONFIG_DM_DEFAULT_KEY
	if (bi_crypt_skip != bio->bi_crypt_skip)
		return false;
#endif
	/* if both of them are not encrypted, no further check is needed */
	if (!bio_dun(bio) && !bio_encrypted)
		return true;
+6 −2
Original line number Diff line number Diff line
@@ -496,6 +496,7 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)

	if (f2fs_may_encrypt_bio(inode, fio))
		fscrypt_set_ice_dun(inode, bio, PG_DUN(inode, fio->page));
	fscrypt_set_ice_skip(bio, fio->encrypted_page ? 1 : 0);

	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
		bio_put(bio);
@@ -518,6 +519,7 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
	struct page *bio_page;
	struct inode *inode;
	bool bio_encrypted;
	int bi_crypt_skip;
	u64 dun;

	f2fs_bug_on(sbi, is_read_io(fio->op));
@@ -543,6 +545,7 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
	bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
	inode = fio->page->mapping->host;
	dun = PG_DUN(inode, fio->page);
	bi_crypt_skip = fio->encrypted_page ? 1 : 0;
	bio_encrypted = f2fs_may_encrypt_bio(inode, fio);

	/* set submitted = true as a return value */
@@ -556,7 +559,7 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
		__submit_merged_bio(io);

	/* ICE support */
	if (!fscrypt_mergeable_bio(io->bio, dun, bio_encrypted))
	if (!fscrypt_mergeable_bio(io->bio, dun, bio_encrypted, bi_crypt_skip))
		__submit_merged_bio(io);

alloc_new:
@@ -572,6 +575,7 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
						fio->type, fio->temp);
		if (bio_encrypted)
			fscrypt_set_ice_dun(inode, io->bio, dun);
		fscrypt_set_ice_skip(io->bio, bi_crypt_skip);
		io->fio = *fio;
	}

@@ -1661,7 +1665,7 @@ static int f2fs_mpage_readpages(struct address_space *mapping,

		dun = PG_DUN(inode, page);
		bio_encrypted = f2fs_may_encrypt_bio(inode, NULL);
		if (!fscrypt_mergeable_bio(bio, dun, bio_encrypted)) {
		if (!fscrypt_mergeable_bio(bio, dun, bio_encrypted, 0)) {
			__submit_bio(F2FS_I_SB(inode), bio, DATA);
			bio = NULL;
		}
+6 −1
Original line number Diff line number Diff line
@@ -183,8 +183,13 @@ static inline int fscrypt_using_hardware_encryption(const struct inode *inode)
static inline void fscrypt_set_ice_dun(const struct inode *inode,
		struct bio *bio, u64 dun) {}

static inline void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip)
{
	return;
}

static inline bool fscrypt_mergeable_bio(struct bio *bio,
		sector_t iv_block, bool bio_encrypted)
		sector_t iv_block, bool bio_encrypted, int bi_crypt_skip)
{
	return true;
}
+3 −1
Original line number Diff line number Diff line
@@ -187,7 +187,9 @@ extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
extern int fscrypt_using_hardware_encryption(const struct inode *inode);
extern void fscrypt_set_ice_dun(const struct inode *inode,
		struct bio *bio, u64 dun);
extern bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted);
extern void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip);
extern bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted,
						int bi_crypt_skip);

/* hooks.c */
extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Loading