PM / devfreq: Fix buffer overflow in trans_stat_show
commit 08e23d05fa6dc4fc13da0ccf09defdd4bbc92ff4 upstream. Fix buffer overflow in trans_stat_show(). Convert simple snprintf to the more secure scnprintf with size of PAGE_SIZE. Add condition checking if we are exceeding PAGE_SIZE and exit early from loop. Also add at the end a warning that we exceeded PAGE_SIZE and that stats is disabled. Return -EFBIG in the case where we don't have enough space to write the full transition table. Also document in the ABI that this function can return -EFBIG error. Link: https://lore.kernel.org/all/20231024183016.14648-2-ansuelsmth@gmail.com/ Cc: stable@vger.kernel.org Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218041 Fixes: e552bbaf5b98 ("PM / devfreq: Add sysfs node for representing frequency transition information.") Signed-off-by: Christian Marangi <ansuelsmth@gmail.com> Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
f4518de40a
commit
8a7729cda2
@ -52,6 +52,9 @@ Description:
|
|||||||
|
|
||||||
echo 0 > /sys/class/devfreq/.../trans_stat
|
echo 0 > /sys/class/devfreq/.../trans_stat
|
||||||
|
|
||||||
|
If the transition table is bigger than PAGE_SIZE, reading
|
||||||
|
this will return an -EFBIG error.
|
||||||
|
|
||||||
What: /sys/class/devfreq/.../available_frequencies
|
What: /sys/class/devfreq/.../available_frequencies
|
||||||
Date: October 2012
|
Date: October 2012
|
||||||
Contact: Nishanth Menon <nm@ti.com>
|
Contact: Nishanth Menon <nm@ti.com>
|
||||||
|
@ -1687,7 +1687,7 @@ static ssize_t trans_stat_show(struct device *dev,
|
|||||||
struct device_attribute *attr, char *buf)
|
struct device_attribute *attr, char *buf)
|
||||||
{
|
{
|
||||||
struct devfreq *df = to_devfreq(dev);
|
struct devfreq *df = to_devfreq(dev);
|
||||||
ssize_t len;
|
ssize_t len = 0;
|
||||||
int i, j;
|
int i, j;
|
||||||
unsigned int max_state;
|
unsigned int max_state;
|
||||||
|
|
||||||
@ -1696,7 +1696,7 @@ static ssize_t trans_stat_show(struct device *dev,
|
|||||||
max_state = df->max_state;
|
max_state = df->max_state;
|
||||||
|
|
||||||
if (max_state == 0)
|
if (max_state == 0)
|
||||||
return sprintf(buf, "Not Supported.\n");
|
return scnprintf(buf, PAGE_SIZE, "Not Supported.\n");
|
||||||
|
|
||||||
mutex_lock(&df->lock);
|
mutex_lock(&df->lock);
|
||||||
if (!df->stop_polling &&
|
if (!df->stop_polling &&
|
||||||
@ -1706,31 +1706,52 @@ static ssize_t trans_stat_show(struct device *dev,
|
|||||||
}
|
}
|
||||||
mutex_unlock(&df->lock);
|
mutex_unlock(&df->lock);
|
||||||
|
|
||||||
len = sprintf(buf, " From : To\n");
|
len += scnprintf(buf + len, PAGE_SIZE - len, " From : To\n");
|
||||||
len += sprintf(buf + len, " :");
|
len += scnprintf(buf + len, PAGE_SIZE - len, " :");
|
||||||
for (i = 0; i < max_state; i++)
|
for (i = 0; i < max_state; i++) {
|
||||||
len += sprintf(buf + len, "%10lu",
|
if (len >= PAGE_SIZE - 1)
|
||||||
|
break;
|
||||||
|
len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu",
|
||||||
df->freq_table[i]);
|
df->freq_table[i]);
|
||||||
|
}
|
||||||
|
if (len >= PAGE_SIZE - 1)
|
||||||
|
return PAGE_SIZE - 1;
|
||||||
|
|
||||||
len += sprintf(buf + len, " time(ms)\n");
|
len += scnprintf(buf + len, PAGE_SIZE - len, " time(ms)\n");
|
||||||
|
|
||||||
for (i = 0; i < max_state; i++) {
|
for (i = 0; i < max_state; i++) {
|
||||||
|
if (len >= PAGE_SIZE - 1)
|
||||||
|
break;
|
||||||
if (df->freq_table[i] == df->previous_freq)
|
if (df->freq_table[i] == df->previous_freq)
|
||||||
len += sprintf(buf + len, "*");
|
len += scnprintf(buf + len, PAGE_SIZE - len, "*");
|
||||||
else
|
else
|
||||||
len += sprintf(buf + len, " ");
|
len += scnprintf(buf + len, PAGE_SIZE - len, " ");
|
||||||
|
if (len >= PAGE_SIZE - 1)
|
||||||
|
break;
|
||||||
|
|
||||||
len += sprintf(buf + len, "%10lu:", df->freq_table[i]);
|
len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu:",
|
||||||
for (j = 0; j < max_state; j++)
|
df->freq_table[i]);
|
||||||
len += sprintf(buf + len, "%10u",
|
for (j = 0; j < max_state; j++) {
|
||||||
|
if (len >= PAGE_SIZE - 1)
|
||||||
|
break;
|
||||||
|
len += scnprintf(buf + len, PAGE_SIZE - len, "%10u",
|
||||||
df->stats.trans_table[(i * max_state) + j]);
|
df->stats.trans_table[(i * max_state) + j]);
|
||||||
|
}
|
||||||
len += sprintf(buf + len, "%10llu\n", (u64)
|
if (len >= PAGE_SIZE - 1)
|
||||||
|
break;
|
||||||
|
len += scnprintf(buf + len, PAGE_SIZE - len, "%10llu\n", (u64)
|
||||||
jiffies64_to_msecs(df->stats.time_in_state[i]));
|
jiffies64_to_msecs(df->stats.time_in_state[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
len += sprintf(buf + len, "Total transition : %u\n",
|
if (len < PAGE_SIZE - 1)
|
||||||
|
len += scnprintf(buf + len, PAGE_SIZE - len, "Total transition : %u\n",
|
||||||
df->stats.total_trans);
|
df->stats.total_trans);
|
||||||
|
|
||||||
|
if (len >= PAGE_SIZE - 1) {
|
||||||
|
pr_warn_once("devfreq transition table exceeds PAGE_SIZE. Disabling\n");
|
||||||
|
return -EFBIG;
|
||||||
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user