tpm: Make TPM buffer allocations more robust
Subsystems (4)
Jarkko Sakkinen
maintainerPATCH v6 1/1 tpm: Make TPM buffer allocations more robust
From: Jarkko Sakkinen <[email protected]>
Drop 'tpm_buf_init', 'tpm_buf_init_sized' and 'tpm_buf_free'. Refine 'struct tpm_buf' to hold capacity in order to enable stack allocation and sizes other than page size.
The updated 'struct tpm_buf' can be allocated either from stack or heap.
The contract is the following:
1. 'tpm_buf_reset' and 'tpm_buf_reset_size' expect that on the first run the passed buffer is zeroed by the caller (e.g. via memset or kzalloc).
2. The same buffer can be reused. On the second and subsequent resets the aforementioned functions verify that 'buf_size' has the same value, and emits warning if not.
As a consequence 'struct tpm_buf' instance can be easily wrapped into managed allocation:
struct tpm_buf *buf __free(kfree) buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
---
v6:
- Removed two empty lines as requested by Stefan:
https://lore.kernel.org/linux-integrity/[email protected]/
- Add 'capacity' field as this makes easy to stretch tpm_buf into stack allocation.
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 | 57 +---- drivers/char/tpm/tpm-sysfs.c | 19 +- drivers/char/tpm/tpm1-cmd.c | 143 ++++++------ drivers/char/tpm/tpm2-cmd.c | 270 ++++++++++------------ 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 | 17 +- security/keys/trusted-keys/trusted_tpm1.c | 40 ++-- security/keys/trusted-keys/trusted_tpm2.c | 153 ++++++------ 10 files changed, 379 insertions(+), 509 deletions(-)
drivers/char/tpm/tpm-buf.c +9 -48
@@ -7,32 +7,14 @@#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 +* @buf_size: Size of the buffer. * @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) +void tpm_buf_reset (struct tpm_buf *buf,u16 buf_size,u16 tag,u32 ordinal) {struct tpm_header *head = (struct tpm_header *)buf->data;
@@ -41,49 +23,30 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) 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); - buf->handles =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 -*/ -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 +* @buf_size: Size of the buffer. */ -void tpm_buf_reset_sized (struct tpm_buf *buf) +void tpm_buf_reset_sized (struct tpm_buf *buf,u16 buf_size) { 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_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 +71,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) > buf->capacity) {WARN (1 ,"tpm_buf: write overflow\n" ); buf->flags |=TPM_BUF_OVERFLOW ;return ;
@@ -242,5 +205,3 @@ u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset)return be32_to_cpu (value); }EXPORT_SYMBOL_GPL (tpm_buf_read_u32); - -
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) = kzalloc(PAGE_SIZE, GFP_KERNEL); +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,PAGE_SIZE ,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 -152
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) = kzalloc(PAGE_SIZE, GFP_KERNEL); +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,PAGE_SIZE ,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,PAGE_SIZE ,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 +8 -9
@@ -375,13 +375,15 @@ 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; +u16 capacity; +u8 data[]; };enum tpm2_object_attributes {
@@ -412,11 +414,8 @@ struct tpm2_hash {unsigned int tpm_id; }; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal); -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); +void tpm_buf_reset (struct tpm_buf *buf,u16 buf_size,u16 tag,u32 ordinal); +void tpm_buf_reset_sized (struct tpm_buf *buf,u16 buf_size);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 +19 -21
@@ -312,21 +312,23 @@ static int TSS_checkhmac2(unsigned char *buffer,*/ static 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) = kzalloc(PAGE_SIZE, GFP_KERNEL); +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 )
@@ -368,7 +370,7 @@ static int osap(struct tpm_buf *tb, struct osapsess *s,if (ret !=TPM_NONCE_SIZE )return -EIO ; -tpm_buf_reset (tb,TPM_TAG_RQU_COMMAND ,TPM_ORD_OSAP ); +tpm_buf_reset (tb,PAGE_SIZE ,TPM_TAG_RQU_COMMAND ,TPM_ORD_OSAP );tpm_buf_append_u16 (tb, type);tpm_buf_append_u32 (tb, handle);tpm_buf_append (tb, ononce,TPM_NONCE_SIZE );
@@ -396,7 +398,7 @@ static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)if (!chip)return -ENODEV ; -tpm_buf_reset (tb,TPM_TAG_RQU_COMMAND ,TPM_ORD_OIAP ); +tpm_buf_reset (tb,PAGE_SIZE ,TPM_TAG_RQU_COMMAND ,TPM_ORD_OIAP ); ret =trusted_tpm_send (tb->data, tb->length);if (ret <0 )return ret;
@@ -494,7 +496,7 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype, goto out;/* build and send the TPM request packet */ -tpm_buf_reset (tb,TPM_TAG_RQU_AUTH1_COMMAND ,TPM_ORD_SEAL ); +tpm_buf_reset (tb,PAGE_SIZE ,TPM_TAG_RQU_AUTH1_COMMAND ,TPM_ORD_SEAL );tpm_buf_append_u32 (tb, keyhandle);tpm_buf_append (tb, td->encauth,SHA1_DIGEST_SIZE );tpm_buf_append_u32 (tb, pcrinfosize);
@@ -585,7 +587,7 @@ static int tpm_unseal(struct tpm_buf *tb,return ret;/* build and send TPM request packet */ -tpm_buf_reset (tb,TPM_TAG_RQU_AUTH2_COMMAND ,TPM_ORD_UNSEAL ); +tpm_buf_reset (tb,PAGE_SIZE ,TPM_TAG_RQU_AUTH2_COMMAND ,TPM_ORD_UNSEAL );tpm_buf_append_u32 (tb, keyhandle);tpm_buf_append (tb, blob, bloblen);tpm_buf_append_u32 (tb, authhandle1);
@@ -624,23 +626,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) = kzalloc(PAGE_SIZE, GFP_KERNEL); +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; }
@@ -650,14 +650,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) = kzalloc(PAGE_SIZE, GFP_KERNEL); +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);
@@ -665,7 +664,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