linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/26] use array_size
@ 2023-06-23 21:14 Julia Lawall
  2023-06-23 21:14 ` [PATCH 01/26] lib/test_vmalloc.c: " Julia Lawall
                   ` (25 more replies)
  0 siblings, 26 replies; 64+ 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] 64+ messages in thread

* [PATCH 01/26] lib/test_vmalloc.c: 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-23 21:14 ` [PATCH 02/26] octeon_ep: " Julia Lawall
                   ` (24 subsequent siblings)
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: linux-kernel; +Cc: keescook, kernel-janitors

Use array_size to protect against multiplication overflows.

The changes were done using the following Coccinelle semantic patch:

// <smpl>
@@
    size_t e1,e2;
    expression COUNT;
    identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
@@

(
      alloc(
-           (e1) * (e2)
+           array_size(e1, e2)
      ,...)
|
      alloc(
-           (e1) * (COUNT)
+           array_size(COUNT, e1)
      ,...)
)

@@
    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>

---
 lib/test_vmalloc.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c
index 3718d9886407..d02a47e0a72b 100644
--- a/lib/test_vmalloc.c
+++ b/lib/test_vmalloc.c
@@ -156,7 +156,7 @@ static int random_size_alloc_test(void)
 
 	for (i = 0; i < test_loop_count; i++) {
 		n = get_random_u32_inclusive(1, 100);
-		p = vmalloc(n * PAGE_SIZE);
+		p = vmalloc(array_size(n, PAGE_SIZE));
 
 		if (!p)
 			return -1;
@@ -175,7 +175,7 @@ static int long_busy_list_alloc_test(void)
 	int rv = -1;
 	int i;
 
-	ptr = vmalloc(sizeof(void *) * 15000);
+	ptr = vmalloc(array_size(15000, sizeof(void *)));
 	if (!ptr)
 		return rv;
 
@@ -221,11 +221,11 @@ static int full_fit_alloc_test(void)
 	junk_length = fls(num_online_cpus());
 	junk_length *= (32 * 1024 * 1024 / PAGE_SIZE);
 
-	ptr = vmalloc(sizeof(void *) * junk_length);
+	ptr = vmalloc(array_size(junk_length, sizeof(void *)));
 	if (!ptr)
 		return rv;
 
-	junk_ptr = vmalloc(sizeof(void *) * junk_length);
+	junk_ptr = vmalloc(array_size(junk_length, sizeof(void *)));
 	if (!junk_ptr) {
 		vfree(ptr);
 		return rv;
@@ -271,7 +271,7 @@ static int fix_size_alloc_test(void)
 		if (use_huge)
 			ptr = vmalloc_huge((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE, GFP_KERNEL);
 		else
-			ptr = vmalloc((nr_pages > 0 ? nr_pages:1) * PAGE_SIZE);
+			ptr = vmalloc(array_size(nr_pages > 0 ? nr_pages : 1, PAGE_SIZE));
 
 		if (!ptr)
 			return -1;
@@ -293,7 +293,7 @@ pcpu_alloc_test(void)
 	size_t size, align;
 	int i;
 
-	pcpu = vmalloc(sizeof(void __percpu *) * 35000);
+	pcpu = vmalloc(array_size(35000, sizeof(void __percpu *)));
 	if (!pcpu)
 		return -1;
 


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

* [PATCH 02/26] octeon_ep: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
  2023-06-23 21:14 ` [PATCH 01/26] lib/test_vmalloc.c: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-24 15:46   ` Simon Horman
  2023-06-24 22:28   ` Jakub Kicinski
  2023-06-23 21:14 ` [PATCH 03/26] drm/gud: " Julia Lawall
                   ` (23 subsequent siblings)
  25 siblings, 2 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Veerasenareddy Burru
  Cc: keescook, kernel-janitors, Abhijit Ayarekar, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, 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/net/ethernet/marvell/octeon_ep/octep_rx.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/marvell/octeon_ep/octep_rx.c b/drivers/net/ethernet/marvell/octeon_ep/octep_rx.c
index 392d9b0da0d7..185b7e50ee77 100644
--- a/drivers/net/ethernet/marvell/octeon_ep/octep_rx.c
+++ b/drivers/net/ethernet/marvell/octeon_ep/octep_rx.c
@@ -158,7 +158,7 @@ static int octep_setup_oq(struct octep_device *oct, int q_no)
 		goto desc_dma_alloc_err;
 	}
 
-	oq->buff_info = vzalloc(oq->max_count * OCTEP_OQ_RECVBUF_SIZE);
+	oq->buff_info = vzalloc(array_size(oq->max_count, OCTEP_OQ_RECVBUF_SIZE));
 	if (unlikely(!oq->buff_info)) {
 		dev_err(&oct->pdev->dev,
 			"Failed to allocate buffer info for OQ-%d\n", q_no);


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

* [PATCH 03/26] drm/gud: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
  2023-06-23 21:14 ` [PATCH 01/26] lib/test_vmalloc.c: " Julia Lawall
  2023-06-23 21:14 ` [PATCH 02/26] octeon_ep: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 04/26] gve: " Julia Lawall
                   ` (22 subsequent siblings)
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Noralf Trønnes
  Cc: keescook, kernel-janitors, David Airlie, Daniel Vetter,
	dri-devel, 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/gpu/drm/gud/gud_pipe.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/gud/gud_pipe.c b/drivers/gpu/drm/gud/gud_pipe.c
index dc16a92625d4..34df847bd829 100644
--- a/drivers/gpu/drm/gud/gud_pipe.c
+++ b/drivers/gpu/drm/gud/gud_pipe.c
@@ -390,7 +390,7 @@ static int gud_fb_queue_damage(struct gud_device *gdrm, struct drm_framebuffer *
 	mutex_lock(&gdrm->damage_lock);
 
 	if (!gdrm->shadow_buf) {
-		gdrm->shadow_buf = vzalloc(fb->pitches[0] * fb->height);
+		gdrm->shadow_buf = vzalloc(array_size(fb->pitches[0], fb->height));
 		if (!gdrm->shadow_buf) {
 			mutex_unlock(&gdrm->damage_lock);
 			return -ENOMEM;


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

* [PATCH 04/26] gve: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (2 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 03/26] drm/gud: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-24 15:47   ` Simon Horman
  2023-06-23 21:14 ` [PATCH 05/26] RDMA/erdma: " Julia Lawall
                   ` (21 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Jeroen de Borst
  Cc: keescook, kernel-janitors, Praveen Kaligineedi, Shailend Chand,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev, linux-kernel

Use array_size to protect against multiplication overflows.

The changes were done using the following Coccinelle semantic patch:

// <smpl>
@@
    size_t e1,e2;
    expression COUNT;
    identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
@@

(
      alloc(
-           (e1) * (e2)
+           array_size(e1, e2)
      ,...)
|
      alloc(
-           (e1) * (COUNT)
+           array_size(COUNT, e1)
      ,...)
)
// </smpl>

Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

---
 drivers/net/ethernet/google/gve/gve_tx.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/google/gve/gve_tx.c b/drivers/net/ethernet/google/gve/gve_tx.c
index 813da572abca..d77ebbb24936 100644
--- a/drivers/net/ethernet/google/gve/gve_tx.c
+++ b/drivers/net/ethernet/google/gve/gve_tx.c
@@ -248,7 +248,7 @@ static int gve_tx_alloc_ring(struct gve_priv *priv, int idx)
 	tx->mask = slots - 1;
 
 	/* alloc metadata */
-	tx->info = vzalloc(sizeof(*tx->info) * slots);
+	tx->info = vzalloc(array_size(slots, sizeof(*tx->info)));
 	if (!tx->info)
 		return -ENOMEM;
 


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

* [PATCH 05/26] RDMA/erdma: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (3 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 04/26] gve: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-27  9:32   ` Cheng Xu
  2023-06-23 21:14 ` [PATCH 06/26] dma-buf: system_heap: " Julia Lawall
                   ` (20 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Cheng Xu
  Cc: keescook, kernel-janitors, Kai Shen, Jason Gunthorpe,
	Leon Romanovsky, linux-rdma, 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/infiniband/hw/erdma/erdma_verbs.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/hw/erdma/erdma_verbs.c b/drivers/infiniband/hw/erdma/erdma_verbs.c
index 83e1b0d55977..c49160f6ff27 100644
--- a/drivers/infiniband/hw/erdma/erdma_verbs.c
+++ b/drivers/infiniband/hw/erdma/erdma_verbs.c
@@ -462,8 +462,8 @@ static int init_kernel_qp(struct erdma_dev *dev, struct erdma_qp *qp,
 		dev->func_bar + (ERDMA_SDB_SHARED_PAGE_INDEX << PAGE_SHIFT);
 	kqp->hw_rq_db = dev->func_bar + ERDMA_BAR_RQDB_SPACE_OFFSET;
 
-	kqp->swr_tbl = vmalloc(qp->attrs.sq_size * sizeof(u64));
-	kqp->rwr_tbl = vmalloc(qp->attrs.rq_size * sizeof(u64));
+	kqp->swr_tbl = vmalloc(array_size(qp->attrs.sq_size, sizeof(u64)));
+	kqp->rwr_tbl = vmalloc(array_size(qp->attrs.rq_size, sizeof(u64)));
 	if (!kqp->swr_tbl || !kqp->rwr_tbl)
 		goto err_out;
 


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

* [PATCH 06/26] dma-buf: system_heap: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (4 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 05/26] RDMA/erdma: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:42   ` John Stultz
  2023-06-23 21:14 ` [PATCH 07/26] scsi: fnic: " Julia Lawall
                   ` (19 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Sumit Semwal
  Cc: keescook, kernel-janitors, Benjamin Gaignard, Liam Mark,
	Laura Abbott, Brian Starkey, John Stultz, Christian König,
	linux-media, dri-devel, linaro-mm-sig, linux-kernel

Use array_size to protect against multiplication overflows.

The changes were done using the following Coccinelle semantic patch:

// <smpl>
@@
    size_t e1,e2;
    expression COUNT;
    identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
@@

(
      alloc(
-           (e1) * (e2)
+           array_size(e1, e2)
      ,...)
|
      alloc(
-           (e1) * (COUNT)
+           array_size(COUNT, e1)
      ,...)
)
// </smpl>

Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

---
 drivers/dma-buf/heaps/system_heap.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
index ee7059399e9c..fb7867599874 100644
--- a/drivers/dma-buf/heaps/system_heap.c
+++ b/drivers/dma-buf/heaps/system_heap.c
@@ -221,7 +221,7 @@ static void *system_heap_do_vmap(struct system_heap_buffer *buffer)
 {
 	struct sg_table *table = &buffer->sg_table;
 	int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;
-	struct page **pages = vmalloc(sizeof(struct page *) * npages);
+	struct page **pages = vmalloc(array_size(npages, sizeof(struct page *)));
 	struct page **tmp = pages;
 	struct sg_page_iter piter;
 	void *vaddr;


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

* [PATCH 07/26] scsi: fnic: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (5 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 06/26] dma-buf: system_heap: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 08/26] virtio-mem: " Julia Lawall
                   ` (18 subsequent siblings)
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Satish Kharat
  Cc: keescook, kernel-janitors, Sesidhar Baddela, Karan Tilak Kumar,
	James E.J. Bottomley, Martin K. Petersen, linux-scsi,
	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/scsi/fnic/fnic_trace.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/fnic/fnic_trace.c b/drivers/scsi/fnic/fnic_trace.c
index f3c3a26a1384..74d428c9f7d3 100644
--- a/drivers/scsi/fnic/fnic_trace.c
+++ b/drivers/scsi/fnic/fnic_trace.c
@@ -465,7 +465,7 @@ int fnic_trace_buf_init(void)
 	fnic_max_trace_entries = (trace_max_pages * PAGE_SIZE)/
 					  FNIC_ENTRY_SIZE_BYTES;
 
-	fnic_trace_buf_p = (unsigned long)vzalloc(trace_max_pages * PAGE_SIZE);
+	fnic_trace_buf_p = (unsigned long)vzalloc(array_size(trace_max_pages, PAGE_SIZE));
 	if (!fnic_trace_buf_p) {
 		printk(KERN_ERR PFX "Failed to allocate memory "
 				  "for fnic_trace_buf_p\n");


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

* [PATCH 08/26] virtio-mem: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (6 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 07/26] scsi: fnic: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-25  1:39   ` Xuan Zhuo
                     ` (2 more replies)
  2023-06-23 21:14 ` [PATCH 09/26] pds_core: " Julia Lawall
                   ` (17 subsequent siblings)
  25 siblings, 3 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: keescook, kernel-janitors, Michael S. Tsirkin, Jason Wang,
	Xuan Zhuo, virtualization, 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/virtio/virtio_mem.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 835f6cc2fb66..a4dfe7aab288 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -399,7 +399,7 @@ static int virtio_mem_bbm_bb_states_prepare_next_bb(struct virtio_mem *vm)
 	if (vm->bbm.bb_states && old_pages == new_pages)
 		return 0;
 
-	new_array = vzalloc(new_pages * PAGE_SIZE);
+	new_array = vzalloc(array_size(new_pages, PAGE_SIZE));
 	if (!new_array)
 		return -ENOMEM;
 
@@ -465,7 +465,7 @@ static int virtio_mem_sbm_mb_states_prepare_next_mb(struct virtio_mem *vm)
 	if (vm->sbm.mb_states && old_pages == new_pages)
 		return 0;
 
-	new_array = vzalloc(new_pages * PAGE_SIZE);
+	new_array = vzalloc(array_size(new_pages, PAGE_SIZE));
 	if (!new_array)
 		return -ENOMEM;
 
@@ -588,7 +588,7 @@ static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm)
 	if (vm->sbm.sb_states && old_pages == new_pages)
 		return 0;
 
-	new_bitmap = vzalloc(new_pages * PAGE_SIZE);
+	new_bitmap = vzalloc(array_size(new_pages, PAGE_SIZE));
 	if (!new_bitmap)
 		return -ENOMEM;
 


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

* [PATCH 09/26] pds_core: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (7 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 08/26] virtio-mem: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-24 15:47   ` Simon Horman
  2023-06-26 16:02   ` Shannon Nelson
  2023-06-23 21:14 ` [PATCH 10/26] bus: mhi: host: " Julia Lawall
                   ` (16 subsequent siblings)
  25 siblings, 2 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Shannon Nelson
  Cc: keescook, kernel-janitors, Brett Creeley, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, 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/net/ethernet/amd/pds_core/core.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/amd/pds_core/core.c b/drivers/net/ethernet/amd/pds_core/core.c
index 483a070d96fa..d87f45a1ee2f 100644
--- a/drivers/net/ethernet/amd/pds_core/core.c
+++ b/drivers/net/ethernet/amd/pds_core/core.c
@@ -196,7 +196,7 @@ int pdsc_qcq_alloc(struct pdsc *pdsc, unsigned int type, unsigned int index,
 	dma_addr_t q_base_pa;
 	int err;
 
-	qcq->q.info = vzalloc(num_descs * sizeof(*qcq->q.info));
+	qcq->q.info = vzalloc(array_size(num_descs, sizeof(*qcq->q.info)));
 	if (!qcq->q.info) {
 		err = -ENOMEM;
 		goto err_out;
@@ -219,7 +219,7 @@ int pdsc_qcq_alloc(struct pdsc *pdsc, unsigned int type, unsigned int index,
 	if (err)
 		goto err_out_free_q_info;
 
-	qcq->cq.info = vzalloc(num_descs * sizeof(*qcq->cq.info));
+	qcq->cq.info = vzalloc(array_size(num_descs, sizeof(*qcq->cq.info)));
 	if (!qcq->cq.info) {
 		err = -ENOMEM;
 		goto err_out_free_irq;


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

* [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (8 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 09/26] pds_core: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:30   ` Jeffrey Hugo
  2023-06-26 14:53   ` Jeffrey Hugo
  2023-06-23 21:14 ` [PATCH 11/26] ionic: " Julia Lawall
                   ` (15 subsequent siblings)
  25 siblings, 2 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Manivannan Sadhasivam
  Cc: keescook, kernel-janitors, mhi, linux-arm-msm, 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/bus/mhi/host/init.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/bus/mhi/host/init.c b/drivers/bus/mhi/host/init.c
index f72fcb66f408..34a543a67068 100644
--- a/drivers/bus/mhi/host/init.c
+++ b/drivers/bus/mhi/host/init.c
@@ -759,8 +759,8 @@ static int parse_ch_cfg(struct mhi_controller *mhi_cntrl,
 	 * so to avoid any memory possible allocation failures, vzalloc is
 	 * used here
 	 */
-	mhi_cntrl->mhi_chan = vzalloc(mhi_cntrl->max_chan *
-				      sizeof(*mhi_cntrl->mhi_chan));
+	mhi_cntrl->mhi_chan = vzalloc(array_size(mhi_cntrl->max_chan,
+				      sizeof(*mhi_cntrl->mhi_chan)));
 	if (!mhi_cntrl->mhi_chan)
 		return -ENOMEM;
 


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

* [PATCH 11/26] ionic: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (9 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 10/26] bus: mhi: host: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-24 15:47   ` Simon Horman
  2023-06-26 16:03   ` Shannon Nelson
  2023-06-23 21:14 ` [PATCH 12/26] btrfs: zoned: " Julia Lawall
                   ` (14 subsequent siblings)
  25 siblings, 2 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Shannon Nelson
  Cc: keescook, kernel-janitors, Brett Creeley, drivers,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev, 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/net/ethernet/pensando/ionic/ionic_lif.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/pensando/ionic/ionic_lif.c b/drivers/net/ethernet/pensando/ionic/ionic_lif.c
index 957027e546b3..f2e2c6853536 100644
--- a/drivers/net/ethernet/pensando/ionic/ionic_lif.c
+++ b/drivers/net/ethernet/pensando/ionic/ionic_lif.c
@@ -560,7 +560,7 @@ static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
 	new->q.dev = dev;
 	new->flags = flags;
 
-	new->q.info = vzalloc(num_descs * sizeof(*new->q.info));
+	new->q.info = vzalloc(array_size(num_descs, sizeof(*new->q.info)));
 	if (!new->q.info) {
 		netdev_err(lif->netdev, "Cannot allocate queue info\n");
 		err = -ENOMEM;
@@ -581,7 +581,7 @@ static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
 	if (err)
 		goto err_out;
 
-	new->cq.info = vzalloc(num_descs * sizeof(*new->cq.info));
+	new->cq.info = vzalloc(array_size(num_descs, sizeof(*new->cq.info)));
 	if (!new->cq.info) {
 		netdev_err(lif->netdev, "Cannot allocate completion queue info\n");
 		err = -ENOMEM;


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

* [PATCH 12/26] btrfs: zoned: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (10 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 11/26] ionic: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-26  6:08   ` Johannes Thumshirn
                     ` (2 more replies)
  2023-06-23 21:14 ` [PATCH 13/26] iommu/tegra: gart: " Julia Lawall
                   ` (13 subsequent siblings)
  25 siblings, 3 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Chris Mason
  Cc: keescook, kernel-janitors, Josef Bacik, David Sterba,
	linux-btrfs, linux-kernel

Use array_size to protect against multiplication overflows.

The changes were done using the following Coccinelle semantic patch:

// <smpl>
@@
    size_t e1,e2;
    expression COUNT;
    identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
@@

(
      alloc(
-           (e1) * (e2)
+           array_size(e1, e2)
      ,...)
|
      alloc(
-           (e1) * (COUNT)
+           array_size(COUNT, e1)
      ,...)
)
// </smpl>

Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

---
 fs/btrfs/zoned.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 39828af4a4e8..0550ce98dcae 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -464,8 +464,9 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
 	 * use the cache.
 	 */
 	if (populate_cache && bdev_is_zoned(device->bdev)) {
-		zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
-						zone_info->nr_zones);
+		zone_info->zone_cache =
+			vzalloc(array_size(zone_info->nr_zones,
+					   sizeof(struct blk_zone)));
 		if (!zone_info->zone_cache) {
 			btrfs_err_in_rcu(device->fs_info,
 				"zoned: failed to allocate zone cache for %s",


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

* [PATCH 13/26] iommu/tegra: gart: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (11 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 12/26] btrfs: zoned: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 14/26] RDMA/siw: " Julia Lawall
                   ` (12 subsequent siblings)
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Thierry Reding
  Cc: keescook, kernel-janitors, Krishna Reddy, Joerg Roedel,
	Will Deacon, Robin Murphy, Jonathan Hunter, linux-tegra, iommu,
	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/iommu/tegra-gart.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/tegra-gart.c b/drivers/iommu/tegra-gart.c
index a482ff838b53..def222da83f1 100644
--- a/drivers/iommu/tegra-gart.c
+++ b/drivers/iommu/tegra-gart.c
@@ -348,8 +348,8 @@ struct gart_device *tegra_gart_probe(struct device *dev, struct tegra_mc *mc)
 	if (err)
 		goto remove_sysfs;
 
-	gart->savedata = vmalloc(resource_size(res) / GART_PAGE_SIZE *
-				 sizeof(u32));
+	gart->savedata = vmalloc(array_size(resource_size(res) / GART_PAGE_SIZE,
+					    sizeof(u32)));
 	if (!gart->savedata) {
 		err = -ENOMEM;
 		goto unregister_iommu;


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

* [PATCH 14/26] RDMA/siw: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (12 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 13/26] iommu/tegra: gart: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-26 11:23   ` Bernard Metzler
  2023-06-23 21:14 ` [PATCH 15/26] habanalabs: " Julia Lawall
                   ` (11 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Bernard Metzler
  Cc: keescook, kernel-janitors, Jason Gunthorpe, Leon Romanovsky,
	linux-rdma, 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/infiniband/sw/siw/siw_qp.c    |    4 ++--
 drivers/infiniband/sw/siw/siw_verbs.c |    6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/infiniband/sw/siw/siw_qp.c b/drivers/infiniband/sw/siw/siw_qp.c
index 81e9bbd9ebda..32ec85af0314 100644
--- a/drivers/infiniband/sw/siw/siw_qp.c
+++ b/drivers/infiniband/sw/siw/siw_qp.c
@@ -204,7 +204,7 @@ static int siw_qp_readq_init(struct siw_qp *qp, int irq_size, int orq_size)
 {
 	if (irq_size) {
 		irq_size = roundup_pow_of_two(irq_size);
-		qp->irq = vzalloc(irq_size * sizeof(struct siw_sqe));
+		qp->irq = vzalloc(array_size(irq_size, sizeof(struct siw_sqe)));
 		if (!qp->irq) {
 			qp->attrs.irq_size = 0;
 			return -ENOMEM;
@@ -212,7 +212,7 @@ static int siw_qp_readq_init(struct siw_qp *qp, int irq_size, int orq_size)
 	}
 	if (orq_size) {
 		orq_size = roundup_pow_of_two(orq_size);
-		qp->orq = vzalloc(orq_size * sizeof(struct siw_sqe));
+		qp->orq = vzalloc(array_size(orq_size, sizeof(struct siw_sqe)));
 		if (!qp->orq) {
 			qp->attrs.orq_size = 0;
 			qp->attrs.irq_size = 0;
diff --git a/drivers/infiniband/sw/siw/siw_verbs.c b/drivers/infiniband/sw/siw/siw_verbs.c
index 398ec13db624..ddf83b638cb0 100644
--- a/drivers/infiniband/sw/siw/siw_verbs.c
+++ b/drivers/infiniband/sw/siw/siw_verbs.c
@@ -381,7 +381,7 @@ int siw_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
 	if (udata)
 		qp->sendq = vmalloc_user(num_sqe * sizeof(struct siw_sqe));
 	else
-		qp->sendq = vzalloc(num_sqe * sizeof(struct siw_sqe));
+		qp->sendq = vzalloc(array_size(num_sqe, sizeof(struct siw_sqe)));
 
 	if (qp->sendq == NULL) {
 		rv = -ENOMEM;
@@ -414,7 +414,7 @@ int siw_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
 			qp->recvq =
 				vmalloc_user(num_rqe * sizeof(struct siw_rqe));
 		else
-			qp->recvq = vzalloc(num_rqe * sizeof(struct siw_rqe));
+			qp->recvq = vzalloc(array_size(num_rqe, sizeof(struct siw_rqe)));
 
 		if (qp->recvq == NULL) {
 			rv = -ENOMEM;
@@ -1624,7 +1624,7 @@ int siw_create_srq(struct ib_srq *base_srq,
 		srq->recvq =
 			vmalloc_user(srq->num_rqe * sizeof(struct siw_rqe));
 	else
-		srq->recvq = vzalloc(srq->num_rqe * sizeof(struct siw_rqe));
+		srq->recvq = vzalloc(array_size(srq->num_rqe, sizeof(struct siw_rqe)));
 
 	if (srq->recvq == NULL) {
 		rv = -ENOMEM;


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

* [PATCH 15/26] habanalabs: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (13 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 14/26] RDMA/siw: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 16/26] drm/i915/gvt: " Julia Lawall
                   ` (10 subsequent siblings)
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Oded Gabbay; +Cc: keescook, kernel-janitors, dri-devel, 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/accel/habanalabs/common/device.c     |    3 ++-
 drivers/accel/habanalabs/common/state_dump.c |    6 +++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/accel/habanalabs/common/device.c b/drivers/accel/habanalabs/common/device.c
index fabfc501ef54..8e2edefa6d3c 100644
--- a/drivers/accel/habanalabs/common/device.c
+++ b/drivers/accel/habanalabs/common/device.c
@@ -2572,7 +2572,8 @@ static void hl_capture_user_mappings(struct hl_device *hdev, bool is_pmmu)
 	 */
 	vfree(pgf_info->user_mappings);
 	pgf_info->user_mappings =
-			vzalloc(pgf_info->num_of_user_mappings * sizeof(struct hl_user_mapping));
+			vzalloc(array_size(pgf_info->num_of_user_mappings,
+					   sizeof(struct hl_user_mapping)));
 	if (!pgf_info->user_mappings) {
 		pgf_info->num_of_user_mappings = 0;
 		goto finish;
diff --git a/drivers/accel/habanalabs/common/state_dump.c b/drivers/accel/habanalabs/common/state_dump.c
index 3a9931f24259..324cb7c9bc26 100644
--- a/drivers/accel/habanalabs/common/state_dump.c
+++ b/drivers/accel/habanalabs/common/state_dump.c
@@ -272,7 +272,7 @@ static u32 *hl_state_dump_read_sync_objects(struct hl_device *hdev, u32 index)
 	base_addr = sds->props[SP_SYNC_OBJ_BASE_ADDR] +
 			sds->props[SP_NEXT_SYNC_OBJ_ADDR] * index;
 
-	sync_objects = vmalloc(sds->props[SP_SYNC_OBJ_AMOUNT] * sizeof(u32));
+	sync_objects = vmalloc(array_size(sds->props[SP_SYNC_OBJ_AMOUNT], sizeof(u32)));
 	if (!sync_objects)
 		return NULL;
 
@@ -453,8 +453,8 @@ hl_state_dump_alloc_read_sm_block_monitors(struct hl_device *hdev, u32 index)
 	s64 base_addr; /* Base addr can be negative */
 	int i;
 
-	monitors = vmalloc(sds->props[SP_MONITORS_AMOUNT] *
-			   sizeof(struct hl_mon_state_dump));
+	monitors = vmalloc(array_size(sds->props[SP_MONITORS_AMOUNT],
+				      sizeof(struct hl_mon_state_dump)));
 	if (!monitors)
 		return NULL;
 


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

* [PATCH 16/26] drm/i915/gvt: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (14 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 15/26] habanalabs: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-26  9:26   ` [Intel-gfx] " Andi Shyti
  2023-06-23 21:14 ` [PATCH 17/26] kcov: " Julia Lawall
                   ` (9 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Zhenyu Wang
  Cc: keescook, kernel-janitors, Zhi Wang, Jani Nikula,
	Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin, David Airlie,
	Daniel Vetter, intel-gvt-dev, intel-gfx, dri-devel, 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/gpu/drm/i915/gvt/gtt.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c
index 4ec85308379a..df52385ad436 100644
--- a/drivers/gpu/drm/i915/gvt/gtt.c
+++ b/drivers/gpu/drm/i915/gvt/gtt.c
@@ -1969,14 +1969,16 @@ static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu)
 		return ERR_PTR(-ENOMEM);
 	}
 
-	mm->ggtt_mm.host_ggtt_aperture = vzalloc((vgpu_aperture_sz(vgpu) >> PAGE_SHIFT) * sizeof(u64));
+	mm->ggtt_mm.host_ggtt_aperture =
+		vzalloc(array_size(vgpu_aperture_sz(vgpu) >> PAGE_SHIFT, sizeof(u64)));
 	if (!mm->ggtt_mm.host_ggtt_aperture) {
 		vfree(mm->ggtt_mm.virtual_ggtt);
 		vgpu_free_mm(mm);
 		return ERR_PTR(-ENOMEM);
 	}
 
-	mm->ggtt_mm.host_ggtt_hidden = vzalloc((vgpu_hidden_sz(vgpu) >> PAGE_SHIFT) * sizeof(u64));
+	mm->ggtt_mm.host_ggtt_hidden =
+		vzalloc(array_size(vgpu_hidden_sz(vgpu) >> PAGE_SHIFT, sizeof(u64)));
 	if (!mm->ggtt_mm.host_ggtt_hidden) {
 		vfree(mm->ggtt_mm.host_ggtt_aperture);
 		vfree(mm->ggtt_mm.virtual_ggtt);


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

* [PATCH 17/26] kcov: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (15 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 16/26] drm/i915/gvt: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-24  5:34   ` Dmitry Vyukov
  2023-06-23 21:14 ` [PATCH 18/26] net: enetc: " Julia Lawall
                   ` (8 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Dmitry Vyukov
  Cc: keescook, kernel-janitors, Andrey Konovalov, kasan-dev, 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>

---
 kernel/kcov.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/kcov.c b/kernel/kcov.c
index 84c717337df0..631444760644 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -900,7 +900,7 @@ void kcov_remote_start(u64 handle)
 	/* Can only happen when in_task(). */
 	if (!area) {
 		local_unlock_irqrestore(&kcov_percpu_data.lock, flags);
-		area = vmalloc(size * sizeof(unsigned long));
+		area = vmalloc(array_size(size, sizeof(unsigned long)));
 		if (!area) {
 			kcov_put(kcov);
 			return;


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

* [PATCH 18/26] net: enetc: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (16 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 17/26] kcov: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-24 15:48   ` Simon Horman
  2023-06-23 21:14 ` [PATCH 19/26] RDMA/bnxt_re: " Julia Lawall
                   ` (7 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Claudiu Manoil
  Cc: keescook, kernel-janitors, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, 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/net/ethernet/freescale/enetc/enetc.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/freescale/enetc/enetc.c b/drivers/net/ethernet/freescale/enetc/enetc.c
index 9e1b2536e9a9..7231f8ea1ba4 100644
--- a/drivers/net/ethernet/freescale/enetc/enetc.c
+++ b/drivers/net/ethernet/freescale/enetc/enetc.c
@@ -1790,7 +1790,7 @@ static int enetc_alloc_tx_resource(struct enetc_bdr_resource *res,
 	res->bd_count = bd_count;
 	res->bd_size = sizeof(union enetc_tx_bd);
 
-	res->tx_swbd = vzalloc(bd_count * sizeof(*res->tx_swbd));
+	res->tx_swbd = vzalloc(array_size(bd_count, sizeof(*res->tx_swbd)));
 	if (!res->tx_swbd)
 		return -ENOMEM;
 
@@ -1878,7 +1878,7 @@ static int enetc_alloc_rx_resource(struct enetc_bdr_resource *res,
 	if (extended)
 		res->bd_size *= 2;
 
-	res->rx_swbd = vzalloc(bd_count * sizeof(struct enetc_rx_swbd));
+	res->rx_swbd = vzalloc(array_size(bd_count, sizeof(struct enetc_rx_swbd)));
 	if (!res->rx_swbd)
 		return -ENOMEM;
 


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

* [PATCH 19/26] RDMA/bnxt_re: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (17 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 18/26] net: enetc: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 20/26] drm/vmwgfx: " Julia Lawall
                   ` (6 subsequent siblings)
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Selvin Xavier
  Cc: keescook, kernel-janitors, Jason Gunthorpe, Leon Romanovsky,
	linux-rdma, 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/infiniband/hw/bnxt_re/qplib_res.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/hw/bnxt_re/qplib_res.c b/drivers/infiniband/hw/bnxt_re/qplib_res.c
index 81b0c5e879f9..f049b627e734 100644
--- a/drivers/infiniband/hw/bnxt_re/qplib_res.c
+++ b/drivers/infiniband/hw/bnxt_re/qplib_res.c
@@ -118,11 +118,11 @@ static int __alloc_pbl(struct bnxt_qplib_res *res,
 	else
 		pages = sginfo->npages;
 	/* page ptr arrays */
-	pbl->pg_arr = vmalloc(pages * sizeof(void *));
+	pbl->pg_arr = vmalloc(array_size(pages, sizeof(void *)));
 	if (!pbl->pg_arr)
 		return -ENOMEM;
 
-	pbl->pg_map_arr = vmalloc(pages * sizeof(dma_addr_t));
+	pbl->pg_map_arr = vmalloc(array_size(pages, sizeof(dma_addr_t)));
 	if (!pbl->pg_map_arr) {
 		vfree(pbl->pg_arr);
 		pbl->pg_arr = NULL;


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

* [PATCH 20/26] drm/vmwgfx: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (18 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 19/26] RDMA/bnxt_re: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-27 17:33   ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 21/26] x86/sgx: " Julia Lawall
                   ` (5 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Zack Rusin
  Cc: keescook, kernel-janitors, VMware Graphics Reviewers,
	David Airlie, Daniel Vetter, dri-devel, linux-kernel

Use array_size to protect against multiplication overflows.

The changes were done using the following Coccinelle semantic patch:

// <smpl>
@@
    size_t e1,e2;
    expression COUNT;
    identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
@@

(
      alloc(
-           (e1) * (e2)
+           array_size(e1, e2)
      ,...)
|
      alloc(
-           (e1) * (COUNT)
+           array_size(COUNT, e1)
      ,...)
)
// </smpl>

Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

---
 drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c b/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c
index 829df395c2ed..c72fc8111a11 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c
@@ -88,7 +88,7 @@ int vmw_devcaps_create(struct vmw_private *vmw)
 	uint32_t i;
 
 	if (gb_objects) {
-		vmw->devcaps = vzalloc(sizeof(uint32_t) * SVGA3D_DEVCAP_MAX);
+		vmw->devcaps = vzalloc(array_size(SVGA3D_DEVCAP_MAX, sizeof(uint32_t)));
 		if (!vmw->devcaps)
 			return -ENOMEM;
 		for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {


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

* [PATCH 21/26] x86/sgx: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (19 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 20/26] drm/vmwgfx: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-07-10 22:02   ` Jarkko Sakkinen
  2023-06-23 21:14 ` [PATCH 22/26] net: mana: " Julia Lawall
                   ` (4 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: keescook, kernel-janitors, Dave Hansen, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin, linux-sgx,
	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>

---
 arch/x86/kernel/cpu/sgx/main.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 166692f2d501..3a234942c586 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -628,7 +628,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
 	if (!section->virt_addr)
 		return false;
 
-	section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page));
+	section->pages = vmalloc(array_size(nr_pages,
+					    sizeof(struct sgx_epc_page)));
 	if (!section->pages) {
 		memunmap(section->virt_addr);
 		return false;


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

* [PATCH 22/26] net: mana: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (20 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 21/26] x86/sgx: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-24 15:48   ` Simon Horman
  2023-06-23 21:14 ` [PATCH 23/26] media: staging: imgu: " Julia Lawall
                   ` (3 subsequent siblings)
  25 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: K. Y. Srinivasan
  Cc: keescook, kernel-janitors, Haiyang Zhang, Wei Liu, Dexuan Cui,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	linux-hyperv, netdev, 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/net/ethernet/microsoft/mana/hw_channel.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/microsoft/mana/hw_channel.c b/drivers/net/ethernet/microsoft/mana/hw_channel.c
index 9d1507eba5b9..e82c513760f9 100644
--- a/drivers/net/ethernet/microsoft/mana/hw_channel.c
+++ b/drivers/net/ethernet/microsoft/mana/hw_channel.c
@@ -627,7 +627,7 @@ static int mana_hwc_establish_channel(struct gdma_context *gc, u16 *q_depth,
 	if (WARN_ON(cq->id >= gc->max_num_cqs))
 		return -EPROTO;
 
-	gc->cq_table = vzalloc(gc->max_num_cqs * sizeof(struct gdma_queue *));
+	gc->cq_table = vzalloc(array_size(gc->max_num_cqs, sizeof(struct gdma_queue *)));
 	if (!gc->cq_table)
 		return -ENOMEM;
 


^ permalink raw reply related	[flat|nested] 64+ 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
                   ` (21 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 22/26] net: mana: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-25  4:59   ` Bingbu Cao
  2023-06-27 17:35   ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 24/26] scsi: qla2xxx: " Julia Lawall
                   ` (2 subsequent siblings)
  25 siblings, 2 replies; 64+ 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] 64+ messages in thread

* [PATCH 24/26] scsi: qla2xxx: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (22 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 23/26] media: staging: imgu: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 25/26] vduse: " Julia Lawall
  2023-06-23 21:14 ` [PATCH 26/26] comedi: " Julia Lawall
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Nilesh Javali
  Cc: keescook, kernel-janitors, GR-QLogic-Storage-Upstream,
	James E.J. Bottomley, Martin K. Petersen, linux-scsi,
	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/scsi/qla2xxx/qla_init.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/qla2xxx/qla_init.c b/drivers/scsi/qla2xxx/qla_init.c
index 1a955c3ff3d6..72569ed6c825 100644
--- a/drivers/scsi/qla2xxx/qla_init.c
+++ b/drivers/scsi/qla2xxx/qla_init.c
@@ -8219,7 +8219,7 @@ qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
 		ql_dbg(ql_dbg_init, vha, 0x0163,
 		    "-> fwdt%u template allocate template %#x words...\n",
 		    j, risc_size);
-		fwdt->template = vmalloc(risc_size * sizeof(*dcode));
+		fwdt->template = vmalloc(array_size(risc_size, sizeof(*dcode)));
 		if (!fwdt->template) {
 			ql_log(ql_log_warn, vha, 0x0164,
 			    "-> fwdt%u failed allocate template.\n", j);
@@ -8474,7 +8474,7 @@ qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
 		ql_dbg(ql_dbg_init, vha, 0x0173,
 		    "-> fwdt%u template allocate template %#x words...\n",
 		    j, risc_size);
-		fwdt->template = vmalloc(risc_size * sizeof(*dcode));
+		fwdt->template = vmalloc(array_size(risc_size, sizeof(*dcode)));
 		if (!fwdt->template) {
 			ql_log(ql_log_warn, vha, 0x0174,
 			    "-> fwdt%u failed allocate template.\n", j);


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

* [PATCH 25/26] vduse: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (23 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 24/26] scsi: qla2xxx: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  2023-06-23 21:14 ` [PATCH 26/26] comedi: " Julia Lawall
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: keescook, kernel-janitors, Jason Wang, Xuan Zhuo, virtualization,
	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/vdpa/vdpa_user/iova_domain.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/iova_domain.c
index 5e4a77b9bae6..ee395e013086 100644
--- a/drivers/vdpa/vdpa_user/iova_domain.c
+++ b/drivers/vdpa/vdpa_user/iova_domain.c
@@ -571,8 +571,9 @@ vduse_domain_create(unsigned long iova_limit, size_t bounce_size)
 
 	domain->iova_limit = iova_limit;
 	domain->bounce_size = PAGE_ALIGN(bounce_size);
-	domain->bounce_maps = vzalloc(bounce_pfns *
-				sizeof(struct vduse_bounce_map));
+	domain->bounce_maps =
+		vzalloc(array_size(bounce_pfns,
+				   sizeof(struct vduse_bounce_map)));
 	if (!domain->bounce_maps)
 		goto err_map;
 

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

* [PATCH 26/26] comedi: use array_size
  2023-06-23 21:14 [PATCH 00/26] use array_size Julia Lawall
                   ` (24 preceding siblings ...)
  2023-06-23 21:14 ` [PATCH 25/26] vduse: " Julia Lawall
@ 2023-06-23 21:14 ` Julia Lawall
  25 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:14 UTC (permalink / raw)
  To: Ian Abbott; +Cc: keescook, kernel-janitors, H Hartley Sweeten, linux-kernel

Use array_size to protect against multiplication overflows.

The changes were done using the following Coccinelle semantic patch:

// <smpl>
@@
    size_t e1,e2;
    expression COUNT;
    identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
@@

(
      alloc(
-           (e1) * (e2)
+           array_size(e1, e2)
      ,...)
|
      alloc(
-           (e1) * (COUNT)
+           array_size(COUNT, e1)
      ,...)
)
// </smpl>

Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

---
 drivers/comedi/comedi_buf.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/comedi/comedi_buf.c b/drivers/comedi/comedi_buf.c
index 393966c09740..32ad3e6e1ce8 100644
--- a/drivers/comedi/comedi_buf.c
+++ b/drivers/comedi/comedi_buf.c
@@ -89,7 +89,7 @@ comedi_buf_map_alloc(struct comedi_device *dev, enum dma_data_direction dma_dir,
 		bm->dma_hw_dev = get_device(dev->hw_dev);
 	}
 
-	bm->page_list = vzalloc(sizeof(*buf) * n_pages);
+	bm->page_list = vzalloc(array_size(n_pages, sizeof(*buf)));
 	if (!bm->page_list)
 		goto err;
 
@@ -169,7 +169,7 @@ static void __comedi_buf_alloc(struct comedi_device *dev,
 		buf = &bm->page_list[0];
 		async->prealloc_buf = buf->virt_addr;
 	} else {
-		pages = vmalloc(sizeof(struct page *) * n_pages);
+		pages = vmalloc(array_size(n_pages, sizeof(struct page *)));
 		if (!pages)
 			return;
 


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

* Re: [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 21:14 ` [PATCH 10/26] bus: mhi: host: " Julia Lawall
@ 2023-06-23 21:30   ` Jeffrey Hugo
  2023-06-23 21:45     ` Julia Lawall
  2023-06-26 11:46     ` Dan Carpenter
  2023-06-26 14:53   ` Jeffrey Hugo
  1 sibling, 2 replies; 64+ messages in thread
From: Jeffrey Hugo @ 2023-06-23 21:30 UTC (permalink / raw)
  To: Julia Lawall, Manivannan Sadhasivam
  Cc: keescook, kernel-janitors, mhi, linux-arm-msm, linux-kernel

On 6/23/2023 3:14 PM, 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/bus/mhi/host/init.c |    4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/bus/mhi/host/init.c b/drivers/bus/mhi/host/init.c
> index f72fcb66f408..34a543a67068 100644
> --- a/drivers/bus/mhi/host/init.c
> +++ b/drivers/bus/mhi/host/init.c
> @@ -759,8 +759,8 @@ static int parse_ch_cfg(struct mhi_controller *mhi_cntrl,
>   	 * so to avoid any memory possible allocation failures, vzalloc is
>   	 * used here
>   	 */
> -	mhi_cntrl->mhi_chan = vzalloc(mhi_cntrl->max_chan *
> -				      sizeof(*mhi_cntrl->mhi_chan));
> +	mhi_cntrl->mhi_chan = vzalloc(array_size(mhi_cntrl->max_chan,
> +				      sizeof(*mhi_cntrl->mhi_chan)));
>   	if (!mhi_cntrl->mhi_chan)
>   		return -ENOMEM;
>   
> 
> 

This doesn't seem like a good fix.

If we've overflowed the multiplication, I don't think we should 
continue, and the function should return an error.  array_size() is 
going to return SIZE_MAX, and it looks like it is possible that 
vzalloc() may be able to allocate that successfully in some scenarios. 
However, that is going to be less memory than parse_ch_cfg() expected to 
allocate, so later on I expect the function will still corrupt memory - 
basically the same result as what the unchecked overflow would do.

I'm not convinced the semantic patch is bringing value as I suspect most 
of the code being patched is in the same situation.

-Jeff

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

* Re: [PATCH 06/26] dma-buf: system_heap: use array_size
  2023-06-23 21:14 ` [PATCH 06/26] dma-buf: system_heap: " Julia Lawall
@ 2023-06-23 21:42   ` John Stultz
  0 siblings, 0 replies; 64+ messages in thread
From: John Stultz @ 2023-06-23 21:42 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Sumit Semwal, keescook, kernel-janitors, Benjamin Gaignard,
	Liam Mark, Laura Abbott, Brian Starkey, Christian König,
	linux-media, dri-devel, linaro-mm-sig, linux-kernel,
	T.J. Mercier

On Fri, Jun 23, 2023 at 2:15 PM Julia Lawall <Julia.Lawall@inria.fr> wrote:
>
> Use array_size to protect against multiplication overflows.
>
> The changes were done using the following Coccinelle semantic patch:
>
> // <smpl>
> @@
>     size_t e1,e2;
>     expression COUNT;
>     identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
> @@
>
> (
>       alloc(
> -           (e1) * (e2)
> +           array_size(e1, e2)
>       ,...)
> |
>       alloc(
> -           (e1) * (COUNT)
> +           array_size(COUNT, e1)
>       ,...)
> )
> // </smpl>
>
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

Thanks for sending this out!

Acked-by: John Stultz <jstultz@google.com>

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

* Re: [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 21:30   ` Jeffrey Hugo
@ 2023-06-23 21:45     ` Julia Lawall
  2023-06-23 22:09       ` Jeffrey Hugo
  2023-06-26 11:46     ` Dan Carpenter
  1 sibling, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-23 21:45 UTC (permalink / raw)
  To: Jeffrey Hugo
  Cc: Manivannan Sadhasivam, keescook, kernel-janitors, mhi,
	linux-arm-msm, linux-kernel



On Fri, 23 Jun 2023, Jeffrey Hugo wrote:

> On 6/23/2023 3:14 PM, 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/bus/mhi/host/init.c |    4 ++--
> >   1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/bus/mhi/host/init.c b/drivers/bus/mhi/host/init.c
> > index f72fcb66f408..34a543a67068 100644
> > --- a/drivers/bus/mhi/host/init.c
> > +++ b/drivers/bus/mhi/host/init.c
> > @@ -759,8 +759,8 @@ static int parse_ch_cfg(struct mhi_controller
> > *mhi_cntrl,
> >   	 * so to avoid any memory possible allocation failures, vzalloc is
> >   	 * used here
> >   	 */
> > -	mhi_cntrl->mhi_chan = vzalloc(mhi_cntrl->max_chan *
> > -				      sizeof(*mhi_cntrl->mhi_chan));
> > +	mhi_cntrl->mhi_chan = vzalloc(array_size(mhi_cntrl->max_chan,
> > +				      sizeof(*mhi_cntrl->mhi_chan)));
> >   	if (!mhi_cntrl->mhi_chan)
> >   		return -ENOMEM;
> >
> >
>
> This doesn't seem like a good fix.
>
> If we've overflowed the multiplication, I don't think we should continue, and
> the function should return an error.  array_size() is going to return
> SIZE_MAX, and it looks like it is possible that vzalloc() may be able to
> allocate that successfully in some scenarios. However, that is going to be
> less memory than parse_ch_cfg() expected to allocate, so later on I expect the
> function will still corrupt memory - basically the same result as what the
> unchecked overflow would do.
>
> I'm not convinced the semantic patch is bringing value as I suspect most of
> the code being patched is in the same situation.

OK, this just brings the code in line with all the calls updated by Kees's
original patch, cited in the cover letter, which were all the
calls containing a multiplication that existed at the time.

42bc47b35320 ("treewide: Use array_size() in vmalloc()")
fad953ce0b22 ("treewide: Use array_size() in vzalloc()")

julia

>
> -Jeff
>

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

* Re: [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 21:45     ` Julia Lawall
@ 2023-06-23 22:09       ` Jeffrey Hugo
  2023-06-23 23:45         ` Kees Cook
  0 siblings, 1 reply; 64+ messages in thread
From: Jeffrey Hugo @ 2023-06-23 22:09 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Manivannan Sadhasivam, keescook, kernel-janitors, mhi,
	linux-arm-msm, linux-kernel

On 6/23/2023 3:45 PM, Julia Lawall wrote:
> 
> 
> On Fri, 23 Jun 2023, Jeffrey Hugo wrote:
> 
>> On 6/23/2023 3:14 PM, 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/bus/mhi/host/init.c |    4 ++--
>>>    1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/bus/mhi/host/init.c b/drivers/bus/mhi/host/init.c
>>> index f72fcb66f408..34a543a67068 100644
>>> --- a/drivers/bus/mhi/host/init.c
>>> +++ b/drivers/bus/mhi/host/init.c
>>> @@ -759,8 +759,8 @@ static int parse_ch_cfg(struct mhi_controller
>>> *mhi_cntrl,
>>>    	 * so to avoid any memory possible allocation failures, vzalloc is
>>>    	 * used here
>>>    	 */
>>> -	mhi_cntrl->mhi_chan = vzalloc(mhi_cntrl->max_chan *
>>> -				      sizeof(*mhi_cntrl->mhi_chan));
>>> +	mhi_cntrl->mhi_chan = vzalloc(array_size(mhi_cntrl->max_chan,
>>> +				      sizeof(*mhi_cntrl->mhi_chan)));
>>>    	if (!mhi_cntrl->mhi_chan)
>>>    		return -ENOMEM;
>>>
>>>
>>
>> This doesn't seem like a good fix.
>>
>> If we've overflowed the multiplication, I don't think we should continue, and
>> the function should return an error.  array_size() is going to return
>> SIZE_MAX, and it looks like it is possible that vzalloc() may be able to
>> allocate that successfully in some scenarios. However, that is going to be
>> less memory than parse_ch_cfg() expected to allocate, so later on I expect the
>> function will still corrupt memory - basically the same result as what the
>> unchecked overflow would do.
>>
>> I'm not convinced the semantic patch is bringing value as I suspect most of
>> the code being patched is in the same situation.
> 
> OK, this just brings the code in line with all the calls updated by Kees's
> original patch, cited in the cover letter, which were all the
> calls containing a multiplication that existed at the time.
> 
> 42bc47b35320 ("treewide: Use array_size() in vmalloc()")
> fad953ce0b22 ("treewide: Use array_size() in vzalloc()")

Eh.  I "git show fad953ce0b22" and it doesn't really tell me much.  The 
commit asserts that uses of vzalloc() and multiplication need 
array_size(), but doesn't really explain why.

This looks like a brute force automated update with no thought and I 
fear the result of this change is the conclusion that we've solved 
multiplication overflow, when it doesn't look like we've really done 
much.  Sure, the multiplication gets capped, but can the code actually 
handle that?

I should probably run the numbers, but with the relevant spec capping 
the number of channels at 256, I don't think we can realistically 
approach overflow, even on a 32-bit system.  However, having correct 
code that is inherently safe seems like a good idea and so I feel this 
function has an issue.  I just don't think this automated conversion 
meaningfully does anything to improve the code here.

Kees, would you please chime in and educate me here?  I feel like I'm 
missing something important here.

-Jeff

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

* Re: [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 22:09       ` Jeffrey Hugo
@ 2023-06-23 23:45         ` Kees Cook
  2023-06-24 16:06           ` Jeffrey Hugo
  0 siblings, 1 reply; 64+ messages in thread
From: Kees Cook @ 2023-06-23 23:45 UTC (permalink / raw)
  To: Jeffrey Hugo
  Cc: Julia Lawall, Manivannan Sadhasivam, kernel-janitors, mhi,
	linux-arm-msm, linux-kernel

On Fri, Jun 23, 2023 at 04:09:46PM -0600, Jeffrey Hugo wrote:
> Kees, would you please chime in and educate me here?  I feel like I'm
> missing something important here.

The array_size() family will saturate at SIZE_MAX (rather than potentially
wrapping around). No allocator can fulfil a 18446744073709551615 byte
(18 exabyte) allocation. :) So the NULL return value will (hopefully)
trigger an error path.

-- 
Kees Cook

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

* Re: [PATCH 17/26] kcov: use array_size
  2023-06-23 21:14 ` [PATCH 17/26] kcov: " Julia Lawall
@ 2023-06-24  5:34   ` Dmitry Vyukov
  0 siblings, 0 replies; 64+ messages in thread
From: Dmitry Vyukov @ 2023-06-24  5:34 UTC (permalink / raw)
  To: Julia.Lawall
  Cc: keescook, kernel-janitors, Andrey Konovalov, kasan-dev, linux-kernel

On Fri, 23 Jun 2023 at 23:15, Julia Lawall <Julia.Lawall@inria.fr> 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>

Reviewed-by: Dmitry Vyukov <dvyukov@google.com>

> ---
>  kernel/kcov.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/kcov.c b/kernel/kcov.c
> index 84c717337df0..631444760644 100644
> --- a/kernel/kcov.c
> +++ b/kernel/kcov.c
> @@ -900,7 +900,7 @@ void kcov_remote_start(u64 handle)
>         /* Can only happen when in_task(). */
>         if (!area) {
>                 local_unlock_irqrestore(&kcov_percpu_data.lock, flags);
> -               area = vmalloc(size * sizeof(unsigned long));
> +               area = vmalloc(array_size(size, sizeof(unsigned long)));
>                 if (!area) {
>                         kcov_put(kcov);
>                         return;
>

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

* Re: [PATCH 02/26] octeon_ep: use array_size
  2023-06-23 21:14 ` [PATCH 02/26] octeon_ep: " Julia Lawall
@ 2023-06-24 15:46   ` Simon Horman
  2023-06-24 22:28   ` Jakub Kicinski
  1 sibling, 0 replies; 64+ messages in thread
From: Simon Horman @ 2023-06-24 15:46 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Veerasenareddy Burru, keescook, kernel-janitors,
	Abhijit Ayarekar, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, netdev, linux-kernel

On Fri, Jun 23, 2023 at 11:14:33PM +0200, 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>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 04/26] gve: use array_size
  2023-06-23 21:14 ` [PATCH 04/26] gve: " Julia Lawall
@ 2023-06-24 15:47   ` Simon Horman
  0 siblings, 0 replies; 64+ messages in thread
From: Simon Horman @ 2023-06-24 15:47 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Jeroen de Borst, keescook, kernel-janitors, Praveen Kaligineedi,
	Shailend Chand, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, netdev, linux-kernel

On Fri, Jun 23, 2023 at 11:14:35PM +0200, Julia Lawall wrote:
> Use array_size to protect against multiplication overflows.
> 
> The changes were done using the following Coccinelle semantic patch:
> 
> // <smpl>
> @@
>     size_t e1,e2;
>     expression COUNT;
>     identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
> @@
> 
> (
>       alloc(
> -           (e1) * (e2)
> +           array_size(e1, e2)
>       ,...)
> |
>       alloc(
> -           (e1) * (COUNT)
> +           array_size(COUNT, e1)
>       ,...)
> )
> // </smpl>
> 
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 09/26] pds_core: use array_size
  2023-06-23 21:14 ` [PATCH 09/26] pds_core: " Julia Lawall
@ 2023-06-24 15:47   ` Simon Horman
  2023-06-26 16:02   ` Shannon Nelson
  1 sibling, 0 replies; 64+ messages in thread
From: Simon Horman @ 2023-06-24 15:47 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Shannon Nelson, keescook, kernel-janitors, Brett Creeley,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev, linux-kernel

On Fri, Jun 23, 2023 at 11:14:40PM +0200, 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>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 11/26] ionic: use array_size
  2023-06-23 21:14 ` [PATCH 11/26] ionic: " Julia Lawall
@ 2023-06-24 15:47   ` Simon Horman
  2023-06-26 16:03   ` Shannon Nelson
  1 sibling, 0 replies; 64+ messages in thread
From: Simon Horman @ 2023-06-24 15:47 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Shannon Nelson, keescook, kernel-janitors, Brett Creeley,
	drivers, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, netdev, linux-kernel

On Fri, Jun 23, 2023 at 11:14:42PM +0200, 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>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 18/26] net: enetc: use array_size
  2023-06-23 21:14 ` [PATCH 18/26] net: enetc: " Julia Lawall
@ 2023-06-24 15:48   ` Simon Horman
  0 siblings, 0 replies; 64+ messages in thread
From: Simon Horman @ 2023-06-24 15:48 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Claudiu Manoil, keescook, kernel-janitors, Vladimir Oltean,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev, linux-kernel

On Fri, Jun 23, 2023 at 11:14:49PM +0200, 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>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 22/26] net: mana: use array_size
  2023-06-23 21:14 ` [PATCH 22/26] net: mana: " Julia Lawall
@ 2023-06-24 15:48   ` Simon Horman
  0 siblings, 0 replies; 64+ messages in thread
From: Simon Horman @ 2023-06-24 15:48 UTC (permalink / raw)
  To: Julia Lawall
  Cc: K. Y. Srinivasan, keescook, kernel-janitors, Haiyang Zhang,
	Wei Liu, Dexuan Cui, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, linux-hyperv, netdev, linux-kernel

On Fri, Jun 23, 2023 at 11:14:53PM +0200, 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>

Reviewed-by: Simon Horman <simon.horman@corigine.com>


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

* Re: [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 23:45         ` Kees Cook
@ 2023-06-24 16:06           ` Jeffrey Hugo
  0 siblings, 0 replies; 64+ messages in thread
From: Jeffrey Hugo @ 2023-06-24 16:06 UTC (permalink / raw)
  To: Kees Cook
  Cc: Julia Lawall, Manivannan Sadhasivam, kernel-janitors, mhi,
	linux-arm-msm, linux-kernel

On 6/23/2023 5:45 PM, Kees Cook wrote:
> On Fri, Jun 23, 2023 at 04:09:46PM -0600, Jeffrey Hugo wrote:
>> Kees, would you please chime in and educate me here?  I feel like I'm
>> missing something important here.
> 
> The array_size() family will saturate at SIZE_MAX (rather than potentially
> wrapping around). No allocator can fulfil a 18446744073709551615 byte
> (18 exabyte) allocation. :) So the NULL return value will (hopefully)
> trigger an error path.
> 

Fair enough, that handles the 64-bit usecase.  I'm guessing the 
assumption is that on a 32-bit usecase where size_t is ~4GB, there won't 
actually be 4GB to allocate and things will also fail.  So far, so good.

What about a 32-bit system with something like ARM's LPAE (Large 
Physical Address Extension) where the host is 32-bit, and so size_t 
would be ~4GB (as far as I can tell) but phys_addr_t is larger than 
that, and so we can have/access more than 4GB of resources?  Lets see, 
ignoring that its a 13 year old feature and probably not in circulation 
anymore, probably still can't satisfy a 4GB allocation since you'd need 
to map all of it to address it, and part of the address space is surely 
reserved for other things.

Ok, I think I'm convinced.  I'm going to sleep on it, but I suspect all 
will still be good early next week.

Thank you for the explanation.

-Jeff

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

* Re: [PATCH 02/26] octeon_ep: use array_size
  2023-06-23 21:14 ` [PATCH 02/26] octeon_ep: " Julia Lawall
  2023-06-24 15:46   ` Simon Horman
@ 2023-06-24 22:28   ` Jakub Kicinski
  2023-06-25 20:14     ` Christophe JAILLET
  1 sibling, 1 reply; 64+ messages in thread
From: Jakub Kicinski @ 2023-06-24 22:28 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Veerasenareddy Burru, keescook, kernel-janitors,
	Abhijit Ayarekar, David S. Miller, Eric Dumazet, Paolo Abeni,
	netdev, linux-kernel

On Fri, 23 Jun 2023 23:14:33 +0200 Julia Lawall wrote:
> -	oq->buff_info = vzalloc(oq->max_count * OCTEP_OQ_RECVBUF_SIZE);
> +	oq->buff_info = vzalloc(array_size(oq->max_count, OCTEP_OQ_RECVBUF_SIZE));

vcalloc seems to exist, is there a reason array_size() is preferred?
-- 
pw-bot: cr

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

* Re: [PATCH 08/26] virtio-mem: use array_size
  2023-06-23 21:14 ` [PATCH 08/26] virtio-mem: " Julia Lawall
@ 2023-06-25  1:39   ` Xuan Zhuo
  2023-06-26  7:40   ` David Hildenbrand
  2023-06-26 10:59   ` Michael S. Tsirkin
  2 siblings, 0 replies; 64+ messages in thread
From: Xuan Zhuo @ 2023-06-25  1:39 UTC (permalink / raw)
  To: Julia Lawall
  Cc: keescook, kernel-janitors, Michael S. Tsirkin, Jason Wang,
	virtualization, linux-kernel, David Hildenbrand

On Fri, 23 Jun 2023 23:14:39 +0200, Julia Lawall <Julia.Lawall@inria.fr> 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>

Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>

>
> ---
>  drivers/virtio/virtio_mem.c |    6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
> index 835f6cc2fb66..a4dfe7aab288 100644
> --- a/drivers/virtio/virtio_mem.c
> +++ b/drivers/virtio/virtio_mem.c
> @@ -399,7 +399,7 @@ static int virtio_mem_bbm_bb_states_prepare_next_bb(struct virtio_mem *vm)
>  	if (vm->bbm.bb_states && old_pages == new_pages)
>  		return 0;
>
> -	new_array = vzalloc(new_pages * PAGE_SIZE);
> +	new_array = vzalloc(array_size(new_pages, PAGE_SIZE));
>  	if (!new_array)
>  		return -ENOMEM;
>
> @@ -465,7 +465,7 @@ static int virtio_mem_sbm_mb_states_prepare_next_mb(struct virtio_mem *vm)
>  	if (vm->sbm.mb_states && old_pages == new_pages)
>  		return 0;
>
> -	new_array = vzalloc(new_pages * PAGE_SIZE);
> +	new_array = vzalloc(array_size(new_pages, PAGE_SIZE));
>  	if (!new_array)
>  		return -ENOMEM;
>
> @@ -588,7 +588,7 @@ static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm)
>  	if (vm->sbm.sb_states && old_pages == new_pages)
>  		return 0;
>
> -	new_bitmap = vzalloc(new_pages * PAGE_SIZE);
> +	new_bitmap = vzalloc(array_size(new_pages, PAGE_SIZE));
>  	if (!new_bitmap)
>  		return -ENOMEM;
>
>

^ permalink raw reply	[flat|nested] 64+ 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; 64+ 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] 64+ messages in thread

* Re: [PATCH 02/26] octeon_ep: use array_size
  2023-06-24 22:28   ` Jakub Kicinski
@ 2023-06-25 20:14     ` Christophe JAILLET
  2023-06-25 20:25       ` Julia Lawall
  0 siblings, 1 reply; 64+ messages in thread
From: Christophe JAILLET @ 2023-06-25 20:14 UTC (permalink / raw)
  To: Jakub Kicinski, Julia Lawall
  Cc: Veerasenareddy Burru, keescook, kernel-janitors,
	Abhijit Ayarekar, David S. Miller, Eric Dumazet, Paolo Abeni,
	netdev, linux-kernel, corbet

Le 25/06/2023 à 00:28, Jakub Kicinski a écrit :
> On Fri, 23 Jun 2023 23:14:33 +0200 Julia Lawall wrote:
>> -	oq->buff_info = vzalloc(oq->max_count * OCTEP_OQ_RECVBUF_SIZE);
>> +	oq->buff_info = vzalloc(array_size(oq->max_count, OCTEP_OQ_RECVBUF_SIZE));
> 
> vcalloc seems to exist, is there a reason array_size() is preferred?

Hi,

just for your information, I've just sent [1].

CJ

[1]: 
https://lore.kernel.org/all/3484e46180dd2cf05d993ff1a78b481bc2ad1f71.1687723931.git.christophe.jaillet@wanadoo.fr/


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

* Re: [PATCH 02/26] octeon_ep: use array_size
  2023-06-25 20:14     ` Christophe JAILLET
@ 2023-06-25 20:25       ` Julia Lawall
  2023-06-25 20:32         ` Christophe JAILLET
  0 siblings, 1 reply; 64+ messages in thread
From: Julia Lawall @ 2023-06-25 20:25 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Jakub Kicinski, Julia Lawall, Veerasenareddy Burru, keescook,
	kernel-janitors, Abhijit Ayarekar, David S. Miller, Eric Dumazet,
	Paolo Abeni, netdev, linux-kernel, corbet

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



On Sun, 25 Jun 2023, Christophe JAILLET wrote:

> Le 25/06/2023 à 00:28, Jakub Kicinski a écrit :
> > On Fri, 23 Jun 2023 23:14:33 +0200 Julia Lawall wrote:
> > > -	oq->buff_info = vzalloc(oq->max_count * OCTEP_OQ_RECVBUF_SIZE);
> > > +	oq->buff_info = vzalloc(array_size(oq->max_count,
> > > OCTEP_OQ_RECVBUF_SIZE));
> >
> > vcalloc seems to exist, is there a reason array_size() is preferred?
>
> Hi,
>
> just for your information, I've just sent [1].
>
> CJ
>
> [1]:
> https://lore.kernel.org/all/3484e46180dd2cf05d993ff1a78b481bc2ad1f71.1687723931.git.christophe.jaillet@wanadoo.fr/

For some reason, I have only received Christophe's mail, not Jakub's...

In any case, thanks for pointing out the existence of these functions.  I
just redid what Kees did in 2018, when I guess these functions didn't
exist.  I will look more carefully to see what functions are now available
and resend the whole thing.

Thanks!

julia

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

* Re: [PATCH 02/26] octeon_ep: use array_size
  2023-06-25 20:25       ` Julia Lawall
@ 2023-06-25 20:32         ` Christophe JAILLET
  2023-06-25 20:57           ` Julia Lawall
  0 siblings, 1 reply; 64+ messages in thread
From: Christophe JAILLET @ 2023-06-25 20:32 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Jakub Kicinski, Veerasenareddy Burru, keescook, kernel-janitors,
	Abhijit Ayarekar, David S. Miller, Eric Dumazet, Paolo Abeni,
	netdev, linux-kernel, corbet

Le 25/06/2023 à 22:25, Julia Lawall a écrit :
> 
> 
> On Sun, 25 Jun 2023, Christophe JAILLET wrote:
> 
>> Le 25/06/2023 à 00:28, Jakub Kicinski a écrit :
>>> On Fri, 23 Jun 2023 23:14:33 +0200 Julia Lawall wrote:
>>>> -	oq->buff_info = vzalloc(oq->max_count * OCTEP_OQ_RECVBUF_SIZE);
>>>> +	oq->buff_info = vzalloc(array_size(oq->max_count,
>>>> OCTEP_OQ_RECVBUF_SIZE));
>>>
>>> vcalloc seems to exist, is there a reason array_size() is preferred?
>>
>> Hi,
>>
>> just for your information, I've just sent [1].
>>
>> CJ
>>
>> [1]:
>> https://lore.kernel.org/all/3484e46180dd2cf05d993ff1a78b481bc2ad1f71.1687723931.git.christophe.jaillet@wanadoo.fr/
> 
> For some reason, I have only received Christophe's mail, not Jakub's...
> 
> In any case, thanks for pointing out the existence of these functions.  I
> just redid what Kees did in 2018, when I guess these functions didn't
> exist.  I will look more carefully to see what functions are now available
> and resend the whole thing.

Hi,

should you want to go 1 step further and simplify some code:

git grep v[mz]alloc.*array_size\( | wc -l
174

CJ

> 
> Thanks!
> 
> julia


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

* Re: [PATCH 02/26] octeon_ep: use array_size
  2023-06-25 20:32         ` Christophe JAILLET
@ 2023-06-25 20:57           ` Julia Lawall
  0 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-25 20:57 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Jakub Kicinski, Veerasenareddy Burru, keescook, kernel-janitors,
	Abhijit Ayarekar, David S. Miller, Eric Dumazet, Paolo Abeni,
	netdev, linux-kernel, corbet

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



On Sun, 25 Jun 2023, Christophe JAILLET wrote:

> Le 25/06/2023 à 22:25, Julia Lawall a écrit :
> >
> >
> > On Sun, 25 Jun 2023, Christophe JAILLET wrote:
> >
> > > Le 25/06/2023 à 00:28, Jakub Kicinski a écrit :
> > > > On Fri, 23 Jun 2023 23:14:33 +0200 Julia Lawall wrote:
> > > > > -	oq->buff_info = vzalloc(oq->max_count *
> > > > > OCTEP_OQ_RECVBUF_SIZE);
> > > > > +	oq->buff_info = vzalloc(array_size(oq->max_count,
> > > > > OCTEP_OQ_RECVBUF_SIZE));
> > > >
> > > > vcalloc seems to exist, is there a reason array_size() is preferred?
> > >
> > > Hi,
> > >
> > > just for your information, I've just sent [1].
> > >
> > > CJ
> > >
> > > [1]:
> > > https://lore.kernel.org/all/3484e46180dd2cf05d993ff1a78b481bc2ad1f71.1687723931.git.christophe.jaillet@wanadoo.fr/
> >
> > For some reason, I have only received Christophe's mail, not Jakub's...
> >
> > In any case, thanks for pointing out the existence of these functions.  I
> > just redid what Kees did in 2018, when I guess these functions didn't
> > exist.  I will look more carefully to see what functions are now available
> > and resend the whole thing.
>
> Hi,
>
> should you want to go 1 step further and simplify some code:
>
> git grep v[mz]alloc.*array_size\( | wc -l
> 174

Yes, thanks for the suggestion.

julia

>
> CJ
>
> >
> > Thanks!
> >
> > julia
>
>

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

* Re: [PATCH 12/26] btrfs: zoned: use array_size
  2023-06-23 21:14 ` [PATCH 12/26] btrfs: zoned: " Julia Lawall
@ 2023-06-26  6:08   ` Johannes Thumshirn
  2023-06-26  7:59   ` Naohiro Aota
  2023-06-29 14:30   ` David Sterba
  2 siblings, 0 replies; 64+ messages in thread
From: Johannes Thumshirn @ 2023-06-26  6:08 UTC (permalink / raw)
  To: Julia Lawall, Chris Mason
  Cc: keescook, kernel-janitors, Josef Bacik, David Sterba,
	linux-btrfs, linux-kernel

Looks good,
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>

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

* Re: [PATCH 08/26] virtio-mem: use array_size
  2023-06-23 21:14 ` [PATCH 08/26] virtio-mem: " Julia Lawall
  2023-06-25  1:39   ` Xuan Zhuo
@ 2023-06-26  7:40   ` David Hildenbrand
  2023-06-26 10:59   ` Michael S. Tsirkin
  2 siblings, 0 replies; 64+ messages in thread
From: David Hildenbrand @ 2023-06-26  7:40 UTC (permalink / raw)
  To: Julia Lawall
  Cc: keescook, kernel-janitors, Michael S. Tsirkin, Jason Wang,
	Xuan Zhuo, virtualization, linux-kernel

On 23.06.23 23:14, 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/virtio/virtio_mem.c |    6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)



Reviewed-by: David Hildenbrand <david@redhat.com>

-- 
Cheers,

David / dhildenb


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

* Re: [PATCH 12/26] btrfs: zoned: use array_size
  2023-06-23 21:14 ` [PATCH 12/26] btrfs: zoned: " Julia Lawall
  2023-06-26  6:08   ` Johannes Thumshirn
@ 2023-06-26  7:59   ` Naohiro Aota
  2023-06-29 14:30   ` David Sterba
  2 siblings, 0 replies; 64+ messages in thread
From: Naohiro Aota @ 2023-06-26  7:59 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Chris Mason, keescook, kernel-janitors, Josef Bacik,
	David Sterba, linux-btrfs, linux-kernel

On Fri, Jun 23, 2023 at 11:14:43PM +0200, Julia Lawall wrote:
> Use array_size to protect against multiplication overflows.
> 
> The changes were done using the following Coccinelle semantic patch:
> 
> // <smpl>
> @@
>     size_t e1,e2;
>     expression COUNT;
>     identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
> @@
> 
> (
>       alloc(
> -           (e1) * (e2)
> +           array_size(e1, e2)
>       ,...)
> |
>       alloc(
> -           (e1) * (COUNT)
> +           array_size(COUNT, e1)
>       ,...)
> )
> // </smpl>
> 
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

Looks good.

Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>

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

* Re: [Intel-gfx] [PATCH 16/26] drm/i915/gvt: use array_size
  2023-06-23 21:14 ` [PATCH 16/26] drm/i915/gvt: " Julia Lawall
@ 2023-06-26  9:26   ` Andi Shyti
  2023-06-26 11:54     ` Dan Carpenter
  0 siblings, 1 reply; 64+ messages in thread
From: Andi Shyti @ 2023-06-26  9:26 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Zhenyu Wang, keescook, intel-gvt-dev, intel-gfx, kernel-janitors,
	linux-kernel, dri-devel, Daniel Vetter, Rodrigo Vivi,
	David Airlie

Hi Julia,

On Fri, Jun 23, 2023 at 11:14:47PM +0200, 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/gpu/drm/i915/gvt/gtt.c |    6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c
> index 4ec85308379a..df52385ad436 100644
> --- a/drivers/gpu/drm/i915/gvt/gtt.c
> +++ b/drivers/gpu/drm/i915/gvt/gtt.c
> @@ -1969,14 +1969,16 @@ static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu)
>  		return ERR_PTR(-ENOMEM);
>  	}
>  
> -	mm->ggtt_mm.host_ggtt_aperture = vzalloc((vgpu_aperture_sz(vgpu) >> PAGE_SHIFT) * sizeof(u64));
> +	mm->ggtt_mm.host_ggtt_aperture =
> +		vzalloc(array_size(vgpu_aperture_sz(vgpu) >> PAGE_SHIFT, sizeof(u64)));
>  	if (!mm->ggtt_mm.host_ggtt_aperture) {
>  		vfree(mm->ggtt_mm.virtual_ggtt);
>  		vgpu_free_mm(mm);
>  		return ERR_PTR(-ENOMEM);
>  	}
>  
> -	mm->ggtt_mm.host_ggtt_hidden = vzalloc((vgpu_hidden_sz(vgpu) >> PAGE_SHIFT) * sizeof(u64));
> +	mm->ggtt_mm.host_ggtt_hidden =
> +		vzalloc(array_size(vgpu_hidden_sz(vgpu) >> PAGE_SHIFT, sizeof(u64)));

thanks for this patch, but I see an issue here. array_size()
truncates the allocation to SIZE_MAX, and I'm OK with it.

The problem is that no error is notified and the user doesn't
know that a truncation has happened. So that if we save from an
overflow here, we might encur to an unwanted access later when we
would start using the array for the size we think is allocated.

kmalloc_array(), for example, returns NULL of there is a
multiplication overflow and I think that's a better behaviour,
although more drastic.

Andi

>  	if (!mm->ggtt_mm.host_ggtt_hidden) {
>  		vfree(mm->ggtt_mm.host_ggtt_aperture);
>  		vfree(mm->ggtt_mm.virtual_ggtt);

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

* Re: [PATCH 08/26] virtio-mem: use array_size
  2023-06-23 21:14 ` [PATCH 08/26] virtio-mem: " Julia Lawall
  2023-06-25  1:39   ` Xuan Zhuo
  2023-06-26  7:40   ` David Hildenbrand
@ 2023-06-26 10:59   ` Michael S. Tsirkin
  2 siblings, 0 replies; 64+ messages in thread
From: Michael S. Tsirkin @ 2023-06-26 10:59 UTC (permalink / raw)
  To: Julia Lawall
  Cc: David Hildenbrand, keescook, kernel-janitors, Jason Wang,
	Xuan Zhuo, virtualization, linux-kernel

On Fri, Jun 23, 2023 at 11:14:39PM +0200, 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/virtio/virtio_mem.c |    6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)

can't hurt I guess.

Acked-by: Michael S. Tsirkin <mst@redhat.com>


> diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
> index 835f6cc2fb66..a4dfe7aab288 100644
> --- a/drivers/virtio/virtio_mem.c
> +++ b/drivers/virtio/virtio_mem.c
> @@ -399,7 +399,7 @@ static int virtio_mem_bbm_bb_states_prepare_next_bb(struct virtio_mem *vm)
>  	if (vm->bbm.bb_states && old_pages == new_pages)
>  		return 0;
>  
> -	new_array = vzalloc(new_pages * PAGE_SIZE);
> +	new_array = vzalloc(array_size(new_pages, PAGE_SIZE));
>  	if (!new_array)
>  		return -ENOMEM;
>
> @@ -465,7 +465,7 @@ static int virtio_mem_sbm_mb_states_prepare_next_mb(struct virtio_mem *vm)
>  	if (vm->sbm.mb_states && old_pages == new_pages)
>  		return 0;
>  
> -	new_array = vzalloc(new_pages * PAGE_SIZE);
> +	new_array = vzalloc(array_size(new_pages, PAGE_SIZE));
>  	if (!new_array)
>  		return -ENOMEM;
>  
> @@ -588,7 +588,7 @@ static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm)
>  	if (vm->sbm.sb_states && old_pages == new_pages)
>  		return 0;
>  
> -	new_bitmap = vzalloc(new_pages * PAGE_SIZE);
> +	new_bitmap = vzalloc(array_size(new_pages, PAGE_SIZE));
>  	if (!new_bitmap)
>  		return -ENOMEM;
>  


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

* RE: [PATCH 14/26] RDMA/siw: use array_size
  2023-06-23 21:14 ` [PATCH 14/26] RDMA/siw: " Julia Lawall
@ 2023-06-26 11:23   ` Bernard Metzler
  0 siblings, 0 replies; 64+ messages in thread
From: Bernard Metzler @ 2023-06-26 11:23 UTC (permalink / raw)
  To: Julia Lawall
  Cc: keescook, kernel-janitors, Jason Gunthorpe, Leon Romanovsky,
	linux-rdma, linux-kernel



> -----Original Message-----
> From: Julia Lawall <JuGlia.Lawall@inria.fr>
> Sent: Friday, 23 June 2023 23:15
> To: Bernard Metzler <BMT@zurich.ibm.com>
> Cc: keescook@chromium.org; kernel-janitors@vger.kernel.org; Jason Gunthorpe
> <jgg@ziepe.ca>; Leon Romanovsky <leon@kernel.org>; linux-
> rdma@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: [EXTERNAL] [PATCH 14/26] RDMA/siw: use array_size
> 
> 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/infiniband/sw/siw/siw_qp.c    |    4 ++--
>  drivers/infiniband/sw/siw/siw_verbs.c |    6 +++---
>  2 files changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/infiniband/sw/siw/siw_qp.c
> b/drivers/infiniband/sw/siw/siw_qp.c
> index 81e9bbd9ebda..32ec85af0314 100644
> --- a/drivers/infiniband/sw/siw/siw_qp.c
> +++ b/drivers/infiniband/sw/siw/siw_qp.c
> @@ -204,7 +204,7 @@ static int siw_qp_readq_init(struct siw_qp *qp, int
> irq_size, int orq_size)
>  {
>  	if (irq_size) {
>  		irq_size = roundup_pow_of_two(irq_size);
> -		qp->irq = vzalloc(irq_size * sizeof(struct siw_sqe));
> +		qp->irq = vzalloc(array_size(irq_size, sizeof(struct siw_sqe)));
>  		if (!qp->irq) {
>  			qp->attrs.irq_size = 0;
>  			return -ENOMEM;
> @@ -212,7 +212,7 @@ static int siw_qp_readq_init(struct siw_qp *qp, int
> irq_size, int orq_size)
>  	}
>  	if (orq_size) {
>  		orq_size = roundup_pow_of_two(orq_size);
> -		qp->orq = vzalloc(orq_size * sizeof(struct siw_sqe));
> +		qp->orq = vzalloc(array_size(orq_size, sizeof(struct siw_sqe)));
>  		if (!qp->orq) {
>  			qp->attrs.orq_size = 0;
>  			qp->attrs.irq_size = 0;
> diff --git a/drivers/infiniband/sw/siw/siw_verbs.c
> b/drivers/infiniband/sw/siw/siw_verbs.c
> index 398ec13db624..ddf83b638cb0 100644
> --- a/drivers/infiniband/sw/siw/siw_verbs.c
> +++ b/drivers/infiniband/sw/siw/siw_verbs.c
> @@ -381,7 +381,7 @@ int siw_create_qp(struct ib_qp *ibqp, struct
> ib_qp_init_attr *attrs,
>  	if (udata)
>  		qp->sendq = vmalloc_user(num_sqe * sizeof(struct siw_sqe));
>  	else
> -		qp->sendq = vzalloc(num_sqe * sizeof(struct siw_sqe));
> +		qp->sendq = vzalloc(array_size(num_sqe, sizeof(struct
> siw_sqe)));
> 
>  	if (qp->sendq == NULL) {
>  		rv = -ENOMEM;
> @@ -414,7 +414,7 @@ int siw_create_qp(struct ib_qp *ibqp, struct
> ib_qp_init_attr *attrs,
>  			qp->recvq =
>  				vmalloc_user(num_rqe * sizeof(struct siw_rqe));
>  		else
> -			qp->recvq = vzalloc(num_rqe * sizeof(struct siw_rqe));
> +			qp->recvq = vzalloc(array_size(num_rqe, sizeof(struct
> siw_rqe)));
> 
>  		if (qp->recvq == NULL) {
>  			rv = -ENOMEM;
> @@ -1624,7 +1624,7 @@ int siw_create_srq(struct ib_srq *base_srq,
>  		srq->recvq =
>  			vmalloc_user(srq->num_rqe * sizeof(struct siw_rqe));
>  	else
> -		srq->recvq = vzalloc(srq->num_rqe * sizeof(struct siw_rqe));
> +		srq->recvq = vzalloc(array_size(srq->num_rqe, sizeof(struct
> siw_rqe)));
> 
>  	if (srq->recvq == NULL) {
>  		rv = -ENOMEM;

lgtm!

Reviewed-by: Bernard Metzler <bmt@zurich.ibm.com>


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

* Re: [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 21:30   ` Jeffrey Hugo
  2023-06-23 21:45     ` Julia Lawall
@ 2023-06-26 11:46     ` Dan Carpenter
  1 sibling, 0 replies; 64+ messages in thread
From: Dan Carpenter @ 2023-06-26 11:46 UTC (permalink / raw)
  To: Jeffrey Hugo
  Cc: Julia Lawall, Manivannan Sadhasivam, keescook, kernel-janitors,
	mhi, linux-arm-msm, linux-kernel

On Fri, Jun 23, 2023 at 03:30:36PM -0600, Jeffrey Hugo wrote:
> On 6/23/2023 3:14 PM, 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/bus/mhi/host/init.c |    4 ++--
> >   1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/bus/mhi/host/init.c b/drivers/bus/mhi/host/init.c
> > index f72fcb66f408..34a543a67068 100644
> > --- a/drivers/bus/mhi/host/init.c
> > +++ b/drivers/bus/mhi/host/init.c
> > @@ -759,8 +759,8 @@ static int parse_ch_cfg(struct mhi_controller *mhi_cntrl,
> >   	 * so to avoid any memory possible allocation failures, vzalloc is
> >   	 * used here
> >   	 */
> > -	mhi_cntrl->mhi_chan = vzalloc(mhi_cntrl->max_chan *
> > -				      sizeof(*mhi_cntrl->mhi_chan));
> > +	mhi_cntrl->mhi_chan = vzalloc(array_size(mhi_cntrl->max_chan,
> > +				      sizeof(*mhi_cntrl->mhi_chan)));
> >   	if (!mhi_cntrl->mhi_chan)
> >   		return -ENOMEM;
> > 
> > 
> 
> This doesn't seem like a good fix.
> 
> If we've overflowed the multiplication, I don't think we should continue,
> and the function should return an error.  array_size() is going to return
> SIZE_MAX, and it looks like it is possible that vzalloc() may be able to
> allocate that successfully in some scenarios.

Nope.  You can never allocate more that size_t because that's the
highest number that the kernel allocation functions can accept.

Obviously on 64bit size_t is unbelievably large.  If I remember right,
on 32bit you didn't used to be able to allocate more than 2GB without
doing all sorts of tricks.  And everyone deleted those tricks when 64bit
machines became super common.

regards,
dan carpenter


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

* Re: [Intel-gfx] [PATCH 16/26] drm/i915/gvt: use array_size
  2023-06-26  9:26   ` [Intel-gfx] " Andi Shyti
@ 2023-06-26 11:54     ` Dan Carpenter
  0 siblings, 0 replies; 64+ messages in thread
From: Dan Carpenter @ 2023-06-26 11:54 UTC (permalink / raw)
  To: Andi Shyti
  Cc: Julia Lawall, Zhenyu Wang, keescook, intel-gvt-dev, intel-gfx,
	kernel-janitors, linux-kernel, dri-devel, Daniel Vetter,
	Rodrigo Vivi, David Airlie

On Mon, Jun 26, 2023 at 11:26:55AM +0200, Andi Shyti wrote:
> > diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c
> > index 4ec85308379a..df52385ad436 100644
> > --- a/drivers/gpu/drm/i915/gvt/gtt.c
> > +++ b/drivers/gpu/drm/i915/gvt/gtt.c
> > @@ -1969,14 +1969,16 @@ static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu)
> >  		return ERR_PTR(-ENOMEM);
> >  	}
> >  
> > -	mm->ggtt_mm.host_ggtt_aperture = vzalloc((vgpu_aperture_sz(vgpu) >> PAGE_SHIFT) * sizeof(u64));
> > +	mm->ggtt_mm.host_ggtt_aperture =
> > +		vzalloc(array_size(vgpu_aperture_sz(vgpu) >> PAGE_SHIFT, sizeof(u64)));
> >  	if (!mm->ggtt_mm.host_ggtt_aperture) {
> >  		vfree(mm->ggtt_mm.virtual_ggtt);
> >  		vgpu_free_mm(mm);
> >  		return ERR_PTR(-ENOMEM);
> >  	}
> >  
> > -	mm->ggtt_mm.host_ggtt_hidden = vzalloc((vgpu_hidden_sz(vgpu) >> PAGE_SHIFT) * sizeof(u64));
> > +	mm->ggtt_mm.host_ggtt_hidden =
> > +		vzalloc(array_size(vgpu_hidden_sz(vgpu) >> PAGE_SHIFT, sizeof(u64)));
> 
> thanks for this patch, but I see an issue here. array_size()
> truncates the allocation to SIZE_MAX, and I'm OK with it.
> 
> The problem is that no error is notified and the user doesn't
> know that a truncation has happened. So that if we save from an
> overflow here, we might encur to an unwanted access later when we
> would start using the array for the size we think is allocated.

SIZE_MAX allocations are guaranteed to fail, so the NULL check
will still return -ENOMEM.

> 
> kmalloc_array(), for example, returns NULL of there is a
> multiplication overflow and I think that's a better behaviour,
> although more drastic.

It's the same either way.

regards,
dan carpenter



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

* Re: [PATCH 10/26] bus: mhi: host: use array_size
  2023-06-23 21:14 ` [PATCH 10/26] bus: mhi: host: " Julia Lawall
  2023-06-23 21:30   ` Jeffrey Hugo
@ 2023-06-26 14:53   ` Jeffrey Hugo
  1 sibling, 0 replies; 64+ messages in thread
From: Jeffrey Hugo @ 2023-06-26 14:53 UTC (permalink / raw)
  To: Julia Lawall, Manivannan Sadhasivam
  Cc: keescook, kernel-janitors, mhi, linux-arm-msm, linux-kernel

On 6/23/2023 3:14 PM, 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>
> 

Reviewed-by: Jeffrey Hugo <quic_jhugo@quicinc.com>
Tested-by: Jeffrey Hugo <quic_jhugo@quicinc.com>

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

* Re: [PATCH 09/26] pds_core: use array_size
  2023-06-23 21:14 ` [PATCH 09/26] pds_core: " Julia Lawall
  2023-06-24 15:47   ` Simon Horman
@ 2023-06-26 16:02   ` Shannon Nelson
  1 sibling, 0 replies; 64+ messages in thread
From: Shannon Nelson @ 2023-06-26 16:02 UTC (permalink / raw)
  To: Julia Lawall
  Cc: keescook, kernel-janitors, Brett Creeley, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel

On 6/23/23 2:14 PM, 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>

Thanks,
Acked-by: Shannon Nelson <shannon.nelson@amd.com>

> 
> ---
>   drivers/net/ethernet/amd/pds_core/core.c |    4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/amd/pds_core/core.c b/drivers/net/ethernet/amd/pds_core/core.c
> index 483a070d96fa..d87f45a1ee2f 100644
> --- a/drivers/net/ethernet/amd/pds_core/core.c
> +++ b/drivers/net/ethernet/amd/pds_core/core.c
> @@ -196,7 +196,7 @@ int pdsc_qcq_alloc(struct pdsc *pdsc, unsigned int type, unsigned int index,
>          dma_addr_t q_base_pa;
>          int err;
> 
> -       qcq->q.info = vzalloc(num_descs * sizeof(*qcq->q.info));
> +       qcq->q.info = vzalloc(array_size(num_descs, sizeof(*qcq->q.info)));
>          if (!qcq->q.info) {
>                  err = -ENOMEM;
>                  goto err_out;
> @@ -219,7 +219,7 @@ int pdsc_qcq_alloc(struct pdsc *pdsc, unsigned int type, unsigned int index,
>          if (err)
>                  goto err_out_free_q_info;
> 
> -       qcq->cq.info = vzalloc(num_descs * sizeof(*qcq->cq.info));
> +       qcq->cq.info = vzalloc(array_size(num_descs, sizeof(*qcq->cq.info)));
>          if (!qcq->cq.info) {
>                  err = -ENOMEM;
>                  goto err_out_free_irq;
> 

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

* Re: [PATCH 11/26] ionic: use array_size
  2023-06-23 21:14 ` [PATCH 11/26] ionic: " Julia Lawall
  2023-06-24 15:47   ` Simon Horman
@ 2023-06-26 16:03   ` Shannon Nelson
  1 sibling, 0 replies; 64+ messages in thread
From: Shannon Nelson @ 2023-06-26 16:03 UTC (permalink / raw)
  To: Julia Lawall
  Cc: keescook, kernel-janitors, Brett Creeley, drivers,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev, linux-kernel

On 6/23/23 2:14 PM, 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>

Thanks,
Acked-by: Shannon Nelson <shannon.nelson@amd.com>

> 
> ---
>   drivers/net/ethernet/pensando/ionic/ionic_lif.c |    4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/pensando/ionic/ionic_lif.c b/drivers/net/ethernet/pensando/ionic/ionic_lif.c
> index 957027e546b3..f2e2c6853536 100644
> --- a/drivers/net/ethernet/pensando/ionic/ionic_lif.c
> +++ b/drivers/net/ethernet/pensando/ionic/ionic_lif.c
> @@ -560,7 +560,7 @@ static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
>          new->q.dev = dev;
>          new->flags = flags;
> 
> -       new->q.info = vzalloc(num_descs * sizeof(*new->q.info));
> +       new->q.info = vzalloc(array_size(num_descs, sizeof(*new->q.info)));
>          if (!new->q.info) {
>                  netdev_err(lif->netdev, "Cannot allocate queue info\n");
>                  err = -ENOMEM;
> @@ -581,7 +581,7 @@ static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
>          if (err)
>                  goto err_out;
> 
> -       new->cq.info = vzalloc(num_descs * sizeof(*new->cq.info));
> +       new->cq.info = vzalloc(array_size(num_descs, sizeof(*new->cq.info)));
>          if (!new->cq.info) {
>                  netdev_err(lif->netdev, "Cannot allocate completion queue info\n");
>                  err = -ENOMEM;
> 
> --
> You received this message because you are subscribed to the Google Groups "Pensando Drivers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to drivers+unsubscribe@pensando.io.
> To view this discussion on the web visit https://groups.google.com/a/pensando.io/d/msgid/drivers/20230623211457.102544-12-Julia.Lawall%40inria.fr.

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

* Re: [PATCH 05/26] RDMA/erdma: use array_size
  2023-06-23 21:14 ` [PATCH 05/26] RDMA/erdma: " Julia Lawall
@ 2023-06-27  9:32   ` Cheng Xu
  0 siblings, 0 replies; 64+ messages in thread
From: Cheng Xu @ 2023-06-27  9:32 UTC (permalink / raw)
  To: Julia Lawall
  Cc: keescook, kernel-janitors, Kai Shen, Jason Gunthorpe,
	Leon Romanovsky, linux-rdma, linux-kernel



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/infiniband/hw/erdma/erdma_verbs.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Thanks,

Acked-by: Cheng Xu <chengyou@linux.alibaba.com>

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

* Re: [PATCH 20/26] drm/vmwgfx: use array_size
  2023-06-23 21:14 ` [PATCH 20/26] drm/vmwgfx: " Julia Lawall
@ 2023-06-27 17:33   ` Julia Lawall
  0 siblings, 0 replies; 64+ messages in thread
From: Julia Lawall @ 2023-06-27 17:33 UTC (permalink / raw)
  To: Zack Rusin
  Cc: keescook, kernel-janitors, VMware Graphics Reviewers,
	David Airlie, Daniel Vetter, dri-devel, 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>
> @@
>     size_t e1,e2;
>     expression COUNT;
>     identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
> @@
>
> (
>       alloc(
> -           (e1) * (e2)
> +           array_size(e1, e2)
>       ,...)
> |
>       alloc(
> -           (e1) * (COUNT)
> +           array_size(COUNT, e1)
>       ,...)
> )
> // </smpl>
>
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
>
> ---
>  drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c b/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c
> index 829df395c2ed..c72fc8111a11 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_devcaps.c
> @@ -88,7 +88,7 @@ int vmw_devcaps_create(struct vmw_private *vmw)
>  	uint32_t i;
>
>  	if (gb_objects) {
> -		vmw->devcaps = vzalloc(sizeof(uint32_t) * SVGA3D_DEVCAP_MAX);
> +		vmw->devcaps = vzalloc(array_size(SVGA3D_DEVCAP_MAX, sizeof(uint32_t)));
>  		if (!vmw->devcaps)
>  			return -ENOMEM;
>  		for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {

Hello,

I think this patch can be dropped, since it's a multiplication of two
constants and no overflow should be possible.

julia

^ permalink raw reply	[flat|nested] 64+ 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; 64+ 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] 64+ 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; 64+ 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] 64+ messages in thread

* Re: [PATCH 12/26] btrfs: zoned: use array_size
  2023-06-23 21:14 ` [PATCH 12/26] btrfs: zoned: " Julia Lawall
  2023-06-26  6:08   ` Johannes Thumshirn
  2023-06-26  7:59   ` Naohiro Aota
@ 2023-06-29 14:30   ` David Sterba
  2 siblings, 0 replies; 64+ messages in thread
From: David Sterba @ 2023-06-29 14:30 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Chris Mason, keescook, kernel-janitors, Josef Bacik,
	David Sterba, linux-btrfs, linux-kernel

On Fri, Jun 23, 2023 at 11:14:43PM +0200, Julia Lawall wrote:
> Use array_size to protect against multiplication overflows.
> 
> The changes were done using the following Coccinelle semantic patch:
> 
> // <smpl>
> @@
>     size_t e1,e2;
>     expression COUNT;
>     identifier alloc = {vmalloc,vzalloc,kvmalloc,kvzalloc};
> @@
> 
> (
>       alloc(
> -           (e1) * (e2)
> +           array_size(e1, e2)
>       ,...)
> |
>       alloc(
> -           (e1) * (COUNT)
> +           array_size(COUNT, e1)
>       ,...)
> )
> // </smpl>
> 
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>

Added to misc-next with updated subject and changelog, thanks.

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

* Re: [PATCH 21/26] x86/sgx: use array_size
  2023-06-23 21:14 ` [PATCH 21/26] x86/sgx: " Julia Lawall
@ 2023-07-10 22:02   ` Jarkko Sakkinen
  0 siblings, 0 replies; 64+ messages in thread
From: Jarkko Sakkinen @ 2023-07-10 22:02 UTC (permalink / raw)
  To: Julia Lawall
  Cc: keescook, kernel-janitors, Dave Hansen, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin, linux-sgx,
	linux-kernel

On Fri, 2023-06-23 at 23:14 +0200, 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>
> 
> ---
>  arch/x86/kernel/cpu/sgx/main.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
> index 166692f2d501..3a234942c586 100644
> --- a/arch/x86/kernel/cpu/sgx/main.c
> +++ b/arch/x86/kernel/cpu/sgx/main.c
> @@ -628,7 +628,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
>  	if (!section->virt_addr)
>  		return false;
>  
> -	section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page));
> +	section->pages = vmalloc(array_size(nr_pages,
> +					    sizeof(struct sgx_epc_page)));
>  	if (!section->pages) {
>  		memunmap(section->virt_addr);
>  		return false;
> 

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko

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

end of thread, other threads:[~2023-07-10 22:03 UTC | newest]

Thread overview: 64+ 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 01/26] lib/test_vmalloc.c: " Julia Lawall
2023-06-23 21:14 ` [PATCH 02/26] octeon_ep: " Julia Lawall
2023-06-24 15:46   ` Simon Horman
2023-06-24 22:28   ` Jakub Kicinski
2023-06-25 20:14     ` Christophe JAILLET
2023-06-25 20:25       ` Julia Lawall
2023-06-25 20:32         ` Christophe JAILLET
2023-06-25 20:57           ` Julia Lawall
2023-06-23 21:14 ` [PATCH 03/26] drm/gud: " Julia Lawall
2023-06-23 21:14 ` [PATCH 04/26] gve: " Julia Lawall
2023-06-24 15:47   ` Simon Horman
2023-06-23 21:14 ` [PATCH 05/26] RDMA/erdma: " Julia Lawall
2023-06-27  9:32   ` Cheng Xu
2023-06-23 21:14 ` [PATCH 06/26] dma-buf: system_heap: " Julia Lawall
2023-06-23 21:42   ` John Stultz
2023-06-23 21:14 ` [PATCH 07/26] scsi: fnic: " Julia Lawall
2023-06-23 21:14 ` [PATCH 08/26] virtio-mem: " Julia Lawall
2023-06-25  1:39   ` Xuan Zhuo
2023-06-26  7:40   ` David Hildenbrand
2023-06-26 10:59   ` Michael S. Tsirkin
2023-06-23 21:14 ` [PATCH 09/26] pds_core: " Julia Lawall
2023-06-24 15:47   ` Simon Horman
2023-06-26 16:02   ` Shannon Nelson
2023-06-23 21:14 ` [PATCH 10/26] bus: mhi: host: " Julia Lawall
2023-06-23 21:30   ` Jeffrey Hugo
2023-06-23 21:45     ` Julia Lawall
2023-06-23 22:09       ` Jeffrey Hugo
2023-06-23 23:45         ` Kees Cook
2023-06-24 16:06           ` Jeffrey Hugo
2023-06-26 11:46     ` Dan Carpenter
2023-06-26 14:53   ` Jeffrey Hugo
2023-06-23 21:14 ` [PATCH 11/26] ionic: " Julia Lawall
2023-06-24 15:47   ` Simon Horman
2023-06-26 16:03   ` Shannon Nelson
2023-06-23 21:14 ` [PATCH 12/26] btrfs: zoned: " Julia Lawall
2023-06-26  6:08   ` Johannes Thumshirn
2023-06-26  7:59   ` Naohiro Aota
2023-06-29 14:30   ` David Sterba
2023-06-23 21:14 ` [PATCH 13/26] iommu/tegra: gart: " Julia Lawall
2023-06-23 21:14 ` [PATCH 14/26] RDMA/siw: " Julia Lawall
2023-06-26 11:23   ` Bernard Metzler
2023-06-23 21:14 ` [PATCH 15/26] habanalabs: " Julia Lawall
2023-06-23 21:14 ` [PATCH 16/26] drm/i915/gvt: " Julia Lawall
2023-06-26  9:26   ` [Intel-gfx] " Andi Shyti
2023-06-26 11:54     ` Dan Carpenter
2023-06-23 21:14 ` [PATCH 17/26] kcov: " Julia Lawall
2023-06-24  5:34   ` Dmitry Vyukov
2023-06-23 21:14 ` [PATCH 18/26] net: enetc: " Julia Lawall
2023-06-24 15:48   ` Simon Horman
2023-06-23 21:14 ` [PATCH 19/26] RDMA/bnxt_re: " Julia Lawall
2023-06-23 21:14 ` [PATCH 20/26] drm/vmwgfx: " Julia Lawall
2023-06-27 17:33   ` Julia Lawall
2023-06-23 21:14 ` [PATCH 21/26] x86/sgx: " Julia Lawall
2023-07-10 22:02   ` Jarkko Sakkinen
2023-06-23 21:14 ` [PATCH 22/26] net: mana: " Julia Lawall
2023-06-24 15:48   ` Simon Horman
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
2023-06-23 21:14 ` [PATCH 24/26] scsi: qla2xxx: " Julia Lawall
2023-06-23 21:14 ` [PATCH 25/26] vduse: " Julia Lawall
2023-06-23 21:14 ` [PATCH 26/26] comedi: " Julia Lawall

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