Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | From 3328e98bcbf2930cd7eea3e6c92ad5dcbdf4794f Mon Sep 17 00:00:00 2001 |
| 2 | From: Yuanjie Huang <yuanjie.huang@windriver.com> |
| 3 | Date: Wed, 24 Aug 2016 03:15:43 +0000 |
| 4 | Subject: [PATCH] Fix potential signed overflow in pointer arithmatic |
| 5 | |
| 6 | Pointer arithmatic results in implementation defined signed integer |
| 7 | type, so that 's - src' in strlcpy and others may trigger signed overflow. |
| 8 | In case of compilation by gcc or clang with -ftrapv option, the overflow |
| 9 | would lead to program abort. |
| 10 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 11 | Upstream-Status: Submitted [http://bugzilla.mindrot.org/show_bug.cgi?id=2608] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 12 | |
| 13 | Signed-off-by: Yuanjie Huang <yuanjie.huang@windriver.com> |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 14 | |
| 15 | Complete the fix |
| 16 | Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com> |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 17 | --- |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 18 | openbsd-compat/strlcat.c | 10 +++++++--- |
| 19 | openbsd-compat/strlcpy.c | 8 ++++++-- |
| 20 | openbsd-compat/strnlen.c | 8 ++++++-- |
| 21 | 3 files changed, 19 insertions(+), 7 deletions(-) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 22 | |
| 23 | diff --git a/openbsd-compat/strlcat.c b/openbsd-compat/strlcat.c |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 24 | index bcc1b61..124e1e3 100644 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 25 | --- a/openbsd-compat/strlcat.c |
| 26 | +++ b/openbsd-compat/strlcat.c |
| 27 | @@ -23,6 +23,7 @@ |
| 28 | |
| 29 | #include <sys/types.h> |
| 30 | #include <string.h> |
| 31 | +#include <stdint.h> |
| 32 | |
| 33 | /* |
| 34 | * Appends src to string dst of size siz (unlike strncat, siz is the |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 35 | @@ -42,7 +43,7 @@ strlcat(char *dst, const char *src, size_t siz) |
| 36 | /* Find the end of dst and adjust bytes left but don't go past end */ |
| 37 | while (n-- != 0 && *d != '\0') |
| 38 | d++; |
| 39 | - dlen = d - dst; |
| 40 | + dlen = (uintptr_t)d - (uintptr_t)dst; |
| 41 | n = siz - dlen; |
| 42 | |
| 43 | if (n == 0) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 44 | @@ -55,8 +56,11 @@ strlcat(char *dst, const char *src, size_t siz) |
| 45 | s++; |
| 46 | } |
| 47 | *d = '\0'; |
| 48 | - |
| 49 | - return(dlen + (s - src)); /* count does not include NUL */ |
| 50 | + /* |
| 51 | + * Cast pointers to unsigned type before calculation, to avoid signed |
| 52 | + * overflow when the string ends where the MSB has changed. |
| 53 | + */ |
| 54 | + return (dlen + ((uintptr_t)s - (uintptr_t)src)); /* count does not include NUL */ |
| 55 | } |
| 56 | |
| 57 | #endif /* !HAVE_STRLCAT */ |
| 58 | diff --git a/openbsd-compat/strlcpy.c b/openbsd-compat/strlcpy.c |
| 59 | index b4b1b60..b06f374 100644 |
| 60 | --- a/openbsd-compat/strlcpy.c |
| 61 | +++ b/openbsd-compat/strlcpy.c |
| 62 | @@ -23,6 +23,7 @@ |
| 63 | |
| 64 | #include <sys/types.h> |
| 65 | #include <string.h> |
| 66 | +#include <stdint.h> |
| 67 | |
| 68 | /* |
| 69 | * Copy src to string dst of size siz. At most siz-1 characters |
| 70 | @@ -51,8 +52,11 @@ strlcpy(char *dst, const char *src, size_t siz) |
| 71 | while (*s++) |
| 72 | ; |
| 73 | } |
| 74 | - |
| 75 | - return(s - src - 1); /* count does not include NUL */ |
| 76 | + /* |
| 77 | + * Cast pointers to unsigned type before calculation, to avoid signed |
| 78 | + * overflow when the string ends where the MSB has changed. |
| 79 | + */ |
| 80 | + return ((uintptr_t)s - (uintptr_t)src - 1); /* count does not include NUL */ |
| 81 | } |
| 82 | |
| 83 | #endif /* !HAVE_STRLCPY */ |
| 84 | diff --git a/openbsd-compat/strnlen.c b/openbsd-compat/strnlen.c |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 85 | index 7ad3573..7040f1f 100644 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 86 | --- a/openbsd-compat/strnlen.c |
| 87 | +++ b/openbsd-compat/strnlen.c |
| 88 | @@ -23,6 +23,7 @@ |
| 89 | #include <sys/types.h> |
| 90 | |
| 91 | #include <string.h> |
| 92 | +#include <stdint.h> |
| 93 | |
| 94 | size_t |
| 95 | strnlen(const char *str, size_t maxlen) |
| 96 | @@ -31,7 +32,10 @@ strnlen(const char *str, size_t maxlen) |
| 97 | |
| 98 | for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) |
| 99 | ; |
| 100 | - |
| 101 | - return (size_t)(cp - str); |
| 102 | + /* |
| 103 | + * Cast pointers to unsigned type before calculation, to avoid signed |
| 104 | + * overflow when the string ends where the MSB has changed. |
| 105 | + */ |
| 106 | + return (size_t)((uintptr_t)cp - (uintptr_t)str); |
| 107 | } |
| 108 | #endif |
| 109 | -- |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 110 | 2.17.1 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | |