]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
x86/boot: Add volatile, clobbers and zero-length test in memcmp()
authorMauricio Faria de Oliveira <mfo@igalia.com>
Thu, 23 Jul 2026 23:08:04 +0000 (20:08 -0300)
committerBorislav Petkov (AMD) <bp@alien8.de>
Tue, 28 Jul 2026 23:32:44 +0000 (16:32 -0700)
Add the volatile qualifier and clobbers parameter to prevent bugs with
instruction reordering and optimization.

Also add TEST for the zero-length case to set ZF, as, if the count register
is zero, the REPE prefix does not run the CMPSB instruction, leaving the ZF
flag undetermined.

  [ bp: Add a comment about the len==0 case. ]

Fixes: 62bd0337d0c4 ("Top header file for new x86 setup code")
Closes: https://sashiko.dev/#/patchset/20260701-pvh-kasan-inline-v6-0-ba99045dfa9f%40igalia.com
Suggested-by: Borislav Petkov <bp@alien8.de>
Signed-off-by: Mauricio Faria de Oliveira <mfo@igalia.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://lore.kernel.org/all/20260721-pvh-kasan-inline-v7-2-38979a50cef0@igalia.com
arch/x86/boot/string.c

index ac0f900ebc47efa81c92e1bb2010ea41677899c4..1632d40e1f545ae0665597069b568ea6b6c263e5 100644 (file)
 int memcmp(const void *s1, const void *s2, size_t len)
 {
        bool diff;
-       asm("repe cmpsb"
-           : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len));
+
+       /*
+        * Make sure ZF is properly set in the len==0 case because in it,
+        * RCX==0 and the REPE; CMPSB won't get executed.
+        */
+       asm volatile("test %3, %3\n\t"
+                    "repe cmpsb"
+                    : "=@ccnz" (diff), "+D" (s1), "+S" (s2), "+c" (len)
+                    : : "cc", "memory");
        return diff;
 }