Skip to content

Commit fab48fe

Browse files
Jaegeuk KimGerrit - the friendly Code Review server
authored andcommitted
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 issue 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)) Issue: 68721442 Change-Id: Icefe85f608b7c3c84beb2bfa4267efd0f3787453 Signed-off-by: Jaegeuk Kim <[email protected]> Signed-off-by: Shivaprasad Hongal <[email protected]> [[email protected]: resolved merged conflicts, compilation issues.] Signed-off-by: Neeraj Soni <[email protected]>
1 parent 137abbe commit fab48fe

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

block/bio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,11 @@ EXPORT_SYMBOL(bio_phys_segments);
568568
static inline void bio_clone_crypt_key(struct bio *dst, const struct bio *src)
569569
{
570570
#ifdef CONFIG_PFK
571-
dst->bi_crypt_key = src->bi_crypt_key;
572571
dst->bi_iter.bi_dun = src->bi_iter.bi_dun;
572+
#ifdef CONFIG_DM_DEFAULT_KEY
573+
dst->bi_crypt_key = src->bi_crypt_key;
574+
dst->bi_crypt_skip = src->bi_crypt_skip;
575+
#endif
573576
dst->bi_dio_inode = src->bi_dio_inode;
574577
#endif
575578
}

fs/crypto/fscrypt_ice.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,29 @@ void fscrypt_set_ice_dun(const struct inode *inode, struct bio *bio, u64 dun)
126126
}
127127
EXPORT_SYMBOL(fscrypt_set_ice_dun);
128128

129+
void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip)
130+
{
131+
#ifdef CONFIG_DM_DEFAULT_KEY
132+
bio->bi_crypt_skip = bi_crypt_skip;
133+
#endif
134+
}
135+
EXPORT_SYMBOL(fscrypt_set_ice_skip);
136+
129137
/*
130138
* This function will be used for filesystem when deciding to merge bios.
131139
* Basic assumption is, if inline_encryption is set, single bio has to
132140
* guarantee consecutive LBAs as well as ino|pg->index.
133141
*/
134-
bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted)
142+
bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted,
143+
int bi_crypt_skip)
135144
{
136145
if (!bio)
137146
return true;
138147

148+
#ifdef CONFIG_DM_DEFAULT_KEY
149+
if (bi_crypt_skip != bio->bi_crypt_skip)
150+
return false;
151+
#endif
139152
/* if both of them are not encrypted, no further check is needed */
140153
if (!bio_dun(bio) && !bio_encrypted)
141154
return true;

fs/f2fs/data.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
448448
1, is_read_io(fio->op), fio->type, fio->temp);
449449

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

453454
if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
454455
bio_put(bio);
@@ -471,6 +472,7 @@ int f2fs_submit_page_write(struct f2fs_io_info *fio)
471472
struct page *bio_page;
472473
struct inode *inode;
473474
bool bio_encrypted;
475+
int bi_crypt_skip;
474476
u64 dun;
475477
int err = 0;
476478

@@ -497,6 +499,7 @@ int f2fs_submit_page_write(struct f2fs_io_info *fio)
497499
bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
498500
inode = fio->page->mapping->host;
499501
dun = PG_DUN(inode, fio->page);
502+
bi_crypt_skip = fio->encrypted_page ? 1 : 0;
500503
bio_encrypted = f2fs_may_encrypt_bio(inode, fio);
501504

502505
/* set submitted = true as a return value */
@@ -510,7 +513,7 @@ int f2fs_submit_page_write(struct f2fs_io_info *fio)
510513
__submit_merged_bio(io);
511514

512515
/* ICE support */
513-
if (!fscrypt_mergeable_bio(io->bio, dun, bio_encrypted))
516+
if (!fscrypt_mergeable_bio(io->bio, dun, bio_encrypted, bi_crypt_skip))
514517
__submit_merged_bio(io);
515518

516519
alloc_new:
@@ -526,7 +529,7 @@ int f2fs_submit_page_write(struct f2fs_io_info *fio)
526529
fio->type, fio->temp);
527530
if (bio_encrypted)
528531
fscrypt_set_ice_dun(inode, io->bio, dun);
529-
532+
fscrypt_set_ice_skip(io->bio, bi_crypt_skip);
530533
io->fio = *fio;
531534
}
532535

@@ -1538,7 +1541,7 @@ static int f2fs_mpage_readpages(struct address_space *mapping,
15381541

15391542
dun = PG_DUN(inode, page);
15401543
bio_encrypted = f2fs_may_encrypt_bio(inode, NULL);
1541-
if (!fscrypt_mergeable_bio(bio, dun, bio_encrypted)) {
1544+
if (!fscrypt_mergeable_bio(bio, dun, bio_encrypted, 0)) {
15421545
__submit_bio(F2FS_I_SB(inode), bio, DATA);
15431546
bio = NULL;
15441547
}

include/linux/blk_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ struct bio {
7878
*/
7979
struct inode *bi_dio_inode;
8080
#endif
81+
#ifdef CONFIG_DM_DEFAULT_KEY
82+
int bi_crypt_skip;
83+
#endif
8184

8285
unsigned short bi_vcnt; /* how many bio_vec's */
8386

include/linux/fscrypt.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ static inline int fscrypt_encrypt_symlink(struct inode *inode,
257257
extern int fscrypt_using_hardware_encryption(const struct inode *inode);
258258
extern void fscrypt_set_ice_dun(const struct inode *inode,
259259
struct bio *bio, u64 dun);
260-
extern bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted);
260+
extern bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted,
261+
int bi_crypt_skip);
262+
extern void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip);
261263
#else
262264
static inline int fscrypt_using_hardware_encryption(const struct inode *inode)
263265
{
@@ -270,8 +272,12 @@ static inline void fscrypt_set_ice_dun(const struct inode *inode,
270272
return;
271273
}
272274

275+
static inline void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip)
276+
{
277+
}
278+
273279
static inline bool fscrypt_mergeable_bio(struct bio *bio,
274-
u64 dun, bool bio_encrypted)
280+
u64 dun, bool bio_encrypted, int bi_crypt_skip)
275281
{
276282
return true;
277283
}

security/pfe/pfk.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ static int pfk_get_key_for_bio(const struct bio *bio,
287287
{
288288
const struct inode *inode;
289289
enum pfe_type which_pfe;
290-
const struct blk_encryption_key *key;
291290
char *s_type = NULL;
291+
const struct blk_encryption_key *key = NULL;
292292

293293
inode = pfk_bio_get_inode(bio);
294294
which_pfe = pfk_get_pfe_type(inode);
@@ -307,7 +307,9 @@ static int pfk_get_key_for_bio(const struct bio *bio,
307307
* bio is not for an encrypted file. Use ->bi_crypt_key if it was set.
308308
* Otherwise, don't encrypt/decrypt the bio.
309309
*/
310+
#ifdef CONFIG_DM_DEFAULT_KEY
310311
key = bio->bi_crypt_key;
312+
#endif
311313
if (!key) {
312314
*is_pfe = false;
313315
return -EINVAL;
@@ -469,13 +471,18 @@ int pfk_load_key_end(const struct bio *bio, bool *is_pfe)
469471
*/
470472
bool pfk_allow_merge_bio(const struct bio *bio1, const struct bio *bio2)
471473
{
472-
const struct blk_encryption_key *key1;
473-
const struct blk_encryption_key *key2;
474+
const struct blk_encryption_key *key1 = NULL;
475+
const struct blk_encryption_key *key2 = NULL;
474476
const struct inode *inode1;
475477
const struct inode *inode2;
476478
enum pfe_type which_pfe1;
477479
enum pfe_type which_pfe2;
478480

481+
#ifdef CONFIG_DM_DEFAULT_KEY
482+
key1 = bio1->bi_crypt_key;
483+
key2 = bio2->bi_crypt_key;
484+
#endif
485+
479486
if (!pfk_is_ready())
480487
return false;
481488

0 commit comments

Comments
 (0)