mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] xen/privcmd: fix static checker warning
@ 2018-06-07 10:21 Paul Durrant
  2018-06-07 10:28 ` [Xen-devel] " Andrew Cooper
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Durrant @ 2018-06-07 10:21 UTC (permalink / raw)
  To: xen-devel, linux-kernel
  Cc: Paul Durrant, Boris Ostrovsky, Juergen Gross, Dan Carpenter

Commit 3ad0876554ca ("xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE")
introduced a static checker warning:

  drivers/xen/privcmd.c:827 privcmd_ioctl_mmap_resource()
  warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'
        64 vs 32.

caused by this cast:

   827  num = xen_remap_domain_mfn_array(vma,
   828                                   kdata.addr & PAGE_MASK,
   829                                   pfns, kdata.num, (int *)pfns,
                                                          ^^^^^^^^^^^

The reason for the cast is that xen_remap_domain_mfn_array() requires an
array of ints to store error codes. It is actually safe to re-use the
pfns array for this purpose but it does look odd (as well as leading to
the warning). It would also be easy for a future implementation change
to make this re-use unsafe so this patch modifies privcmd to use a
separately allocated array for error codes.

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
---
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/xen/privcmd.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c
index 8ae0349..8507c13 100644
--- a/drivers/xen/privcmd.c
+++ b/drivers/xen/privcmd.c
@@ -822,11 +822,18 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
 		unsigned int domid =
 			(xdata.flags & XENMEM_rsrc_acq_caller_owned) ?
 			DOMID_SELF : kdata.dom;
+		int *errs;
 		int num;
 
+		errs = kcalloc(kdata.num, sizeof(*errs), GFP_KERNEL);
+		if (!errs) {
+			rc = -ENOMEM;
+			goto out;
+		}
+
 		num = xen_remap_domain_mfn_array(vma,
 						 kdata.addr & PAGE_MASK,
-						 pfns, kdata.num, (int *)pfns,
+						 pfns, kdata.num, errs,
 						 vma->vm_page_prot,
 						 domid,
 						 vma->vm_private_data);
@@ -836,12 +843,14 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
 			unsigned int i;
 
 			for (i = 0; i < num; i++) {
-				rc = pfns[i];
+				rc = errs[i];
 				if (rc < 0)
 					break;
 			}
 		} else
 			rc = 0;
+
+		kfree(errs);
 	}
 
 out:
-- 
2.1.4

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

* Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warning
  2018-06-07 10:21 [PATCH] xen/privcmd: fix static checker warning Paul Durrant
@ 2018-06-07 10:28 ` Andrew Cooper
  2018-06-07 11:23   ` Paul Durrant
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Cooper @ 2018-06-07 10:28 UTC (permalink / raw)
  To: Paul Durrant, xen-devel, linux-kernel
  Cc: Juergen Gross, Boris Ostrovsky, Dan Carpenter

On 07/06/18 11:21, Paul Durrant wrote:
> Commit 3ad0876554ca ("xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE")
> introduced a static checker warning:
>
>   drivers/xen/privcmd.c:827 privcmd_ioctl_mmap_resource()
>   warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'
>         64 vs 32.
>
> caused by this cast:
>
>    827  num = xen_remap_domain_mfn_array(vma,
>    828                                   kdata.addr & PAGE_MASK,
>    829                                   pfns, kdata.num, (int *)pfns,
>                                                           ^^^^^^^^^^^
>
> The reason for the cast is that xen_remap_domain_mfn_array() requires an
> array of ints to store error codes. It is actually safe to re-use the
> pfns array for this purpose but it does look odd (as well as leading to
> the warning). It would also be easy for a future implementation change
> to make this re-use unsafe so this patch modifies privcmd to use a
> separately allocated array for error codes.
>
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Paul Durrant <paul.durrant@citrix.com>

It may be safe to reuse pfns[] as the storage space for the errs array,
but code is incorrect when sizeof(pfn) != sizeof(int).  In such a case,
you skip over every other err, and second half of pfns[] is junk from
the point of view of the errs loop.

~Andrew

> ---
> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> Cc: Juergen Gross <jgross@suse.com>
> Cc: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  drivers/xen/privcmd.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c
> index 8ae0349..8507c13 100644
> --- a/drivers/xen/privcmd.c
> +++ b/drivers/xen/privcmd.c
> @@ -822,11 +822,18 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
>  		unsigned int domid =
>  			(xdata.flags & XENMEM_rsrc_acq_caller_owned) ?
>  			DOMID_SELF : kdata.dom;
> +		int *errs;
>  		int num;
>  
> +		errs = kcalloc(kdata.num, sizeof(*errs), GFP_KERNEL);
> +		if (!errs) {
> +			rc = -ENOMEM;
> +			goto out;
> +		}
> +
>  		num = xen_remap_domain_mfn_array(vma,
>  						 kdata.addr & PAGE_MASK,
> -						 pfns, kdata.num, (int *)pfns,
> +						 pfns, kdata.num, errs,
>  						 vma->vm_page_prot,
>  						 domid,
>  						 vma->vm_private_data);
> @@ -836,12 +843,14 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
>  			unsigned int i;
>  
>  			for (i = 0; i < num; i++) {
> -				rc = pfns[i];
> +				rc = errs[i];
>  				if (rc < 0)
>  					break;
>  			}
>  		} else
>  			rc = 0;
> +
> +		kfree(errs);
>  	}
>  
>  out:

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

* RE: [Xen-devel] [PATCH] xen/privcmd: fix static checker warning
  2018-06-07 10:28 ` [Xen-devel] " Andrew Cooper
@ 2018-06-07 11:23   ` Paul Durrant
  2018-06-07 14:03     ` Juergen Gross
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Durrant @ 2018-06-07 11:23 UTC (permalink / raw)
  To: Andrew Cooper, xen-devel, linux-kernel
  Cc: Juergen Gross, Boris Ostrovsky, Dan Carpenter

> -----Original Message-----
> From: Andrew Cooper
> Sent: 07 June 2018 11:28
> To: Paul Durrant <Paul.Durrant@citrix.com>; xen-devel@lists.xenproject.org;
> linux-kernel@vger.kernel.org
> Cc: Juergen Gross <jgross@suse.com>; Boris Ostrovsky
> <boris.ostrovsky@oracle.com>; Dan Carpenter
> <dan.carpenter@oracle.com>
> Subject: Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warning
> 
> On 07/06/18 11:21, Paul Durrant wrote:
> > Commit 3ad0876554ca ("xen/privcmd: add
> IOCTL_PRIVCMD_MMAP_RESOURCE")
> > introduced a static checker warning:
> >
> >   drivers/xen/privcmd.c:827 privcmd_ioctl_mmap_resource()
> >   warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'
> >         64 vs 32.
> >
> > caused by this cast:
> >
> >    827  num = xen_remap_domain_mfn_array(vma,
> >    828                                   kdata.addr & PAGE_MASK,
> >    829                                   pfns, kdata.num, (int *)pfns,
> >                                                           ^^^^^^^^^^^
> >
> > The reason for the cast is that xen_remap_domain_mfn_array() requires
> an
> > array of ints to store error codes. It is actually safe to re-use the
> > pfns array for this purpose but it does look odd (as well as leading to
> > the warning). It would also be easy for a future implementation change
> > to make this re-use unsafe so this patch modifies privcmd to use a
> > separately allocated array for error codes.
> >
> > Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> > Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
> 
> It may be safe to reuse pfns[] as the storage space for the errs array,
> but code is incorrect when sizeof(pfn) != sizeof(int).  In such a case,
> you skip over every other err, and second half of pfns[] is junk from
> the point of view of the errs loop.
> 

Yep, that is indeed what happens without this patch.

  Paul

> ~Andrew
> 
> > ---
> > Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> > Cc: Juergen Gross <jgross@suse.com>
> > Cc: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >  drivers/xen/privcmd.c | 13 +++++++++++--
> >  1 file changed, 11 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c
> > index 8ae0349..8507c13 100644
> > --- a/drivers/xen/privcmd.c
> > +++ b/drivers/xen/privcmd.c
> > @@ -822,11 +822,18 @@ static long privcmd_ioctl_mmap_resource(struct
> file *file, void __user *udata)
> >  		unsigned int domid =
> >  			(xdata.flags & XENMEM_rsrc_acq_caller_owned) ?
> >  			DOMID_SELF : kdata.dom;
> > +		int *errs;
> >  		int num;
> >
> > +		errs = kcalloc(kdata.num, sizeof(*errs), GFP_KERNEL);
> > +		if (!errs) {
> > +			rc = -ENOMEM;
> > +			goto out;
> > +		}
> > +
> >  		num = xen_remap_domain_mfn_array(vma,
> >  						 kdata.addr & PAGE_MASK,
> > -						 pfns, kdata.num, (int *)pfns,
> > +						 pfns, kdata.num, errs,
> >  						 vma->vm_page_prot,
> >  						 domid,
> >  						 vma->vm_private_data);
> > @@ -836,12 +843,14 @@ static long privcmd_ioctl_mmap_resource(struct
> file *file, void __user *udata)
> >  			unsigned int i;
> >
> >  			for (i = 0; i < num; i++) {
> > -				rc = pfns[i];
> > +				rc = errs[i];
> >  				if (rc < 0)
> >  					break;
> >  			}
> >  		} else
> >  			rc = 0;
> > +
> > +		kfree(errs);
> >  	}
> >
> >  out:


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

* Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warning
  2018-06-07 11:23   ` Paul Durrant
@ 2018-06-07 14:03     ` Juergen Gross
  0 siblings, 0 replies; 4+ messages in thread
From: Juergen Gross @ 2018-06-07 14:03 UTC (permalink / raw)
  To: Paul Durrant, Andrew Cooper, xen-devel, linux-kernel
  Cc: Boris Ostrovsky, Dan Carpenter

On 07/06/18 13:23, Paul Durrant wrote:
>> -----Original Message-----
>> From: Andrew Cooper
>> Sent: 07 June 2018 11:28
>> To: Paul Durrant <Paul.Durrant@citrix.com>; xen-devel@lists.xenproject.org;
>> linux-kernel@vger.kernel.org
>> Cc: Juergen Gross <jgross@suse.com>; Boris Ostrovsky
>> <boris.ostrovsky@oracle.com>; Dan Carpenter
>> <dan.carpenter@oracle.com>
>> Subject: Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warning
>>
>> On 07/06/18 11:21, Paul Durrant wrote:
>>> Commit 3ad0876554ca ("xen/privcmd: add
>> IOCTL_PRIVCMD_MMAP_RESOURCE")
>>> introduced a static checker warning:
>>>
>>>   drivers/xen/privcmd.c:827 privcmd_ioctl_mmap_resource()
>>>   warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'
>>>         64 vs 32.
>>>
>>> caused by this cast:
>>>
>>>    827  num = xen_remap_domain_mfn_array(vma,
>>>    828                                   kdata.addr & PAGE_MASK,
>>>    829                                   pfns, kdata.num, (int *)pfns,
>>>                                                           ^^^^^^^^^^^
>>>
>>> The reason for the cast is that xen_remap_domain_mfn_array() requires
>> an
>>> array of ints to store error codes. It is actually safe to re-use the
>>> pfns array for this purpose but it does look odd (as well as leading to
>>> the warning). It would also be easy for a future implementation change
>>> to make this re-use unsafe so this patch modifies privcmd to use a
>>> separately allocated array for error codes.
>>>
>>> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
>>> Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
>>
>> It may be safe to reuse pfns[] as the storage space for the errs array,
>> but code is incorrect when sizeof(pfn) != sizeof(int).  In such a case,
>> you skip over every other err, and second half of pfns[] is junk from
>> the point of view of the errs loop.
>>
> 
> Yep, that is indeed what happens without this patch.

Can you please update the commit message accordingly?


Juergen

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

end of thread, other threads:[~2018-06-07 14:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-07 10:21 [PATCH] xen/privcmd: fix static checker warning Paul Durrant
2018-06-07 10:28 ` [Xen-devel] " Andrew Cooper
2018-06-07 11:23   ` Paul Durrant
2018-06-07 14:03     ` Juergen Gross

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®