tpm: Managed allocations for tpm_buf instances
Subsystems (4)
Jarkko Sakkinen
maintainerPATCH v4 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 4090 bytes of free space for the payload.
Given that kfree() is now the destructor for struct tpm_buf instances, tpm_buf_destroy() becomes obsolete, and can be safely wiped of too.
The actual gist is that now a tpm_buf can be now declared using __free(kfree) declared in linux/slab.h:
struct tpm_buf *buf __free(kfree) = NULL; /* ... */ buf = tpm_buf_alloc();
Doing this has two-folded benefits:
1. Yet to be discoverd memory leaks in the pre-existing code base.
2. Memory leaks concerning new features and other contributions.
In addition, the barrier to contribute is lowered given that managing memory is a factor easier.
---
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 | 42 ---- drivers/char/tpm/tpm-sysfs.c | 20 +- drivers/char/tpm/tpm1-cmd.c | 150 ++++++------ drivers/char/tpm/tpm2-cmd.c | 272 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 122 +++++----- drivers/char/tpm/tpm2-space.c | 44 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 ++- include/linux/tpm.h | 31 ++- security/keys/trusted-keys/trusted_tpm1.c | 35 +-- security/keys/trusted-keys/trusted_tpm2.c | 157 ++++++------- 10 files changed, 413 insertions(+), 490 deletions(-)
drivers/char/tpm/tpm-buf.c +0 -42
@@ -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
drivers/char/tpm/tpm-sysfs.c +10 -10
@@ -32,28 +32,30 @@ 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_buf *tpm_buf __free(kfree) = NULL;struct tpm_readpubek_out *out;int i;char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev);char anti_replay[20 ]; + tpm_buf =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 +73,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 +72 -78
276 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +128 -144
521 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.c +59 -63
227 lines not on this page · raw message
drivers/char/tpm/tpm2-space.c +18 -26
106 lines not on this page · raw message
drivers/char/tpm/tpm_vtpm_proxy.c +13 -17
@@ -395,40 +395,36 @@ 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; + struct tpm_buf *buf __free(kfree) = NULL;int rc;const struct tpm_header *header;struct proxy_dev *proxy_dev =dev_get_drvdata (&chip->dev); + buf =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 +23 -8
@@ -375,13 +375,14 @@ 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 handles; +u16 flags; +u16 length; +u16 handles; +u8 data[]; };enum tpm2_object_attributes {
@@ -412,11 +413,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 %PAGE_SIZE of memory and places &tpm_buf to the beginning of the +* allocated buffer. For that reason the total capacity can be at most 4090 +* 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 +18 -17
@@ -356,21 +356,24 @@ static int TSS_checkhmac2(unsigned char *buffer,*/ int trusted_tpm_send (unsigned char *cmd,size_t buflen) { -struct tpm_buf buf; + struct tpm_buf *buf __free(kfree) = NULL;int rc;if (!chip)return -ENODEV ; + buf =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 +675,22 @@ 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; + struct tpm_buf *tb __free(kfree) = NULL;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + tb =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 +700,14 @@ 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; + struct tpm_buf *tb __free(kfree) = NULL;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + tb =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 +715,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 +72 -85
288 lines not on this page · raw message
Signature · 1 line
2.39.5
Quoted 9 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 4090 bytes of free space for the payload.Given that
kfree()is now the destructor for structtpm_bufinstances,tpm_buf_destroy()becomes obsolete, and can be safely wiped of too.
s/of/off/ or s/wiped of/remove,/
Quoted 2 lines
The actual gist is that now a
tpm_bufcan be now declared using
s/that now a/that a/
Quoted 11 lines
__free(kfree)declared inlinux/slab.h:struct tpm_buf *buf __free(kfree) = NULL; /* ... */ buf = tpm_buf_alloc();Doing this has two-folded benefits:
1. Yet to be discoverd memory leaks in the pre-existing code base.
-> discovered
A couple of nits below and one stray 'return rc;' that should not be there...
Quoted 45 lines
2. Memory leaks concerning new features and other contributions.
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 +6
-8@@ -165,14 +165,18 @@ struct tpm2_pcr_read_out {int tpm2_pcr_read (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digest,u16 *digest_size_ptr) { + struct tpm_buf *buf __free(kfree) = NULL;int i;int rc; -struct tpm_buf buf;struct tpm2_pcr_read_out *out;u8 pcr_select[TPM2_PCR_SELECT_MIN ] = {0 };u16 digest_size;u16 expected_digest_size =0 ; + buf =tpm_buf_alloc (); +if (!buf) +return -ENOMEM ; +if (pcr_idx >=TPM2_PLATFORM_PCR )return -EINVAL ;@@ -187,23 +191,21 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, expected_digest_size = chip->allocated_banks[i].digest_size; } - rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_PCR_READ ); -if (rc) -return rc; +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_PCR_READ ); pcr_select[pcr_idx >>3 ] =1 << (pcr_idx &0x7 ); -tpm_buf_append_u32 (&buf,1 ); -tpm_buf_append_u16 (&buf, digest->alg_id); -tpm_buf_append_u8 (&buf,TPM2_PCR_SELECT_MIN ); - tpm_buf_append(&buf, (const unsigned char *)pcr_select,
drivers/char/tpm/tpm2-cmd.c int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, · line 206
Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +5
-1+tpm_buf_append_u32 (buf,1 ); +tpm_buf_append_u16 (buf, digest->alg_id); +tpm_buf_append_u8 (buf,TPM2_PCR_SELECT_MIN ); +tpm_buf_append (buf, (const unsigned char *)pcr_select,sizeof (pcr_select)); - rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting to read a pcr value" ); + rc =tpm_transmit_cmd (chip, buf,0 ,"attempting to read a pcr value" );if (rc) goto out;
nit: -> return rc; ?
drivers/char/tpm/tpm2-cmd.c int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, · line 220
Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +1
-1- out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE ]; + out = (struct tpm2_pcr_read_out *)&buf->data[TPM_HEADER_SIZE ]; digest_size =be16_to_cpu (out->digest_size);if (digest_size >sizeof (digest->digest) || (!digest_size_ptr && digest_size != expected_digest_size)) {@@ -216,7 +218,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,memcpy (digest->digest, out->digest, digest_size); out:
probably can remove this label
Quoted 52 lines
drivers/char/tpm/tpm2-cmd.c +13
-15-tpm_buf_destroy (&buf);return rc; }@@ -574,8 +569,8 @@ struct tpm2_pcr_selection {ssize_t tpm2_get_pcr_allocation (struct tpm_chip *chip) { + struct tpm_buf *buf __free(kfree) = NULL;struct tpm2_pcr_selection pcr_selection; -struct tpm_buf buf;void *marker;void *end;void *pcr_select_offset;@@ -587,41 +582,39 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)int rc;int i =0 ; - rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); -if (rc) -return rc; + buf =tpm_buf_alloc (); +if (!buf) +return -ENOMEM ; -tpm_buf_append_u32 (&buf,TPM2_CAP_PCRS ); -tpm_buf_append_u32 (&buf,0 ); -tpm_buf_append_u32 (&buf,1 ); +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); +tpm_buf_append_u32 (buf,TPM2_CAP_PCRS ); +tpm_buf_append_u32 (buf,0 ); +tpm_buf_append_u32 (buf,1 ); - rc =tpm_transmit_cmd (chip, &buf,9 ,"get tpm pcr allocation" ); + rc =tpm_transmit_cmd (chip, buf,9 ,"get tpm pcr allocation" );if (rc) - goto out; +return rc; nr_possible_banks =be32_to_cpup ( - (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); + (__be32 *)&buf->data[TPM_HEADER_SIZE +5 ]); chip->allocated_banks =kcalloc (nr_possible_banks,sizeof (*chip->allocated_banks),GFP_KERNEL ); -if (!chip->allocated_banks) { - rc = -ENOMEM ; - goto out; - } +if (!chip->allocated_banks) +return -ENOMEM ;
drivers/char/tpm/tpm2-cmd.c ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 612
Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +4
-3- marker = &buf.data[TPM_HEADER_SIZE +9 ]; + marker = &buf->data[TPM_HEADER_SIZE +9 ]; - rsp_len =be32_to_cpup ((__be32 *)&buf.data[2 ]); - end = &buf.data[rsp_len]; + rsp_len =be32_to_cpup ((__be32 *)&buf->data[2 ]); + end = &buf->data[rsp_len]; +return rc;
this doesn't look right...
Quoted 18 lines
drivers/char/tpm/tpm2-cmd.c +2
-4for (i =0 ; i < nr_possible_banks; i++) { pcr_select_offset = marker + offsetof(struct tpm2_pcr_selection , size_of_select); -if (pcr_select_offset >= end) { - rc = -EFAULT ; -break ; - } +if (pcr_select_offset >= end) +return -EFAULT ;memcpy (&pcr_selection, marker,sizeof (pcr_selection)); hash_alg =be16_to_cpu (pcr_selection.hash_alg);@@ -633,7 +626,7 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)security/keys/trusted-keys/trusted_tpm2.c +0
-0
security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip, · line 245
Quoted 5 lines
security/keys/trusted-keys/trusted_tpm2.c +2
-0@@ -241,14 +241,23 @@ int tpm2_seal_trusted(struct tpm_chip *chip,struct trusted_key_payload *payload, struct trusted_key_options *options) { + struct tpm_buf *buf __free(kfree) = NULL; + struct tpm_buf *sized __free(kfree) = NULL;
Revert order of the above two lines.
Quoted 137 lines
security/keys/trusted-keys/trusted_tpm2.c +37
-43off_t offset =TPM_HEADER_SIZE ; -struct tpm_buf buf, sized;int blob_len =0 ;u32 hash;u32 flags;int i;int rc; + buf =tpm_buf_alloc (); +if (!buf) +return -ENOMEM ; + + sized =tpm_buf_alloc (); +if (!sized) +return -ENOMEM ; + for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {> if (options->hash == tpm2_hash_map[i].crypto_id) { hash = tpm2_hash_map[i].tpm_id;@@ -270,89 +279,76 @@ int tpm2_seal_trusted(struct tpm_chip *chip,if (rc) goto out_put; - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_CREATE ); -if (rc) { -tpm2_end_auth_session (chip); - goto out_put; - } - - rc =tpm_buf_init_sized (&sized); -if (rc) { -tpm_buf_destroy (&buf); -tpm2_end_auth_session (chip); - goto out_put; - } - -tpm_buf_append_name (chip, &buf, options->keyhandle,NULL ); -tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_DECRYPT , +tpm_buf_reset (buf,TPM2_ST_SESSIONS ,TPM2_CC_CREATE ); +tpm_buf_reset_sized (sized); +tpm_buf_append_name (chip, buf, options->keyhandle,NULL ); +tpm_buf_append_hmac_session (chip, buf,TPM2_SA_DECRYPT , options->keyauth,TPM_DIGEST_SIZE );/* sensitive */ -tpm_buf_append_u16 (&sized, options->blobauth_len); +tpm_buf_append_u16 (sized, options->blobauth_len);if (options->blobauth_len) -tpm_buf_append (&sized, options->blobauth, options->blobauth_len); +tpm_buf_append (sized, options->blobauth, options->blobauth_len); -tpm_buf_append_u16 (&sized, payload->key_len); -tpm_buf_append (&sized, payload->key, payload->key_len); -tpm_buf_append (&buf, sized.data, sized.length); +tpm_buf_append_u16 (sized, payload->key_len); +tpm_buf_append (sized, payload->key, payload->key_len); +tpm_buf_append (buf, sized->data, sized->length);/* public */ -tpm_buf_reset_sized (&sized); -tpm_buf_append_u16 (&sized,TPM_ALG_KEYEDHASH ); -tpm_buf_append_u16 (&sized, hash); +tpm_buf_reset_sized (sized); +tpm_buf_append_u16 (sized,TPM_ALG_KEYEDHASH ); +tpm_buf_append_u16 (sized, hash);/* key properties */ flags =0 ; flags |= options->policydigest_len ?0 :TPM2_OA_USER_WITH_AUTH ; flags |= payload->migratable ?0 : (TPM2_OA_FIXED_TPM |TPM2_OA_FIXED_PARENT ); -tpm_buf_append_u32 (&sized, flags); +tpm_buf_append_u32 (sized, flags);/* policy */ -tpm_buf_append_u16 (&sized, options->policydigest_len); +tpm_buf_append_u16 (sized, options->policydigest_len);if (options->policydigest_len) -tpm_buf_append (&sized, options->policydigest, options->policydigest_len); +tpm_buf_append (sized, options->policydigest, options->policydigest_len);/* public parameters */ -tpm_buf_append_u16 (&sized,TPM_ALG_NULL ); -tpm_buf_append_u16 (&sized,0 ); +tpm_buf_append_u16 (sized,TPM_ALG_NULL ); +tpm_buf_append_u16 (sized,0 ); -tpm_buf_append (&buf, sized.data, sized.length); +tpm_buf_append (buf, sized->data, sized->length);/* outside info */ -tpm_buf_append_u16 (&buf,0 ); +tpm_buf_append_u16 (buf,0 );/* creation PCR */ -tpm_buf_append_u32 (&buf,0 ); +tpm_buf_append_u32 (buf,0 ); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf->flags &TPM_BUF_OVERFLOW ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out; } -tpm_buf_fill_hmac_session (chip, &buf); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sealing data" ); - rc =tpm_buf_check_hmac_response (chip, &buf, rc); +tpm_buf_fill_hmac_session (chip, buf); + rc =tpm_transmit_cmd (chip, buf,4 ,"sealing data" ); + rc =tpm_buf_check_hmac_response (chip, buf, rc);if (rc) goto out; - blob_len =tpm_buf_read_u32 (&buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf.flags &TPM_BUF_BOUNDARY_ERROR ) { + blob_len =tpm_buf_read_u32 (buf, &offset); +if (blob_len >MAX_BLOB_SIZE || buf->flags &TPM_BUF_BOUNDARY_ERROR ) { rc = -E2BIG ; goto out; } -if (buf.length - offset < blob_len) { +if (buf->length - offset < blob_len) { rc = -EFAULT ; goto out; } - blob_len =tpm2_key_encode (payload, options, &buf.data[offset], blob_len); + blob_len =tpm2_key_encode (payload, options, &buf->data[offset], + blob_len); out: -tpm_buf_destroy (&sized); -tpm_buf_destroy (&buf); -if (rc >0 ) {if (tpm2_rc_value (rc) ==TPM2_RC_HASH ) rc = -EINVAL ;
Jarkko Sakkinen
maintainerQuoted 90 lines · Stefan Berger
Quoted 9 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 4090 bytes of free space for the payload.Given that
kfree()is now the destructor for structtpm_bufinstances,tpm_buf_destroy()becomes obsolete, and can be safely wiped of too.s/of/off/ or s/wiped of/remove,/
Quoted 2 lines
The actual gist is that now a
tpm_bufcan be now declared usings/that now a/that a/
Quoted 11 lines
__free(kfree)declared inlinux/slab.h:struct tpm_buf *buf __free(kfree) = NULL; /* ... */ buf = tpm_buf_alloc();Doing this has two-folded benefits:
1. Yet to be discoverd memory leaks in the pre-existing code base.
-> discovered
A couple of nits below and one stray 'return rc;' that should not be there...
Quoted 39 lines
2. Memory leaks concerning new features and other contributions.
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 +6
-7@@ -165,14 +165,18 @@ struct tpm2_pcr_read_out {int tpm2_pcr_read (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digest,u16 *digest_size_ptr) { + struct tpm_buf *buf __free(kfree) = NULL;int i;int rc; -struct tpm_buf buf;struct tpm2_pcr_read_out *out;u8 pcr_select[TPM2_PCR_SELECT_MIN ] = {0 };u16 digest_size;u16 expected_digest_size =0 ; + buf =tpm_buf_alloc (); +if (!buf) +return -ENOMEM ; +if (pcr_idx >=TPM2_PLATFORM_PCR )return -EINVAL ;@@ -187,23 +191,21 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, expected_digest_size = chip->allocated_banks[i].digest_size; } - rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_PCR_READ ); -if (rc) -return rc; +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_PCR_READ ); pcr_select[pcr_idx >>3 ] =1 << (pcr_idx &0x7 ); -tpm_buf_append_u32 (&buf,1 ); -tpm_buf_append_u16 (&buf, digest->alg_id); -tpm_buf_append_u8 (&buf,TPM2_PCR_SELECT_MIN );drivers/char/tpm/tpm2-cmd.c int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, · line 202
Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +5
-2-tpm_buf_append (&buf, (const unsigned char *)pcr_select, +tpm_buf_append_u32 (buf,1 ); +tpm_buf_append_u16 (buf, digest->alg_id); +tpm_buf_append_u8 (buf,TPM2_PCR_SELECT_MIN ); +tpm_buf_append (buf, (const unsigned char *)pcr_select,sizeof (pcr_select)); - rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting to read a pcr value" ); + rc =tpm_transmit_cmd (chip, buf,0 ,"attempting to read a pcr value" );if (rc) goto out;nit: -> return rc; ?
Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, · line 219
Quoted 8 lines
drivers/char/tpm/tpm2-cmd.c +1
-1- out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE ]; + out = (struct tpm2_pcr_read_out *)&buf->data[TPM_HEADER_SIZE ]; digest_size =be16_to_cpu (out->digest_size);if (digest_size >sizeof (digest->digest) || (!digest_size_ptr && digest_size != expected_digest_size)) {@@ -216,7 +218,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,memcpy (digest->digest, out->digest, digest_size); out:probably can remove this label
yup, i agree
Quoted 48 lines
Quoted 48 lines
drivers/char/tpm/tpm2-cmd.c +14
-16-tpm_buf_destroy (&buf);return rc; }@@ -574,8 +569,8 @@ struct tpm2_pcr_selection {ssize_t tpm2_get_pcr_allocation (struct tpm_chip *chip) { + struct tpm_buf *buf __free(kfree) = NULL;struct tpm2_pcr_selection pcr_selection; -struct tpm_buf buf;void *marker;void *end;void *pcr_select_offset;@@ -587,41 +582,39 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)int rc;int i =0 ; - rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); -if (rc) -return rc; + buf =tpm_buf_alloc (); +if (!buf) +return -ENOMEM ; -tpm_buf_append_u32 (&buf,TPM2_CAP_PCRS ); -tpm_buf_append_u32 (&buf,0 ); -tpm_buf_append_u32 (&buf,1 ); +tpm_buf_reset (buf,TPM2_ST_NO_SESSIONS ,TPM2_CC_GET_CAPABILITY ); +tpm_buf_append_u32 (buf,TPM2_CAP_PCRS ); +tpm_buf_append_u32 (buf,0 ); +tpm_buf_append_u32 (buf,1 ); - rc =tpm_transmit_cmd (chip, &buf,9 ,"get tpm pcr allocation" ); + rc =tpm_transmit_cmd (chip, buf,9 ,"get tpm pcr allocation" );if (rc) - goto out; +return rc; nr_possible_banks =be32_to_cpup ( - (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); + (__be32 *)&buf->data[TPM_HEADER_SIZE +5 ]); chip->allocated_banks =kcalloc (nr_possible_banks,sizeof (*chip->allocated_banks),GFP_KERNEL ); -if (!chip->allocated_banks) { - rc = -ENOMEM ; - goto out; - } +if (!chip->allocated_banks) +return -ENOMEM ; - marker = &buf.data[TPM_HEADER_SIZE +9 ]; + marker = &buf->data[TPM_HEADER_SIZE +9 ];
Quoted 8 lines
drivers/char/tpm/tpm2-cmd.c ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 612
Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +3
-2- rsp_len =be32_to_cpup ((__be32 *)&buf.data[2 ]); - end = &buf.data[rsp_len]; + rsp_len =be32_to_cpup ((__be32 *)&buf->data[2 ]); + end = &buf->data[rsp_len]; +return rc;this doesn't look right...
thanks for catching this!
Quoted 140 lines
Quoted 19 lines
drivers/char/tpm/tpm2-cmd.c +2
-4for (i =0 ; i < nr_possible_banks; i++) { pcr_select_offset = marker + offsetof(struct tpm2_pcr_selection , size_of_select); -if (pcr_select_offset >= end) { - rc = -EFAULT ; -break ; - } +if (pcr_select_offset >= end) +return -EFAULT ;memcpy (&pcr_selection, marker,sizeof (pcr_selection)); hash_alg =be16_to_cpu (pcr_selection.hash_alg);@@ -633,7 +626,7 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)security/keys/trusted-keys/trusted_tpm2.c +0
-0security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip, · line 245
Quoted 5 lines
security/keys/trusted-keys/trusted_tpm2.c +2
-0@@ -241,14 +241,23 @@ int tpm2_seal_trusted(struct tpm_chip *chip,struct trusted_key_payload *payload, struct trusted_key_options *options) { + struct tpm_buf *buf __free(kfree) = NULL; + struct tpm_buf *sized __free(kfree) = NULL;Revert order of the above two lines.
Quoted 6 lines
security/keys/trusted-keys/trusted_tpm2.c +0
-1off_t offset =TPM_HEADER_SIZE ; -struct tpm_buf buf, sized;int blob_len =0 ;u32 hash;u32 flags;int i;security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip,
Quoted 10 lines
security/keys/trusted-keys/trusted_tpm2.c +8
-0int rc; + buf =tpm_buf_alloc (); +if (!buf) +return -ENOMEM ; + + sized =tpm_buf_alloc (); +if (!sized) +return -ENOMEM ; + for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {> if(
options->hash== tpm2_hash_map[i].crypto_id) {Quoted 96 lines
security/keys/trusted-keys/trusted_tpm2.c +28
-39hash = tpm2_hash_map[i].tpm_id;@@ -270,89 +279,76 @@ int tpm2_seal_trusted(struct tpm_chip *chip,if (rc) goto out_put; - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_CREATE ); -if (rc) { -tpm2_end_auth_session (chip); - goto out_put; - } - - rc =tpm_buf_init_sized (&sized); -if (rc) { -tpm_buf_destroy (&buf); -tpm2_end_auth_session (chip); - goto out_put; - } - -tpm_buf_append_name (chip, &buf, options->keyhandle,NULL ); -tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_DECRYPT , +tpm_buf_reset (buf,TPM2_ST_SESSIONS ,TPM2_CC_CREATE ); +tpm_buf_reset_sized (sized); +tpm_buf_append_name (chip, buf, options->keyhandle,NULL ); +tpm_buf_append_hmac_session (chip, buf,TPM2_SA_DECRYPT , options->keyauth,TPM_DIGEST_SIZE );/* sensitive */ -tpm_buf_append_u16 (&sized, options->blobauth_len); +tpm_buf_append_u16 (sized, options->blobauth_len);if (options->blobauth_len) -tpm_buf_append (&sized, options->blobauth, options->blobauth_len); +tpm_buf_append (sized, options->blobauth, options->blobauth_len); -tpm_buf_append_u16 (&sized, payload->key_len); -tpm_buf_append (&sized, payload->key, payload->key_len); -tpm_buf_append (&buf, sized.data, sized.length); +tpm_buf_append_u16 (sized, payload->key_len); +tpm_buf_append (sized, payload->key, payload->key_len); +tpm_buf_append (buf, sized->data, sized->length);/* public */ -tpm_buf_reset_sized (&sized); -tpm_buf_append_u16 (&sized,TPM_ALG_KEYEDHASH ); -tpm_buf_append_u16 (&sized, hash); +tpm_buf_reset_sized (sized); +tpm_buf_append_u16 (sized,TPM_ALG_KEYEDHASH ); +tpm_buf_append_u16 (sized, hash);/* key properties */ flags =0 ; flags |= options->policydigest_len ?0 :TPM2_OA_USER_WITH_AUTH ; flags |= payload->migratable ?0 : (TPM2_OA_FIXED_TPM |TPM2_OA_FIXED_PARENT ); -tpm_buf_append_u32 (&sized, flags); +tpm_buf_append_u32 (sized, flags);/* policy */ -tpm_buf_append_u16 (&sized, options->policydigest_len); +tpm_buf_append_u16 (sized, options->policydigest_len);if (options->policydigest_len) -tpm_buf_append (&sized, options->policydigest, options->policydigest_len); +tpm_buf_append (sized, options->policydigest, options->policydigest_len);/* public parameters */ -tpm_buf_append_u16 (&sized,TPM_ALG_NULL ); -tpm_buf_append_u16 (&sized,0 ); +tpm_buf_append_u16 (sized,TPM_ALG_NULL ); +tpm_buf_append_u16 (sized,0 ); -tpm_buf_append (&buf, sized.data, sized.length); +tpm_buf_append (buf, sized->data, sized->length);/* outside info */ -tpm_buf_append_u16 (&buf,0 ); +tpm_buf_append_u16 (buf,0 );/* creation PCR */ -tpm_buf_append_u32 (&buf,0 ); +tpm_buf_append_u32 (buf,0 ); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf->flags &TPM_BUF_OVERFLOW ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out; } -tpm_buf_fill_hmac_session (chip, &buf); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sealing data" ); - rc =tpm_buf_check_hmac_response (chip, &buf, rc); +tpm_buf_fill_hmac_session (chip, buf); + rc =tpm_transmit_cmd (chip, buf,4 ,"sealing data" ); + rc =tpm_buf_check_hmac_response (chip, buf, rc);if (rc) goto out; - blob_len =tpm_buf_read_u32 (&buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf.flags &TPM_BUF_BOUNDARY_ERROR ) { + blob_len =tpm_buf_read_u32 (buf, &offset); +if (blob_len >MAX_BLOB_SIZE || buf->flags &TPM_BUF_BOUNDARY_ERROR ) { rc = -E2BIG ; goto out; } -if (buf.length - offset < blob_len) { +if (buf->length - offset < blob_len) { rc = -EFAULT ; goto out; } - blob_len =tpm2_key_encode (payload, options, &buf.data[offset], blob_len); + blob_len = tpm2_key_encode(payload, options, &buf->data[offset],
Quoted 8 lines
security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip, · line 354
Quoted 8 lines
security/keys/trusted-keys/trusted_tpm2.c +1
-3+ blob_len); out: -tpm_buf_destroy (&sized); -tpm_buf_destroy (&buf); -if (rc >0 ) {if (tpm2_rc_value (rc) ==TPM2_RC_HASH ) rc = -EINVAL ;
Thanks for the remarks.
BR, Jarkko
James Bottomley
maintainerQuoted 4 lines · Jarkko Sakkinen
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 4090 bytes of free space for the payload.
Shouldn't this be accounted for in tpm_buf_append()? right now it will let us run off the end of the allocation by six bytes before it signals overflow because it's checking final length against PAGE_SIZE not PAGE_SIZE - sizeof(struct tpm_buf). I realise this should be an impossible condition in production, but it's useful for debugging so we should be accurate about it to avoid hard to detect bugs.
Regards,
James
Jarkko Sakkinen
maintainerQuoted 6 lines · James Bottomley
Quoted 4 lines · Jarkko Sakkinen
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 4090 bytes of free space for the payload.
Quoted 6 lines
Shouldn't this be accounted for in
tpm_buf_append()? right now it will let us run off the end of the allocation by six bytes before it signals overflow because it's checking final length againstPAGE_SIZEnotPAGE_SIZE-sizeof(struct tpm_buf). I realise this should be an impossible condition in production, but it's useful for debugging so we should be accurate about it to avoid hard to detect bugs.
Fully agree. Thanks for pointing this out!
Quoted 5 lines
Regards,
James
BR, Jarkko
Jason Gunthorpe
reviewerQuoted 5 lines · Jarkko Sakkinen
drivers/char/tpm/tpm-sysfs.c +0
-1@@ -32,28 +32,30 @@ struct tpm_readpubek_out {static ssize_t pubek_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct tpm_buf tpm_buf;
drivers/char/tpm/tpm-sysfs.c struct tpm_readpubek_out { · line 44
Quoted 10 lines
drivers/char/tpm/tpm-sysfs.c +4
-0+ struct tpm_buf *tpm_buf __free(kfree) = NULL;struct tpm_readpubek_out *out;int i;char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev);char anti_replay[20 ]; + tpm_buf =tpm_buf_alloc (); +if (!tpm_buf) +return -ENOMEM ;
apprently this isn't the style guide, you are supposed to write:
char anti_replay[20];
struct tpm_buf *tpm_buf __free(kfree) = tpm_buf_alloc(); if (!tpm_buf) return -ENOMEM;
Jason
Jarkko Sakkinen
maintainerQuoted 19 lines · Jason Gunthorpe
Quoted 5 lines · Jarkko Sakkinen
drivers/char/tpm/tpm-sysfs.c +0
-1@@ -32,28 +32,30 @@ struct tpm_readpubek_out {static ssize_t pubek_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct tpm_buf tpm_buf;drivers/char/tpm/tpm-sysfs.c struct tpm_readpubek_out { · line 44
Quoted 10 lines
drivers/char/tpm/tpm-sysfs.c +4
-0+ struct tpm_buf *tpm_buf __free(kfree) = NULL;struct tpm_readpubek_out *out;int i;char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev);char anti_replay[20 ]; + tpm_buf =tpm_buf_alloc (); +if (!tpm_buf) +return -ENOMEM ;apprently this isn't the style guide, you are supposed to write:
Quoted 5 lines
char anti_replay[20];struct tpm_buf *tpm_buf __free(kfree) = tpm_buf_alloc(); if (!tpm_buf) return -ENOMEM;
Sounds fair to me. I'll shift to this direction in the next revision.
Quoted 2 lines
Jason
BR, Jarkko
BR, Jarkko