diff mbox

[1/3] ima_fs: One check less in ima_write_policy() after error detection

Message ID 28a6918c-5714-cec8-2df7-85bcc37e4d75@users.sourceforge.net (mailing list archive)
State New, archived
Headers show

Commit Message

SF Markus Elfring Jan. 25, 2017, 9:31 a.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Tue, 24 Jan 2017 20:30:55 +0100

Move the jump label directly before the desired assignment for the
variable "valid_policy" at the end so that the variable "result" will not
be checked once more after it was determined that a received input
parameter was not zero or a memory allocation failed.
Use the identifier "reset_validity" instead of the label "out".

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 security/integrity/ima/ima_fs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Mimi Zohar Jan. 27, 2017, 12:38 p.m. UTC | #1
On Wed, 2017-01-25 at 10:31 +0100, SF Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 24 Jan 2017 20:30:55 +0100
> 
> Move the jump label directly before the desired assignment for the
> variable "valid_policy" at the end so that the variable "result" will not
> be checked once more after it was determined that a received input
> parameter was not zero or a memory allocation failed.
> Use the identifier "reset_validity" instead of the label "out".
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  security/integrity/ima/ima_fs.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
> index ca303e5d2b94..c1c8d34d111d 100644
> --- a/security/integrity/ima/ima_fs.c
> +++ b/security/integrity/ima/ima_fs.c
> @@ -321,12 +321,12 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
>  	/* No partial writes. */
>  	result = -EINVAL;
>  	if (*ppos != 0)
> -		goto out;
> +		goto reset_validity;
> 
>  	result = -ENOMEM;
>  	data = kmalloc(datalen + 1, GFP_KERNEL);
>  	if (!data)
> -		goto out;
> +		goto reset_validity;
> 
>  	*(data + datalen) = '\0';
> 
> @@ -353,8 +353,8 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
>  	mutex_unlock(&ima_write_mutex);
>  out_free:
>  	kfree(data);
> -out:
>  	if (result < 0)
> +reset_validity:

Really?!   Do you really think this makes the code more readable?   A
more common, readable approach is to have two exit points - a normal
exit and an error exit.   Let's leave it to the compiler to do the
optimization.

Mimi

>  		valid_policy = 0;
> 
>  	return result;


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
index ca303e5d2b94..c1c8d34d111d 100644
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@ -321,12 +321,12 @@  static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 	/* No partial writes. */
 	result = -EINVAL;
 	if (*ppos != 0)
-		goto out;
+		goto reset_validity;
 
 	result = -ENOMEM;
 	data = kmalloc(datalen + 1, GFP_KERNEL);
 	if (!data)
-		goto out;
+		goto reset_validity;
 
 	*(data + datalen) = '\0';
 
@@ -353,8 +353,8 @@  static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 	mutex_unlock(&ima_write_mutex);
 out_free:
 	kfree(data);
-out:
 	if (result < 0)
+reset_validity:
 		valid_policy = 0;
 
 	return result;