Skip to contentSkip to conversation

linux-next: manual merge of the security tree with the vfs tree

DISCUSSION Stephen Rothwell 5 participants 8 messages Dec 31, 2015 – Jan 4, 2016

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

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

Signature · 52 lines
Cheers,
Stephen Rothwell                    [email protected]

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);
#1 lore raw
Quoted 38 lines · Stephen Rothwell

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

(quoted diff) +5 -1
+ 	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';

Review

Quoted 10 lines
(quoted diff) +4 -2
 -
 -	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.

#2 lore raw
Quoted 50 lines · Al Viro
Quoted 38 lines · Stephen Rothwell

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

(quoted diff) +5 -1
+ 	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';

Review

Quoted 10 lines
(quoted diff) +4 -2
 -
 -	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;
++	}
Quoted 4 lines

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

#3 lore raw
Quoted 3 lines · Petko Manolov

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?

#4 lore raw
Quoted 7 lines · Al Viro
Quoted 3 lines · Petko Manolov

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

Quoted 1 line

From 6c9058009c59fda5b8e98a3fc09497ce3efdb3e9 Mon Sep 17 00:00:00 2001

From: Petko Manolov <[email protected]>
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.

---
 security/integrity/ima/ima_fs.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
security/integrity/ima/ima_fs.c +10 -10
index 3caed6d..d2c0d55 100644
@@ -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;
 }
 
Signature · 1 line
2.6.4
#5 lore raw

Re: [PATCH] ima_write_policy() optimizations;

reproduce:

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 <[email protected]>
:::::: CC: James Morris <[email protected]>

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

#6 lore raw
In reply to Stephen Rothwell
Quoted 10 lines · Stephen Rothwell

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()")

Quoted 8 lines

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

#7 lore raw

Hi Mimi,

Quoted 6 lines · On Sun, 03 Jan 2016 20:37:20 -0500 Mimi Zohar <[email protected]> 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.

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]