Skip to content

Commit b858a48

Browse files
Pratik32Gao Xiang
authored andcommitted
erofs: support superblock checksum
Introduce superblock checksum feature in order to check at mounting time. Note that the first 1024 bytes are ignore for x86 boot sectors and other oddities. Link: https://lore.kernel.org/r/20191104024937.113939-1-gaoxiang25@huawei.com Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com> Reviewed-by: Chao Yu <yuchao0@huawei.com> Cc: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Gao Xiang <gaoxiang25@huawei.com>
1 parent a93f8c3 commit b858a48

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎fs/erofs/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
config EROFS_FS
44
tristate "EROFS filesystem support"
55
depends on BLOCK
6+
select LIBCRC32C
67
help
78
EROFS (Enhanced Read-Only File System) is a lightweight
89
read-only file system with modern designs (eg. page-sized

‎fs/erofs/erofs_fs.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#define EROFS_SUPER_OFFSET 1024
1313

14+
#define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001
15+
1416
/*
1517
* Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should
1618
* be incompatible with this kernel version.
@@ -37,7 +39,6 @@ struct erofs_super_block {
3739
__u8 uuid[16]; /* 128-bit uuid for volume */
3840
__u8 volume_name[16]; /* volume name */
3941
__le32 feature_incompat;
40-
4142
__u8 reserved2[44];
4243
};
4344

‎fs/erofs/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct erofs_sb_info {
8585

8686
u8 uuid[16]; /* 128-bit uuid for volume */
8787
u8 volume_name[16]; /* volume name */
88+
u32 feature_compat;
8889
u32 feature_incompat;
8990

9091
unsigned int mount_opt;

‎fs/erofs/super.c‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/statfs.h>
1010
#include <linux/parser.h>
1111
#include <linux/seq_file.h>
12+
#include <linux/crc32c.h>
1213
#include "xattr.h"
1314

1415
#define CREATE_TRACE_POINTS
@@ -46,6 +47,30 @@ void _erofs_info(struct super_block *sb, const char *function,
4647
va_end(args);
4748
}
4849

50+
static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
51+
{
52+
struct erofs_super_block *dsb;
53+
u32 expected_crc, crc;
54+
55+
dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
56+
EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
57+
if (!dsb)
58+
return -ENOMEM;
59+
60+
expected_crc = le32_to_cpu(dsb->checksum);
61+
dsb->checksum = 0;
62+
/* to allow for x86 boot sectors and other oddities. */
63+
crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
64+
kfree(dsb);
65+
66+
if (crc != expected_crc) {
67+
erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
68+
crc, expected_crc);
69+
return -EBADMSG;
70+
}
71+
return 0;
72+
}
73+
4974
static void erofs_inode_init_once(void *ptr)
5075
{
5176
struct erofs_inode *vi = ptr;
@@ -112,7 +137,7 @@ static int erofs_read_superblock(struct super_block *sb)
112137

113138
sbi = EROFS_SB(sb);
114139

115-
data = kmap_atomic(page);
140+
data = kmap(page);
116141
dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
117142

118143
ret = -EINVAL;
@@ -121,6 +146,13 @@ static int erofs_read_superblock(struct super_block *sb)
121146
goto out;
122147
}
123148

149+
sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
150+
if (sbi->feature_compat & EROFS_FEATURE_COMPAT_SB_CHKSUM) {
151+
ret = erofs_superblock_csum_verify(sb, data);
152+
if (ret)
153+
goto out;
154+
}
155+
124156
blkszbits = dsb->blkszbits;
125157
/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
126158
if (blkszbits != LOG_BLOCK_SIZE) {
@@ -155,7 +187,7 @@ static int erofs_read_superblock(struct super_block *sb)
155187
}
156188
ret = 0;
157189
out:
158-
kunmap_atomic(data);
190+
kunmap(page);
159191
put_page(page);
160192
return ret;
161193
}

0 commit comments

Comments
 (0)