tpm: Managed allocations for tpm_buf instances
Subsystems (4)
Jarkko Sakkinen
maintainerPATCH v5 1/1 tpm: Managed allocations for tpm_buf instances
From: Jarkko Sakkinen <[email protected]>
Repeal and replace tpm_buf_init() and tpm_buf_init_sized() with tpm_buf_alloc(), which returns a buffer of memory with the struct tpm_buf header at the beginning of the returned buffer. This leaves 4092 bytes of free space for the payload.
Given that kfree() becomes the destructor for struct tpm_buf instances, tpm_buf_destroy() is now obsolete, and can be removed.
The actual gist is that a struct tpm_buf instance can be declared using __free(kfree) from linux/slab.h:
struct tpm_buf *buf __free(kfree) buf = tpm_buf_alloc();
Doing this has two-folded benefits associated with struct tpm_buf:
1. New features will not introduce memory leaks.
2. It addresses undiscovered memory leaks.
In addition, the barrier to contribute is lowered given that managing memory is a factor easier.
---
v5:
- I tested this version also with TPM 1.2 by booting up and checking that sysfs attributes work.
- Fixed the length check against capacity (James) with TPM_BUF_CAPACITY.
- Fixed declaration style: do it at the site (Jason).
- Improved commit message (Stefan).
- Removed "out" label from tpm2_pcr_read() (Stefan).
- Removed spurious "return rc;" from tpm2_get_pcr_allocation() (Stefan). v4:
- Wrote a more a descriptive short summary and improved description.
- Fixed the error in documentation: there is 4090 bytes of space left for the payload - not 4088 bytes.
- Turned tpm_buf_alloc() into inline function.
v3:
- Removed the cleanup class and moved on using __free(kfree) instead.
- Removed buf_size (James).
- I'm open for the idea of splitting still (Jason) but I'll hold at least this revision just to check that my core idea here is correct.
v2:
- Implement also memory allocation using the cleanup class.
- Rewrote the commit message.
- Implemented CLASS_TPM_BUF() helper macro.
--- drivers/char/tpm/tpm-buf.c | 44 +--- drivers/char/tpm/tpm-sysfs.c | 19 +- drivers/char/tpm/tpm1-cmd.c | 143 ++++++------ drivers/char/tpm/tpm2-cmd.c | 268 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 118 +++++----- drivers/char/tpm/tpm2-space.c | 42 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 29 +-- include/linux/tpm.h | 31 ++- security/keys/trusted-keys/trusted_tpm1.c | 32 ++- security/keys/trusted-keys/trusted_tpm2.c | 153 ++++++------ 10 files changed, 383 insertions(+), 496 deletions(-)
drivers/char/tpm/tpm-buf.c +1 -43
@@ -7,25 +7,6 @@#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) -{ - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; - -tpm_buf_reset (buf, tag, ordinal); -return 0 ; -} -EXPORT_SYMBOL_GPL (tpm_buf_init); -/** * tpm_buf_reset() - Initialize a TPM command * @buf: A &tpm_buf
@@ -48,23 +29,6 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) }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 -*/ -int tpm_buf_init_sized (struct tpm_buf *buf) -{ - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; - -tpm_buf_reset_sized (buf); -return 0 ; -} -EXPORT_SYMBOL_GPL (tpm_buf_init_sized); -/** * tpm_buf_reset_sized() - Initialize a sized buffer * @buf: A &tpm_buf
@@ -78,12 +42,6 @@ void tpm_buf_reset_sized(struct tpm_buf *buf) }EXPORT_SYMBOL_GPL (tpm_buf_reset_sized); -void tpm_buf_destroy (struct tpm_buf *buf) -{ -free_page ((unsigned long )buf->data); -} -EXPORT_SYMBOL_GPL (tpm_buf_destroy); -/** * tpm_buf_length() - Return the number of bytes consumed by the data * @buf: A &tpm_buf
@@ -108,7 +66,7 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length)if (buf->flags &TPM_BUF_OVERFLOW )return ; -if ((buf->length + new_length) >PAGE_SIZE ) { +if ((buf->length + new_length) >TPM_BUF_CAPACITY ) {WARN (1 ,"tpm_buf: write overflow\n" ); buf->flags |=TPM_BUF_OVERFLOW ;return ;
drivers/char/tpm/tpm-sysfs.c +9 -10
@@ -32,28 +32,29 @@ struct tpm_readpubek_out {static ssize_t pubek_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct tpm_buf tpm_buf;struct tpm_readpubek_out *out;int i;char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev);char anti_replay[20 ]; + struct tpm_buf *tpm_buf __free(kfree) = tpm_buf_alloc(); +if (!tpm_buf) +return -ENOMEM ; +memset (&anti_replay,0 ,sizeof (anti_replay));if (tpm_try_get_ops (chip))return 0 ; -if (tpm_buf_init (&tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK )) - goto out_ops; - -tpm_buf_append (&tpm_buf, anti_replay,sizeof (anti_replay)); +tpm_buf_reset (tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK ); +tpm_buf_append (tpm_buf, anti_replay,sizeof (anti_replay)); -if (tpm_transmit_cmd (chip, &tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE , +if (tpm_transmit_cmd (chip, tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE ,"attempting to read the PUBEK" )) - goto out_buf; + goto out_ops; - out = (struct tpm_readpubek_out *)&tpm_buf.data[10 ]; + out = (struct tpm_readpubek_out *)&tpm_buf->data[10 ]; str +=sprintf (str, "Algorithm: %4ph\n"
@@ -71,8 +72,6 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,for (i =0 ; i <256 ; i +=16 ) str +=sprintf (str,"%16ph\n" , &out->modulus[i]); -out_buf: -tpm_buf_destroy (&tpm_buf); out_ops:tpm_put_ops (chip);return str - buf;
drivers/char/tpm/tpm1-cmd.c +65 -78
266 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +118 -150
510 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.c +55 -63
221 lines not on this page · raw message
drivers/char/tpm/tpm2-space.c +16 -26
104 lines not on this page · raw message
drivers/char/tpm/tpm_vtpm_proxy.c +12 -17
@@ -395,40 +395,35 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)static int vtpm_proxy_request_locality (struct tpm_chip *chip,int locality) { -struct tpm_buf buf;int rc;const struct tpm_header *header;struct proxy_dev *proxy_dev =dev_get_drvdata (&chip->dev); + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc(); +if (!buf) +return -ENOMEM ; +if (chip->flags &TPM_CHIP_FLAG_TPM2 ) - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS , -TPM2_CC_SET_LOCALITY ); +tpm_buf_reset (buf,TPM2_ST_SESSIONS ,TPM2_CC_SET_LOCALITY );else - rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND , -TPM_ORD_SET_LOCALITY ); -if (rc) -return rc; -tpm_buf_append_u8 (&buf, locality); +tpm_buf_reset (buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_SET_LOCALITY ); + +tpm_buf_append_u8 (buf, locality); proxy_dev->state |=STATE_DRIVER_COMMAND ; - rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting to set locality" ); + rc =tpm_transmit_cmd (chip, buf,0 ,"attempting to set locality" ); proxy_dev->state &= ~STATE_DRIVER_COMMAND ; -if (rc <0 ) { - locality = rc; - goto out; - } +if (rc <0 ) +return rc; - header = (const struct tpm_header *)buf.data; + header = (const struct tpm_header *)buf->data; rc =be32_to_cpu (header->return_code);if (rc) locality =-1 ; -out: -tpm_buf_destroy (&buf); -return locality; }
include/linux/tpm.h +24 -7
@@ -375,15 +375,18 @@ enum tpm_buf_flags { };/* -* A string buffer type for constructing TPM commands. +* A buffer for constructing and parsing TPM commands, responses and sized +* (TPM2B) buffers. */ struct tpm_buf { -u32 flags; -u32 length; -u8 *data; +u8 flags;u8 handles; +u16 length; +u8 data[]; }; +#define TPM_BUF_CAPACITY (PAGE_SIZE - sizeof(struct tpm_buf)) +enum tpm2_object_attributes {TPM2_OA_FIXED_TPM =BIT (1 ),TPM2_OA_ST_CLEAR =BIT (2 ),
@@ -412,11 +415,25 @@ struct tpm2_hash {unsigned int tpm_id; }; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal); +/** +* tpm_buf_alloc() - Allocates a buffer for reading and writing TPM binary data +* +* Allocates 4096 bytes of memory and places &tpm_buf to the beginning of the +* allocated buffer. For that reason the total capacity can be at most 4092 +* bytes. If a caller tries to surpass the maximum capacity, %TPM_BUF_OVERFLOW +* will be set and any future operations will be ignored. +* +* Return: +* * &tpm_buf: success +* * NULL: out of memory +*/ +static inline struct tpm_buf *tpm_buf_alloc (void ) +{ +return kzalloc (PAGE_SIZE ,GFP_KERNEL ); +} +void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal); -int tpm_buf_init_sized (struct tpm_buf *buf);void tpm_buf_reset_sized (struct tpm_buf *buf); -void tpm_buf_destroy (struct tpm_buf *buf);u32 tpm_buf_length (struct tpm_buf *buf);void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length);void tpm_buf_append_u8 (struct tpm_buf *buf,const u8 value);
security/keys/trusted-keys/trusted_tpm1.c +15 -17
@@ -356,21 +356,23 @@ static int TSS_checkhmac2(unsigned char *buffer,*/ int trusted_tpm_send (unsigned char *cmd,size_t buflen) { -struct tpm_buf buf;int rc;if (!chip)return -ENODEV ; + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc(); +if (!buf) +return -ENOMEM ; + rc =tpm_try_get_ops (chip);if (rc)return rc; - buf.flags =0 ; - buf.length = buflen; - buf.data = cmd; +tpm_buf_append (buf, cmd, buflen); +dump_tpm_buf (cmd); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sending data" ); + rc =tpm_transmit_cmd (chip, buf,4 ,"sending data" );dump_tpm_buf (cmd);if (rc >0 )
@@ -672,23 +674,21 @@ 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) = tpm_buf_alloc(); +if (!tb) +return -ENOMEM ;/* 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; }
@@ -698,14 +698,13 @@ 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) = tpm_buf_alloc(); +if (!tb) +return -ENOMEM ; - 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);
@@ -713,7 +712,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; }
security/keys/trusted-keys/trusted_tpm2.c +68 -85
283 lines not on this page · raw message
Signature · 1 line
2.39.5
Quoted 27 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
Repeal and replace
tpm_buf_init()andtpm_buf_init_sized()withtpm_buf_alloc(), which returns a buffer of memory with the structtpm_bufheader at the beginning of the returned buffer. This leaves 4092 bytes of free space for the payload.Given that
kfree()becomes the destructor for structtpm_bufinstances,tpm_buf_destroy()is now obsolete, and can be removed.The actual gist is that a struct
tpm_bufinstance can be declared using__free(kfree)fromlinux/slab.h:struct tpm_buf *buf __free(kfree) buf = tpm_buf_alloc();Doing this has two-folded benefits associated with struct
tpm_buf:1. New features will not introduce memory leaks.
2. It addresses undiscovered memory leaks.In addition, the barrier to contribute is lowered given that managing memory is a factor easier.
Signed-off-by Jarkko Sakkinen <[email protected]>---
drivers/char/tpm/tpm2-cmd.c int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) · line 366
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +1
-2@@ -374,20 +362,18 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)*/ void tpm2_flush_context (struct tpm_chip *chip,u32 handle) { -struct tpm_buf buf; -int rc; + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc();
Remove empty line?
Quoted 57 lines
drivers/char/tpm/tpm2-cmd.c +14
-15- rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_FLUSH_CONTEXT ); -if (rc) { +if (!buf) {dev_warn (&chip->dev,"0x%08x was not flushed, out of memory\n" , handle);return ; } -tpm_buf_append_u32 (&buf, handle); +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_FLUSH_CONTEXT ); +tpm_buf_append_u32 (buf, handle); -tpm_transmit_cmd (chip, &buf,0 ,"flushing context" ); -tpm_buf_destroy (&buf); +tpm_transmit_cmd (chip, buf,0 ,"flushing context" ); }EXPORT_SYMBOL_GPL (tpm2_flush_context);@@ -414,19 +400,20 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, const char *desc) {struct tpm2_get_cap_out *out; -struct tpm_buf buf;int rc; - rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); -if (rc) -return rc; -tpm_buf_append_u32 (&buf,TPM2_CAP_TPM_PROPERTIES ); -tpm_buf_append_u32 (&buf, property_id); -tpm_buf_append_u32 (&buf,1 ); - rc =tpm_transmit_cmd (chip, &buf,0 ,NULL ); + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc(); +if (!buf) +return -ENOMEM ; + +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); +tpm_buf_append_u32 (buf,TPM2_CAP_TPM_PROPERTIES ); +tpm_buf_append_u32 (buf, property_id); +tpm_buf_append_u32 (buf,1 ); + rc =tpm_transmit_cmd (chip, buf,0 ,NULL );if (!rc) { out = (struct tpm2_get_cap_out *) - &buf.data[TPM_HEADER_SIZE ]; + &buf->data[TPM_HEADER_SIZE ];/* * To prevent failing boot up of some systems, Infineon TPM2.0 * returns SUCCESS on TPM2_Startup in field upgrade mode. Also @@ -438,7 +425,6 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,else rc = -ENODATA ; } -tpm_buf_destroy (&buf);return rc; }EXPORT_SYMBOL_GPL (tpm2_get_tpm_pt);
drivers/char/tpm/tpm2-cmd.c EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt); · line 445
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +1
-2@@ -455,15 +441,14 @@ EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);*/ void tpm2_shutdown (struct tpm_chip *chip,u16 shutdown_type) { -struct tpm_buf buf; -int rc; + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc();
Remove empty line here.
With this nit fixed:
Jarkko Sakkinen
maintainerQuoted 29 lines · Stefan Berger
Quoted 25 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
Repeal and replace
tpm_buf_init()andtpm_buf_init_sized()withtpm_buf_alloc(), which returns a buffer of memory with the structtpm_bufheader at the beginning of the returned buffer. This leaves 4092 bytes of free space for the payload.Given that
kfree()becomes the destructor for structtpm_bufinstances,tpm_buf_destroy()is now obsolete, and can be removed.The actual gist is that a struct
tpm_bufinstance can be declared using__free(kfree)fromlinux/slab.h:struct tpm_buf *buf __free(kfree) buf = tpm_buf_alloc();Doing this has two-folded benefits associated with struct
tpm_buf:1. New features will not introduce memory leaks.
2. It addresses undiscovered memory leaks.In addition, the barrier to contribute is lowered given that managing memory is a factor easier.
Signed-off-by Jarkko Sakkinen <[email protected]>---
Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) · line 366
Quoted 8 lines
drivers/char/tpm/tpm2-cmd.c +1
-2@@ -374,20 +362,18 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)*/ void tpm2_flush_context (struct tpm_chip *chip,u32 handle) { -struct tpm_buf buf; -int rc; + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc();Remove empty line?
I recalled from the past that checkpatch.pl would complain if there was no empty line after the declarations.
Now that I tested removing that line, it did not so I guess I can remove that empty line. The presumed checkpatch error was the only reason for having it.
Quoted 63 lines
Quoted 54 lines
drivers/char/tpm/tpm2-cmd.c +14
-15- rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_FLUSH_CONTEXT ); -if (rc) { +if (!buf) {dev_warn (&chip->dev,"0x%08x was not flushed, out of memory\n" , handle);return ; } -tpm_buf_append_u32 (&buf, handle); +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_FLUSH_CONTEXT ); +tpm_buf_append_u32 (buf, handle); -tpm_transmit_cmd (chip, &buf,0 ,"flushing context" ); -tpm_buf_destroy (&buf); +tpm_transmit_cmd (chip, buf,0 ,"flushing context" ); }EXPORT_SYMBOL_GPL (tpm2_flush_context);@@ -414,19 +400,20 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, const char *desc) {struct tpm2_get_cap_out *out; -struct tpm_buf buf;int rc; - rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); -if (rc) -return rc; -tpm_buf_append_u32 (&buf,TPM2_CAP_TPM_PROPERTIES ); -tpm_buf_append_u32 (&buf, property_id); -tpm_buf_append_u32 (&buf,1 ); - rc =tpm_transmit_cmd (chip, &buf,0 ,NULL ); + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc(); +if (!buf) +return -ENOMEM ; + +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); +tpm_buf_append_u32 (buf,TPM2_CAP_TPM_PROPERTIES ); +tpm_buf_append_u32 (buf, property_id); +tpm_buf_append_u32 (buf,1 ); + rc =tpm_transmit_cmd (chip, buf,0 ,NULL );if (!rc) { out = (struct tpm2_get_cap_out *) - &buf.data[TPM_HEADER_SIZE ]; + &buf->data[TPM_HEADER_SIZE ];/* * To prevent failing boot up of some systems, Infineon TPM2.0 * returns SUCCESS on TPM2_Startup in field upgrade mode. Also @@ -438,7 +425,6 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,else rc = -ENODATA ; } -tpm_buf_destroy (&buf);return rc; }EXPORT_SYMBOL_GPL (tpm2_get_tpm_pt);drivers/char/tpm/tpm2-cmd.c EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt); · line 444
Quoted 6 lines
drivers/char/tpm/tpm2-cmd.c +1
-2@@ -455,15 +441,14 @@ EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);*/ void tpm2_shutdown (struct tpm_chip *chip,u16 shutdown_type) { -struct tpm_buf buf; -int rc; + struct tpm_buf *buf __free(kfree) = tpm_buf_alloc();Remove empty line here.
Quoted 3 lines
With this nit fixed:
Reviewed-by Stefan Berger <[email protected]>
Thanks and I'm happy to fixup those. They did look also silly to me :-)
BR, Jarkko
Hi Jarkko,
kernel test robot noticed the following build warnings:
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jarkko-Sakkinen/tpm-Managed-allocations-for-tpm_buf-instances/20250704-021800 base: char-misc/char-misc-testing patch link: https://lore.kernel.org/r/20250703181712.923302-1-jarkko%40kernel.org patch subject: [PATCH v5] tpm: Managed allocations for tpm_buf instances config: i386-randconfig-141-20250704 (https://download.01.org/0day-ci/archive/20250706/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/
smatch warnings:drivers/char/tpm/tpm2-cmd.c:211 tpm2_pcr_read() warn: missing error code? 'rc'
vim +/rc +211 drivers/char/tpm/tpm2-cmd.c
879b589210a9a0 Roberto Sassu 2019-02-06 165 int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
879b589210a9a0 Roberto Sassu 2019-02-06 166 struct tpm_digest *digest, u16 *digest_size_ptr)
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 167 {
879b589210a9a0 Roberto Sassu 2019-02-06 168 int i;
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 169 int rc;
91f7f3d773a469 Roberto Sassu 2017-06-23 170 struct tpm2_pcr_read_out *out;
91f7f3d773a469 Roberto Sassu 2017-06-23 171 u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
879b589210a9a0 Roberto Sassu 2019-02-06 172 u16 digest_size;
879b589210a9a0 Roberto Sassu 2019-02-06 173 u16 expected_digest_size = 0;
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 174
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 175 struct tpm_buf *buf __free(kfree) = tpm_buf_alloc();
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 176 if (!buf)
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 177 return -ENOMEM;
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 178
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 179 if (pcr_idx >= TPM2_PLATFORM_PCR)
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 180 return -EINVAL;
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 181
879b589210a9a0 Roberto Sassu 2019-02-06 182 if (!digest_size_ptr) {
879b589210a9a0 Roberto Sassu 2019-02-06 183 for (i = 0; i < chip->nr_allocated_banks &&
879b589210a9a0 Roberto Sassu 2019-02-06 184 chip->allocated_banks[i].alg_id != digest->alg_id; i++)
879b589210a9a0 Roberto Sassu 2019-02-06 185 ;
879b589210a9a0 Roberto Sassu 2019-02-06 186
879b589210a9a0 Roberto Sassu 2019-02-06 187 if (i == chip->nr_allocated_banks)
879b589210a9a0 Roberto Sassu 2019-02-06 188 return -EINVAL;
879b589210a9a0 Roberto Sassu 2019-02-06 189
879b589210a9a0 Roberto Sassu 2019-02-06 190 expected_digest_size = chip->allocated_banks[i].digest_size;
879b589210a9a0 Roberto Sassu 2019-02-06 191 }
879b589210a9a0 Roberto Sassu 2019-02-06 192
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 193 tpm_buf_reset(buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
91f7f3d773a469 Roberto Sassu 2017-06-23 194
91f7f3d773a469 Roberto Sassu 2017-06-23 195 pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
91f7f3d773a469 Roberto Sassu 2017-06-23 196
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 197 tpm_buf_append_u32(buf, 1);
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 198 tpm_buf_append_u16(buf, digest->alg_id);
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 199 tpm_buf_append_u8(buf, TPM2_PCR_SELECT_MIN);
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 200 tpm_buf_append(buf, (const unsigned char *)pcr_select,
91f7f3d773a469 Roberto Sassu 2017-06-23 201 sizeof(pcr_select));
91f7f3d773a469 Roberto Sassu 2017-06-23 202
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 203 rc = tpm_transmit_cmd(chip, buf, 0, "attempting to read a pcr value");
879b589210a9a0 Roberto Sassu 2019-02-06 204 if (rc)
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 205 return rc;
879b589210a9a0 Roberto Sassu 2019-02-06 206
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 207 out = (struct tpm2_pcr_read_out *)&buf->data[TPM_HEADER_SIZE];
879b589210a9a0 Roberto Sassu 2019-02-06 208 digest_size = be16_to_cpu(out->digest_size);
879b589210a9a0 Roberto Sassu 2019-02-06 209 if (digest_size > sizeof(digest->digest) ||
a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 210 (!digest_size_ptr && digest_size != expected_digest_size))a5dbe6d43b61d5 Jarkko Sakkinen 2025-07-03 @211 return rc;
return -EINVAL?
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 212 879b589210a9a0 Roberto Sassu 2019-02-06 213 if (digest_size_ptr) 879b589210a9a0 Roberto Sassu 2019-02-06 214 *digest_size_ptr = digest_size; 879b589210a9a0 Roberto Sassu 2019-02-06 215 879b589210a9a0 Roberto Sassu 2019-02-06 216 memcpy(digest->digest, out->digest, digest_size); 7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 217 return rc;
return 0; pls
7a1d7e6dd76a20 Jarkko Sakkinen 2014-12-12 218 }
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki