misc bug and other fixes
Mimi Zohar
maintainercover misc bug and other fixes
Fix coverity reported issues and the tpm2_pcr_supported() informational message.
Mimi Zohar (4):
Don't ignore number of items read
Define and verify the template data length upper bounds Sanity check the template data field sizes
Fix tpm2_pcr_supported() output messages
src/evmctl.c | 57 +++++++++++++++++++++++++++++++++++++------- src/imaevm.h | 10 ++++++++ src/pcr_ibmtss.c | 12 +--------- src/pcr_tsspcrread.c | 4 ++-- 4 files changed, 62 insertions(+), 21 deletions(-)
Signature · 1 line
2.31.1
Mimi Zohar
maintainerPATCH 1/4 Don't ignore number of items read
fread() either returns the number of bytes read or the number of items of data read. Check that it returns the requested number of items read.
--- src/evmctl.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
src/evmctl.c +7 -5
@@ -2161,7 +2161,7 @@ static int ima_measurement(const char *file) }memset (entry.name,0x00 ,sizeof (entry.name)); -if (!fread (entry.name, entry.header.name_len,1 , fp)) { +if (fread (entry.name, entry.header.name_len,1 , fp) !=1 ) {log_err ("Unable to read template name\n" ); goto out; }
@@ -2184,8 +2184,8 @@ static int ima_measurement(const char *file)/* The "ima" template data is not length prefixed. Skip it. */ if (!is_ima_template) { -if (!fread (&entry.template_len, -sizeof (entry.template_len),1 , fp)) { +if (fread (&entry.template_len, +sizeof (entry.template_len),1 , fp) !=1 ) {log_err ("Unable to read template length\n" ); goto out; }
@@ -2205,7 +2205,8 @@ static int ima_measurement(const char *file) }if (!is_ima_template) { -if (!fread (entry.template, entry.template_len,1 , fp)) { +if (fread (entry.template, entry.template_len, +1 , fp) !=1 ) {log_errno ("Unable to read template\n" ); goto out; }
@@ -2217,7 +2218,8 @@ static int ima_measurement(const char *file)* The "ima" template data format is digest, * filename length, filename. */ -if (!fread (entry.template,SHA_DIGEST_LENGTH ,1 , fp)) { +if (fread (entry.template,SHA_DIGEST_LENGTH , +1 , fp) !=1 ) {log_errno ("Unable to read file data hash\n" ); goto out; }
Signature · 1 line
2.31.1
Re: [PATCH ima-evm-utils 1/4] Don't ignore number of items read
Quoted 43 lines · Mimi Zohar
fread()either returns the number of bytes read or the number of items of data read. Check that it returns the requested number of items read.Signed-off-by Mimi Zohar <[email protected]>--- src/evmctl.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)src/evmctl.c +5
-4@@ -2161,7 +2161,7 @@ static int ima_measurement(const char *file) }memset (entry.name,0x00 ,sizeof (entry.name)); -if (!fread (entry.name, entry.header.name_len,1 , fp)) { +if (fread (entry.name, entry.header.name_len,1 , fp) !=1 ) {log_err ("Unable to read template name\n" ); goto out; }@@ -2184,8 +2184,8 @@ static int ima_measurement(const char *file)/* The "ima" template data is not length prefixed. Skip it. */ if (!is_ima_template) { -if (!fread (&entry.template_len, -sizeof (entry.template_len),1 , fp)) { +if (fread (&entry.template_len, +sizeof (entry.template_len),1 , fp) !=1 ) {log_err ("Unable to read template length\n" ); goto out; }@@ -2205,7 +2205,8 @@ static int ima_measurement(const char *file) }if (!is_ima_template) { -if (!fread (entry.template, entry.template_len,1 , fp)) { +if (fread (entry.template, entry.template_len, +1 , fp) !=1 ) {log_errno ("Unable to read template\n" ); goto out; }
src/evmctl.c static int ima_measurement(const char *file) · line 2225
Quoted 9 lines
src/evmctl.c +2
-1@@ -2217,7 +2218,8 @@ static int ima_measurement(const char *file)* The "ima" template data format is digest, * filename length, filename. */ -if (!fread (entry.template,SHA_DIGEST_LENGTH ,1 , fp)) { +if (fread (entry.template,SHA_DIGEST_LENGTH , +1 , fp) !=1 ) {log_errno ("Unable to read file data hash\n" ); goto out; }
It was correct before as well ...
Mimi Zohar
maintainerRe: [PATCH ima-evm-utils 1/4] Don't ignore number of items read
Hi Stefan,
Thank you for this and the other reviews.
Quoted 5 lines · Stefan Berger
Quoted 3 lines · Mimi Zohar
fread()either returns the number of bytes read or the number of items of data read. Check that it returns the requested number of items read.
Quoted 7 lines
Quoted 1 line
Signed-off-by Mimi Zohar <[email protected]>It was correct before as well ...
Reviewed-by Stefan Berger <[email protected]>
Yeah, I'll update the patch description explaining that this stops the static analysis complaints
Signature · 3 lines
thanks, Mimi
Mimi Zohar
maintainerPATCH 2/4 Define and verify the template data length upper bounds
In reply to Mimi ZoharThe template data length is variable, based on the template format. Define some sort of upper bounds.
--- src/evmctl.c | 3 ++- src/imaevm.h | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-)
src/evmctl.c +2 -1
@@ -2189,7 +2189,8 @@ static int ima_measurement(const char *file)log_err ("Unable to read template length\n" ); goto out; } -if (entry.template_len ==0 ) { +if (entry.template_len ==0 || + entry.template_len >MAX_TEMPLATE_SIZE ) {log_err ("Invalid template data len\n" ); goto out; }
src/imaevm.h +10 -0
@@ -91,6 +91,16 @@#define MAX_DIGEST_SIZE 64#define MAX_SIGNATURE_SIZE 1024 +/* +* The maximum template data size is dependent on the template format. For +* example the 'ima-modsig' template includes two signatures - one for the +* entire file, the other without the appended signature - and other fields +* (e.g. file digest, file name, file digest without the appended signature). +* +* Other template formats are much smaller. +*/ +#define MAX_TEMPLATE_SIZE (MAX_SIGNATURE_SIZE * 4) +#define __packed __attribute__((packed))enum evm_ima_xattr_type {
Signature · 1 line
2.31.1
Re: [PATCH ima-evm-utils 2/4] Define and verify the template data length upper bounds
Quoted 30 lines · Mimi Zohar
The template data length is variable, based on the template format. Define some sort of upper bounds.
Signed-off-by Mimi Zohar <[email protected]>--- src/evmctl.c | 3 ++- src/imaevm.h | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-)src/evmctl.c +2
-1@@ -2189,7 +2189,8 @@ static int ima_measurement(const char *file)log_err ("Unable to read template length\n" ); goto out; } -if (entry.template_len ==0 ) { +if (entry.template_len ==0 || + entry.template_len >MAX_TEMPLATE_SIZE ) {log_err ("Invalid template data len\n" ); goto out; }src/imaevm.h +0
-0@@ -91,6 +91,16 @@#define MAX_DIGEST_SIZE 64#define MAX_SIGNATURE_SIZE 1024
src/imaevm.h line 102
Quoted 10 lines
src/imaevm.h +9
-0+/* +* The maximum template data size is dependent on the template format. For +* example the 'ima-modsig' template includes two signatures - one for the +* entire file, the other without the appended signature - and other fields +* (e.g. file digest, file name, file digest without the appended signature). +* +* Other template formats are much smaller. +*/ +#define MAX_TEMPLATE_SIZE (MAX_SIGNATURE_SIZE * 4)
Quoted 4 lines
src/imaevm.h +1
-0+#define __packed __attribute__((packed))enum evm_ima_xattr_type {
Mimi Zohar
maintainerPATCH 3/4 Sanity check the template data field sizes
In reply to Mimi ZoharThe field sizes of the original "ima" template data are static, but the other template data fields are not. They're prefixed with a size.
Add some data field size sanity checks in ima_show_ng().
--- src/evmctl.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
src/evmctl.c +40 -2
@@ -1591,8 +1591,9 @@ void ima_ng_show(struct template_entry *entry) {uint8_t *fieldp = entry->template;uint32_t field_len; -int total_len = entry->template_len, digest_len, len, sig_len, fbuf_len; +int total_len = entry->template_len, digest_len, len, fbuf_len;uint8_t *digest, *sig =NULL , *fbuf =NULL ; +int sig_len =0 ;char *algo, *path;int found;int err;
@@ -1601,33 +1602,65 @@ void ima_ng_show(struct template_entry *entry) field_len = *(uint32_t *)fieldp; fieldp +=sizeof (field_len); total_len -=sizeof (field_len); +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + } algo = (char *)fieldp; len =strnlen (algo, field_len -1 ) +1 ; digest_len = field_len - len; +if (digest_len <SHA_DIGEST_LENGTH || + digest_len >MAX_DIGEST_SIZE ) { +log_err ("Template \"%s\" invalid digest length\n" , entry->name); +return ; + } digest = fieldp + len;/* move to next field */ fieldp += field_len; total_len -= field_len; +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + }/* get path */ field_len = *(uint32_t *)fieldp; fieldp +=sizeof (field_len); total_len -=sizeof (field_len); +if (field_len ==0 || field_len >PATH_MAX || total_len < field_len) { +log_err ("Template \"%s\" invalid file pathname\n" , entry->name); +return ; + } path = (char *)fieldp;/* move to next field */ fieldp += field_len; total_len -= field_len; +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + }if (!strcmp (entry->name,"ima-sig" ) || !strcmp (entry->name,"ima-sigv2" )) { -/* get signature */ +/* get signature, if it exists */ field_len = *(uint32_t *)fieldp; fieldp +=sizeof (field_len); +if (field_len >MAX_SIGNATURE_SIZE ) { +log_err ("Template \"%s\" invalid file signature size\n" , + entry->name); +return ; + } + total_len -=sizeof (field_len); +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , + entry->name); +return ; + }if (field_len) { sig = fieldp;
@@ -1651,6 +1684,11 @@ void ima_ng_show(struct template_entry *entry) } } +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + } +/* ascii_runtime_measurements */ if (imaevm_params.verbose >LOG_INFO ) {log_info ("%d " , entry->header.pcr);
Signature · 1 line
2.31.1
Re: [PATCH ima-evm-utils 3/4] Sanity check the template data field sizes
Quoted 94 lines · Mimi Zohar
The field sizes of the original "ima" template data are static, but the other template data fields are not. They're prefixed with a size.
Add some data field size sanity checks in
ima_show_ng().Signed-off-by Mimi Zohar <[email protected]>--- src/evmctl.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)src/evmctl.c +35
-2@@ -1591,8 +1591,9 @@ void ima_ng_show(struct template_entry *entry) {uint8_t *fieldp = entry->template;uint32_t field_len; -int total_len = entry->template_len, digest_len, len, sig_len, fbuf_len; +int total_len = entry->template_len, digest_len, len, fbuf_len;uint8_t *digest, *sig =NULL , *fbuf =NULL ; +int sig_len =0 ;char *algo, *path;int found;int err;@@ -1601,33 +1602,65 @@ void ima_ng_show(struct template_entry *entry) field_len = *(uint32_t *)fieldp; fieldp +=sizeof (field_len); total_len -=sizeof (field_len); +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + } algo = (char *)fieldp; len =strnlen (algo, field_len -1 ) +1 ; digest_len = field_len - len; +if (digest_len <SHA_DIGEST_LENGTH || + digest_len >MAX_DIGEST_SIZE ) { +log_err ("Template \"%s\" invalid digest length\n" , entry->name); +return ; + } digest = fieldp + len;/* move to next field */ fieldp += field_len; total_len -= field_len; +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + }/* get path */ field_len = *(uint32_t *)fieldp; fieldp +=sizeof (field_len); total_len -=sizeof (field_len); +if (field_len ==0 || field_len >PATH_MAX || total_len < field_len) { +log_err ("Template \"%s\" invalid file pathname\n" , entry->name); +return ; + } path = (char *)fieldp;/* move to next field */ fieldp += field_len; total_len -= field_len; +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + }if (!strcmp (entry->name,"ima-sig" ) || !strcmp (entry->name,"ima-sigv2" )) { -/* get signature */ +/* get signature, if it exists */ field_len = *(uint32_t *)fieldp; fieldp +=sizeof (field_len); +if (field_len >MAX_SIGNATURE_SIZE ) { +log_err ("Template \"%s\" invalid file signature size\n" , + entry->name); +return ; + } + total_len -=sizeof (field_len); +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , + entry->name); +return ; + }if (field_len) { sig = fieldp;@@ -1651,6 +1684,11 @@ void ima_ng_show(struct template_entry *entry) }
src/evmctl.c void ima_ng_show(struct template_entry *entry) · line 1694
Quoted 10 lines
src/evmctl.c +5
-0} +if (total_len <0 ) { +log_err ("Template \"%s\" invalid template data\n" , entry->name); +return ; + } +/* ascii_runtime_measurements */ if (imaevm_params.verbose >LOG_INFO ) {log_info ("%d " , entry->header.pcr);
Mimi Zohar
maintainerPATCH 4/4 Fix tpm2_pcr_supported() output messages
In reply to Mimi ZoharRemove unnecessary path check in pcr_ibmtss.c and update the syntax in the other.
--- src/pcr_ibmtss.c | 12 +----------- src/pcr_tsspcrread.c | 4 ++-- 2 files changed, 3 insertions(+), 13 deletions(-)
src/pcr_ibmtss.c +1 -11
@@ -20,21 +20,11 @@#undef MAX_DIGEST_SIZE/* imaevm uses a different value than the TSS */ #include <ibmtss/tss.h> -#define CMD "tsspcrread" - -static char path[PATH_MAX ]; -int tpm2_pcr_supported (void ) {if (imaevm_params.verbose >LOG_INFO ) -log_info ("Using %s to read PCRs.\n" ,CMD ); - -if (get_cmd_path (CMD , path,sizeof (path))) { -log_debug ("Couldn't find '%s' in $PATH\n" ,CMD ); -return 0 ; - } +log_info ("Using ibmtss to read PCRs.\n" ); -log_debug ("Found '%s' in $PATH\n" ,CMD );return 1 ; }
src/pcr_tsspcrread.c +2 -2
@@ -60,11 +60,11 @@ int tpm2_pcr_supported(void)log_info ("Using %s to read PCRs.\n" ,CMD );if (get_cmd_path (CMD , path,sizeof (path))) { -log_debug ("Couldn't find '%s' in $PATH\n" ,CMD ); +log_info ("Couldn't find '%s' in %s\n" ,CMD , path);return 0 ; } -log_debug ("Found '%s' in $PATH\n" ,CMD ); +log_debug ("Found '%s' in %s\n" ,CMD , path);return 1 ; }
Signature · 1 line
2.31.1
Re: [PATCH ima-evm-utils 4/4] Fix tpm2_pcr_supported() output messages
Quoted 44 lines · Mimi Zohar
Remove unnecessary path check in
pcr_ibmtss.cand update the syntax in the other.Signed-off-by Mimi Zohar <[email protected]>--- src/pcr_ibmtss.c | 12 +----------- src/pcr_tsspcrread.c | 4 ++-- 2 files changed, 3 insertions(+), 13 deletions(-)src/pcr_ibmtss.c +1
-11@@ -20,21 +20,11 @@#undef MAX_DIGEST_SIZE/* imaevm uses a different value than the TSS */ #include <ibmtss/tss.h> -#define CMD "tsspcrread" - -static char path[PATH_MAX ]; -int tpm2_pcr_supported (void ) {if (imaevm_params.verbose >LOG_INFO ) -log_info ("Using %s to read PCRs.\n" ,CMD ); - -if (get_cmd_path (CMD , path,sizeof (path))) { -log_debug ("Couldn't find '%s' in $PATH\n" ,CMD ); -return 0 ; - } +log_info ("Using ibmtss to read PCRs.\n" ); -log_debug ("Found '%s' in $PATH\n" ,CMD );return 1 ; }src/pcr_tsspcrread.c +0
-0@@ -60,11 +60,11 @@ int tpm2_pcr_supported(void)log_info ("Using %s to read PCRs.\n" ,CMD );if (get_cmd_path (CMD , path,sizeof (path))) {
src/pcr_tsspcrread.c int tpm2_pcr_supported(void) · line 70
Quoted 10 lines
src/pcr_tsspcrread.c +2
-2-log_debug ("Couldn't find '%s' in $PATH\n" ,CMD ); +log_info ("Couldn't find '%s' in %s\n" ,CMD , path);return 0 ; } -log_debug ("Found '%s' in $PATH\n" ,CMD ); +log_debug ("Found '%s' in %s\n" ,CMD , path);return 1 ; }