linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] ima_fs: Fine-tuning for ima_write_policy()
@ 2017-01-25  9:30 SF Markus Elfring
  2017-01-25  9:31 ` [PATCH 1/3] ima_fs: One check less in ima_write_policy() after error detection SF Markus Elfring
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-01-25  9:30 UTC (permalink / raw)
  To: linux-ima-devel, linux-ima-user, linux-security-module,
	Dmitry Kasatkin, James Morris, Mimi Zohar, Serge E. Hallyn
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 25 Jan 2017 10:20:30 +0100

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (3):
  One check less after error detection
  Reorder input parameter validation
  Move three error code assignments

 security/integrity/ima/ima_fs.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

-- 
2.11.0

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] ima_fs: One check less in ima_write_policy() after error detection
  2017-01-25  9:30 [PATCH 0/3] ima_fs: Fine-tuning for ima_write_policy() SF Markus Elfring
@ 2017-01-25  9:31 ` SF Markus Elfring
  2017-01-27 12:38   ` Mimi Zohar
  2017-01-25  9:33 ` [PATCH 2/3] ima_fs: Reorder input parameter validation in ima_write_policy() SF Markus Elfring
  2017-01-25  9:34 ` [PATCH 3/3] ima_fs: Move three error code assignments " SF Markus Elfring
  2 siblings, 1 reply; 7+ messages in thread
From: SF Markus Elfring @ 2017-01-25  9:31 UTC (permalink / raw)
  To: linux-ima-devel, linux-ima-user, linux-security-module,
	Dmitry Kasatkin, James Morris, Mimi Zohar, Serge E. Hallyn
  Cc: LKML, kernel-janitors

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:
 		valid_policy = 0;
 
 	return result;
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] ima_fs: Reorder input parameter validation in ima_write_policy()
  2017-01-25  9:30 [PATCH 0/3] ima_fs: Fine-tuning for ima_write_policy() SF Markus Elfring
  2017-01-25  9:31 ` [PATCH 1/3] ima_fs: One check less in ima_write_policy() after error detection SF Markus Elfring
@ 2017-01-25  9:33 ` SF Markus Elfring
  2017-01-25  9:34 ` [PATCH 3/3] ima_fs: Move three error code assignments " SF Markus Elfring
  2 siblings, 0 replies; 7+ messages in thread
From: SF Markus Elfring @ 2017-01-25  9:33 UTC (permalink / raw)
  To: linux-ima-devel, linux-ima-user, linux-security-module,
	Dmitry Kasatkin, James Morris, Mimi Zohar, Serge E. Hallyn
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Tue, 24 Jan 2017 22:38:00 +0100

Move validation for the input parameter "ppos" to the beginning in this
function so that a following check for the input parameter "datalen"
can be occasionally avoided earlier.

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

diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
index c1c8d34d111d..98304411915d 100644
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@ -315,15 +315,14 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 	char *data;
 	ssize_t result;
 
-	if (datalen >= PAGE_SIZE)
-		datalen = PAGE_SIZE - 1;
-
 	/* No partial writes. */
 	result = -EINVAL;
 	if (*ppos != 0)
 		goto reset_validity;
 
 	result = -ENOMEM;
+	if (datalen >= PAGE_SIZE)
+		datalen = PAGE_SIZE - 1;
 	data = kmalloc(datalen + 1, GFP_KERNEL);
 	if (!data)
 		goto reset_validity;
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] ima_fs: Move three error code assignments in ima_write_policy()
  2017-01-25  9:30 [PATCH 0/3] ima_fs: Fine-tuning for ima_write_policy() SF Markus Elfring
  2017-01-25  9:31 ` [PATCH 1/3] ima_fs: One check less in ima_write_policy() after error detection SF Markus Elfring
  2017-01-25  9:33 ` [PATCH 2/3] ima_fs: Reorder input parameter validation in ima_write_policy() SF Markus Elfring
@ 2017-01-25  9:34 ` SF Markus Elfring
  2017-01-27 12:39   ` Mimi Zohar
  2 siblings, 1 reply; 7+ messages in thread
From: SF Markus Elfring @ 2017-01-25  9:34 UTC (permalink / raw)
  To: linux-ima-devel, linux-ima-user, linux-security-module,
	Dmitry Kasatkin, James Morris, Mimi Zohar, Serge E. Hallyn
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Tue, 24 Jan 2017 22:47:07 +0100

A local variable was set to an error code in three cases before a concrete
error situation was detected. Thus move the corresponding assignments into
if branches to indicate a software failure there.

This issue was detected by using the Coccinelle software.

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

diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
index 98304411915d..a50c26f9772c 100644
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@ -317,21 +317,24 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 
 	/* No partial writes. */
 	result = -EINVAL;
-	if (*ppos != 0)
+	if (*ppos != 0) {
+		result = -EINVAL;
 		goto reset_validity;
+	}
 
-	result = -ENOMEM;
 	if (datalen >= PAGE_SIZE)
 		datalen = PAGE_SIZE - 1;
 	data = kmalloc(datalen + 1, GFP_KERNEL);
-	if (!data)
+	if (!data) {
+		result = -ENOMEM;
 		goto reset_validity;
+	}
 
 	*(data + datalen) = '\0';
-
-	result = -EFAULT;
-	if (copy_from_user(data, buf, datalen))
+	if (copy_from_user(data, buf, datalen)) {
+		result = -EFAULT;
 		goto out_free;
+	}
 
 	result = mutex_lock_interruptible(&ima_write_mutex);
 	if (result < 0)
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] ima_fs: One check less in ima_write_policy() after error detection
  2017-01-25  9:31 ` [PATCH 1/3] ima_fs: One check less in ima_write_policy() after error detection SF Markus Elfring
@ 2017-01-27 12:38   ` Mimi Zohar
  0 siblings, 0 replies; 7+ messages in thread
From: Mimi Zohar @ 2017-01-27 12:38 UTC (permalink / raw)
  To: SF Markus Elfring
  Cc: linux-ima-devel, linux-ima-user, linux-security-module,
	Dmitry Kasatkin, James Morris, Serge E. Hallyn, LKML,
	kernel-janitors

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;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] ima_fs: Move three error code assignments in ima_write_policy()
  2017-01-25  9:34 ` [PATCH 3/3] ima_fs: Move three error code assignments " SF Markus Elfring
@ 2017-01-27 12:39   ` Mimi Zohar
  2017-01-29 23:43     ` James Morris
  0 siblings, 1 reply; 7+ messages in thread
From: Mimi Zohar @ 2017-01-27 12:39 UTC (permalink / raw)
  To: SF Markus Elfring
  Cc: linux-ima-devel, linux-ima-user, linux-security-module,
	Dmitry Kasatkin, James Morris, Serge E. Hallyn, LKML,
	kernel-janitors

On Wed, 2017-01-25 at 10:34 +0100, SF Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 24 Jan 2017 22:47:07 +0100
> 
> A local variable was set to an error code in three cases before a concrete
> error situation was detected. Thus move the corresponding assignments into
> if branches to indicate a software failure there.
> 
> This issue was detected by using the Coccinelle software.

This coding style was pretty common.  I assume the compiler is smart
enough to do the right thing.  Is this a FYI, letting us know for the
future the preferred coding style, or are we really upstreaming these
sorts of coding style changes?

Mimi

> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  security/integrity/ima/ima_fs.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
> index 98304411915d..a50c26f9772c 100644
> --- a/security/integrity/ima/ima_fs.c
> +++ b/security/integrity/ima/ima_fs.c
> @@ -317,21 +317,24 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
> 
>  	/* No partial writes. */
>  	result = -EINVAL;
> -	if (*ppos != 0)
> +	if (*ppos != 0) {
> +		result = -EINVAL;
>  		goto reset_validity;
> +	}
> 
> -	result = -ENOMEM;
>  	if (datalen >= PAGE_SIZE)
>  		datalen = PAGE_SIZE - 1;
>  	data = kmalloc(datalen + 1, GFP_KERNEL);
> -	if (!data)
> +	if (!data) {
> +		result = -ENOMEM;
>  		goto reset_validity;
> +	}
> 
>  	*(data + datalen) = '\0';
> -
> -	result = -EFAULT;
> -	if (copy_from_user(data, buf, datalen))
> +	if (copy_from_user(data, buf, datalen)) {
> +		result = -EFAULT;
>  		goto out_free;
> +	}
> 
>  	result = mutex_lock_interruptible(&ima_write_mutex);
>  	if (result < 0)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] ima_fs: Move three error code assignments in ima_write_policy()
  2017-01-27 12:39   ` Mimi Zohar
@ 2017-01-29 23:43     ` James Morris
  0 siblings, 0 replies; 7+ messages in thread
From: James Morris @ 2017-01-29 23:43 UTC (permalink / raw)
  To: Mimi Zohar
  Cc: SF Markus Elfring, linux-ima-devel, linux-ima-user,
	linux-security-module, Dmitry Kasatkin, James Morris,
	Serge E. Hallyn, LKML, kernel-janitors

On Fri, 27 Jan 2017, Mimi Zohar wrote:

> On Wed, 2017-01-25 at 10:34 +0100, SF Markus Elfring wrote:
> > From: Markus Elfring <elfring@users.sourceforge.net>
> > Date: Tue, 24 Jan 2017 22:47:07 +0100
> > 
> > A local variable was set to an error code in three cases before a concrete
> > error situation was detected. Thus move the corresponding assignments into
> > if branches to indicate a software failure there.
> > 
> > This issue was detected by using the Coccinelle software.
> 
> This coding style was pretty common.  I assume the compiler is smart
> enough to do the right thing.  Is this a FYI, letting us know for the
> future the preferred coding style, or are we really upstreaming these
> sorts of coding style changes?

Nope, and I generally don't want cleanup patches from Markus going into 
the security tree.

See also:
https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1254425.html


- James
-- 
James Morris
<jmorris@namei.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-01-30  0:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-25  9:30 [PATCH 0/3] ima_fs: Fine-tuning for ima_write_policy() SF Markus Elfring
2017-01-25  9:31 ` [PATCH 1/3] ima_fs: One check less in ima_write_policy() after error detection SF Markus Elfring
2017-01-27 12:38   ` Mimi Zohar
2017-01-25  9:33 ` [PATCH 2/3] ima_fs: Reorder input parameter validation in ima_write_policy() SF Markus Elfring
2017-01-25  9:34 ` [PATCH 3/3] ima_fs: Move three error code assignments " SF Markus Elfring
2017-01-27 12:39   ` Mimi Zohar
2017-01-29 23:43     ` James Morris

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).