cxl/region: prevent underflow in ways_to_cxl()
authorDan Carpenter <dan.carpenter@oracle.com>
Mon, 1 Aug 2022 10:20:12 +0000 (13:20 +0300)
committerDan Williams <dan.j.williams@intel.com>
Mon, 1 Aug 2022 19:12:33 +0000 (12:12 -0700)
The "ways" variable comes from the user.  The ways_to_cxl() function
has an upper bound but it doesn't check for negatives.  Make
the "ways" variable an unsigned int to fix this bug.

Fixes: 80d10a6cee05 ("cxl/region: Add interleave geometry attributes")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Link: https://lore.kernel.org/r/Yueo3NV2hFCXx1iV@kili
[djbw: fixup interleave_ways_store() to only accept unsigned input]
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
drivers/cxl/core/region.c
drivers/cxl/cxl.h

index c80932b..516ba7e 100644 (file)
@@ -319,10 +319,11 @@ static ssize_t interleave_ways_store(struct device *dev,
        struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
        struct cxl_region *cxlr = to_cxl_region(dev);
        struct cxl_region_params *p = &cxlr->params;
-       int rc, val, save;
+       unsigned int val, save;
+       int rc;
        u8 iw;
 
-       rc = kstrtoint(buf, 0, &val);
+       rc = kstrtouint(buf, 0, &val);
        if (rc)
                return rc;
 
index 7567440..969953c 100644 (file)
@@ -102,7 +102,7 @@ static inline int granularity_to_cxl(int g, u16 *ig)
        return 0;
 }
 
-static inline int ways_to_cxl(int ways, u8 *iw)
+static inline int ways_to_cxl(unsigned int ways, u8 *iw)
 {
        if (ways > 16)
                return -EINVAL;