On Tue Apr 4, 2023 at 12:39 AM EEST, James Bottomley wrote: > Extracting values from returned TPM buffers can be hard. Add cursor > based (moving poiner) functions that make it easier to extract TPM > returned values from a buffer. > > Signed-off-by: James Bottomley <James.Bottomley@xxxxxxxxxxxxxxxxxxxxx> > > --- > v4: add kernel doc and reword commit > --- > drivers/char/tpm/tpm-buf.c | 48 ++++++++++++++++++++++++++++++++++++++ > include/linux/tpm.h | 3 +++ > 2 files changed, 51 insertions(+) > > diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c > index b7e42fb6266c..da0f6e725c3f 100644 > --- a/drivers/char/tpm/tpm-buf.c > +++ b/drivers/char/tpm/tpm-buf.c > @@ -6,6 +6,8 @@ > #include <linux/module.h> > #include <linux/tpm.h> > > +#include <asm/unaligned.h> > + > static int __tpm_buf_init(struct tpm_buf *buf) > { > buf->data = (u8 *)__get_free_page(GFP_KERNEL); > @@ -229,3 +231,49 @@ void tpm_buf_append_2b(struct tpm_buf *buf, struct tpm_buf *tpm2b) > tpm_buf_reset_int(tpm2b); > } > EXPORT_SYMBOL_GPL(tpm_buf_append_2b); > + > +/* functions for unmarshalling data and moving the cursor */ > + > +/** > + * tpm_get_inc_u8 - read a u8 and move pointer beyond it > + * @ptr: pointer to pointer > + * > + * @return: value read > + */ > +u8 tpm_get_inc_u8(const u8 **ptr) > +{ > + return *((*ptr)++); > +} > +EXPORT_SYMBOL_GPL(tpm_get_inc_u8); No overflow check, and these should be static inlines. Please consider this: /** * tpm_buf_read_u8() - Read a byte from a TPM buffer * @buf: &tpm_buf instance * @offset: offset within the consumed part of the buffer */ static inline int tpm_buf_read_u8(const struct tpm_buf *buf, offs_t *offset) { if (*offset++ >= buf->length) return -EINVAL; return buf->data[*offset - 1]; } Depends on: https://lore.kernel.org/linux-integrity/20230821033630.1039527-1-jarkko@xxxxxxxxxx/ No motivation for weird cursor concept, when the reality is just a read from a buffer. BR, Jarkko