Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp436732ybx; Tue, 29 Oct 2019 22:09:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqxDEDOfJghZ4kov3taDV0I4/vyvyXiC4OP5c4WXVcmRh2rAaHXyaKzGrH9LIwU4TSk5cOxf X-Received: by 2002:a05:6402:88c:: with SMTP id e12mr28967150edy.170.1572412173557; Tue, 29 Oct 2019 22:09:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572412173; cv=none; d=google.com; s=arc-20160816; b=EPaokY+S+gnO9HXLFslt02pZOklUi+j+SKkKdga1z577mR0N02pTXk7yTdHiAIS8ae 55BjOLJCCXh/1/6ChRiPNRCx7mMdHsnac8m6gNJTMdOGBoQijhQm9L90c55uHhC1UwJc AEdwtuj2kHYor80ZkgYBcNNl5TmzKEIXROeitQvft7HudbWy1Df60mBo1vfMPP1ki8r/ y7T0FQAeLDMPNkYt9SZGYvjMJAtaAUkfjyr9IuApDNz9KTv1lmmS01fS+ZWWUCLdNkI4 BYPyEZ9InYeoRA9EXU8qlJJ8Tf7LnIHu/RwJMC3X0OTdSJGu98yLzaMyyTbSQog8vCA4 /hZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=jlJ4qwwb0VxTso4QtGUT4fbLHTnED+EriixLWdB0Hug=; b=hukno24zVCeg/Ww/vkNz10fT2BADq6zbfeWS2DiaA5iq1I3JaOu1bMRS06EToR9p1j Mj2Q/v7xsFY/kLdo9p9g+OrKKEGyV0KqpgiVMx0OD0a1vxgro9wR5vmIJbfTSAlmgYpy 3LPjc6PP+P2uBNhCwPk+VNKZ8odvQFdUUrbyUzSge1ojiUJ5xvhPKTI8qYTo6G1sykW2 O1I1EtNTdmJe0BvNi4JycxW9hlFTlgLnK+ow9UkbYKTztSBG41GPGMMnrQ++N94vlIJ7 pcIYW+LqVdHXruOOHSUd1bZQfFvpRDbaiAtCDmS26ByjLhlHGJPNtxAtmQ9Dc5v41LQ0 nUFg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s1si451381ejl.235.2019.10.29.22.09.09; Tue, 29 Oct 2019 22:09:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727119AbfJ3FGN (ORCPT + 99 others); Wed, 30 Oct 2019 01:06:13 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:5224 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725855AbfJ3FGN (ORCPT ); Wed, 30 Oct 2019 01:06:13 -0400 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 04441AD359BF000244D6; Wed, 30 Oct 2019 13:06:11 +0800 (CST) Received: from architecture4.huawei.com (10.140.130.215) by smtp.huawei.com (10.3.19.208) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 30 Oct 2019 13:06:00 +0800 From: Gao Xiang To: Chao Yu , CC: LKML , Chao Yu , "Pratik Shinde" , Gao Xiang Subject: [PATCH v6] erofs: support superblock checksum Date: Wed, 30 Oct 2019 13:08:46 +0800 Message-ID: <20191030050846.175623-1-gaoxiang25@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191030025616.GB161610@architecture4> References: <20191030025616.GB161610@architecture4> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.140.130.215] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pratik Shinde 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. Signed-off-by: Pratik Shinde Reviewed-by: Chao Yu Signed-off-by: Gao Xiang --- changes since v5: - update commit message which was missed in v5. fs/erofs/Kconfig | 1 + fs/erofs/erofs_fs.h | 3 ++- fs/erofs/internal.h | 2 ++ fs/erofs/super.c | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/fs/erofs/Kconfig b/fs/erofs/Kconfig index 9d634d3a1845..74b0aaa7114c 100644 --- a/fs/erofs/Kconfig +++ b/fs/erofs/Kconfig @@ -3,6 +3,7 @@ config EROFS_FS tristate "EROFS filesystem support" depends on BLOCK + select LIBCRC32C help EROFS (Enhanced Read-Only File System) is a lightweight read-only file system with modern designs (eg. page-sized diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h index b1ee5654750d..385fa49c7749 100644 --- a/fs/erofs/erofs_fs.h +++ b/fs/erofs/erofs_fs.h @@ -11,6 +11,8 @@ #define EROFS_SUPER_OFFSET 1024 +#define EROFS_FEATURE_COMPAT_SB_CHKSUM 0x00000001 + /* * Any bits that aren't in EROFS_ALL_FEATURE_INCOMPAT should * be incompatible with this kernel version. @@ -37,7 +39,6 @@ struct erofs_super_block { __u8 uuid[16]; /* 128-bit uuid for volume */ __u8 volume_name[16]; /* volume name */ __le32 feature_incompat; - __u8 reserved2[44]; }; diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h index 544a453f3076..a3778f597bf6 100644 --- a/fs/erofs/internal.h +++ b/fs/erofs/internal.h @@ -85,6 +85,7 @@ struct erofs_sb_info { u8 uuid[16]; /* 128-bit uuid for volume */ u8 volume_name[16]; /* volume name */ + u32 feature_compat; u32 feature_incompat; unsigned int mount_opt; @@ -426,6 +427,7 @@ static inline void z_erofs_exit_zip_subsystem(void) {} #endif /* !CONFIG_EROFS_FS_ZIP */ #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */ +#define EFSBADCRC EBADMSG /* Bad CRC detected */ #endif /* __EROFS_INTERNAL_H */ diff --git a/fs/erofs/super.c b/fs/erofs/super.c index 0e369494f2f2..2fcf44b656dd 100644 --- a/fs/erofs/super.c +++ b/fs/erofs/super.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "xattr.h" #define CREATE_TRACE_POINTS @@ -46,6 +47,30 @@ void _erofs_info(struct super_block *sb, const char *function, va_end(args); } +static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata) +{ + struct erofs_super_block *dsb; + u32 expected_crc, crc; + + dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, + EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL); + if (!dsb) + return -ENOMEM; + + expected_crc = le32_to_cpu(dsb->checksum); + dsb->checksum = 0; + /* to allow for x86 boot sectors and other oddities. */ + crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET); + kfree(dsb); + + if (crc != expected_crc) { + erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected", + crc, expected_crc); + return -EFSBADCRC; + } + return 0; +} + static void erofs_inode_init_once(void *ptr) { struct erofs_inode *vi = ptr; @@ -112,7 +137,7 @@ static int erofs_read_superblock(struct super_block *sb) sbi = EROFS_SB(sb); - data = kmap_atomic(page); + data = kmap(page); dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET); ret = -EINVAL; @@ -121,6 +146,13 @@ static int erofs_read_superblock(struct super_block *sb) goto out; } + sbi->feature_compat = le32_to_cpu(dsb->feature_compat); + if (sbi->feature_compat & EROFS_FEATURE_COMPAT_SB_CHKSUM) { + ret = erofs_superblock_csum_verify(sb, data); + if (ret) + goto out; + } + blkszbits = dsb->blkszbits; /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */ if (blkszbits != LOG_BLOCK_SIZE) { @@ -155,7 +187,7 @@ static int erofs_read_superblock(struct super_block *sb) } ret = 0; out: - kunmap_atomic(data); + kunmap(data); put_page(page); return ret; } -- 2.17.1