* [PATCH] erofs: code for verifying superblock checksum of an erofs image.
@ 2019-10-20 13:03 Pratik Shinde
2019-10-20 14:28 ` Gao Xiang via Linux-erofs
0 siblings, 1 reply; 6+ messages in thread
From: Pratik Shinde @ 2019-10-20 13:03 UTC (permalink / raw)
To: linux-erofs, gaoxiang25, yuchao0
Patch for kernel side changes of checksum feature.I used kernel's
crc32c library for calculating the checksum.
Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
---
fs/erofs/erofs_fs.h | 5 +++--
fs/erofs/internal.h | 2 +-
fs/erofs/super.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h
index b1ee565..bab5506 100644
--- a/fs/erofs/erofs_fs.h
+++ b/fs/erofs/erofs_fs.h
@@ -17,6 +17,7 @@
*/
#define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
#define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
+#define EROFS_FEATURE_SB_CHKSUM 0x0001
/* 128-byte erofs on-disk super block */
struct erofs_super_block {
@@ -37,8 +38,8 @@ struct erofs_super_block {
__u8 uuid[16]; /* 128-bit uuid for volume */
__u8 volume_name[16]; /* volume name */
__le32 feature_incompat;
-
- __u8 reserved2[44];
+ __le32 chksum_blocks; /* number of blocks used for checksum */
+ __u8 reserved2[40];
};
/*
diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
index 544a453..cd3af45 100644
--- a/fs/erofs/internal.h
+++ b/fs/erofs/internal.h
@@ -86,7 +86,7 @@ struct erofs_sb_info {
u8 uuid[16]; /* 128-bit uuid for volume */
u8 volume_name[16]; /* volume name */
u32 feature_incompat;
-
+ u32 features;
unsigned int mount_opt;
};
diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 0e36949..94e1d6a 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -9,6 +9,7 @@
#include <linux/statfs.h>
#include <linux/parser.h>
#include <linux/seq_file.h>
+#include <linux/crc32c.h>
#include "xattr.h"
#define CREATE_TRACE_POINTS
@@ -46,6 +47,45 @@ void _erofs_info(struct super_block *sb, const char *function,
va_end(args);
}
+static int erofs_validate_sb_chksum(struct erofs_super_block *dsb,
+ struct super_block *sb)
+{
+ u32 disk_chksum = le32_to_cpu(dsb->checksum);
+ u32 nblocks = le32_to_cpu(dsb->chksum_blocks);
+ u32 crc;
+ struct erofs_super_block *dsb2;
+ char *buf;
+ unsigned int off = 0;
+ void *kaddr;
+ struct page *page;
+ int i, ret = -EINVAL;
+
+ buf = kmalloc(nblocks * EROFS_BLKSIZ, GFP_KERNEL);
+ if (!buf)
+ goto out;
+ for (i = 0; i < nblocks; i++) {
+ page = erofs_get_meta_page(sb, i);
+ if (IS_ERR(page))
+ goto out;
+ kaddr = kmap_atomic(page);
+ (void) memcpy(buf + off, kaddr, EROFS_BLKSIZ);
+ kunmap_atomic(kaddr);
+ unlock_page(page);
+ /* first page will be released by erofs_read_superblock */
+ if (i != 0)
+ put_page(page);
+ off += EROFS_BLKSIZ;
+ }
+ dsb2 = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
+ dsb2->checksum = 0;
+ crc = crc32c(0, buf, nblocks * EROFS_BLKSIZ);
+ if (crc != disk_chksum)
+ goto out;
+ ret = 0;
+out: kfree(buf);
+ return ret;
+}
+
static void erofs_inode_init_once(void *ptr)
{
struct erofs_inode *vi = ptr;
@@ -109,18 +149,20 @@ static int erofs_read_superblock(struct super_block *sb)
erofs_err(sb, "cannot read erofs superblock");
return PTR_ERR(page);
}
-
sbi = EROFS_SB(sb);
-
data = kmap_atomic(page);
dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
-
ret = -EINVAL;
if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
erofs_err(sb, "cannot find valid erofs superblock");
goto out;
}
-
+ if (dsb->feature_compat & EROFS_FEATURE_SB_CHKSUM) {
+ if (erofs_validate_sb_chksum(dsb, sb)) {
+ erofs_err(sb, "super block checksum incorrect");
+ goto out;
+ }
+ }
blkszbits = dsb->blkszbits;
/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
if (blkszbits != LOG_BLOCK_SIZE) {
--
2.9.3
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] erofs: code for verifying superblock checksum of an erofs image.
2019-10-20 13:03 [PATCH] erofs: code for verifying superblock checksum of an erofs image Pratik Shinde
@ 2019-10-20 14:28 ` Gao Xiang via Linux-erofs
0 siblings, 0 replies; 6+ messages in thread
From: Gao Xiang via Linux-erofs @ 2019-10-20 14:28 UTC (permalink / raw)
To: Pratik Shinde; +Cc: linux-erofs
Hi Pritik,
On Sun, Oct 20, 2019 at 06:33:20PM +0530, Pratik Shinde wrote:
> Patch for kernel side changes of checksum feature.I used kernel's
> crc32c library for calculating the checksum.
>
> Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
> ---
> fs/erofs/erofs_fs.h | 5 +++--
> fs/erofs/internal.h | 2 +-
> fs/erofs/super.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
> 3 files changed, 50 insertions(+), 7 deletions(-)
>
> diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h
> index b1ee565..bab5506 100644
> --- a/fs/erofs/erofs_fs.h
> +++ b/fs/erofs/erofs_fs.h
> @@ -17,6 +17,7 @@
> */
> #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
> #define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
> +#define EROFS_FEATURE_SB_CHKSUM 0x0001
Thanks for your patch :) I will comment this patch tomorrow,
but when you decide to send a kernel patch (it is a common
practice), don't forget to run scripts/get_maintainer.pl and
generally Cc all of them
(It is really needed to Cc LKML mailing list linux-kernel@vger.kernel.org.)
However, no rush to update and resend this patch.
I will do detailed comments tomorrow, it needs some
changes (especially stuffs in erofs_validate_sb_chksum()).
Thanks,
Gao Xiang
>
> /* 128-byte erofs on-disk super block */
> struct erofs_super_block {
> @@ -37,8 +38,8 @@ struct erofs_super_block {
> __u8 uuid[16]; /* 128-bit uuid for volume */
> __u8 volume_name[16]; /* volume name */
> __le32 feature_incompat;
> -
> - __u8 reserved2[44];
> + __le32 chksum_blocks; /* number of blocks used for checksum */
> + __u8 reserved2[40];
> };
>
> /*
> diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
> index 544a453..cd3af45 100644
> --- a/fs/erofs/internal.h
> +++ b/fs/erofs/internal.h
> @@ -86,7 +86,7 @@ struct erofs_sb_info {
> u8 uuid[16]; /* 128-bit uuid for volume */
> u8 volume_name[16]; /* volume name */
> u32 feature_incompat;
> -
> + u32 features;
> unsigned int mount_opt;
> };
>
> diff --git a/fs/erofs/super.c b/fs/erofs/super.c
> index 0e36949..94e1d6a 100644
> --- a/fs/erofs/super.c
> +++ b/fs/erofs/super.c
> @@ -9,6 +9,7 @@
> #include <linux/statfs.h>
> #include <linux/parser.h>
> #include <linux/seq_file.h>
> +#include <linux/crc32c.h>
> #include "xattr.h"
>
> #define CREATE_TRACE_POINTS
> @@ -46,6 +47,45 @@ void _erofs_info(struct super_block *sb, const char *function,
> va_end(args);
> }
>
> +static int erofs_validate_sb_chksum(struct erofs_super_block *dsb,
> + struct super_block *sb)
> +{
> + u32 disk_chksum = le32_to_cpu(dsb->checksum);
> + u32 nblocks = le32_to_cpu(dsb->chksum_blocks);
> + u32 crc;
> + struct erofs_super_block *dsb2;
> + char *buf;
> + unsigned int off = 0;
> + void *kaddr;
> + struct page *page;
> + int i, ret = -EINVAL;
> +
> + buf = kmalloc(nblocks * EROFS_BLKSIZ, GFP_KERNEL);
> + if (!buf)
> + goto out;
> + for (i = 0; i < nblocks; i++) {
> + page = erofs_get_meta_page(sb, i);
> + if (IS_ERR(page))
> + goto out;
> + kaddr = kmap_atomic(page);
> + (void) memcpy(buf + off, kaddr, EROFS_BLKSIZ);
> + kunmap_atomic(kaddr);
> + unlock_page(page);
> + /* first page will be released by erofs_read_superblock */
> + if (i != 0)
> + put_page(page);
> + off += EROFS_BLKSIZ;
> + }
> + dsb2 = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
> + dsb2->checksum = 0;
> + crc = crc32c(0, buf, nblocks * EROFS_BLKSIZ);
> + if (crc != disk_chksum)
> + goto out;
> + ret = 0;
> +out: kfree(buf);
> + return ret;
> +}
> +
> static void erofs_inode_init_once(void *ptr)
> {
> struct erofs_inode *vi = ptr;
> @@ -109,18 +149,20 @@ static int erofs_read_superblock(struct super_block *sb)
> erofs_err(sb, "cannot read erofs superblock");
> return PTR_ERR(page);
> }
> -
> sbi = EROFS_SB(sb);
> -
> data = kmap_atomic(page);
> dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
> -
> ret = -EINVAL;
> if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
> erofs_err(sb, "cannot find valid erofs superblock");
> goto out;
> }
> -
> + if (dsb->feature_compat & EROFS_FEATURE_SB_CHKSUM) {
> + if (erofs_validate_sb_chksum(dsb, sb)) {
> + erofs_err(sb, "super block checksum incorrect");
> + goto out;
> + }
> + }
> blkszbits = dsb->blkszbits;
> /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
> if (blkszbits != LOG_BLOCK_SIZE) {
> --
> 2.9.3
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] erofs: code for verifying superblock checksum of an erofs image.
2019-10-20 19:28 ` Pratik Shinde
@ 2019-10-21 6:25 ` Gao Xiang
-1 siblings, 0 replies; 6+ messages in thread
From: Gao Xiang @ 2019-10-21 6:25 UTC (permalink / raw)
To: Pratik Shinde; +Cc: linux-erofs, yuchao0, linux-kernel
Hi Pratik,
Some comments as below...
On Mon, Oct 21, 2019 at 12:58:28AM +0530, Pratik Shinde wrote:
> Patch for kernel side changes of checksum feature.I used kernel's
> crc32c library for calculating the checksum.
>
> Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
> ---
> fs/erofs/erofs_fs.h | 5 +++--
> fs/erofs/internal.h | 2 +-
> fs/erofs/super.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
> 3 files changed, 50 insertions(+), 7 deletions(-)
>
> diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h
> index b1ee565..bab5506 100644
> --- a/fs/erofs/erofs_fs.h
> +++ b/fs/erofs/erofs_fs.h
> @@ -17,6 +17,7 @@
> */
> #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
> #define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
> +#define EROFS_FEATURE_SB_CHKSUM 0x0001
How about keeping in sync with erofs-utils?
>
> /* 128-byte erofs on-disk super block */
> struct erofs_super_block {
> @@ -37,8 +38,8 @@ struct erofs_super_block {
> __u8 uuid[16]; /* 128-bit uuid for volume */
> __u8 volume_name[16]; /* volume name */
> __le32 feature_incompat;
> -
> - __u8 reserved2[44];
> + __le32 chksum_blocks; /* number of blocks used for checksum */
> + __u8 reserved2[40];
> };
>
> /*
> diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
> index 544a453..cd3af45 100644
> --- a/fs/erofs/internal.h
> +++ b/fs/erofs/internal.h
> @@ -86,7 +86,7 @@ struct erofs_sb_info {
> u8 uuid[16]; /* 128-bit uuid for volume */
> u8 volume_name[16]; /* volume name */
> u32 feature_incompat;
> -
> + u32 features;
no use? and
how about keeping in sync with erofs-utils, naming feature_compat as well? see,
https://lore.kernel.org/r/20191020141102.GA30399@hsiangkao-HP-ZHAN-66-Pro-G1/
> unsigned int mount_opt;
> };
>
> diff --git a/fs/erofs/super.c b/fs/erofs/super.c
> index 0e36949..94e1d6a 100644
> --- a/fs/erofs/super.c
> +++ b/fs/erofs/super.c
> @@ -9,6 +9,7 @@
> #include <linux/statfs.h>
> #include <linux/parser.h>
> #include <linux/seq_file.h>
> +#include <linux/crc32c.h>
> #include "xattr.h"
>
> #define CREATE_TRACE_POINTS
> @@ -46,6 +47,45 @@ void _erofs_info(struct super_block *sb, const char *function,
> va_end(args);
> }
>
> +static int erofs_validate_sb_chksum(struct erofs_super_block *dsb,
> + struct super_block *sb)
erofs_validate_sb_chksum(struct super_block *sb, void *sbdata)
Can we pass void *sbdata --- first page(block) instead?
> +{
> + u32 disk_chksum = le32_to_cpu(dsb->checksum);
> + u32 nblocks = le32_to_cpu(dsb->chksum_blocks);
> + u32 crc;
> + struct erofs_super_block *dsb2;
> + char *buf;
> + unsigned int off = 0;
> + void *kaddr;
> + struct page *page;
> + int i, ret = -EINVAL;
> +
> + buf = kmalloc(nblocks * EROFS_BLKSIZ, GFP_KERNEL);
IMO, I think we could just
1) malloc EROFS_BLKSIZ;
2) copy sbdata -> buf;
3) dsb->checksum = 0;
4) crc32c the first copied block;
5) kfree.
or some better approach without allocating memory.
> + if (!buf)
> + goto out;
how about erroring out with proper return value?
> + for (i = 0; i < nblocks; i++) {
> + page = erofs_get_meta_page(sb, i);
> + if (IS_ERR(page))
ditto.
> + goto out;
> + kaddr = kmap_atomic(page);
> + (void) memcpy(buf + off, kaddr, EROFS_BLKSIZ);
we can call crc32c for multiple times, and no need to
get and calc 0-th block again...
and no memcpy at all.
> + kunmap_atomic(kaddr);
> + unlock_page(page);
> + /* first page will be released by erofs_read_superblock */
> + if (i != 0)
> + put_page(page);
> + off += EROFS_BLKSIZ;
> + }
> + dsb2 = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
> + dsb2->checksum = 0;
> + crc = crc32c(0, buf, nblocks * EROFS_BLKSIZ);
> + if (crc != disk_chksum)
> + goto out;
> + ret = 0;
> +out: kfree(buf);
> + return ret;
> +}
> +
> static void erofs_inode_init_once(void *ptr)
> {
> struct erofs_inode *vi = ptr;
> @@ -109,18 +149,20 @@ static int erofs_read_superblock(struct super_block *sb)
> erofs_err(sb, "cannot read erofs superblock");
> return PTR_ERR(page);
> }
> -
> sbi = EROFS_SB(sb);
> -
> data = kmap_atomic(page);
should use kmap() then, because kmalloc() and get_meta_page() can sleep and
last for long time.
> dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
> -
> ret = -EINVAL;
> if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
> erofs_err(sb, "cannot find valid erofs superblock");
> goto out;
> }
> -
> + if (dsb->feature_compat & EROFS_FEATURE_SB_CHKSUM) {
> + if (erofs_validate_sb_chksum(dsb, sb)) {
erofs_validate_sb_chksum(sb, data)
> + erofs_err(sb, "super block checksum incorrect");
It should be with a proper return value and corresponding messages,
see the related discussion:
https://lore.kernel.org/r/20190822090541.GA193349@architecture4/
Thanks,
Gao Xiang
> + goto out;
> + }
> + }
> blkszbits = dsb->blkszbits;
> /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
> if (blkszbits != LOG_BLOCK_SIZE) {
> --
> 2.9.3
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] erofs: code for verifying superblock checksum of an erofs image.
@ 2019-10-21 6:25 ` Gao Xiang
0 siblings, 0 replies; 6+ messages in thread
From: Gao Xiang @ 2019-10-21 6:25 UTC (permalink / raw)
To: Pratik Shinde; +Cc: linux-erofs, linux-kernel
Hi Pratik,
Some comments as below...
On Mon, Oct 21, 2019 at 12:58:28AM +0530, Pratik Shinde wrote:
> Patch for kernel side changes of checksum feature.I used kernel's
> crc32c library for calculating the checksum.
>
> Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
> ---
> fs/erofs/erofs_fs.h | 5 +++--
> fs/erofs/internal.h | 2 +-
> fs/erofs/super.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
> 3 files changed, 50 insertions(+), 7 deletions(-)
>
> diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h
> index b1ee565..bab5506 100644
> --- a/fs/erofs/erofs_fs.h
> +++ b/fs/erofs/erofs_fs.h
> @@ -17,6 +17,7 @@
> */
> #define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
> #define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
> +#define EROFS_FEATURE_SB_CHKSUM 0x0001
How about keeping in sync with erofs-utils?
>
> /* 128-byte erofs on-disk super block */
> struct erofs_super_block {
> @@ -37,8 +38,8 @@ struct erofs_super_block {
> __u8 uuid[16]; /* 128-bit uuid for volume */
> __u8 volume_name[16]; /* volume name */
> __le32 feature_incompat;
> -
> - __u8 reserved2[44];
> + __le32 chksum_blocks; /* number of blocks used for checksum */
> + __u8 reserved2[40];
> };
>
> /*
> diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
> index 544a453..cd3af45 100644
> --- a/fs/erofs/internal.h
> +++ b/fs/erofs/internal.h
> @@ -86,7 +86,7 @@ struct erofs_sb_info {
> u8 uuid[16]; /* 128-bit uuid for volume */
> u8 volume_name[16]; /* volume name */
> u32 feature_incompat;
> -
> + u32 features;
no use? and
how about keeping in sync with erofs-utils, naming feature_compat as well? see,
https://lore.kernel.org/r/20191020141102.GA30399@hsiangkao-HP-ZHAN-66-Pro-G1/
> unsigned int mount_opt;
> };
>
> diff --git a/fs/erofs/super.c b/fs/erofs/super.c
> index 0e36949..94e1d6a 100644
> --- a/fs/erofs/super.c
> +++ b/fs/erofs/super.c
> @@ -9,6 +9,7 @@
> #include <linux/statfs.h>
> #include <linux/parser.h>
> #include <linux/seq_file.h>
> +#include <linux/crc32c.h>
> #include "xattr.h"
>
> #define CREATE_TRACE_POINTS
> @@ -46,6 +47,45 @@ void _erofs_info(struct super_block *sb, const char *function,
> va_end(args);
> }
>
> +static int erofs_validate_sb_chksum(struct erofs_super_block *dsb,
> + struct super_block *sb)
erofs_validate_sb_chksum(struct super_block *sb, void *sbdata)
Can we pass void *sbdata --- first page(block) instead?
> +{
> + u32 disk_chksum = le32_to_cpu(dsb->checksum);
> + u32 nblocks = le32_to_cpu(dsb->chksum_blocks);
> + u32 crc;
> + struct erofs_super_block *dsb2;
> + char *buf;
> + unsigned int off = 0;
> + void *kaddr;
> + struct page *page;
> + int i, ret = -EINVAL;
> +
> + buf = kmalloc(nblocks * EROFS_BLKSIZ, GFP_KERNEL);
IMO, I think we could just
1) malloc EROFS_BLKSIZ;
2) copy sbdata -> buf;
3) dsb->checksum = 0;
4) crc32c the first copied block;
5) kfree.
or some better approach without allocating memory.
> + if (!buf)
> + goto out;
how about erroring out with proper return value?
> + for (i = 0; i < nblocks; i++) {
> + page = erofs_get_meta_page(sb, i);
> + if (IS_ERR(page))
ditto.
> + goto out;
> + kaddr = kmap_atomic(page);
> + (void) memcpy(buf + off, kaddr, EROFS_BLKSIZ);
we can call crc32c for multiple times, and no need to
get and calc 0-th block again...
and no memcpy at all.
> + kunmap_atomic(kaddr);
> + unlock_page(page);
> + /* first page will be released by erofs_read_superblock */
> + if (i != 0)
> + put_page(page);
> + off += EROFS_BLKSIZ;
> + }
> + dsb2 = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
> + dsb2->checksum = 0;
> + crc = crc32c(0, buf, nblocks * EROFS_BLKSIZ);
> + if (crc != disk_chksum)
> + goto out;
> + ret = 0;
> +out: kfree(buf);
> + return ret;
> +}
> +
> static void erofs_inode_init_once(void *ptr)
> {
> struct erofs_inode *vi = ptr;
> @@ -109,18 +149,20 @@ static int erofs_read_superblock(struct super_block *sb)
> erofs_err(sb, "cannot read erofs superblock");
> return PTR_ERR(page);
> }
> -
> sbi = EROFS_SB(sb);
> -
> data = kmap_atomic(page);
should use kmap() then, because kmalloc() and get_meta_page() can sleep and
last for long time.
> dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
> -
> ret = -EINVAL;
> if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
> erofs_err(sb, "cannot find valid erofs superblock");
> goto out;
> }
> -
> + if (dsb->feature_compat & EROFS_FEATURE_SB_CHKSUM) {
> + if (erofs_validate_sb_chksum(dsb, sb)) {
erofs_validate_sb_chksum(sb, data)
> + erofs_err(sb, "super block checksum incorrect");
It should be with a proper return value and corresponding messages,
see the related discussion:
https://lore.kernel.org/r/20190822090541.GA193349@architecture4/
Thanks,
Gao Xiang
> + goto out;
> + }
> + }
> blkszbits = dsb->blkszbits;
> /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
> if (blkszbits != LOG_BLOCK_SIZE) {
> --
> 2.9.3
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] erofs: code for verifying superblock checksum of an erofs image.
@ 2019-10-20 19:28 ` Pratik Shinde
0 siblings, 0 replies; 6+ messages in thread
From: Pratik Shinde @ 2019-10-20 19:28 UTC (permalink / raw)
To: linux-erofs, gaoxiang25, yuchao0; +Cc: linux-kernel, Pratik Shinde
Patch for kernel side changes of checksum feature.I used kernel's
crc32c library for calculating the checksum.
Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
---
fs/erofs/erofs_fs.h | 5 +++--
fs/erofs/internal.h | 2 +-
fs/erofs/super.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h
index b1ee565..bab5506 100644
--- a/fs/erofs/erofs_fs.h
+++ b/fs/erofs/erofs_fs.h
@@ -17,6 +17,7 @@
*/
#define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
#define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
+#define EROFS_FEATURE_SB_CHKSUM 0x0001
/* 128-byte erofs on-disk super block */
struct erofs_super_block {
@@ -37,8 +38,8 @@ struct erofs_super_block {
__u8 uuid[16]; /* 128-bit uuid for volume */
__u8 volume_name[16]; /* volume name */
__le32 feature_incompat;
-
- __u8 reserved2[44];
+ __le32 chksum_blocks; /* number of blocks used for checksum */
+ __u8 reserved2[40];
};
/*
diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
index 544a453..cd3af45 100644
--- a/fs/erofs/internal.h
+++ b/fs/erofs/internal.h
@@ -86,7 +86,7 @@ struct erofs_sb_info {
u8 uuid[16]; /* 128-bit uuid for volume */
u8 volume_name[16]; /* volume name */
u32 feature_incompat;
-
+ u32 features;
unsigned int mount_opt;
};
diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 0e36949..94e1d6a 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -9,6 +9,7 @@
#include <linux/statfs.h>
#include <linux/parser.h>
#include <linux/seq_file.h>
+#include <linux/crc32c.h>
#include "xattr.h"
#define CREATE_TRACE_POINTS
@@ -46,6 +47,45 @@ void _erofs_info(struct super_block *sb, const char *function,
va_end(args);
}
+static int erofs_validate_sb_chksum(struct erofs_super_block *dsb,
+ struct super_block *sb)
+{
+ u32 disk_chksum = le32_to_cpu(dsb->checksum);
+ u32 nblocks = le32_to_cpu(dsb->chksum_blocks);
+ u32 crc;
+ struct erofs_super_block *dsb2;
+ char *buf;
+ unsigned int off = 0;
+ void *kaddr;
+ struct page *page;
+ int i, ret = -EINVAL;
+
+ buf = kmalloc(nblocks * EROFS_BLKSIZ, GFP_KERNEL);
+ if (!buf)
+ goto out;
+ for (i = 0; i < nblocks; i++) {
+ page = erofs_get_meta_page(sb, i);
+ if (IS_ERR(page))
+ goto out;
+ kaddr = kmap_atomic(page);
+ (void) memcpy(buf + off, kaddr, EROFS_BLKSIZ);
+ kunmap_atomic(kaddr);
+ unlock_page(page);
+ /* first page will be released by erofs_read_superblock */
+ if (i != 0)
+ put_page(page);
+ off += EROFS_BLKSIZ;
+ }
+ dsb2 = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
+ dsb2->checksum = 0;
+ crc = crc32c(0, buf, nblocks * EROFS_BLKSIZ);
+ if (crc != disk_chksum)
+ goto out;
+ ret = 0;
+out: kfree(buf);
+ return ret;
+}
+
static void erofs_inode_init_once(void *ptr)
{
struct erofs_inode *vi = ptr;
@@ -109,18 +149,20 @@ static int erofs_read_superblock(struct super_block *sb)
erofs_err(sb, "cannot read erofs superblock");
return PTR_ERR(page);
}
-
sbi = EROFS_SB(sb);
-
data = kmap_atomic(page);
dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
-
ret = -EINVAL;
if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
erofs_err(sb, "cannot find valid erofs superblock");
goto out;
}
-
+ if (dsb->feature_compat & EROFS_FEATURE_SB_CHKSUM) {
+ if (erofs_validate_sb_chksum(dsb, sb)) {
+ erofs_err(sb, "super block checksum incorrect");
+ goto out;
+ }
+ }
blkszbits = dsb->blkszbits;
/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
if (blkszbits != LOG_BLOCK_SIZE) {
--
2.9.3
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH] erofs: code for verifying superblock checksum of an erofs image.
@ 2019-10-20 19:28 ` Pratik Shinde
0 siblings, 0 replies; 6+ messages in thread
From: Pratik Shinde @ 2019-10-20 19:28 UTC (permalink / raw)
To: linux-erofs, gaoxiang25, yuchao0; +Cc: linux-kernel
Patch for kernel side changes of checksum feature.I used kernel's
crc32c library for calculating the checksum.
Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
---
fs/erofs/erofs_fs.h | 5 +++--
fs/erofs/internal.h | 2 +-
fs/erofs/super.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h
index b1ee565..bab5506 100644
--- a/fs/erofs/erofs_fs.h
+++ b/fs/erofs/erofs_fs.h
@@ -17,6 +17,7 @@
*/
#define EROFS_FEATURE_INCOMPAT_LZ4_0PADDING 0x00000001
#define EROFS_ALL_FEATURE_INCOMPAT EROFS_FEATURE_INCOMPAT_LZ4_0PADDING
+#define EROFS_FEATURE_SB_CHKSUM 0x0001
/* 128-byte erofs on-disk super block */
struct erofs_super_block {
@@ -37,8 +38,8 @@ struct erofs_super_block {
__u8 uuid[16]; /* 128-bit uuid for volume */
__u8 volume_name[16]; /* volume name */
__le32 feature_incompat;
-
- __u8 reserved2[44];
+ __le32 chksum_blocks; /* number of blocks used for checksum */
+ __u8 reserved2[40];
};
/*
diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h
index 544a453..cd3af45 100644
--- a/fs/erofs/internal.h
+++ b/fs/erofs/internal.h
@@ -86,7 +86,7 @@ struct erofs_sb_info {
u8 uuid[16]; /* 128-bit uuid for volume */
u8 volume_name[16]; /* volume name */
u32 feature_incompat;
-
+ u32 features;
unsigned int mount_opt;
};
diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 0e36949..94e1d6a 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -9,6 +9,7 @@
#include <linux/statfs.h>
#include <linux/parser.h>
#include <linux/seq_file.h>
+#include <linux/crc32c.h>
#include "xattr.h"
#define CREATE_TRACE_POINTS
@@ -46,6 +47,45 @@ void _erofs_info(struct super_block *sb, const char *function,
va_end(args);
}
+static int erofs_validate_sb_chksum(struct erofs_super_block *dsb,
+ struct super_block *sb)
+{
+ u32 disk_chksum = le32_to_cpu(dsb->checksum);
+ u32 nblocks = le32_to_cpu(dsb->chksum_blocks);
+ u32 crc;
+ struct erofs_super_block *dsb2;
+ char *buf;
+ unsigned int off = 0;
+ void *kaddr;
+ struct page *page;
+ int i, ret = -EINVAL;
+
+ buf = kmalloc(nblocks * EROFS_BLKSIZ, GFP_KERNEL);
+ if (!buf)
+ goto out;
+ for (i = 0; i < nblocks; i++) {
+ page = erofs_get_meta_page(sb, i);
+ if (IS_ERR(page))
+ goto out;
+ kaddr = kmap_atomic(page);
+ (void) memcpy(buf + off, kaddr, EROFS_BLKSIZ);
+ kunmap_atomic(kaddr);
+ unlock_page(page);
+ /* first page will be released by erofs_read_superblock */
+ if (i != 0)
+ put_page(page);
+ off += EROFS_BLKSIZ;
+ }
+ dsb2 = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
+ dsb2->checksum = 0;
+ crc = crc32c(0, buf, nblocks * EROFS_BLKSIZ);
+ if (crc != disk_chksum)
+ goto out;
+ ret = 0;
+out: kfree(buf);
+ return ret;
+}
+
static void erofs_inode_init_once(void *ptr)
{
struct erofs_inode *vi = ptr;
@@ -109,18 +149,20 @@ static int erofs_read_superblock(struct super_block *sb)
erofs_err(sb, "cannot read erofs superblock");
return PTR_ERR(page);
}
-
sbi = EROFS_SB(sb);
-
data = kmap_atomic(page);
dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
-
ret = -EINVAL;
if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
erofs_err(sb, "cannot find valid erofs superblock");
goto out;
}
-
+ if (dsb->feature_compat & EROFS_FEATURE_SB_CHKSUM) {
+ if (erofs_validate_sb_chksum(dsb, sb)) {
+ erofs_err(sb, "super block checksum incorrect");
+ goto out;
+ }
+ }
blkszbits = dsb->blkszbits;
/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
if (blkszbits != LOG_BLOCK_SIZE) {
--
2.9.3
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2019-10-21 6:22 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-20 13:03 [PATCH] erofs: code for verifying superblock checksum of an erofs image Pratik Shinde
2019-10-20 14:28 ` Gao Xiang via Linux-erofs
2019-10-20 19:28 Pratik Shinde
2019-10-20 19:28 ` Pratik Shinde
2019-10-21 6:25 ` Gao Xiang
2019-10-21 6:25 ` Gao Xiang
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.