1
0
mirror of https://git.code.sf.net/p/linux-ima/ima-evm-utils synced 2025-07-02 13:43:16 +02:00

ima-evm-utils: Fix file2bin stat and fopen relations

Check stat(2) return value, use fstat(2) to avoid race between
stat() and fopen(), remove now unused get_filesize().

Fixes: CID 229889.
Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
This commit is contained in:
Vitaly Chikunov
2019-07-15 23:05:52 +03:00
committed by Mimi Zohar
parent 9d52489bd3
commit 08a51e7460
3 changed files with 21 additions and 14 deletions

View File

@ -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,33 @@ 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);
free(data);
return NULL;
}
fclose(fp);
*size = len;
*size = (int)len;
return data;
}