diff mbox series

[1/4] tcgbios: Change format of S_CRTM_VERSION string to ucs-2

Message ID 20210615144115.2113484-2-stefanb@linux.ibm.com
State Superseded
Headers show
Series tcgbios: Use the proper hashes for the TPM 2 PCR banks | expand

Commit Message

Stefan Berger June 15, 2021, 2:41 p.m. UTC
Change the format of the S_CRTM_VERSION string to ucs-2 since this
is what seems to be commonly used by other firmwares following
insight from a TCG work group member.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 lib/libtpm/tcgbios.c | 45 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 8 deletions(-)

Comments

Alexey Kardashevskiy June 28, 2021, 7:31 a.m. UTC | #1
On 6/16/21 00:41, Stefan Berger wrote:
> Change the format of the S_CRTM_VERSION string to ucs-2 since this
> is what seems to be commonly used by other firmwares following
> insight from a TCG work group member.
> 
> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> ---
>   lib/libtpm/tcgbios.c | 45 ++++++++++++++++++++++++++++++++++++--------
>   1 file changed, 37 insertions(+), 8 deletions(-)
> 
> diff --git a/lib/libtpm/tcgbios.c b/lib/libtpm/tcgbios.c
> index d3deccb..6ba4873 100644
> --- a/lib/libtpm/tcgbios.c
> +++ b/lib/libtpm/tcgbios.c
> @@ -1091,6 +1091,29 @@ uint32_t tpm_measure_gpt(void)
>   					  (const uint8_t *)uefi_gpt_data, sz);
>   }
>   
> +/* convert a normal string of given length into a ucs-2 string */
> +static char *string2ucs2(const char *str, size_t str_length,
> +			 size_t *ucs2_length)
> +{
> +	char *ucs2;
> +	size_t i;
> +
> +	if (str_length > 0) {
> +		*ucs2_length = str_length * 2;
> +		if (str[str_length - 1] != 0)
> +			*ucs2_length += 2;

If the source string was not null terminated, you add 2 bytes for the 0 
in the destination string but you do not seem to write the actual 0 at 
the end.


> +	} else {
> +		*ucs2_length = 2;
> +	}
> +	ucs2 = SLOF_alloc_mem(*ucs2_length);
> +	if (ucs2) {
> +		memset(ucs2, 0, *ucs2_length);
> +		for (i = 0; i < str_length; i++)
> +			ucs2[i * 2] = str[i];

(a nit) may be "ucs2[i * 2 + 1] = 0" and drop the memset?


> +	}


if ucs2==NULL, *ucs2_length won't be 0 but it should.


> +	return ucs2;
> +}
> +
>   uint32_t tpm_measure_scrtm(void)
>   {
>   	uint32_t rc;
> @@ -1100,19 +1123,25 @@ uint32_t tpm_measure_scrtm(void)
>   	char *slof_text_start = (char *)&_slof_text;
>   	uint32_t slof_text_length = (long)&_slof_text_end - (long)&_slof_text;
>   	const char *scrtm = "S-CRTM Contents";
> +	char *ucs2_version;
> +	size_t ucs2_length;
>   
>   	version_end = strchr(version_start, '\r');
>   	version_length = version_end - version_start;
>   

I am looking at this version_start/end and thinking can we just use the 
RELEASE macro instead (this is what board-qemu/slof/version.S uses 
anyway)? And then L##RELEASE (or whatever the preprocessor syntax is for 
gluing "L" and a macro).


> -	dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
> -		version_start, version_length);
> +	ucs2_version = string2ucs2(version_start, version_length, &ucs2_length);
> +	if (ucs2_version) {
> +		dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
> +			ucs2_version, ucs2_length);
>   
> -	rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
> -					version_start, version_length,
> -					(uint8_t *)version_start,
> -					version_length);
> -	if (rc)
> -		return rc;
> +		rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
> +						ucs2_version, ucs2_length,
> +						(uint8_t *)ucs2_version,
> +						ucs2_length);
> +		SLOF_free_mem(ucs2_version, ucs2_length);
> +		if (rc)
> +			return rc;
> +	}
>   
>   	dprintf("Measure S-CRTM Content (text): start = %p, length = %d\n",
>   		slof_text_start, slof_text_length);
>
diff mbox series

Patch

diff --git a/lib/libtpm/tcgbios.c b/lib/libtpm/tcgbios.c
index d3deccb..6ba4873 100644
--- a/lib/libtpm/tcgbios.c
+++ b/lib/libtpm/tcgbios.c
@@ -1091,6 +1091,29 @@  uint32_t tpm_measure_gpt(void)
 					  (const uint8_t *)uefi_gpt_data, sz);
 }
 
+/* convert a normal string of given length into a ucs-2 string */
+static char *string2ucs2(const char *str, size_t str_length,
+			 size_t *ucs2_length)
+{
+	char *ucs2;
+	size_t i;
+
+	if (str_length > 0) {
+		*ucs2_length = str_length * 2;
+		if (str[str_length - 1] != 0)
+			*ucs2_length += 2;
+	} else {
+		*ucs2_length = 2;
+	}
+	ucs2 = SLOF_alloc_mem(*ucs2_length);
+	if (ucs2) {
+		memset(ucs2, 0, *ucs2_length);
+		for (i = 0; i < str_length; i++)
+			ucs2[i * 2] = str[i];
+	}
+	return ucs2;
+}
+
 uint32_t tpm_measure_scrtm(void)
 {
 	uint32_t rc;
@@ -1100,19 +1123,25 @@  uint32_t tpm_measure_scrtm(void)
 	char *slof_text_start = (char *)&_slof_text;
 	uint32_t slof_text_length = (long)&_slof_text_end - (long)&_slof_text;
 	const char *scrtm = "S-CRTM Contents";
+	char *ucs2_version;
+	size_t ucs2_length;
 
 	version_end = strchr(version_start, '\r');
 	version_length = version_end - version_start;
 
-	dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
-		version_start, version_length);
+	ucs2_version = string2ucs2(version_start, version_length, &ucs2_length);
+	if (ucs2_version) {
+		dprintf("Measure S-CRTM Version: addr = %p, length = %d\n",
+			ucs2_version, ucs2_length);
 
-	rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
-					version_start, version_length,
-					(uint8_t *)version_start,
-					version_length);
-	if (rc)
-		return rc;
+		rc = tpm_add_measurement_to_log(0, EV_S_CRTM_VERSION,
+						ucs2_version, ucs2_length,
+						(uint8_t *)ucs2_version,
+						ucs2_length);
+		SLOF_free_mem(ucs2_version, ucs2_length);
+		if (rc)
+			return rc;
+	}
 
 	dprintf("Measure S-CRTM Content (text): start = %p, length = %d\n",
 		slof_text_start, slof_text_length);