PM / devfreq: Fix buffer overflow in trans_stat_show
Subsystems (2)
PATCH RESEND 1/2 PM / devfreq: Fix buffer overflow in trans_stat_show
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.
--- Documentation/ABI/testing/sysfs-class-devfreq | 3 + drivers/devfreq/devfreq.c | 57 +++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-)
Documentation/ABI/testing/sysfs-class-devfreq +3 -0
@@ -52,6 +52,9 @@ Description: 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 Date: October 2012 Contact: Nishanth Menon <[email protected]>
drivers/devfreq/devfreq.c +39 -18
84 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH RESEND 2/2 PM / devfreq: Convert to use sysfs_emit_at() API
Follow the advice of the Documentation/filesystems/sysfs.rst and show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space.
--- drivers/devfreq/devfreq.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-)
drivers/devfreq/devfreq.c +17 -20
@@ -1697,7 +1697,7 @@ static ssize_t trans_stat_show(struct device *dev, max_state = df->max_state;if (max_state ==0 ) -return scnprintf (buf,PAGE_SIZE ,"Not Supported.\n" ); +return sysfs_emit (buf,"Not Supported.\n" );mutex_lock (&df->lock); if (!df->stop_polling &&
@@ -1707,47 +1707,44 @@ static ssize_t trans_stat_show(struct device *dev, }mutex_unlock (&df->lock); - len +=scnprintf (buf + len,PAGE_SIZE - len," From : To\n" ); - len +=scnprintf (buf + len,PAGE_SIZE - len," :" ); + len +=sysfs_emit_at (buf, len," From : To\n" ); + len +=sysfs_emit_at (buf, len," :" );for (i =0 ; i < max_state; i++) {if (len >=PAGE_SIZE -1 )break ; - len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu" , - df->freq_table[i]); + len +=sysfs_emit_at (buf, len,"%10lu" , + df->freq_table[i]); } +if (len >=PAGE_SIZE -1 )return PAGE_SIZE -1 ; - - len +=scnprintf (buf + len,PAGE_SIZE - len," time(ms)\n" ); + len +=sysfs_emit_at (buf, len," time(ms)\n" );for (i =0 ; i < max_state; i++) {if (len >=PAGE_SIZE -1 )break ; -if (df->freq_table[i] == df->previous_freq) - len +=scnprintf (buf + len,PAGE_SIZE - len,"*" ); +if (df->freq_table[2 ] == df->previous_freq) + len +=sysfs_emit_at (buf, len,"*" );else - len +=scnprintf (buf + len,PAGE_SIZE - len," " ); + len +=sysfs_emit_at (buf, len," " );if (len >=PAGE_SIZE -1 )break ; - - len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu:" , - df->freq_table[i]); + len +=sysfs_emit_at (buf, len,"%10lu:" , df->freq_table[i]);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]); + len +=sysfs_emit_at (buf, len,"%10u" , + df->stats.trans_table[(i * max_state) + j]); }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])); + len +=sysfs_emit_at (buf, len,"%10llu\n" , (u64 ) +jiffies64_to_msecs (df->stats.time_in_state[i])); }if (len <PAGE_SIZE -1 ) - len +=scnprintf (buf + len,PAGE_SIZE - len,"Total transition : %u\n" , - df->stats.total_trans); - + len +=sysfs_emit_at (buf, len,"Total transition : %u\n" , + df->stats.total_trans);if (len >=PAGE_SIZE -1 ) {pr_warn_once ("devfreq transition table exceeds PAGE_SIZE. Disabling\n" );return -EFBIG ;
Signature · 1 line
2.43.0
Le 04/01/2024 à 22:55, Christian Marangi a écrit :
Quoted 51 lines
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_SIZEand exit early from loop. Also add at the end a warning that we exceededPAGE_SIZEand that stats is disabled.Return
-EFBIGin 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
-EFBIGerror.Fixes e552bbaf5b98 ("PM / devfreq: Add sysfs node for representing frequency transition information.")Signed-off-by Christian Marangi <[email protected]>--- Documentation/ABI/testing/sysfs-class-devfreq | 3 + drivers/devfreq/devfreq.c | 57 +++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-)Documentation/ABI/testing/sysfs-class-devfreq +3
-0@@ -52,6 +52,9 @@ Description: 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 Date: October 2012 Contact: Nishanth Menon <[email protected]>drivers/devfreq/devfreq.c +1
-1@@ -1688,7 +1688,7 @@ static ssize_t trans_stat_show(struct device *dev, struct device_attribute *attr, char *buf) { struct devfreq *df = to_devfreq(dev); -ssize_t len; +ssize_t len =0 ;int i, j;unsigned int max_state;
drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1700
Quoted 5 lines
drivers/devfreq/devfreq.c +1
-1@@ -1697,7 +1697,7 @@ static ssize_t trans_stat_show(struct device *dev, max_state = df->max_state;if (max_state ==0 ) -return sprintf (buf,"Not Supported.\n" ); +return scnprintf (buf,PAGE_SIZE ,"Not Supported.\n" );
Hi,
maybe using sysfs_emit_at() could be even cleaner and less verbose?
Quoted 19 lines
drivers/devfreq/devfreq.c +7
-5mutex_lock (&df->lock); if (!df->stop_polling &&@@ -1707,31 +1707,52 @@ static ssize_t trans_stat_show(struct device *dev, }mutex_unlock (&df->lock); - len =sprintf (buf," From : To\n" ); - len +=sprintf (buf + len," :" ); -for (i =0 ; i < max_state; i++) - len +=sprintf (buf + len,"%10lu" , - df->freq_table[i]); + len +=scnprintf (buf + len,PAGE_SIZE - len," From : To\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len," :" ); +for (i =0 ; i < max_state; i++) { +if (len >=PAGE_SIZE -1 ) +break ; + len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu" , + df->freq_table[i]);
drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1725
Quoted 10 lines
drivers/devfreq/devfreq.c +6
-1+ } +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++) { +if (len >=PAGE_SIZE -1 ) +break ;
I'm not sure that adding all these tests is needed. It could save some cycles in the worse case (when buf could overflow), but in fact wastes cycles in the normel case.
CJ
Quoted 43 lines
drivers/devfreq/devfreq.c +24
-10if (df->freq_table[i] == df->previous_freq) - len +=sprintf (buf + len,"*" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"*" );else - len +=sprintf (buf + len," " ); + len +=scnprintf (buf + len,PAGE_SIZE - len," " ); +if (len >=PAGE_SIZE -1 ) +break ; + + len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu:" , + df->freq_table[i]); +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]); + } +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])); + } - len +=sprintf (buf + len,"%10lu:" , df->freq_table[i]); -for (j =0 ; j < max_state; j++) - len +=sprintf (buf + len,"%10u" , - df->stats.trans_table[(i * max_state) + j]); +if (len <PAGE_SIZE -1 ) + len +=scnprintf (buf + len,PAGE_SIZE - len,"Total transition : %u\n" , + df->stats.total_trans); - len +=sprintf (buf + len,"%10llu\n" , (u64 ) -jiffies64_to_msecs (df->stats.time_in_state[i])); +if (len >=PAGE_SIZE -1 ) { +pr_warn_once ("devfreq transition table exceeds PAGE_SIZE. Disabling\n" ); +return -EFBIG ; } - len +=sprintf (buf + len,"Total transition : %u\n" , - df->stats.total_trans);return len; }
Quoted 56 lines · Christophe JAILLET
Le 04/01/2024 à 22:55, Christian Marangi a écrit :
Quoted 48 lines
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_SIZEand exit early from loop. Also add at the end a warning that we exceededPAGE_SIZEand that stats is disabled.Return
-EFBIGin 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
-EFBIGerror.Fixes e552bbaf5b98 ("PM / devfreq: Add sysfs node for representing frequency transition information.")Signed-off-by Christian Marangi <[email protected]>--- Documentation/ABI/testing/sysfs-class-devfreq | 3 + drivers/devfreq/devfreq.c | 57 +++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-)Documentation/ABI/testing/sysfs-class-devfreq +3
-0@@ -52,6 +52,9 @@ Description: 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 Date: October 2012 Contact: Nishanth Menon <[email protected]>drivers/devfreq/devfreq.c +1
-1@@ -1688,7 +1688,7 @@ static ssize_t trans_stat_show(struct device *dev, struct device_attribute *attr, char *buf) { struct devfreq *df = to_devfreq(dev); -ssize_t len; +ssize_t len =0 ;int i, j;unsigned int max_state;drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1699
Quoted 4 lines
drivers/devfreq/devfreq.c +1
-1@@ -1697,7 +1697,7 @@ static ssize_t trans_stat_show(struct device *dev, max_state = df->max_state;if (max_state ==0 ) -return sprintf (buf,"Not Supported.\n" ); +return scnprintf (buf,PAGE_SIZE ,"Not Supported.\n" );Hi,
Quoted 2 lines
maybe using
sysfs_emit_at()could be even cleaner and less verbose?
If you notice this change is done in the second patch of the series. This patch still use this more generic way to permit this to be backported on stable kernel. (older kernel doesn't have sysfs_emit_at() hence it can't be backported)
Quoted 21 lines
Quoted 21 lines
drivers/devfreq/devfreq.c +10
-6mutex_lock (&df->lock); if (!df->stop_polling &&@@ -1707,31 +1707,52 @@ static ssize_t trans_stat_show(struct device *dev, }mutex_unlock (&df->lock); - len =sprintf (buf," From : To\n" ); - len +=sprintf (buf + len," :" ); -for (i =0 ; i < max_state; i++) - len +=sprintf (buf + len,"%10lu" , - df->freq_table[i]); + len +=scnprintf (buf + len,PAGE_SIZE - len," From : To\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len," :" ); +for (i =0 ; i < max_state; i++) { +if (len >=PAGE_SIZE -1 ) +break ; + len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu" , + df->freq_table[i]); + } +if (len >=PAGE_SIZE -1 ) +return PAGE_SIZE -1 ; - len +=sprintf (buf + len," time(ms)\n" );
Quoted 9 lines
drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1725
Quoted 4 lines
drivers/devfreq/devfreq.c +3
-0+ len +=scnprintf (buf + len,PAGE_SIZE - len," time(ms)\n" );for (i =0 ; i < max_state; i++) { +if (len >=PAGE_SIZE -1 ) +break ;I'm not sure that adding all these tests is needed. It could save some cycles in the worse case (when buf could overflow), but in fact wastes cycles in the normel case.
Consider that cpufreq stats does the same exact checks and I feel the 2 thing should be equal (given they do the same exact task)
Also with case of -EBIG, I would expact the thing to be very big and exiting early might be beneficial, for normal stats I would expact only a few cycle added. Myabe we can reduce them just for the for cycle?
Quoted 40 lines
Quoted 39 lines
drivers/devfreq/devfreq.c +24
-10if (df->freq_table[i] == df->previous_freq) - len +=sprintf (buf + len,"*" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"*" );else - len +=sprintf (buf + len," " ); + len +=scnprintf (buf + len,PAGE_SIZE - len," " ); +if (len >=PAGE_SIZE -1 ) +break ; + + len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu:" , + df->freq_table[i]); +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]); + } +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])); + } - len +=sprintf (buf + len,"%10lu:" , df->freq_table[i]); -for (j =0 ; j < max_state; j++) - len +=sprintf (buf + len,"%10u" , - df->stats.trans_table[(i * max_state) + j]); +if (len <PAGE_SIZE -1 ) + len +=scnprintf (buf + len,PAGE_SIZE - len,"Total transition : %u\n" , + df->stats.total_trans); - len +=sprintf (buf + len,"%10llu\n" , (u64 ) -jiffies64_to_msecs (df->stats.time_in_state[i])); +if (len >=PAGE_SIZE -1 ) { +pr_warn_once ("devfreq transition table exceeds PAGE_SIZE. Disabling\n" ); +return -EFBIG ; } - len +=sprintf (buf + len,"Total transition : %u\n" , - df->stats.total_trans);return len; }
Signature · 1 line
Ansuel
Greg KH
maintainerQuoted 17 lines · Christian Marangi
Quoted 16 lines · Christophe JAILLET
drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1699
Quoted 13 lines
drivers/devfreq/devfreq.c +2
-2@@ -1688,7 +1688,7 @@ static ssize_t trans_stat_show(struct device *dev, struct device_attribute *attr, char *buf) { struct devfreq *df = to_devfreq(dev); -ssize_t len; +ssize_t len =0 ;int i, j;unsigned int max_state;@@ -1697,7 +1697,7 @@ static ssize_t trans_stat_show(struct device *dev, max_state = df->max_state;if (max_state ==0 ) -return sprintf (buf,"Not Supported.\n" ); +return scnprintf (buf,PAGE_SIZE ,"Not Supported.\n" );Hi,
Quoted 7 lines
Quoted 2 lines
maybe using
sysfs_emit_at()could be even cleaner and less verbose?If you notice this change is done in the second patch of the series. This patch still use this more generic way to permit this to be backported on stable kernel. (older kernel doesn't have
sysfs_emit_at()hence it can't be backported)
All activly supported kernels on the kernel.org front page have sysfs_emit_at(), so this should not be an issue for anyone. Just do the change here at the same time.
thanks,
greg k-h
Le 04/01/2024 à 23:44, Christian Marangi a écrit :
Quoted 57 lines · Christophe JAILLET
Quoted 56 lines
Le 04/01/2024 à 22:55, Christian Marangi a écrit :
Quoted 48 lines
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_SIZEand exit early from loop. Also add at the end a warning that we exceededPAGE_SIZEand that stats is disabled.Return
-EFBIGin 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
-EFBIGerror.Fixes e552bbaf5b98 ("PM / devfreq: Add sysfs node for representing frequency transition information.")Signed-off-by Christian Marangi <[email protected]>--- Documentation/ABI/testing/sysfs-class-devfreq | 3 + drivers/devfreq/devfreq.c | 57 +++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-)Documentation/ABI/testing/sysfs-class-devfreq +3
-0@@ -52,6 +52,9 @@ Description: 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 Date: October 2012 Contact: Nishanth Menon <[email protected]>drivers/devfreq/devfreq.c +1
-1@@ -1688,7 +1688,7 @@ static ssize_t trans_stat_show(struct device *dev, struct device_attribute *attr, char *buf) { struct devfreq *df = to_devfreq(dev); -ssize_t len; +ssize_t len =0 ;int i, j;unsigned int max_state;drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1699
Quoted 4 lines
drivers/devfreq/devfreq.c +1
-1@@ -1697,7 +1697,7 @@ static ssize_t trans_stat_show(struct device *dev, max_state = df->max_state;if (max_state ==0 ) -return sprintf (buf,"Not Supported.\n" ); +return scnprintf (buf,PAGE_SIZE ,"Not Supported.\n" );Hi,
Quoted 7 lines
Quoted 2 lines
maybe using
sysfs_emit_at()could be even cleaner and less verbose?If you notice this change is done in the second patch of the series. This patch still use this more generic way to permit this to be backported on stable kernel. (older kernel doesn't have
sysfs_emit_at()hence it can't be backported)
Ok. Thanks for the clarification.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/devfreq/devfreq.c +2
-5mutex_lock (&df->lock); if (!df->stop_polling &&@@ -1707,31 +1707,52 @@ static ssize_t trans_stat_show(struct device *dev, }mutex_unlock (&df->lock); - len =sprintf (buf," From : To\n" ); - len +=sprintf (buf + len," :" ); -for (i =0 ; i < max_state; i++) - len +=sprintf (buf + len,"%10lu" , - df->freq_table[i]); + len +=scnprintf (buf + len,PAGE_SIZE - len," From : To\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len," :" );
Quoted 8 lines
Quoted 8 lines
drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1719
Quoted 8 lines
drivers/devfreq/devfreq.c +8
-0+for (i =0 ; i < max_state; i++) { +if (len >=PAGE_SIZE -1 ) +break ; + len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu" , + df->freq_table[i]); + } +if (len >=PAGE_SIZE -1 ) +return PAGE_SIZE -1 ;
[1]
Quoted 11 lines
Quoted 10 lines
drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1725
Quoted 5 lines
drivers/devfreq/devfreq.c +3
-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++) { +if (len >=PAGE_SIZE -1 ) +break ;I'm not sure that adding all these tests is needed. It could save some cycles in the worse case (when buf could overflow), but in fact wastes cycles in the normel case.
Quoted 2 lines
Consider that cpufreq stats does the same exact checks and I feel the 2 thing should be equal (given they do the same exact task)
Make sense.
But I think that show_trans_table(() could also save some tests.
I agree with you that limiting these tests to at most 1 per loop should be already a first step.
Also surprising to me, is that if the output is too big, at [1] (above) we silently truncate it, and at [2] (below) we return an error.
CJ
Quoted 31 lines
Also with case of
-EBIG, I would expact the thing to be very big and exiting early might be beneficial, for normal stats I would expact only a few cycle added. Myabe we can reduce them just for the for cycle?Quoted 26 lines
Quoted 26 lines
drivers/devfreq/devfreq.c +18
-6if (df->freq_table[i] == df->previous_freq) - len +=sprintf (buf + len,"*" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"*" );else - len +=sprintf (buf + len," " ); + len +=scnprintf (buf + len,PAGE_SIZE - len," " ); +if (len >=PAGE_SIZE -1 ) +break ; + + len +=scnprintf (buf + len,PAGE_SIZE - len,"%10lu:" , + df->freq_table[i]); +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]); + } +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])); + } - len +=sprintf (buf + len,"%10lu:" , df->freq_table[i]); -for (j =0 ; j < max_state; j++) - len +=sprintf (buf + len,"%10u" , - df->stats.trans_table[(i * max_state) + j]);
Quoted 8 lines
Quoted 8 lines
drivers/devfreq/devfreq.c static ssize_t trans_stat_show(struct device *dev, · line 1753
Quoted 8 lines
drivers/devfreq/devfreq.c +6
-2+if (len <PAGE_SIZE -1 ) + len +=scnprintf (buf + len,PAGE_SIZE - len,"Total transition : %u\n" , + df->stats.total_trans); - len +=sprintf (buf + len,"%10llu\n" , (u64 ) -jiffies64_to_msecs (df->stats.time_in_state[i])); +if (len >=PAGE_SIZE -1 ) { +pr_warn_once ("devfreq transition table exceeds PAGE_SIZE. Disabling\n" ); +return -EFBIG ;
[2]
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
drivers/devfreq/devfreq.c +0
-2} - len +=sprintf (buf + len,"Total transition : %u\n" , - df->stats.total_trans);return len; }