xref: /freebsd/include/ssp/ssp.h (revision f2055611fe5624017bd7769c8dc6419c348806ff)
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 && (size_t)len > bos(buf)) \
71 		__chk_fail()
72 
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
77 
78 #define __ssp_redirect_raw(rtype, fun, symbol, args, call, cond, bos, len) \
79 __ssp_redirect_raw_impl(rtype, fun, symbol, args) { \
80 	if (cond) \
81 		__ssp_check(__buf, len, bos); \
82 	return __ssp_real_(fun) call; \
83 }
84 
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)
89 
90 #include <machine/_stdint.h>
91 
92 __ssp_inline int
93 __ssp_overlap(const void *leftp, const void *rightp, __size_t sz)
94 {
95 	__uintptr_t left = (__uintptr_t)leftp;
96 	__uintptr_t right = (__uintptr_t)rightp;
97 
98 	if (left <= right)
99 		return (SIZE_MAX - sz < left || right < left + sz);
100 
101 	return (SIZE_MAX - sz < right || left < right + sz);
102 }
103 
104 #include <sys/_iovec.h>
105 
106 __BEGIN_DECLS
107 void __stack_chk_fail(void) __dead2;
108 void __chk_fail(void) __dead2;
109 __END_DECLS
110 
111 __ssp_inline void
112 __ssp_check_iovec(const struct iovec *iov, int iovcnt)
113 {
114 	const size_t iovsz = __ssp_bos(iov);
115 	int i;
116 
117 	if (iovsz != (size_t)-1 && iovsz / sizeof(*iov) < (size_t)iovcnt)
118 		__chk_fail();
119 
120 	for (i = 0; i < iovcnt; i++) {
121 		if (__ssp_bos(iov[i].iov_base) < iov[i].iov_len)
122 			__chk_fail();
123 	}
124 }
125 
126 #endif /* _SSP_SSP_H_ */
127