hwmon: (coretemp) Fix out-of-bounds memory access
[platform/kernel/linux-starfive.git] / drivers / hwmon / coretemp.c
index eba94f6..e78c769 100644 (file)
@@ -42,7 +42,7 @@ MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
 #define PKG_SYSFS_ATTR_NO      1       /* Sysfs attribute for package temp */
 #define BASE_SYSFS_ATTR_NO     2       /* Sysfs Base attr no for coretemp */
 #define NUM_REAL_CORES         128     /* Number of Real cores per cpu */
-#define CORETEMP_NAME_LENGTH   19      /* String Length of attrs */
+#define CORETEMP_NAME_LENGTH   28      /* String Length of attrs */
 #define MAX_CORE_ATTRS         4       /* Maximum no of basic attrs */
 #define TOTAL_ATTRS            (MAX_CORE_ATTRS + 1)
 #define MAX_CORE_DATA          (NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)
@@ -509,18 +509,14 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
        if (pkg_flag) {
                attr_no = PKG_SYSFS_ATTR_NO;
        } else {
-               index = ida_alloc(&pdata->ida, GFP_KERNEL);
+               index = ida_alloc_max(&pdata->ida, NUM_REAL_CORES - 1, GFP_KERNEL);
                if (index < 0)
                        return index;
+
                pdata->cpu_map[index] = topology_core_id(cpu);
                attr_no = index + BASE_SYSFS_ATTR_NO;
        }
 
-       if (attr_no > MAX_CORE_DATA - 1) {
-               err = -ERANGE;
-               goto ida_free;
-       }
-
        tdata = init_temp_data(cpu, pkg_flag);
        if (!tdata) {
                err = -ENOMEM;