Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1k9n79-0005y8-FB for mharc-grub-devel@gnu.org; Sun, 23 Aug 2020 06:20:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35668) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k9n76-0005xy-Va for grub-devel@gnu.org; Sun, 23 Aug 2020 06:20:36 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:46347) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k9n74-0003pd-TV for grub-devel@gnu.org; Sun, 23 Aug 2020 06:20:36 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 298175C00C8; Sun, 23 Aug 2020 06:20:32 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sun, 23 Aug 2020 06:20:32 -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=IXID08g1s+yVxGsLFls/iGycTZg Diiw1Jq4wTY7sy4E=; b=VXxD07/5cjni6ZzDNY7GoE0KjFk3feyRt0WWVXWPSVD K1EJElVKZYjf7dSRsE+rZJsoMrYd9wlqfRVy/U/Myu+p0AS/zShMQGGzVrXEfcDd nEpoNrqf9lT1VLjuTza5asCM4wfpWfs8r5V9RuPr1263txp4bGjy0rShnpRE80Ez L4IEHrlngimZbb6646pOe72ALP7d5Reu7E/FrEEPIWkyQuzdo5bARU9uxHPKOcr6 nuMPFk4HE/dfEkw8L5HvQ8VxzrQSGStVKXLoFO1/zvhkED8iszadkH7tVPqXQ/+0 ipCG2c0hhMJsTaQcDTPXf+gpYYsBzfvAnpk3unMfKhg== 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=IXID08 g1s+yVxGsLFls/iGycTZgDiiw1Jq4wTY7sy4E=; b=W3qxxNU1AKK/GQro+SJRDs zI2CGYXns2XC24IgveeTc4gDeN/ohVEqh0ubEMrxXnfL7coySTSICUrCVQIMR2X4 0fHsZ6zAHaCjnwUfiWHtgvdnlizdw7zq9RYAOjvp5sWPKr1u8ItSzaCx0rtZ5VDZ Um2MhL8kC44mORHDhgI+OOpGORezbLT2kXs5wAJkJLRgH0x+bD1g9b+mDe2+r30C uQPoD6C/O7hXRdaY+h/5fMGaeuUamT0vxjhcf9nHqw0cQ4PuGAyKjbCM/MFg20ad J19JtLk8RKjuKAel3A4vU12y5YUsYUyKYpACpzhLLOqV2CkCdd6Gc0FLG5zei+2A == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrudduiedgvdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeehgefhtdefueffheekgfffudelffejtdfhvdejkedthfehvdelgfetgfdvtedthfen ucfkphepjeejrddukeefrdehkedrudelheenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Received: from vm-mail.pks.im (x4db73ac3.dyn.telefonica.de [77.183.58.195]) by mail.messagingengine.com (Postfix) with ESMTPA id 179393280059; Sun, 23 Aug 2020 06:20:30 -0400 (EDT) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id 7ee33a1d (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 23 Aug 2020 10:20:26 +0000 (UTC) Date: Sun, 23 Aug 2020 12:20:28 +0200 From: Patrick Steinhardt To: Glenn Washburn Cc: grub-devel@gnu.org, Daniel Kiper Subject: Re: [CRYPTO-LUKS v1 07/19] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors. Message-ID: <20200823102028.GA100475@ncase.pks.im> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="RnlQjJ0d97Da+TV1" Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=66.111.4.25; envelope-from=ps@pks.im; helo=out1-smtp.messagingengine.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/23 06:20:32 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.001, RCVD_IN_MSPIKE_WL=0.001, 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: Sun, 23 Aug 2020 10:20:37 -0000 --RnlQjJ0d97Da+TV1 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jul 31, 2020 at 07:01:48AM -0500, Glenn Washburn wrote: > The total_length field is named confusingly because length usually > refers to bytes, whereas in this case its really the total number of > sectors on the device. Also counter-intuitively, grub_disk_get_size > returns the total number of device native sectors sectors. We need to > convert the sectors from the size of the underlying device to the > cryptodisk sector size. Is this actually true in all cases? ``` grub_uint64_t grub_disk_get_size (grub_disk_t disk) { if (disk->partition) return grub_partition_get_len (disk->partition); else if (disk->total_sectors !=3D GRUB_DISK_SIZE_UNKNOWN) return disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR= _BITS); else return GRUB_DISK_SIZE_UNKNOWN; } static inline grub_uint64_t grub_partition_get_len (const grub_partition_t p) { return p->len; } /* Partition description. */ struct grub_partition { =2E.. /* The length in sector units. */ grub_uint64_t len; }; ``` So for partitions, this does seem correct as it'll return the number of sectors. But if it's not a partition, we return `disk->total_sectors` shifted by the sector size. Which is the length in bytes, right? So to me, it does rather feel like `grub_disk_get_size` is actually wrong. Naturally, there's no documentation stating what the function is supposed to do, but judging by its name it should've returned the number of bytes and not the number of sectors. I think the fix should rather be: diff --git a/grub-core/kern/disk.c b/grub-core/kern/disk.c index ffb09c8ee..f57cf99a1 100644 --- a/grub-core/kern/disk.c +++ b/grub-core/kern/disk.c @@ -536,7 +536,7 @@ grub_uint64_t grub_disk_get_size (grub_disk_t disk) { if (disk->partition) - return grub_partition_get_len (disk->partition); + return grub_partition_get_len (disk->partition) << (disk->log_sector_s= ize - GRUB_DISK_SECTOR_BITS); else if (disk->total_sectors !=3D GRUB_DISK_SIZE_UNKNOWN) return disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTO= R_BITS); else I'll send a patch to address this finding. Patrick > And > segment.size is in bytes which need to be converted to cryptodisk sectors. >=20 > Signed-off-by: Glenn Washburn > --- > grub-core/disk/luks2.c | 11 ++++++++--- > 1 file changed, 8 insertions(+), 3 deletions(-) >=20 > diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c > index 9f58780a2..41329f745 100644 > --- a/grub-core/disk/luks2.c > +++ b/grub-core/disk/luks2.c > @@ -416,7 +416,7 @@ luks2_decrypt_key (grub_uint8_t *out_key, > grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN]; > grub_uint8_t *split_key =3D NULL; > grub_size_t saltlen =3D sizeof (salt); > - char cipher[32], *p;; > + char cipher[32], *p; > const gcry_md_spec_t *hash; > gcry_err_code_t gcry_ret; > grub_err_t ret; > @@ -602,9 +602,14 @@ luks2_recover_key (grub_disk_t source, > crypt->log_sector_size =3D sizeof (unsigned int) * 8 > - __builtin_clz ((unsigned int) segment.sector_size) - 1; > if (grub_strcmp (segment.size, "dynamic") =3D=3D 0) > - crypt->total_length =3D grub_disk_get_size (source) - crypt->offset; > + /* > + * Convert to cryptodisk sized sectors from source disk sized sectors > + * before subtracting the offset, which is in cryptodisk sized sectors. > + */ > + crypt->total_length =3D (grub_disk_get_size (source) >> (crypt->log_sec= tor_size - source->log_sector_size)) > + - crypt->offset; > else > - crypt->total_length =3D grub_strtoull (segment.size, NULL, 10); > + crypt->total_length =3D grub_strtoull (segment.size, NULL, 10) >> crypt= ->log_sector_size; > =20 > ret =3D luks2_decrypt_key (candidate_key, source, crypt, &keyslot, > (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); > --=20 > 2.25.1 >=20 --RnlQjJ0d97Da+TV1 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAl9CQuwACgkQVbJhu7ck PpSc8g//WXTZ8K3a7zCUuWDhCa/uhgGX6bHeD9KCOsxknNUSsARVw/qhE63KNuIu u5c6V5kLRneYYmN86QGkPUCaB2CVl8cUTH+JKo3DifhbvRCCkiPHaDkv7dtq/YQ4 qOMNi1M1aPV4cSfeuTJBMIAZ+GsOdLJcLsYE4dxMH0tMJ0m0qaRr88ZeGxHfTJzM CFVZJgHj3YgkcWcxh2LX5r/+Apfbh6ghHlkrcrdNmuRSjDurgHqRsJzqC7npKnVI rcafg9AnWSz/miOkGKf65aB13xrDkuL9j9bDPms4EwLnuBJGXiaLlKSnfsy78inn cz10coQp7NdaNdpLGEmfOV7B+GU1MuFmuPJDS/kioyMDjrSsS6UFWbolOwx74uof m+8e77EcCzIriv/FypCytPbvk1VNWa04EEM927+Z+TmaPJRoHlNuYk0Yp2W3M1HP ze3tPcfI1U2S1O9itOXTq9NJCFmUwGk/Tm6SYyp0rHjM+r2xNGX0L60yON1XRV7c 77+zgyR68O4JCprpjugkRNbxUbIHtn8HyntE0nqTB0D/ItOU0mYpT9MxMIylOFwY ze/C2r3MAt33G4xeOApg7jSIBPS2mGWR7cVnLKjnP2s2StXoHLC4Rjt7tsW9uibc UkJyP70P6zbPQHKzK61WTiysb1c44Nt4uhkci+ssGI+CQrZhgxg= =KJp7 -----END PGP SIGNATURE----- --RnlQjJ0d97Da+TV1--