Skip to content

Commit

Permalink
fortify: Prepare to improve strnlen() and strlen() warnings
Browse files Browse the repository at this point in the history
In order to have strlen() use fortified strnlen() internally, swap their
positions in the source. Doing this as part of later changes makes
review difficult, so reoroder it here; no code changes.

Cc: Francis Laniel <laniel_francis@privacyrequired.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
  • Loading branch information
kees committed Sep 25, 2021
1 parent 072af0c commit 369cd21
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions include/linux/fortify-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ __FORTIFY_INLINE char *strcat(char *p, const char *q)
return p;
}

extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
__FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
{
size_t p_size = __builtin_object_size(p, 1);
__kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);

if (p_size <= ret && maxlen != ret)
fortify_panic(__func__);
return ret;
}

__FORTIFY_INLINE __kernel_size_t strlen(const char *p)
{
__kernel_size_t ret;
Expand All @@ -71,17 +82,6 @@ __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
return ret;
}

extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
__FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
{
size_t p_size = __builtin_object_size(p, 1);
__kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);

if (p_size <= ret && maxlen != ret)
fortify_panic(__func__);
return ret;
}

/* defined after fortified strlen to reuse it */
extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
__FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
Expand Down

0 comments on commit 369cd21

Please sign in to comment.