summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsin2015-02-24 18:12:27 +0000
committersin2015-02-24 18:14:33 +0000
commiteecef18261cc278fbc13ecbfb4e5bc10762cc794 (patch)
tree483074e25fbbcbb198ac4d339b84ace4205987f6
parent9a77136c5914f6be50df195dac0f99424252a297 (diff)
Remove compile time checks
These can produce false positives. Given that we support fortify source level 1 we shouldn't break valid code.
Diffstat (limited to '')
-rw-r--r--include/stdio.h15
-rw-r--r--include/string.h40
-rw-r--r--include/strings.h12
-rw-r--r--include/sys/socket.h12
-rw-r--r--include/unistd.h24
5 files changed, 0 insertions, 103 deletions
diff --git a/include/stdio.h b/include/stdio.h
index a637f83..aeff658 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -5,24 +5,17 @@
5 5
6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
7 7
8#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg)))
9
10__errordecl(__fgets_error, "fgets: buffer overflow detected");
11static inline __attribute__ ((always_inline)) 8static inline __attribute__ ((always_inline))
12char * 9char *
13__fortify_fgets(char *s, int n, FILE *fp) 10__fortify_fgets(char *s, int n, FILE *fp)
14{ 11{
15 size_t bos = __builtin_object_size(s, 0); 12 size_t bos = __builtin_object_size(s, 0);
16 13
17 if (__builtin_constant_p(n) && (size_t)n > bos)
18 __fgets_error();
19
20 if ((size_t)n > bos) 14 if ((size_t)n > bos)
21 __builtin_trap(); 15 __builtin_trap();
22 return fgets(s, n, fp); 16 return fgets(s, n, fp);
23} 17}
24 18
25__errordecl(__vsnprintf_error, "vsnprintf: buffer overflow detected");
26static inline 19static inline
27__attribute__ ((always_inline)) 20__attribute__ ((always_inline))
28__attribute__ ((__format__ (printf, 3, 0))) 21__attribute__ ((__format__ (printf, 3, 0)))
@@ -32,9 +25,6 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap)
32{ 25{
33 size_t bos = __builtin_object_size(s, 0); 26 size_t bos = __builtin_object_size(s, 0);
34 27
35 if (__builtin_constant_p(n) && n > bos)
36 __vsnprintf_error();
37
38 if (n > bos) 28 if (n > bos)
39 __builtin_trap(); 29 __builtin_trap();
40 return vsnprintf(s, n, fmt, ap); 30 return vsnprintf(s, n, fmt, ap);
@@ -45,20 +35,15 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap)
45#undef vsnprintf 35#undef vsnprintf
46#define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap) 36#define vsnprintf(s, n, fmt, ap) __fortify_vsnprintf(s, n, fmt, ap)
47 37
48__errordecl(__snprintf_error, "snprintf: buffer overflow detected");
49#undef snprintf 38#undef snprintf
50#define snprintf(s, n, fmt, ...) ({ \ 39#define snprintf(s, n, fmt, ...) ({ \
51 size_t _n = (n); \ 40 size_t _n = (n); \
52 size_t bos = __builtin_object_size(s, 0); \ 41 size_t bos = __builtin_object_size(s, 0); \
53 if (__builtin_constant_p(_n) && _n > bos) \
54 __snprintf_error(); \
55 if (_n > bos) \ 42 if (_n > bos) \
56 __builtin_trap(); \ 43 __builtin_trap(); \
57 snprintf(s, _n, fmt, ## __VA_ARGS__); \ 44 snprintf(s, _n, fmt, ## __VA_ARGS__); \
58}) 45})
59 46
60#undef __errordecl
61
62#endif 47#endif
63 48
64#endif 49#endif
diff --git a/include/string.h b/include/string.h
index 9f69d31..9d40e77 100644
--- a/include/string.h
+++ b/include/string.h
@@ -5,9 +5,6 @@
5 5
6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
7 7
8#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg)))
9
10__errordecl(__memcpy_error, "memcpy: buffer overflow detected");
11static inline __attribute__ ((always_inline)) 8static inline __attribute__ ((always_inline))
12void * 9void *
13__fortify_memcpy(void *dest, const void *src, size_t n) 10__fortify_memcpy(void *dest, const void *src, size_t n)
@@ -16,9 +13,6 @@ __fortify_memcpy(void *dest, const void *src, size_t n)
16 char *d = dest; 13 char *d = dest;
17 const char *s = src; 14 const char *s = src;
18 15
19 if (__builtin_constant_p(n) && n > bos)
20 __memcpy_error();
21
22 /* trap if pointers are overlapping but not if dest == src */ 16 /* trap if pointers are overlapping but not if dest == src */
23 if ((d < s && d + n > s) || 17 if ((d < s && d + n > s) ||
24 (s < d && s + n > d)) 18 (s < d && s + n > d))
@@ -28,31 +22,23 @@ __fortify_memcpy(void *dest, const void *src, size_t n)
28 return memcpy(dest, src, n); 22 return memcpy(dest, src, n);
29} 23}
30 24
31__errordecl(__memmove_error, "memmove: buffer overflow detected");
32static inline __attribute__ ((always_inline)) 25static inline __attribute__ ((always_inline))
33void * 26void *
34__fortify_memmove(void *dest, const void *src, size_t n) 27__fortify_memmove(void *dest, const void *src, size_t n)
35{ 28{
36 size_t bos = __builtin_object_size(dest, 0); 29 size_t bos = __builtin_object_size(dest, 0);
37 30
38 if (__builtin_constant_p(n) && n > bos)
39 __memmove_error();
40
41 if (n > bos) 31 if (n > bos)
42 __builtin_trap(); 32 __builtin_trap();
43 return memmove(dest, src, n); 33 return memmove(dest, src, n);
44} 34}
45 35
46__errordecl(__memset_error, "memset: buffer overflow detected");
47static inline __attribute__ ((always_inline)) 36static inline __attribute__ ((always_inline))
48void * 37void *
49__fortify_memset(void *dest, int c, size_t n) 38__fortify_memset(void *dest, int c, size_t n)
50{ 39{
51 size_t bos = __builtin_object_size(dest, 0); 40 size_t bos = __builtin_object_size(dest, 0);
52 41
53 if (__builtin_constant_p(n) && n > bos)
54 __memset_error();
55
56 if (n > bos) 42 if (n > bos)
57 __builtin_trap(); 43 __builtin_trap();
58 return memset(dest, c, n); 44 return memset(dest, c, n);
@@ -69,16 +55,12 @@ __fortify_stpcpy(char *dest, const char *src)
69 return stpcpy(dest, src); 55 return stpcpy(dest, src);
70} 56}
71 57
72__errordecl(__stpncpy_error, "stpncpy: buffer overflow detected");
73static inline __attribute__ ((always_inline)) 58static inline __attribute__ ((always_inline))
74char * 59char *
75__fortify_stpncpy(char *dest, const char *src, size_t n) 60__fortify_stpncpy(char *dest, const char *src, size_t n)
76{ 61{
77 size_t bos = __builtin_object_size(dest, 0); 62 size_t bos = __builtin_object_size(dest, 0);
78 63
79 if (__builtin_constant_p(n) && n > bos)
80 __stpncpy_error();
81
82 if (n > bos) 64 if (n > bos)
83 __builtin_trap(); 65 __builtin_trap();
84 return stpncpy(dest, src, n); 66 return stpncpy(dest, src, n);
@@ -106,7 +88,6 @@ __fortify_strcpy(char *dest, const char *src)
106 return strcpy(dest, src); 88 return strcpy(dest, src);
107} 89}
108 90
109__errordecl(__strncat_error, "strncat: buffer overflow detected");
110static inline __attribute__ ((always_inline)) 91static inline __attribute__ ((always_inline))
111char * 92char *
112__fortify_strncat(char *dest, const char *src, size_t n) 93__fortify_strncat(char *dest, const char *src, size_t n)
@@ -114,9 +95,6 @@ __fortify_strncat(char *dest, const char *src, size_t n)
114 size_t bos = __builtin_object_size(dest, 0); 95 size_t bos = __builtin_object_size(dest, 0);
115 size_t slen, dlen; 96 size_t slen, dlen;
116 97
117 if (__builtin_constant_p(n) && n > bos)
118 __strncat_error();
119
120 if (n > bos) { 98 if (n > bos) {
121 slen = strlen(src); 99 slen = strlen(src);
122 dlen = strlen(dest); 100 dlen = strlen(dest);
@@ -128,32 +106,24 @@ __fortify_strncat(char *dest, const char *src, size_t n)
128 return strncat(dest, src, n); 106 return strncat(dest, src, n);
129} 107}
130 108
131__errordecl(__strncpy_error, "strncpy: buffer overflow detected");
132static inline __attribute__ ((always_inline)) 109static inline __attribute__ ((always_inline))
133char * 110char *
134__fortify_strncpy(char *dest, const char *src, size_t n) 111__fortify_strncpy(char *dest, const char *src, size_t n)
135{ 112{
136 size_t bos = __builtin_object_size(dest, 0); 113 size_t bos = __builtin_object_size(dest, 0);
137 114
138 if (__builtin_constant_p(n) && n > bos)
139 __strncpy_error();
140
141 if (n > bos) 115 if (n > bos)
142 __builtin_trap(); 116 __builtin_trap();
143 return strncpy(dest, src, n); 117 return strncpy(dest, src, n);
144} 118}
145 119
146#ifdef _GNU_SOURCE 120#ifdef _GNU_SOURCE
147__errordecl(__mempcpy_error, "mempcpy: buffer overflow detected");
148static inline __attribute__ ((always_inline)) 121static inline __attribute__ ((always_inline))
149void * 122void *
150__fortify_mempcpy(void *dest, const void *src, size_t n) 123__fortify_mempcpy(void *dest, const void *src, size_t n)
151{ 124{
152 size_t bos = __builtin_object_size(dest, 0); 125 size_t bos = __builtin_object_size(dest, 0);
153 126
154 if (__builtin_constant_p(n) && n > bos)
155 __mempcpy_error();
156
157 if (n > bos) 127 if (n > bos)
158 __builtin_trap(); 128 __builtin_trap();
159 return mempcpy(dest, src, n); 129 return mempcpy(dest, src, n);
@@ -161,31 +131,23 @@ __fortify_mempcpy(void *dest, const void *src, size_t n)
161#endif 131#endif
162 132
163#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 133#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
164__errordecl(__strlcat_error, "strlcat: buffer overflow detected");
165static inline __attribute__ ((always_inline)) 134static inline __attribute__ ((always_inline))
166size_t 135size_t
167__fortify_strlcat(char *dest, const char *src, size_t n) 136__fortify_strlcat(char *dest, const char *src, size_t n)
168{ 137{
169 size_t bos = __builtin_object_size(dest, 0); 138 size_t bos = __builtin_object_size(dest, 0);
170 139
171 if (__builtin_constant_p(n) && n > bos)
172 __strlcat_error();
173
174 if (n > bos) 140 if (n > bos)
175 __builtin_trap(); 141 __builtin_trap();
176 return strlcat(dest, src, n); 142 return strlcat(dest, src, n);
177} 143}
178 144
179__errordecl(__strlcpy_error, "strlcpy: buffer overflow detected");
180static inline __attribute__ ((always_inline)) 145static inline __attribute__ ((always_inline))
181size_t 146size_t
182__fortify_strlcpy(char *dest, const char *src, size_t n) 147__fortify_strlcpy(char *dest, const char *src, size_t n)
183{ 148{
184 size_t bos = __builtin_object_size(dest, 0); 149 size_t bos = __builtin_object_size(dest, 0);
185 150
186 if (__builtin_constant_p(n) && n > bos)
187 __strlcpy_error();
188
189 if (n > bos) 151 if (n > bos)
190 __builtin_trap(); 152 __builtin_trap();
191 return strlcpy(dest, src, n); 153 return strlcpy(dest, src, n);
@@ -223,8 +185,6 @@ __fortify_strlcpy(char *dest, const char *src, size_t n)
223#define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n) 185#define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n)
224#endif 186#endif
225 187
226#undef __errordecl
227
228#endif 188#endif
229 189
230#endif 190#endif
diff --git a/include/strings.h b/include/strings.h
index e23c8eb..943b565 100644
--- a/include/strings.h
+++ b/include/strings.h
@@ -5,36 +5,26 @@
5 5
6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
7 7
8#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg)))
9
10#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_SOURCE) \ 8#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_SOURCE) \
11 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE+0 < 200809L) \ 9 || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE+0 < 200809L) \
12 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700) 10 || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700)
13__errordecl(__bcopy_error, "bcopy: buffer overflow detected");
14static inline __attribute__ ((always_inline)) 11static inline __attribute__ ((always_inline))
15void 12void
16__fortify_bcopy(const void *src, void *dest, size_t n) 13__fortify_bcopy(const void *src, void *dest, size_t n)
17{ 14{
18 size_t bos = __builtin_object_size(dest, 0); 15 size_t bos = __builtin_object_size(dest, 0);
19 16
20 if (__builtin_constant_p(n) && n > bos)
21 __bcopy_error();
22
23 if (n > bos) 17 if (n > bos)
24 __builtin_trap(); 18 __builtin_trap();
25 return bcopy(src, dest, n); 19 return bcopy(src, dest, n);
26} 20}
27 21
28__errordecl(__bzero_error, "bzero: buffer overflow detected");
29static inline __attribute__ ((always_inline)) 22static inline __attribute__ ((always_inline))
30void 23void
31__fortify_bzero(void *src, size_t n) 24__fortify_bzero(void *src, size_t n)
32{ 25{
33 size_t bos = __builtin_object_size(src, 0); 26 size_t bos = __builtin_object_size(src, 0);
34 27
35 if (__builtin_constant_p(n) && n > bos)
36 __bzero_error();
37
38 if (n > bos) 28 if (n > bos)
39 __builtin_trap(); 29 __builtin_trap();
40 return bzero(src, n); 30 return bzero(src, n);
@@ -46,8 +36,6 @@ __fortify_bzero(void *src, size_t n)
46#define bzero(src, n) __fortify_bzero(src, n) 36#define bzero(src, n) __fortify_bzero(src, n)
47#endif 37#endif
48 38
49#undef __errordecl
50
51#endif 39#endif
52 40
53#endif 41#endif
diff --git a/include/sys/socket.h b/include/sys/socket.h
index e8f8db6..85ba63b 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -5,33 +5,23 @@
5 5
6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
7 7
8#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg)))
9
10__errordecl(__recv_error, "recv: buffer overflow detected");
11static inline __attribute__ ((always_inline)) 8static inline __attribute__ ((always_inline))
12ssize_t 9ssize_t
13__fortify_recv(int sockfd, void *buf, size_t n, int flags) 10__fortify_recv(int sockfd, void *buf, size_t n, int flags)
14{ 11{
15 size_t bos = __builtin_object_size(buf, 0); 12 size_t bos = __builtin_object_size(buf, 0);
16 13
17 if (__builtin_constant_p(n) && n > bos)
18 __recv_error();
19
20 if (n > bos) 14 if (n > bos)
21 __builtin_trap(); 15 __builtin_trap();
22 return recv(sockfd, buf, n, flags); 16 return recv(sockfd, buf, n, flags);
23} 17}
24 18
25__errordecl(__recvfrom_error, "recvfrom: buffer overflow detected");
26static inline __attribute__ ((always_inline)) 19static inline __attribute__ ((always_inline))
27ssize_t 20ssize_t
28__fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, struct sockaddr *sa, socklen_t *salen) 21__fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, struct sockaddr *sa, socklen_t *salen)
29{ 22{
30 size_t bos = __builtin_object_size(buf, 0); 23 size_t bos = __builtin_object_size(buf, 0);
31 24
32 if (__builtin_constant_p(n) && n > bos)
33 __recvfrom_error();
34
35 if (n > bos) 25 if (n > bos)
36 __builtin_trap(); 26 __builtin_trap();
37 return recvfrom(sockfd, buf, n, flags, sa, salen); 27 return recvfrom(sockfd, buf, n, flags, sa, salen);
@@ -42,8 +32,6 @@ __fortify_recvfrom(int sockfd, void *buf, size_t n, int flags, struct sockaddr *
42#undef recvfrom 32#undef recvfrom
43#define recvfrom(sockfd, buf, n, flags, sa, salen) __fortify_recvfrom(sockfd, buf, n, flags, sa, salen) 33#define recvfrom(sockfd, buf, n, flags, sa, salen) __fortify_recvfrom(sockfd, buf, n, flags, sa, salen)
44 34
45#undef __errordecl
46
47#endif 35#endif
48 36
49#endif 37#endif
diff --git a/include/unistd.h b/include/unistd.h
index bffccf7..b13b507 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -5,78 +5,56 @@
5 5
6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
7 7
8#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg)))
9
10__errordecl(__confstr_error, "confstr: buffer overflow detected");
11static inline __attribute__ ((always_inline)) 8static inline __attribute__ ((always_inline))
12size_t 9size_t
13__fortify_confstr(int name, char *buf, size_t len) 10__fortify_confstr(int name, char *buf, size_t len)
14{ 11{
15 size_t bos = __builtin_object_size(buf, 0); 12 size_t bos = __builtin_object_size(buf, 0);
16 13
17 if (__builtin_constant_p(len) && len > bos)
18 __confstr_error();
19
20 if (len > bos) 14 if (len > bos)
21 __builtin_trap(); 15 __builtin_trap();
22 return confstr(name, buf, len); 16 return confstr(name, buf, len);
23} 17}
24 18
25__errordecl(__getcwd_error, "getcwd: buffer overflow detected");
26static inline __attribute__ ((always_inline)) 19static inline __attribute__ ((always_inline))
27char * 20char *
28__fortify_getcwd(char *buf, size_t len) 21__fortify_getcwd(char *buf, size_t len)
29{ 22{
30 size_t bos = __builtin_object_size(buf, 0); 23 size_t bos = __builtin_object_size(buf, 0);
31 24
32 if (__builtin_constant_p(len) && len > bos)
33 __getcwd_error();
34
35 if (len > bos) 25 if (len > bos)
36 __builtin_trap(); 26 __builtin_trap();
37 return getcwd(buf, len); 27 return getcwd(buf, len);
38} 28}
39 29
40__errordecl(__gethostname_error, "gethostname: buffer overflow detected");
41static inline __attribute__ ((always_inline)) 30static inline __attribute__ ((always_inline))
42int 31int
43__fortify_gethostname(char *name, size_t len) 32__fortify_gethostname(char *name, size_t len)
44{ 33{
45 size_t bos = __builtin_object_size(name, 0); 34 size_t bos = __builtin_object_size(name, 0);
46 35
47 if (__builtin_constant_p(len) && len > bos)
48 __gethostname_error();
49
50 if (len > bos) 36 if (len > bos)
51 __builtin_trap(); 37 __builtin_trap();
52 return gethostname(name, len); 38 return gethostname(name, len);
53} 39}
54 40
55__errordecl(__pread_error, "pread: buffer overflow detected");
56static inline __attribute__ ((always_inline)) 41static inline __attribute__ ((always_inline))
57ssize_t 42ssize_t
58__fortify_pread(int fd, void *buf, size_t n, off_t offset) 43__fortify_pread(int fd, void *buf, size_t n, off_t offset)
59{ 44{
60 size_t bos = __builtin_object_size(buf, 0); 45 size_t bos = __builtin_object_size(buf, 0);
61 46
62 if (__builtin_constant_p(n) && n > bos)
63 __pread_error();
64
65 if (n > bos) 47 if (n > bos)
66 __builtin_trap(); 48 __builtin_trap();
67 return pread(fd, buf, n, offset); 49 return pread(fd, buf, n, offset);
68} 50}
69 51
70__errordecl(__read_error, "read: buffer overflow detected");
71static inline __attribute__ ((always_inline)) 52static inline __attribute__ ((always_inline))
72ssize_t 53ssize_t
73__fortify_read(int fd, void *buf, size_t n) 54__fortify_read(int fd, void *buf, size_t n)
74{ 55{
75 size_t bos = __builtin_object_size(buf, 0); 56 size_t bos = __builtin_object_size(buf, 0);
76 57
77 if (__builtin_constant_p(n) && n > bos)
78 __read_error();
79
80 if (n > bos) 58 if (n > bos)
81 __builtin_trap(); 59 __builtin_trap();
82 return read(fd, buf, n); 60 return read(fd, buf, n);
@@ -93,8 +71,6 @@ __fortify_read(int fd, void *buf, size_t n)
93#undef read 71#undef read
94#define read(fd, buf, n) __fortify_read(fd, buf, n) 72#define read(fd, buf, n) __fortify_read(fd, buf, n)
95 73
96#undef __errordecl
97
98#endif 74#endif
99 75
100#endif 76#endif