Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kA4ky-0001i5-Pu for mharc-grub-devel@gnu.org; Mon, 24 Aug 2020 01:10:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49332) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kA4kx-0001hy-RJ for grub-devel@gnu.org; Mon, 24 Aug 2020 01:10:55 -0400 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:44965) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kA4kv-0007Op-GW for grub-devel@gnu.org; Mon, 24 Aug 2020 01:10:55 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 3E01F60F; Mon, 24 Aug 2020 01:10:51 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 24 Aug 2020 01:10:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=p8AfOJB+4oTXMZ586WUEnHOXP6s 2qiz1aaXmgerJMKk=; b=Nvgb4x87evLtKDDT/tfodPuK7iWPhZUzgqejxMT12AT rp9MjCkxORTIEopwwop6LZeQXF3RkGXItYmrqWzWHdHv6V3vjSTi9N+xQI+uZcIy KCHb70KTcvAUA5HfGe77VlZQhn0dnevZTxniQ/WQBOyefAw6vl20NSFvxM/efFgr OBS7BVkuf4kDDIjgQ5rIGFKgqw7Nd0VOYhbsEMuNMwDAYVx2sZTK/+CHajzqy1wT bDEzuCjY6otZS9/Vx2LRN0B8H25vQywvFEw6xq7xDJsUubP+SPfAUanh7x79Lhj3 XlgbPo6iV+XbPIWhHTIickm6G/f4m3q9PYruSP/kbeg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=p8AfOJ B+4oTXMZ586WUEnHOXP6s2qiz1aaXmgerJMKk=; b=p5jMw70rbb7txBjumi92Hx syCCLHUYWSDBU9+jbwZrvroBQVxgRvUQctLB12Xq7DI7nGyof1DbJYpQByopSwT7 9fQHzOrn2WW1xgSKuScKbnslmLx8edP2tE2SL4MMaHojJsVv65wSzY+2LqjKsICj O7zFIoqOYEc3Ly6Q1AnPKhG9yPe6BwelBPmQicOtqedkwNpMYLJhC+TQKyMyvCgL m1eOgaweQWjhA+m4/nTBzBex21ortXnMoAIsDc9Lz5cm/hLe788MkPlTPn8VMthb HwCBNJBnvtrcbxBcxEMixM9EBTQuGSvw7Q1Nrrjmy0HulroEY65U5AENxsjvDQDA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedruddujedgjeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeehgefhtdefueffheekgfffudelffejtdfhvdejkedthfehvdelgfetgfdvtedthfen ucfkphepjeejrdduuddrhedvrdejieenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Received: from vm-mail.pks.im (x4d0b344c.dyn.telefonica.de [77.11.52.76]) by mail.messagingengine.com (Postfix) with ESMTPA id D077D30600B1; Mon, 24 Aug 2020 01:10:49 -0400 (EDT) Received: from localhost (tanuki [10.192.0.23]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id 3d0532c7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 24 Aug 2020 05:10:44 +0000 (UTC) Date: Mon, 24 Aug 2020 07:10:51 +0200 From: Patrick Steinhardt To: Glenn Washburn Cc: grub-devel@gnu.org, Daniel Kiper Subject: Re: [CRYPTO-LUKS v1 03/19] cryptodisk: Incorrect calculation of start sector for grub_disk_read in grub_cryptodisk_read. Message-ID: <20200824051051.GA953@tanuki.pks.im> References: <87e472bcc95950cacefcf9ca60b1ae192d65b748.1596196383.git.development@efficientek.com> <20200823103903.GB100475@ncase.pks.im> <20200823233146.0ca4278f@crass-HP-ZBook-15-G2> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="6c2NcOVqGQ03X4Wi" Content-Disposition: inline In-Reply-To: <20200823233146.0ca4278f@crass-HP-ZBook-15-G2> Received-SPF: pass client-ip=64.147.123.25; envelope-from=ps@pks.im; helo=wout2-smtp.messagingengine.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 01:10:51 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Aug 2020 05:10:56 -0000 --6c2NcOVqGQ03X4Wi Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Aug 23, 2020 at 11:31:46PM -0500, Glenn Washburn wrote: > On Sun, 23 Aug 2020 12:39:03 +0200 > Patrick Steinhardt wrote: >=20 > > On Fri, Jul 31, 2020 at 07:01:44AM -0500, Glenn Washburn wrote: > > > Here dev is a grub_cryptodisk_t and dev->offset is offset in > > > sectors of size native to the cryptodisk device. The sector is > > > correctly transformed into native grub sector size, but then added > > > to dev->offset which is not transformed. It would be nice if the > > > type system would help us with this. > > >=20 > > > Signed-off-by: Glenn Washburn > > > --- > > > grub-core/disk/cryptodisk.c | 4 ++-- > > > 1 file changed, 2 insertions(+), 2 deletions(-) > > >=20 > > > diff --git a/grub-core/disk/cryptodisk.c > > > b/grub-core/disk/cryptodisk.c index d8f66e9ef..2791a4870 100644 > > > --- a/grub-core/disk/cryptodisk.c > > > +++ b/grub-core/disk/cryptodisk.c > > > @@ -757,8 +757,8 @@ grub_cryptodisk_read (grub_disk_t disk, > > > grub_disk_addr_t sector, size, sector, dev->offset); > > > =20 > > > err =3D grub_disk_read (dev->source_disk, > > > - (sector << (disk->log_sector_size > > > - - GRUB_DISK_SECTOR_BITS)) + > > > dev->offset, 0, > > > + ((sector + dev->offset) << > > > (disk->log_sector_size > > > + - > > > GRUB_DISK_SECTOR_BITS)), 0, size << disk->log_sector_size, buf); > > > if (err) > > > { > >=20 > > So for LUKS2, we initialize `crypt->offset` with `crypt->offset =3D > > grub_divmod64 (segment.offset, segment.sector_size, NULL)` where > > `segment.offset` is the offset in bytes and `segment.sector_size` is > > the sector size. So yup, it's in sectors. > >=20 > > For LUKS1, we do `newdev->offset =3D grub_be_to_cpu32 > > (header.payloadOffset)`, which also is in sectors of 512 bytes. > >=20 > > So the fix does seem correct to me, but I think it's incomplete as > > we'd have to do the same for `grub_cryptodisk_write`. >=20 > Yep, good catch. I didn't even think to check for grub_cryptodisk_write > since I tend to think of grub as read-only. I'm actually not sure how > to trigger a disk write in grub. The only thing that comes to mind is > I think there's a way to set some partition flags. =20 I'd assume that things like the grub.env file get updated based e.g. on the last chosen boot entry. Which would require support to write to disk. But I honestly don't now and have never used write-functionality in GRUB. Do you want to update your patch to also fix the write part? I'd then pull it into a v2 of my patch series for v2.06 fixes. > > Out of curiosity: did you test these changes? >=20 > Yes, these changes have definitely been tested. In fact, I found these > bugs precisely because I was trying to get grub to decrypt my LUKS2 > device which has a 4096 byte sector size. Feeling like I wasn't > getting much traction on my patches, I wrote the CRYPTOMOUNT-TEST patch > series to allow independent verification of the bugs and my fixes. > There are a series of tests for block sizes 1024, 2048, and 4096 which > all are successful for me (all the sector size fixes are needed). I'll have to have a look at them. Improved testability is definitely something I'm interested in, mostly because I found the manual compile-boot-test cycle quite unwieldy, even with virtualization. > Now that I'm looking at grub_cryptodisk_read again, it looks like the > GRUB_UTIL part doesn't even take in to account the offset. So that's > probably not working either. I'm not exactly sure how to test that > code right yet either. >=20 > > The offset should always > > be a positive number, except with a detached header. So wouldn't we > > always have hit this bug if this behaviour was indeed wrong? >=20 > Yes, offset can only be zero in a detached header scenario. The key > here to why this issue was never hit is because LUKS1 and grub > native disks have a hardcoded 512-byte sector size (ie. > disk->log_sector_size =3D=3D GRUB_DISK_SECTOR_BITS =3D=3D 9 ). So > (disk->log_sector_size - GRUB_DISK_SECTOR_BITS) =3D=3D 0 and x << 0 =3D= =3D x. > Effectively, the bit shift was always a noop and still is for 512-byte > sector cryptodisks, which is why the LUKS2 support also works in the > default sector-size case of 512-bytes. Cool, thanks a lot for explaining the issue in more depth! Patrick --6c2NcOVqGQ03X4Wi Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAl9DS9sACgkQVbJhu7ck PpTe8xAAoU9j29BVmcd/foerPGH0tNp8m2jOeZ2JNR7B0VAaTwWybiLlJgHvFPCK SyS9Hs8uSFhbVnRtKUKntfiO1bYlxXUcEcnv58wtLnG/1KQcvDDFkmBfyM1DoGpP xsF1WBKXZC+U0S7D1x1ylipi1cvEv/8NKNvEaBJFuAsDePJTqF5MTP6KUNochB/+ rzcaGJchHZjPQ+eNIpZPmHJgfrCh0JyBpW6JFFd2MgKrRigi3jkqCKfmLdhG7O93 PFVKdaMUnLzwLpBVUBj09U5dBwhHbUR4pmo+3Co/Y3A+DsjhHnkqeTvVVKV756gy HDU1l6HZ2CS44/4fxNHXpPq2H4bcMq27fUA47kM3TXZa82QNLdZR0tMzVpwfMU34 +4IJoHvbtjKsYnzaab1c0ja2zxkmu6ntk1tECb/8z+gWjmd+qDwjiHyWFgapA01m 5ePVnlhu0lGg4H65/Oj2y96ELTTadtFrM84NlTSvuXnPeGBJI0afhFzTryH1xnEr pjuaHmFMrrz5ElzrbMNRV/ob0EGKYCRV5wz6QKuw3GtTKp45g387tWHCWjaxl7ur wxCnZ6fDaVdsChW76G4PDOElTs5L7yRakAJyUp7Ac/a5mWyc8hx7o78jfc6DeCx8 Y+/mZwqh9Ol8jsvGywxodsoLV8S+TYmGCbuBgzT0kRo0GGT4t+M= =+c5Q -----END PGP SIGNATURE----- --6c2NcOVqGQ03X4Wi--