Enable --enable-fortify-source with clang

clang generates internal calls for some _chk symbol, so add internal
aliases for them, and stub some with rtld-stubbed-symbols to avoid
ld.so linker issues.
This commit is contained in:
Adhemerval Zanella 2024-02-08 10:56:30 -03:00
parent c8f302070f
commit 628d34afdc
20 changed files with 93 additions and 12 deletions

View File

@ -35,3 +35,4 @@ __vasprintf_chk (char **result_ptr, int flag, const char *format, va_list ap)
return __vasprintf_internal (result_ptr, format, ap, mode);
}
libc_hidden_def (__vasprintf_chk)

View File

@ -29,3 +29,4 @@ ___vfprintf_chk (FILE *fp, int flag, const char *format, va_list ap)
return __vfprintf_internal (fp, format, ap, mode);
}
ldbl_strong_alias (___vfprintf_chk, __vfprintf_chk)
ldbl_hidden_def (___vfprintf_chk, __vfprintf_chk)

View File

@ -1468,6 +1468,14 @@ rtld-stubbed-symbols = \
realloc \
# rtld-stubbed-symbols
# These symbols might be emitted by the compiler when fortify is enabled
# (through builtins).
rtld-stubbed-symbols += \
__GI___vfprintf_chk \
__GI___vsprintf_chk \
__GI___vsyslog_chk \
# rtld-stubbed-symbols
ifeq ($(have-ssp),yes)
# rtld is not built with the stack protector, so these references will
# go away in the rebuilds.

View File

@ -14,12 +14,10 @@ libc_hidden_proto (__inet_aton_exact)
extern __typeof (inet_ntop) __inet_ntop;
libc_hidden_proto (__inet_ntop)
libc_hidden_proto (__inet_ntop_chk)
libc_hidden_proto (inet_pton)
extern __typeof (inet_pton) __inet_pton;
libc_hidden_proto (__inet_pton)
libc_hidden_proto (__inet_pton_chk)
extern __typeof (inet_makeaddr) __inet_makeaddr;
libc_hidden_proto (__inet_makeaddr)

View File

@ -1 +1,6 @@
#include <inet/bits/inet-fortified-decl.h>
#ifndef _ISOMAC
libc_hidden_proto (__inet_ntop_chk)
libc_hidden_proto (__inet_pton_chk)
#endif

View File

@ -1 +1,8 @@
#include <libio/bits/stdio2-decl.h>
#ifndef _ISOMAC
libc_hidden_proto (__fgets_unlocked_chk)
libc_hidden_ldbl_proto (vfprintf)
libc_hidden_ldbl_proto (__vasprintf_chk)
libc_hidden_ldbl_proto (__vfprintf_chk)
#endif

View File

@ -1 +1,16 @@
#ifndef _ISOMAC
# if IS_IN(libc) && defined SHARED
/* Redirect calls from __builtin_stpcpy_chk to internal __stpcpy when building
with fortify enable. */
__asm__ ("stpcpy = __GI___stpcpy");
# endif
/* Add the internal aliass attribute to symbol before they first usage on the
fortify wrappers. */
libc_hidden_builtin_proto (memcpy)
libc_hidden_builtin_proto (mempcpy)
libc_hidden_builtin_proto (memmove)
libc_hidden_builtin_proto (memset)
#endif
#include <string/bits/string_fortified.h>

View File

@ -1 +1,5 @@
#include <misc/bits/syslog-decl.h>
#ifndef _ISOMAC
libc_hidden_ldbl_proto (__vsyslog_chk)
#endif

View File

@ -1 +1,7 @@
#include <posix/bits/unistd-decl.h>
#ifndef _ISOMAC
libc_hidden_proto (__read_chk)
libc_hidden_proto (__getdomainname_chk)
libc_hidden_proto (__getlogin_r_chk)
#endif

View File

@ -1 +1,6 @@
#include <wcsmbs/bits/wchar2-decl.h>
#ifndef _ISOMAC
libc_hidden_builtin_proto (__wmemset_chk)
libc_hidden_proto (__wcrtomb_chk)
#endif

View File

@ -23,8 +23,12 @@
redirecting ldouble to _Float128 variants. We can therefore safely
directly alias them to their internal name. */
# if __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1 && IS_IN (libc)
# define stdio_hidden_ldbl_proto(p, f) \
extern __typeof (p ## f) p ## f __asm (__ASMNAME ("___ieee128_" #f));
# ifdef SHARED
# define stdio_hidden_ldbl_proto(p, f) __LDBL_REDIR2_DECL (f)
# else
# define stdio_hidden_ldbl_proto(p, f) \
extern __typeof (p ## f) p ## f __asm (#p __ASMNAME (#f "ieee128"));
# endif
# elif __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
# define stdio_hidden_ldbl_proto(p,f) __LDBL_REDIR1_DECL (p ## f, p ## f ## ieee128)
# else
@ -70,10 +74,12 @@ extern int __printf_chk (int, const char *, ...);
extern int __fprintf_chk (FILE *, int, const char *, ...);
extern int __vprintf_chk (int, const char *, __gnuc_va_list);
extern int __vfprintf_chk (FILE *, int, const char *, __gnuc_va_list);
stdio_hidden_ldbl_proto (__, vfprintf_chk)
extern char *__fgets_unlocked_chk (char *buf, size_t size, int n, FILE *fp);
extern char *__fgets_chk (char *buf, size_t size, int n, FILE *fp);
extern int __asprintf_chk (char **, int, const char *, ...) __THROW;
extern int __vasprintf_chk (char **, int, const char *, __gnuc_va_list) __THROW;
stdio_hidden_ldbl_proto (__, vasprintf_chk)
extern int __dprintf_chk (int, int, const char *, ...);
extern int __vdprintf_chk (int, int, const char *, __gnuc_va_list);
extern int __obstack_printf_chk (struct obstack *, int, const char *, ...)

View File

@ -32,10 +32,6 @@ libc_hidden_proto (readlinkat)
libc_hidden_proto (fsync)
libc_hidden_proto (fdatasync)
libc_hidden_proto (__read_chk)
libc_hidden_proto (__getdomainname_chk)
libc_hidden_proto (__getlogin_r_chk)
/* Now define the internal interfaces. */
extern int __access (const char *__name, int __type);
libc_hidden_proto (__access)

View File

@ -166,7 +166,6 @@ libc_hidden_proto (putwc)
libc_hidden_proto (mbrtowc)
libc_hidden_proto (wcrtomb)
libc_hidden_proto (__wcrtomb_chk)
extern int __wcscmp (const wchar_t *__s1, const wchar_t *__s2)
__THROW __attribute_pure__;

View File

@ -45,6 +45,7 @@ __NTH (inet_pton (int __af,
__fortify_clang_warning_only_if_bos0_lt
(4, __dst, "inet_pton called with destination buffer size less than 4")
{
#if !__fortify_use_clang
size_t sz = 0;
if (__af == AF_INET)
sz = sizeof (struct in_addr);
@ -56,6 +57,11 @@ __NTH (inet_pton (int __af,
return __glibc_fortify (inet_pton, sz, sizeof (char),
__glibc_objsize (__dst),
__af, __src, __dst);
#else
return __glibc_fortify (inet_pton, 0, sizeof (char),
__glibc_objsize (__dst),
__af, __src, __dst);
#endif
};
#endif /* bits/inet-fortified.h. */

View File

@ -60,6 +60,7 @@ routines_no_fortify += \
iofgets_u \
iofgetws \
iofgetws_u \
iovdprintf \
swprintf \
vasprintf \
vsnprintf \

View File

@ -115,10 +115,12 @@ ldbl_hidden_def (___syslog_chk, __syslog_chk)
ldbl_strong_alias (___syslog_chk, __syslog_chk)
void
__vsyslog_chk (int pri, int flag, const char *fmt, va_list ap)
___vsyslog_chk (int pri, int flag, const char *fmt, va_list ap)
{
__vsyslog_internal (pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0);
}
ldbl_hidden_def (___vsyslog_chk, __vsyslog_chk)
ldbl_strong_alias (___vsyslog_chk, __vsyslog_chk)
void
__vsyslog_internal (int pri, const char *fmt, va_list ap,

View File

@ -120,6 +120,7 @@ routines := \
# Exclude fortified routines from being built with _FORTIFY_SOURCE
routines_no_fortify += \
bzero \
explicit_bzero \
memcpy \
memmove \

View File

@ -15,6 +15,21 @@ asm ("memcpy = __GI_memcpy");
asm ("abort = __GI_abort");
#endif
/* clang might generate the internal fortfify calls when it is enabled,
through the buitintin. */
asm ("__vfprintf_chk = __GI___vfprintf_chk");
asm ("__vsprintf_chk = __GI___vsprintf_chk");
asm ("__vsyslog_chk = __GI___vsyslog_chk");
asm ("__memcpy_chk = __GI___memcpy_chk");
asm ("__memmove_chk = __GI___memmove_chk");
asm ("__memset_chk = __GI___memset_chk");
asm ("__mempcpy_chk = __GI___mempcpy_chk");
asm ("__stpcpy_chk = __GI___stpcpy_chk");
asm ("__strcpy_chk = __GI___strcpy_chk");
asm ("strcpy = __GI_strcpy");
asm ("strncpy = __GI_strncpy");
asm ("strcat = __GI_strcat");
/* Some targets do not use __stack_chk_fail_local. In libc.so,
redirect __stack_chk_fail to a hidden reference
__stack_chk_fail_local, to avoid the PLT reference.

View File

@ -33,3 +33,7 @@ ___ieee128_snprintf (char *s, size_t maxlen, const char *format, ...)
return done;
}
strong_alias (___ieee128_snprintf, __snprintfieee128)
ldbl_hidden_def (___ieee128_snprintf, ___ieee128_snprintf)
#ifdef SHARED
strong_alias (___ieee128_snprintf, __GI____ieee128___snprintf)
#endif

View File

@ -56,7 +56,7 @@ hidden_def (___ieee128___syslog_chk)
strong_alias (___ieee128___syslog_chk, __syslog_chkieee128)
void
___ieee128___vsyslog_chk (int pri, int flag, const char *fmt, va_list ap)
____ieee128___vsyslog_chk (int pri, int flag, const char *fmt, va_list ap)
{
unsigned int mode = PRINTF_LDBL_USES_FLOAT128;
if (flag > 0)
@ -64,4 +64,5 @@ ___ieee128___vsyslog_chk (int pri, int flag, const char *fmt, va_list ap)
__vsyslog_internal (pri, fmt, ap, mode);
}
strong_alias (___ieee128___vsyslog_chk, __vsyslog_chkieee128)
strong_alias (____ieee128___vsyslog_chk, ___ieee128___vsyslog_chk)
strong_alias (____ieee128___vsyslog_chk, __vsyslog_chkieee128)