Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pGFR3-0002UO-DH for mharc-grub-devel@gnu.org; Fri, 13 Jan 2023 03:29:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pGFR1-0002Tn-GN for grub-devel@gnu.org; Fri, 13 Jan 2023 03:29:11 -0500 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pGFQz-0001gZ-Ev for grub-devel@gnu.org; Fri, 13 Jan 2023 03:29:11 -0500 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 89C375C00EA; Fri, 13 Jan 2023 03:29:08 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 13 Jan 2023 03:29:08 -0500 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=1673598548; x=1673684948; bh=MfuYFLVhAB O2ICO54RnuqzQDB0sa1OXp97xLVoTCb7w=; b=WZiYrLy/8dvXHRH95ji+T0pw3R xjx4hyKAg700p3Ipv/S5GDQ3YxNZ6MmEnz+DDKz1w6PdUvdbvfvTeyfLt6MRXWXH c8nI8gpQCCQiUlXwfLXT8VELCg3d4cZUqEN9QtnO6XxTUfZp7/SZRwmYpnpTZp1r hRA31qTZQIH171D8bujyKOm43N/O2OST9oXFQc4Nu+Q0k8g0o2EElpYq56Z3bZWX pvPgRj+WQlt9ZRmu48SGhsUFFZ2EIOyu1MGMfOvXYdAuF1ZZJb5/X2XIWYwc1JhQ TfhuNDH7LdHLtj0zn4ctzGYfWfWxMwvE1z/FdCtLEhalEnoFfzlO/JMpWgwg== 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=1673598548; x=1673684948; bh=MfuYFLVhABO2ICO54RnuqzQDB0sa 1OXp97xLVoTCb7w=; b=lQxOsOimPRZrj+rkmB51FoRZthxXAh+5RxqBJ5epDeVW g7FO0clvQv8ejptjQZJhB//fKbnGxZ/OM3G/vY+24ashMdfILR++3wOaZaMyeeK7 BgeVdM43iN973VAAW6a5NDDZOSx7PHiZ/9i4gv4A1E5O9HMnG6gONrK97Uwwr1hj GCIpvKrtu15vEFOSORcHzLciCZF7lx+k96FF9HKdekY0G0hR3+nlO0uTLThLi54F x0+6xU4BpbLI4L+wQF3SA5K0yEn2xWBu/m4tb/sTPY88B5raNrq+eyABQG1geY4j Upmv3sSIT67Pf8UPL00DHyiPpIa0TQBl2ATVBjIFcA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrleejgdduvddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlvdefmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredt tddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhksh drihhmqeenucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddv feefgeehgfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 13 Jan 2023 03:29:06 -0500 (EST) Received: by pks.im (OpenSMTPD) with ESMTPSA id 98c00af4 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 13 Jan 2023 08:28:53 +0000 (UTC) Date: Fri, 13 Jan 2023 09:29:02 +0100 From: Patrick Steinhardt To: Glenn Washburn Cc: grub-devel@gnu.org, Daniel Kiper , Pierre-Louis Bonicoli , Josselin Poiret , Fabian Vogt Subject: Re: [PATCH 3/4] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters Message-ID: References: <20230112230510.1319896-1-development@efficientek.com> <20230112230510.1319896-4-development@efficientek.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="TDWC6So6PgRr2jky" Content-Disposition: inline In-Reply-To: <20230112230510.1319896-4-development@efficientek.com> Received-SPF: pass client-ip=66.111.4.25; envelope-from=ps@pks.im; helo=out1-smtp.messagingengine.com 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.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Jan 2023 08:29:11 -0000 --TDWC6So6PgRr2jky Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Jan 12, 2023 at 05:05:09PM -0600, Glenn Washburn wrote: > From: Josselin Poiret >=20 > This lets a LUKS2 cryptodisk have its cipher and hash filled out, > otherwise they wouldn't be initialized if cheat mounted. >=20 > Signed-off-by: Josselin Poiret > Tested-by: Glenn Washburn > --- > grub-core/osdep/devmapper/getroot.c | 107 +++++++++++++++++++++++++++- > 1 file changed, 106 insertions(+), 1 deletion(-) >=20 > diff --git a/grub-core/osdep/devmapper/getroot.c b/grub-core/osdep/devmap= per/getroot.c > index 2bf4264cf0..090ac71670 100644 > --- a/grub-core/osdep/devmapper/getroot.c > +++ b/grub-core/osdep/devmapper/getroot.c > @@ -51,6 +51,8 @@ > #include > #include > =20 > +#include > + > static int > grub_util_open_dm (const char *os_dev, struct dm_tree **tree, > struct dm_tree_node **node) > @@ -186,7 +188,6 @@ grub_util_pull_devmapper (const char *os_dev) > && lastsubdev) > { > char *grdev =3D grub_util_get_grub_dev (lastsubdev); > - dm_tree_free (tree); > if (grdev) > { > grub_err_t err; > @@ -194,7 +195,111 @@ grub_util_pull_devmapper (const char *os_dev) > if (err) > grub_util_error (_("can't mount encrypted volume `%s': %s"), > lastsubdev, grub_errmsg); > + if (strncmp (uuid, "CRYPT-LUKS2-", sizeof ("CRYPT-LUKS2-") - 1= ) =3D=3D 0) > + { > + /* > + * set LUKS2 cipher from dm parameters, since it is not > + * possible to determine the correct one without > + * unlocking, as there might be multiple segments. > + */ > + grub_disk_t source; > + grub_cryptodisk_t cryptodisk; > + grub_uint64_t start, length; > + char *target_type; > + char *params; > + const char *name; > + char *cipher, *cipher_mode; > + struct dm_task *dmt; > + char *seek_head, *c; > + unsigned int remaining; > + > + source =3D grub_disk_open (grdev); > + if (! source) > + grub_util_error (_("cannot open grub disk `%s'"), grdev); > + cryptodisk =3D grub_cryptodisk_get_by_source_disk (source); > + if (! cryptodisk) > + grub_util_error (_("cannot get cryptodisk from source di= sk `%s'"), grdev); > + grub_disk_close (source); > + > + /* > + * the following function always returns a non-NULL pointe= r, > + * but the string may be empty if the relevant info is not= present > + */ > + name =3D dm_tree_node_get_name (node); > + if (grub_strlen (name) =3D=3D 0) > + grub_util_error (_("cannot get dm node name for grub dev= `%s'"), grdev); > + > + grub_util_info ("populating parameters of cryptomount `%s'= from DM device `%s'", > + uuid, name); > + > + dmt =3D dm_task_create (DM_DEVICE_TABLE); > + if (dmt =3D=3D NULL) > + grub_util_error (_("can't create dm task DM_DEVICE_TABLE= ")); > + if (dm_task_set_name (dmt, name) =3D=3D 0) > + grub_util_error (_("can't set dm task name to `%s'"), na= me); > + if (dm_task_run (dmt) =3D=3D 0) > + grub_util_error (_("can't run dm task for `%s'"), name); > + /* > + * dm_get_next_target doesn't have any error modes, everyt= hing has > + * been handled by dm_task_run. > + */ > + dm_get_next_target (dmt, NULL, &start, &length, > + &target_type, ¶ms); > + if (strncmp (target_type, "crypt", sizeof ("crypt")) !=3D = 0) > + grub_util_error (_("dm target of type `%s' is not `crypt= '"), target_type); > + > + /* > + * dm target parameters for dm-crypt is > + * [<#op= t_params> ...] > + */ > + c =3D params; > + remaining =3D grub_strlen (c); > + > + /* first, get the cipher name from the cipher */ > + seek_head =3D grub_memchr (c, '-', remaining); > + if (seek_head =3D=3D NULL) > + grub_util_error (_("can't get cipher from dm-crypt param= eters `%s'"), > + params); > + cipher =3D grub_strndup (c, seek_head - c); > + if (cipher =3D=3D NULL) > + grub_util_error (_("could not strndup cipher of length `= %lu'"), seek_head - c); > + remaining -=3D seek_head - c + 1; > + c =3D seek_head + 1; > + > + /* now, the cipher mode */ > + seek_head =3D grub_memchr (c, ' ', remaining); > + if (seek_head =3D=3D NULL) > + grub_util_error (_("can't get cipher mode from dm-crypt = parameters `%s'"), > + params); > + cipher_mode =3D grub_strndup (c, seek_head - c); > + if (cipher_mode =3D=3D NULL) > + grub_util_error (_("could not strndup cipher_mode of len= gth `%lu'"), seek_head - c); > + > + remaining -=3D seek_head - c + 1; > + c =3D seek_head + 1; > + > + err =3D grub_cryptodisk_setcipher (cryptodisk, cipher, cip= her_mode); > + if (err) > + grub_util_error (_("can't set cipher of cryptodisk `%s= ' to `%s' with mode `%s'"), > + uuid, cipher, cipher_mode); > + > + grub_free (cipher); > + grub_free (cipher_mode); > + > + /* > + * This is the only hash usable by PBKDF2, and we don't > + * have Argon2 support yet, so set it by default, > + * otherwise grub-probe would miss the required > + * abstraction > + */ > + cryptodisk->hash =3D grub_crypto_lookup_md_by_name ("sha25= 6"); > + if (cryptodisk->hash =3D=3D NULL) > + grub_util_error (_("can't lookup hash sha256 by name")= ); I had to re-check whether this is actually true, but at least the LUKS2 spec says it's always SHA256. The format allows for a different hashing function, but that does not yet seem to be used according to the spec. We can iterate at a later point as required. Patrick > + dm_task_destroy (dmt); > + } > } > + dm_tree_free (tree); > grub_free (grdev); > } > else > --=20 > 2.34.1 >=20 --TDWC6So6PgRr2jky Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAmPBFk0ACgkQVbJhu7ck PpRREA//Us1A+U0a0boP+IPCDlUsPw/h0PB0A6Wtbg1tiEuY6XVQcyK36PjkR5iG 3boqRhc9l6qBgDQypg0tyidCA8MtymfM9WZaxZ4YdNs+CH0hIn2SQBNWclRhXO6s i/6QWLimrOIesPFFaogH5bhVCfBVjR7jXj+SPCrf1S9asqLu8962B5GHhHJESZnR 7uaoWEmWu+uw0yBc+HvVLFHv7wTEjK33XZy17C2XZQ3n9PM5j4wbP+rRsgHl/jc1 q4f4UUCqhAZLA1CKLVM7tbW3ySeewU5SZ3Z2FZO+YSu31XwoIv43nJHV96fDSz5G 6FqX70+Z4W8dSznvWV2XreFDTPcy2h85+IwJZrbxTvJEbbxnEhGtJplnxfvwovGA d5bn86PXzbM0TrHM8lEEQcCGCLTfxtMr/bb7oPZy8JUs0w78saUndOzDylNn31SN RP8KTLhUgEaOLGTx79P8cLz2RAgZ4VE+/GUkgNva4iDNL5e5cJBvwIxMW7DgkauM vIHUvd27fEvS0c97IMZWfj0dyeu8Be4ZGrQAOH0rmLsA/wrZCvyUISNpTVgvI2p8 RtzSRzr4gub5QL6k2YthPeEbIyKntBKyQSE6idgLOk8Irw4p9xFW2pJ6OL4d2T9l dKKZ2C2BIFrIfqBBKORUawL1nVj5PcgYhdcx9CsiT9jZAPQYWEA= =5RYW -----END PGP SIGNATURE----- --TDWC6So6PgRr2jky--