Patches contributed by University of Minnesota


commit f5e284bb74ab296f98122673c7ecd22028b2c200
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Wed Dec 1 11:37:03 2021 +0800

    drm/komeda: Fix an undefined behavior bug in komeda_plane_add()
    
    In komeda_plane_add(), komeda_get_layer_fourcc_list() is assigned to
    formats and used in drm_universal_plane_init().
    drm_universal_plane_init() passes formats to
    __drm_universal_plane_init(). __drm_universal_plane_init() further
    passes formats to memcpy() as src parameter, which could lead to an
    undefined behavior bug on failure of komeda_get_layer_fourcc_list().
    
    Fix this bug by adding a check of formats.
    
    This bug was found by a static analyzer. The analysis employs
    differential checking to identify inconsistent security operations
    (e.g., checks or kfrees) between two code paths and confirms that the
    inconsistent operations are not recovered in the current function or
    the callers, so they constitute bugs.
    
    Note that, as a bug found by static analysis, it can be a false
    positive or hard to trigger. Multiple researchers have cross-reviewed
    the bug.
    
    Builds with CONFIG_DRM_KOMEDA=m show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: 61f1c4a8ab75 ("drm/komeda: Attach komeda_dev to DRM-KMS")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
    Link: https://lore.kernel.org/dri-devel/20211201033704.32054-1-zhou1615@umn.edu

diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_plane.c b/drivers/gpu/drm/arm/display/komeda/komeda_plane.c
index 541949f2d44a..9a8197a23c45 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_plane.c
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_plane.c
@@ -256,6 +256,10 @@ static int komeda_plane_add(struct komeda_kms_dev *kms,
 
 	formats = komeda_get_layer_fourcc_list(&mdev->fmt_tbl,
 					       layer->layer_type, &n_formats);
+	if (!formats) {
+		kfree(kplane);
+		return -ENOMEM;
+	}
 
 	err = drm_universal_plane_init(&kms->base, plane,
 			get_possible_crtcs(kms, c->pipeline),

commit abd77889851d2ead0d0c9c4d29f1808801477b00
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Mon Jan 24 18:20:01 2022 +0100

    media: ti-vpe: cal: Fix a NULL pointer dereference in cal_ctx_v4l2_init_formats()
    
    In cal_ctx_v4l2_init_formats(), devm_kzalloc() is assigned to
    ctx->active_fmt and there is a dereference of it after that, which could
    lead to NULL pointer dereference on failure of devm_kzalloc().
    
    Fix this bug by adding a NULL check of ctx->active_fmt.
    
    This bug was found by a static analyzer.
    
    Builds with 'make allyesconfig' show no new warnings, and our static
    analyzer no longer warns about this code.
    
    Fixes: 7168155002cf ("media: ti-vpe: cal: Move format handling to cal.c and expose helpers")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Reviewed-by: Pratyush Yadav <p.yadav@ti.com>
    Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
    Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>

diff --git a/drivers/media/platform/ti-vpe/cal-video.c b/drivers/media/platform/ti-vpe/cal-video.c
index 7799da1cc261..3e936a2ca36c 100644
--- a/drivers/media/platform/ti-vpe/cal-video.c
+++ b/drivers/media/platform/ti-vpe/cal-video.c
@@ -823,6 +823,9 @@ static int cal_ctx_v4l2_init_formats(struct cal_ctx *ctx)
 	/* Enumerate sub device formats and enable all matching local formats */
 	ctx->active_fmt = devm_kcalloc(ctx->cal->dev, cal_num_formats,
 				       sizeof(*ctx->active_fmt), GFP_KERNEL);
+	if (!ctx->active_fmt)
+		return -ENOMEM;
+
 	ctx->num_active_fmt = 0;
 
 	for (j = 0, i = 0; ; ++j) {

commit ab3824427b848da10e9fe2727f035bbeecae6ff4
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Wed Dec 1 01:22:53 2021 +0800

    spi: spi-zynq-qspi: Fix a NULL pointer dereference in zynq_qspi_exec_mem_op()
    
    In zynq_qspi_exec_mem_op(), kzalloc() is directly used in memset(),
    which could lead to a NULL pointer dereference on failure of
    kzalloc().
    
    Fix this bug by adding a check of tmpbuf.
    
    This bug was found by a static analyzer. The analysis employs
    differential checking to identify inconsistent security operations
    (e.g., checks or kfrees) between two code paths and confirms that the
    inconsistent operations are not recovered in the current function or
    the callers, so they constitute bugs.
    
    Note that, as a bug found by static analysis, it can be a false
    positive or hard to trigger. Multiple researchers have cross-reviewed
    the bug.
    
    Builds with CONFIG_SPI_ZYNQ_QSPI=m show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: 67dca5e580f1 ("spi: spi-mem: Add support for Zynq QSPI controller")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Link: https://lore.kernel.org/r/20211130172253.203700-1-zhou1615@umn.edu
    Signed-off-by: Mark Brown <broonie@kernel.org>

diff --git a/drivers/spi/spi-zynq-qspi.c b/drivers/spi/spi-zynq-qspi.c
index cfa222c9bd5e..78f31b61a2aa 100644
--- a/drivers/spi/spi-zynq-qspi.c
+++ b/drivers/spi/spi-zynq-qspi.c
@@ -570,6 +570,9 @@ static int zynq_qspi_exec_mem_op(struct spi_mem *mem,
 
 	if (op->dummy.nbytes) {
 		tmpbuf = kzalloc(op->dummy.nbytes, GFP_KERNEL);
+		if (!tmpbuf)
+			return -ENOMEM;
+
 		memset(tmpbuf, 0xff, op->dummy.nbytes);
 		reinit_completion(&xqspi->data_completion);
 		xqspi->txbuf = tmpbuf;

commit 9b6d90e2085ca2ce72ef9ea78658bf270855e62e
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Tue Jan 25 00:45:25 2022 +0800

    ata: pata_platform: Fix a NULL pointer dereference in __pata_platform_probe()
    
    In __pata_platform_probe(), devm_kzalloc() is assigned to ap->ops and
    there is a dereference of it right after that, which could introduce a
    NULL pointer dereference bug.
    
    Fix this by adding a NULL check of ap->ops.
    
    This bug was found by a static analyzer.
    
    Builds with 'make allyesconfig' show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: f3d5e4f18dba ("ata: pata_of_platform: Allow to use 16-bit wide data transfer")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Signed-off-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>
    Reviewed-by: Sergey Shtylyov <s.shtylyov@omp.ru>

diff --git a/drivers/ata/pata_platform.c b/drivers/ata/pata_platform.c
index 028329428b75..87c7c90676ca 100644
--- a/drivers/ata/pata_platform.c
+++ b/drivers/ata/pata_platform.c
@@ -128,6 +128,8 @@ int __pata_platform_probe(struct device *dev, struct resource *io_res,
 	ap = host->ports[0];
 
 	ap->ops = devm_kzalloc(dev, sizeof(*ap->ops), GFP_KERNEL);
+	if (!ap->ops)
+		return -ENOMEM;
 	ap->ops->inherits = &ata_sff_port_ops;
 	ap->ops->cable_detect = ata_cable_unknown;
 	ap->ops->set_mode = pata_platform_set_mode;

commit 588a70177df3b1777484267584ef38ab2ca899a2
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Tue Jan 25 00:57:29 2022 +0800

    drm/amd/display: Fix a NULL pointer dereference in amdgpu_dm_connector_add_common_modes()
    
    In amdgpu_dm_connector_add_common_modes(), amdgpu_dm_create_common_mode()
    is assigned to mode and is passed to drm_mode_probed_add() directly after
    that. drm_mode_probed_add() passes &mode->head to list_add_tail(), and
    there is a dereference of it in list_add_tail() without recoveries, which
    could lead to NULL pointer dereference on failure of
    amdgpu_dm_create_common_mode().
    
    Fix this by adding a NULL check of mode.
    
    This bug was found by a static analyzer.
    
    Builds with 'make allyesconfig' show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: e7b07ceef2a6 ("drm/amd/display: Merge amdgpu_dm_types and amdgpu_dm")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
index 44f4d862a022..e6ff40fdf18b 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
@@ -8138,6 +8138,9 @@ static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
 		mode = amdgpu_dm_create_common_mode(encoder,
 				common_modes[i].name, common_modes[i].w,
 				common_modes[i].h);
+		if (!mode)
+			continue;
+
 		drm_mode_probed_add(connector, mode);
 		amdgpu_dm_connector->num_modes++;
 	}

commit 153a9529d7f372ce7ceb5eae7e2c312c0cd64d41
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Tue Jan 25 00:55:51 2022 +0800

    drm/amd/display/dc/calcs/dce_calcs: Fix a memleak in calculate_bandwidth()
    
    In calculate_bandwidth(), the tag free_sclk and free_yclk are reversed,
    which could lead to a memory leak of yclk.
    
    Fix this bug by changing the location of free_sclk and free_yclk.
    
    This bug was found by a static analyzer.
    
    Builds with 'make allyesconfig' show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: 2be8989d0fc2 ("drm/amd/display/dc/calcs/dce_calcs: Move some large variables from the stack to the heap")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

diff --git a/drivers/gpu/drm/amd/display/dc/dml/calcs/dce_calcs.c b/drivers/gpu/drm/amd/display/dc/dml/calcs/dce_calcs.c
index 8f136db39f3e..0100a6053ab6 100644
--- a/drivers/gpu/drm/amd/display/dc/dml/calcs/dce_calcs.c
+++ b/drivers/gpu/drm/amd/display/dc/dml/calcs/dce_calcs.c
@@ -2033,10 +2033,10 @@ static void calculate_bandwidth(
 	kfree(surface_type);
 free_tiling_mode:
 	kfree(tiling_mode);
-free_yclk:
-	kfree(yclk);
 free_sclk:
 	kfree(sclk);
+free_yclk:
+	kfree(yclk);
 }
 
 /*******************************************************************************

commit ebc77bcc6e1660a011483c035d53c461c8dcc4f5
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Tue Jan 25 00:55:51 2022 +0800

    drm/amd/display/dc/calcs/dce_calcs: Fix a memleak in calculate_bandwidth()
    
    In calculate_bandwidth(), the tag free_sclk and free_yclk are reversed,
    which could lead to a memory leak of yclk.
    
    Fix this bug by changing the location of free_sclk and free_yclk.
    
    This bug was found by a static analyzer.
    
    Builds with 'make allyesconfig' show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: 2be8989d0fc2 ("drm/amd/display/dc/calcs/dce_calcs: Move some large variables from the stack to the heap")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

diff --git a/drivers/gpu/drm/amd/display/dc/calcs/dce_calcs.c b/drivers/gpu/drm/amd/display/dc/calcs/dce_calcs.c
index ff5bb152ef49..e6ef36de0825 100644
--- a/drivers/gpu/drm/amd/display/dc/calcs/dce_calcs.c
+++ b/drivers/gpu/drm/amd/display/dc/calcs/dce_calcs.c
@@ -2033,10 +2033,10 @@ static void calculate_bandwidth(
 	kfree(surface_type);
 free_tiling_mode:
 	kfree(tiling_mode);
-free_yclk:
-	kfree(yclk);
 free_sclk:
 	kfree(sclk);
+free_yclk:
+	kfree(yclk);
 }
 
 /*******************************************************************************

commit 2343bcdb4747d4f418a4daf2e898b94f86c24a59
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Tue Jan 25 00:58:55 2022 +0800

    drm/nouveau/acr: Fix undefined behavior in nvkm_acr_hsfw_load_bl()
    
    In nvkm_acr_hsfw_load_bl(), the return value of kmalloc() is directly
    passed to memcpy(), which could lead to undefined behavior on failure
    of kmalloc().
    
    Fix this bug by using kmemdup() instead of kmalloc()+memcpy().
    
    This bug was found by a static analyzer.
    
    Builds with 'make allyesconfig' show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: 22dcda45a3d1 ("drm/nouveau/acr: implement new subdev to replace "secure boot"")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Reviewed-by: Lyude Paul <lyude@redhat.com>
    Signed-off-by: Lyude Paul <lyude@redhat.com>
    Link: https://patchwork.freedesktop.org/patch/msgid/20220124165856.57022-1-zhou1615@umn.edu

diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/acr/hsfw.c b/drivers/gpu/drm/nouveau/nvkm/subdev/acr/hsfw.c
index 667fa016496e..a6ea89a5d51a 100644
--- a/drivers/gpu/drm/nouveau/nvkm/subdev/acr/hsfw.c
+++ b/drivers/gpu/drm/nouveau/nvkm/subdev/acr/hsfw.c
@@ -142,11 +142,12 @@ nvkm_acr_hsfw_load_bl(struct nvkm_acr *acr, const char *name, int ver,
 
 	hsfw->imem_size = desc->code_size;
 	hsfw->imem_tag = desc->start_tag;
-	hsfw->imem = kmalloc(desc->code_size, GFP_KERNEL);
-	memcpy(hsfw->imem, data + desc->code_off, desc->code_size);
-
+	hsfw->imem = kmemdup(data + desc->code_off, desc->code_size, GFP_KERNEL);
 	nvkm_firmware_put(fw);
-	return 0;
+	if (!hsfw->imem)
+		return -ENOMEM;
+	else
+		return 0;
 }
 
 int

commit 977d2e7c63c3d04d07ba340b39987742e3241554
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Wed Dec 1 02:11:40 2021 +0800

    pcmcia: rsrc_nonstatic: Fix a NULL pointer dereference in nonstatic_find_mem_region()
    
    In nonstatic_find_mem_region(), pcmcia_make_resource() is assigned to
    res and used in pci_bus_alloc_resource(). There a dereference of res
    in pci_bus_alloc_resource(), which could lead to a NULL pointer
    dereference on failure of pcmcia_make_resource().
    
    Fix this bug by adding a check of res.
    
    This bug was found by a static analyzer. The analysis employs
    differential checking to identify inconsistent security operations
    (e.g., checks or kfrees) between two code paths and confirms that the
    inconsistent operations are not recovered in the current function or
    the callers, so they constitute bugs.
    
    Note that, as a bug found by static analysis, it can be a false
    positive or hard to trigger. Multiple researchers have cross-reviewed
    the bug.
    
    Builds with CONFIG_PCCARD_NONSTATIC=y show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: 49b1153adfe1 ("pcmcia: move all pcmcia_resource_ops providers into one module")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>

diff --git a/drivers/pcmcia/rsrc_nonstatic.c b/drivers/pcmcia/rsrc_nonstatic.c
index 827ca6e9ee54..1cac52870711 100644
--- a/drivers/pcmcia/rsrc_nonstatic.c
+++ b/drivers/pcmcia/rsrc_nonstatic.c
@@ -812,6 +812,9 @@ static struct resource *nonstatic_find_mem_region(u_long base, u_long num,
 	unsigned long min, max;
 	int ret, i, j;
 
+	if (!res)
+		return NULL;
+
 	low = low || !(s->features & SS_CAP_PAGE_REGS);
 
 	data.mask = align - 1;

commit ca0fe0d7c35c97528bdf621fdca75f13157c27af
Author: Zhou Qingyang <zhou1615@umn.edu>
Date:   Wed Dec 1 00:59:23 2021 +0800

    pcmcia: rsrc_nonstatic: Fix a NULL pointer dereference in __nonstatic_find_io_region()
    
    In __nonstatic_find_io_region(), pcmcia_make_resource() is assigned to
    res and used in pci_bus_alloc_resource(). There is a dereference of res
    in pci_bus_alloc_resource(), which could lead to a NULL pointer
    dereference on failure of pcmcia_make_resource().
    
    Fix this bug by adding a check of res.
    
    This bug was found by a static analyzer. The analysis employs
    differential checking to identify inconsistent security operations
    (e.g., checks or kfrees) between two code paths and confirms that the
    inconsistent operations are not recovered in the current function or
    the callers, so they constitute bugs.
    
    Note that, as a bug found by static analysis, it can be a false
    positive or hard to trigger. Multiple researchers have cross-reviewed
    the bug.
    
    Builds with CONFIG_PCCARD_NONSTATIC=y show no new warnings,
    and our static analyzer no longer warns about this code.
    
    Fixes: 49b1153adfe1 ("pcmcia: move all pcmcia_resource_ops providers into one module")
    Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
    [linux@dominikbrodowski.net: Fix typo in commit message]
    Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>

diff --git a/drivers/pcmcia/rsrc_nonstatic.c b/drivers/pcmcia/rsrc_nonstatic.c
index bb15a8bdbaab..827ca6e9ee54 100644
--- a/drivers/pcmcia/rsrc_nonstatic.c
+++ b/drivers/pcmcia/rsrc_nonstatic.c
@@ -690,6 +690,9 @@ static struct resource *__nonstatic_find_io_region(struct pcmcia_socket *s,
 	unsigned long min = base;
 	int ret;
 
+	if (!res)
+		return NULL;
+
 	data.mask = align - 1;
 	data.offset = base & data.mask;
 	data.map = &s_data->io_db;