Skip to content

Commit c93d8f8

Browse files
committed
ext4: add basic fs-verity support
Add most of fs-verity support to ext4. fs-verity is a filesystem feature that enables transparent integrity protection and authentication of read-only files. It uses a dm-verity like mechanism at the file level: a Merkle tree is used to verify any block in the file in log(filesize) time. It is implemented mainly by helper functions in fs/verity/. See Documentation/filesystems/fsverity.rst for the full documentation. This commit adds all of ext4 fs-verity support except for the actual data verification, including: - Adding a filesystem feature flag and an inode flag for fs-verity. - Implementing the fsverity_operations to support enabling verity on an inode and reading/writing the verity metadata. - Updating ->write_begin(), ->write_end(), and ->writepages() to support writing verity metadata pages. - Calling the fs-verity hooks for ->open(), ->setattr(), and ->ioctl(). ext4 stores the verity metadata (Merkle tree and fsverity_descriptor) past the end of the file, starting at the first 64K boundary beyond i_size. This approach works because (a) verity files are readonly, and (b) pages fully beyond i_size aren't visible to userspace but can be read/written internally by ext4 with only some relatively small changes to ext4. This approach avoids having to depend on the EA_INODE feature and on rearchitecturing ext4's xattr support to support paging multi-gigabyte xattrs into memory, and to support encrypting xattrs. Note that the verity metadata *must* be encrypted when the file is, since it contains hashes of the plaintext data. This patch incorporates work by Theodore Ts'o and Chandan Rajendra. Reviewed-by: Theodore Ts'o <[email protected]> Signed-off-by: Eric Biggers <[email protected]>
1 parent 432434c commit c93d8f8

File tree

8 files changed

+457
-17
lines changed

8 files changed

+457
-17
lines changed

fs/ext4/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ ext4-y := balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \
1313

1414
ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
1515
ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
16+
ext4-$(CONFIG_FS_VERITY) += verity.o

fs/ext4/ext4.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#endif
4242

4343
#include <linux/fscrypt.h>
44+
#include <linux/fsverity.h>
4445

4546
#include <linux/compiler.h>
4647

@@ -395,14 +396,15 @@ struct flex_groups {
395396
#define EXT4_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
396397
#define EXT4_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
397398
#define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
399+
#define EXT4_VERITY_FL 0x00100000 /* Verity protected inode */
398400
#define EXT4_EA_INODE_FL 0x00200000 /* Inode used for large EA */
399401
#define EXT4_EOFBLOCKS_FL 0x00400000 /* Blocks allocated beyond EOF */
400402
#define EXT4_INLINE_DATA_FL 0x10000000 /* Inode has inline data. */
401403
#define EXT4_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
402404
#define EXT4_CASEFOLD_FL 0x40000000 /* Casefolded file */
403405
#define EXT4_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
404406

405-
#define EXT4_FL_USER_VISIBLE 0x704BDFFF /* User visible flags */
407+
#define EXT4_FL_USER_VISIBLE 0x705BDFFF /* User visible flags */
406408
#define EXT4_FL_USER_MODIFIABLE 0x604BC0FF /* User modifiable flags */
407409

408410
/* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
@@ -467,6 +469,7 @@ enum {
467469
EXT4_INODE_TOPDIR = 17, /* Top of directory hierarchies*/
468470
EXT4_INODE_HUGE_FILE = 18, /* Set to each huge file */
469471
EXT4_INODE_EXTENTS = 19, /* Inode uses extents */
472+
EXT4_INODE_VERITY = 20, /* Verity protected inode */
470473
EXT4_INODE_EA_INODE = 21, /* Inode used for large EA */
471474
EXT4_INODE_EOFBLOCKS = 22, /* Blocks allocated beyond EOF */
472475
EXT4_INODE_INLINE_DATA = 28, /* Data in inode. */
@@ -512,6 +515,7 @@ static inline void ext4_check_flag_values(void)
512515
CHECK_FLAG_VALUE(TOPDIR);
513516
CHECK_FLAG_VALUE(HUGE_FILE);
514517
CHECK_FLAG_VALUE(EXTENTS);
518+
CHECK_FLAG_VALUE(VERITY);
515519
CHECK_FLAG_VALUE(EA_INODE);
516520
CHECK_FLAG_VALUE(EOFBLOCKS);
517521
CHECK_FLAG_VALUE(INLINE_DATA);
@@ -1560,6 +1564,7 @@ enum {
15601564
EXT4_STATE_MAY_INLINE_DATA, /* may have in-inode data */
15611565
EXT4_STATE_EXT_PRECACHED, /* extents have been precached */
15621566
EXT4_STATE_LUSTRE_EA_INODE, /* Lustre-style ea_inode */
1567+
EXT4_STATE_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */
15631568
};
15641569

15651570
#define EXT4_INODE_BIT_FNS(name, field, offset) \
@@ -1610,6 +1615,12 @@ static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
16101615
#define EXT4_SB(sb) (sb)
16111616
#endif
16121617

1618+
static inline bool ext4_verity_in_progress(struct inode *inode)
1619+
{
1620+
return IS_ENABLED(CONFIG_FS_VERITY) &&
1621+
ext4_test_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
1622+
}
1623+
16131624
#define NEXT_ORPHAN(inode) EXT4_I(inode)->i_dtime
16141625

16151626
/*
@@ -1662,6 +1673,7 @@ static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
16621673
#define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM 0x0400
16631674
#define EXT4_FEATURE_RO_COMPAT_READONLY 0x1000
16641675
#define EXT4_FEATURE_RO_COMPAT_PROJECT 0x2000
1676+
#define EXT4_FEATURE_RO_COMPAT_VERITY 0x8000
16651677

16661678
#define EXT4_FEATURE_INCOMPAT_COMPRESSION 0x0001
16671679
#define EXT4_FEATURE_INCOMPAT_FILETYPE 0x0002
@@ -1756,6 +1768,7 @@ EXT4_FEATURE_RO_COMPAT_FUNCS(bigalloc, BIGALLOC)
17561768
EXT4_FEATURE_RO_COMPAT_FUNCS(metadata_csum, METADATA_CSUM)
17571769
EXT4_FEATURE_RO_COMPAT_FUNCS(readonly, READONLY)
17581770
EXT4_FEATURE_RO_COMPAT_FUNCS(project, PROJECT)
1771+
EXT4_FEATURE_RO_COMPAT_FUNCS(verity, VERITY)
17591772

17601773
EXT4_FEATURE_INCOMPAT_FUNCS(compression, COMPRESSION)
17611774
EXT4_FEATURE_INCOMPAT_FUNCS(filetype, FILETYPE)
@@ -1813,7 +1826,8 @@ EXT4_FEATURE_INCOMPAT_FUNCS(casefold, CASEFOLD)
18131826
EXT4_FEATURE_RO_COMPAT_BIGALLOC |\
18141827
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\
18151828
EXT4_FEATURE_RO_COMPAT_QUOTA |\
1816-
EXT4_FEATURE_RO_COMPAT_PROJECT)
1829+
EXT4_FEATURE_RO_COMPAT_PROJECT |\
1830+
EXT4_FEATURE_RO_COMPAT_VERITY)
18171831

18181832
#define EXTN_FEATURE_FUNCS(ver) \
18191833
static inline bool ext4_has_unknown_ext##ver##_compat_features(struct super_block *sb) \
@@ -3283,6 +3297,9 @@ extern int ext4_bio_write_page(struct ext4_io_submit *io,
32833297
/* mmp.c */
32843298
extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);
32853299

3300+
/* verity.c */
3301+
extern const struct fsverity_operations ext4_verityops;
3302+
32863303
/*
32873304
* Add new method to test whether block and inode bitmaps are properly
32883305
* initialized. With uninit_bg reading the block from disk is not enough

fs/ext4/file.c

+4
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
457457
if (ret)
458458
return ret;
459459

460+
ret = fsverity_file_open(inode, filp);
461+
if (ret)
462+
return ret;
463+
460464
/*
461465
* Set up the jbd2_inode if we are opening the inode for
462466
* writing and the journal is present

fs/ext4/inode.c

+38-15
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,9 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
13401340
}
13411341

13421342
if (ret) {
1343+
bool extended = (pos + len > inode->i_size) &&
1344+
!ext4_verity_in_progress(inode);
1345+
13431346
unlock_page(page);
13441347
/*
13451348
* __block_write_begin may have instantiated a few blocks
@@ -1349,11 +1352,11 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
13491352
* Add inode to orphan list in case we crash before
13501353
* truncate finishes
13511354
*/
1352-
if (pos + len > inode->i_size && ext4_can_truncate(inode))
1355+
if (extended && ext4_can_truncate(inode))
13531356
ext4_orphan_add(handle, inode);
13541357

13551358
ext4_journal_stop(handle);
1356-
if (pos + len > inode->i_size) {
1359+
if (extended) {
13571360
ext4_truncate_failed_write(inode);
13581361
/*
13591362
* If truncate failed early the inode might
@@ -1406,6 +1409,7 @@ static int ext4_write_end(struct file *file,
14061409
int ret = 0, ret2;
14071410
int i_size_changed = 0;
14081411
int inline_data = ext4_has_inline_data(inode);
1412+
bool verity = ext4_verity_in_progress(inode);
14091413

14101414
trace_ext4_write_end(inode, pos, len, copied);
14111415
if (inline_data) {
@@ -1423,12 +1427,16 @@ static int ext4_write_end(struct file *file,
14231427
/*
14241428
* it's important to update i_size while still holding page lock:
14251429
* page writeout could otherwise come in and zero beyond i_size.
1430+
*
1431+
* If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
1432+
* blocks are being written past EOF, so skip the i_size update.
14261433
*/
1427-
i_size_changed = ext4_update_inode_size(inode, pos + copied);
1434+
if (!verity)
1435+
i_size_changed = ext4_update_inode_size(inode, pos + copied);
14281436
unlock_page(page);
14291437
put_page(page);
14301438

1431-
if (old_size < pos)
1439+
if (old_size < pos && !verity)
14321440
pagecache_isize_extended(inode, old_size, pos);
14331441
/*
14341442
* Don't mark the inode dirty under page lock. First, it unnecessarily
@@ -1439,7 +1447,7 @@ static int ext4_write_end(struct file *file,
14391447
if (i_size_changed || inline_data)
14401448
ext4_mark_inode_dirty(handle, inode);
14411449

1442-
if (pos + len > inode->i_size && ext4_can_truncate(inode))
1450+
if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
14431451
/* if we have allocated more blocks and copied
14441452
* less. We will have blocks allocated outside
14451453
* inode->i_size. So truncate them
@@ -1450,7 +1458,7 @@ static int ext4_write_end(struct file *file,
14501458
if (!ret)
14511459
ret = ret2;
14521460

1453-
if (pos + len > inode->i_size) {
1461+
if (pos + len > inode->i_size && !verity) {
14541462
ext4_truncate_failed_write(inode);
14551463
/*
14561464
* If truncate failed early the inode might still be
@@ -1511,6 +1519,7 @@ static int ext4_journalled_write_end(struct file *file,
15111519
unsigned from, to;
15121520
int size_changed = 0;
15131521
int inline_data = ext4_has_inline_data(inode);
1522+
bool verity = ext4_verity_in_progress(inode);
15141523

15151524
trace_ext4_journalled_write_end(inode, pos, len, copied);
15161525
from = pos & (PAGE_SIZE - 1);
@@ -1540,13 +1549,14 @@ static int ext4_journalled_write_end(struct file *file,
15401549
if (!partial)
15411550
SetPageUptodate(page);
15421551
}
1543-
size_changed = ext4_update_inode_size(inode, pos + copied);
1552+
if (!verity)
1553+
size_changed = ext4_update_inode_size(inode, pos + copied);
15441554
ext4_set_inode_state(inode, EXT4_STATE_JDATA);
15451555
EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
15461556
unlock_page(page);
15471557
put_page(page);
15481558

1549-
if (old_size < pos)
1559+
if (old_size < pos && !verity)
15501560
pagecache_isize_extended(inode, old_size, pos);
15511561

15521562
if (size_changed || inline_data) {
@@ -1555,7 +1565,7 @@ static int ext4_journalled_write_end(struct file *file,
15551565
ret = ret2;
15561566
}
15571567

1558-
if (pos + len > inode->i_size && ext4_can_truncate(inode))
1568+
if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
15591569
/* if we have allocated more blocks and copied
15601570
* less. We will have blocks allocated outside
15611571
* inode->i_size. So truncate them
@@ -1566,7 +1576,7 @@ static int ext4_journalled_write_end(struct file *file,
15661576
ret2 = ext4_journal_stop(handle);
15671577
if (!ret)
15681578
ret = ret2;
1569-
if (pos + len > inode->i_size) {
1579+
if (pos + len > inode->i_size && !verity) {
15701580
ext4_truncate_failed_write(inode);
15711581
/*
15721582
* If truncate failed early the inode might still be
@@ -2162,7 +2172,8 @@ static int ext4_writepage(struct page *page,
21622172

21632173
trace_ext4_writepage(page);
21642174
size = i_size_read(inode);
2165-
if (page->index == size >> PAGE_SHIFT)
2175+
if (page->index == size >> PAGE_SHIFT &&
2176+
!ext4_verity_in_progress(inode))
21662177
len = size & ~PAGE_MASK;
21672178
else
21682179
len = PAGE_SIZE;
@@ -2246,7 +2257,8 @@ static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
22462257
* after page tables are updated.
22472258
*/
22482259
size = i_size_read(mpd->inode);
2249-
if (page->index == size >> PAGE_SHIFT)
2260+
if (page->index == size >> PAGE_SHIFT &&
2261+
!ext4_verity_in_progress(mpd->inode))
22502262
len = size & ~PAGE_MASK;
22512263
else
22522264
len = PAGE_SIZE;
@@ -2345,6 +2357,9 @@ static int mpage_process_page_bufs(struct mpage_da_data *mpd,
23452357
ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
23462358
>> inode->i_blkbits;
23472359

2360+
if (ext4_verity_in_progress(inode))
2361+
blocks = EXT_MAX_BLOCKS;
2362+
23482363
do {
23492364
BUG_ON(buffer_locked(bh));
23502365

@@ -3061,8 +3076,8 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
30613076

30623077
index = pos >> PAGE_SHIFT;
30633078

3064-
if (ext4_nonda_switch(inode->i_sb) ||
3065-
S_ISLNK(inode->i_mode)) {
3079+
if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
3080+
ext4_verity_in_progress(inode)) {
30663081
*fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
30673082
return ext4_write_begin(file, mapping, pos,
30683083
len, flags, pagep, fsdata);
@@ -4739,6 +4754,8 @@ static bool ext4_should_use_dax(struct inode *inode)
47394754
return false;
47404755
if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
47414756
return false;
4757+
if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
4758+
return false;
47424759
return true;
47434760
}
47444761

@@ -4763,9 +4780,11 @@ void ext4_set_inode_flags(struct inode *inode)
47634780
new_fl |= S_ENCRYPTED;
47644781
if (flags & EXT4_CASEFOLD_FL)
47654782
new_fl |= S_CASEFOLD;
4783+
if (flags & EXT4_VERITY_FL)
4784+
new_fl |= S_VERITY;
47664785
inode_set_flags(inode, new_fl,
47674786
S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
4768-
S_ENCRYPTED|S_CASEFOLD);
4787+
S_ENCRYPTED|S_CASEFOLD|S_VERITY);
47694788
}
47704789

47714790
static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
@@ -5555,6 +5574,10 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
55555574
if (error)
55565575
return error;
55575576

5577+
error = fsverity_prepare_setattr(dentry, attr);
5578+
if (error)
5579+
return error;
5580+
55585581
if (is_quota_modification(inode, attr)) {
55595582
error = dquot_initialize(inode);
55605583
if (error)

fs/ext4/ioctl.c

+13
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,17 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
11711171
}
11721172
case EXT4_IOC_SHUTDOWN:
11731173
return ext4_shutdown(sb, arg);
1174+
1175+
case FS_IOC_ENABLE_VERITY:
1176+
if (!ext4_has_feature_verity(sb))
1177+
return -EOPNOTSUPP;
1178+
return fsverity_ioctl_enable(filp, (const void __user *)arg);
1179+
1180+
case FS_IOC_MEASURE_VERITY:
1181+
if (!ext4_has_feature_verity(sb))
1182+
return -EOPNOTSUPP;
1183+
return fsverity_ioctl_measure(filp, (void __user *)arg);
1184+
11741185
default:
11751186
return -ENOTTY;
11761187
}
@@ -1233,6 +1244,8 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
12331244
case EXT4_IOC_GET_ENCRYPTION_POLICY:
12341245
case EXT4_IOC_SHUTDOWN:
12351246
case FS_IOC_GETFSMAP:
1247+
case FS_IOC_ENABLE_VERITY:
1248+
case FS_IOC_MEASURE_VERITY:
12361249
break;
12371250
default:
12381251
return -ENOIOCTLCMD;

fs/ext4/super.c

+9
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ void ext4_clear_inode(struct inode *inode)
11791179
EXT4_I(inode)->jinode = NULL;
11801180
}
11811181
fscrypt_put_encryption_info(inode);
1182+
fsverity_cleanup_inode(inode);
11821183
}
11831184

11841185
static struct inode *ext4_nfs_get_inode(struct super_block *sb,
@@ -4272,6 +4273,9 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
42724273
#ifdef CONFIG_FS_ENCRYPTION
42734274
sb->s_cop = &ext4_cryptops;
42744275
#endif
4276+
#ifdef CONFIG_FS_VERITY
4277+
sb->s_vop = &ext4_verityops;
4278+
#endif
42754279
#ifdef CONFIG_QUOTA
42764280
sb->dq_op = &ext4_quota_operations;
42774281
if (ext4_has_feature_quota(sb))
@@ -4419,6 +4423,11 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
44194423
goto failed_mount_wq;
44204424
}
44214425

4426+
if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4427+
ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4428+
goto failed_mount_wq;
4429+
}
4430+
44224431
if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
44234432
!ext4_has_feature_encrypt(sb)) {
44244433
ext4_set_feature_encrypt(sb);

fs/ext4/sysfs.c

+6
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ EXT4_ATTR_FEATURE(encryption);
242242
#ifdef CONFIG_UNICODE
243243
EXT4_ATTR_FEATURE(casefold);
244244
#endif
245+
#ifdef CONFIG_FS_VERITY
246+
EXT4_ATTR_FEATURE(verity);
247+
#endif
245248
EXT4_ATTR_FEATURE(metadata_csum_seed);
246249

247250
static struct attribute *ext4_feat_attrs[] = {
@@ -253,6 +256,9 @@ static struct attribute *ext4_feat_attrs[] = {
253256
#endif
254257
#ifdef CONFIG_UNICODE
255258
ATTR_LIST(casefold),
259+
#endif
260+
#ifdef CONFIG_FS_VERITY
261+
ATTR_LIST(verity),
256262
#endif
257263
ATTR_LIST(metadata_csum_seed),
258264
NULL,

0 commit comments

Comments
 (0)