Subject: [PATCH] s390/extmem: Replace sprintf with snprintf for buffer safety

From: Josephine Pfeiffer <[email-masked]>
Date: Wed, 1 Oct 2025 21:14:04 +0200

Replace unsafe sprintf() calls with snprintf() in segment_save() to
prevent potential buffer overflows. The function builds command strings
by repeatedly appending to a fixed-size buffer, which could overflow if
segment ranges are numerous or values are large.

Signed-off-by: Josephine Pfeiffer <[email-masked]>
---
 arch/s390/mm/extmem.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/arch/s390/mm/extmem.c b/arch/s390/mm/extmem.c
index f7da53e212f5..b6464a322eb1 100644
--- a/arch/s390/mm/extmem.c
+++ b/arch/s390/mm/extmem.c
@@ -598,14 +598,16 @@ segment_save(char *name)
 		goto out;
 	}
 
-	sprintf(cmd1, "DEFSEG %s", name);
+	snprintf(cmd1, sizeof(cmd1), "DEFSEG %s", name);
 	for (i=0; i<seg->segcnt; i++) {
-		sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
-			seg->range[i].start >> PAGE_SHIFT,
-			seg->range[i].end >> PAGE_SHIFT,
-			segtype_string[seg->range[i].start & 0xff]);
+		size_t len = strlen(cmd1);
+
+		snprintf(cmd1 + len, sizeof(cmd1) - len, " %lX-%lX %s",
+			 seg->range[i].start >> PAGE_SHIFT,
+			 seg->range[i].end >> PAGE_SHIFT,
+			 segtype_string[seg->range[i].start & 0xff]);
 	}
-	sprintf(cmd2, "SAVESEG %s", name);
+	snprintf(cmd2, sizeof(cmd2), "SAVESEG %s", name);
 	response = 0;
 	cpcmd(cmd1, NULL, 0, &response);
 	if (response) {
-- 
2.51.0

---

From: Josephine Pfeiffer <[email-masked]>
Date: Wed, 1 Oct 2025 19:40:55 +0200

Signed-off-by: Josephine Pfeiffer <[email-masked]>
---
 arch/s390/mm/extmem.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/s390/mm/extmem.c b/arch/s390/mm/extmem.c
index 0bc8746b6192..b6464a322eb1 100644
--- a/arch/s390/mm/extmem.c
+++ b/arch/s390/mm/extmem.c
@@ -601,6 +601,7 @@ segment_save(char *name)
 	snprintf(cmd1, sizeof(cmd1), "DEFSEG %s", name);
 	for (i=0; i<seg->segcnt; i++) {
 		size_t len = strlen(cmd1);
+
 		snprintf(cmd1 + len, sizeof(cmd1) - len, " %lX-%lX %s",
 			 seg->range[i].start >> PAGE_SHIFT,
 			 seg->range[i].end >> PAGE_SHIFT,
-- 
2.51.0