Skip to content

Commit e555125

Browse files
committed
Prepare the system for _FORTIFY_SOURCE
Notably: - libc needs to #undef some of the macros from ssp/* for underlying implementations - ssp/* wants a __RENAME() macro (snatched more or less from NetBSD) There's some extra hinkiness included for read(), since libc spells it as "_read" while the rest of the world spells it "read." Reviewed by: imp, ngie Sponsored by: Stormshield Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D32307
1 parent be04fec commit e555125

File tree

20 files changed

+58
-3
lines changed

20 files changed

+58
-3
lines changed

contrib/netbsd-tests/lib/libc/ssp/h_gets.c

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ __RCSID("$NetBSD: h_gets.c,v 1.1 2010/12/27 02:04:19 pgoyette Exp $");
3434
#include <stdio.h>
3535

3636
#ifdef __FreeBSD__
37+
/* _FORTIFY_SOURCE, at the very least, may #define a gets() macro. */
38+
#undef gets
39+
3740
/*
3841
* We want to test the gets() implementation, but cannot simply link against
3942
* the gets symbol because it is not in the default version. (We've made it

include/ssp/unistd.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@
3939
#if __SSP_FORTIFY_LEVEL > 0
4040
__BEGIN_DECLS
4141

42-
__ssp_redirect0(ssize_t, read, (int __fd, void *__buf, size_t __len), \
43-
(__fd, __buf, __len));
42+
#ifndef _FORTIFY_SOURCE_read
43+
#define _FORTIFY_SOURCE_read read
44+
#endif
45+
46+
__ssp_redirect0(ssize_t, _FORTIFY_SOURCE_read, (int __fd, void *__buf,
47+
size_t __len), (__fd, __buf, __len));
4448

4549
__ssp_redirect(ssize_t, readlink, (const char *__restrict __path, \
4650
char *__restrict __buf, size_t __len), (__path, __buf, __len));

lib/libc/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ LIBC_ARCH=${M}
1919
LIBC_ARCH=${MACHINE_CPUARCH}
2020
.endif
2121

22+
CFLAGS+=-D_FORTIFY_SOURCE_read=_read
23+
2224
# All library objects contain FreeBSD revision strings by default; they may be
2325
# excluded as a space-saving measure. To produce a library that does
2426
# not contain these strings, add -DSTRIP_FBSDID (see <sys/cdefs.h>) to CFLAGS

lib/libc/amd64/string/bcopy.c

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <string.h>
66

7+
#undef bcopy /* _FORTIFY_SOURCE */
8+
79
void
810
bcopy(const void *src, void *dst, size_t len)
911
{

lib/libc/amd64/string/bzero.c

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <string.h>
66

7+
#undef bzero /* _FORTIFY_SOURCE */
8+
79
void
810
bzero(void *b, size_t len)
911
{

lib/libc/amd64/string/strncat.c

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <string.h>
1010

11+
#undef strncat /* _FORTIFY_SOURCE */
12+
1113
void *__memccpy(void *restrict, const void *restrict, int, size_t);
1214

1315
char *

lib/libc/amd64/string/strncpy.c

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <sys/cdefs.h>
3030
#include <string.h>
3131

32+
#undef strncpy /* _FORTIFY_SOURCE */
33+
3234
char *__stpncpy(char *restrict, const char *restrict, size_t);
3335

3436
char *

lib/libc/gen/getcwd.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <stdlib.h>
4141
#include <string.h>
4242
#include <unistd.h>
43+
#include <ssp/ssp.h>
4344
#include "un-namespace.h"
4445

4546
#include "gen-private.h"
@@ -51,7 +52,7 @@
5152
extern int __getcwd(char *, size_t);
5253

5354
char *
54-
getcwd(char *pt, size_t size)
55+
__ssp_real(getcwd)(char *pt, size_t size)
5556
{
5657
struct dirent *dp;
5758
DIR *dir = NULL;

lib/libc/stdio/fgets.c

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#include "local.h"
4141
#include "libc_private.h"
4242

43+
#undef fgets /* _FORTIFY_SOURCE */
44+
4345
/*
4446
* Read at most n-1 characters from the given file.
4547
* Stop when a newline has been read, or the count runs out.

lib/libc/stdio/snprintf.c

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
#include "local.h"
4747

48+
#undef snprintf /* _FORTIFY_SOURCE */
49+
4850
int
4951
snprintf(char * __restrict str, size_t n, char const * __restrict fmt, ...)
5052
{

lib/libc/stdio/sprintf.c

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "local.h"
4444
#include "xlocale_private.h"
4545

46+
#undef sprintf /* _FORTIFY_SOURCE */
47+
4648
int
4749
sprintf(char * __restrict str, char const * __restrict fmt, ...)
4850
{

lib/libc/stdio/vsnprintf.c

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "local.h"
4444
#include "xlocale_private.h"
4545

46+
#undef vsnprintf /* _FORTIFY_SOURCE */
47+
4648
int
4749
vsnprintf_l(char * __restrict str, size_t n, locale_t locale,
4850
const char * __restrict fmt, __va_list ap)

lib/libc/stdio/vsprintf.c

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "local.h"
4343
#include "xlocale_private.h"
4444

45+
#undef vsprintf /* _FORTIFY_SOURCE */
46+
4547
int
4648
vsprintf_l(char * __restrict str, locale_t locale,
4749
const char * __restrict fmt, __va_list ap)

lib/libc/string/bcopy.c

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ typedef intptr_t word; /* "word" used for optimal copy speed */
4747
#if defined(MEMCOPY) || defined(MEMMOVE)
4848
#include <string.h>
4949

50+
#undef memcpy /* _FORTIFY_SOURCE */
51+
#undef memmove /* _FORTIFY_SOURCE */
52+
5053
void *
5154
#ifdef MEMCOPY
5255
memcpy
@@ -57,6 +60,8 @@ memmove
5760
#else
5861
#include <strings.h>
5962

63+
#undef bcopy /* _FORTIFY_SOURCE */
64+
6065
void
6166
bcopy(const void *src0, void *dst0, size_t length)
6267
#endif

lib/libc/string/memset.c

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#ifdef BZERO
4343
#include <strings.h>
4444

45+
#undef bzero /* _FORTIFY_SOURCE */
46+
4547
#define RETURN return
4648
#define VAL 0
4749
#define WIDEVAL 0
@@ -51,6 +53,8 @@ bzero(void *dst0, size_t length)
5153
#else
5254
#include <string.h>
5355

56+
#undef memset /* _FORTIFY_SOURCE */
57+
5458
#define RETURN return (dst0)
5559
#define VAL c0
5660
#define WIDEVAL c

lib/libc/string/stpcpy.c

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
#include <string.h>
3535

36+
#undef stpcpy /* _FORTIFY_SOURCE */
37+
3638
char *
3739
stpcpy(char * __restrict to, const char * __restrict from)
3840
{

lib/libc/string/stpncpy.c

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include <string.h>
3030

31+
#undef stpncpy /* _FORTIFY_SOURCE */
32+
3133
char *
3234
stpncpy(char * __restrict dst, const char * __restrict src, size_t n)
3335
{

lib/libc/string/strcat.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include <string.h>
3333

34+
#undef strcat /* _FORTIFY_SOURCE */
35+
3436
char *
3537
strcat(char * __restrict s, const char * __restrict append)
3638
{

lib/libc/string/strncat.c

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#include <string.h>
3636

37+
#undef strncat /* _FORTIFY_SOURCE */
38+
3739
/*
3840
* Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes
3941
* are written at dst (at most n+1 bytes being appended). Return dst.

sys/sys/cdefs.h

+10
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,16 @@
609609
#define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
610610
#endif
611611

612+
#if !defined(_STANDALONE) && !defined(_KERNEL)
613+
#if defined(__GNUC__) || defined(__PCC__)
614+
#define __RENAME(x) __asm(__STRING(x))
615+
#else
616+
#define __RENAME(x) no renaming support for compiler in use
617+
#endif /* __GNUC__ */
618+
#else /* _STANDALONE || _KERNEL */
619+
#define __RENAME(x) no renaming in kernel/standalone environment
620+
#endif
621+
612622
/*-
613623
* The following definitions are an extension of the behavior originally
614624
* implemented in <sys/_posix.h>, but with a different level of granularity.

0 commit comments

Comments
 (0)