Skip to content

Commit 9bfd3b4

Browse files
committed
Add a build knob for _FORTIFY_SOURCE
In the future, we will Default to _FORTIFY_SOURCE=2 if SSP is enabled, otherwise default to _FORTIFY_SOURCE=0. For now we default it to 0 unconditionally to ease bisect across older versions without the new symbols, and we'll put out a call for testing. include/*.h include their ssp/*.h equivalents as needed based on the knob. Programs and users are allowed to override FORTIFY_SOURCE in their Makefiles or src.conf/make.conf to force it off. Reviewed by: des, markj Relnotes: yes Sponsored by: Stormshield Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D32308
1 parent e555125 commit 9bfd3b4

File tree

10 files changed

+108
-0
lines changed

10 files changed

+108
-0
lines changed

include/stdio.h

+3
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,7 @@ extern int __isthreaded;
530530
__END_DECLS
531531
__NULLABILITY_PRAGMA_POP
532532

533+
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
534+
#include <ssp/stdio.h>
535+
#endif
533536
#endif /* !_STDIO_H_ */

include/string.h

+3
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,7 @@ errno_t memset_s(void *, rsize_t, int, rsize_t);
168168
#endif /* __EXT1_VISIBLE */
169169
__END_DECLS
170170

171+
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
172+
#include <ssp/string.h>
173+
#endif
171174
#endif /* _STRING_H_ */

include/strings.h

+3
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ int strncasecmp(const char *, const char *, size_t) __pure;
6868
#endif
6969
__END_DECLS
7070

71+
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
72+
#include <ssp/strings.h>
73+
#endif
7174
#endif /* _STRINGS_H_ */

include/unistd.h

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#include <sys/_null.h>
3838
#include <sys/_types.h>
3939

40+
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
41+
#include <ssp/unistd.h>
42+
#endif
43+
4044
#ifndef _GID_T_DECLARED
4145
typedef __gid_t gid_t;
4246
#define _GID_T_DECLARED

lib/libthr/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ LDFLAGS+= -Wl,--rpath=/usr/lib${COMPAT_libcompat}
1111

1212
.include <src.opts.mk>
1313
MK_SSP= no
14+
# SSP forced off already implies FORTIFY_SOURCE=0, but we must make sure that
15+
# one cannot turn it back on.
16+
FORTIFY_SOURCE= 0
1417

1518
LIB=thr
1619
SHLIB_MAJOR= 3

libexec/rtld-elf/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ MK_UBSAN= no
1515

1616
.include <bsd.compat.pre.mk>
1717

18+
# SSP forced off already implies FORTIFY_SOURCE=0, but we must make sure that
19+
# one cannot turn it back on.
20+
FORTIFY_SOURCE= 0
21+
1822
.if !defined(NEED_COMPAT)
1923
CONFS= libmap.conf
2024
.endif

share/man/man7/security.7

+75
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,81 @@ option that SSH allows in its
939939
.Pa authorized_keys
940940
file to make the key only usable to entities logging in from specific
941941
machines.
942+
.Sh STACK OVERFLOW PROTECTION
943+
.Fx
944+
supports stack overflow protection using the Stack Smashing Protector
945+
.Pq SSP
946+
compiler feature.
947+
In userland, SSP adds a per-process randomized canary at the end of every stack
948+
frame which is checked for corruption upon return from the function.
949+
In the kernel, a single randomized canary is used globally except on aarch64,
950+
which has a
951+
.Dv PERTHREAD_SSP
952+
.Xr config 8
953+
option to enable per-thread randomized canaries.
954+
If stack corruption is detected, then the process aborts to avoid potentially
955+
malicious execution as a result of the corruption.
956+
SSP may be enabled or disabled when building
957+
.Fx
958+
base with the
959+
.Xr src.conf 5
960+
SSP knob.
961+
.Pp
962+
When
963+
.Va WITH_SSP
964+
is enabled, which is the default, world is built with the
965+
.Fl fstack-protector-strong
966+
compiler option.
967+
The kernel is built with the
968+
.Fl fstack-protector
969+
option.
970+
.Pp
971+
In addition to SSP, a
972+
.Dq FORTIFY_SOURCE
973+
implementation is supported up to level 2 by defining
974+
.Va _FORTIFY_SOURCE
975+
to
976+
.Dv 1
977+
or
978+
.Dv 2
979+
before including any
980+
.Fx
981+
headers.
982+
.Fx
983+
world builds can set
984+
.Va FORTIFY_SOURCE
985+
to provide a default value for
986+
.Va _FORTIFY_SOURCE .
987+
When enabled,
988+
.Dq FORTIFY_SOURCE
989+
enables extra bounds checking in various functions that accept buffers to be
990+
written into.
991+
These functions currently have extra bounds checking support:
992+
.Bl -column -offset indent "snprintf" "memmove" "strncpy" "vsnprintf" "readlink"
993+
.It bcopy Ta bzero Ta fgets Ta getcwd Ta gets
994+
.It memcpy Ta memmove Ta memset Ta read Ta readlink
995+
.It snprintf Ta sprintf Ta stpcpy Ta stpncpy Ta strcat
996+
.It strcpy Ta strncat Ta strncpy Ta vsnprintf Ta vsprintf
997+
.El
998+
.Pp
999+
.Dq FORTIFY_SOURCE
1000+
requires compiler support from
1001+
.Xr clang 1
1002+
or
1003+
.Xr gcc 1 ,
1004+
which provide the
1005+
.Xr __builtin_object_size 3
1006+
function that is used to determine the bounds of an object.
1007+
This feature works best at optimization levels
1008+
.Fl O1
1009+
and above, as some object sizes may be less obvious without some data that the
1010+
compiler would collect in an optimization pass.
1011+
.Pp
1012+
Similar to SSP, violating the bounds of an object will cause the program to
1013+
abort in an effort to avoid malicious execution.
1014+
This effectively provides finer-grained protection than SSP for some class of
1015+
function and system calls, along with some protection for buffers allocated as
1016+
part of the program data.
9421017
.Sh KNOBS AND TWEAKS
9431018
.Fx
9441019
provides several knobs and tweak handles that make some introspection

share/mk/bsd.sys.mk

+7
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,18 @@ CFLAGS.clang+= -Qunused-arguments
294294
# but not yet.
295295
CXXFLAGS.clang+= -Wno-c++11-extensions
296296

297+
# XXX This should be defaulted to 2 when WITH_SSP is in use after further
298+
# testing and soak time.
299+
FORTIFY_SOURCE?= 0
297300
.if ${MK_SSP} != "no"
298301
# Don't use -Wstack-protector as it breaks world with -Werror.
299302
SSP_CFLAGS?= -fstack-protector-strong
300303
CFLAGS+= ${SSP_CFLAGS}
301304
.endif # SSP
305+
.if ${FORTIFY_SOURCE} > 0
306+
CFLAGS+= -D_FORTIFY_SOURCE=${FORTIFY_SOURCE}
307+
CXXFLAGS+= -D_FORTIFY_SOURCE=${FORTIFY_SOURCE}
308+
.endif
302309

303310
# Additional flags passed in CFLAGS and CXXFLAGS when MK_DEBUG_FILES is
304311
# enabled.

tools/build/options/WITHOUT_SSP

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
Do not build world with stack smashing protection.
2+
See
3+
.Xr security 7
4+
for more information.

tools/build/options/WITH_SSP

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
Build world with stack smashing protection.
2+
See
3+
.Xr security 7
4+
for more information.

0 commit comments

Comments
 (0)