* [PATCH 00/26] use array_size
@ 2023-06-23 21:14 Julia Lawall
2023-06-23 21:14 ` [PATCH 23/26] media: staging: imgu: " Julia Lawall
0 siblings, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
To: linux-staging
Cc: keescook, kernel-janitors, Tianshu Qiu, Bingbu Cao, linux-sgx,
H. Peter Anvin, Dave Hansen, kasan-dev, Andrey Konovalov,
Dmitry Vyukov, iommu, linux-tegra, Robin Murphy, Krishna Reddy,
linux-scsi, linux-rdma, dri-devel, linux-kernel, netdev,
Shailend Chand, Benjamin Gaignard, Liam Mark, Laura Abbott,
Brian Starkey, John Stultz, linux-media, linaro-mm-sig, Xuan Zhuo,
virtualization, mhi, linux-arm-msm, linux-btrfs, intel-gvt-dev,
intel-gfx, VMware Graphics Reviewers, linux-hyperv
Use array_size to protect against multiplication overflows.
This follows up on the following patches by Kees Cook from 2018.
42bc47b35320 ("treewide: Use array_size() in vmalloc()")
fad953ce0b22 ("treewide: Use array_size() in vzalloc()")
The changes were done using the following Coccinelle semantic patch,
adapted from the one posted by Kees.
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
type t = {u8,__u8,char,unsigned char};
identifier alloc = {vmalloc,vzalloc};
@@
alloc(
- (sizeof(t)) * (COUNT)
+ COUNT
, ...)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression COUNT;
size_t e1, e2, e3;
identifier alloc = {vmalloc,vzalloc};
@@
(
alloc(
- (e1) * (e2) * (e3)
+ array3_size(e1, e2, e3)
,...)
|
alloc(
- (e1) * (e2) * (COUNT)
+ array3_size(COUNT, e1, e2)
,...)
)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression STRIDE, COUNT;
size_t e;
identifier alloc = {vmalloc,vzalloc};
@@
alloc(
- (e) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, e)
,...)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
identifier alloc = {vmalloc,vzalloc};
@@
(
alloc(C1 * C2 * C3,...)
|
alloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
,...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
size_t e1,e2;
expression COUNT;
identifier alloc = {vmalloc,vzalloc};
@@
(
alloc(
- (e1) * (e2)
+ array_size(e1, e2)
,...)
|
alloc(
- (e1) * (COUNT)
+ array_size(COUNT, e1)
,...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
identifier alloc = {vmalloc,vzalloc};
@@
(
alloc(C1 * C2,...)
|
alloc(
- (E1) * (E2)
+ array_size(E1, E2)
,...)
)
---
arch/x86/kernel/cpu/sgx/main.c | 3 ++-
drivers/accel/habanalabs/common/device.c | 3 ++-
drivers/accel/habanalabs/common/state_dump.c | 6 +++---
drivers/bus/mhi/host/init.c | 4 ++--
drivers/comedi/comedi_buf.c | 4 ++--
drivers/dma-buf/heaps/system_heap.c | 2 +-
drivers/gpu/drm/gud/gud_pipe.c | 2 +-
drivers/gpu/drm/i915/gvt/gtt.c | 6 ++++--
drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c | 2 +-
drivers/infiniband/hw/bnxt_re/qplib_res.c | 4 ++--
drivers/infiniband/hw/erdma/erdma_verbs.c | 4 ++--
drivers/infiniband/sw/siw/siw_qp.c | 4 ++--
drivers/infiniband/sw/siw/siw_verbs.c | 6 +++---
drivers/iommu/tegra-gart.c | 4 ++--
drivers/net/ethernet/amd/pds_core/core.c | 4 ++--
drivers/net/ethernet/freescale/enetc/enetc.c | 4 ++--
drivers/net/ethernet/google/gve/gve_tx.c | 2 +-
drivers/net/ethernet/marvell/octeon_ep/octep_rx.c | 2 +-
drivers/net/ethernet/microsoft/mana/hw_channel.c | 2 +-
drivers/net/ethernet/pensando/ionic/ionic_lif.c | 4 ++--
drivers/scsi/fnic/fnic_trace.c | 2 +-
drivers/scsi/qla2xxx/qla_init.c | 4 ++--
drivers/staging/media/ipu3/ipu3-mmu.c | 2 +-
drivers/vdpa/vdpa_user/iova_domain.c | 3 +--
drivers/virtio/virtio_mem.c | 6 +++---
fs/btrfs/zoned.c | 5 +++--
kernel/kcov.c | 2 +-
lib/test_vmalloc.c | 12 ++++++------
28 files changed, 56 insertions(+), 52 deletions(-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 23/26] media: staging: imgu: use array_size
2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
2023-06-25 4:59 ` Bingbu Cao
2023-06-27 17:35 ` Julia Lawall
0 siblings, 2 replies; 5+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
To: Sakari Ailus
Cc: keescook, kernel-janitors, Bingbu Cao, Tianshu Qiu,
Mauro Carvalho Chehab, Greg Kroah-Hartman, linux-media,
linux-staging, linux-kernel
Use array_size to protect against multiplication overflows.
The changes were done using the following Coccinelle semantic patch:
// <smpl>
@@
expression E1, E2;
constant C1, C2;
identifier alloc = {vmalloc,vzalloc};
@@
(
alloc(C1 * C2,...)
|
alloc(
- (E1) * (E2)
+ array_size(E1, E2)
,...)
)
// </smpl>
Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
---
drivers/staging/media/ipu3/ipu3-mmu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/staging/media/ipu3/ipu3-mmu.c b/drivers/staging/media/ipu3/ipu3-mmu.c
index cb9bf5fb29a5..9c4adb815c94 100644
--- a/drivers/staging/media/ipu3/ipu3-mmu.c
+++ b/drivers/staging/media/ipu3/ipu3-mmu.c
@@ -464,7 +464,7 @@ struct imgu_mmu_info *imgu_mmu_init(struct device *parent, void __iomem *base)
* Allocate the array of L2PT CPU pointers, initialized to zero,
* which means the dummy L2PT allocated above.
*/
- mmu->l2pts = vzalloc(IPU3_PT_PTES * sizeof(*mmu->l2pts));
+ mmu->l2pts = vzalloc(array_size(IPU3_PT_PTES, sizeof(*mmu->l2pts)));
if (!mmu->l2pts)
goto fail_l2pt;
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 23/26] media: staging: imgu: use array_size
2023-06-23 21:14 ` [PATCH 23/26] media: staging: imgu: " Julia Lawall
@ 2023-06-25 4:59 ` Bingbu Cao
2023-06-27 17:35 ` Julia Lawall
1 sibling, 0 replies; 5+ messages in thread
From: Bingbu Cao @ 2023-06-25 4:59 UTC (permalink / raw)
To: Julia Lawall, Sakari Ailus
Cc: keescook, kernel-janitors, Bingbu Cao, Tianshu Qiu,
Mauro Carvalho Chehab, Greg Kroah-Hartman, linux-media,
linux-staging, linux-kernel
Julia,
Thanks for your patch.
On 6/24/23 5:14 AM, Julia Lawall wrote:
> Use array_size to protect against multiplication overflows.
>
> The changes were done using the following Coccinelle semantic patch:
>
> // <smpl>
> @@
> expression E1, E2;
> constant C1, C2;
> identifier alloc = {vmalloc,vzalloc};
> @@
>
> (
> alloc(C1 * C2,...)
> |
> alloc(
> - (E1) * (E2)
> + array_size(E1, E2)
> ,...)
> )
> // </smpl>
>
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
>
> ---
> drivers/staging/media/ipu3/ipu3-mmu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/staging/media/ipu3/ipu3-mmu.c b/drivers/staging/media/ipu3/ipu3-mmu.c
> index cb9bf5fb29a5..9c4adb815c94 100644
> --- a/drivers/staging/media/ipu3/ipu3-mmu.c
> +++ b/drivers/staging/media/ipu3/ipu3-mmu.c
> @@ -464,7 +464,7 @@ struct imgu_mmu_info *imgu_mmu_init(struct device *parent, void __iomem *base)
> * Allocate the array of L2PT CPU pointers, initialized to zero,
> * which means the dummy L2PT allocated above.
> */
> - mmu->l2pts = vzalloc(IPU3_PT_PTES * sizeof(*mmu->l2pts));
> + mmu->l2pts = vzalloc(array_size(IPU3_PT_PTES, sizeof(*mmu->l2pts)));
> if (!mmu->l2pts)
> goto fail_l2pt;
>
>
Reviewed-by: Bingbu Cao <bingbu.cao@intel.com>
--
Best regards,
Bingbu Cao
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 23/26] media: staging: imgu: use array_size
2023-06-23 21:14 ` [PATCH 23/26] media: staging: imgu: " Julia Lawall
2023-06-25 4:59 ` Bingbu Cao
@ 2023-06-27 17:35 ` Julia Lawall
2023-06-29 7:34 ` Sakari Ailus
1 sibling, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2023-06-27 17:35 UTC (permalink / raw)
To: Julia Lawall
Cc: Sakari Ailus, keescook, kernel-janitors, Bingbu Cao, Tianshu Qiu,
Mauro Carvalho Chehab, Greg Kroah-Hartman, linux-media,
linux-staging, linux-kernel
On Fri, 23 Jun 2023, Julia Lawall wrote:
> Use array_size to protect against multiplication overflows.
>
> The changes were done using the following Coccinelle semantic patch:
>
> // <smpl>
> @@
> expression E1, E2;
> constant C1, C2;
> identifier alloc = {vmalloc,vzalloc};
> @@
>
> (
> alloc(C1 * C2,...)
> |
> alloc(
> - (E1) * (E2)
> + array_size(E1, E2)
> ,...)
> )
> // </smpl>
>
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
>
> ---
> drivers/staging/media/ipu3/ipu3-mmu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/staging/media/ipu3/ipu3-mmu.c b/drivers/staging/media/ipu3/ipu3-mmu.c
> index cb9bf5fb29a5..9c4adb815c94 100644
> --- a/drivers/staging/media/ipu3/ipu3-mmu.c
> +++ b/drivers/staging/media/ipu3/ipu3-mmu.c
> @@ -464,7 +464,7 @@ struct imgu_mmu_info *imgu_mmu_init(struct device *parent, void __iomem *base)
> * Allocate the array of L2PT CPU pointers, initialized to zero,
> * which means the dummy L2PT allocated above.
> */
> - mmu->l2pts = vzalloc(IPU3_PT_PTES * sizeof(*mmu->l2pts));
> + mmu->l2pts = vzalloc(array_size(IPU3_PT_PTES, sizeof(*mmu->l2pts)));
> if (!mmu->l2pts)
> goto fail_l2pt;
I think that this patch can be dropped. Since it is a multiplcation of
two constants, if there is an overflow, I guess the compiler would detect
it?
julia
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 23/26] media: staging: imgu: use array_size
2023-06-27 17:35 ` Julia Lawall
@ 2023-06-29 7:34 ` Sakari Ailus
0 siblings, 0 replies; 5+ messages in thread
From: Sakari Ailus @ 2023-06-29 7:34 UTC (permalink / raw)
To: Julia Lawall
Cc: keescook, kernel-janitors, Bingbu Cao, Tianshu Qiu,
Mauro Carvalho Chehab, Greg Kroah-Hartman, linux-media,
linux-staging, linux-kernel
Hi Julia, Bingbu,
On Tue, Jun 27, 2023 at 07:35:47PM +0200, Julia Lawall wrote:
>
>
> On Fri, 23 Jun 2023, Julia Lawall wrote:
>
> > Use array_size to protect against multiplication overflows.
> >
> > The changes were done using the following Coccinelle semantic patch:
> >
> > // <smpl>
> > @@
> > expression E1, E2;
> > constant C1, C2;
> > identifier alloc = {vmalloc,vzalloc};
> > @@
> >
> > (
> > alloc(C1 * C2,...)
> > |
> > alloc(
> > - (E1) * (E2)
> > + array_size(E1, E2)
> > ,...)
> > )
> > // </smpl>
> >
> > Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
> >
> > ---
> > drivers/staging/media/ipu3/ipu3-mmu.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/staging/media/ipu3/ipu3-mmu.c b/drivers/staging/media/ipu3/ipu3-mmu.c
> > index cb9bf5fb29a5..9c4adb815c94 100644
> > --- a/drivers/staging/media/ipu3/ipu3-mmu.c
> > +++ b/drivers/staging/media/ipu3/ipu3-mmu.c
> > @@ -464,7 +464,7 @@ struct imgu_mmu_info *imgu_mmu_init(struct device *parent, void __iomem *base)
> > * Allocate the array of L2PT CPU pointers, initialized to zero,
> > * which means the dummy L2PT allocated above.
> > */
> > - mmu->l2pts = vzalloc(IPU3_PT_PTES * sizeof(*mmu->l2pts));
> > + mmu->l2pts = vzalloc(array_size(IPU3_PT_PTES, sizeof(*mmu->l2pts)));
> > if (!mmu->l2pts)
> > goto fail_l2pt;
>
> I think that this patch can be dropped. Since it is a multiplcation of
> two constants, if there is an overflow, I guess the compiler would detect
> it?
Indeed. vcalloc() would be perhaps nicer but the original isn't wrong
either.
--
Kind regards,
Sakari Ailus
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-06-29 7:34 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
2023-06-23 21:14 ` [PATCH 23/26] media: staging: imgu: " Julia Lawall
2023-06-25 4:59 ` Bingbu Cao
2023-06-27 17:35 ` Julia Lawall
2023-06-29 7:34 ` Sakari Ailus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox