xref: /freebsd/include/ssp/ssp.h (revision b8a496dfb6df7b86e014d0d4476cd75850e060c1)
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_ */
92