lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Sun, 22 Oct 2023 18:35:47 +0700
From: James Tirta Halim <tirtajames45@...il.com>
To: linux-hardening@...r.kernel.org,
	bpf@...r.kernel.org
Cc: James Tirta Halim <tirtajames45@...il.com>
Subject: [PATCH] strstarts: avoid calling strlen() if first char does not match

---
 include/linux/string.h  | 9 ++++++---
 tools/bpf/bpftool/gen.c | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index dbfc66400050..1c51039604e7 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -214,7 +214,7 @@ int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
  */
 static inline bool strstarts(const char *str, const char *prefix)
 {
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return (*str == *prefix) ? strncmp(str, prefix, strlen(prefix)) == 0 : (*prefix == '\0');
 }
 
 size_t memweight(const void *ptr, size_t bytes);
@@ -356,8 +356,11 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  */
 static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
 {
-	size_t len = strlen(prefix);
-	return strncmp(str, prefix, len) == 0 ? len : 0;
+	if (*str == *prefix) {
+		size_t len = strlen(prefix);
+		return strncmp(str, prefix, len) == 0 ? len : 0;
+	}
+	return *prefix == '\0';
 }
 
 #endif /* _LINUX_STRING_H_ */
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index 2883660d6b67..5f8db7e517bc 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -36,7 +36,7 @@ static void sanitize_identifier(char *name)
 
 static bool str_has_prefix(const char *str, const char *prefix)
 {
-	return strncmp(str, prefix, strlen(prefix)) == 0;
+	return (*str == *prefix) ? strncmp(str, prefix, strlen(prefix)) == 0 : (*prefix == '\0');
 }
 
 static bool str_has_suffix(const char *str, const char *suffix)
-- 
2.42.0


Powered by blists - more mailing lists