Petr Mladek 4cafd1
From 973c39115cb308b6b1fe64b4f342996f3eef06d0 Mon Sep 17 00:00:00 2001
Petr Mladek 4cafd1
From: Joe Perches <joe@perches.com>
Petr Mladek 4cafd1
Date: Wed, 16 Sep 2020 13:40:40 -0700
Petr Mladek 4cafd1
Subject: [PATCH] drivers core: Remove strcat uses around sysfs_emit and neaten
Petr Mladek 4cafd1
Git-commit: 973c39115cb308b6b1fe64b4f342996f3eef06d0
Petr Mladek 4cafd1
Patch-mainline: v5.10-rc1
Petr Mladek 4cafd1
References: bsc#1200598 cve-2022-20166
Petr Mladek 4cafd1
Petr Mladek 4cafd1
strcat is no longer necessary for sysfs_emit and sysfs_emit_at uses.
Petr Mladek 4cafd1
Petr Mladek 4cafd1
Convert the strcat uses to sysfs_emit calls and neaten other block
Petr Mladek 4cafd1
uses of direct returns to use an intermediate const char *.
Petr Mladek 4cafd1
Petr Mladek 4cafd1
Signed-off-by: Joe Perches <joe@perches.com>
Petr Mladek 4cafd1
Link: https://lore.kernel.org/r/5d606519698ce4c8f1203a2b35797d8254c6050a.1600285923.git.joe@perches.com
Petr Mladek 4cafd1
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Petr Mladek 4cafd1
Acked-by: Petr Mladek <pmladek@suse.com>
Petr Mladek 4cafd1
Petr Mladek 4cafd1
[ pmladek@suse.com: Removed changes where the buffer could never overflow. ]
Petr Mladek 4cafd1
---
Petr Mladek 4cafd1
 drivers/base/memory.c |   33 ++++++++++++++++-----------------
Petr Mladek 4cafd1
 1 file changed, 16 insertions(+), 17 deletions(-)
Petr Mladek 4cafd1
Petr Mladek 4cafd1
--- a/drivers/base/memory.c
Petr Mladek 4cafd1
+++ b/drivers/base/memory.c
Petr Mladek 4cafd1
@@ -379,17 +379,16 @@ static ssize_t phys_device_show(struct d
Petr Mladek 4cafd1
 }
Petr Mladek 4cafd1
 
Petr Mladek 4cafd1
 #ifdef CONFIG_MEMORY_HOTREMOVE
Petr Mladek 4cafd1
-static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
Petr Mladek 4cafd1
-		unsigned long nr_pages, int online_type,
Petr Mladek 4cafd1
-		struct zone *default_zone)
Petr Mladek 4cafd1
+static int print_allowed_zone(char *buf, int len, int nid,
Petr Mladek 4cafd1
+			      unsigned long start_pfn, unsigned long nr_pages,
Petr Mladek 4cafd1
+			      int online_type, struct zone *default_zone)
Petr Mladek 4cafd1
 {
Petr Mladek 4cafd1
 	struct zone *zone;
Petr Mladek 4cafd1
 
Petr Mladek 4cafd1
 	zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
Petr Mladek 4cafd1
-	if (zone != default_zone) {
Petr Mladek 4cafd1
-		strcat(buf, " ");
Petr Mladek 4cafd1
-		strcat(buf, zone->name);
Petr Mladek 4cafd1
-	}
Petr Mladek 4cafd1
+	if (zone == default_zone)
Petr Mladek 4cafd1
+		return 0;
Petr Mladek 4cafd1
+	return sysfs_emit_at(buf, len, " %s", zone->name);
Petr Mladek 4cafd1
 }
Petr Mladek 4cafd1
 
Petr Mladek 4cafd1
 static ssize_t valid_zones_show(struct device *dev,
Petr Mladek 4cafd1
@@ -400,6 +399,7 @@ static ssize_t valid_zones_show(struct d
Petr Mladek 4cafd1
 	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Petr Mladek 4cafd1
 	unsigned long valid_start_pfn, valid_end_pfn;
Petr Mladek 4cafd1
 	struct zone *default_zone;
Petr Mladek 4cafd1
+	int len = 0;
Petr Mladek 4cafd1
 	int nid;
Petr Mladek 4cafd1
 
Petr Mladek 4cafd1
 	/*
Petr Mladek 4cafd1
@@ -413,24 +413,23 @@ static ssize_t valid_zones_show(struct d
Petr Mladek 4cafd1
 		 */
Petr Mladek 4cafd1
 		if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
Petr Mladek 4cafd1
 					  &valid_start_pfn, &valid_end_pfn))
Petr Mladek 4cafd1
-			return sysfs_emit(buf, "none\n");
Petr Mladek 4cafd1
+			return sysfs_emit(buf, "%s\n", "none");
Petr Mladek 4cafd1
 		start_pfn = valid_start_pfn;
Petr Mladek 4cafd1
-		strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
Petr Mladek 4cafd1
+		len += sysfs_emit_at(buf, len, "%s", page_zone(pfn_to_page(start_pfn))->name);
Petr Mladek 4cafd1
 		goto out;
Petr Mladek 4cafd1
 	}
Petr Mladek 4cafd1
 
Petr Mladek 4cafd1
 	nid = mem->nid;
Petr Mladek 4cafd1
 	default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
Petr Mladek 4cafd1
-	strcat(buf, default_zone->name);
Petr Mladek 4cafd1
 
Petr Mladek 4cafd1
-	print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
Petr Mladek 4cafd1
-			default_zone);
Petr Mladek 4cafd1
-	print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
Petr Mladek 4cafd1
-			default_zone);
Petr Mladek 4cafd1
+	len += sysfs_emit_at(buf, len, "%s", default_zone->name);
Petr Mladek 4cafd1
+	len += print_allowed_zone(buf, len, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
Petr Mladek 4cafd1
+				  default_zone);
Petr Mladek 4cafd1
+	len += print_allowed_zone(buf, len, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
Petr Mladek 4cafd1
+				  default_zone);
Petr Mladek 4cafd1
 out:
Petr Mladek 4cafd1
-	strcat(buf, "\n");
Petr Mladek 4cafd1
-
Petr Mladek 4cafd1
-	return strlen(buf);
Petr Mladek 4cafd1
+	len += sysfs_emit_at(buf, len, "%s", "\n");
Petr Mladek 4cafd1
+	return len;
Petr Mladek 4cafd1
 }
Petr Mladek 4cafd1
 static DEVICE_ATTR_RO(valid_zones);
Petr Mladek 4cafd1
 #endif