xref: /netbsd-src/include/ssp/ssp.h (revision 04bc78e5804d9cbba4f84f6a8be18966cb8f3bfc)
1*04bc78e5Schristos /*	$NetBSD: ssp.h,v 1.16 2023/11/15 03:14:16 christos Exp $	*/
22368dc66Stls 
32368dc66Stls /*-
440f1ca55Schristos  * Copyright (c) 2006, 2011, 2023 The NetBSD Foundation, Inc.
52368dc66Stls  * All rights reserved.
62368dc66Stls  *
72368dc66Stls  * This code is derived from software contributed to The NetBSD Foundation
82368dc66Stls  * by Christos Zoulas.
92368dc66Stls  *
102368dc66Stls  * Redistribution and use in source and binary forms, with or without
112368dc66Stls  * modification, are permitted provided that the following conditions
122368dc66Stls  * are met:
132368dc66Stls  * 1. Redistributions of source code must retain the above copyright
142368dc66Stls  *    notice, this list of conditions and the following disclaimer.
152368dc66Stls  * 2. Redistributions in binary form must reproduce the above copyright
162368dc66Stls  *    notice, this list of conditions and the following disclaimer in the
172368dc66Stls  *    documentation and/or other materials provided with the distribution.
182368dc66Stls  *
192368dc66Stls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202368dc66Stls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212368dc66Stls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222368dc66Stls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232368dc66Stls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242368dc66Stls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252368dc66Stls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262368dc66Stls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272368dc66Stls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282368dc66Stls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292368dc66Stls  * POSSIBILITY OF SUCH DAMAGE.
302368dc66Stls  */
312368dc66Stls #ifndef _SSP_SSP_H_
322368dc66Stls #define _SSP_SSP_H_
332368dc66Stls 
342368dc66Stls #include <sys/cdefs.h>
352368dc66Stls 
362368dc66Stls #if !defined(__cplusplus)
375e0b4409Sjoerg # if _FORTIFY_SOURCE > 0 && !defined(__lint__) && \
385e0b4409Sjoerg      (__OPTIMIZE__ > 0 || defined(__clang__)) && __GNUC_PREREQ__(4, 1)
3940f1ca55Schristos #  if _FORTIFY_SOURCE > 2 && __has_builtin(__builtin_dynamic_object_size)
4040f1ca55Schristos #   define __SSP_FORTIFY_LEVEL 3
4140f1ca55Schristos #  elif _FORTIFY_SOURCE > 1
422368dc66Stls #   define __SSP_FORTIFY_LEVEL 2
432368dc66Stls #  else
442368dc66Stls #   define __SSP_FORTIFY_LEVEL 1
452368dc66Stls #  endif
464bf46019Sjoerg # else
474bf46019Sjoerg #  define __SSP_FORTIFY_LEVEL 0
482368dc66Stls # endif
494bf46019Sjoerg #else
504bf46019Sjoerg # define __SSP_FORTIFY_LEVEL 0
512368dc66Stls #endif
522368dc66Stls 
534bf46019Sjoerg /* __ssp_real is used by the implementation in libc */
544bf46019Sjoerg #if __SSP_FORTIFY_LEVEL == 0
554bf46019Sjoerg #define __ssp_real_(fun)	fun
562d76e866Schristos #else
574bf46019Sjoerg #define __ssp_real_(fun)	__ssp_real_ ## fun
582d76e866Schristos #endif
594bf46019Sjoerg #define __ssp_real(fun)		__ssp_real_(fun)
602d76e866Schristos 
61*04bc78e5Schristos #ifndef __ssp_inline
628cbc788fSchristos #define __ssp_inline extern __inline \
638cbc788fSchristos     __attribute__((__always_inline__, __gnu_inline__))
64*04bc78e5Schristos #endif
65416c220cSchristos 
6640f1ca55Schristos #if __SSP_FORTIFY_LEVEL > 2
6740f1ca55Schristos # define __ssp_bos(ptr) __builtin_dynamic_object_size(ptr, 1)
6840f1ca55Schristos # define __ssp_bos0(ptr) __builtin_dynamic_object_size(ptr, 0)
6940f1ca55Schristos #else
702368dc66Stls # define __ssp_bos(ptr) __builtin_object_size(ptr, __SSP_FORTIFY_LEVEL > 1)
712368dc66Stls # define __ssp_bos0(ptr) __builtin_object_size(ptr, 0)
7240f1ca55Schristos #endif
73416c220cSchristos 
743203d4e9Schristos #define __ssp_check(buf, len, bos) \
753203d4e9Schristos 	if (bos(buf) != (size_t)-1 && len > bos(buf)) \
763203d4e9Schristos 		__chk_fail()
77c7f24be6Sjoerg #define __ssp_redirect_raw(rtype, fun, symbol, args, call, cond, bos) \
784bf46019Sjoerg rtype __ssp_real_(fun) args __RENAME(symbol); \
794bf46019Sjoerg __ssp_inline rtype fun args __RENAME(__ssp_protected_ ## fun); \
80416c220cSchristos __ssp_inline rtype fun args { \
81c7f24be6Sjoerg 	if (cond) \
823203d4e9Schristos 		__ssp_check(__buf, __len, bos); \
834bf46019Sjoerg 	return __ssp_real_(fun) call; \
842368dc66Stls }
852368dc66Stls 
862368dc66Stls #define __ssp_redirect(rtype, fun, args, call) \
87c7f24be6Sjoerg     __ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos)
882368dc66Stls #define __ssp_redirect0(rtype, fun, args, call) \
89c7f24be6Sjoerg     __ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos0)
902368dc66Stls 
91adfc40c5Schristos #define __ssp_overlap(a, b, l) \
92762a9b9cSplunky     (((a) <= (b) && (b) < (a) + (l)) || ((b) <= (a) && (a) < (b) + (l)))
93adfc40c5Schristos 
942368dc66Stls __BEGIN_DECLS
95c33b9df9Sjruoho void __stack_chk_fail(void) __dead;
96c33b9df9Sjruoho void __chk_fail(void) __dead;
972368dc66Stls __END_DECLS
982368dc66Stls 
992368dc66Stls #endif /* _SSP_SSP_H_ */
100