xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xen/gntdev: fix gntdev_mmap() error exit path
@ 2021-04-23  5:40 Juergen Gross
  2021-04-23  6:55 ` Luca Fancellu
  2021-05-10  9:26 ` Juergen Gross
  0 siblings, 2 replies; 6+ messages in thread
From: Juergen Gross @ 2021-04-23  5:40 UTC (permalink / raw)
  To: xen-devel, linux-kernel
  Cc: Juergen Gross, Boris Ostrovsky, Stefano Stabellini, stable,
	Marek Marczykowski-Górecki

Commit d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
introduced an error in gntdev_mmap(): in case the call of
mmu_interval_notifier_insert_locked() fails the exit path should not
call mmu_interval_notifier_remove(), as this might result in NULL
dereferences.

One reason for failure is e.g. a signal pending for the running
process.

Fixes: d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
Cc: stable@vger.kernel.org
Reported-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
---
 drivers/xen/gntdev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c
index f01d58c7a042..a3e7be96527d 100644
--- a/drivers/xen/gntdev.c
+++ b/drivers/xen/gntdev.c
@@ -1017,8 +1017,10 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 		err = mmu_interval_notifier_insert_locked(
 			&map->notifier, vma->vm_mm, vma->vm_start,
 			vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
-		if (err)
+		if (err) {
+			map->vma = NULL;
 			goto out_unlock_put;
+		}
 	}
 	mutex_unlock(&priv->lock);
 
-- 
2.26.2



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

* Re: [PATCH] xen/gntdev: fix gntdev_mmap() error exit path
  2021-04-23  5:40 [PATCH] xen/gntdev: fix gntdev_mmap() error exit path Juergen Gross
@ 2021-04-23  6:55 ` Luca Fancellu
  2021-04-23  7:00   ` Juergen Gross
  2021-05-10  9:26 ` Juergen Gross
  1 sibling, 1 reply; 6+ messages in thread
From: Luca Fancellu @ 2021-04-23  6:55 UTC (permalink / raw)
  To: Juergen Gross
  Cc: xen-devel, linux-kernel, Boris Ostrovsky, Stefano Stabellini,
	stable, Marek Marczykowski-Górecki



> On 23 Apr 2021, at 06:40, Juergen Gross <jgross@suse.com> wrote:
> 
> Commit d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
> introduced an error in gntdev_mmap(): in case the call of
> mmu_interval_notifier_insert_locked() fails the exit path should not
> call mmu_interval_notifier_remove(), as this might result in NULL
> dereferences.
> 
> One reason for failure is e.g. a signal pending for the running
> process.
> 
> Fixes: d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
> Cc: stable@vger.kernel.org
> Reported-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
> drivers/xen/gntdev.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c
> index f01d58c7a042..a3e7be96527d 100644
> --- a/drivers/xen/gntdev.c
> +++ b/drivers/xen/gntdev.c
> @@ -1017,8 +1017,10 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
> 		err = mmu_interval_notifier_insert_locked(
> 			&map->notifier, vma->vm_mm, vma->vm_start,
> 			vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
> -		if (err)
> +		if (err) {
> +			map->vma = NULL;
> 			goto out_unlock_put;
> +		}
> 	}
> 	mutex_unlock(&priv->lock);
> 
> -- 
> 2.26.2
> 
> 

Hi Juergen,

I can see from the code that there is another path to out_unlock_put label some lines before:

        […]
        vma->vm_private_data = map;
	if (map->flags) {
		if ((vma->vm_flags & VM_WRITE) &&
				(map->flags & GNTMAP_readonly))
			goto out_unlock_put;
	} else {
		map->flags = GNTMAP_host_map;
		if (!(vma->vm_flags & VM_WRITE))
			map->flags |= GNTMAP_readonly;
	}
        […]

Can be the case that use_ptemod is != 0 also for that path?

Cheers,
Luca

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

* Re: [PATCH] xen/gntdev: fix gntdev_mmap() error exit path
  2021-04-23  6:55 ` Luca Fancellu
@ 2021-04-23  7:00   ` Juergen Gross
  2021-04-23  7:04     ` Luca Fancellu
  0 siblings, 1 reply; 6+ messages in thread
From: Juergen Gross @ 2021-04-23  7:00 UTC (permalink / raw)
  To: Luca Fancellu
  Cc: xen-devel, linux-kernel, Boris Ostrovsky, Stefano Stabellini,
	stable, Marek Marczykowski-Górecki


[-- Attachment #1.1.1: Type: text/plain, Size: 2134 bytes --]

On 23.04.21 08:55, Luca Fancellu wrote:
> 
> 
>> On 23 Apr 2021, at 06:40, Juergen Gross <jgross@suse.com> wrote:
>>
>> Commit d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
>> introduced an error in gntdev_mmap(): in case the call of
>> mmu_interval_notifier_insert_locked() fails the exit path should not
>> call mmu_interval_notifier_remove(), as this might result in NULL
>> dereferences.
>>
>> One reason for failure is e.g. a signal pending for the running
>> process.
>>
>> Fixes: d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
>> Cc: stable@vger.kernel.org
>> Reported-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
>> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>> drivers/xen/gntdev.c | 4 +++-
>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c
>> index f01d58c7a042..a3e7be96527d 100644
>> --- a/drivers/xen/gntdev.c
>> +++ b/drivers/xen/gntdev.c
>> @@ -1017,8 +1017,10 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
>> 		err = mmu_interval_notifier_insert_locked(
>> 			&map->notifier, vma->vm_mm, vma->vm_start,
>> 			vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
>> -		if (err)
>> +		if (err) {
>> +			map->vma = NULL;
>> 			goto out_unlock_put;
>> +		}
>> 	}
>> 	mutex_unlock(&priv->lock);
>>
>> -- 
>> 2.26.2
>>
>>
> 
> Hi Juergen,
> 
> I can see from the code that there is another path to out_unlock_put label some lines before:
> 
>          […]
>          vma->vm_private_data = map;
> 	if (map->flags) {
> 		if ((vma->vm_flags & VM_WRITE) &&
> 				(map->flags & GNTMAP_readonly))
> 			goto out_unlock_put;
> 	} else {
> 		map->flags = GNTMAP_host_map;
> 		if (!(vma->vm_flags & VM_WRITE))
> 			map->flags |= GNTMAP_readonly;
> 	}
>          […]
> 
> Can be the case that use_ptemod is != 0 also for that path?

map->vma will be NULL in this case, so there will be no problem
resulting from that path.


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

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

* Re: [PATCH] xen/gntdev: fix gntdev_mmap() error exit path
  2021-04-23  7:00   ` Juergen Gross
@ 2021-04-23  7:04     ` Luca Fancellu
  2021-05-08  0:41       ` Marek Marczykowski-Górecki
  0 siblings, 1 reply; 6+ messages in thread
From: Luca Fancellu @ 2021-04-23  7:04 UTC (permalink / raw)
  To: Juergen Gross
  Cc: xen-devel, linux-kernel, Boris Ostrovsky, Stefano Stabellini,
	stable, Marek Marczykowski-Górecki



> On 23 Apr 2021, at 08:00, Juergen Gross <jgross@suse.com> wrote:
> 
> On 23.04.21 08:55, Luca Fancellu wrote:
>>> On 23 Apr 2021, at 06:40, Juergen Gross <jgross@suse.com> wrote:
>>> 
>>> Commit d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
>>> introduced an error in gntdev_mmap(): in case the call of
>>> mmu_interval_notifier_insert_locked() fails the exit path should not
>>> call mmu_interval_notifier_remove(), as this might result in NULL
>>> dereferences.
>>> 
>>> One reason for failure is e.g. a signal pending for the running
>>> process.
>>> 
>>> Fixes: d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
>>> Cc: stable@vger.kernel.org
>>> Reported-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
>>> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
>>> Signed-off-by: Juergen Gross <jgross@suse.com>
>>> ---
>>> drivers/xen/gntdev.c | 4 +++-
>>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>> 
>>> diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c
>>> index f01d58c7a042..a3e7be96527d 100644
>>> --- a/drivers/xen/gntdev.c
>>> +++ b/drivers/xen/gntdev.c
>>> @@ -1017,8 +1017,10 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
>>> 		err = mmu_interval_notifier_insert_locked(
>>> 			&map->notifier, vma->vm_mm, vma->vm_start,
>>> 			vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
>>> -		if (err)
>>> +		if (err) {
>>> +			map->vma = NULL;
>>> 			goto out_unlock_put;
>>> +		}
>>> 	}
>>> 	mutex_unlock(&priv->lock);
>>> 
>>> -- 
>>> 2.26.2
>>> 
>>> 
>> Hi Juergen,
>> I can see from the code that there is another path to out_unlock_put label some lines before:
>>         […]
>>         vma->vm_private_data = map;
>> 	if (map->flags) {
>> 		if ((vma->vm_flags & VM_WRITE) &&
>> 				(map->flags & GNTMAP_readonly))
>> 			goto out_unlock_put;
>> 	} else {
>> 		map->flags = GNTMAP_host_map;
>> 		if (!(vma->vm_flags & VM_WRITE))
>> 			map->flags |= GNTMAP_readonly;
>> 	}
>>         […]
>> Can be the case that use_ptemod is != 0 also for that path?
> 
> map->vma will be NULL in this case, so there will be no problem
> resulting from that path.
> 

Right, thanks, seems good to me.

Reviewed-by: Luca Fancellu <luca.fancellu@arm.com>

Cheers,
Luca

> 
> Juergen
> <OpenPGP_0xB0DE9DD628BF132F.asc>



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

* Re: [PATCH] xen/gntdev: fix gntdev_mmap() error exit path
  2021-04-23  7:04     ` Luca Fancellu
@ 2021-05-08  0:41       ` Marek Marczykowski-Górecki
  0 siblings, 0 replies; 6+ messages in thread
From: Marek Marczykowski-Górecki @ 2021-05-08  0:41 UTC (permalink / raw)
  To: Luca Fancellu
  Cc: Juergen Gross, xen-devel, linux-kernel, Boris Ostrovsky,
	Stefano Stabellini, stable

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

On Fri, Apr 23, 2021 at 08:04:57AM +0100, Luca Fancellu wrote:
> > On 23 Apr 2021, at 08:00, Juergen Gross <jgross@suse.com> wrote:
> > On 23.04.21 08:55, Luca Fancellu wrote:
> >>> On 23 Apr 2021, at 06:40, Juergen Gross <jgross@suse.com> wrote:
> >>> 
> >>> Commit d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
> >>> introduced an error in gntdev_mmap(): in case the call of
> >>> mmu_interval_notifier_insert_locked() fails the exit path should not
> >>> call mmu_interval_notifier_remove(), as this might result in NULL
> >>> dereferences.
> >>> 
> >>> One reason for failure is e.g. a signal pending for the running
> >>> process.
> >>> 
> >>> Fixes: d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
> >>> Cc: stable@vger.kernel.org
> >>> Reported-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> >>> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> >>> Signed-off-by: Juergen Gross <jgross@suse.com>

(...)

> Right, thanks, seems good to me.
> 
> Reviewed-by: Luca Fancellu <luca.fancellu@arm.com>

Can somebody ack this fix please? 

-- 
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] xen/gntdev: fix gntdev_mmap() error exit path
  2021-04-23  5:40 [PATCH] xen/gntdev: fix gntdev_mmap() error exit path Juergen Gross
  2021-04-23  6:55 ` Luca Fancellu
@ 2021-05-10  9:26 ` Juergen Gross
  1 sibling, 0 replies; 6+ messages in thread
From: Juergen Gross @ 2021-05-10  9:26 UTC (permalink / raw)
  To: xen-devel, linux-kernel
  Cc: Boris Ostrovsky, Stefano Stabellini, stable,
	Marek Marczykowski-Górecki


[-- Attachment #1.1.1: Type: text/plain, Size: 785 bytes --]

On 23.04.21 07:40, Juergen Gross wrote:
> Commit d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
> introduced an error in gntdev_mmap(): in case the call of
> mmu_interval_notifier_insert_locked() fails the exit path should not
> call mmu_interval_notifier_remove(), as this might result in NULL
> dereferences.
> 
> One reason for failure is e.g. a signal pending for the running
> process.
> 
> Fixes: d3eeb1d77c5d0af ("xen/gntdev: use mmu_interval_notifier_insert")
> Cc: stable@vger.kernel.org
> Reported-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> Signed-off-by: Juergen Gross <jgross@suse.com>

Pushed to xen/tip.git for-linus-5.13b


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

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

end of thread, other threads:[~2021-05-10  9:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-23  5:40 [PATCH] xen/gntdev: fix gntdev_mmap() error exit path Juergen Gross
2021-04-23  6:55 ` Luca Fancellu
2021-04-23  7:00   ` Juergen Gross
2021-04-23  7:04     ` Luca Fancellu
2021-05-08  0:41       ` Marek Marczykowski-Górecki
2021-05-10  9:26 ` Juergen Gross

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