[CRIU] [PATCH 6/9] string.h/pie: use builtin strncmp instead of strcmp

Dmitry Safonov dsafonov at virtuozzo.com
Wed Mar 30 08:12:27 PDT 2016


Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
 criu/include/asm-generic/string.h | 18 ++++++++----------
 criu/pie/util-vdso.c              | 12 +++---------
 2 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/criu/include/asm-generic/string.h b/criu/include/asm-generic/string.h
index 0a545e65960d..9e649c027da1 100644
--- a/criu/include/asm-generic/string.h
+++ b/criu/include/asm-generic/string.h
@@ -31,17 +31,15 @@ static always_inline int builtin_memcmp(const void *cs, const void *ct, size_t c
 }
 #endif
 
-#ifndef HAS_BUILTIN_STRCMP
-static always_inline int builtin_strcmp(const char *cs, const char *ct)
+#ifndef HAS_BUILTIN_STRNCMP
+static always_inline int builtin_strncmp(const char *cs, const char *ct, size_t count)
 {
-	unsigned char c1, c2;
-
-	while (1) {
-		c1 = *cs++;
-		c2 = *ct++;
-		if (c1 != c2)
-			return c1 < c2 ? -1 : 1;
-		if (!c1)
+	size_t i;
+
+	for (i = 0; i < count; i++) {
+		if (cs[i] != ct[i])
+			return cs[i] < ct[i] ? -1 : 1;
+		if (!cs[i])
 			break;
 	}
 	return 0;
diff --git a/criu/pie/util-vdso.c b/criu/pie/util-vdso.c
index 5d1605380135..a39d27e1efa1 100644
--- a/criu/pie/util-vdso.c
+++ b/criu/pie/util-vdso.c
@@ -239,20 +239,14 @@ int vdso_fill_symtable(uintptr_t mem, size_t size, struct vdso_symtable *t)
 				continue;
 
 			addr = (uintptr_t)dynsymbol_names + sym->st_name;
-			if (__ptr_struct_oob(addr, sizeof(t->symbols[i].name),
-						mem, size))
+			if (__ptr_struct_oob(addr, VDSO_SYMBOL_MAX, mem, size))
 				continue;
 			name = (void *)addr;
 
-			/*
-			 * XXX: Hope will not go out of mem+size.
-			 * (i.e. with broken elf or malicious pointer in header)
-			 * Otherwise, we need builtin_strncmp.
-			 */
-			if (builtin_strcmp(name, symbol))
+			if (builtin_strncmp(name, symbol, VDSO_SYMBOL_MAX))
 				continue;
 
-			builtin_memcpy(t->symbols[i].name, name, sizeof(t->symbols[i].name));
+			builtin_memcpy(t->symbols[i].name, name, VDSO_SYMBOL_MAX);
 			t->symbols[i].offset = (unsigned long)sym->st_value - load->p_vaddr;
 			break;
 		}
-- 
2.7.4



More information about the CRIU mailing list