From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oBGaH-0006jJ-1O for mharc-grub-devel@gnu.org; Tue, 12 Jul 2022 10:09:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49366) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oBGa8-0006gr-AX for grub-devel@gnu.org; Tue, 12 Jul 2022 10:09:46 -0400 Received: from out4-smtp.messagingengine.com ([66.111.4.28]:57723) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oBGZt-0006jd-5P for grub-devel@gnu.org; Tue, 12 Jul 2022 10:09:43 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id D29E55C012D; Tue, 12 Jul 2022 10:09:24 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 12 Jul 2022 10:09:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm3; t=1657634964; x=1657721364; bh=Awv6n7m8kn mIAp9FspXwJl8qEMoGxXGc7r6m696HlaY=; b=N2g1LrZMuXZ9QrMfG1ClDoLHYd Ze55gwrZnBL4Oa5qYwjx3TtVNTOy9jajB1vMJoHxj4P2I3RB9XPJslhP2wit7WHy 1rJBW2POYDixiImOE4UnXoyEE3OHwn7n1QMEY1wOzbbGXyQkG2hGnJzzFoKt7iV5 k3w1a9ujB+g0CyfAhyLv4El9TwmyerFO7u4edhsSCrdb4bWr9rIqTvp0s0/oygp9 7rAy+e6bjOX9XD3+xZj1oMKbXl2fMsRjvTEdfeYXSbmuXxa9+dsW3J8Z3CxavbDD Q08ZFlwh5hjnuFRK77L/QUoI04baE5TJqJSLaH+VqSMRXF8J14kuVqoUun/Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1657634964; x=1657721364; bh=Awv6n7m8knmIAp9FspXwJl8qEMoG xXGc7r6m696HlaY=; b=D5n9qaiJMfI8KviOlbG+eeWd42z1LC7CZAHKpCiT6slu 7VhjhhrEYXjnZy59/RYW98yNJPIRqr0ZDKZ7qi+m2kfGtSWnfR1ZS0/K6CIIC7b/ +NEMLitzuYjR8b3EEsPpeTjWuZzYtqJpCAq0sv9jyv7uLiG44xCV/wUxeIfCDUah 5WP8x4X6OBuY7vIpCgheYeSGspbfHmgIFbNPc+g9bBSB3liRbY6huaNwxviJQsIi LLfzgsUPNrSlP5OYHjchnugGYBM29TXK+7NOw08NFjZ5J9ThrKTI2EnYRRGZXu50 KMsn4XUEMGu4VVhNQDZuGEN6BGmUwFYRbaFEI75ZIA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrudejhedgjeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 12 Jul 2022 10:09:23 -0400 (EDT) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id 06f06de2 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 12 Jul 2022 14:09:20 +0000 (UTC) Date: Tue, 12 Jul 2022 16:09:18 +0200 From: Patrick Steinhardt To: Daniel Kiper Cc: grub-devel@gnu.org, Glenn Washburn Subject: Re: [PATCH v5 2/2] luks2: Fix decoding of digests and salts with escaped chars Message-ID: References: <60ccd669d4d7fc24b92e453a9a95f3fd915b1f8e.1657536104.git.ps@pks.im> <20220712133022.ybsq6w3foq5ynlhv@tomti.i.net-space.pl> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ZG75aRNBk4U6ZkLS" Content-Disposition: inline In-Reply-To: <20220712133022.ybsq6w3foq5ynlhv@tomti.i.net-space.pl> Received-SPF: pass client-ip=66.111.4.28; envelope-from=ps@pks.im; helo=out4-smtp.messagingengine.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jul 2022 14:09:48 -0000 --ZG75aRNBk4U6ZkLS Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Jul 12, 2022 at 03:30:22PM +0200, Daniel Kiper wrote: > On Mon, Jul 11, 2022 at 12:44:59PM +0200, Patrick Steinhardt wrote: > > It was reported in the #grub IRC channel on Libera that decryption of > > LUKS2 partitions fails with errors about invalid digests and/or salts. > > In all of these cases, what failed was decoding the Base64 > > representation of these, where the encoded data contained invalid > > characters. > > > > As it turns out, the root cause is that json-c, which is used by > > cryptsetup to read and write the JSON header, will escape some > > characters by prepending a backslash when writing JSON strings by > > default. Most importantly, json-c also escapes the forward slash, which > > is part of the Base64 alphabet. Because GRUB doesn't know to unescape > > such characters, decoding this string will rightfully fail. > > > > Interestingly, this issue has until now only been reported by users of > > Ubuntu 18.04. And a bit of digging in fact reveals that cryptsetup has > > changed the logic in a054206d (Suppress useless slash escaping in json > > lib, 2018-04-20), which has been released with cryptsetup v2.0.3. Ubuntu > > 18.04 is still shipping with cryptsetup v2.0.2 though, which explains > > why this is not a more frequent issue. > > > > Fix the issue by using our new `grub_json_unescape ()` helper function > > that handles unescaping for us. > > > > Reported-by: Afdal >=20 > Any chance to add this guy email here? He didn't want his mail address to be associated with his name here. > > Signed-off-by: Patrick Steinhardt > > --- > > grub-core/disk/luks2.c | 28 ++++++++++++++++++++++++---- > > 1 file changed, 24 insertions(+), 4 deletions(-) > > > > diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c > > index bf741d70f..c24c6e98d 100644 > > --- a/grub-core/disk/luks2.c > > +++ b/grub-core/disk/luks2.c > > @@ -384,6 +384,24 @@ luks2_scan (grub_disk_t disk, grub_cryptomount_arg= s_t cargs) > > return cryptodisk; > > } > > > > +static grub_err_t > > +luks2_base64_decode (const char *in, grub_size_t inlen, grub_uint8_t *= decoded, idx_t *decodedlen) > > +{ > > + grub_size_t unescaped_len =3D 0; > > + char *unescaped =3D NULL; > > + bool successful; > > + > > + if (grub_json_unescape (&unescaped, &unescaped_len, in, inlen) !=3D = GRUB_ERR_NONE) > > + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("could not unescape B= ase64 string")); > > + > > + successful =3D base64_decode (unescaped, (grub_size_t) unescaped_len= , (char *) decoded, decodedlen); >=20 > Now (grub_size_t) cast seems redundant because unescaped_len is defined > as grub_size_t. >=20 > Otherwise patch LGTM. So, if you fix this you can add my RB. Oops, right. Will fix. Patrick > > + grub_free (unescaped); > > + if (!successful) > > + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("could not decode Bas= e64 string")); > > + > > + return GRUB_ERR_NONE; > > +} > > + > > static grub_err_t > > luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key, > > grub_size_t candidate_key_len) > > @@ -395,9 +413,11 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uin= t8_t *candidate_key, > > gcry_err_code_t gcry_ret; > > > > /* Decode both digest and salt */ > > - if (!base64_decode (d->digest, grub_strlen (d->digest), (char *)dige= st, &digestlen)) > > + if (luks2_base64_decode (d->digest, grub_strlen (d->digest), > > + digest, &digestlen) !=3D GRUB_ERR_NONE) > > return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid digest"); > > - if (!base64_decode (d->salt, grub_strlen (d->salt), (char *)salt, &s= altlen)) > > + if (luks2_base64_decode (d->salt, grub_strlen (d->salt), > > + salt, &saltlen) !=3D GRUB_ERR_NONE) > > return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid digest salt"); > > > > /* Configure the hash used for the digest. */ > > @@ -435,8 +455,8 @@ luks2_decrypt_key (grub_uint8_t *out_key, > > gcry_err_code_t gcry_ret; > > grub_err_t ret; > > > > - if (!base64_decode (k->kdf.salt, grub_strlen (k->kdf.salt), > > - (char *)salt, &saltlen)) > > + if (luks2_base64_decode (k->kdf.salt, grub_strlen (k->kdf.salt), > > + salt, &saltlen) !=3D GRUB_ERR_NONE) > > { > > ret =3D grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid keyslot salt= "); > > goto err; >=20 > Daniel --ZG75aRNBk4U6ZkLS Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAmLNgI4ACgkQVbJhu7ck PpRR9w//UU5Bp2s9+U7P7KmcCArwqmJ0xUQZA49x0xjw7e4NgwcDmuePdbt8Unqg pazMYU6oj1epOY5WwGq8NiyCg22nn6SziSNyEdcqv3zZTR4yseo6Ihst21nh4H2p YMN4s3PChQhu5/f9lCc/Crp7WPFtjrBxVLkJTIs4wtMu0cNwEB8AWk8WW2KhhLea 6ARJ4iXlTk8repTNMyNpw5JB37Wof8ZdUortD0ulH/luHWGmRWF6W60Uu2ckP+2n iO2X3NF0S8GHkFszFKI6waayaeOxuPzGSyqOEKRHqH6TYyM2CSrFNsrPCgx8r6cR ydnnKHgi4Y+CzHFIHsu0YOrVccc7rJCk49DrjZyIB+ojqS9aDExaYbxQHaynFpHC mQRAiP0s+2G6k2+f+/2SJBNiK4M17Jll/GS/no9YAKhP+Ww9dnkahUwt1Dj32bLf yQPyLjAzKampKpSJ4/Qhhyzc1Mm5bInvp4yxA2i3qIhFtoZQIgs/liCnrqkFW7tg UJs7lehVyqunysASpiOO4fDjtgGcwufFFsB3IcO3mIUIjeQRb4aDpkX1qoTMJXqI rz8YgN/OAlOJ3M70PaXvYEuNcHur1H5BbzdJYk2NT7oT+rZpz9DZm1Xclckoa5ae IZvWMkd7SlI+fq0lg+T1a2a8CVQTf2+OXjElJ3stShpizN55AXk= =hn+7 -----END PGP SIGNATURE----- --ZG75aRNBk4U6ZkLS--