linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups
@ 2020-03-11  8:02 Takashi Iwai
  2020-03-11  8:02 ` [PATCH v2 1/2] drivers/base/cpu: Use scnprintf() for avoiding potential buffer overflow Takashi Iwai
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Takashi Iwai @ 2020-03-11  8:02 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Rafael J . Wysocki, linux-kernel

Hi,

this is a respin of my previous patch [*].
Now the scnprintf() conversion is done only in the needed places,
and the second patch cleans up the superfluous s*nprintf() usages.

Takashi

[*] https://lore.kernel.org/r/20200311071200.4024-1-tiwai@suse.de

===
Takashi Iwai (2):
  drivers/base/cpu: Use scnprintf() for avoiding potential buffer
    overflow
  drivers/base/cpu: Simplify s*nprintf() usages

 drivers/base/cpu.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

-- 
2.16.4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] drivers/base/cpu: Use scnprintf() for avoiding potential buffer overflow
  2020-03-11  8:02 [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups Takashi Iwai
@ 2020-03-11  8:02 ` Takashi Iwai
  2020-03-11  8:02 ` [PATCH v2 2/2] drivers/base/cpu: Simplify s*nprintf() usages Takashi Iwai
  2020-03-11  8:06 ` [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2020-03-11  8:02 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Rafael J . Wysocki, linux-kernel

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 <tiwai@suse.de>
---
 drivers/base/cpu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c
index 6265871a4af2..67aaa052c7a2 100644
--- a/drivers/base/cpu.c
+++ b/drivers/base/cpu.c
@@ -258,13 +258,13 @@ static ssize_t print_cpus_offline(struct device *dev,
 			buf[n++] = ',';
 
 		if (nr_cpu_ids == total_cpus-1)
-			n += snprintf(&buf[n], len - n, "%u", nr_cpu_ids);
+			n += scnprintf(&buf[n], len - n, "%u", nr_cpu_ids);
 		else
-			n += snprintf(&buf[n], len - n, "%u-%d",
+			n += scnprintf(&buf[n], len - n, "%u-%d",
 						      nr_cpu_ids, total_cpus-1);
 	}
 
-	n += snprintf(&buf[n], len - n, "\n");
+	n += scnprintf(&buf[n], len - n, "\n");
 	return n;
 }
 static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
-- 
2.16.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] drivers/base/cpu: Simplify s*nprintf() usages
  2020-03-11  8:02 [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups Takashi Iwai
  2020-03-11  8:02 ` [PATCH v2 1/2] drivers/base/cpu: Use scnprintf() for avoiding potential buffer overflow Takashi Iwai
@ 2020-03-11  8:02 ` Takashi Iwai
  2020-03-11  8:06 ` [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2020-03-11  8:02 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Rafael J . Wysocki, linux-kernel

Use the simpler sprintf() instead of snprintf() or scnprintf() in a
single-shot sysfs output callbacks where you are very sure that it
won't go over PAGE_SIZE buffer limit.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 drivers/base/cpu.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c
index 67aaa052c7a2..df19c002d747 100644
--- a/drivers/base/cpu.c
+++ b/drivers/base/cpu.c
@@ -231,8 +231,7 @@ static struct cpu_attr cpu_attrs[] = {
 static ssize_t print_cpus_kernel_max(struct device *dev,
 				     struct device_attribute *attr, char *buf)
 {
-	int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
-	return n;
+	return sprintf(buf, "%d\n", NR_CPUS - 1);
 }
 static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
 
@@ -272,7 +271,7 @@ static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
 static ssize_t print_cpus_isolated(struct device *dev,
 				  struct device_attribute *attr, char *buf)
 {
-	int n = 0, len = PAGE_SIZE-2;
+	int n;
 	cpumask_var_t isolated;
 
 	if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
@@ -280,7 +279,7 @@ static ssize_t print_cpus_isolated(struct device *dev,
 
 	cpumask_andnot(isolated, cpu_possible_mask,
 		       housekeeping_cpumask(HK_FLAG_DOMAIN));
-	n = scnprintf(buf, len, "%*pbl\n", cpumask_pr_args(isolated));
+	n = sprintf(buf, "%*pbl\n", cpumask_pr_args(isolated));
 
 	free_cpumask_var(isolated);
 
@@ -292,11 +291,7 @@ static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
 static ssize_t print_cpus_nohz_full(struct device *dev,
 				  struct device_attribute *attr, char *buf)
 {
-	int n = 0, len = PAGE_SIZE-2;
-
-	n = scnprintf(buf, len, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
-
-	return n;
+	return sprintf(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
 }
 static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);
 #endif
-- 
2.16.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups
  2020-03-11  8:02 [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups Takashi Iwai
  2020-03-11  8:02 ` [PATCH v2 1/2] drivers/base/cpu: Use scnprintf() for avoiding potential buffer overflow Takashi Iwai
  2020-03-11  8:02 ` [PATCH v2 2/2] drivers/base/cpu: Simplify s*nprintf() usages Takashi Iwai
@ 2020-03-11  8:06 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2020-03-11  8:06 UTC (permalink / raw)
  To: Takashi Iwai; +Cc: Rafael J . Wysocki, linux-kernel

On Wed, Mar 11, 2020 at 09:02:05AM +0100, Takashi Iwai wrote:
> Hi,
> 
> this is a respin of my previous patch [*].
> Now the scnprintf() conversion is done only in the needed places,
> and the second patch cleans up the superfluous s*nprintf() usages.
> 
> Takashi
> 
> [*] https://lore.kernel.org/r/20200311071200.4024-1-tiwai@suse.de

Thanks for these, much nicer, I'll go queue them up now.

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-03-11  8:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-11  8:02 [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups Takashi Iwai
2020-03-11  8:02 ` [PATCH v2 1/2] drivers/base/cpu: Use scnprintf() for avoiding potential buffer overflow Takashi Iwai
2020-03-11  8:02 ` [PATCH v2 2/2] drivers/base/cpu: Simplify s*nprintf() usages Takashi Iwai
2020-03-11  8:06 ` [PATCH v2 0/2] drivers/base/cpu: s*nprintf() usage fixes / cleanups Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).