Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3384571ybg; Sun, 20 Oct 2019 12:32:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqy+NwsD4hMkJiL/yUL/w4XBP1155UzRddeKdpmYXPzFG7Jo1QNP/ProM/yF/R2/AH2OT24u X-Received: by 2002:a50:890c:: with SMTP id e12mr3009450ede.277.1571599969941; Sun, 20 Oct 2019 12:32:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571599969; cv=none; d=google.com; s=arc-20160816; b=JmjrMecJXiGx/cHhP0/cGEWWJC6xtpwhoSA8m+h4EG6XOKRBCWZ8Tgs/2Q4yrsjgcz lBvNvMODAx4HjdrwTSFyC8mlQkjgLEOHdKrLkmeHg3rUSwJ+tLN7+JRNbRRLExIfMmdJ JMT82fQrQFW0lLPgQ1PxaOJPeiTzxOr/KTIDtLR/Zn75qSzNwINkPy00+aB+DVfLgYko HUk0Daq8UbolmFu03eZHooCItm7SQygS7JEqUNkm+0FFUkyS/xuPnitIX9kZtjsUPt70 vu1Lr52Y2U9o7CZhPEy/SrYB7lfXFQgsVN98ibCYfVPPeGkgTsUowq4y7pjtuVx4vZys Qyyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=nR5/PUycvY3vVFKY1gw3WwFO6bJ14K3sXYLBVXSi9VY=; b=ce69ndZ9U50ZSCAdkQ05lv9irCYVA+vlVNywoU++fcPBVX5X7+QCYwywl5qgjaDeAM duT+INeGLe8SB17734N57ouzZjYj/BO36Hv8ZHJqoGMD2a5E4SYiUuwaQlGdBMh8Baos 4hv1vqTbyM5Fa792v0Any7M+jWZjqbkEkqjjrKfmMibUejNVUvLxLUp/giyA1FQSQ8Cp v2OwZ6yugHzbvXre/luMzmOuLj9MIANu4aK4aF/xmB3D1+yyP1nbDSgCCHjW1x/QUukg 1ew0z5PiIb2qdrtALrcvagjEbsoKnSzbzGhqjBOtspKStGjKu8amcgytHZEz5B6llUl1 WhiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Wjk/GMBQ"; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id oq24si7252804ejb.202.2019.10.20.12.32.25; Sun, 20 Oct 2019 12:32:49 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Wjk/GMBQ"; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726005AbfJTT2u (ORCPT + 99 others); Sun, 20 Oct 2019 15:28:50 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:41952 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725933AbfJTT2u (ORCPT ); Sun, 20 Oct 2019 15:28:50 -0400 Received: by mail-pf1-f194.google.com with SMTP id q7so6943136pfh.8 for ; Sun, 20 Oct 2019 12:28:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=nR5/PUycvY3vVFKY1gw3WwFO6bJ14K3sXYLBVXSi9VY=; b=Wjk/GMBQLsHEzT2/w1aDaC/FhEwRz5PVMC+wyOxAnn+YhUDJzFix/mmNmDYxsOIygj Z5U1v2OphFon6mejTVenFgy4cfkeY3YzrbtTE2WBn8KzgvPyUc9Uao66q7JI6B6SMHaL xcmH0FWJB6Px8d5ILH9yeEWdKP9+EE8BMifIxZ+VAei9LRX5dfpeo7YD04u1RAIeN0bp zlcPXLnkL4vezEXbBewAZXmjPUuRkiMKSw2QNrNWuwXFvnbh16tXx+Szyu7jNoq2FC8d DbOkKX3nuSZ8OwHwUO2gGq1UICUFEbf0JGCiLPaQtWOK70WqId6DZFbFULBAANvKDhvz zDkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=nR5/PUycvY3vVFKY1gw3WwFO6bJ14K3sXYLBVXSi9VY=; b=Ad7956s8ZnwbNOlPSqjOE7kgobNOANwLrULguzP6sEoCTjcOYApSCvjAv5FUCydVAS xovcAL0rWevkQn/Jq6pM+B8DBEVAqr7p+edmhsiaCxIgFJmqxcoHTZ5VY2Cxt1lrnKID ISxx6bbxI2Bz+gfZ7TadN8FXrxs2HeTSGpWsJErgjdmuuGunJm01Fgh6LxPjvqT5/zYa moq1nSTUZxKtZTzaNnBbl0mor2ifVzYcSsZW3+ElXpJjI9Yv5m7tGmyxP+k/tuUtAnG0 eGHYHfAP+IOnoAvIfD3KUkpOw6obSFaaWgBSBLPd93b0Avl/T6FDtAVln60xYwYXGJs9 6RoA== X-Gm-Message-State: APjAAAVXrb6ly+esOAJxj1PviEYuNFCtqynGPHhjW7rym5pUhqzwInu6 LYCdYY6N7N74g4Cu4r7ASNo= X-Received: by 2002:a17:90a:d588:: with SMTP id v8mr23905743pju.51.1571599729521; Sun, 20 Oct 2019 12:28:49 -0700 (PDT) Received: from localhost.localdomain ([103.97.240.176]) by smtp.gmail.com with ESMTPSA id p88sm12494994pjp.22.2019.10.20.12.28.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 20 Oct 2019 12:28:48 -0700 (PDT) From: Pratik Shinde To: linux-erofs@lists.ozlabs.org, gaoxiang25@huawei.com, yuchao0@huawei.com Cc: linux-kernel@vger.kernel.org, Pratik Shinde Subject: [PATCH] erofs: code for verifying superblock checksum of an erofs image. Date: Mon, 21 Oct 2019 00:58:28 +0530 Message-Id: <20191020192828.10772-1-pratikshinde320@gmail.com> X-Mailer: git-send-email 2.9.3 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Patch for kernel side changes of checksum feature.I used kernel's crc32c library for calculating the checksum. Signed-off-by: Pratik Shinde --- 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 #include #include +#include #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