Skip to content

Commit be04fec

Browse files
committed
Import _FORTIFY_SOURCE implementation from NetBSD
This is a mostly-unmodified copy of the various *_chk implementations and headers from NetBSD, without yet modifying system headers to start actually including them. A future commit will also apply the needed bits to fix ssp/unistd.h. Reviewed by: imp, pauamma_gundo.com (both previous versions), kib Sponsored by: Stormshield Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D32306
1 parent 94b09d3 commit be04fec

32 files changed

+1621
-140
lines changed

etc/mtree/BSD.include.dist

+2
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@
372372
mac_veriexec
373373
..
374374
..
375+
ssp
376+
..
375377
sys
376378
disk
377379
..

include/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
PACKAGE=clibs
66
CLEANFILES= osreldate.h version
7-
SUBDIR= arpa protocols rpcsvc rpc xlocale
7+
SUBDIR= arpa protocols rpcsvc rpc ssp xlocale
88
.if ${MACHINE_CPUARCH} == "amd64"
99
SUBDIR+= i386
1010
INCLUDE_SUBDIRS+= i386

include/ssp/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# $FreeBSD$
2+
3+
INCS= ssp.h stdio.h string.h strings.h unistd.h
4+
INCSDIR= ${INCLUDEDIR}/ssp
5+
6+
.include <bsd.prog.mk>

include/ssp/ssp.h

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* $NetBSD: ssp.h,v 1.13 2015/09/03 20:43:47 plunky Exp $ */
2+
3+
/*-
4+
*
5+
* SPDX-License-Identifier: BSD-2-Clause
6+
*
7+
* Copyright (c) 2006, 2011 The NetBSD Foundation, Inc.
8+
* All rights reserved.
9+
*
10+
* This code is derived from software contributed to The NetBSD Foundation
11+
* by Christos Zoulas.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
* 1. Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
#ifndef _SSP_SSP_H_
35+
#define _SSP_SSP_H_
36+
37+
#include <sys/cdefs.h>
38+
39+
#if !defined(__cplusplus)
40+
# if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && \
41+
(__OPTIMIZE__ > 0 || defined(__clang__))
42+
# if _FORTIFY_SOURCE > 1
43+
# define __SSP_FORTIFY_LEVEL 2
44+
# else
45+
# define __SSP_FORTIFY_LEVEL 1
46+
# endif
47+
# else
48+
# define __SSP_FORTIFY_LEVEL 0
49+
# endif
50+
#else
51+
# define __SSP_FORTIFY_LEVEL 0
52+
#endif
53+
54+
#define __ssp_var(type) __CONCAT(__ssp_ ## type, __COUNTER__)
55+
56+
/* __ssp_real is used by the implementation in libc */
57+
#if __SSP_FORTIFY_LEVEL == 0
58+
#define __ssp_real_(fun) fun
59+
#else
60+
#define __ssp_real_(fun) __ssp_real_ ## fun
61+
#endif
62+
#define __ssp_real(fun) __ssp_real_(fun)
63+
64+
#define __ssp_inline static __inline __attribute__((__always_inline__))
65+
66+
#define __ssp_bos(ptr) __builtin_object_size(ptr, __SSP_FORTIFY_LEVEL > 1)
67+
#define __ssp_bos0(ptr) __builtin_object_size(ptr, 0)
68+
69+
#define __ssp_check(buf, len, bos) \
70+
if (bos(buf) != (size_t)-1 && len > bos(buf)) \
71+
__chk_fail()
72+
#define __ssp_redirect_raw(rtype, fun, symbol, args, call, cond, bos) \
73+
rtype __ssp_real_(fun) args __RENAME(symbol); \
74+
__ssp_inline rtype fun args __RENAME(__ssp_protected_ ## fun); \
75+
__ssp_inline rtype fun args { \
76+
if (cond) \
77+
__ssp_check(__buf, __len, bos); \
78+
return __ssp_real_(fun) call; \
79+
}
80+
81+
#define __ssp_redirect(rtype, fun, args, call) \
82+
__ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos)
83+
#define __ssp_redirect0(rtype, fun, args, call) \
84+
__ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos0)
85+
86+
__BEGIN_DECLS
87+
void __stack_chk_fail(void) __dead2;
88+
void __chk_fail(void) __dead2;
89+
__END_DECLS
90+
91+
#endif /* _SSP_SSP_H_ */

include/ssp/stdio.h

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* $NetBSD: stdio.h,v 1.5 2011/07/17 20:54:34 joerg Exp $ */
2+
3+
/*-
4+
*
5+
* SPDX-License-Identifier: BSD-2-Clause
6+
*
7+
* Copyright (c) 2006 The NetBSD Foundation, Inc.
8+
* All rights reserved.
9+
*
10+
* This code is derived from software contributed to The NetBSD Foundation
11+
* by Christos Zoulas.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
* 1. Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
#ifndef _SSP_STDIO_H_
35+
#define _SSP_STDIO_H_
36+
37+
#include <ssp/ssp.h>
38+
39+
__BEGIN_DECLS
40+
int __sprintf_chk(char *__restrict, int, size_t, const char *__restrict, ...)
41+
__printflike(4, 5);
42+
int __vsprintf_chk(char *__restrict, int, size_t, const char *__restrict,
43+
__va_list)
44+
__printflike(4, 0);
45+
int __snprintf_chk(char *__restrict, size_t, int, size_t,
46+
const char *__restrict, ...)
47+
__printflike(5, 6);
48+
int __vsnprintf_chk(char *__restrict, size_t, int, size_t,
49+
const char *__restrict, __va_list)
50+
__printflike(5, 0);
51+
char *__gets_chk(char *, size_t);
52+
char *__fgets_chk(char *, int, size_t, FILE *);
53+
__END_DECLS
54+
55+
#if __SSP_FORTIFY_LEVEL > 0
56+
57+
#define sprintf(str, ...) ({ \
58+
char *_ssp_str = (str); \
59+
__builtin___sprintf_chk(_ssp_str, 0, __ssp_bos(_ssp_str), \
60+
__VA_ARGS__); \
61+
})
62+
63+
#define vsprintf(str, fmt, ap) ({ \
64+
char *_ssp_str = (str); \
65+
__builtin___vsprintf_chk(_ssp_str, 0, __ssp_bos(_ssp_str), fmt, \
66+
ap); \
67+
})
68+
69+
#define snprintf(str, len, ...) ({ \
70+
char *_ssp_str = (str); \
71+
__builtin___snprintf_chk(_ssp_str, len, 0, __ssp_bos(_ssp_str), \
72+
__VA_ARGS__); \
73+
})
74+
75+
#define vsnprintf(str, len, fmt, ap) ({ \
76+
char *_ssp_str = (str); \
77+
__builtin___vsnprintf_chk(_ssp_str, len, 0, __ssp_bos(_ssp_str), \
78+
fmt, ap); \
79+
})
80+
81+
#define gets(str) ({ \
82+
char *_ssp_str = (str); \
83+
__gets_chk(_ssp_str, __ssp_bos(_ssp_str)); \
84+
})
85+
86+
#define fgets(str, len, fp) ({ \
87+
char *_ssp_str = (str); \
88+
__fgets_chk(_ssp_str, len, __ssp_bos(_ssp_str), fp); \
89+
})
90+
91+
#endif /* __SSP_FORTIFY_LEVEL > 0 */
92+
93+
#endif /* _SSP_STDIO_H_ */

include/ssp/string.h

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* $NetBSD: string.h,v 1.14 2020/09/05 13:37:59 mrg Exp $ */
2+
3+
/*-
4+
*
5+
* SPDX-License-Identifier: BSD-2-Clause
6+
*
7+
* Copyright (c) 2006 The NetBSD Foundation, Inc.
8+
* All rights reserved.
9+
*
10+
* This code is derived from software contributed to The NetBSD Foundation
11+
* by Christos Zoulas.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
* 1. Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
#ifndef _SSP_STRING_H_
35+
#define _SSP_STRING_H_
36+
37+
#include <ssp/ssp.h>
38+
39+
__BEGIN_DECLS
40+
void *__memcpy_chk(void *, const void *, size_t, size_t);
41+
void *__memmove_chk(void *, const void *, size_t, size_t);
42+
void *__memset_chk(void *, int, size_t, size_t);
43+
char *__stpcpy_chk(char *, const char *, size_t);
44+
char *__stpncpy_chk(char *, const char *, size_t, size_t);
45+
char *__strcat_chk(char *, const char *, size_t);
46+
char *__strcpy_chk(char *, const char *, size_t);
47+
char *__strncat_chk(char *, const char *, size_t, size_t);
48+
char *__strncpy_chk(char *, const char *, size_t, size_t);
49+
__END_DECLS
50+
51+
#if __SSP_FORTIFY_LEVEL > 0
52+
53+
#define __ssp_bos_check3_typed_var(fun, dsttype, dsrvar, dst, srctype, srcvar, \
54+
src, lenvar, len) ({ \
55+
srctype srcvar = (src); \
56+
dsttype dstvar = (dst); \
57+
size_t lenvar = (len); \
58+
((__ssp_bos0(dstvar) != (size_t)-1) ? \
59+
__builtin___ ## fun ## _chk(dstvar, srcvar, lenvar, \
60+
__ssp_bos0(dstvar)) : \
61+
__ ## fun ## _ichk(dstvar, srcvar, lenvar)); \
62+
})
63+
64+
#define __ssp_bos_check3_typed(fun, dsttype, dst, srctype, src, len) \
65+
__ssp_bos_check3_typed_var(fun, dsttype, __ssp_var(dstv), dst, \
66+
srctype, __ssp_var(srcv), src, __ssp_var(lenv), len)
67+
68+
#define __ssp_bos_check3(fun, dst, src, len) \
69+
__ssp_bos_check3_typed_var(fun, void *, __ssp_var(dstv), dst, \
70+
const void *, __ssp_var(srcv), src, __ssp_var(lenv), len)
71+
72+
#define __ssp_bos_check2_var(fun, dstvar, dst, srcvar, src) ({ \
73+
const void *srcvar = (src); \
74+
void *dstvar = (dst); \
75+
((__ssp_bos0(dstvar) != (size_t)-1) ? \
76+
__builtin___ ## fun ## _chk(dstvar, srcvar, \
77+
__ssp_bos0(dstvar)) : \
78+
__ ## fun ## _ichk(dstvar, srcvar)); \
79+
})
80+
81+
#define __ssp_bos_check2(fun, dst, src) \
82+
__ssp_bos_check2_var(fun, __ssp_var(dstv), dst, __ssp_var(srcv), src)
83+
84+
#define __ssp_bos_icheck3_restrict(fun, type1, type2) \
85+
static __inline type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \
86+
static __inline __attribute__((__always_inline__)) type1 \
87+
__ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \
88+
return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
89+
}
90+
91+
#define __ssp_bos_icheck3(fun, type1, type2) \
92+
static __inline type1 __ ## fun ## _ichk(type1, type2, size_t); \
93+
static __inline __attribute__((__always_inline__)) type1 \
94+
__ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \
95+
return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
96+
}
97+
98+
#define __ssp_bos_icheck2_restrict(fun, type1, type2) \
99+
static __inline type1 __ ## fun ## _ichk(type1, type2); \
100+
static __inline __attribute__((__always_inline__)) type1 \
101+
__ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \
102+
return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \
103+
}
104+
105+
__BEGIN_DECLS
106+
__ssp_bos_icheck3_restrict(memcpy, void *, const void *)
107+
__ssp_bos_icheck3(memmove, void *, const void *)
108+
__ssp_bos_icheck3(memset, void *, int)
109+
__ssp_bos_icheck2_restrict(stpcpy, char *, const char *)
110+
__ssp_bos_icheck3_restrict(stpncpy, char *, const char *)
111+
__ssp_bos_icheck2_restrict(strcpy, char *, const char *)
112+
__ssp_bos_icheck2_restrict(strcat, char *, const char *)
113+
__ssp_bos_icheck3_restrict(strncpy, char *, const char *)
114+
__ssp_bos_icheck3_restrict(strncat, char *, const char *)
115+
__END_DECLS
116+
117+
#define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len)
118+
#define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len)
119+
#define memset(dst, val, len) \
120+
__ssp_bos_check3_typed(memset, void *, dst, int, val, len)
121+
#define stpcpy(dst, src) __ssp_bos_check2(stpcpy, dst, src)
122+
#define stpncpy(dst, src, len) __ssp_bos_check3(stpncpy, dst, src, len)
123+
#define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src)
124+
#define strcat(dst, src) __ssp_bos_check2(strcat, dst, src)
125+
#define strncpy(dst, src, len) __ssp_bos_check3(strncpy, dst, src, len)
126+
#define strncat(dst, src, len) __ssp_bos_check3(strncat, dst, src, len)
127+
128+
#endif /* __SSP_FORTIFY_LEVEL > 0 */
129+
#endif /* _SSP_STRING_H_ */

0 commit comments

Comments
 (0)