ima: skip memory allocation for empty files
authorDmitry Kasatkin <d.kasatkin@samsung.com>
Thu, 27 Feb 2014 18:16:47 +0000 (20:16 +0200)
committerMimi Zohar <zohar@linux.vnet.ibm.com>
Fri, 7 Mar 2014 17:15:48 +0000 (12:15 -0500)
Memory allocation is unnecessary for empty files.
This patch calculates the hash without memory allocation.

Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
security/integrity/ima/ima_crypto.c

index d257e3631152cd45d32d01786862fa1505c02728..1bde8e62776620e6e5ed2cb2900d39c91df992f8 100644 (file)
@@ -87,16 +87,20 @@ static int ima_calc_file_hash_tfm(struct file *file,
        if (rc != 0)
                return rc;
 
-       rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
-       if (!rbuf) {
-               rc = -ENOMEM;
+       i_size = i_size_read(file_inode(file));
+
+       if (i_size == 0)
                goto out;
-       }
+
+       rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+       if (!rbuf)
+               return -ENOMEM;
+
        if (!(file->f_mode & FMODE_READ)) {
                file->f_mode |= FMODE_READ;
                read = 1;
        }
-       i_size = i_size_read(file_inode(file));
+
        while (offset < i_size) {
                int rbuf_len;
 
@@ -113,12 +117,12 @@ static int ima_calc_file_hash_tfm(struct file *file,
                if (rc)
                        break;
        }
-       kfree(rbuf);
-       if (!rc)
-               rc = crypto_shash_final(&desc.shash, hash->digest);
        if (read)
                file->f_mode &= ~FMODE_READ;
+       kfree(rbuf);
 out:
+       if (!rc)
+               rc = crypto_shash_final(&desc.shash, hash->digest);
        return rc;
 }