summaryrefslogtreecommitdiff
path: root/include/string.h
blob: 42c43a2f072b901bbae56a00de6d891d00ed3f45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef _FORTIFY_STRING_H
#define _FORTIFY_STRING_H

#include_next <string.h>

#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
#include "fortify-headers.h"

#ifdef __cplusplus
extern "C" {
#endif

#undef memcpy
#undef memmove
#undef memset
#undef strcat
#undef strcpy
#undef strncat
#undef strncpy

fortify_fn(memcpy) void *memcpy(void *dst, const void *src, size_t n)
{
	size_t bos_dst = __builtin_object_size(dst, 0);
	size_t bos_src = __builtin_object_size(src, 0);
	char *d = (char *)dst;
	const char *s = (const char *)src;

	/* trap if pointers are overlapping but not if dst == src.
	 * gcc seems to like to generate code that relies on dst == src */
	if ((d < s && d + n > s) ||
	    (s < d && s + n > d))
		__builtin_trap();
	if (n > bos_dst || n > bos_src)
		__builtin_trap();
	return __orig_memcpy(dst, src, n);
}

fortify_fn(memmove) void *memmove(void *dst, const void *src, size_t n)
{
	size_t bos_dst = __builtin_object_size(dst, 0);
	size_t bos_src = __builtin_object_size(src, 0);

	if (n > bos_dst || n > bos_src)
		__builtin_trap();
	return __orig_memmove(dst, src, n);
}

fortify_fn(memset) void *memset(void *dst, int c, size_t n)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (n > bos)
		__builtin_trap();
	return __orig_memset(dst, c, n);
}

#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
 || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
 || defined(_BSD_SOURCE)
#undef stpcpy
fortify_fn(stpcpy) char *stpcpy(char *dst, const char *src)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (strlen(src) + 1 > bos)
		__builtin_trap();
	return __orig_stpcpy(dst, src);
}

#undef stpncpy
fortify_fn(stpncpy) char *stpncpy(char *dst, const char *src, size_t n)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (n > bos)
		__builtin_trap();
	return __orig_stpncpy(dst, src, n);
}
#endif

fortify_fn(strcat) char *strcat(char *dst, const char *src)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (strlen(src) + strlen(dst) + 1 > bos)
		__builtin_trap();
	return __orig_strcat(dst, src);
}

fortify_fn(strcpy) char *strcpy(char *dst, const char *src)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (strlen(src) + 1 > bos)
		__builtin_trap();
	return __orig_strcpy(dst, src);
}

fortify_fn(strncat) char *strncat(char *dst, const char *src, size_t n)
{
	size_t bos = __builtin_object_size(dst, 0);
	size_t slen, dlen;

	if (n > bos) {
		slen = strlen(src);
		dlen = strlen(dst);
		if (slen > n)
			slen = n;
		if (slen + dlen + 1 > bos)
			__builtin_trap();
	}
	return __orig_strncat(dst, src, n);
}

fortify_fn(strncpy) char *strncpy(char *dst, const char *src, size_t n)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (n > bos)
		__builtin_trap();
	return __orig_strncpy(dst, src, n);
}

#ifdef _GNU_SOURCE
#undef mempcpy
fortify_fn(mempcpy) void *mempcpy(void *dst, const void *src, size_t n)
{
	size_t bos_dst = __builtin_object_size(dst, 0);
	size_t bos_src = __builtin_object_size(src, 0);

	if (n > bos_dst || n > bos_src)
		__builtin_trap();
	return __orig_mempcpy(dst, src, n);
}
#endif

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#undef strlcat
#undef strlcpy
fortify_fn(strlcat) size_t strlcat(char *dst, const char *src, size_t n)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (n > bos)
		__builtin_trap();
	return __orig_strlcat(dst, src, n);
}

fortify_fn(strlcpy) size_t strlcpy(char *dst, const char *src, size_t n)
{
	size_t bos = __builtin_object_size(dst, 0);

	if (n > bos)
		__builtin_trap();
	return __orig_strlcpy(dst, src, n);
}
#endif

#ifdef __cplusplus
}
#endif

#endif

#endif