Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp5905841ybg; Tue, 22 Oct 2019 10:04:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqyLvxpp9xSG3zfnSMTCA+oJoj5nmq03ExhVgoPpGx0rjU2e0pzPbVu6Y2cgY5S4YugKFaJi X-Received: by 2002:a17:906:1c87:: with SMTP id g7mr21144151ejh.189.1571763852937; Tue, 22 Oct 2019 10:04:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571763852; cv=none; d=google.com; s=arc-20160816; b=bfxUGH8MBtiyL2J4fUTrkUl1GlLf0sWGrO8pg8xWQ3+bIL1pW75Dcec4CORhLABBR7 T7O+6pEUc0JHsvBsVUeD5sDzJ/79RJsEXnKORS4sH5Mz1xKoWfeLvnxIIXLAIqonH7nJ VhJMPdHpJ/yodlqmqVNO/uoADbW4epKpSGLA8899EzUFA7uYAHXiLQooLshkgX+++Bwt KbdBkHrTcIdePzo9UmB3x7i/NqeeCC2Kmo+CxaqvXetfcKNjIO7lz26w8SGQ6+FgPcwE u4FTdHtHB+abRxOYIEBan1MPcS36bDX8YOXR0MqhLIknYzqC2hgARDLDzJRFgrsQd4Ea Zuog== 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=+0awgcrU0T0gJr+sSnKtP3Vx7pwyrZ4ja7Mttfvl/RM=; b=x2M5OiiSbrYQKinmOS+uno92T70/T9UFBeqc9emG/yDy4DCZVBHJ3CF8FHnOAZ9Gkd OF4O1JgyCN1SwnkpiNs0SfeHckOOF0Tbi3THy2aIWTUGMJbslJmsMemo6i4nCztU9srQ 2uyOv2MDOHxkYyc+veTYVgd52onispJbGUy23Q+FBVNmOBi+R4VQ+24lc6DiW4yKiQIY XGdSivgb9BHehJQJh3PpKrJXiiYauGdFF4hT2adqqFKlgR7MCH2tzk0xIaBuPFbu4WYh rwUOrjh/bBrqaQOyZAjluhs7EwIPYzGJc1pvnGpjv/mMd9pmu7Mg83wwhqNq9bvtM6ih SR2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ptV9anfs; 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 17si10514636ejt.431.2019.10.22.10.03.47; Tue, 22 Oct 2019 10:04:12 -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=ptV9anfs; 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 S1732517AbfJVPkL (ORCPT + 99 others); Tue, 22 Oct 2019 11:40:11 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:43567 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730141AbfJVPkK (ORCPT ); Tue, 22 Oct 2019 11:40:10 -0400 Received: by mail-pf1-f193.google.com with SMTP id a2so10880775pfo.10 for ; Tue, 22 Oct 2019 08:40:10 -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=+0awgcrU0T0gJr+sSnKtP3Vx7pwyrZ4ja7Mttfvl/RM=; b=ptV9anfs6GPxZoupqgTAdWXbToAEdpkjso3HaFKaHa87FGi+1V9TCAfkSJBHlxcDl4 KZ/BGcCqnStgrkx4RojbK921FbE80nYktDJMw1d1TfC2I7WMFECpNFfOqliYL/sqqygc o89ONML8Vgzkeu5Jk/23T2PUEJPIl/o3xv0Iq0huanxL+DlJvVHLopetTpNMZCmuEdPI sdjQwc/PZL5aHdSkDfBpCSQPjoWPF8k5h/RBsKoh+2alue0colVNMdmB+gipEu1Jqk6J XU7WDAZ+pe85eRf//yjQEke8MnXe45cdn06w4/gqgN1lTUX/QeEy6hdmLZnWCjz979KY MYHw== 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=+0awgcrU0T0gJr+sSnKtP3Vx7pwyrZ4ja7Mttfvl/RM=; b=kSVfBtyjNwkQ0JE3r/W5LmClNijirupOIryLyM9onRvBQjwMiEmal6JHEguouQpCqV KE2c7ujCroFYQLpg6ymyU5JaXi7PmKNIxQ17JlfvrCD8EEz0XTLa2yC/bsZTGF1qJrs4 aqvTq4+KFbuE9Hw0WKwCucTeOLe24w3zeQXixYqEg5y+nJ51g79Mj6VtQaN6JnoLeqrQ wvUFRxCNGOebYKiuNiTwKYKB+Sn1nBZxviFKObs6ElnnTP/UkMU8c8gAKVluECMs/gZN 4u8aEgoOesb/oc4sntIDqn/QM7dnltIu8CG70AyGEat07T8HOvcD+9M2E5KjMBmSjBhP N3Kg== X-Gm-Message-State: APjAAAWXXnTVaf9g+lZ4GWwDYs5kiayK8/COUdaQqxq8qwcnBHzMV/xx BX8SfH2Ia4UQloXs3hbpstlfZi+Pxl6qAg== X-Received: by 2002:a62:ae06:: with SMTP id q6mr5021963pff.96.1571758810041; Tue, 22 Oct 2019 08:40:10 -0700 (PDT) Received: from localhost.localdomain ([42.107.68.84]) by smtp.gmail.com with ESMTPSA id j10sm19681856pfn.128.2019.10.22.08.40.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Oct 2019 08:40:09 -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-v2] erofs: code for verifying superblock checksum of an erofs image. Date: Tue, 22 Oct 2019 21:09:56 +0530 Message-Id: <20191022153956.16867-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.Used kernel's crc32c library for calculating the checksum. Signed-off-by: Pratik Shinde --- fs/erofs/erofs_fs.h | 5 +++-- fs/erofs/internal.h | 3 ++- fs/erofs/super.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h index b1ee565..4d8097a 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_COMPAT_SB_CHKSUM 0x00000001 /* 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..cec27ca 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 feature_compat; unsigned int mount_opt; }; @@ -426,6 +426,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 found */ #endif /* __EROFS_INTERNAL_H */ diff --git a/fs/erofs/super.c b/fs/erofs/super.c index 0e36949..9cda72d 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,31 @@ 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, nblocks, crc = 0; + void *kaddr; + struct page *page; + int i; + + disk_chksum = le32_to_cpu(dsb->checksum); + nblocks = le32_to_cpu(dsb->chksum_blocks); + dsb->checksum = 0; + for (i = 0; i < nblocks; i++) { + page = erofs_get_meta_page(sb, i); + if (IS_ERR(page)) + return PTR_ERR(page); + kaddr = kmap(page); + crc = crc32c(crc, kaddr, EROFS_BLKSIZ); + kunmap(page); + unlock_page(page); + } + if (crc != disk_chksum) + return -EFSBADCRC; + return 0; +} + static void erofs_inode_init_once(void *ptr) { struct erofs_inode *vi = ptr; @@ -121,6 +147,13 @@ static int erofs_read_superblock(struct super_block *sb) goto out; } + if (dsb->feature_compat & EROFS_FEATURE_COMPAT_SB_CHKSUM) { + ret = erofs_validate_sb_chksum(dsb, sb); + if (ret < 0) { + 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