linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* linux-next: manual merge of the security tree with the vfs tree
@ 2015-12-31  4:24 Stephen Rothwell
  2015-12-31  4:30 ` Al Viro
  2016-01-04  1:37 ` linux-next: manual merge of the security tree with the vfs tree Mimi Zohar
  0 siblings, 2 replies; 8+ messages in thread
From: Stephen Rothwell @ 2015-12-31  4:24 UTC (permalink / raw)
  To: James Morris, Al Viro; +Cc: linux-next, linux-kernel, Petko Manolov, Mimi Zohar

Hi James,

Today's linux-next merge of the security tree got a conflict in:

  security/integrity/ima/ima_fs.c

between commit:

  3bc8f29b149e ("new helper: memdup_user_nul()")

from the vfs tree and commit:

  38d859f991f3 ("IMA: policy can now be updated multiple times")

from the security tree.

I fixed it up (hopefully, see below) and can carry the fix as necessary
(no action is required).

-- 
Cheers,
Stephen Rothwell                    sfr@canb.auug.org.au

diff --cc security/integrity/ima/ima_fs.c
index 71aa60b8d257,3caed6de610c..000000000000
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@@ -259,21 -261,35 +261,30 @@@ static const struct file_operations ima
  static ssize_t ima_write_policy(struct file *file, const char __user *buf,
  				size_t datalen, loff_t *ppos)
  {
 -	char *data = NULL;
  	ssize_t result;
- 	char *data;
++	char *data = NULL;
+ 	int res;
+ 
+ 	res = mutex_lock_interruptible(&ima_write_mutex);
+ 	if (res)
+ 		return res;
  
  	if (datalen >= PAGE_SIZE)
  		datalen = PAGE_SIZE - 1;
  
  	/* No partial writes. */
+ 	result = -EINVAL;
  	if (*ppos != 0)
- 		return -EINVAL;
+ 		goto out;
  
 -	result = -ENOMEM;
 -	data = kmalloc(datalen + 1, GFP_KERNEL);
 -	if (!data)
 -		goto out;
 -
 -	*(data + datalen) = '\0';
 -
 -	result = -EFAULT;
 -	if (copy_from_user(data, buf, datalen))
 +	data = memdup_user_nul(buf, datalen);
- 	if (IS_ERR(data))
- 		return PTR_ERR(data);
++	if (IS_ERR(data)) {
++		result = PTR_ERR(data);
+ 		goto out;
++	}
  
  	result = ima_parse_add_rule(data);
+ out:
  	if (result < 0)
  		valid_policy = 0;
  	kfree(data);

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

* Re: linux-next: manual merge of the security tree with the vfs tree
  2015-12-31  4:24 linux-next: manual merge of the security tree with the vfs tree Stephen Rothwell
@ 2015-12-31  4:30 ` Al Viro
  2015-12-31 10:45   ` Petko Manolov
  2016-01-04  1:37 ` linux-next: manual merge of the security tree with the vfs tree Mimi Zohar
  1 sibling, 1 reply; 8+ messages in thread
From: Al Viro @ 2015-12-31  4:30 UTC (permalink / raw)
  To: Stephen Rothwell
  Cc: James Morris, linux-next, linux-kernel, Petko Manolov, Mimi Zohar

On Thu, Dec 31, 2015 at 03:24:53PM +1100, Stephen Rothwell wrote:
> Hi James,
> 
> Today's linux-next merge of the security tree got a conflict in:
> 
>   security/integrity/ima/ima_fs.c
> 
> between commit:
> 
>   3bc8f29b149e ("new helper: memdup_user_nul()")
> 
> from the vfs tree and commit:
> 
>   38d859f991f3 ("IMA: policy can now be updated multiple times")
> 
> from the security tree.
> 
> I fixed it up (hopefully, see below) and can carry the fix as necessary
> (no action is required).
 
> + 	res = mutex_lock_interruptible(&ima_write_mutex);
> + 	if (res)
> + 		return res;
>   
>   	if (datalen >= PAGE_SIZE)
>   		datalen = PAGE_SIZE - 1;
>   
>   	/* No partial writes. */
> + 	result = -EINVAL;
>   	if (*ppos != 0)
> - 		return -EINVAL;
> + 		goto out;
>   
>  -	result = -ENOMEM;
>  -	data = kmalloc(datalen + 1, GFP_KERNEL);
>  -	if (!data)
>  -		goto out;
>  -
>  -	*(data + datalen) = '\0';
>  -
>  -	result = -EFAULT;
>  -	if (copy_from_user(data, buf, datalen))
>  +	data = memdup_user_nul(buf, datalen);
> - 	if (IS_ERR(data))
> - 		return PTR_ERR(data);
> ++	if (IS_ERR(data)) {
> ++		result = PTR_ERR(data);
> + 		goto out;
> ++	}

Why do it in this order?  With or without opencoding memdup_user_nul(),
what's the point of taking the mutex before copying the data from
userland?  All it achieves is holding it longer, over the area that
needs no exclusion whatsoever.

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

* Re: linux-next: manual merge of the security tree with the vfs tree
  2015-12-31  4:30 ` Al Viro
@ 2015-12-31 10:45   ` Petko Manolov
  2016-01-01  4:34     ` Al Viro
  0 siblings, 1 reply; 8+ messages in thread
From: Petko Manolov @ 2015-12-31 10:45 UTC (permalink / raw)
  To: Al Viro
  Cc: Stephen Rothwell, James Morris, linux-next, linux-kernel, Mimi Zohar

On 15-12-31 04:30:19, Al Viro wrote:
> On Thu, Dec 31, 2015 at 03:24:53PM +1100, Stephen Rothwell wrote:
> > Hi James,
> > 
> > Today's linux-next merge of the security tree got a conflict in:
> > 
> >   security/integrity/ima/ima_fs.c
> > 
> > between commit:
> > 
> >   3bc8f29b149e ("new helper: memdup_user_nul()")
> > 
> > from the vfs tree and commit:
> > 
> >   38d859f991f3 ("IMA: policy can now be updated multiple times")
> > 
> > from the security tree.
> > 
> > I fixed it up (hopefully, see below) and can carry the fix as necessary
> > (no action is required).
>  
> > + 	res = mutex_lock_interruptible(&ima_write_mutex);
> > + 	if (res)
> > + 		return res;
> >   
> >   	if (datalen >= PAGE_SIZE)
> >   		datalen = PAGE_SIZE - 1;
> >   
> >   	/* No partial writes. */
> > + 	result = -EINVAL;
> >   	if (*ppos != 0)
> > - 		return -EINVAL;
> > + 		goto out;
> >   
> >  -	result = -ENOMEM;
> >  -	data = kmalloc(datalen + 1, GFP_KERNEL);
> >  -	if (!data)
> >  -		goto out;
> >  -
> >  -	*(data + datalen) = '\0';
> >  -
> >  -	result = -EFAULT;
> >  -	if (copy_from_user(data, buf, datalen))
> >  +	data = memdup_user_nul(buf, datalen);
> > - 	if (IS_ERR(data))
> > - 		return PTR_ERR(data);
> > ++	if (IS_ERR(data)) {
> > ++		result = PTR_ERR(data);
> > + 		goto out;
> > ++	}
> 
> Why do it in this order?  With or without opencoding memdup_user_nul(),
> what's the point of taking the mutex before copying the data from
> userland?  All it achieves is holding it longer, over the area that
> needs no exclusion whatsoever.

I introduced the write mutex when ima_write_policy() stopped being serialized by 
other means.  Come to think about it the semaphore could be taken right before 
copy_from_user() so it is my fault, not Stephen's.

The patch, however, leaves out a bug where free without allocation can occur.  
Look at *ppos evaluation.  Instead of "goto out" it should be "return -EINVAL;".  
This requires the mutex lock to be moved down, though.


cheers,
Petko

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

* Re: linux-next: manual merge of the security tree with the vfs tree
  2015-12-31 10:45   ` Petko Manolov
@ 2016-01-01  4:34     ` Al Viro
  2016-01-01 17:29       ` Petko Manolov
  0 siblings, 1 reply; 8+ messages in thread
From: Al Viro @ 2016-01-01  4:34 UTC (permalink / raw)
  To: Petko Manolov
  Cc: Stephen Rothwell, James Morris, linux-next, linux-kernel, Mimi Zohar

On Thu, Dec 31, 2015 at 12:45:35PM +0200, Petko Manolov wrote:

> I introduced the write mutex when ima_write_policy() stopped being serialized by 
> other means.  Come to think about it the semaphore could be taken right before 
> copy_from_user() so it is my fault, not Stephen's.

s/before/after/, surely?

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

* Re: linux-next: manual merge of the security tree with the vfs tree
  2016-01-01  4:34     ` Al Viro
@ 2016-01-01 17:29       ` Petko Manolov
  2016-01-01 17:41         ` [PATCH] ima_write_policy() optimizations; kbuild test robot
  0 siblings, 1 reply; 8+ messages in thread
From: Petko Manolov @ 2016-01-01 17:29 UTC (permalink / raw)
  To: Al Viro
  Cc: Stephen Rothwell, James Morris, linux-next, linux-kernel, Mimi Zohar

On 16-01-01 04:34:16, Al Viro wrote:
> On Thu, Dec 31, 2015 at 12:45:35PM +0200, Petko Manolov wrote:
> 
> > I introduced the write mutex when ima_write_policy() stopped being serialized by 
> > other means.  Come to think about it the semaphore could be taken right before 
> > copy_from_user() so it is my fault, not Stephen's.
> 
> s/before/after/, surely?

Right.  This is a quick patch which i hope solves most issues...


		Petko


>From 6c9058009c59fda5b8e98a3fc09497ce3efdb3e9 Mon Sep 17 00:00:00 2001
From: Petko Manolov <petkan@mip-labs.com>
Date: Fri, 1 Jan 2016 19:10:43 +0200
Subject: [PATCH] ima_write_policy() optimizations;

There is no need to hold the write semaphore for so long.  We only need it
around ima_parse_add_rule();

The return path now takes into account failed kmalloc() call.

Signed-off-by: Petko Manolov <petkan@mip-labs.com>
---
 security/integrity/ima/ima_fs.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
index 3caed6d..d2c0d55 100644
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@ -261,13 +261,7 @@ static const struct file_operations ima_ascii_measurements_ops = {
 static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 				size_t datalen, loff_t *ppos)
 {
-	char *data = NULL;
 	ssize_t result;
-	int res;
-
-	res = mutex_lock_interruptible(&ima_write_mutex);
-	if (res)
-		return res;
 
 	if (datalen >= PAGE_SIZE)
 		datalen = PAGE_SIZE - 1;
@@ -286,15 +280,21 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 
 	result = -EFAULT;
 	if (copy_from_user(data, buf, datalen))
-		goto out;
+		goto out_free;
+
+	result = mutex_lock_interruptible(&ima_write_mutex);
+	if (result)
+		goto out_free;
 
 	result = ima_parse_add_rule(data);
-out:
+
+	mutex_unlock(&ima_write_mutex);
+
 	if (result < 0)
 		valid_policy = 0;
+out_free:
 	kfree(data);
-	mutex_unlock(&ima_write_mutex);
-
+out:
 	return result;
 }
 
-- 
2.6.4



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

* Re: [PATCH] ima_write_policy() optimizations;
  2016-01-01 17:29       ` Petko Manolov
@ 2016-01-01 17:41         ` kbuild test robot
  0 siblings, 0 replies; 8+ messages in thread
From: kbuild test robot @ 2016-01-01 17:41 UTC (permalink / raw)
  To: Petko Manolov
  Cc: kbuild-all, Al Viro, Stephen Rothwell, James Morris, linux-next,
	linux-kernel, Mimi Zohar

[-- Attachment #1: Type: text/plain, Size: 2369 bytes --]

Hi Petko,

[auto build test ERROR on integrity/next]
[cannot apply to v4.4-rc7 next-20151231]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Petko-Manolov/ima_write_policy-optimizations/20160102-013037
base:   https://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git next
config: i386-randconfig-x002-12300610 (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

   security/integrity/ima/ima_fs.c:275:9: sparse: undefined identifier 'data'
   security/integrity/ima/ima_fs.c:276:14: sparse: undefined identifier 'data'
   security/integrity/ima/ima_fs.c:279:11: sparse: undefined identifier 'data'
   security/integrity/ima/ima_fs.c:282:28: sparse: undefined identifier 'data'
   security/integrity/ima/ima_fs.c:289:37: sparse: undefined identifier 'data'
   security/integrity/ima/ima_fs.c:296:15: sparse: undefined identifier 'data'
   security/integrity/ima/ima_fs.c: In function 'ima_write_policy':
>> security/integrity/ima/ima_fs.c:275:2: error: 'data' undeclared (first use in this function)
     data = kmalloc(datalen + 1, GFP_KERNEL);
     ^
   security/integrity/ima/ima_fs.c:275:2: note: each undeclared identifier is reported only once for each function it appears in

vim +/data +275 security/integrity/ima/ima_fs.c

4af4662f Mimi Zohar 2009-02-04  269  	/* No partial writes. */
6ccd0456 Eric Paris 2010-04-20  270  	result = -EINVAL;
6ccd0456 Eric Paris 2010-04-20  271  	if (*ppos != 0)
6ccd0456 Eric Paris 2010-04-20  272  		goto out;
6ccd0456 Eric Paris 2010-04-20  273  
6ccd0456 Eric Paris 2010-04-20  274  	result = -ENOMEM;
4af4662f Mimi Zohar 2009-02-04 @275  	data = kmalloc(datalen + 1, GFP_KERNEL);
4af4662f Mimi Zohar 2009-02-04  276  	if (!data)
6ccd0456 Eric Paris 2010-04-20  277  		goto out;
4af4662f Mimi Zohar 2009-02-04  278  

:::::: The code at line 275 was first introduced by commit
:::::: 4af4662fa4a9dc62289c580337ae2506339c4729 integrity: IMA policy

:::::: TO: Mimi Zohar <zohar@linux.vnet.ibm.com>
:::::: CC: James Morris <jmorris@namei.org>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 18304 bytes --]

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

* Re: linux-next: manual merge of the security tree with the vfs tree
  2015-12-31  4:24 linux-next: manual merge of the security tree with the vfs tree Stephen Rothwell
  2015-12-31  4:30 ` Al Viro
@ 2016-01-04  1:37 ` Mimi Zohar
  2016-01-04  1:55   ` Stephen Rothwell
  1 sibling, 1 reply; 8+ messages in thread
From: Mimi Zohar @ 2016-01-04  1:37 UTC (permalink / raw)
  To: Stephen Rothwell
  Cc: James Morris, Al Viro, linux-next, linux-kernel, Petko Manolov

On Thu, 2015-12-31 at 15:24 +1100, Stephen Rothwell wrote:
> Hi James,
> 
> Today's linux-next merge of the security tree got a conflict in:
> 
>   security/integrity/ima/ima_fs.c
> 
> between commit:
> 
>   3bc8f29b149e ("new helper: memdup_user_nul()")
> 
> from the vfs tree and commit:
> 
>   38d859f991f3 ("IMA: policy can now be updated multiple times")
> 
> from the security tree.
> 
> I fixed it up (hopefully, see below) and can carry the fix as necessary
> (no action is required).

Hi Stephen,

FYI, I pushed out Petko's patch to linux-integrity/next earlier today.
His patch moves taking the ima_write_mutex to after the the call to
copy_from_user(), as discussed.  This obviously won't fix the conflict
with Al's patch.  How do you want to handle it?  Do I need to do
anything?

Thanks!

Mimi


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

* Re: linux-next: manual merge of the security tree with the vfs tree
  2016-01-04  1:37 ` linux-next: manual merge of the security tree with the vfs tree Mimi Zohar
@ 2016-01-04  1:55   ` Stephen Rothwell
  0 siblings, 0 replies; 8+ messages in thread
From: Stephen Rothwell @ 2016-01-04  1:55 UTC (permalink / raw)
  To: Mimi Zohar; +Cc: James Morris, Al Viro, linux-next, linux-kernel, Petko Manolov

Hi Mimi,

On Sun, 03 Jan 2016 20:37:20 -0500 Mimi Zohar <zohar@linux.vnet.ibm.com> wrote:
>
> FYI, I pushed out Petko's patch to linux-integrity/next earlier today.
> His patch moves taking the ima_write_mutex to after the the call to
> copy_from_user(), as discussed.  This obviously won't fix the conflict
> with Al's patch.  How do you want to handle it?  Do I need to do
> anything?

No, I will just do a different conflict resolution.  I will send the
usual email when I get to it.

-- 
Cheers,
Stephen Rothwell                    sfr@canb.auug.org.au

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

end of thread, other threads:[~2016-01-04  1:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-31  4:24 linux-next: manual merge of the security tree with the vfs tree Stephen Rothwell
2015-12-31  4:30 ` Al Viro
2015-12-31 10:45   ` Petko Manolov
2016-01-01  4:34     ` Al Viro
2016-01-01 17:29       ` Petko Manolov
2016-01-01 17:41         ` [PATCH] ima_write_policy() optimizations; kbuild test robot
2016-01-04  1:37 ` linux-next: manual merge of the security tree with the vfs tree Mimi Zohar
2016-01-04  1:55   ` Stephen Rothwell

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).