linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] prevent get_user_pages call from kernel thread
@ 2008-06-08 15:37 Dmitri Monakhov
  2008-06-30 20:32 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitri Monakhov @ 2008-06-08 15:37 UTC (permalink / raw)
  To: linux-kernel, linux-fsdevel

Yes, yes.. everybody know that it is bad to write from kernel thread, and it
is madness to do it with O_DIRECT. But occasionally file with O_DIRECT flag
may be passed to loop device via LOOP_SET_FD. So if system hasn't address
space ops, or simply hide it like GFS, it is possible to kill kernel via
two lines program. In fact we can't effectively guard kernel space by
disabling O_DIRECT in loop's code, because user space can change it back
via fcntl(,F_SETFL,). Let's simply add sanity check mm related logic.

Signed-off-by: Dmitri Monakhov <dmonakhov@openvz.org>
---
 drivers/block/loop.c |    1 +
 fs/direct-io.c       |    4 ++++
 2 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index d3a25b0..bb2a262 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -753,6 +753,7 @@ static int loop_set_fd(struct loop_device *lo, struct file *lo_file,
 
 	mapping = file->f_mapping;
 	inode = mapping->host;
+	file->f_mode &= ~O_DIRECT;
 
 	if (!(file->f_mode & FMODE_WRITE))
 		lo_flags |= LO_FLAGS_READ_ONLY;
diff --git a/fs/direct-io.c b/fs/direct-io.c
index 9e81add..8e17224 100644
--- a/fs/direct-io.c
+++ b/fs/direct-io.c
@@ -149,6 +149,10 @@ static int dio_refill_pages(struct dio *dio)
 	int ret;
 	int nr_pages;
 
+	if (unlikely(!current->mm)) {
+		WARN_ON_ONCE(1);
+		return -EINVAL;
+	}
 	nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
 	down_read(&current->mm->mmap_sem);
 	ret = get_user_pages(
-- 
1.5.4.rc4



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

* Re: [PATCH] prevent get_user_pages call from kernel thread
  2008-06-08 15:37 [PATCH] prevent get_user_pages call from kernel thread Dmitri Monakhov
@ 2008-06-30 20:32 ` Andrew Morton
  2008-07-01  9:36   ` Dmitri Monakhov
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2008-06-30 20:32 UTC (permalink / raw)
  To: Dmitri Monakhov; +Cc: linux-kernel, linux-fsdevel

On Sun, 08 Jun 2008 19:37:32 +0400
Dmitri Monakhov <dmonakhov@openvz.org> wrote:

> Yes, yes.. everybody know that it is bad to write from kernel thread, and it
> is madness to do it with O_DIRECT. But occasionally file with O_DIRECT flag
> may be passed to loop device via LOOP_SET_FD. So if system hasn't address
> space ops, or simply hide it like GFS, it is possible to kill kernel via
> two lines program. In fact we can't effectively guard kernel space by
> disabling O_DIRECT in loop's code, because user space can change it back
> via fcntl(,F_SETFL,). Let's simply add sanity check mm related logic.
> 
> Signed-off-by: Dmitri Monakhov <dmonakhov@openvz.org>
> ---
>  drivers/block/loop.c |    1 +
>  fs/direct-io.c       |    4 ++++
>  2 files changed, 5 insertions(+), 0 deletions(-)
> 
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index d3a25b0..bb2a262 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -753,6 +753,7 @@ static int loop_set_fd(struct loop_device *lo, struct file *lo_file,
>  
>  	mapping = file->f_mapping;
>  	inode = mapping->host;
> +	file->f_mode &= ~O_DIRECT;
>  
>  	if (!(file->f_mode & FMODE_WRITE))
>  		lo_flags |= LO_FLAGS_READ_ONLY;
> diff --git a/fs/direct-io.c b/fs/direct-io.c
> index 9e81add..8e17224 100644
> --- a/fs/direct-io.c
> +++ b/fs/direct-io.c
> @@ -149,6 +149,10 @@ static int dio_refill_pages(struct dio *dio)
>  	int ret;
>  	int nr_pages;
>  
> +	if (unlikely(!current->mm)) {
> +		WARN_ON_ONCE(1);
> +		return -EINVAL;
> +	}
>  	nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
>  	down_read(&current->mm->mmap_sem);
>  	ret = get_user_pages(

It's a bit rude to silently turn off O_DIRECT in this case.

I think it would be better to simply fail the LOOP_SET_FD if the file
has O_DIRECT and the relevant address_space_operations vectors are
NULL.


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

* Re: [PATCH] prevent get_user_pages call from kernel thread
  2008-06-30 20:32 ` Andrew Morton
@ 2008-07-01  9:36   ` Dmitri Monakhov
  0 siblings, 0 replies; 3+ messages in thread
From: Dmitri Monakhov @ 2008-07-01  9:36 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, linux-fsdevel

Andrew Morton <akpm@linux-foundation.org> writes:

> On Sun, 08 Jun 2008 19:37:32 +0400
> Dmitri Monakhov <dmonakhov@openvz.org> wrote:
>
>> Yes, yes.. everybody know that it is bad to write from kernel thread, and it
>> is madness to do it with O_DIRECT. But occasionally file with O_DIRECT flag
>> may be passed to loop device via LOOP_SET_FD. So if system hasn't address
>> space ops, or simply hide it like GFS, it is possible to kill kernel via
>> two lines program. In fact we can't effectively guard kernel space by
>> disabling O_DIRECT in loop's code, because user space can change it back
>> via fcntl(,F_SETFL,). Let's simply add sanity check mm related logic.
>> 
>> Signed-off-by: Dmitri Monakhov <dmonakhov@openvz.org>
>> ---
>>  drivers/block/loop.c |    1 +
>>  fs/direct-io.c       |    4 ++++
>>  2 files changed, 5 insertions(+), 0 deletions(-)
>> 
>> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
>> index d3a25b0..bb2a262 100644
>> --- a/drivers/block/loop.c
>> +++ b/drivers/block/loop.c
>> @@ -753,6 +753,7 @@ static int loop_set_fd(struct loop_device *lo, struct file *lo_file,
>>  
>>  	mapping = file->f_mapping;
>>  	inode = mapping->host;
>> +	file->f_mode &= ~O_DIRECT;
>>  
>>  	if (!(file->f_mode & FMODE_WRITE))
>>  		lo_flags |= LO_FLAGS_READ_ONLY;
>> diff --git a/fs/direct-io.c b/fs/direct-io.c
>> index 9e81add..8e17224 100644
>> --- a/fs/direct-io.c
>> +++ b/fs/direct-io.c
>> @@ -149,6 +149,10 @@ static int dio_refill_pages(struct dio *dio)
>>  	int ret;
>>  	int nr_pages;
>>  
>> +	if (unlikely(!current->mm)) {
>> +		WARN_ON_ONCE(1);
>> +		return -EINVAL;
>> +	}
>>  	nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
>>  	down_read(&current->mm->mmap_sem);
>>  	ret = get_user_pages(
>
> It's a bit rude to silently turn off O_DIRECT in this case.
>
> I think it would be better to simply fail the LOOP_SET_FD if the file
> has O_DIRECT and the relevant address_space_operations vectors are
> NULL.
Ok, i'll resend updated version, but we sill have to protect dio_defill_pages
against fcntl(,F_SETFL,) hack.

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

end of thread, other threads:[~2008-07-01  9:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-08 15:37 [PATCH] prevent get_user_pages call from kernel thread Dmitri Monakhov
2008-06-30 20:32 ` Andrew Morton
2008-07-01  9:36   ` Dmitri Monakhov

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