xen/privcmd: fix static checker warning
PATCH 1/1 xen/privcmd: fix static checker warning
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.
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Dan Carpenter <[email protected]>
--- drivers/xen/privcmd.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/xen/privcmd.c +11 -2
@@ -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:
Signature · 1 line
2.1.4
Quoted 21 lines · Paul Durrant
Commit
3ad0876554ca("xen/privcmd: addIOCTL_PRIVCMD_MMAP_RESOURCE") introduced a static checker warning:
drivers/xen/privcmd.c:827privcmd_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.
Quoted 2 lines
Reported-by Dan Carpenter <[email protected]>Signed-off-by Paul Durrant <[email protected]>
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
Quoted 38 lines
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Dan Carpenter <[email protected]>--- drivers/xen/privcmd.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)drivers/xen/privcmd.c +9
-2@@ -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];
drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata) · line 856
Quoted 10 lines
drivers/xen/privcmd.c +2
-0if (rc <0 )break ; } }else rc =0 ; + +kfree (errs); } out:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 21 lines · Paul Durrant
Commit
3ad0876554ca("xen/privcmd: addIOCTL_PRIVCMD_MMAP_RESOURCE") introduced a static checker warning:
drivers/xen/privcmd.c:827privcmd_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.
Quoted 2 lines
Reported-by Dan Carpenter <[email protected]>Signed-off-by Paul Durrant <[email protected]>
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
Quoted 48 lines
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Dan Carpenter <[email protected]>--- drivers/xen/privcmd.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)drivers/xen/privcmd.c +11
-2@@ -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:
Quoted 34 lines
-----Original Message-----
From: Andrew Cooper
Sent: 07 June 2018 11:28
To: Paul Durrant <[email protected]>; [email protected]; [email protected]
Cc: Juergen Gross <[email protected]>; Boris Ostrovsky <[email protected]>; Dan Carpenter <[email protected]>
Subject: Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warningQuoted 1 line · Paul Durrant
Commit
3ad0876554ca("xen/privcmd: add
IOCTL_PRIVCMD_MMAP_RESOURCE")Quoted 6 lines
introduced a static checker warning:
drivers/xen/privcmd.c:827privcmd_ioctl_mmap_resource()
warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'64 vs 32.Quoted 8 lines
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()requiresan
Quoted 6 lines
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.
Quoted 8 lines
Quoted 2 lines
Reported-by Dan Carpenter <[email protected]>Signed-off-by Paul Durrant <[email protected]>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
Quoted 51 lines
~Andrew
drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct
Quoted 13 lines
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Dan Carpenter <[email protected]>--- drivers/xen/privcmd.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)drivers/xen/privcmd.c +0
-0@@ -822,11 +822,18 @@ static long privcmd_ioctl_mmap_resource(structfile *file, void
__user*udata)drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct
Quoted 20 lines
drivers/xen/privcmd.c +8
-1unsigned 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(structfile *file, void
__user*udata)Quoted 14 lines
drivers/xen/privcmd.c +3
-1unsigned int i;for (i =0 ; i < num; i++) { - rc = pfns[i]; + rc = errs[i];if (rc <0 )break ; } }else rc =0 ; + +kfree (errs); }
Quoted 1 line
drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct · line 856
Quoted 1 line
drivers/xen/privcmd.c +0
-0out:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 34 lines
-----Original Message-----
From: Andrew Cooper
Sent: 07 June 2018 11:28
To: Paul Durrant <[email protected]>; [email protected]; [email protected]
Cc: Juergen Gross <[email protected]>; Boris Ostrovsky <[email protected]>; Dan Carpenter <[email protected]>
Subject: Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warningQuoted 1 line · Paul Durrant
Commit
3ad0876554ca("xen/privcmd: add
IOCTL_PRIVCMD_MMAP_RESOURCE")Quoted 6 lines
introduced a static checker warning:
drivers/xen/privcmd.c:827privcmd_ioctl_mmap_resource()
warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'64 vs 32.Quoted 8 lines
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()requiresan
Quoted 6 lines
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.
Quoted 8 lines
Quoted 2 lines
Reported-by Dan Carpenter <[email protected]>Signed-off-by Paul Durrant <[email protected]>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
Quoted 52 lines
~Andrew
drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct
Quoted 13 lines
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Dan Carpenter <[email protected]>--- drivers/xen/privcmd.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)drivers/xen/privcmd.c +0
-0@@ -822,11 +822,18 @@ static long privcmd_ioctl_mmap_resource(structfile *file, void
__user*udata)drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct
Quoted 20 lines
drivers/xen/privcmd.c +8
-1unsigned 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(structfile *file, void
__user*udata)Quoted 15 lines
drivers/xen/privcmd.c +3
-1unsigned int i;for (i =0 ; i < num; i++) { - rc = pfns[i]; + rc = errs[i];if (rc <0 )break ; } }else rc =0 ; + +kfree (errs); } out:
Juergen Gross
maintainerQuoted 37 lines · Paul Durrant
Quoted 37 lines
-----Original Message-----
From: Andrew Cooper
Sent: 07 June 2018 11:28
To: Paul Durrant <[email protected]>; [email protected]; [email protected]
Cc: Juergen Gross <[email protected]>; Boris Ostrovsky <[email protected]>; Dan Carpenter <[email protected]>
Subject: Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warningQuoted 1 line · Paul Durrant
Commit
3ad0876554ca("xen/privcmd: add
IOCTL_PRIVCMD_MMAP_RESOURCE")Quoted 6 lines
introduced a static checker warning:
drivers/xen/privcmd.c:827privcmd_ioctl_mmap_resource()
warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'64 vs 32.Quoted 8 lines
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()requiresan
Quoted 8 lines
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 <[email protected]>Signed-off-by Paul Durrant <[email protected]>
Quoted 7 lines
Quoted 5 lines
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
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Juergen Gross
maintainerQuoted 37 lines · Paul Durrant
Quoted 37 lines
-----Original Message-----
From: Andrew Cooper
Sent: 07 June 2018 11:28
To: Paul Durrant <[email protected]>; [email protected]; [email protected]
Cc: Juergen Gross <[email protected]>; Boris Ostrovsky <[email protected]>; Dan Carpenter <[email protected]>
Subject: Re: [Xen-devel] [PATCH] xen/privcmd: fix static checker warningQuoted 1 line · Paul Durrant
Commit
3ad0876554ca("xen/privcmd: add
IOCTL_PRIVCMD_MMAP_RESOURCE")Quoted 6 lines
introduced a static checker warning:
drivers/xen/privcmd.c:827privcmd_ioctl_mmap_resource()
warn: passing casted pointer 'pfns' to 'xen_remap_domain_mfn_array()'64 vs 32.Quoted 8 lines
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()requiresan
Quoted 8 lines
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 <[email protected]>Signed-off-by Paul Durrant <[email protected]>
Quoted 7 lines
Quoted 5 lines
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