Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe()

devm_kcalloc() may fail. ndtest_probe() allocates three DMA address
arrays (dcr_dma, label_dma, dimm_dma) and later unconditionally uses
them in ndtest_nvdimm_init(), which can lead to a NULL pointer
dereference under low-memory conditions.

Check all three allocations and return -ENOMEM if any allocation fails,
jumping to the common error path. Do not emit an extra error message
since the allocator already warns on allocation failure.

Fixes: 9399ab61ad82 ("ndtest: Add dimms to the two buses")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
Reviewed-by: Alison Schofield <alison.schofield@intel.com>
Reviewed-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Signed-off-by: Ira Weiny <ira.weiny@intel.com>

authored by

Guangshuo Li and committed by
Ira Weiny
a9e6aa99 88506435

+12 -1
+12 -1
tools/testing/nvdimm/test/ndtest.c
··· 850 850 851 851 p->dcr_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR, 852 852 sizeof(dma_addr_t), GFP_KERNEL); 853 + if (!p->dcr_dma) { 854 + rc = -ENOMEM; 855 + goto err; 856 + } 853 857 p->label_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR, 854 858 sizeof(dma_addr_t), GFP_KERNEL); 859 + if (!p->label_dma) { 860 + rc = -ENOMEM; 861 + goto err; 862 + } 855 863 p->dimm_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR, 856 864 sizeof(dma_addr_t), GFP_KERNEL); 857 - 865 + if (!p->dimm_dma) { 866 + rc = -ENOMEM; 867 + goto err; 868 + } 858 869 rc = ndtest_nvdimm_init(p); 859 870 if (rc) 860 871 goto err;