blob: 7def77ef6dc79905764a8687e2ef343dde1faaef [file] [log] [blame]
Dan Albert2fbb1b62014-10-08 11:21:32 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef _FORTIFY_SOURCE
18#define _FORTIFY_SOURCE 2
Yabin Cui20f22682015-03-03 20:27:58 -080019#include <fcntl.h>
20#include <netinet/in.h>
21#include <poll.h>
22#include <stdarg.h>
Dan Albert2fbb1b62014-10-08 11:21:32 -070023#include <stdio.h>
Daniel Micayafdd1542015-07-20 21:37:29 -040024#include <stdlib.h>
Yabin Cui20f22682015-03-03 20:27:58 -080025#include <string.h>
26#include <sys/socket.h>
27#include <sys/stat.h>
28#include <time.h>
29#include <unistd.h>
Dan Albert2fbb1b62014-10-08 11:21:32 -070030
31void test_sprintf() {
32 char buf[4];
33
34 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070035 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
George Burgess IV7cc779f2017-02-09 00:00:31 -080036 // CLANG: error: call to unavailable function 'sprintf': format string will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -080037 sprintf(buf, "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070038
39 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070040 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070041 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080042 sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070043}
44
45void test_snprintf() {
46 char buf[4];
47
48 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070049 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
George Burgess IV7cc779f2017-02-09 00:00:31 -080050 // CLANG: error: call to unavailable function 'snprintf': format string will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -080051 snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070052
53 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070054 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070055 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080056 snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070057
58 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070059 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070060 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080061 snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070062
63 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070064 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070065 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080066 snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf)
67}
68
69void test_memcpy() {
70 char buf[4];
71
72 // NOLINTNEXTLINE(whitespace/line_length)
73 // GCC: warning: call to void* __builtin___memcpy_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
George Burgess IVb6300462017-07-31 21:29:42 -070074 // CLANG: error: 'memcpy' called with size bigger than buffer
George Burgess IV7cc779f2017-02-09 00:00:31 -080075 memcpy(buf, "foobar", sizeof("foobar") + 100);
Yabin Cui20f22682015-03-03 20:27:58 -080076}
77
78void test_memmove() {
79 char buf[4];
80
81 // NOLINTNEXTLINE(whitespace/line_length)
82 // GCC: warning: call to void* __builtin___memmove_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
George Burgess IVb6300462017-07-31 21:29:42 -070083 // CLANG: error: 'memmove' called with size bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -080084 memmove(buf, "foobar", sizeof("foobar"));
85}
86
87void test_memset() {
88 char buf[4];
89
90 // NOLINTNEXTLINE(whitespace/line_length)
91 // GCC: warning: call to void* __builtin___memset_chk(void*, int, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
George Burgess IVb6300462017-07-31 21:29:42 -070092 // CLANG: error: 'memset' called with size bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -080093 memset(buf, 0, 6);
94}
95
96void test_strcpy() {
97 char buf[4];
98
99 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuif3bd3052015-03-04 21:43:14 -0800100 // GCC: warning: call to {{(char\* __builtin___strcpy_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
George Burgess IVb6300462017-07-31 21:29:42 -0700101 // CLANG: error: 'strcpy' called with string bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800102 strcpy(buf, "foobar"); // NOLINT(runtime/printf)
George Burgess IVb6300462017-07-31 21:29:42 -0700103
104 // NOLINTNEXTLINE(whitespace/line_length)
105 // GCC: warning: call to {{(char\* __builtin___strcpy_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
106 // CLANG: error: 'strcpy' called with string bigger than buffer
107 strcpy(buf, "quux");
Yabin Cui20f22682015-03-03 20:27:58 -0800108}
109
110void test_stpcpy() {
111 char buf[4];
112
113 // NOLINTNEXTLINE(whitespace/line_length)
114 // GCC: warning: call to char* __builtin___stpcpy_chk(char*, const char*, {{(long )?}}unsigned int) will always overflow destination buffer
George Burgess IVb6300462017-07-31 21:29:42 -0700115 // CLANG: error: 'stpcpy' called with string bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800116 stpcpy(buf, "foobar");
George Burgess IVb6300462017-07-31 21:29:42 -0700117
118 // NOLINTNEXTLINE(whitespace/line_length)
119 // GCC: warning: call to char* __builtin___stpcpy_chk(char*, const char*, {{(long )?}}unsigned int) will always overflow destination buffer
120 // CLANG: error: 'stpcpy' called with string bigger than buffer
121 stpcpy(buf, "quux");
Yabin Cui20f22682015-03-03 20:27:58 -0800122}
123
124void test_strncpy() {
125 char buf[4];
126
127 // NOLINTNEXTLINE(whitespace/line_length)
128 // GCC: warning: call to char* __builtin___strncpy_chk(char*, const char*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
129 // clang should emit a warning, but doesn't
130 strncpy(buf, "foobar", sizeof("foobar"));
131}
132
133void test_strcat() {
134 char buf[4] = "";
135
136 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuif3bd3052015-03-04 21:43:14 -0800137 // GCC: warning: call to {{(char\* __builtin___strcat_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800138 // clang should emit a warning, but doesn't
139 strcat(buf, "foobar"); // NOLINT(runtime/printf)
140}
141
142void test_strncat() {
143 char buf[4] = "";
144
145 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuif3bd3052015-03-04 21:43:14 -0800146 // GCC: warning: call to {{(char\* __builtin___strcat_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800147 // gcc output warning with __builtin___strcat_chk for __builtin___strncat_chk.
148 // clang should emit a warning, but doesn't
149 strncat(buf, "foobar", sizeof("foobar"));
150}
151
152void test_vsprintf(const char* fmt, ...) {
153 va_list va;
154 char buf[4];
155 va_start(va, fmt);
156
157 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuid9647592015-03-05 00:39:09 -0800158 // GCC: warning: call to int __builtin___vsprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)|(__va_list_tag\*)}}) will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800159 // clang should emit a warning, but doesn't
160 vsprintf(buf, "foobar", va);
161 va_end(va);
162}
163
164void test_vsnprintf(const char* fmt, ...) {
165 va_list va;
166 char buf[4];
167 va_start(va, fmt);
168
169 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuid9647592015-03-05 00:39:09 -0800170 // GCC: warning: call to int __builtin___vsnprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)|(__va_list_tag\*)}}) will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800171 // clang should emit a warning, but doesn't
172 vsnprintf(buf, 5, "foobar", va); // NOLINT(runtime/printf)
173
174 va_end(va);
175}
176
177void test_fgets() {
178 char buf[4];
179
180 // NOLINTNEXTLINE(whitespace/line_length)
181 // GCC: error: call to '__fgets_too_small_error' declared with attribute error: fgets called with size less than zero
George Burgess IV7cc779f2017-02-09 00:00:31 -0800182 // CLANG: error: call to unavailable function 'fgets': size is negative
Yabin Cui20f22682015-03-03 20:27:58 -0800183 fgets(buf, -1, stdin);
184
185 // NOLINTNEXTLINE(whitespace/line_length)
186 // GCC: error: call to '__fgets_too_big_error' declared with attribute error: fgets called with size bigger than buffer
George Burgess IV7cc779f2017-02-09 00:00:31 -0800187 // CLANG: error: call to unavailable function 'fgets': size is larger than the destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800188 fgets(buf, 6, stdin);
189}
190
191void test_recvfrom() {
192 char buf[4];
193 sockaddr_in addr;
194
195 // NOLINTNEXTLINE(whitespace/line_length)
196 // GCC: error: call to '__recvfrom_error' declared with attribute error: recvfrom called with size bigger than buffer
George Burgess IV156d5a82017-02-10 13:56:22 -0800197 // CLANG: error: call to unavailable function 'recvfrom': recvfrom called with size bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800198 recvfrom(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), NULL);
199}
200
201void test_umask() {
202 // NOLINTNEXTLINE(whitespace/line_length)
203 // GCC: error: call to '__umask_invalid_mode' declared with attribute error: umask called with invalid mode
George Burgess IV7cc779f2017-02-09 00:00:31 -0800204 // CLANG: error: call to unavailable function 'umask': umask called with invalid mode
Yabin Cui20f22682015-03-03 20:27:58 -0800205 umask(01777);
206}
207
208void test_read() {
209 char buf[4];
210 // NOLINTNEXTLINE(whitespace/line_length)
211 // GCC: error: call to '__read_dest_size_error' declared with attribute error: read called with size bigger than destination
George Burgess IV16c17392017-07-31 21:30:47 -0700212 // CLANG: error: in call to 'read', 'count' bytes overflows the given object
Yabin Cui20f22682015-03-03 20:27:58 -0800213 read(0, buf, 6);
214}
215
216void test_open() {
217 // NOLINTNEXTLINE(whitespace/line_length)
218 // GCC: error: call to '__creat_missing_mode' declared with attribute error: called with O_CREAT, but missing mode
George Burgess IV7cc779f2017-02-09 00:00:31 -0800219 // CLANG: error: call to unavailable function 'open': called with O_CREAT, but missing mode
Yabin Cui20f22682015-03-03 20:27:58 -0800220 open("/dev/null", O_CREAT);
221
222 // NOLINTNEXTLINE(whitespace/line_length)
223 // GCC: error: call to '__creat_too_many_args' declared with attribute error: too many arguments
George Burgess IV7cc779f2017-02-09 00:00:31 -0800224 // CLANG: error: call to unavailable function 'open': too many arguments
Yabin Cui20f22682015-03-03 20:27:58 -0800225 open("/dev/null", O_CREAT, 0, 0);
226}
227
228void test_poll() {
229 pollfd fds[1];
230 // NOLINTNEXTLINE(whitespace/line_length)
231 // GCC: error: call to '__poll_too_small_error' declared with attribute error: poll: pollfd array smaller than fd count
George Burgess IV7cc779f2017-02-09 00:00:31 -0800232 // CLANG: error: call to unavailable function 'poll': too many fds specified
Yabin Cui20f22682015-03-03 20:27:58 -0800233 poll(fds, 2, 0);
234}
235
236void test_ppoll() {
237 pollfd fds[1];
238 timespec timeout;
239 // NOLINTNEXTLINE(whitespace/line_length)
240 // GCC: error: call to '__ppoll_too_small_error' declared with attribute error: ppoll: pollfd array smaller than fd count
George Burgess IV7cc779f2017-02-09 00:00:31 -0800241 // CLANG: error: call to unavailable function 'ppoll': too many fds specified
Yabin Cui20f22682015-03-03 20:27:58 -0800242 ppoll(fds, 2, &timeout, NULL);
Dan Albert2fbb1b62014-10-08 11:21:32 -0700243}
Daniel Micayfed26592015-07-18 13:55:51 -0400244
245void test_fread_overflow() {
246 char buf[4];
247 // NOLINTNEXTLINE(whitespace/line_length)
248 // GCC: error: call to '__fread_overflow' declared with attribute error: fread called with overflowing size * count
George Burgess IV7cc779f2017-02-09 00:00:31 -0800249 // CLANG: error: call to unavailable function 'fread': size * count overflows
Daniel Micayfed26592015-07-18 13:55:51 -0400250 fread(buf, 2, (size_t)-1, stdin);
251}
252
253void test_fread_too_big() {
254 char buf[4];
255 // NOLINTNEXTLINE(whitespace/line_length)
256 // GCC: error: call to '__fread_too_big_error' declared with attribute error: fread called with size * count bigger than buffer
George Burgess IV7cc779f2017-02-09 00:00:31 -0800257 // CLANG: error: call to unavailable function 'fread': size * count is too large
Daniel Micayfed26592015-07-18 13:55:51 -0400258 fread(buf, 1, 5, stdin);
259}
260
261void test_fwrite_overflow() {
Daniel Micayafdd1542015-07-20 21:37:29 -0400262 char buf[4] = {0};
Daniel Micayfed26592015-07-18 13:55:51 -0400263 // NOLINTNEXTLINE(whitespace/line_length)
264 // GCC: error: call to '__fwrite_overflow' declared with attribute error: fwrite called with overflowing size * count
George Burgess IV7cc779f2017-02-09 00:00:31 -0800265 // CLANG: error: call to unavailable function 'fwrite': size * count overflows
Daniel Micayfed26592015-07-18 13:55:51 -0400266 fwrite(buf, 2, (size_t)-1, stdout);
267}
268
269void test_fwrite_too_big() {
270 char buf[4] = {0};
271 // NOLINTNEXTLINE(whitespace/line_length)
272 // GCC: error: call to '__fwrite_too_big_error' declared with attribute error: fwrite called with size * count bigger than buffer
George Burgess IV7cc779f2017-02-09 00:00:31 -0800273 // CLANG: error: call to unavailable function 'fwrite': size * count is too large
Daniel Micayfed26592015-07-18 13:55:51 -0400274 fwrite(buf, 1, 5, stdout);
275}
Daniel Micay9101b002015-05-20 15:31:26 -0400276
277void test_getcwd() {
278 char buf[4];
279 // NOLINTNEXTLINE(whitespace/line_length)
280 // GCC: error: call to '__getcwd_dest_size_error' declared with attribute error: getcwd called with size bigger than destination
George Burgess IV16c17392017-07-31 21:30:47 -0700281 // CLANG: error: in call to 'getcwd', 'size' bytes overflows the given object
Daniel Micay9101b002015-05-20 15:31:26 -0400282 getcwd(buf, 5);
283}
Daniel Micayafdd1542015-07-20 21:37:29 -0400284
285void test_pwrite64_size() {
286 char buf[4] = {0};
287 // NOLINTNEXTLINE(whitespace/line_length)
288 // GCC: error: call to '__pwrite64_dest_size_error' declared with attribute error: pwrite64 called with size bigger than destination
George Burgess IV16c17392017-07-31 21:30:47 -0700289 // CLANG: error: in call to 'pwrite64', 'count' bytes overflows the given object
Daniel Micayafdd1542015-07-20 21:37:29 -0400290 pwrite64(STDOUT_FILENO, buf, 5, 0);
291}
292
George Burgess IV7cc779f2017-02-09 00:00:31 -0800293void test_pwrite64_too_big_malloc() {
Daniel Micayafdd1542015-07-20 21:37:29 -0400294 void *buf = calloc(atoi("5"), 1);
295 // NOLINTNEXTLINE(whitespace/line_length)
296 // GCC: error: call to '__pwrite64_count_toobig_error' declared with attribute error: pwrite64 called with count > SSIZE_MAX
George Burgess IV7cc779f2017-02-09 00:00:31 -0800297 // clang should emit a warning, but probably never will.
298 pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0);
299}
300
301void test_pwrite64_too_big() {
302 char buf[4] = {0};
303 // NOLINTNEXTLINE(whitespace/line_length)
304 // GCC: error: call to '__pwrite64_count_toobig_error' declared with attribute error: pwrite64 called with count > SSIZE_MAX
George Burgess IV16c17392017-07-31 21:30:47 -0700305 // CLANG: error: in call to 'pwrite64', 'count' must be <= SSIZE_MAX
Daniel Micayafdd1542015-07-20 21:37:29 -0400306 pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0);
307}
308
309void test_write_size() {
310 char buf[4] = {0};
311 // NOLINTNEXTLINE(whitespace/line_length)
312 // GCC: error: call to '__write_dest_size_error' declared with attribute error: write called with size bigger than destination
George Burgess IV16c17392017-07-31 21:30:47 -0700313 // CLANG: error: in call to 'write', 'count' bytes overflows the given object
Daniel Micayafdd1542015-07-20 21:37:29 -0400314 write(STDOUT_FILENO, buf, 5);
315}
George Burgess IV7cc779f2017-02-09 00:00:31 -0800316
317void test_memset_args_flipped() {
318 char from[4] = {0};
319 // NOLINTNEXTLINE(whitespace/line_length)
George Burgess IVb6300462017-07-31 21:29:42 -0700320 // CLANG: 'memset' will set 0 bytes; maybe the arguments got flipped?
George Burgess IV7cc779f2017-02-09 00:00:31 -0800321 memset(from, sizeof(from), 0);
322}
Daniel Micay95b59c52017-02-13 17:27:59 -0800323
324void test_sendto() {
325 char buf[4] = {0};
326 sockaddr_in addr;
327
328 // NOLINTNEXTLINE(whitespace/line_length)
329 // GCC: error: call to '__sendto_error' declared with attribute error: sendto called with size bigger than buffer
330 // CLANG: error: call to unavailable function 'sendto': sendto called with size bigger than buffer
331 sendto(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr_in));
332}
333
334void test_send() {
335 char buf[4] = {0};
336
337 // NOLINTNEXTLINE(whitespace/line_length)
338 // GCC: error: call to '__sendto_error' declared with attribute error: sendto called with size bigger than buffer
339 // CLANG: error: call to unavailable function 'send': send called with size bigger than buffer
340 send(0, buf, 6, 0);
341}