Skip to contentSkip to conversation

Fix potential issue on memmove

PATCH Superseded [email protected] 1 participant 1 message Aug 12, 2010 x86

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Fix potential issue on memmove

From: root <[email protected]>

memmove allow source and destination address to be overlap, but no limitation for memcpy. So memmove use forward or backward copy mode to handle src > dest and dest > src cases respectively. However memcpy has not address overlap, it may use any copy mode theoretically. Our original memmove will call memcpy and assume it must use forward copy mode, otherwise the system will crash, it is potential issue. The patch avoid assumption, meanwhile re-active patch id a1e5278e40f16a4611264f8da9e557c16cb6f6ed, which will use forward/backward copy mode according to offset address.

---
In this version we try to keep and optimize original memmove.

 arch/x86/lib/memcpy_32.c  |   38 ++++++++++++++++++++++++++----------
 arch/x86/lib/memmove_64.c |   46 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 68 insertions(+), 16 deletions(-)
arch/x86/lib/memcpy_32.c +27 -11
index 5415a9d..be424df 100644
@@ -25,19 +25,35 @@ void *memmove(void *dest, const void *src, size_t n)
 	int d0, d1, d2;
 
 	if (dest < src) {
-		memcpy(dest, src, n);
+		if ((dest + n) < src)
+			 return memcpy(dest, src, n);
+		else
+			__asm__ __volatile__(
+				"rep\n\t"
+				"movsb\n\t"
+				: "=&c" (d0), "=&S" (d1), "=&D" (d2)
+				:"0" (n),
+				 "1" (src),
+				 "2" (dest)
+				:"memory");
+
 	} else {
-		__asm__ __volatile__(
-			"std\n\t"
-			"rep\n\t"
-			"movsb\n\t"
-			"cld"
-			: "=&c" (d0), "=&S" (d1), "=&D" (d2)
-			:"0" (n),
-			 "1" (n-1+src),
-			 "2" (n-1+dest)
-			:"memory");
+
+		if((src + count) < dest)
+			return memcpy(dest, src, count);
+		else
+			__asm__ __volatile__(
+				"std\n\t"
+				"rep\n\t"
+				"movsb\n\t"
+				"cld"
+				: "=&c" (d0), "=&S" (d1), "=&D" (d2)
+				:"0" (n),
+				 "1" (n-1+src),
+				 "2" (n-1+dest)
+				:"memory");
 	}
+
 	return dest;
 }
 EXPORT_SYMBOL(memmove);
arch/x86/lib/memmove_64.c +41 -5

54 lines not on this page · raw message

Signature · 1 line
1.6.5.2