linux-integrity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: Stefano Garzarella <sgarzare@redhat.com>
Cc: linux-integrity@vger.kernel.org, dpsmith@apertussolutions.com,
	ross.philipson@oracle.com,
	Jarkko Sakkinen <jarkko.sakkinen@opinsys.com>,
	Stefan Berger <stefanb@linux.ibm.com>,
	Peter Huewe <peterhuewe@gmx.de>, Jason Gunthorpe <jgg@ziepe.ca>,
	David Howells <dhowells@redhat.com>,
	Paul Moore <paul@paul-moore.com>,
	James Morris <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	James Bottomley <James.Bottomley@hansenpartnership.com>,
	Mimi Zohar <zohar@linux.ibm.com>,
	open list <linux-kernel@vger.kernel.org>,
	"open list:KEYS/KEYRINGS" <keyrings@vger.kernel.org>,
	"open list:SECURITY SUBSYSTEM"
	<linux-security-module@vger.kernel.org>
Subject: Re: [PATCH v3 10/10] tpm-buf: Enable managed and stack allocations.
Date: Tue, 30 Sep 2025 16:11:00 +0300	[thread overview]
Message-ID: <aNvW5KMUO2C0i233@kernel.org> (raw)
In-Reply-To: <u7zay2gb3dff4ptbh34qw7ini63ar3246ivd4xnxtdxc6ijktx@lutatpeg7f7z>

On Tue, Sep 30, 2025 at 02:44:52PM +0200, Stefano Garzarella wrote:
> On Mon, Sep 29, 2025 at 10:48:32PM +0300, Jarkko Sakkinen wrote:
> > From: Jarkko Sakkinen <jarkko.sakkinen@opinsys.com>
> > 
> > Decouple kzalloc from buffer creation, so that  a managed allocation can be
> > done trivially:
> > 
> > 	struct tpm_buf *buf __free(kfree) buf = kzalloc(TPM_BUFSIZE,
>                                                         ^
> In the code, we use PAGE_SIZE instead of TPM_BUFSIZE with kzalloc().
> Should we do the same in this example? (Perhaps adding the reason, if you
> think it would be useful)

I think that should be fixed up in the patch and use TPM_BUFSIZE
consistently for kzallocs. Thanks for the remark.

> 
> > 							GFP_KERNEL);
> > 	if (!buf)
> > 		return -ENOMEM;
> > 	tpm_buf_init(buf, TPM_BUFSIZE);
> > 
> > Alternatively, stack allocations are also possible:
> > 
> > 	u8 buf_data[512];
> > 	struct tpm_buf *buf = (struct tpm_buf *)buf_data;
> > 	tpm_buf_init(buf, sizeof(buf_data));
> > 
> > Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
> > Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@opinsys.com>
> > ---
> > v3:
> > - A new patch from the earlier series with more scoped changes and
> >  less abstract commit message.
> > ---
> > drivers/char/tpm/tpm-buf.c                | 122 +++++----
> > drivers/char/tpm/tpm-sysfs.c              |  20 +-
> > drivers/char/tpm/tpm.h                    |   1 -
> > drivers/char/tpm/tpm1-cmd.c               | 147 +++++------
> > drivers/char/tpm/tpm2-cmd.c               | 290 ++++++++++------------
> > drivers/char/tpm/tpm2-sessions.c          | 121 +++++----
> > drivers/char/tpm/tpm2-space.c             |  44 ++--
> > drivers/char/tpm/tpm_vtpm_proxy.c         |  30 +--
> > include/linux/tpm.h                       |  18 +-
> > security/keys/trusted-keys/trusted_tpm1.c |  34 ++-
> > security/keys/trusted-keys/trusted_tpm2.c | 176 ++++++-------
> > 11 files changed, 482 insertions(+), 521 deletions(-)
> > 
> > diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c
> > index c9e6e5d097ca..1cb649938c01 100644
> > --- a/drivers/char/tpm/tpm-buf.c
> > +++ b/drivers/char/tpm/tpm-buf.c
> > @@ -7,82 +7,109 @@
> > #include <linux/module.h>
> > #include <linux/tpm.h>
> > 
> > -/**
> > - * tpm_buf_init() - Allocate and initialize a TPM command
> > - * @buf:	A &tpm_buf
> > - * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
> > - * @ordinal:	A command ordinal
> > - *
> > - * Return: 0 or -ENOMEM
> > - */
> > -int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
> > +static void __tpm_buf_size_invariant(struct tpm_buf *buf, u16 buf_size)
> > {
> > -	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
> > -	if (!buf->data)
> > -		return -ENOMEM;
> > -
> > -	tpm_buf_reset(buf, tag, ordinal);
> > -	return 0;
> > +	u32 buf_size_2 = (u32)buf->capacity + (u32)sizeof(*buf);
> > +
> > +	if (!buf->capacity) {
> > +		if (buf_size > TPM_BUFSIZE) {
> > +			WARN(1, "%s: size overflow: %u\n", __func__, buf_size);
> > +			buf->flags |= TPM_BUF_INVALID;
> > +		}
> > +	} else {
> > +		if (buf_size != buf_size_2) {
> > +			WARN(1, "%s: size mismatch: %u != %u\n", __func__, buf_size,
> > +			     buf_size_2);
> > +			buf->flags |= TPM_BUF_INVALID;
> > +		}
> > +	}
> > }
> > -EXPORT_SYMBOL_GPL(tpm_buf_init);
> > 
> > -/**
> > - * tpm_buf_reset() - Initialize a TPM command
> > - * @buf:	A &tpm_buf
> > - * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
> > - * @ordinal:	A command ordinal
> > - */
> > -void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
> > +static void __tpm_buf_reset(struct tpm_buf *buf, u16 buf_size, u16 tag, u32 ordinal)
> > {
> > 	struct tpm_header *head = (struct tpm_header *)buf->data;
> > 
> > +	__tpm_buf_size_invariant(buf, buf_size);
> > +
> > +	if (buf->flags & TPM_BUF_INVALID)
> > +		return;
> > +
> > 	WARN_ON(tag != TPM_TAG_RQU_COMMAND && tag != TPM2_ST_NO_SESSIONS &&
> > 		tag != TPM2_ST_SESSIONS && tag != 0);
> > 
> > 	buf->flags = 0;
> > 	buf->length = sizeof(*head);
> > +	buf->capacity = buf_size - sizeof(*buf);
> > +	buf->handles = 0;
> > 	head->tag = cpu_to_be16(tag);
> > 	head->length = cpu_to_be32(sizeof(*head));
> > 	head->ordinal = cpu_to_be32(ordinal);
> > +}
> > +
> > +static void __tpm_buf_reset_sized(struct tpm_buf *buf, u16 buf_size)
> > +{
> > +	__tpm_buf_size_invariant(buf, buf_size);
> > +
> > +	if (buf->flags & TPM_BUF_INVALID)
> > +		return;
> > +
> > +	buf->flags = TPM_BUF_TPM2B;
> > +	buf->length = 2;
> > +	buf->capacity = buf_size - sizeof(*buf);
> > 	buf->handles = 0;
> > +	buf->data[0] = 0;
> > +	buf->data[1] = 0;
> > }
> > -EXPORT_SYMBOL_GPL(tpm_buf_reset);
> > 
> > /**
> > - * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer
> > - * @buf:	A @tpm_buf
> > - *
> > - * Return: 0 or -ENOMEM
> > + * tpm_buf_init() - Initialize a TPM command
> > + * @buf:	A &tpm_buf
> > + * @buf_size:	Size of the buffer.
> >  */
> > -int tpm_buf_init_sized(struct tpm_buf *buf)
> > +void tpm_buf_init(struct tpm_buf *buf, u16 buf_size)
> > {
> > -	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
> > -	if (!buf->data)
> > -		return -ENOMEM;
> > +	memset(buf, 0, buf_size);
> > +	__tpm_buf_reset(buf, buf_size, TPM_TAG_RQU_COMMAND, 0);
> > +}
> > +EXPORT_SYMBOL_GPL(tpm_buf_init);
> > 
> > -	tpm_buf_reset_sized(buf);
> > -	return 0;
> > +/**
> > + * tpm_buf_init_sized() - Initialize a sized buffer
> > + * @buf:	A &tpm_buf
> > + * @buf_size:	Size of the buffer.
> > + */
> > +void tpm_buf_init_sized(struct tpm_buf *buf, u16 buf_size)
> > +{
> > +	memset(buf, 0, buf_size);
> > +	__tpm_buf_reset_sized(buf, buf_size);
> > }
> > EXPORT_SYMBOL_GPL(tpm_buf_init_sized);
> > 
> > /**
> > - * tpm_buf_reset_sized() - Initialize a sized buffer
> > + * tpm_buf_reset() - Re-initialize a TPM command
> >  * @buf:	A &tpm_buf
> > + * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
> > + * @ordinal:	A command ordinal
> >  */
> > -void tpm_buf_reset_sized(struct tpm_buf *buf)
> > +void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
> > {
> > -	buf->flags = TPM_BUF_TPM2B;
> > -	buf->length = 2;
> > -	buf->data[0] = 0;
> > -	buf->data[1] = 0;
> > +	u16 buf_size = buf->capacity + sizeof(*buf);
> > +
> > +	__tpm_buf_reset(buf, buf_size, tag, ordinal);
> > }
> > -EXPORT_SYMBOL_GPL(tpm_buf_reset_sized);
> > +EXPORT_SYMBOL_GPL(tpm_buf_reset);
> > 
> > -void tpm_buf_destroy(struct tpm_buf *buf)
> > +/**
> > + * tpm_buf_reset_sized() - Re-initialize a sized buffer
> > + * @buf:	A &tpm_buf
> > + */
> > +void tpm_buf_reset_sized(struct tpm_buf *buf)
> > {
> > -	free_page((unsigned long)buf->data);
> > +	u16 buf_size = buf->capacity + sizeof(*buf);
> > +
> > +	__tpm_buf_reset_sized(buf, buf_size);
> > }
> > -EXPORT_SYMBOL_GPL(tpm_buf_destroy);
> > +EXPORT_SYMBOL_GPL(tpm_buf_reset_sized);
> > 
> > /**
> >  * tpm_buf_length() - Return the number of bytes consumed by the data
> > @@ -92,6 +119,9 @@ EXPORT_SYMBOL_GPL(tpm_buf_destroy);
> >  */
> > u32 tpm_buf_length(struct tpm_buf *buf)
> 
> Should we update the return value to u16?
> 
> > {
> > +	if (buf->flags & TPM_BUF_INVALID)
> > +		return 0;
> > +
> > 	return buf->length;
> > }
> > EXPORT_SYMBOL_GPL(tpm_buf_length);
> > @@ -104,10 +134,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_length);
> >  */
> > void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length)
> > {
> > +	u32 total_length = (u32)buf->length + (u32)new_length;
> > +
> > 	if (buf->flags & TPM_BUF_INVALID)
> > 		return;
> > 
> > -	if ((buf->length + new_length) > PAGE_SIZE) {
> > +	if (total_length > (u32)buf->capacity) {
> > 		WARN(1, "tpm_buf: write overflow\n");
> > 		buf->flags |= TPM_BUF_INVALID;
> > 		return;
> 
> [...]
> 
> > diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> > index 636acb66a4f6..6e6a9fb48e63 100644
> > --- a/security/keys/trusted-keys/trusted_tpm1.c
> > +++ b/security/keys/trusted-keys/trusted_tpm1.c
> > @@ -310,9 +310,8 @@ static int TSS_checkhmac2(unsigned char *buffer,
> >  * For key specific tpm requests, we will generate and send our
> >  * own TPM command packets using the drivers send function.
> >  */
> > -static int trusted_tpm_send(unsigned char *cmd, size_t buflen)
> > +static int trusted_tpm_send(void *cmd, size_t buflen)
> > {
> > -	struct tpm_buf buf;
> > 	int rc;
> > 
> > 	if (!chip)
> > @@ -322,15 +321,12 @@ static int trusted_tpm_send(unsigned char *cmd, size_t buflen)
> > 	if (rc)
> > 		return rc;
> > 
> > -	buf.flags = 0;
> > -	buf.length = buflen;
> > -	buf.data = cmd;
> > 	dump_tpm_buf(cmd);
> > -	rc = tpm_transmit_cmd(chip, &buf, 4, "sending data");
> > +	rc = tpm_transmit_cmd(chip, cmd, 4, "sending data");
> 
> Is it fine here to remove the intermediate tpm_buf ?
> 
> IIUC tpm_transmit_cmd() needs a tpm_buf, while here we are passing just
> the "data", or in some way it's a nested tpm_buf?
> 
> (Sorry if it's a stupid question, but I'm still a bit new with this code).
> 
> > 	dump_tpm_buf(cmd);
> > 
> > +	/* Convert TPM error to -EPERM. */
> > 	if (rc > 0)
> > -		/* TPM error */
> > 		rc = -EPERM;
> > 
> > 	tpm_put_ops(chip);
> > @@ -624,23 +620,23 @@ static int tpm_unseal(struct tpm_buf *tb,
> > static int key_seal(struct trusted_key_payload *p,
> > 		    struct trusted_key_options *o)
> > {
> > -	struct tpm_buf tb;
> > 	int ret;
> > 
> > -	ret = tpm_buf_init(&tb, 0, 0);
> > -	if (ret)
> > -		return ret;
> > +	struct tpm_buf *tb __free(kfree) = kzalloc(PAGE_SIZE, GFP_KERNEL);
> > +	if (!tb)
> > +		return -ENOMEM;
> > +
> > +	tpm_buf_init(tb, TPM_BUFSIZE);
> > 
> > 	/* include migratable flag at end of sealed key */
> > 	p->key[p->key_len] = p->migratable;
> > 
> > -	ret = tpm_seal(&tb, o->keytype, o->keyhandle, o->keyauth,
> > +	ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
> > 		       p->key, p->key_len + 1, p->blob, &p->blob_len,
> > 		       o->blobauth, o->pcrinfo, o->pcrinfo_len);
> > 	if (ret < 0)
> > 		pr_info("srkseal failed (%d)\n", ret);
> > 
> > -	tpm_buf_destroy(&tb);
> > 	return ret;
> > }
> > 
> > @@ -650,14 +646,15 @@ static int key_seal(struct trusted_key_payload *p,
> > static int key_unseal(struct trusted_key_payload *p,
> > 		      struct trusted_key_options *o)
> > {
> > -	struct tpm_buf tb;
> > 	int ret;
> > 
> > -	ret = tpm_buf_init(&tb, 0, 0);
> > -	if (ret)
> > -		return ret;
> > +	struct tpm_buf *tb __free(kfree) = kzalloc(PAGE_SIZE, GFP_KERNEL);
> > +	if (!tb)
> > +		return -ENOMEM;
> > +
> > +	tpm_buf_init(tb, TPM_BUFSIZE);
> > 
> > -	ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
> > +	ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
> > 			 o->blobauth, p->key, &p->key_len);
> > 	if (ret < 0)
> > 		pr_info("srkunseal failed (%d)\n", ret);
> > @@ -665,7 +662,6 @@ static int key_unseal(struct trusted_key_payload *p,
> > 		/* pull migratable flag out of sealed key */
> > 		p->migratable = p->key[--p->key_len];
> > 
> > -	tpm_buf_destroy(&tb);
> > 	return ret;
> > }
> 
> The rest LGTM, but it's a huge patch (not your issue at all), so yeah not
> sure :-)

It's still a single logical change :-)

> 
> Thanks,
> Stefano
> 

BR, Jarkko

  reply	other threads:[~2025-09-30 13:11 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-29 19:48 [PATCH v3 00/10] tpm: Decouple Trenchboot dependencies Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 01/10] tpm: Cap the number of PCR banks Jarkko Sakkinen
2025-09-30 11:09   ` Jonathan McDowell
2025-09-30 12:36     ` Jarkko Sakkinen
2025-09-30 14:17       ` James Bottomley
2025-10-01 11:16         ` Jarkko Sakkinen
2025-10-01 12:52           ` Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 02/10] tpm: Use -EPERM as fallback error code in tpm_ret_to_err Jarkko Sakkinen
2025-09-30 12:11   ` Stefano Garzarella
2025-09-30 12:37     ` Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 03/10] KEYS: trusted: Use tpm_ret_to_err() in trusted_tpm2 Jarkko Sakkinen
2025-09-30 12:12   ` Stefano Garzarella
2025-09-30 12:39     ` Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 04/10] tpm2-sessions: Remove 'attributes' from tpm_buf_append_auth Jarkko Sakkinen
2025-09-30 11:10   ` Jonathan McDowell
2025-09-30 12:39     ` Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 05/10] tpm2-sessions: Umask tpm_buf_append_hmac_session() Jarkko Sakkinen
2025-09-30 11:11   ` Jonathan McDowell
2025-09-30 12:41     ` Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 06/10] KEYS: trusted: Open code tpm2_buf_append() Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 07/10] tpm-buf: check for corruption in tpm_buf_append_handle() Jarkko Sakkinen
2025-09-30 11:13   ` Jonathan McDowell
2025-09-30 12:43     ` Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 08/10] tpm-buf: Remove chip parameter from tpm_buf_append_handle Jarkko Sakkinen
2025-09-30 11:14   ` Jonathan McDowell
2025-09-29 19:48 ` [PATCH v3 09/10] tpm-buf: Build PCR extend commands Jarkko Sakkinen
2025-09-29 19:48 ` [PATCH v3 10/10] tpm-buf: Enable managed and stack allocations Jarkko Sakkinen
2025-09-30 12:44   ` Stefano Garzarella
2025-09-30 13:11     ` Jarkko Sakkinen [this message]
2025-09-30 13:20       ` Stefano Garzarella
2025-09-29 20:10 ` [PATCH v3 00/10] tpm: Decouple Trenchboot dependencies Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aNvW5KMUO2C0i233@kernel.org \
    --to=jarkko@kernel.org \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=dhowells@redhat.com \
    --cc=dpsmith@apertussolutions.com \
    --cc=jarkko.sakkinen@opinsys.com \
    --cc=jgg@ziepe.ca \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=peterhuewe@gmx.de \
    --cc=ross.philipson@oracle.com \
    --cc=serge@hallyn.com \
    --cc=sgarzare@redhat.com \
    --cc=stefanb@linux.ibm.com \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).