cpufreq: Use scnprintf() for avoiding potential buffer overflow
PATCH 1/1 cpufreq: Use scnprintf() for avoiding potential buffer overflow
Since snprintf() returns the would-be-output size instead of the actual output size, the succeeding calls may go beyond the given buffer limit. Fix it by replacing with scnprintf().
Signed-off-by Takashi Iwai <[email protected]>
--- drivers/cpufreq/cpufreq_stats.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/cpufreq/cpufreq_stats.c +7 -7
@@ -90,35 +90,35 @@ static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)if (policy->fast_switch_enabled)return 0 ; - len +=snprintf (buf + len,PAGE_SIZE - len," From : To\n" ); - len +=snprintf (buf + len,PAGE_SIZE - len," : " ); + len +=scnprintf (buf + len,PAGE_SIZE - len," From : To\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len," : " );for (i =0 ; i < stats->state_num; i++) {if (len >=PAGE_SIZE )break ; - len +=snprintf (buf + len,PAGE_SIZE - len,"%9u " , + len +=scnprintf (buf + len,PAGE_SIZE - len,"%9u " , stats->freq_table[i]); }if (len >=PAGE_SIZE )return PAGE_SIZE ; - len +=snprintf (buf + len,PAGE_SIZE - len,"\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"\n" );for (i =0 ; i < stats->state_num; i++) {if (len >=PAGE_SIZE )break ; - len +=snprintf (buf + len,PAGE_SIZE - len,"%9u: " , + len +=scnprintf (buf + len,PAGE_SIZE - len,"%9u: " , stats->freq_table[i]);for (j =0 ; j < stats->state_num; j++) {if (len >=PAGE_SIZE )break ; - len +=snprintf (buf + len,PAGE_SIZE - len,"%9u " , + len +=scnprintf (buf + len,PAGE_SIZE - len,"%9u " , stats->trans_table[i*stats->max_state+j]); }if (len >=PAGE_SIZE )break ; - len +=snprintf (buf + len,PAGE_SIZE - len,"\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"\n" ); }if (len >=PAGE_SIZE ) {
Signature · 1 line
2.16.4
Quoted 46 lines · Takashi Iwai
Since
snprintf()returns the would-be-output size instead of the actual output size, the succeeding calls may go beyond the given buffer limit. Fix it by replacing withscnprintf().Signed-off-by Takashi Iwai <[email protected]>--- drivers/cpufreq/cpufreq_stats.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)drivers/cpufreq/cpufreq_stats.c +5
-6@@ -90,35 +90,35 @@ static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf)if (policy->fast_switch_enabled)return 0 ; - len +=snprintf (buf + len,PAGE_SIZE - len," From : To\n" ); - len +=snprintf (buf + len,PAGE_SIZE - len," : " ); + len +=scnprintf (buf + len,PAGE_SIZE - len," From : To\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len," : " );for (i =0 ; i < stats->state_num; i++) {if (len >=PAGE_SIZE )break ; - len +=snprintf (buf + len,PAGE_SIZE - len,"%9u " , + len +=scnprintf (buf + len,PAGE_SIZE - len,"%9u " , stats->freq_table[i]); }if (len >=PAGE_SIZE )return PAGE_SIZE ; - len +=snprintf (buf + len,PAGE_SIZE - len,"\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"\n" );for (i =0 ; i < stats->state_num; i++) {if (len >=PAGE_SIZE )break ; - len +=snprintf (buf + len,PAGE_SIZE - len,"%9u: " , + len +=scnprintf (buf + len,PAGE_SIZE - len,"%9u: " , stats->freq_table[i]);for (j =0 ; j < stats->state_num; j++) {if (len >=PAGE_SIZE )break ; - len += snprintf(buf + len, PAGE_SIZE - len, "%9u ",
drivers/cpufreq/cpufreq_stats.c static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf) · line 124
Quoted 10 lines
drivers/cpufreq/cpufreq_stats.c +2
-1+ len +=scnprintf (buf + len,PAGE_SIZE - len,"%9u " , stats->trans_table[i*stats->max_state+j]); }if (len >=PAGE_SIZE )break ; - len +=snprintf (buf + len,PAGE_SIZE - len,"\n" ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"\n" ); }if (len >=PAGE_SIZE ) {
Acked-by Viresh Kumar <[email protected]>
Signature · 1 line
viresh