Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp3426918ybx; Sun, 3 Nov 2019 18:48:46 -0800 (PST) X-Google-Smtp-Source: APXvYqzWdiQsHXuLl82ec3b3rZf4Col8W4CSK7EfTLRLPp54qoecORceq5NFF52G+wob+WKfu/OO X-Received: by 2002:a50:ac1c:: with SMTP id v28mr27244883edc.156.1572835726795; Sun, 03 Nov 2019 18:48:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572835726; cv=none; d=google.com; s=arc-20160816; b=B+Os1y14JqrNOdDZnAve1IxrP0jhSYWCXvIQ6sErz4fhTIQ6gKqNZ5I74RDXcFiHD1 nTaNgseHjtdhyyM/kz3RaMgp7mDXx4GHGJ8irzSZn7S1XKaKwiFPDvPsxfXD5uIxJhiC z2Q0C3HSlVftLLlcndCB65D8q+xrmFbzkJvnnIOcj1VwA44NNalsVnKqpePxSAWcCbUp whSAEav3zSmCEQDhwn0cAoMmgUzZHMpGPccrlX9i39gbbMSIjxbz5c7n87IuB8E/0A0m +0ZZXffRniilN1VKY1eu8hM4osv/fbtMfXJWOSzmWwRMK0Nwh0av8eKC1ulIQQRWGxX+ INOA== 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=Bowf8zbd0xWBAGtd66ohypNyzhx6Ktd8VvXMH53nvY0=; b=oVAV61h0Oipf5E843M0cuie3U9ujKlp6QU9KlAu8Z8p6uZSGIxdSFRD0qPZviZGxjN JHAFOMNzxzY+pD47cdqqDWpSmW/Og7gRl6LOJgb8LwlrPSKaBPAIzqbYIVHAx5vJMoE3 EJ3kwXO30MiyZtFGAMf5mfGts9lTQy7aKHF1QrXaYWTvA9VesPRw+Cz62k3F1ELGh1Cd WWYmRXbtQ8B9PDsPaVNl52VYtdzYTul4d58KopYZ44GAzmELAqlOIYP3NuDoLDAAsqk7 WTNhN7p4F3uT+H+u4Di9hlZOEMUr+hyTcUyN8+lbej8PzSX7czgqrFyN1p+N0LnGEOeL f8sQ== 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 y6si10133014ejo.397.2019.11.03.18.48.20; Sun, 03 Nov 2019 18:48:46 -0800 (PST) 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 S1728904AbfKDCrJ (ORCPT + 99 others); Sun, 3 Nov 2019 21:47:09 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:49692 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728227AbfKDCrI (ORCPT ); Sun, 3 Nov 2019 21:47:08 -0500 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id DBFE7DE1AF02E49B7528; Mon, 4 Nov 2019 10:47:06 +0800 (CST) Received: from architecture4.huawei.com (10.140.130.215) by smtp.huawei.com (10.3.19.213) with Microsoft SMTP Server (TLS) id 14.3.439.0; Mon, 4 Nov 2019 10:46:56 +0800 From: Gao Xiang To: Chao Yu , CC: Pratik Shinde , Dan Carpenter , LKML , Gao Xiang Subject: [PATCH v7] erofs: support superblock checksum Date: Mon, 4 Nov 2019 10:49:37 +0800 Message-ID: <20191104024937.113939-1-gaoxiang25@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191030050846.175623-1-gaoxiang25@huawei.com> References: <20191030050846.175623-1-gaoxiang25@huawei.com> 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. Link: https://lore.kernel.org/r/20191030050846.175623-1-gaoxiang25@huawei.com Signed-off-by: Pratik Shinde Reviewed-by: Chao Yu Cc: Dan Carpenter Signed-off-by: Gao Xiang --- changes since v6: - fix kunmap(data) to kunmap(page) by mistake reported by Dan Carpenter; fs/erofs/Kconfig | 1 + fs/erofs/erofs_fs.h | 3 ++- fs/erofs/internal.h | 1 + fs/erofs/super.c | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 38 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..96d97eab88e3 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; diff --git a/fs/erofs/super.c b/fs/erofs/super.c index 0e369494f2f2..849c0bdf49d9 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 -EBADMSG; + } + 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(page); put_page(page); return ret; } -- 2.17.1