linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Pedro Falcato <pedro.falcato@gmail.com>
Cc: linux-ext4@vger.kernel.org, "Darrick J. Wong" <djwong@kernel.org>
Subject: Re: Question regarding the use of CRC32c for checksumming
Date: Tue, 27 Jun 2023 21:52:06 -0700	[thread overview]
Message-ID: <20230628045206.GA1908@sol.localdomain> (raw)
In-Reply-To: <CAKbZUD01uR5kfP4=SSfQ111jKsfKi8ojfDZs5CStLD_h5qb5GQ@mail.gmail.com>

Hi Pedro,

On Mon, Jun 26, 2023 at 09:17:10PM +0100, Pedro Falcato wrote:
> Hi,
> 
> (+CC the original author, Darrick)
> I've been investigating (in the context of my EFI ext4 driver) why all
> ext4 checksums appear inverted. After making sure my CRC32c
> implementation was correct and up-to-par with other ones, I looked at
> the fs/ext4 checksumming code, which took me to the implementation of
> ext4_chksum in ext4.h (excuse the gmail whitespace damage):
> 
> >static inline u32 ext4_chksum(struct ext4_sb_info *sbi, u32 crc,
> >       const void *address, unsigned int length)
> >{
> > struct {
> > struct shash_desc shash;
> > char ctx[4];
> > } desc;
> 
> Open coding the crc32c crypto driver's internal state, seemingly to save a call?
> >
> > BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver)!=sizeof(desc.ctx));
> >
> > desc.shash.tfm = sbi->s_chksum_driver;
> > *(u32 *)desc.ctx = crc;
> 
> ...we set the starting CRC
> >
> > BUG_ON(crypto_shash_update(&desc.shash, address, length));
> 
> then call update, which keeps the current internal state in ctx[4]
> >
> > return *(u32 *)desc.ctx;
> 
> and then we never call ->final() (nor ->finup()), which for crc32c would do:
> > put_unaligned_le32(~ctx->crc, out);
> 
> and as such get me the properly "inverted" crc32c I would expect.
> FreeBSD never found this issue as their calculate_crc32c seems borked
> too, and never inverts the result.
> 
> Is my assessment correct? Was ->final() never called on purpose, or is
> it an accident? Or is this merely a CRC32c variation I'm unaware of?
> 
> I'd like to make sure I get all the context on this, before sending
> any kind of documentation patch :)
> 
> Thanks,
> Pedro

As far as I can tell, you are correct that ext4's CRC32C is just a raw CRC.  It
doesn't do the bitwise inversion at either the beginning or end.

IMO, this is a mistake.  In the design of CRCs, doing these inversions is
recommended to strengthen the CRC slightly.

However, it's also a common "mistake" to leave them out, and not too important,
especially if many of the messages checksummed are fixed-length structures.

Yes, if ext4 had used the kernel crypto API "properly", with crypto_shash_init()
+ crypto_shash_update() + crypto_shash_final(), it would have gotten the
inversion at the beginning and end.  (Note, this is true for "crc32c" but not
"crc32".  The crypto API isn't consistent about its CRC conventions.)

But I'd also think of ext4's direct use of crypto_shash_update() as less of ext4
taking a shortcut or hack, and more of ext4 just having to work around the
kernel crypto API being very clunky and inefficient for use cases like this...

- Eric

  reply	other threads:[~2023-06-28  8:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-26 20:17 Question regarding the use of CRC32c for checksumming Pedro Falcato
2023-06-28  4:52 ` Eric Biggers [this message]
2023-06-28 18:58   ` Darrick J. Wong
2023-07-03 19:48     ` Eric Biggers
2023-08-04 21:08       ` Pedro Falcato

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230628045206.GA1908@sol.localdomain \
    --to=ebiggers@kernel.org \
    --cc=djwong@kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=pedro.falcato@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).