diff mbox series

[ima-evm-utils,1/4] Don't ignore number of items read

Message ID 20220914142225.1381077-2-zohar@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series misc bug and other fixes | expand

Commit Message

Mimi Zohar Sept. 14, 2022, 2:22 p.m. UTC
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 <zohar@linux.ibm.com>
---
 src/evmctl.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Comments

Stefan Berger Sept. 14, 2022, 9:30 p.m. UTC | #1
On 9/14/22 10:22, Mimi Zohar wrote:
> 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 <zohar@linux.ibm.com>
> ---
>   src/evmctl.c | 12 +++++++-----
>   1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/src/evmctl.c b/src/evmctl.c
> index 2e21da67c444..bcf724c828f7 100644
> --- a/src/evmctl.c
> +++ b/src/evmctl.c
> @@ -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;
>   			}

It was correct before as well ...

Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Mimi Zohar Sept. 15, 2022, 12:07 p.m. UTC | #2
Hi Stefan,

Thank you for this and the other reviews.

On Wed, 2022-09-14 at 17:30 -0400, Stefan Berger wrote:
> 
> On 9/14/22 10:22, Mimi Zohar wrote:
> > 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 <zohar@linux.ibm.com>


> 
> It was correct before as well ...
> 
> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>

Yeah, I'll update the patch description explaining that this stops the
static analysis complaints
diff mbox series

Patch

diff --git a/src/evmctl.c b/src/evmctl.c
index 2e21da67c444..bcf724c828f7 100644
--- a/src/evmctl.c
+++ b/src/evmctl.c
@@ -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;
 			}