2015-12-31 04:24:57

by Stephen Rothwell

[permalink] [raw]
Subject: linux-next: manual merge of the security tree with the vfs tree

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 [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);


2015-12-31 04:30:29

by Al Viro

[permalink] [raw]
Subject: Re: linux-next: manual merge of the security tree with the vfs tree

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.

2015-12-31 10:45:17

by Petko Manolov

[permalink] [raw]
Subject: Re: linux-next: manual merge of the security tree with the vfs tree

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

2016-01-01 04:34:31

by Al Viro

[permalink] [raw]
Subject: Re: linux-next: manual merge of the security tree with the vfs tree

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?