[v1,4/5] ima-evm-utils: Fix file2bin stat and fopen relations
diff mbox series

Message ID 20190712212833.29280-4-vt@altlinux.org
State New
Headers show
Series
  • [v1,1/5] ima-evm-utils: Fix null dereference from file2bin to memcpy
Related show

Commit Message

Vitaly Chikunov July 12, 2019, 9:28 p.m. UTC
Check stat(2) return value, use fstat(2) to avoid race between
stat() and fopen(), remove now unused get_filesize().
Fixes CID 229889.
---
 src/evmctl.c    | 25 ++++++++++++++++++++-----
 src/imaevm.h    |  1 -
 src/libimaevm.c |  8 --------
 3 files changed, 20 insertions(+), 14 deletions(-)

Comments

Mimi Zohar July 15, 2019, 7:09 p.m. UTC | #1
On Sat, 2019-07-13 at 00:28 +0300, Vitaly Chikunov wrote:

<snip>

> @@ -186,18 +187,32 @@ static unsigned char *file2bin(const char *file, const char *ext, int *size)
> 
>  	log_info("Reading to %s\n", name);
> 
> -	len = get_filesize(name);
>  	fp = fopen(name, "r");
>  	if (!fp) {
>  		log_err("Failed to open: %s\n", name);
>  		return NULL;
>  	}
> +	if (fstat(fileno(fp), &stats) == -1) {
> +		log_err("Failed to fstat: %s (%s)\n", name, strerror(errno));
> +		fclose(fp);
> +		return NULL;
> +	}
> +	len = stats.st_size;
> +
>  	data = malloc(len);
> -	if (!fread(data, len, 1, fp))
> -		len = 0;
> +	if (!data) {
> +		log_err("Failed to malloc %zu bytes: %s\n", len, name);

Missing "free(data)"

> +		fclose(fp);
> +		return NULL;
> +	}
> +	if (fread(data, len, 1, fp) != len) {
> +		log_err("Failed to fread %zu bytes: %s\n", len, name);
> +		fclose(fp);

and here
> +		return NULL;
> +	}
>  	fclose(fp);
> 
> -	*size = len;
> +	*size = (int)len;
>  	return data;
>  }
>
Vitaly Chikunov July 15, 2019, 8:04 p.m. UTC | #2
Mimi,

On Mon, Jul 15, 2019 at 03:09:34PM -0400, Mimi Zohar wrote:
> On Sat, 2019-07-13 at 00:28 +0300, Vitaly Chikunov wrote:
> > @@ -186,18 +187,32 @@ static unsigned char *file2bin(const char *file, const char *ext, int *size)
> > 
> >  	log_info("Reading to %s\n", name);
> > 
> > -	len = get_filesize(name);
> >  	fp = fopen(name, "r");
> >  	if (!fp) {
> >  		log_err("Failed to open: %s\n", name);
> >  		return NULL;
> >  	}
> > +	if (fstat(fileno(fp), &stats) == -1) {
> > +		log_err("Failed to fstat: %s (%s)\n", name, strerror(errno));
> > +		fclose(fp);
> > +		return NULL;
> > +	}
> > +	len = stats.st_size;
> > +
> >  	data = malloc(len);
> > -	if (!fread(data, len, 1, fp))
> > -		len = 0;
> > +	if (!data) {
> > +		log_err("Failed to malloc %zu bytes: %s\n", len, name);
> 
> Missing "free(data)"

In the next patch set I will apply all your suggestions except this one,
because this is return where data was not allocated.


> > +		fclose(fp);
> > +		return NULL;
> > +	}
> > +	if (fread(data, len, 1, fp) != len) {
> > +		log_err("Failed to fread %zu bytes: %s\n", len, name);
> > +		fclose(fp);
> 
> and here
> > +		return NULL;
> > +	}
> >  	fclose(fp);
> > 
> > -	*size = len;
> > +	*size = (int)len;
> >  	return data;
> >  }
> >

Patch
diff mbox series

diff --git a/src/evmctl.c b/src/evmctl.c
index c556d05..f60c12e 100644
--- a/src/evmctl.c
+++ b/src/evmctl.c
@@ -175,9 +175,10 @@  static int bin2file(const char *file, const char *ext, const unsigned char *data
 static unsigned char *file2bin(const char *file, const char *ext, int *size)
 {
 	FILE *fp;
-	int len;
+	size_t len;
 	unsigned char *data;
 	char name[strlen(file) + (ext ? strlen(ext) : 0) + 2];
+	struct stat stats;
 
 	if (ext)
 		sprintf(name, "%s.%s", file, ext);
@@ -186,18 +187,32 @@  static unsigned char *file2bin(const char *file, const char *ext, int *size)
 
 	log_info("Reading to %s\n", name);
 
-	len = get_filesize(name);
 	fp = fopen(name, "r");
 	if (!fp) {
 		log_err("Failed to open: %s\n", name);
 		return NULL;
 	}
+	if (fstat(fileno(fp), &stats) == -1) {
+		log_err("Failed to fstat: %s (%s)\n", name, strerror(errno));
+		fclose(fp);
+		return NULL;
+	}
+	len = stats.st_size;
+
 	data = malloc(len);
-	if (!fread(data, len, 1, fp))
-		len = 0;
+	if (!data) {
+		log_err("Failed to malloc %zu bytes: %s\n", len, name);
+		fclose(fp);
+		return NULL;
+	}
+	if (fread(data, len, 1, fp) != len) {
+		log_err("Failed to fread %zu bytes: %s\n", len, name);
+		fclose(fp);
+		return NULL;
+	}
 	fclose(fp);
 
-	*size = len;
+	*size = (int)len;
 	return data;
 }
 
diff --git a/src/imaevm.h b/src/imaevm.h
index dc81a3a..36050f4 100644
--- a/src/imaevm.h
+++ b/src/imaevm.h
@@ -211,7 +211,6 @@  extern struct libevm_params params;
 
 void do_dump(FILE *fp, const void *ptr, int len, bool cr);
 void dump(const void *ptr, int len);
-int get_filesize(const char *filename);
 int ima_calc_hash(const char *file, uint8_t *hash);
 int get_hash_algo(const char *algo);
 RSA *read_pub_key(const char *keyfile, int x509);
diff --git a/src/libimaevm.c b/src/libimaevm.c
index f8ab812..1562aaf 100644
--- a/src/libimaevm.c
+++ b/src/libimaevm.c
@@ -116,14 +116,6 @@  const char *get_hash_algo_by_id(int algo)
 	return "unknown";
 }
 
-int get_filesize(const char *filename)
-{
-	struct stat stats;
-	/*  Need to know the file length */
-	stat(filename, &stats);
-	return (int)stats.st_size;
-}
-
 static inline off_t get_fdsize(int fd)
 {
 	struct stat stats;