View | Details | Raw Unified | Return to bug 202311 | Differences between
and this patch

Collapse All | Expand All

(-)etc/mtree/BSD.include.dist (+2 lines)
Lines 321-326 Link Here
321
        mac_partition
321
        mac_partition
322
        ..
322
        ..
323
    ..
323
    ..
324
    secure
325
    ..
324
    ssp
326
    ssp
325
    ..
327
    ..
326
    sys
328
    sys
(-)include/Makefile (-1 / +1 lines)
Lines 6-12 Link Here
6
.include <src.opts.mk>
6
.include <src.opts.mk>
7
7
8
CLEANFILES= osreldate.h version vers.c
8
CLEANFILES= osreldate.h version vers.c
9
SUBDIR= arpa protocols rpcsvc rpc xlocale
9
SUBDIR= arpa protocols rpcsvc rpc secure xlocale
10
INCS=	a.out.h ar.h assert.h bitstring.h complex.h cpio.h _ctype.h ctype.h \
10
INCS=	a.out.h ar.h assert.h bitstring.h complex.h cpio.h _ctype.h ctype.h \
11
	db.h \
11
	db.h \
12
	dirent.h dlfcn.h elf.h elf-hints.h err.h fmtmsg.h fnmatch.h fstab.h \
12
	dirent.h dlfcn.h elf.h elf-hints.h err.h fmtmsg.h fnmatch.h fstab.h \
(-)include/secure/Makefile (+6 lines)
Line 0 Link Here
1
# $FreeBSD$
2
3
INCS= security.h _poll.h _socket.h _stat.h _stdio.h _string.h _strings.h _unistd.h
4
INCSDIR= ${INCLUDEDIR}/secure
5
6
.include <bsd.prog.mk>
(-)include/secure/_poll.h (+96 lines)
Line 0 Link Here
1
/*
2
 * Copyright (C) 2008 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: eeb9f5e41662828989f3913d81ec23229a668434
29
 *
30
 * $FreeBSD$
31
 */
32
33
#ifndef _SYS_POLL_H_
34
#error "You should not use <secure/_poll.h> directly; include <sys/poll.h> instead."
35
#endif
36
37
#ifndef _SECURE_POLL_H_
38
#define _SECURE_POLL_H_
39
40
#include <secure/security.h>
41
42
__BEGIN_DECLS
43
44
int	__poll_chk(struct pollfd *, nfds_t, int, size_t);
45
int	__poll_real(struct pollfd *, nfds_t, int) __RENAME(poll);
46
__errordecl(__poll_too_small_error, "poll: pollfd array smaller than fd count");
47
48
int	__ppoll_chk(struct pollfd *, nfds_t, const struct timespec *, const sigset_t *, size_t);
49
int	__ppoll_real(struct pollfd *, nfds_t, const struct timespec *, const sigset_t *) __RENAME(ppoll);
50
__errordecl(__ppoll_too_small_error, "ppoll: pollfd array smaller than fd count");
51
52
#ifdef __BSD_FORTIFY
53
54
__FORTIFY_INLINE int
55
poll(struct pollfd *_fds, nfds_t _fd_count, int _timeout)
56
{
57
	size_t _bos = __bos(_fds);
58
59
#ifdef __clang__
60
	return (__poll_chk(_fds, _fd_count, _timeout, _bos));
61
#else
62
	if (_bos != __FORTIFY_UNKNOWN_SIZE) {
63
		if (!__builtin_constant_p(_fd_count))
64
			return (__poll_chk(_fds, _fd_count, _timeout, _bos));
65
		else if (_bos / sizeof(*_fds) < _fd_count)
66
			__poll_too_small_error();
67
	}
68
	return (__poll_real(_fds, _fd_count, _timeout));
69
#endif
70
}
71
72
#if __BSD_VISIBLE
73
__FORTIFY_INLINE int
74
ppoll(struct pollfd *_fds, nfds_t _fd_count, const struct timespec *_timeout, const sigset_t *_mask)
75
{
76
	size_t _bos = __bos(_fds);
77
78
#ifdef __clang__
79
	return (__ppoll_chk(_fds, _fd_count, _timeout, _mask, _bos));
80
#else
81
	if (_bos != __FORTIFY_UNKNOWN_SIZE) {
82
		if (!__builtin_constant_p(_fd_count))
83
			return (__ppoll_chk(_fds, _fd_count, _timeout, _mask, _bos));
84
		else if (_bos / sizeof(*_fds) < _fd_count)
85
			__ppoll_too_small_error();
86
	}
87
	return (__ppoll_real(_fds, _fd_count, _timeout, _mask));
88
#endif
89
}
90
#endif
91
92
#endif
93
94
__END_DECLS
95
96
#endif	/* !_SECURE_POLL_H_ */
(-)include/secure/_socket.h (+83 lines)
Line 0 Link Here
1
/*
2
 * Copyright (C) 2008 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: a8993c994e45ec2dc00dcef15910560e22d67be9 
29
 *
30
 * $FreeBSD$
31
 */
32
33
34
#ifndef _SYS_SOCKET_H_
35
#error "You should not use <secure/_socket.h> directly; include <sys/socket.h> instead."
36
#endif
37
38
#ifndef _SECURE_SOCKET_H_
39
#define _SECURE_SOCKET_H_
40
41
#include <sys/_null.h>
42
#include <secure/security.h>
43
44
__BEGIN_DECLS
45
46
extern ssize_t	__recvfrom_chk(int, void *, size_t, size_t, int, struct sockaddr * __restrict, socklen_t * __restrict);
47
extern ssize_t	__recvfrom_real(int, void *, size_t, int, const struct sockaddr *, socklen_t *) __RENAME(recvfrom);
48
__errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
49
50
#ifdef __BSD_FORTIFY
51
52
__FORTIFY_INLINE ssize_t
53
recvfrom(int _s, void *_buf, size_t _len, int _flags, struct sockaddr * __restrict _from, socklen_t * __restrict _fromlen)
54
{
55
	size_t _bos = __bos0(_buf);
56
57
#ifndef __clang__
58
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
59
		return (__recvfrom_real(_s, _buf, _len, _flags, _from, _fromlen));
60
61
	if (__builtin_constant_p(_len) && (_len <= _bos))
62
		return (__recvfrom_real(_s, _buf, _len, _flags, _from, _fromlen));
63
64
	if (__builtin_constant_p(_len) && (_len > _bos))
65
		__recvfrom_error();
66
#endif
67
68
	return (__recvfrom_chk(_s, _buf, _len, _bos, _flags, _from, _fromlen));
69
}
70
71
72
__FORTIFY_INLINE ssize_t
73
recv(int _s, void *_buf, size_t _len, int _flags)
74
{
75
76
	return recvfrom(_s, _buf, _len, _flags, NULL, 0);
77
}
78
79
#endif /* !__BSD_FORTIFY */
80
81
__END_DECLS
82
83
#endif /* !_SECURE_SOCKET_H */
(-)include/secure/_stat.h (+70 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2008 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#ifndef _SYS_STAT_H_
34
#error "You should not use <secure/_stat.h> directly; include <sys/stat.h> instead."
35
#endif
36
37
#ifndef _SECURE_STAT_H_
38
#define	_SECURE_STAT_H_
39
40
#include <secure/security.h>
41
42
__BEGIN_DECLS
43
44
extern mode_t __umask_chk(mode_t);
45
#ifndef __FORTIFY_UMASK_REAL
46
#define	__FORTIFY_UMASK_REAL	1
47
extern mode_t __umask_real(mode_t) __RENAME(umask);
48
#endif
49
__errordecl(__umask_invalid_mode, "umask called with invalid mode");
50
51
#ifdef __BSD_FORTIFY
52
53
__FORTIFY_INLINE mode_t
54
umask(mode_t _mode)
55
{
56
#ifndef __clang__
57
	if (__builtin_constant_p(_mode)) {
58
		if ((_mode & 0777) != _mode)
59
			__umask_invalid_mode();
60
61
		return (__umask_real(_mode));
62
	}
63
#endif
64
	return (__umask_chk(_mode));
65
}
66
#endif /* defined(__BSD_FORTIFY) */
67
68
__END_DECLS
69
70
#endif /* !_SECURE_STAT_H_ */
(-)include/secure/_stdio.h (+240 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 1990 The Regents of the University of California.
3
 * All rights reserved.
4
 *
5
 * This code is derived from software contributed to Berkeley by
6
 * Chris Torek.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 *
32
 *	@(#)stdio.h	5.17 (Berkeley) 6/3/91
33
 *	$OpenBSD: stdio.h,v 1.35 2006/01/13 18:10:09 miod Exp $
34
 *	$NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $
35
 *	bionic rev: 6cc98af72b0c48c58b2ab5fdb5f7abb842175299
36
 *	$FreeBSD$
37
 */
38
39
#ifndef _STDIO_H_
40
#error "You should not use <secure/_stdio.h> directly; include <stdio.h> instead."
41
#endif
42
43
#ifndef	_SECURE_STDIO_H_
44
#define	_SECURE_STDIO_H_
45
46
#include <secure/security.h>
47
#include <stdarg.h>
48
49
__BEGIN_DECLS
50
51
extern char	*__fgets_chk(char *, int, FILE *, size_t);
52
extern char	*__fgets_real(char *, int, FILE *) __RENAME(fgets);
53
__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
54
__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
55
extern size_t	__fread_chk(void * __restrict, size_t, size_t, FILE * __restrict, size_t);
56
extern size_t	__fread_real(void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fread);
57
__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
58
__errordecl(__fread_overflow, "fread called with overflowing size * count");
59
extern size_t	__fwrite_chk(const void * __restrict, size_t, size_t, FILE * __restrict, size_t);
60
extern size_t	__fwrite_real(const void * __restrict, size_t, size_t, FILE * __restrict) __RENAME(fwrite);
61
__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
62
__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
63
extern int	__sprintf_chk(char * __restrict, int, size_t, const char * __restrict, ...);
64
extern int	__sprintf_real(char * __restrict, const char * __restrict, ...) __RENAME(sprintf);
65
extern int	__vsprintf_chk(char * __restrict, int, size_t, const char * __restrict, __va_list);
66
extern int	__vsprintf_real(char * __restrict, const char * __restrict, __va_list) __RENAME(vsprintf);
67
68
#if __ISO_C_VISIBLE >= 1999
69
extern int	__snprintf_chk(char * __restrict, size_t, int, size_t, const char * __restrict, ...);
70
extern int	__snprintf_real(char * __restrict, size_t, const char * __restrict, ...) __RENAME(snprintf) __printflike(3, 4);
71
extern int	__vsnprintf_chk(char * __restrict, size_t, int, size_t, const char * __restrict, __va_list);
72
extern int	__vsnprintf_real(char * __restrict, size_t, const char * __restrict, __va_list) __RENAME(vsnprintf) __printflike(3, 0);
73
#endif
74
75
#ifdef __BSD_FORTIFY
76
77
#if __ISO_C_VISIBLE >= 1999
78
__FORTIFY_INLINE __printflike(3, 0) int
79
vsnprintf(char *_dest, size_t _size, const char *_format, __va_list _ap)
80
{
81
	size_t _bos = __bos(_dest);
82
83
#ifndef __clang__
84
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
85
		return (__vsnprintf_real(_dest, _size, _format, _ap));
86
#endif
87
88
	return (__vsnprintf_chk(_dest, _size, 0, _bos, _format, _ap));
89
}
90
#endif /* __ISO_C_VISIBLE */
91
92
__FORTIFY_INLINE __printflike(2, 0) int
93
vsprintf(char *_dest, const char *_format, __va_list _ap)
94
{
95
	size_t _bos = __bos(_dest);
96
97
#ifndef __clang__
98
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
99
		return (__vsprintf_real(_dest, _format, _ap));
100
#endif
101
102
	return (__vsprintf_chk(_dest, 0, _bos, _format, _ap));
103
}
104
105
106
#if __ISO_C_VISIBLE >= 1999
107
#if !__has_builtin(__builtin_va_arg_pack) && !__GNUC_PREREQ__(4, 3)	/* defined(__clang__) */
108
#if !defined(snprintf) && !defined(__cplusplus)
109
#define	__wrap_snprintf(_dest, _size, ...)	__snprintf_chk(_dest, _size, 0, __bos(_dest), __VA_ARGS__)
110
#define	snprintf(...)	__wrap_snprintf(__VA_ARGS__)
111
#endif /* !snprintf */
112
#else /* __GNUC_PREREQ__(4, 3) */
113
__FORTIFY_INLINE __printflike(3, 4) int
114
snprintf(char *_dest, size_t _size, const char *_format, ...)
115
{
116
	size_t _bos = __bos(_dest);
117
118
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
119
		return (__snprintf_real(_dest, _size, _format,
120
		    __builtin_va_arg_pack()));
121
122
	return (__snprintf_chk(_dest, _size, 0, _bos, _format,
123
	    __builtin_va_arg_pack()));
124
}
125
#endif /* !__GNUC_PREREQ__(4, 3) */
126
#endif /* __ISO_C_VISIBLE */
127
128
#if !__has_builtin(__builtin_va_arg_pack) && !__GNUC_PREREQ__(4, 3)	/* defined(__clang__) */
129
#if !defined(sprintf) && !defined(__cplusplus)
130
#define	__wrap_sprintf(_dest, ...)	__sprintf_chk(_dest, 0, __bos(_dest), __VA_ARGS__)
131
#define	sprintf(...)	__wrap_sprintf(__VA_ARGS__)
132
#endif /* !sprintf */
133
#else /* __GNUC_PREREQ__(4, 3) */
134
__FORTIFY_INLINE __printflike(2, 3) int
135
sprintf(char *_dest, const char *_format, ...)
136
{
137
	size_t _bos = __bos(_dest);
138
139
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
140
		return (__sprintf_real(_dest, _format,
141
		    __builtin_va_arg_pack()));
142
143
	return (__sprintf_chk(_dest, 0, _bos, _format,
144
	    __builtin_va_arg_pack()));
145
}
146
147
#endif /* !__GNUC_PREREQ__(4, 3) */
148
149
__FORTIFY_INLINE char *
150
fgets(char *_buf, int _n, FILE *_stream)
151
{
152
	size_t _bos = __bos(_buf);
153
154
#ifndef __clang__
155
	/*
156
	 * Compiler can prove, at compile time, that the passed in size
157
	 * is always negative.
158
	 * Force a compiler error.
159
	 */
160
	if (__builtin_constant_p(_n) && (_n < 0))
161
		__fgets_too_small_error();
162
	/*
163
	 * Compiler doesn 't know destination size.
164
	 * Don' t call __fgets_chk.
165
	 */
166
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
167
		return (__fgets_real(_buf, _n, _stream));
168
	/*
169
	 * Compiler can prove, at compile time, that the passed in size
170
	 * is always <= the actual object size.
171
	 * Don 't call __fgets_chk.
172
	 */
173
	if (__builtin_constant_p(_n) && (_n <= (int)_bos))
174
		return (__fgets_real(_buf, _n, _stream));
175
	/*
176
	 * Compiler can prove, at compile time, that the passed in size
177
	 * is always > the actual object size.
178
	 * Force a compiler error.
179
	 */
180
	if (__builtin_constant_p(_n) && (_n > (int)_bos))
181
		__fgets_too_big_error();
182
#endif
183
	return (__fgets_chk(_buf, _n, _stream, _bos));
184
}
185
186
187
__FORTIFY_INLINE size_t
188
fread(void * __restrict _ptr, size_t _size, size_t _nmemb, FILE * __restrict _stream)
189
{
190
	size_t _bos = __bos0(_ptr);
191
#ifndef __clang__
192
	size_t _total;
193
194
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
195
		return (__fread_real(_ptr, _size, _nmemb, _stream));
196
197
	if (__builtin_constant_p(_size) && __builtin_constant_p(_nmemb)) {
198
		if (__size_mul_overflow(_size, _nmemb, &_total))
199
			__fread_overflow();
200
201
		if (_total > _bos)
202
			__fread_too_big_error();
203
204
		return (__fread_real(_ptr, _size, _nmemb, _stream));
205
	}
206
#endif
207
208
	return (__fread_chk(_ptr, _size, _nmemb, _stream, _bos));
209
}
210
211
212
__FORTIFY_INLINE size_t
213
fwrite(const void * __restrict _ptr, size_t _size, size_t _nmemb, FILE * __restrict _stream)
214
{
215
	size_t _bos = __bos0(_ptr);
216
#ifndef __clang__
217
	size_t _total;
218
219
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
220
		return __fwrite_real(_ptr, _size, _nmemb, _stream);
221
222
	if (__builtin_constant_p(_size) && __builtin_constant_p(_nmemb)) {
223
		if (__size_mul_overflow(_size, _nmemb, &_total))
224
			__fwrite_overflow();
225
226
		if (_total > _bos)
227
			__fwrite_too_big_error();
228
229
		return (__fwrite_real(_ptr, _size, _nmemb, _stream));
230
	}
231
#endif
232
233
	return (__fwrite_chk(_ptr, _size, _nmemb, _stream, _bos));
234
}
235
236
#endif /* defined(__BSD_FORTIFY) */
237
238
__END_DECLS
239
240
#endif /* !_SECURE_STDIO_H_ */
(-)include/secure/_string.h (+448 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 2015 Olivér Pintér
3
 * Copyright (C) 2008 The Android Open Source Project
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *  * Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 *  * Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in
13
 *    the documentation and/or other materials provided with the
14
 *    distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * bionic rev: 9ef26a3c4cd2e6d469f771815a07cb820800beb6
30
 *
31
 * $FreeBSD$
32
 */
33
34
#ifndef _STRING_H_
35
#error "You should not use <secure/_string.h> directly; include <string.h> instead."
36
#endif
37
38
#ifndef _SECURE_STRING_H_
39
#define	_SECURE_STRING_H_
40
41
#include <secure/security.h>
42
43
__BEGIN_DECLS
44
45
extern void 	*__memccpy_chk(void *, const void *, int, size_t, size_t);
46
extern void	*__memccpy_real(void *, const void *, int, size_t) __RENAME(memccpy);
47
extern void	*__memchr_chk(const void *, int, size_t, size_t);
48
extern void	*__memchr_real(const void *, int, size_t) __RENAME(memchr);
49
extern void	*__memcpy_chk(void *, const void *, size_t, size_t);
50
extern void	*__memcpy_real(void *, const void *, size_t) __RENAME(memcpy);
51
__errordecl(__memchr_buf_size_error, "memchr called with size bigger than buffer");
52
extern void	*__memmove_chk(void *, const void *, size_t, size_t);
53
extern void	*__memmove_real(void *, const void *, size_t) __RENAME(memmove);
54
extern void	*__memrchr_chk(const void *, int, size_t, size_t);
55
extern void	*__memrchr_real(const void *, int, size_t) __RENAME(memrchr);
56
__errordecl(__memrchr_buf_size_error, "memrchr called with size bigger than buffer");
57
extern void	*__memset_chk(void *, int, size_t, size_t);
58
extern void	*__memset_real(void *, int, size_t) __RENAME(memset);
59
extern char	*__strcat_chk(char *__restrict, const char *__restrict, size_t);
60
extern char	*__strcat_real(char *__restrict, const char *__restrict) __RENAME(strcat);
61
extern char	*__strncat_chk(char *__restrict, const char *__restrict, size_t, size_t);
62
extern char	*__strncat_real(char *__restrict, const char *__restrict, size_t) __RENAME(strncat);
63
extern char	*__stpcpy_chk(char *, const char *, size_t);
64
extern char	*__stpcpy_real(char *, const char *) __RENAME(stpcpy);
65
extern char	*__stpncpy_chk(char * __restrict, const char * __restrict, size_t, size_t);
66
extern char	*__stpncpy_chk2(char * __restrict, const char * __restrict, size_t, size_t, size_t);
67
extern char	*__stpncpy_real(char * __restrict, const char * __restrict, size_t) __RENAME(stpncpy);
68
extern char	*__strcpy_chk(char *, const char *, size_t);
69
extern char	*__strcpy_real(char *, const char *) __RENAME(strcpy);
70
extern char	*__strncpy_chk(char *, const char *, size_t, size_t);
71
extern char	*__strncpy_chk2(char * __restrict, const char * __restrict, size_t, size_t, size_t);
72
extern char	*__strncpy_real(char *, const char *, size_t) __RENAME(strncpy);
73
extern size_t	 __strlcpy_chk(char *, const char *, size_t, size_t);
74
extern size_t	 __strlcpy_real(char * __restrict, const char * __restrict, size_t) __RENAME(strlcpy);
75
extern size_t	 __strlcat_chk(char * __restrict, const char * __restrict, size_t, size_t);
76
extern size_t	 __strlcat_real(char * __restrict, const char * __restrict, size_t) __RENAME(strlcat);
77
extern size_t	 __strlen_chk(const char *, size_t);
78
extern size_t	 __strlen_real(const char *) __RENAME(strlen);
79
extern char	*__strchr_chk(const char *, int, size_t);
80
extern char	*__strchr_real(const char *, int) __RENAME(strchr);
81
extern char	*__strchrnul_chk(const char *, int, size_t);
82
extern char	*__strchrnul_real(const char *, int) __RENAME(strchrnul);
83
extern char	*__strrchr_chk(const char *, int, size_t);
84
extern char	*__strrchr_real(const char *, int) __RENAME(strrchr);
85
86
#ifdef __BSD_FORTIFY
87
88
#if __XSI_VISIBLE >= 600
89
__FORTIFY_INLINE void *
90
memccpy(void * __restrict _d, const void * __restrict _s, int _c, size_t _n)
91
{
92
	size_t _bos = __bos0(_d);
93
94
#ifndef __clang__
95
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
96
		return (__memccpy_real(_d, _s, _c, _n));
97
#endif
98
99
	return (__memccpy_chk(_d, _s, _c, _n, _bos));
100
}
101
#endif /* __XSI_VISIBLE */
102
103
104
__FORTIFY_INLINE void *
105
memchr(const void *_s, int _c, size_t _n)
106
{
107
	size_t _bos = __bos(_s);
108
109
#ifndef __clang__
110
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
111
		return (__memchr_real(_s, _c, _n));
112
113
	if (__builtin_constant_p(_n) && (_n > _bos))
114
		__memchr_buf_size_error();
115
116
	/*
117
	 * Compiler can prove, at compile time, that the passed in size
118
	 * is always <= the actual object size. Don't call __memchr_chk.
119
	 */
120
	if (__builtin_constant_p(_n) && (_n <= _bos))
121
		return (__memchr_real(_s, _c, _n));
122
#endif
123
124
	return (__memchr_chk(_s, _c, _n, _bos));
125
}
126
127
128
#if __BSD_VISIBLE
129
__FORTIFY_INLINE void *
130
memrchr(const void *_s, int _c, size_t _n)
131
{
132
	size_t _bos = __bos(_s);
133
134
#ifndef __clang__
135
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
136
		return (__memrchr_real(_s, _c, _n));
137
138
	if (__builtin_constant_p(_n) && (_n > _bos))
139
		(__memrchr_buf_size_error());
140
141
	if (__builtin_constant_p(_n) && (_n <= _bos))
142
		return __memrchr_real(_s, _c, _n);
143
#endif
144
145
	return (__memrchr_chk(_s, _c, _n, _bos));
146
}
147
#endif /* __BSD_VISIBLE */
148
149
150
__FORTIFY_INLINE void *
151
memcpy(void * __restrict _d, const void * __restrict _s, size_t _n)
152
{
153
	size_t _bos = __bos0(_d);
154
155
#ifndef __clang__
156
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
157
		return (__memcpy_real(_d, _s, _n));
158
#endif
159
160
	return (__memcpy_chk(_d, _s, _n, _bos));
161
}
162
163
164
__FORTIFY_INLINE void *
165
memmove(void *_d, const void *_s, size_t _n)
166
{
167
	size_t _bos = __bos0(_d);
168
169
#ifndef __clang__
170
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
171
		return (__memmove_real(_d, _s, _n));
172
#endif
173
174
	return (__memmove_chk(_d, _s, _n, _bos));
175
}
176
177
178
#if __POSIX_VISIBLE >= 200809
179
__FORTIFY_INLINE char *
180
stpcpy(char * __restrict _d, const char * __restrict _s)
181
{
182
	size_t _bos = __bos(_d);
183
184
#ifndef __clang__
185
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
186
		return (__stpcpy_real(_d, _s));
187
#endif
188
189
	return (__stpcpy_chk(_d, _s, _bos));
190
}
191
#endif /* __POSIX_VISIBLE */
192
193
194
__FORTIFY_INLINE char *
195
strcpy(char * __restrict _d, const char * __restrict _s)
196
{
197
	size_t _bos = __bos(_d);
198
199
#ifndef __clang__
200
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
201
		return (__strcpy_real(_d, _s));
202
#endif
203
204
	return (__strcpy_chk(_d, _s, _bos));
205
}
206
207
208
#if __POSIX_VISIBLE >= 200809
209
__FORTIFY_INLINE char *
210
stpncpy(char * __restrict _d, const char * __restrict _s, size_t _n)
211
{
212
	size_t _d_bos = __bos(_d);
213
	size_t _s_bos = __bos(_s);
214
#ifndef __clang__
215
	size_t _slen;
216
217
	if (_d_bos == __FORTIFY_UNKNOWN_SIZE)
218
		return (__stpncpy_real(_d, _s, _n));
219
220
	if (_s_bos == __FORTIFY_UNKNOWN_SIZE)
221
		return (__stpncpy_chk(_d, _s, _n, _d_bos));
222
223
	if (__builtin_constant_p(_n) && (_n <= _s_bos))
224
		return (__stpncpy_chk(_d, _s, _n, _d_bos));
225
226
	_slen = __builtin_strlen(_s);
227
	if (__builtin_constant_p(_slen))
228
		return (__stpncpy_chk(_d, _s, _n, _d_bos));
229
#endif
230
231
	return (__stpncpy_chk2(_d, _s, _n, _d_bos, _s_bos));
232
}
233
#endif /* __POSIX_VISIBLE */
234
235
236
__FORTIFY_INLINE char *
237
strncpy(char * __restrict _d, const char * __restrict _s, size_t _n)
238
{
239
	size_t _d_bos = __bos(_d);
240
	size_t _s_bos = __bos(_s);
241
#ifndef __clang__
242
	size_t _slen;
243
244
	if (_d_bos == __FORTIFY_UNKNOWN_SIZE)
245
		return (__strncpy_real(_d, _s, _n));
246
247
	if (_s_bos == __FORTIFY_UNKNOWN_SIZE)
248
		return (__strncpy_chk(_d, _s, _n, _d_bos));
249
250
	if (__builtin_constant_p(_n) && (_n <= _s_bos))
251
		return (__strncpy_chk(_d, _s, _n, _d_bos));
252
253
	_slen = __builtin_strlen(_s);
254
	if (__builtin_constant_p(_slen))
255
		return (__strncpy_chk(_d, _s, _n, _d_bos));
256
#endif
257
258
	return (__strncpy_chk2(_d, _s, _n, _d_bos, _s_bos));
259
}
260
261
262
__FORTIFY_INLINE char *
263
strcat(char * __restrict _d, const char * __restrict _s)
264
{
265
	size_t _bos = __bos(_d);
266
267
#ifndef __clang__
268
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
269
		return (__strcat_real(_d, _s));
270
#endif
271
272
	return (__strcat_chk(_d, _s, _bos));
273
}
274
275
276
__FORTIFY_INLINE char *
277
strncat(char * __restrict _d, const char * __restrict _s, size_t _n)
278
{
279
	size_t _bos = __bos(_d);
280
281
#ifndef __clang__
282
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
283
		return (__strncat_real(_d, _s, _n));
284
#endif
285
286
	return (__strncat_chk(_d, _s, _n, _bos));
287
}
288
289
290
__FORTIFY_INLINE void *
291
memset(void *_s, int _c, size_t _n)
292
{
293
	size_t _bos = __bos(_s);
294
295
#ifndef __clang__
296
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
297
		return (__memset_real(_s, _c, _n));
298
#endif
299
300
	return (__memset_chk(_s, _c, _n, _bos));
301
}
302
303
304
#if __BSD_VISIBLE
305
__FORTIFY_INLINE size_t
306
strlcpy(char * __restrict _d, const char * __restrict _s, size_t _n)
307
{
308
	size_t _bos = __bos(_d);
309
310
#ifndef __clang__
311
	/* Compiler doesn't know destination size. Don't call __strlcpy_chk. */
312
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
313
		return (__strlcpy_real(_d, _s, _n));
314
315
	/*
316
	 * Compiler can prove, at compile time, that the passed in size
317
	 * is always <= the actual object size. Don't call __strlcpy_chk.
318
	 */
319
	if (__builtin_constant_p(_n) && (_n <= _bos))
320
		return (__strlcpy_real(_d, _s, _n));
321
#endif
322
323
	return (__strlcpy_chk(_d, _s, _n, _bos));
324
}
325
#endif /* __BSD_VISIBLE */
326
327
328
#if __BSD_VISIBLE
329
__FORTIFY_INLINE size_t
330
strlcat(char * __restrict _d, const char * __restrict _s, size_t _n)
331
{
332
	size_t _bos = __bos(_d);
333
334
#ifndef __clang__
335
	/* Compiler doesn't know destination size. Don't call __strlcat_chk. */
336
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
337
		return (__strlcat_real(_d, _s, _n));
338
339
	/*
340
	 * Compiler can prove, at compile time, that the passed in size
341
	 * is always <= the actual object size. Don't call __strlcat_chk.
342
	 */
343
	if (__builtin_constant_p(_n) && (_n <= _bos))
344
		return (__strlcat_real(_d, _s, _n));
345
#endif
346
347
	return (__strlcat_chk(_d, _s, _n, _bos));
348
}
349
#endif /* __BSD_VISIBLE */
350
351
352
__FORTIFY_INLINE size_t
353
strlen(const char *_s)
354
{
355
	size_t _bos = __bos(_s);
356
#ifndef __clang__
357
	size_t _slen;
358
359
	/* Compiler doesn't know destination size. Don't call __strlen_chk. */
360
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
361
		return (__strlen_real(_s));
362
363
	_slen = __builtin_strlen(_s);
364
	if (__builtin_constant_p(_slen))
365
		return (_slen);
366
#endif
367
368
	return (__strlen_chk(_s, _bos));
369
}
370
371
__FORTIFY_INLINE char *
372
strchr(const char *_s, int _c)
373
{
374
	size_t _bos = __bos(_s);
375
#ifndef __clang__
376
	size_t _slen;
377
378
	/* Compiler doesn't know destination size. Don't call __strchr_chk. */
379
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
380
		return (__strchr_real(_s, _c));
381
382
	/*
383
	 * Compiler can prove, at compile time, that the passed in size
384
	 * is always <= the actual object size. Don't call __strlchr_chk.
385
	 */
386
	_slen = __builtin_strlen(_s);
387
	if (__builtin_constant_p(_slen) && (_slen < _bos))
388
		return (__strchr_real(_s, _c));
389
#endif
390
391
	return (__strchr_chk(_s, _c, _bos));
392
}
393
394
395
#if __BSD_VISIBLE
396
__FORTIFY_INLINE char *
397
strchrnul(const char *_s, int _c)
398
{
399
	size_t _bos = __bos(_s);
400
#ifndef __clang__
401
	size_t _slen;
402
403
	/* Compiler doesn't know destination size. Don't call __strchr_chk. */
404
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
405
		return (__strchrnul_real(_s, _c));
406
	/*
407
	 * Compiler can prove, at compile time, that the passed in size
408
	 * is always <= the actual object size. Don't call __strlchrnul_chk.
409
	 */
410
	_slen = __builtin_strlen(_s);
411
	if (__builtin_constant_p(_slen) && (_slen < _bos))
412
		return (__strchrnul_real(_s, _c));
413
#endif
414
415
	return (__strchrnul_chk(_s, _c, _bos));
416
}
417
#endif
418
419
420
__FORTIFY_INLINE char *
421
strrchr(const char *_s, int _c)
422
{
423
	size_t _bos = __bos(_s);
424
#ifndef __clang__
425
	size_t _slen;
426
427
	/* Compiler doesn't know destination size. Don't call __strrchr_chk. */
428
	if (__predict_false(_bos == __FORTIFY_UNKNOWN_SIZE))
429
		return (__strrchr_real(_s, _c));
430
431
	/*
432
	 * Compiler can prove, at compile time, that the passed in size
433
	 * is always <= the actual object size. Don't call __strlen_chk.
434
	 */
435
	_slen = __strlen_real(_s);
436
	if (__builtin_constant_p(_slen) && (_slen < _bos))
437
		return (__strrchr_real(_s, _c));
438
#endif
439
440
	return (__strrchr_chk(_s, _c, _bos));
441
}
442
443
444
#endif /* defined(__BSD_FORTIFY) */
445
446
__END_DECLS
447
448
#endif /* !_SECURE_STRING_H */
(-)include/secure/_strings.h (+112 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 Olivér Pintér
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * $FreeBSD$
29
 */
30
31
#ifndef _STRINGS_H_
32
#error "You should not use <secure/_strings.h> directly; include <sys/strings.h> instead."
33
#endif
34
35
#ifndef _SECURE_STRINGS_H_
36
#define _SECURE_STRINGS_H_
37
38
#include <secure/security.h>
39
40
__BEGIN_DECLS
41
42
extern void	*__bcopy_chk(void *, const void *, size_t, size_t) __RENAME(__memmove_chk);
43
extern void	 __bcopy_real(const void *, void *, size_t) __RENAME(bcopy);
44
extern void	*__bzero_chk(void *, int, size_t, size_t) __RENAME(__memset_chk);
45
extern void	 __bzero_real(void *, size_t) __RENAME(bzero);
46
extern char	*__rindex_chk(const char *, int, size_t);
47
extern char	*__rindex_real(const char *, int) __RENAME(rindex);
48
49
#ifdef __BSD_FORTIFY
50
#if __BSD_VISIBLE || __POSIX_VISIBLE <= 200112
51
__FORTIFY_INLINE void
52
bcopy(const void *_s, void *_d, size_t _l)
53
{
54
	size_t _bos = __bos0(_d);
55
56
#ifndef __clang__
57
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
58
		__bcopy_real(_s, _d, _l);
59
#endif
60
61
	(void)(__bcopy_chk(_d, _s, _l, _bos));
62
}
63
#endif
64
65
66
#if __BSD_VISIBLE || __POSIX_VISIBLE <= 200112
67
__FORTIFY_INLINE void
68
bzero(void *_s, size_t _n)
69
{
70
	size_t _bos = __bos(_s);
71
72
#ifndef __clang__
73
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
74
		__bzero_real(_s, _n);
75
#endif
76
77
	(void)(__bzero_chk(_s, '\0', _n, _bos));
78
}
79
#endif
80
81
82
#if __BSD_VISIBLE || __POSIX_VISIBLE <= 200112
83
__FORTIFY_INLINE char *
84
rindex(const char *_s, int _c)
85
{
86
	size_t _bos = __bos(_s);
87
#ifndef __clang__
88
	size_t _slen;
89
90
	/* Compiler doesn't know destination size. Don't call __strrchr_chk. */
91
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
92
		return (__rindex_real(_s, _c));
93
94
	/*
95
	 * Compiler can prove, at compile time, that the passed in size
96
	 * is always <= the actual object size. Don't call __rindex_chk.
97
	 */
98
	_slen = __builtin_strlen(_s);
99
	if (__builtin_constant_p(_slen) && (_slen < _bos))
100
		return (__rindex_real(_s, _c));
101
#endif
102
103
	return (__rindex_chk(_s, _c, _bos));
104
}
105
#endif
106
107
108
#endif /* !__BSD_FORTIFY */
109
110
__END_DECLS
111
112
#endif /* !defined(_SECURE_STRINGS_H_) */
(-)include/secure/_unistd.h (+200 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 2015 Olivér Pintér
3
 * Copyright (C) 2008 The Android Open Source Project
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *  * Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 *  * Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in
13
 *    the documentation and/or other materials provided with the
14
 *    distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * rev: 9ef26a3c4cd2e6d469f771815a07cb820800beb6
30
 *
31
 * $FreeBSD$
32
 */
33
34
#ifndef _UNISTD_H_
35
#error "You should not use <secure/_unistd.h> directly; include <unistd.h> instead."
36
#endif
37
38
#ifndef _SECURE_UNISTD_H_
39
#define	_SECURE_UNISTD_H_
40
41
#include <sys/limits.h>
42
#include <secure/security.h>
43
44
__BEGIN_DECLS
45
46
extern char	*__getcwd_chk(char*, size_t, size_t);
47
extern char	*__getcwd_real(char*, size_t) __RENAME(getcwd);
48
__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
49
extern ssize_t	 __pread_chk(int, void *, size_t, off_t, size_t);
50
extern ssize_t	 __pread_real(int, void *, size_t, off_t) __RENAME(pread);
51
__errordecl(__pread_dest_size_error, "pread called with size bigger than destination");
52
__errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX");
53
extern ssize_t	 __read_chk(int, void *, size_t, size_t);
54
extern ssize_t	 __read_real(int, void *, size_t) __RENAME(read);
55
__errordecl(__read_dest_size_error, "read called with size bigger than destination");
56
__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
57
extern ssize_t	 __readlink_chk(const char *, char *, size_t, size_t);
58
extern ssize_t	 __readlink_real(const char *, char *, size_t) __RENAME(readlink);
59
__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
60
__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
61
extern ssize_t	 __readlinkat_chk(int, const char *, char *, size_t, size_t);
62
extern ssize_t	 __readlinkat_real(int, const char *, char *, size_t) __RENAME(readlinkat);
63
__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
64
__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
65
66
#ifdef __BSD_FORTIFY
67
68
__FORTIFY_INLINE
69
char *
70
getcwd(char *_buf, size_t _size)
71
{
72
	size_t	_bos = __bos(_buf);
73
74
#ifdef __clang__
75
	/*
76
	 * Work around LLVM's incorrect __builtin_object_size implementation
77
	 * here to avoid needing the workaround in the __getcwd_chk ABI
78
	 * forever.
79
	 * 
80
	 * https://llvm.org/bugs/show_bug.cgi?id=23277
81
	 */
82
	if (_buf == NULL)
83
		_bos = __FORTIFY_UNKNOWN_SIZE;
84
#else
85
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
86
		return (__getcwd_real(_buf, _size));
87
	if (__builtin_constant_p(_size) && (_size > _bos))
88
		__getcwd_dest_size_error();
89
	if (__builtin_constant_p(_size) && (_size <= _bos))
90
		return (__getcwd_real(_buf, _size));
91
#endif
92
93
	return (__getcwd_chk(_buf, _size, _bos));
94
}
95
96
97
/* 1003.1-2008 */
98
#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
99
__FORTIFY_INLINE ssize_t
100
pread(int _fd, void *_buf, size_t _count, off_t _offset)
101
{
102
	size_t _bos = __bos0(_buf);
103
104
#ifndef __clang__
105
	if (__builtin_constant_p(_count) && (_count > SSIZE_MAX))
106
		__pread_count_toobig_error();
107
108
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
109
		return (__pread_real(_fd, _buf, _count, _offset));
110
111
	if (__builtin_constant_p(_count) && (_count > _bos))
112
		__pread_dest_size_error();
113
114
	if (__builtin_constant_p(_count) && (_count <= _bos))
115
		return (__pread_real(_fd, _buf, _count, _offset));
116
#endif
117
118
	return (__pread_chk(_fd, _buf, _count, _offset, _bos));
119
}
120
#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
121
122
123
__FORTIFY_INLINE ssize_t
124
read(int _fd, void *_buf, size_t _count)
125
{
126
	size_t _bos = __bos0(_buf);
127
128
#ifndef __clang__
129
	if (__builtin_constant_p(_count) && (_count > SSIZE_MAX))
130
		__read_count_toobig_error();
131
132
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
133
		return (__read_real(_fd, _buf, _count));
134
135
	if (__builtin_constant_p(_count) && (_count > _bos))
136
		__read_dest_size_error();
137
138
	if (__builtin_constant_p(_count) && (_count <= _bos))
139
		return (__read_real(_fd, _buf, _count));
140
#endif
141
142
	return (__read_chk(_fd, _buf, _count, _bos));
143
}
144
145
146
/* 1003.1-2001 */
147
#if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE
148
__FORTIFY_INLINE ssize_t
149
readlink(const char *_path, char *_buf, size_t _size)
150
{
151
	size_t _bos = __bos(_buf);
152
153
#ifndef __clang__
154
	if (__builtin_constant_p(_size) && (_size > SSIZE_MAX))
155
		__readlink_size_toobig_error();
156
157
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
158
		return (__readlink_real(_path, _buf, _size));
159
160
	if (__builtin_constant_p(_size) && (_size > _bos))
161
		__readlink_dest_size_error();
162
163
	if (__builtin_constant_p(_size) && (_size <= _bos))
164
		return (__readlink_real(_path, _buf, _size));
165
#endif
166
167
	return (__readlink_chk(_path, _buf, _size, _bos));
168
}
169
#endif /* __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE */
170
171
172
#if __POSIX_VISIBLE >= 200809
173
__FORTIFY_INLINE ssize_t
174
readlinkat(int _dirfd, const char *_path, char *_buf, size_t _size)
175
{
176
	size_t _bos = __bos(_buf);
177
178
#ifndef __clang__
179
	if (__builtin_constant_p(_size) && (_size > SSIZE_MAX))
180
		(__readlinkat_size_toobig_error());
181
182
	if (_bos == __FORTIFY_UNKNOWN_SIZE)
183
		return (__readlinkat_real(_dirfd, _path, _buf, _size));
184
185
	if (__builtin_constant_p(_size) && (_size > _bos))
186
		__readlinkat_dest_size_error();
187
188
	if (__builtin_constant_p(_size) && (_size <= _bos))
189
		return (__readlinkat_real(_dirfd, _path, _buf, _size));
190
#endif
191
192
	return (__readlinkat_chk(_dirfd, _path, _buf, _size, _bos));
193
}
194
#endif /* __POSIX_VISIBLE >= 200809 */
195
196
#endif	/* defined(__BSD_FORTIFY) */
197
198
__END_DECLS
199
200
#endif	/* !_SECURE_UNISTD_H_ */
(-)include/secure/security.h (+86 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 2015 Olivér Pintér
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 *
26
 * $FreeBSD$
27
 */
28
29
#ifndef _SECURE_SECURITY_
30
#define	_SECURE_SECURITY_
31
32
#include <sys/cdefs.h>
33
#include <sys/types.h>
34
35
#ifndef __clang__
36
#if __GNUC_PREREQ__(4, 3)
37
#define	__errordecl(name, msg)	extern void name(void) __error_attr(msg)
38
#else
39
#define	__errordecl(name, msg)		\
40
static void name(void) __dead2;		\
41
static void name(void)			\
42
{					\
43
					\
44
	__fortify_chk_fail(msg);	\
45
}
46
#endif	/* __GNUC_PREREQ(4, 3) */
47
#else	/* !__clang__ */
48
#define	__errordecl(name, msg)
49
#endif	/* !__clang__ */
50
51
#define	__RENAME(x)	__asm__(#x)
52
53
#if __has_builtin(__builtin_umul_overflow) || __GNUC_PREREQ__(5, 0)
54
#if __LP64__
55
#define	__size_mul_overflow(a, b, result)	__builtin_umull_overflow(a, b, result)
56
#else
57
#define	__size_mul_overflow(a, b, result)	__builtin_umul_overflow(a, b, result)
58
#endif
59
#else
60
static __inline __always_inline int
61
__size_mul_overflow(__SIZE_TYPE__ a, __SIZE_TYPE__ b, __SIZE_TYPE__ *result)
62
{
63
    static const __SIZE_TYPE__ mul_no_overflow = 1UL << (sizeof(__SIZE_TYPE__) * 4);
64
65
    *result = a * b;
66
67
    return (a >= mul_no_overflow || b >= mul_no_overflow) && a > 0 && (__SIZE_TYPE__)-1 / a < b;
68
}
69
#endif
70
71
__BEGIN_DECLS
72
73
/* Common fail function. */
74
void	__secure_fail(const char *msg) __dead2 __nonnull(1);
75
76
/* SSP related fail functions. */
77
void	__chk_fail(void) __dead2;
78
void	__stack_chk_fail(void) __dead2;
79
80
/* FORTIFY_SOURCE related fail function. */
81
void	__fortify_chk_fail(const char* msg) __dead2 __nonnull(1);
82
int	__fortify_chk_overlap(const void *a, const void *b, size_t len);
83
84
__END_DECLS
85
86
#endif /* !_SECURE_SECURITY_ */
(-)include/stdio.h (+5 lines)
Lines 521-524 Link Here
521
#endif /* __cplusplus */
521
#endif /* __cplusplus */
522
522
523
__END_DECLS
523
__END_DECLS
524
525
#if defined(__BSD_FORTIFY)
526
#include <secure/_stdio.h>
527
#endif
528
524
#endif /* !_STDIO_H_ */
529
#endif /* !_STDIO_H_ */
(-)include/string.h (+4 lines)
Lines 141-144 Link Here
141
#endif
141
#endif
142
__END_DECLS
142
__END_DECLS
143
143
144
#if defined(__BSD_FORTIFY)
145
#include <secure/_string.h>
146
#endif
147
144
#endif /* _STRING_H_ */
148
#endif /* _STRING_H_ */
(-)include/strings.h (+4 lines)
Lines 68-71 Link Here
68
#endif
68
#endif
69
__END_DECLS
69
__END_DECLS
70
70
71
#if defined(__BSD_FORTIFY)
72
#include <secure/_strings.h>
73
#endif
74
71
#endif /* _STRINGS_H_ */
75
#endif /* _STRINGS_H_ */
(-)include/unistd.h (+4 lines)
Lines 589-592 Link Here
589
#endif /* __BSD_VISIBLE */
589
#endif /* __BSD_VISIBLE */
590
__END_DECLS
590
__END_DECLS
591
591
592
#if defined(__BSD_FORTIFY)
593
#include <secure/_unistd.h>
594
#endif
595
592
#endif /* !_UNISTD_H_ */
596
#endif /* !_UNISTD_H_ */
(-)lib/libc/gen/getcwd.c (+2 lines)
Lines 30-35 Link Here
30
#if defined(LIBC_SCCS) && !defined(lint)
30
#if defined(LIBC_SCCS) && !defined(lint)
31
static char sccsid[] = "@(#)getcwd.c	8.5 (Berkeley) 2/7/95";
31
static char sccsid[] = "@(#)getcwd.c	8.5 (Berkeley) 2/7/95";
32
#endif /* LIBC_SCCS and not lint */
32
#endif /* LIBC_SCCS and not lint */
33
34
#undef _FORTIFY_SOURCE
33
#include <sys/cdefs.h>
35
#include <sys/cdefs.h>
34
__FBSDID("$FreeBSD$");
36
__FBSDID("$FreeBSD$");
35
37
(-)lib/libc/secure/Makefile.inc (+42 lines)
Lines 6-12 Link Here
6
6
7
# Sources common to both syscall interfaces:
7
# Sources common to both syscall interfaces:
8
SRCS+=	\
8
SRCS+=	\
9
	fortify_source.c \
10
	secure_common.c \
9
	stack_protector.c \
11
	stack_protector.c \
10
	stack_protector_compat.c
12
	stack_protector_compat.c
11
13
14
# Sources which contains FORTIFY_SOURCE functions:
15
SRCS+=	\
16
	__fgets_chk.c \
17
	__fread_chk.c \
18
	__fwrite_chk.c \
19
	__getcwd_chk.c \
20
	__memccpy_chk.c \
21
	__memchr_chk.c \
22
	__memcpy_chk.c \
23
	__memmove_chk.c \
24
	__memrchr_chk.c \
25
	__memset_chk.c \
26
	__pread_chk.c \
27
	__read_chk.c \
28
	__readlink_chk.c \
29
	__readlinkat_chk.c \
30
	__stpcpy_chk.c \
31
	__stpncpy_chk.c \
32
	__strcat_chk.c \
33
	__strchr_chk.c \
34
	__strchrnul_chk.c \
35
	__strcpy_chk.c \
36
	__strlcat_chk.c \
37
	__strlcpy_chk.c \
38
	__strlen_chk.c \
39
	__strncat_chk.c \
40
	__strncpy_chk.c \
41
	__strrchr_chk.c \
42
	__vsnprintf_chk.c \
43
	__vsprintf_chk.c
44
45
# Sources which contains FORTIFY_SOURCE functions,
46
# but live in .h files under sys/sys
47
SRCS+=	\
48
	__poll_chk.c \
49
	__recvfrom_chk.c \
50
	__umask_chk.c
51
12
SYM_MAPS+=    ${LIBC_SRCTOP}/secure/Symbol.map
52
SYM_MAPS+=    ${LIBC_SRCTOP}/secure/Symbol.map
53
54
MAN+=   __builtin_object_size.3
(-)lib/libc/secure/Symbol.map (+54 lines)
Lines 7-9 Link Here
7
	__stack_chk_fail;
7
	__stack_chk_fail;
8
	__stack_chk_guard;
8
	__stack_chk_guard;
9
};
9
};
10
11
FBSD_1.1 {
12
};
13
14
FBSD_1.2 {
15
};
16
17
FBSD_1.3 {
18
};
19
20
FBSD_1.4 {
21
	__fgets_chk;
22
	__fortify_chk_fail;
23
	__fread_chk;
24
	__fwrite_chk;
25
	__getcwd_chk;
26
	__memccpy_chk;
27
	__memchr_chk;
28
	__memcpy_chk;
29
	__memmove_chk;
30
	__memrchr_chk;
31
	__memset_chk;
32
	__poll_chk;
33
	__ppoll_chk;
34
	__pread_chk;
35
	__read_chk;
36
	__readlink_chk;
37
	__readlinkat_chk;
38
	__recvfrom_chk;
39
	__rindex_chk;
40
	__snprintf_chk;
41
	__sprintf_chk;
42
	__stpcpy_chk;
43
	__stpncpy_chk;
44
	__stpncpy_chk2;
45
	__strcat_chk;
46
	__strchr_chk;
47
	__strchrnul_chk;
48
	__strcpy_chk;
49
	__strlcat_chk;
50
	__strlcpy_chk;
51
	__strlen_chk;
52
	__strncat_chk;
53
	__strncpy_chk;
54
	__strncpy_chk2;
55
	__strrchr_chk;
56
	__umask_chk;
57
	__vsnprintf_chk;
58
	__vsprintf_chk;
59
	__secure_fail;
60
};
61
62
FBSDprivate_1.0 {
63
};
(-)lib/libc/secure/__builtin_object_size.3 (+101 lines)
Line 0 Link Here
1
.\"	$NetBSD: __builtin_object_size.3,v 1.10 2012/07/19 06:44:12 wiz Exp $
2
.\"
3
.\" Copyright (c) 2007 The NetBSD Foundation, Inc.
4
.\" All rights reserved.
5
.\"
6
.\" This code is derived from software contributed to The NetBSD Foundation
7
.\" by Christos Zoulas.
8
.\"
9
.\" Redistribution and use in source and binary forms, with or without
10
.\" modification, are permitted provided that the following conditions
11
.\" are met:
12
.\" 1. Redistributions of source code must retain the above copyright
13
.\"    notice, this list of conditions and the following disclaimer.
14
.\" 2. Redistributions in binary form must reproduce the above copyright
15
.\"    notice, this list of conditions and the following disclaimer in the
16
.\"    documentation and/or other materials provided with the distribution.
17
.\"
18
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
.\" POSSIBILITY OF SUCH DAMAGE.
29
.\"
30
.\"
31
.Dd July 18, 2012
32
.Dt __BUILTIN_OBJECT_SIZE 3
33
.Os
34
.Sh NAME
35
.Nm __builtin_object_size
36
.Nd return the size of the given object
37
.Sh SYNOPSIS
38
.Ft size_t
39
.Fn __builtin_object_size "void *ptr" "int type"
40
.Sh DESCRIPTION
41
The
42
.Fn __builtin_object_size
43
function is a
44
.Xr gcc 1
45
built-in function that returns the size of the
46
.Fa ptr
47
object if known at compile time and the object does not have any side
48
effects.
49
.Sh RETURN VALUES
50
If the size of the object is not known or it has side effects the
51
.Fn __builtin_object_size
52
function returns:
53
.Bl -tag -width (size_t)\-1 -offset indent
54
.It Dv (size_t)\-1
55
for
56
.Fa type
57
.Dv 0
58
and
59
.Dv 1 .
60
.It Dv (size_t)0
61
for
62
.Fa type
63
.Dv 2
64
and
65
.Dv 3 .
66
.El
67
.Pp
68
If the size of the object is known, then the
69
.Fn __builtin_object_size
70
function returns the maximum size of all the objects that the compiler
71
knows that they can be pointed to by
72
.Fa ptr
73
when
74
.Fa type
75
.Dv \*[Am] 2 == 0 ,
76
and the minimum size when
77
.Fa type
78
.Dv \*[Am] 2 != 0 .
79
.Sh SEE ALSO
80
.Xr gcc 1 ,
81
.Xr __builtin_return_address 3 ,
82
.Xr attribute 3 ,
83
.Xr ssp 3
84
.Sh HISTORY
85
The
86
.Fn __builtin_object_size
87
appeared in
88
.Tn GCC 4.1 .
89
.Sh CAVEATS
90
This is a non-standard, compiler-specific extension.
91
.Pp
92
Note that currently the object size calculation pass is only done at -O1
93
or above, meaning that this function always returns \-1 when the optimizer
94
is off.
95
.Pp
96
There are some discussions about always doing the object size pass, but
97
the issue is that without the optimization pass data sizes are not going
98
to be correct.
99
.Pp
100
For that reason currently code fortification (size-checked replacement
101
functions) is disabled when optimization is off.
(-)lib/libc/secure/__fgets_chk.c (+56 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: 6cc98af72b0c48c58b2ab5fdb5f7abb842175299 
29
 *
30
 * $FreeBSD$
31
 */
32
33
34
#undef _FORTIFY_SOURCE
35
36
#include <sys/cdefs.h>
37
#include <secure/security.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include "secure/_stdio.h"
41
42
char *
43
__fgets_chk(char *buf, int n, FILE *stream, size_t bos)
44
{
45
46
	if (bos == __FORTIFY_UNKNOWN_SIZE)
47
		return (fgets(buf, n, stream));
48
49
	if (n < 0)
50
		__fortify_chk_fail("fgets: buffer size < 0");
51
52
	if (((size_t)n) > bos)
53
		__fortify_chk_fail("fgets: prevented write past end of buffer");
54
55
	return (fgets(buf, n, stream));
56
}
(-)lib/libc/secure/__fread_chk.c (+56 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: fed2659869ec41a93f655be8058568ddab419e01
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <stdio.h>
38
#include "secure/_stdio.h"
39
40
size_t
41
__fread_chk(void *__restrict ptr, size_t size, size_t nmemb, FILE * __restrict stream, size_t bos)
42
{
43
	size_t total;
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (fread(ptr, size, nmemb, stream));
47
48
	if (__predict_false(__size_mul_overflow(size, nmemb, &total))) {
49
		//overflow: trigger the error path in fread
50
		return (fread(ptr, size, nmemb, stream));
51
	}
52
	if (__predict_false(total > bos))
53
		__fortify_chk_fail("fread: prevented write past end of buffer");
54
55
	return (fread(ptr, size, nmemb, stream));
56
}
(-)lib/libc/secure/__fwrite_chk.c (+56 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: fed2659869ec41a93f655be8058568ddab419e01
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
#include <sys/cdefs.h>
35
#include <secure/security.h>
36
#include <stdio.h>
37
#include "secure/_stdio.h"
38
39
size_t
40
__fwrite_chk(const void * __restrict ptr, size_t size, size_t nmemb, FILE * __restrict stream, size_t bos)
41
{
42
	size_t total;
43
44
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
45
		return (fwrite(ptr, size, nmemb, stream));
46
47
	if (__predict_false(__size_mul_overflow(size, nmemb, &total))) {
48
		// overflow: trigger the error path in fwrite
49
		return (fwrite(ptr, size, nmemb, stream));
50
	}
51
52
	if (__predict_false(total > bos))
53
		__fortify_chk_fail("fwrite: prevented read past end of buffer");
54
55
	return (fwrite(ptr, size, nmemb, stream));
56
}
(-)lib/libc/secure/__getcwd_chk.c (+51 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: 7e919daeaad62515ebbbf7b06badc77625a14d90
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <unistd.h>
38
#include "secure/_unistd.h"
39
40
char *
41
__getcwd_chk(char* buf, size_t size, size_t bos)
42
{
43
44
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
45
		return (getcwd(buf, size));
46
47
	if (__predict_false(size > bos))
48
		__fortify_chk_fail("getcwd: prevented write past end of buffer");
49
50
	return (getcwd(buf, size));
51
}
(-)lib/libc/secure/__memccpy_chk.c (+66 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 Oliver Pinter
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * $FreeBSD$
29
 */
30
31
#undef _FORTIFY_SOURCE
32
33
#include <sys/cdefs.h>
34
#include <secure/security.h>
35
#include <string.h>
36
#include <stdlib.h>
37
#include "secure/_string.h"
38
39
void *
40
__memccpy_chk(void *dest, const void *src, int c, size_t n, size_t bos)
41
{
42
	void *ret;
43
	size_t len;
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (memccpy(dest, src, c, n));
47
48
	if (__predict_false(n > bos))
49
		__fortify_chk_fail("memccpy: prevented write past end of buffer");
50
51
	/*
52
	 * If n was copied, then return NULL, otherwise
53
	 * a pointer to the byte after the copy of c in the string
54
	 * dest is returned.
55
	 *
56
	 * See the memccpy(3) manpage for more details.
57
	 */
58
	ret = memccpy(dest, src, c, n);
59
	if (ret != NULL)
60
		len = ret - dest;
61
62
	if (__predict_false(__fortify_chk_overlap(dest, src, len)))
63
		__fortify_chk_fail("memccpy: prevented overlapping strings");
64
65
	return (ret);
66
}
(-)lib/libc/secure/__memchr_chk.c (+52 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: 6cc98af72b0c48c58b2ab5fdb5f7abb842175299
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include "secure/_string.h"
39
40
void *
41
__memchr_chk(const void *s, int c, size_t n, size_t bos)
42
{
43
44
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
45
		return (memchr(s, c, n));
46
47
	if (__predict_false(n > bos))
48
		__fortify_chk_fail(
49
		    "memchr: prevented read past end of buffer");
50
51
	return (memchr(s, c, n));
52
}
(-)lib/libc/secure/__memcpy_chk.c (+57 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 Oliver Pinter
3
 * Copyright (C) 2012 The Android Open Source Project
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *  * Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 *  * Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in
13
 *    the documentation and/or other materials provided with the
14
 *    distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * bionic rev: 6cc98af72b0c48c58b2ab5fdb5f7abb842175299
30
 *
31
 * $FreeBSD$
32
 */
33
34
#undef _FORTIFY_SOURCE
35
36
#include <sys/cdefs.h>
37
#include <secure/security.h>
38
#include <string.h>
39
#include <stdlib.h>
40
#include "secure/_string.h"
41
42
void *
43
__memcpy_chk(void *d, const void *s, size_t n, size_t bos)
44
{
45
46
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
47
		return (memcpy(d, s, n));
48
49
	if (__predict_false(n > bos))
50
		__fortify_chk_fail("memcpy: prevented write past end of buffer");
51
52
	/* See the memcpy(3) for more details. */
53
	if (__predict_false(__fortify_chk_overlap(d, s, n)))
54
		__fortify_chk_fail("memcpy: prevented overlaping strings");
55
56
	return (memcpy(d, s, n));
57
}
(-)lib/libc/secure/__memmove_chk.c (+56 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: 6cc98af72b0c48c58b2ab5fdb5f7abb842175299
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
void *
42
__memmove_chk(void *d, const void *s, size_t n, size_t bos)
43
{
44
45
	/*
46
	 * Compiler doesn 't know dination size.
47
	 * Fallback to the original function.
48
	 */
49
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
50
		return (memmove(d, s, n));
51
52
	if (__predict_false(n > bos))
53
		__fortify_chk_fail("memmove: prevented write past end of buffer");
54
55
	return (memmove(d, s, n));
56
}
(-)lib/libc/secure/__memrchr_chk.c (+51 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: 6cc98af72b0c48c58b2ab5fdb5f7abb842175299
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include "secure/_string.h"
39
40
void *
41
__memrchr_chk(const void *s, int c, size_t n, size_t bos)
42
{
43
44
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
45
		return (memrchr(s, c, n));
46
47
	if (__predict_false(n > bos))
48
		__fortify_chk_fail("memrchr: prevented read past end of buffer");
49
50
	return (memrchr(s, c, n));
51
}
(-)lib/libc/secure/__memset_chk.c (+52 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: 6cc98af72b0c48c58b2ab5fdb5f7abb842175299
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
void *
42
__memset_chk(void *d, int c, size_t n, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return memset(d, c, n);
47
48
	if (__predict_false(n > bos))
49
		__fortify_chk_fail("memset: prevented write past end of buffer");
50
51
	return memset(d, c, n);
52
}
(-)lib/libc/secure/__poll_chk.c (+64 lines)
Line 0 Link Here
1
/*
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: eeb9f5e41662828989f3913d81ec23229a668434
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <sys/poll.h>
38
#include "secure/_poll.h"
39
40
int
41
__poll_chk(struct pollfd *fds, nfds_t fd_count, int timeout, size_t bos)
42
{
43
44
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
45
		return (poll(fds, fd_count, timeout));
46
47
	if (__predict_false(bos / sizeof(*fds) < fd_count))
48
		__fortify_chk_fail("poll: pollfd array smaller than fd count");
49
50
	return (poll(fds, fd_count, timeout));
51
}
52
53
int
54
__ppoll_chk(struct pollfd *fds, nfds_t fd_count, const struct timespec *timeout, const sigset_t *mask, size_t bos)
55
{
56
57
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
58
		return (ppoll(fds, fd_count, timeout, mask));
59
60
	if (__predict_false(bos / sizeof(*fds) < fd_count))
61
		__fortify_chk_fail("ppoll: pollfd array smaller than fd count");
62
63
	return (ppoll(fds, fd_count, timeout, mask));
64
}
(-)lib/libc/secure/__pread_chk.c (+55 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: a8993c994e45ec2dc00dcef15910560e22d67be9
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <sys/limits.h>
37
#include <secure/security.h>
38
#include <unistd.h>
39
#include "secure/_unistd.h"
40
41
ssize_t
42
__pread_chk(int fd, void *buf, size_t count, off_t offset, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (pread(fd, buf, count, offset));
47
48
	if (__predict_false(count > bos))
49
		__fortify_chk_fail("pread: prevented write past end of buffer");
50
51
	if (__predict_false(count > SSIZE_MAX))
52
		__fortify_chk_fail("pread: count > SSIZE_MAX");
53
54
	return (pread(fd, buf, count, offset));
55
}
(-)lib/libc/secure/__read_chk.c (+55 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2013 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * rev: a8993c994e45ec2dc00dcef15910560e22d67be9
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <sys/limits.h>
37
#include <secure/security.h>
38
#include <unistd.h>
39
#include "secure/_unistd.h"
40
41
ssize_t
42
__read_chk(int fd, void *buf, size_t count, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (read(fd, buf, count));
47
48
	if (__predict_false(count > bos))
49
		__fortify_chk_fail("read: prevented write past end of buffer");
50
51
	if (__predict_false(count > SSIZE_MAX))
52
		__fortify_chk_fail("read: count > SSIZE_MAX");
53
54
	return (read(fd, buf, count));
55
}
(-)lib/libc/secure/__readlink_chk.c (+55 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: a8993c994e45ec2dc00dcef15910560e22d67be9
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <sys/limits.h>
37
#include <secure/security.h>
38
#include <unistd.h>
39
#include "secure/_unistd.h"
40
41
ssize_t
42
__readlink_chk(const char *path, char *buf, size_t size, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (readlink(path, buf, size));
47
48
	if (__predict_false(size > bos))
49
		__fortify_chk_fail("readlink: prevented write past end of buffer");
50
51
	if (__predict_false(size > SSIZE_MAX))
52
		__fortify_chk_fail("readlink: size > SSIZE_MAX");
53
54
	return (readlink(path, buf, size));
55
}
(-)lib/libc/secure/__readlinkat_chk.c (+55 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2015 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: a8993c994e45ec2dc00dcef15910560e22d67be9
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <sys/limits.h>
37
#include <secure/security.h>
38
#include <unistd.h>
39
#include "secure/_unistd.h"
40
41
ssize_t
42
__readlinkat_chk(int dirfd, const char *path, char *buf, size_t size, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (readlinkat(dirfd, path, buf, size));
47
48
	if (__predict_false(size > bos))
49
		__fortify_chk_fail("readlinkat: prevented write past end of buffer");
50
51
	if (__predict_false(size > SSIZE_MAX))
52
		__fortify_chk_fail("readlinkat: size > SSIZE_MAX");
53
54
	return (readlinkat(dirfd, path, buf, size));
55
}
(-)lib/libc/secure/__recvfrom_chk.c (+53 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2013 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: a8993c994e45ec2dc00dcef15910560e22d67be9
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <sys/socket.h>
37
#include <secure/security.h>
38
#include "secure/_socket.h"
39
40
ssize_t 
41
__recvfrom_chk(int s, void *buf, size_t len, size_t bos,
42
    int flags, struct sockaddr * __restrict from,
43
    socklen_t * __restrict fromlen)
44
{
45
46
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
47
		return (recvfrom(s, buf, len, flags, from, fromlen));
48
49
	if (__predict_false(len > bos))
50
		__fortify_chk_fail("recvfrom: prevented write past end of buffer");
51
52
	return (recvfrom(s, buf, len, flags, from, fromlen));
53
}
(-)lib/libc/secure/__stpcpy_chk.c (+59 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2014 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
char *
42
__stpcpy_chk(char *dest, const char *src, size_t bos)
43
{
44
	/* TODO:optimize so we don't scan src twice. */
45
	size_t src_len;
46
47
	/*
48
	 * Compiler doesn 't know destination size.
49
	 * Fallback to the original function.
50
	 */
51
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
52
		return (stpcpy(dest, src));
53
54
	src_len = strlen(src) + 1;
55
	if (__predict_false(src_len > bos))
56
		__fortify_chk_fail("stpcpy: prevented write past end of buffer");
57
58
	return (stpcpy(dest, src));
59
}
(-)lib/libc/secure/__stpncpy_chk.c (+98 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2014 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
34
#undef _FORTIFY_SOURCE
35
36
#include <sys/cdefs.h>
37
#include <secure/security.h>
38
#include <string.h>
39
#include <stdlib.h>
40
#include "secure/_string.h"
41
42
char *
43
__stpncpy_chk(char *__restrict d, const char *__restrict s, size_t n, size_t bos)
44
{
45
46
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
47
		return (stpncpy(d, s, n));
48
49
	if (__predict_false(n > bos))
50
		__fortify_chk_fail("stpncpy: prevented write past end of buffer");
51
52
	return stpncpy(d, s, n);
53
}
54
55
/*
56
 * __stpncpy_chk2
57
 *
58
 * This is a variant of __stpncpy_chk, but it also checks to make
59
 * sure we don't read beyond the end of "s". The code for this is
60
 * based on the original version of stpncpy, but modified to check
61
 * how much we read from "s" at the end of the copy operation.
62
 */
63
char *
64
__stpncpy_chk2(char *__restrict d, const char *__restrict s, size_t n, size_t d_bos, size_t s_bos)
65
{
66
	char *_d;
67
	const char *_s = s;
68
	size_t s_copy_len ;
69
70
	if (__predict_false(d_bos == __FORTIFY_UNKNOWN_SIZE))
71
		return (stpncpy(d, s, n));
72
73
	if (__predict_false(s_bos == __FORTIFY_UNKNOWN_SIZE))
74
		return (__stpncpy_chk(d, s, n, d_bos));
75
76
	if (__predict_false(n > d_bos))
77
		__fortify_chk_fail("stpncpy: prevented write past end of buffer");
78
79
	if (n != 0) {
80
		_d = d;
81
82
		do {
83
			if ((*_d++ = *_s++) == 0) {
84
				/* NUL pad the remaining n-1 bytes */
85
				while (--n != 0)
86
					*_d++ = 0;
87
				break;
88
			}
89
		} while (--n != 0);
90
91
		s_copy_len = (size_t)(_s - s);
92
93
		if (__predict_false(s_copy_len > s_bos))
94
			__fortify_chk_fail("stpncpy: prevented read past end of buffer");
95
	}
96
97
	return (d);
98
}
(-)lib/libc/secure/__strcat_chk.c (+66 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
char *
42
__strcat_chk(char *__restrict d, const char *__restrict s, size_t bos)
43
{
44
	char *save = d;
45
	size_t d_len;
46
47
	/*
48
	 * Compiler doesn 't know destination size.
49
	 * Fallback to the original function.
50
	 */
51
	if (bos == __FORTIFY_UNKNOWN_SIZE)
52
		return (strcat(d, s));
53
54
	d_len = __strlen_chk(d, bos);
55
56
	d += d_len;
57
	bos -= d_len;
58
59
	while ((*d++ = *s++) != '\0') {
60
		bos--;
61
		if (__predict_false(bos == 0))
62
			__fortify_chk_fail("strcat: prevented write past end of buffer");
63
	}
64
65
	return (save);
66
}
(-)lib/libc/secure/__strchr_chk.c (+56 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 1990 The Regents of the University of California.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the University nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
30
 *
31
 * $FreeBSD$
32
 */
33
34
#undef _FORTIFY_SOURCE
35
36
#include <sys/cdefs.h>
37
#include <secure/security.h>
38
#include <string.h>
39
#include "secure/_string.h"
40
41
char *
42
__strchr_chk(const char *p, int ch, size_t bos)
43
{
44
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
45
		return (strchr(p, ch));
46
47
	for (;; ++p, bos--) {
48
		if (__predict_false(bos == 0))
49
			__fortify_chk_fail("strchr: prevented read past end of buffer");
50
		if (*p == (char)(ch))
51
			return ((char *)(p));
52
		if (*p == '\0')
53
			return (NULL);
54
	}
55
	/* NOTREACHED */
56
}
(-)lib/libc/secure/__strchrnul_chk.c (+59 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 2015 Pinter Oliver
3
 * Copyright (c) 1990 The Regents of the University of California.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. Neither the name of the University nor the names of its contributors
15
 *    may be used to endorse or promote products derived from this software
16
 *    without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
31
 *
32
 * $FreeBSD$
33
 */
34
35
#undef _FORTIFY_SOURCE
36
37
#include <sys/cdefs.h>
38
#include <secure/security.h>
39
#include <string.h>
40
#include "secure/_string.h"
41
42
char *
43
__strchrnul_chk(const char *p, int ch, size_t bos)
44
{
45
46
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
47
		return (strchrnul(p, ch));
48
49
	for (;; ++p, bos--) {
50
		if (__predict_false(bos == 0))
51
			__fortify_chk_fail(
52
			    "strchrnul: prevented read past end of buffer");
53
		if (*p == (char)(ch))
54
			return ((char *)(p));
55
		if (*p == '\0')
56
			return ((char *)(p));
57
	}
58
	/* NOTREACHED */
59
}
(-)lib/libc/secure/__strcpy_chk.c (+59 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
char *
42
__strcpy_chk(char *d, const char *s, size_t bos)
43
{
44
	/* TODO: optimize so we don't scan s twice. */
45
	size_t s_len;
46
47
	/*
48
	 * Compiler doesn 't know destination size.
49
	 * Fallback to the original function.
50
	 */
51
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
52
		return (strcpy(d, s));
53
54
	s_len = strlen(s) + 1;
55
	if (__predict_false(s_len > bos))
56
		__fortify_chk_fail("strcpy: prevented write past end of buffer");
57
58
	return (strcpy(d, s));
59
}
(-)lib/libc/secure/__strlcat_chk.c (+52 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
size_t
42
__strlcat_chk(char *d, const char *s, size_t n, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (strlcat(d, s, n));
47
48
	if (__predict_false(n > bos))
49
		__fortify_chk_fail("strlcat: prevented write past end of buffer");
50
51
	return (strlcat(d, s, n));
52
}
(-)lib/libc/secure/__strlcpy_chk.c (+52 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
size_t
42
__strlcpy_chk(char *d, const char *s, size_t n, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (strlcpy(d, s, n));
47
48
	if (__predict_false(n > bos))
49
		__fortify_chk_fail("strlcpy: prevented write past end of buffer");
50
51
	return (strlcpy(d, s, n));
52
}
(-)lib/libc/secure/__strlen_chk.c (+67 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
/*
42
 * This test is designed to detect code such as:
43
 *
44
 * int main() {
45
 *   char buf[10];
46
 *   memcpy(buf, "1234567890", sizeof(buf));
47
 *   size_t len = strlen(buf); // segfault here with _FORTIFY_SOURCE
48
 *   printf("%d\n", len);
49
 *   return 0;
50
 * }
51
 *
52
 * or anytime strlen reads beyond an object boundary.
53
 */
54
size_t
55
__strlen_chk(const char *s, size_t bos)
56
{
57
	size_t ret;
58
59
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
60
		return (strlen(s));
61
62
	ret = strlen(s);
63
	if (__predict_false(ret >= bos))
64
		__fortify_chk_fail("strlen: prevented read past end of buffer");
65
66
	return (ret);
67
}
(-)lib/libc/secure/__strncat_chk.c (+70 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
char *
42
__strncat_chk(char *__restrict d, const char *__restrict s, size_t n, size_t bos)
43
{
44
	size_t d_len;
45
	char *_d;
46
47
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
48
		return (strncat(d, s, n));
49
50
	if (n == 0)
51
		return (d);
52
53
	d_len = __strlen_chk(d, bos);
54
	_d = d + d_len;
55
	bos -= d_len;
56
57
	while (*s != '\0') {
58
		*_d++ = *s++;
59
		n--;
60
		bos--;
61
62
		if (__predict_false(bos == 0))
63
			__fortify_chk_fail("strncat: prevented write past end of buffer");
64
		if (n == 0)
65
			break;
66
	}
67
	*_d = '\0';
68
69
	return (d);
70
}
(-)lib/libc/secure/__strncpy_chk.c (+97 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include "secure/_string.h"
40
41
char *
42
__strncpy_chk(char *__restrict d, const char *__restrict s, size_t n, size_t bos)
43
{
44
45
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
46
		return (strncpy(d, s, n));
47
48
	if (__predict_false(n > bos))
49
		__fortify_chk_fail(
50
		    "strncpy: prevented write past end of buffer");
51
52
	return (strncpy(d, s, n));
53
}
54
55
/*
56
 * __strncpy_chk2
57
 *
58
 * This is a variant of __strncpy_chk, but it also checks to make
59
 * sure we don't read beyond the end of "src". The code for this is
60
 * based on the original version of strncpy, but modified to check
61
 * how much we read from "src" at the end of the copy operation.
62
 */
63
char *
64
__strncpy_chk2(char *__restrict d, const char *__restrict s, size_t n, size_t d_bos, size_t s_bos)
65
{
66
	size_t s_copy_len;
67
68
	if (__predict_false(d_bos == __FORTIFY_UNKNOWN_SIZE))
69
		return (strncpy(d, s, n));
70
71
	if (__predict_false(s_bos == __FORTIFY_UNKNOWN_SIZE))
72
		return (__strncpy_chk(d, s, n, d_bos));
73
74
	if (__predict_false(n > d_bos))
75
		__fortify_chk_fail("strncpy: prevented write past end of buffer");
76
77
	if (n != 0) {
78
		char *_d = d;
79
		const char *_s = s;
80
81
		do {
82
			if ((*_d++ = *_s++) == 0) {
83
				/* NUL pad the remaining n-1 bytes */
84
				while (--n != 0)
85
					*_d++ = 0;
86
				break;
87
			}
88
		} while (--n != 0);
89
90
		s_copy_len = (size_t)(_s - s);
91
92
		if (__predict_false(s_copy_len > s_bos))
93
			__fortify_chk_fail("strncpy: prevented read past end of buffer");
94
	}
95
96
	return (d);
97
}
(-)lib/libc/secure/__strrchr_chk.c (+62 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 1988 Regents of the University of California.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the University nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
30
 *
31
 * $FreeBSD$
32
 */
33
34
#undef _FORTIFY_SOURCE
35
36
#include <sys/cdefs.h>
37
#include <secure/security.h>
38
#include <string.h>
39
#include "secure/_string.h"
40
41
char *
42
__strrchr_chk(const char *p, int ch, size_t bos)
43
{
44
	char *save;
45
46
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
47
		return (strrchr(p, ch));
48
49
	for (save = NULL;; ++p, bos--) {
50
		if (bos == 0)
51
			__fortify_chk_fail(
52
			    "strrchr: prevented read past end of buffer");
53
		if (*p == (char)ch)
54
			save = (char *)p;
55
		if (!*p)
56
			return (save);
57
	}
58
	/* NOTREACHED */
59
}
60
61
__weak_reference(__strrchr_chk, __rindex_chk);
62
(-)lib/libc/secure/__umask_chk.c (+53 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <sys/types.h>
38
#include <sys/stat.h>
39
#include <stdlib.h>
40
#include "secure/_stat.h"
41
42
/*
43
 * Validate that umask is called with sane mode.
44
 */
45
mode_t 
46
__umask_chk(mode_t mode)
47
{
48
49
	if (__predict_false((mode & 0777) != mode))
50
		__fortify_chk_fail("umask: called with invalid mask");
51
52
	return (umask(mode));
53
}
(-)lib/libc/secure/__vsnprintf_chk.c (+76 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <stdarg.h>
40
#include "secure/_stdio.h"
41
42
int
43
__vsnprintf_chk(char *d, size_t n, int flags, size_t bos, const char *fmt, va_list va)
44
{
45
46
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
47
		return (vsnprintf(d, n, fmt, va));
48
49
	if (__predict_false(n > bos))
50
		__fortify_chk_fail(
51
		    "vsnprintf: prevented write past end of buffer");
52
53
	return (vsnprintf(d, n, fmt, va));
54
}
55
56
57
int
58
__snprintf_chk(char *d, size_t n, int flags, size_t bos, const char *fmt, ...)
59
{
60
	va_list va;
61
	int result;
62
63
	if (bos == __FORTIFY_UNKNOWN_SIZE) {
64
		va_start(va, fmt);
65
		result = vsnprintf(d, n, fmt, va);
66
		va_end(va);
67
68
		return (result);
69
	}
70
71
	va_start(va, fmt);
72
	result = __vsnprintf_chk(d, n, flags, bos, fmt, va);
73
	va_end(va);
74
75
	return (result);
76
}
(-)lib/libc/secure/__vsprintf_chk.c (+78 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (C) 2012 The Android Open Source Project
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *  * Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *  * Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in
12
 *    the documentation and/or other materials provided with the
13
 *    distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * bionic rev: d807b9a12d3e49132b095df3d883618452033b51
29
 *
30
 * $FreeBSD$
31
 */
32
33
#undef _FORTIFY_SOURCE
34
35
#include <sys/cdefs.h>
36
#include <secure/security.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <stdarg.h>
40
#include "secure/_stdio.h"
41
42
int
43
__vsprintf_chk(char * __restrict d, int flags, size_t bos, const char * __restrict fmt, __va_list va)
44
{
45
	int result;
46
47
	if (__predict_false(bos == __FORTIFY_UNKNOWN_SIZE))
48
		return (vsprintf(d, fmt, va));
49
50
	result = vsnprintf(d, bos, fmt, va);
51
	if ((size_t)result >= bos)
52
		__fortify_chk_fail(
53
		    "vsprintf: prevented write past end of buffer");
54
55
	return (result);
56
}
57
58
59
int
60
__sprintf_chk(char * __restrict d, int flags, size_t bos, const char * __restrict fmt, ...)
61
{
62
	va_list va;
63
	int result;
64
65
	if (bos == __FORTIFY_UNKNOWN_SIZE) {
66
		va_start(va, fmt);
67
		result = vsprintf(d, fmt, va);
68
		va_end(va);
69
70
		return (result);
71
	}
72
73
	va_start(va, fmt);
74
	result = __vsprintf_chk(d, flags, bos, fmt, va);
75
	va_end(va);
76
77
	return (result);
78
}
(-)lib/libc/secure/fortify_source.c (+47 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 2015 Olivér Pintér <op@FreeBSD.org>
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 *
26
 * $FreeBSD$
27
 */
28
29
#include <sys/cdefs.h>
30
#include <sys/param.h>
31
#include <sys/sysctl.h>
32
#include <sys/types.h>
33
#include "secure/security.h"
34
35
void
36
__fortify_chk_fail(const char* msg)
37
{
38
39
	__secure_fail(msg);
40
}
41
42
int
43
__fortify_chk_overlap(const void *a, const void *b, size_t len)
44
{
45
46
	return ((a <= b && b <= a + len) || (b <= a && a <= b + len));
47
}
(-)lib/libc/secure/secure_common.c (+75 lines)
Line 0 Link Here
1
/* $NetBSD: stack_protector.c,v 1.4 2006/11/22 17:23:25 christos Exp $	*/
2
/* $OpenBSD: stack_protector.c,v 1.10 2006/03/31 05:34:44 deraadt Exp $	*/
3
/*
4
 * Copyright (c) 2002 Hiroaki Etoh, Federico G. Schwindt, and Miodrag Vallat.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
20
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 * POSSIBILITY OF SUCH DAMAGE.
27
 *
28
 */
29
30
#include <sys/cdefs.h>
31
__FBSDID("$FreeBSD$");
32
33
#include <sys/param.h>
34
#include <sys/sysctl.h>
35
#include <sys/types.h>
36
#include <errno.h>
37
#include <link.h>
38
#include <signal.h>
39
#include <string.h>
40
#include <syslog.h>
41
#include <unistd.h>
42
#include "libc_private.h"
43
44
static void __fail(const char *) __dead2;
45
46
/*ARGSUSED*/
47
static void
48
__fail(const char *msg)
49
{
50
	struct sigaction sa;
51
	sigset_t mask;
52
53
	/* Immediately block all signal handlers from running code */
54
	(void)sigfillset(&mask);
55
	(void)sigdelset(&mask, SIGABRT);
56
	(void)sigprocmask(SIG_BLOCK, &mask, NULL);
57
58
	/* This may fail on a chroot jail... */
59
	syslog(LOG_CRIT, "%s", msg);
60
61
	(void)memset(&sa, 0, sizeof(sa));
62
	(void)sigemptyset(&sa.sa_mask);
63
	sa.sa_flags = 0;
64
	sa.sa_handler = SIG_DFL;
65
	(void)sigaction(SIGABRT, &sa, NULL);
66
	(void)kill(getpid(), SIGABRT);
67
	_exit(127);
68
}
69
70
void
71
__secure_fail(const char *msg)
72
{
73
74
	__fail(msg);
75
}
(-)lib/libc/secure/stack_protector.c (-34 / +6 lines)
Lines 33-44 Link Here
33
#include <sys/param.h>
33
#include <sys/param.h>
34
#include <sys/sysctl.h>
34
#include <sys/sysctl.h>
35
#include <sys/types.h>
35
#include <sys/types.h>
36
#include <errno.h>
36
#include <secure/security.h>
37
#include <link.h>
37
#include <link.h>
38
#include <signal.h>
39
#include <string.h>
40
#include <syslog.h>
41
#include <unistd.h>
42
#include "libc_private.h"
38
#include "libc_private.h"
43
39
44
extern int __sysctl(const int *name, u_int namelen, void *oldp,
40
extern int __sysctl(const int *name, u_int namelen, void *oldp,
Lines 46-54 Link Here
46
42
47
long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
43
long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
48
static void __guard_setup(void) __attribute__((__constructor__, __used__));
44
static void __guard_setup(void) __attribute__((__constructor__, __used__));
49
static void __fail(const char *);
45
void __stack_chk_fail(void) __dead2;
50
void __stack_chk_fail(void);
51
void __chk_fail(void);
52
46
53
/*LINTED used*/
47
/*LINTED used*/
54
static void
48
static void
Lines 76-115 Link Here
76
	}
70
	}
77
}
71
}
78
72
79
/*ARGSUSED*/
80
static void
81
__fail(const char *msg)
82
{
83
	struct sigaction sa;
84
	sigset_t mask;
85
86
	/* Immediately block all signal handlers from running code */
87
	(void)sigfillset(&mask);
88
	(void)sigdelset(&mask, SIGABRT);
89
	(void)sigprocmask(SIG_BLOCK, &mask, NULL);
90
91
	/* This may fail on a chroot jail... */
92
	syslog(LOG_CRIT, "%s", msg);
93
94
	(void)memset(&sa, 0, sizeof(sa));
95
	(void)sigemptyset(&sa.sa_mask);
96
	sa.sa_flags = 0;
97
	sa.sa_handler = SIG_DFL;
98
	(void)sigaction(SIGABRT, &sa, NULL);
99
	(void)kill(getpid(), SIGABRT);
100
	_exit(127);
101
}
102
103
void
73
void
104
__stack_chk_fail(void)
74
__stack_chk_fail(void)
105
{
75
{
106
	__fail("stack overflow detected; terminated");
76
77
	__secure_fail("stack overflow detected; terminated");
107
}
78
}
108
79
109
void
80
void
110
__chk_fail(void)
81
__chk_fail(void)
111
{
82
{
112
	__fail("buffer overflow detected; terminated");
83
84
	__secure_fail("buffer overflow detected; terminated");
113
}
85
}
114
86
115
#ifndef PIC
87
#ifndef PIC
(-)lib/libc/stdio/fgets.c (+2 lines)
Lines 33-38 Link Here
33
#if defined(LIBC_SCCS) && !defined(lint)
33
#if defined(LIBC_SCCS) && !defined(lint)
34
static char sccsid[] = "@(#)fgets.c	8.2 (Berkeley) 12/22/93";
34
static char sccsid[] = "@(#)fgets.c	8.2 (Berkeley) 12/22/93";
35
#endif /* LIBC_SCCS and not lint */
35
#endif /* LIBC_SCCS and not lint */
36
37
#undef _FORTIFY_SOURCE
36
#include <sys/cdefs.h>
38
#include <sys/cdefs.h>
37
__FBSDID("$FreeBSD$");
39
__FBSDID("$FreeBSD$");
38
40
(-)lib/libc/stdio/fread.c (+2 lines)
Lines 33-38 Link Here
33
#if defined(LIBC_SCCS) && !defined(lint)
33
#if defined(LIBC_SCCS) && !defined(lint)
34
static char sccsid[] = "@(#)fread.c	8.2 (Berkeley) 12/11/93";
34
static char sccsid[] = "@(#)fread.c	8.2 (Berkeley) 12/11/93";
35
#endif /* LIBC_SCCS and not lint */
35
#endif /* LIBC_SCCS and not lint */
36
37
#undef _FORTIFY_SOURCE
36
#include <sys/cdefs.h>
38
#include <sys/cdefs.h>
37
__FBSDID("$FreeBSD$");
39
__FBSDID("$FreeBSD$");
38
40
(-)lib/libc/stdio/fwrite.c (+2 lines)
Lines 33-38 Link Here
33
#if defined(LIBC_SCCS) && !defined(lint)
33
#if defined(LIBC_SCCS) && !defined(lint)
34
static char sccsid[] = "@(#)fwrite.c	8.1 (Berkeley) 6/4/93";
34
static char sccsid[] = "@(#)fwrite.c	8.1 (Berkeley) 6/4/93";
35
#endif /* LIBC_SCCS and not lint */
35
#endif /* LIBC_SCCS and not lint */
36
37
#undef _FORTIFY_SOURCE
36
#include <sys/cdefs.h>
38
#include <sys/cdefs.h>
37
__FBSDID("$FreeBSD$");
39
__FBSDID("$FreeBSD$");
38
40
(-)lib/libc/stdio/gets.c (+1 lines)
Lines 33-38 Link Here
33
#if defined(LIBC_SCCS) && !defined(lint)
33
#if defined(LIBC_SCCS) && !defined(lint)
34
static char sccsid[] = "@(#)gets.c	8.1 (Berkeley) 6/4/93";
34
static char sccsid[] = "@(#)gets.c	8.1 (Berkeley) 6/4/93";
35
#endif /* LIBC_SCCS and not lint */
35
#endif /* LIBC_SCCS and not lint */
36
36
#include <sys/cdefs.h>
37
#include <sys/cdefs.h>
37
__FBSDID("$FreeBSD$");
38
__FBSDID("$FreeBSD$");
38
39
(-)lib/libc/stdio/snprintf.c (+2 lines)
Lines 38-43 Link Here
38
#if defined(LIBC_SCCS) && !defined(lint)
38
#if defined(LIBC_SCCS) && !defined(lint)
39
static char sccsid[] = "@(#)snprintf.c	8.1 (Berkeley) 6/4/93";
39
static char sccsid[] = "@(#)snprintf.c	8.1 (Berkeley) 6/4/93";
40
#endif /* LIBC_SCCS and not lint */
40
#endif /* LIBC_SCCS and not lint */
41
42
#undef _FORTIFY_SOURCE
41
#include <sys/cdefs.h>
43
#include <sys/cdefs.h>
42
__FBSDID("$FreeBSD$");
44
__FBSDID("$FreeBSD$");
43
45
(-)lib/libc/stdio/sprintf.c (+1 lines)
Lines 38-43 Link Here
38
#if defined(LIBC_SCCS) && !defined(lint)
38
#if defined(LIBC_SCCS) && !defined(lint)
39
static char sccsid[] = "@(#)sprintf.c	8.1 (Berkeley) 6/4/93";
39
static char sccsid[] = "@(#)sprintf.c	8.1 (Berkeley) 6/4/93";
40
#endif /* LIBC_SCCS and not lint */
40
#endif /* LIBC_SCCS and not lint */
41
#undef _FORTIFY_SOURCE
41
#include <sys/cdefs.h>
42
#include <sys/cdefs.h>
42
__FBSDID("$FreeBSD$");
43
__FBSDID("$FreeBSD$");
43
44
(-)lib/libc/stdio/vsnprintf.c (+2 lines)
Lines 38-43 Link Here
38
#if defined(LIBC_SCCS) && !defined(lint)
38
#if defined(LIBC_SCCS) && !defined(lint)
39
static char sccsid[] = "@(#)vsnprintf.c	8.1 (Berkeley) 6/4/93";
39
static char sccsid[] = "@(#)vsnprintf.c	8.1 (Berkeley) 6/4/93";
40
#endif /* LIBC_SCCS and not lint */
40
#endif /* LIBC_SCCS and not lint */
41
42
#undef _FORTIFY_SOURCE
41
#include <sys/cdefs.h>
43
#include <sys/cdefs.h>
42
__FBSDID("$FreeBSD$");
44
__FBSDID("$FreeBSD$");
43
45
(-)lib/libc/string/bzero.c (+1 lines)
Lines 1-3 Link Here
1
#undef _FORTIFY_SOURCE
1
#include <sys/cdefs.h>
2
#include <sys/cdefs.h>
2
__FBSDID("$FreeBSD$");
3
__FBSDID("$FreeBSD$");
3
4
(-)lib/libc/string/memccpy.c (+2 lines)
Lines 30-35 Link Here
30
#if defined(LIBC_SCCS) && !defined(lint)
30
#if defined(LIBC_SCCS) && !defined(lint)
31
static char sccsid[] = "@(#)memccpy.c	8.1 (Berkeley) 6/4/93";
31
static char sccsid[] = "@(#)memccpy.c	8.1 (Berkeley) 6/4/93";
32
#endif /* LIBC_SCCS and not lint */
32
#endif /* LIBC_SCCS and not lint */
33
34
#undef _FORTIFY_SOURCE
33
#include <sys/cdefs.h>
35
#include <sys/cdefs.h>
34
__FBSDID("$FreeBSD$");
36
__FBSDID("$FreeBSD$");
35
37
(-)lib/libc/string/memchr.c (+2 lines)
Lines 33-38 Link Here
33
#if defined(LIBC_SCCS) && !defined(lint)
33
#if defined(LIBC_SCCS) && !defined(lint)
34
static char sccsid[] = "@(#)memchr.c	8.1 (Berkeley) 6/4/93";
34
static char sccsid[] = "@(#)memchr.c	8.1 (Berkeley) 6/4/93";
35
#endif /* LIBC_SCCS and not lint */
35
#endif /* LIBC_SCCS and not lint */
36
37
#undef _FORTIFY_SOURCE
36
#include <sys/cdefs.h>
38
#include <sys/cdefs.h>
37
__FBSDID("$FreeBSD$");
39
__FBSDID("$FreeBSD$");
38
40
(-)lib/libc/string/memset.c (+1 lines)
Lines 33-38 Link Here
33
#if defined(LIBC_SCCS) && !defined(lint)
33
#if defined(LIBC_SCCS) && !defined(lint)
34
static char sccsid[] = "@(#)memset.c	8.1 (Berkeley) 6/4/93";
34
static char sccsid[] = "@(#)memset.c	8.1 (Berkeley) 6/4/93";
35
#endif /* LIBC_SCCS and not lint */
35
#endif /* LIBC_SCCS and not lint */
36
#undef _FORTIFY_SOURCE
36
#include <sys/cdefs.h>
37
#include <sys/cdefs.h>
37
__FBSDID("$FreeBSD$");
38
__FBSDID("$FreeBSD$");
38
39
(-)lib/libc/string/stpncpy.c (+1 lines)
Lines 24-29 Link Here
24
 * SUCH DAMAGE.
24
 * SUCH DAMAGE.
25
 */
25
 */
26
26
27
#undef _FORTIFY_SOURCE
27
#include <sys/cdefs.h>
28
#include <sys/cdefs.h>
28
__FBSDID("$FreeBSD$");
29
__FBSDID("$FreeBSD$");
29
30
(-)lib/libc/string/strchr.c (+2 lines)
Lines 30-35 Link Here
30
#if defined(LIBC_SCCS) && !defined(lint)
30
#if defined(LIBC_SCCS) && !defined(lint)
31
static char sccsid[] = "@(#)index.c	8.1 (Berkeley) 6/4/93";
31
static char sccsid[] = "@(#)index.c	8.1 (Berkeley) 6/4/93";
32
#endif /* LIBC_SCCS and not lint */
32
#endif /* LIBC_SCCS and not lint */
33
34
#undef _FORTIFY_SOURCE
33
#include <sys/cdefs.h>
35
#include <sys/cdefs.h>
34
__FBSDID("$FreeBSD$");
36
__FBSDID("$FreeBSD$");
35
37
(-)lib/libc/string/strchrnul.c (+1 lines)
Lines 25-30 Link Here
25
 *
25
 *
26
 */
26
 */
27
27
28
#undef _FORTIFY_SOURCE
28
#include <sys/cdefs.h>
29
#include <sys/cdefs.h>
29
__FBSDID("$FreeBSD$");
30
__FBSDID("$FreeBSD$");
30
31
(-)lib/libc/string/strlcat.c (+1 lines)
Lines 16-21 Link Here
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
17
 */
18
18
19
#undef _FORTIFY_SOURCE
19
#include <sys/cdefs.h>
20
#include <sys/cdefs.h>
20
__FBSDID("$FreeBSD$");
21
__FBSDID("$FreeBSD$");
21
22
(-)lib/libc/string/strlcpy.c (+1 lines)
Lines 16-21 Link Here
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
17
 */
18
18
19
#undef _FORTIFY_SOURCE
19
#include <sys/cdefs.h>
20
#include <sys/cdefs.h>
20
__FBSDID("$FreeBSD$");
21
__FBSDID("$FreeBSD$");
21
22
(-)lib/libc/string/strlen.c (+1 lines)
Lines 24-29 Link Here
24
 * SUCH DAMAGE.
24
 * SUCH DAMAGE.
25
 */
25
 */
26
26
27
#undef _FORTIFY_SOURCE
27
#include <sys/cdefs.h>
28
#include <sys/cdefs.h>
28
__FBSDID("$FreeBSD$");
29
__FBSDID("$FreeBSD$");
29
30
(-)lib/libc/string/strrchr.c (+2 lines)
Lines 30-35 Link Here
30
#if defined(LIBC_SCCS) && !defined(lint)
30
#if defined(LIBC_SCCS) && !defined(lint)
31
static char sccsid[] = "@(#)rindex.c	8.1 (Berkeley) 6/4/93";
31
static char sccsid[] = "@(#)rindex.c	8.1 (Berkeley) 6/4/93";
32
#endif /* LIBC_SCCS and not lint */
32
#endif /* LIBC_SCCS and not lint */
33
34
#undef _FORTIFY_SOURCE
33
#include <sys/cdefs.h>
35
#include <sys/cdefs.h>
34
__FBSDID("$FreeBSD$");
36
__FBSDID("$FreeBSD$");
35
37
(-)lib/libc/sys/read.c (+1 lines)
Lines 30-35 Link Here
30
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
 */
31
 */
32
32
33
#undef _FORTIFY_SOURCE
33
#include <sys/cdefs.h>
34
#include <sys/cdefs.h>
34
__FBSDID("$FreeBSD$");
35
__FBSDID("$FreeBSD$");
35
36
(-)lib/libc/sys/readv.c (+1 lines)
Lines 30-35 Link Here
30
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
 */
31
 */
32
32
33
#undef _FORTIFY_SOURCE
33
#include <sys/cdefs.h>
34
#include <sys/cdefs.h>
34
__FBSDID("$FreeBSD$");
35
__FBSDID("$FreeBSD$");
35
36
(-)lib/libstand/Makefile (+1 lines)
Lines 8-13 Link Here
8
8
9
MK_PROFILE=	no
9
MK_PROFILE=	no
10
MK_SSP=		no
10
MK_SSP=		no
11
MK_FORTIFY=	no
11
12
12
.include <src.opts.mk>
13
.include <src.opts.mk>
13
14
(-)lib/libstand/stand.h (+6 lines)
Lines 61-66 Link Here
61
#ifndef	STAND_H
61
#ifndef	STAND_H
62
#define	STAND_H
62
#define	STAND_H
63
63
64
#undef _FORTIFY_SOURCE
65
/*
66
 * Yes, this second one is a big hack.
67
 */
68
#undef __BSD_FORTIFY
69
64
#include <sys/types.h>
70
#include <sys/types.h>
65
#include <sys/cdefs.h>
71
#include <sys/cdefs.h>
66
#include <sys/stat.h>
72
#include <sys/stat.h>
(-)lib/libthr/Makefile (+1 lines)
Lines 12-17 Link Here
12
12
13
.include <src.opts.mk>
13
.include <src.opts.mk>
14
MK_SSP=	no
14
MK_SSP=	no
15
MK_FORTIFY=	no
15
16
16
LIB=thr
17
LIB=thr
17
SHLIB_MAJOR= 3
18
SHLIB_MAJOR= 3
(-)libexec/rtld-elf/Makefile (+1 lines)
Lines 6-11 Link Here
6
6
7
.include <src.opts.mk>
7
.include <src.opts.mk>
8
MK_SSP=		no
8
MK_SSP=		no
9
MK_FORTIFY=	no
9
10
10
PROG?=		ld-elf.so.1
11
PROG?=		ld-elf.so.1
11
SRCS=		rtld_start.S \
12
SRCS=		rtld_start.S \
(-)rescue/librescue/Makefile (+1 lines)
Lines 4-9 Link Here
4
4
5
.include <src.opts.mk>
5
.include <src.opts.mk>
6
MK_SSP=		no
6
MK_SSP=		no
7
MK_FORTIFY=	no
7
8
8
# Certain library entries have hard-coded references to
9
# Certain library entries have hard-coded references to
9
# /bin, /sbin, etc, that require those entries to be
10
# /bin, /sbin, etc, that require those entries to be
(-)rescue/rescue/Makefile (+1 lines)
Lines 5-10 Link Here
5
5
6
MAN=
6
MAN=
7
MK_SSP=	no
7
MK_SSP=	no
8
MK_FORTIFY=	no
8
9
9
PROG=	rescue
10
PROG=	rescue
10
BINDIR?=/rescue
11
BINDIR?=/rescue
(-)share/mk/bsd.opts.mk (+1 lines)
Lines 51-56 Link Here
51
__DEFAULT_YES_OPTIONS = \
51
__DEFAULT_YES_OPTIONS = \
52
    ASSERT_DEBUG \
52
    ASSERT_DEBUG \
53
    DOCCOMPRESS \
53
    DOCCOMPRESS \
54
    FORTIFY \
54
    INCLUDES \
55
    INCLUDES \
55
    INSTALLLIB \
56
    INSTALLLIB \
56
    KERBEROS \
57
    KERBEROS \
(-)share/mk/bsd.sys.mk (+5 lines)
Lines 153-158 Link Here
153
CFLAGS+=	${SSP_CFLAGS}
153
CFLAGS+=	${SSP_CFLAGS}
154
.endif # SSP && !ARM && !MIPS
154
.endif # SSP && !ARM && !MIPS
155
155
156
.if ${MK_FORTIFY} != "no"
157
FORTIFY_CFLAGS?=	-D_FORTIFY_SOURCE=1
158
CFLAGS+=	${FORTIFY_CFLAGS}
159
.endif # FORTIFY
160
156
# Allow user-specified additional warning flags, plus compiler specific flag overrides.
161
# Allow user-specified additional warning flags, plus compiler specific flag overrides.
157
# Unless we've overriden this...
162
# Unless we've overriden this...
158
.if ${MK_WARNS} != "no"
163
.if ${MK_WARNS} != "no"
(-)sys/boot/arm/uboot/Makefile (+2 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
4
5
MK_FORTIFY=	no
6
5
FILES=		ubldr ubldr.bin
7
FILES=		ubldr ubldr.bin
6
8
7
NEWVERSWHAT=	"U-Boot loader" ${MACHINE_ARCH}
9
NEWVERSWHAT=	"U-Boot loader" ${MACHINE_ARCH}
(-)sys/boot/arm64/Makefile (+4 lines)
Lines 1-3 Link Here
1
# $FreeBSD$
1
# $FreeBSD$
2
2
3
.include <src.opts.mk>
4
5
MK_FORTIFY=	no
6
3
.include <bsd.subdir.mk>
7
.include <bsd.subdir.mk>
(-)sys/boot/common/Makefile (+2 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
4
5
MK_FORTIFY=	no
6
5
MAN+=	loader.8
7
MAN+=	loader.8
6
.if ${MK_ZFS} != "no"
8
.if ${MK_ZFS} != "no"
7
MAN+=	zfsloader.8
9
MAN+=	zfsloader.8
(-)sys/boot/efi/boot1/Makefile (+1 lines)
Lines 8-13 Link Here
8
.if ${COMPILER_TYPE} != "gcc"
8
.if ${COMPILER_TYPE} != "gcc"
9
9
10
MK_SSP=		no
10
MK_SSP=		no
11
MK_FORTIFY=	no
11
12
12
PROG=		loader.sym
13
PROG=		loader.sym
13
INTERNALPROG=
14
INTERNALPROG=
(-)sys/boot/efi/fdt/Makefile (+2 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
4
5
MK_FORTIFY=	no
6
5
.PATH: ${.CURDIR}/../../common
7
.PATH: ${.CURDIR}/../../common
6
8
7
LIB=		efi_fdt
9
LIB=		efi_fdt
(-)sys/boot/efi/libefi/Makefile (+4 lines)
Lines 1-5 Link Here
1
# $FreeBSD$
1
# $FreeBSD$
2
2
3
.include <src.opts.mk>
4
5
MK_FORTIFY=	no
6
3
LIB=	efi
7
LIB=	efi
4
INTERNALLIB=
8
INTERNALLIB=
5
9
(-)sys/boot/efi/loader/Makefile (+1 lines)
Lines 8-13 Link Here
8
.if ${COMPILER_TYPE} != "gcc"
8
.if ${COMPILER_TYPE} != "gcc"
9
9
10
MK_SSP=		no
10
MK_SSP=		no
11
MK_FORTIFY=	no
11
12
12
PROG=		loader.sym
13
PROG=		loader.sym
13
INTERNALPROG=
14
INTERNALPROG=
(-)sys/boot/fdt/Makefile (+4 lines)
Lines 1-5 Link Here
1
# $FreeBSD$
1
# $FreeBSD$
2
2
3
.include <src.opts.mk>
4
5
MK_FORTIFY=	no
6
3
.PATH:		${.CURDIR}/../../contrib/libfdt/
7
.PATH:		${.CURDIR}/../../contrib/libfdt/
4
8
5
LIB=		fdt
9
LIB=		fdt
(-)sys/boot/ficl/Makefile (+4 lines)
Lines 1-6 Link Here
1
# $FreeBSD$
1
# $FreeBSD$
2
#
2
#
3
3
4
.include <bsd.opts.mk>
5
6
MK_FORTIFY=	no
7
4
FICLDIR?=	${.CURDIR}
8
FICLDIR?=	${.CURDIR}
5
9
6
.if defined(FICL32)
10
.if defined(FICL32)
(-)sys/boot/i386/libi386/Makefile (+5 lines)
Lines 1-5 Link Here
1
# $FreeBSD$
1
# $FreeBSD$
2
#
2
#
3
4
.include <bsd.opts.mk>
5
6
MK_FORTIFY=		no
7
3
LIB=			i386
8
LIB=			i386
4
INTERNALLIB=
9
INTERNALLIB=
5
10
(-)sys/boot/i386/loader/Makefile (+1 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
MK_SSP=		no
4
MK_SSP=		no
5
MK_FORTIFY=	no
5
6
6
LOADER?=	loader
7
LOADER?=	loader
7
PROG=		${LOADER}.sym
8
PROG=		${LOADER}.sym
(-)sys/boot/libstand32/Makefile (+1 lines)
Lines 10-15 Link Here
10
10
11
.include <src.opts.mk>
11
.include <src.opts.mk>
12
MK_SSP=		no
12
MK_SSP=		no
13
MK_FORTIFY=	no
13
14
14
LIBSTAND_SRC=	${.CURDIR}/../../../lib/libstand
15
LIBSTAND_SRC=	${.CURDIR}/../../../lib/libstand
15
LIBC_SRC=	${LIBSTAND_SRC}/../libc
16
LIBC_SRC=	${LIBSTAND_SRC}/../libc
(-)sys/boot/mips/beri/loader/Makefile (+1 lines)
Lines 31-36 Link Here
31
31
32
.include <src.opts.mk>
32
.include <src.opts.mk>
33
MK_SSP=		no
33
MK_SSP=		no
34
MK_FORTIFY=	no
34
MAN=
35
MAN=
35
36
36
PROG?=		loader
37
PROG?=		loader
(-)sys/boot/pc98/loader/Makefile (+1 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
MK_SSP=		no
4
MK_SSP=		no
5
MK_FORTIFY=	no
5
MAN=
6
MAN=
6
7
7
LOADER?=	loader
8
LOADER?=	loader
(-)sys/boot/powerpc/kboot/Makefile (+1 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
MK_SSP=		no
4
MK_SSP=		no
5
MK_FORTIFY=	no
5
MAN=
6
MAN=
6
7
7
PROG=		loader.kboot
8
PROG=		loader.kboot
(-)sys/boot/powerpc/ofw/Makefile (+1 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
MK_SSP=		no
4
MK_SSP=		no
5
MK_FORTIFY=	no
5
MAN=
6
MAN=
6
7
7
PROG=		loader
8
PROG=		loader
(-)sys/boot/powerpc/ps3/Makefile (+1 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
MK_SSP=		no
4
MK_SSP=		no
5
MK_FORTIFY=	no
5
MAN=
6
MAN=
6
7
7
PROG=		loader.ps3
8
PROG=		loader.ps3
(-)sys/boot/powerpc/uboot/Makefile (+1 lines)
Lines 1-6 Link Here
1
# $FreeBSD$
1
# $FreeBSD$
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
MK_FORTIFY=	no
4
5
5
PROG=		ubldr
6
PROG=		ubldr
6
NEWVERSWHAT=	"U-Boot loader" ${MACHINE_ARCH}
7
NEWVERSWHAT=	"U-Boot loader" ${MACHINE_ARCH}
(-)sys/boot/sparc64/loader/Makefile (+1 lines)
Lines 2-7 Link Here
2
2
3
.include <src.opts.mk>
3
.include <src.opts.mk>
4
MK_SSP=		no
4
MK_SSP=		no
5
MK_FORTIFY=	no
5
MAN=
6
MAN=
6
7
7
PROG?=		loader
8
PROG?=		loader
(-)sys/boot/zfs/Makefile (+4 lines)
Lines 1-5 Link Here
1
# $FreeBSD$
1
# $FreeBSD$
2
2
3
.include <bsd.opts.mk>
4
5
MK_FORTIFY=	no
6
3
LIB=		zfsboot
7
LIB=		zfsboot
4
INTERNALLIB=
8
INTERNALLIB=
5
9
(-)sys/sys/cdefs.h (+20 lines)
Lines 545-550 Link Here
545
#define	__gnu_inline
545
#define	__gnu_inline
546
#endif
546
#endif
547
547
548
#if __has_attribute(error) || __GNUC_PREREQ__(4, 3)
549
#define	__error_attr(msg)	__attribute__((__error__(msg)))
550
#else
551
#define	__error_attr(msg)
552
#endif
553
554
/* FORTIFY_SOURCE related defines. */
555
#if __GNUC_PREREQ__(4, 1) && defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && \
556
    defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 && !defined(lint)
557
#define	__BSD_FORTIFY	1
558
#if _FORTIFY_SOURCE >= 2
559
#define	__bos(s)	__builtin_object_size((s), 1)
560
#else
561
#define	__bos(s)	__builtin_object_size((s), 0)
562
#endif
563
#define	__bos0(s)	__builtin_object_size((s), 0)
564
#define	__FORTIFY_INLINE	extern __inline __always_inline __gnu_inline
565
#endif /* !_FORTIFY_SOURCE */
566
#define	__FORTIFY_UNKNOWN_SIZE	((size_t) -1)
567
548
/* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
568
/* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
549
#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \
569
#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \
550
    defined(__GNUC__) && !defined(__INTEL_COMPILER)
570
    defined(__GNUC__) && !defined(__INTEL_COMPILER)
(-)sys/sys/poll.h (+4 lines)
Lines 117-122 Link Here
117
#endif
117
#endif
118
__END_DECLS
118
__END_DECLS
119
119
120
#ifdef __BSD_FORTIFY
121
#include <secure/_poll.h>
122
#endif
123
120
#endif /* !_KERNEL */
124
#endif /* !_KERNEL */
121
125
122
#endif /* !_SYS_POLL_H_ */
126
#endif /* !_SYS_POLL_H_ */
(-)sys/sys/select.h (+1 lines)
Lines 103-108 Link Here
103
int	select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
103
int	select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
104
#endif
104
#endif
105
__END_DECLS
105
__END_DECLS
106
106
#endif /* !_KERNEL */
107
#endif /* !_KERNEL */
107
108
108
#endif /* _SYS_SELECT_H_ */
109
#endif /* _SYS_SELECT_H_ */
(-)sys/sys/socket.h (+4 lines)
Lines 630-635 Link Here
630
int	socketpair(int, int, int, int *);
630
int	socketpair(int, int, int, int *);
631
__END_DECLS
631
__END_DECLS
632
632
633
#ifdef __BSD_FORTIFY
634
#include <secure/_socket.h>
635
#endif
636
633
#endif /* !_KERNEL */
637
#endif /* !_KERNEL */
634
638
635
#ifdef _KERNEL
639
#ifdef _KERNEL
(-)sys/sys/stat.h (+5 lines)
Lines 356-361 Link Here
356
int	mknodat(int, const char *, mode_t, dev_t);
356
int	mknodat(int, const char *, mode_t, dev_t);
357
#endif
357
#endif
358
__END_DECLS
358
__END_DECLS
359
360
#ifdef __BSD_FORTIFY
361
#include <secure/_stat.h>
362
#endif
363
359
#endif /* !_KERNEL */
364
#endif /* !_KERNEL */
360
365
361
#endif /* !_SYS_STAT_H_ */
366
#endif /* !_SYS_STAT_H_ */
(-)tools/build/options/WITHOUT_FORTIFY (+4 lines)
Line 0 Link Here
1
.\" $FreeBSD$
2
Set to not build world with FORTIFY_SOURCE.
3
4
FORTIFY_SOURCE feature provides primitive support for detecting buffer overflows in various functions which operates on memory areas and strings. A limited set of buffer overflows can be detected with this feature, but it provides an additional level of validation for some functions which are potentially an origin of buffer overflow flaws.

Return to bug 202311