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
35// zig patch: ssp/ssp.h header was added in FreeBSD 15
36#if __FreeBSD_version < 1500500
37 #error "ssp/ssp.h did not exist before FreeBSD 15"
38#endif /* error for FreeBSD before 15 */
39
40#ifndef _SSP_SSP_H_
41#define _SSP_SSP_H_
42
43#include <sys/cdefs.h>
44
45#if !defined(__cplusplus)
46# if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && \
47 (__OPTIMIZE__ > 0 || defined(__clang__))
48# if _FORTIFY_SOURCE > 1
49# define __SSP_FORTIFY_LEVEL 2
50# else
51# define __SSP_FORTIFY_LEVEL 1
52# endif
53# else
54# define __SSP_FORTIFY_LEVEL 0
55# endif
56#else
57# define __SSP_FORTIFY_LEVEL 0
58#endif
59
60#define __ssp_var(type) __CONCAT(__ssp_ ## type, __COUNTER__)
61
62/* __ssp_real is used by the implementation in libc */
63#if __SSP_FORTIFY_LEVEL == 0
64#define __ssp_real_(fun) fun
65#else
66#define __ssp_real_(fun) __ssp_real_ ## fun
67#endif
68#define __ssp_real(fun) __ssp_real_(fun)
69
70#define __ssp_inline static __inline __attribute__((__always_inline__))
71
72#define __ssp_bos(ptr) __builtin_object_size(ptr, __SSP_FORTIFY_LEVEL > 1)
73#define __ssp_bos0(ptr) __builtin_object_size(ptr, 0)
74
75#define __ssp_check(buf, len, bos) \
76 if (bos(buf) != (size_t)-1 && (size_t)len > bos(buf)) \
77 __chk_fail()
78
79#define __ssp_redirect_raw_impl(rtype, fun, symbol, args) \
80rtype __ssp_real_(fun) args __RENAME(symbol); \
81__ssp_inline rtype fun args __RENAME(__ssp_protected_ ## fun); \
82__ssp_inline rtype fun args
83
84#define __ssp_redirect_raw(rtype, fun, symbol, args, call, cond, bos, len) \
85__ssp_redirect_raw_impl(rtype, fun, symbol, args) { \
86 if (cond) \
87 __ssp_check(__buf, len, bos); \
88 return __ssp_real_(fun) call; \
89}
90
91#define __ssp_redirect(rtype, fun, args, call) \
92 __ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos, __len)
93#define __ssp_redirect0(rtype, fun, args, call) \
94 __ssp_redirect_raw(rtype, fun, fun, args, call, 1, __ssp_bos0, __len)
95
96#include <sys/_types.h>
97#include <machine/_limits.h>
98
99__ssp_inline int
100__ssp_overlap(const void *leftp, const void *rightp, __size_t sz)
101{
102 __uintptr_t left = (__uintptr_t)leftp;
103 __uintptr_t right = (__uintptr_t)rightp;
104
105 if (left <= right)
106 return (__SIZE_T_MAX - sz < left || right < left + sz);
107
108 return (__SIZE_T_MAX - sz < right || left < right + sz);
109}
110
111#include <sys/_iovec.h>
112
113__BEGIN_DECLS
114void __stack_chk_fail(void) __dead2;
115void __chk_fail(void) __dead2;
116__END_DECLS
117
118__ssp_inline void
119__ssp_check_iovec(const struct iovec *iov, int iovcnt)
120{
121 const size_t iovsz = __ssp_bos(iov);
122 int i;
123
124 if (iovsz != (size_t)-1 && iovsz / sizeof(*iov) < (size_t)iovcnt)
125 __chk_fail();
126
127 for (i = 0; i < iovcnt; i++) {
128 if (__ssp_bos(iov[i].iov_base) < iov[i].iov_len)
129 __chk_fail();
130 }
131}
132
133#endif /* _SSP_SSP_H_ */