1 /* $NetBSD: ssp.h,v 1.13 2015/09/03 20:43:47 plunky Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c) 2006, 2011 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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.
37 #include <sys/cdefs.h>
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
45 # define __SSP_FORTIFY_LEVEL 1
48 # define __SSP_FORTIFY_LEVEL 0
51 # define __SSP_FORTIFY_LEVEL 0
54 #define __ssp_var(type) __CONCAT(__ssp_ ## type, __COUNTER__)
56 /* __ssp_real is used by the implementation in libc */
57 #if __SSP_FORTIFY_LEVEL == 0
58 #define __ssp_real_(fun) fun
60 #define __ssp_real_(fun) __ssp_real_ ## fun
62 #define __ssp_real(fun) __ssp_real_(fun)
64 #define __ssp_inline static __inline __attribute__((__always_inline__))
66 #define __ssp_bos(ptr) __builtin_object_size(ptr, __SSP_FORTIFY_LEVEL > 1)
67 #define __ssp_bos0(ptr) __builtin_object_size(ptr, 0)
69 #define __ssp_check(buf, len, bos) \
70 if (bos(buf) != (size_t)-1 && (size_t)len > bos(buf)) \
73 #define __ssp_redirect_raw_impl(rtype, fun, symbol, args) \
74 rtype __ssp_real_(fun) args __RENAME(symbol); \
75 __ssp_inline rtype fun args __RENAME(__ssp_protected_ ## fun); \
76 __ssp_inline rtype fun args
78 #define __ssp_redirect_raw(rtype, fun, symbol, args, call, cond, bos, len) \
79 __ssp_redirect_raw_impl(rtype, fun, symbol, args) { \
81 __ssp_check(__buf, len, bos); \
82 return __ssp_real_(fun) call; \
85 #define __ssp_redirect(rtype, fun, args, call) \
86 __ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos, __len)
87 #define __ssp_redirect0(rtype, fun, args, call) \
88 __ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos0, __len)
90 #include <machine/_limits.h>
93 __ssp_overlap(const void *leftp
, const void *rightp
, __size_t sz
)
95 __uintptr_t left
= (__uintptr_t
)leftp
;
96 __uintptr_t right
= (__uintptr_t
)rightp
;
99 return (__SIZE_T_MAX
- sz
< left
|| right
< left
+ sz
);
101 return (__SIZE_T_MAX
- sz
< right
|| left
< right
+ sz
);
104 #include <sys/_iovec.h>
107 void __stack_chk_fail(void) __dead2
;
108 void __chk_fail(void) __dead2
;
112 __ssp_check_iovec(const struct iovec
*iov
, int iovcnt
)
114 const size_t iovsz
= __ssp_bos(iov
);
117 if (iovsz
!= (size_t)-1 && iovsz
/ sizeof(*iov
) < (size_t)iovcnt
)
120 for (i
= 0; i
< iovcnt
; i
++) {
121 if (__ssp_bos(iov
[i
].iov_base
) < iov
[i
].iov_len
)
126 #endif /* _SSP_SSP_H_ */