blob: 5f8709830cf1ac3de3d441c9b61dedf970d9d5c0 [file] [log] [blame]
George Burgess IV9a274102019-06-04 15:39:52 -07001/*
2 * Copyright (C) 2019 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#ifndef __clang__
18#error "Non-clang isn't supported"
19#endif
20
21// Clang compile-time and run-time tests for Bionic's FORTIFY.
22//
23// This file is compiled in two configurations ways to give us a sane set of tests for clang's
24// FORTIFY implementation.
25//
26// One configuration uses clang's diagnostic consumer
27// (https://clang.llvm.org/doxygen/classclang_1_1VerifyDiagnosticConsumer.html#details)
28// to check diagnostics (e.g. the expected-* comments everywhere).
29//
30// Please note that this test does things like leaking memory. That's WAI.
31
32// Silence all "from 'diagnose_if'" `note`s from anywhere, including headers; they're uninteresting
33// for this test case, and their line numbers may change over time.
34// expected-note@* 0+{{from 'diagnose_if'}}
35//
36// Similarly, there are a few overload tricks we have to emit errors. Ignore any notes from those.
37// expected-note@* 0+{{candidate function}}
38
39#ifndef _FORTIFY_SOURCE
40#error "_FORTIFY_SOURCE must be defined"
41#endif
42
43#include <sys/cdefs.h>
44
45// This is a test specifically of bionic's FORTIFY machinery. Other stdlibs need not apply.
46#ifndef __BIONIC__
47// expected-no-diagnostics
48#else
49
50// As alluded to above, we're going to be doing some obviously very broken things in this file.
51// FORTIFY helpfully flags a lot of it at compile-time, but we want it to *actually* crash, too. So
52// let's wipe out any build-time errors.
53#ifndef COMPILATION_TESTS
54#undef __clang_error_if
55#define __clang_error_if(...)
56#undef __clang_warning_if
57#define __clang_warning_if(...)
George Burgess IV26d25a22019-06-06 17:45:05 -070058
59// SOMETIMES_CONST allows clang to emit eager diagnostics when we're doing compilation tests, but
60// blocks them otherwise. This is needed for diagnostics emitted with __enable_if.
61#define SOMETIMES_CONST volatile
62#else
63#define SOMETIMES_CONST const
George Burgess IV9a274102019-06-04 15:39:52 -070064#endif
65
66#include <err.h>
67#include <fcntl.h>
68#include <limits.h>
69#include <poll.h>
70#include <signal.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74#include <sys/socket.h>
75#include <sys/stat.h>
76#include <sys/wait.h>
77#include <syslog.h>
78#include <unistd.h>
79#include <wchar.h>
80
81#ifndef COMPILATION_TESTS
82#include <gtest/gtest.h>
83#include "BionicDeathTest.h"
84
85#define CONCAT2(x, y) x##y
86#define CONCAT(x, y) CONCAT2(x, y)
87#define FORTIFY_TEST_NAME CONCAT(clang_fortify_test_, _FORTIFY_SOURCE)
88
89namespace {
90struct FORTIFY_TEST_NAME : BionicDeathTest {
91 protected:
92 void SetUp() override {
93 stdin_saved = dup(STDIN_FILENO);
94 if (stdin_saved < 0) err(1, "failed to dup stdin");
95
96 int devnull = open("/dev/null", O_RDONLY);
97 if (devnull < 0) err(1, "failed to open /dev/null");
98
99 if (!dup2(devnull, STDIN_FILENO)) err(1, "failed to overwrite stdin");
100 static_cast<void>(close(devnull));
101
102 BionicDeathTest::SetUp();
103 }
104
105 void TearDown() override {
106 if (stdin_saved == -1) return;
107 if (!dup2(stdin_saved, STDIN_FILENO)) warn("failed to restore stdin");
108
109 static_cast<void>(close(stdin_saved));
110
111 BionicDeathTest::TearDown();
112 }
113
114 private:
115 int stdin_saved = -1;
116};
117} // namespace
118
119template <typename Fn>
120__attribute__((noreturn)) static void ExitAfter(Fn&& f) {
121 f();
122 // No need to tear things down; our parent process should handle that.
123 _exit(0);
124}
125
126// In any case (including failing tests), we always want to die after this.
127#define DIE_WITH(expr, cond, regex) EXPECT_EXIT(ExitAfter([&] { (expr); }), cond, regex)
128
129// EXPECT_NO_DEATH forks so that the test remains alive on a bug, and so that the environment
130// doesn't get modified on no bug. (Environment modification is especially tricky to deal with given
131// the *_STRUCT variants below.)
132#define EXPECT_NO_DEATH(expr) DIE_WITH(expr, testing::ExitedWithCode(0), "")
133#define EXPECT_FORTIFY_DEATH(expr) DIE_WITH(expr, testing::KilledBySignal(SIGABRT), "FORTIFY")
134// Expecting death, but only if we're doing a "strict" struct-checking mode.
135#if _FORTIFY_SOURCE > 1
136#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
137#else
138#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_NO_DEATH
139#endif
140
141#define FORTIFY_TEST(test_name) TEST(FORTIFY_TEST_NAME, test_name)
142
143#else // defined(COMPILATION_TESTS)
144
145#define EXPECT_NO_DEATH(expr) expr
146#define EXPECT_FORTIFY_DEATH(expr) expr
147#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
148#define FORTIFY_TEST(test_name) void test_name()
149#endif
150
151const static int kBogusFD = -1;
152
153FORTIFY_TEST(string) {
154 char small_buffer[8] = {};
155
156 {
157 char large_buffer[sizeof(small_buffer) + 1] = {};
158 // expected-error@+1{{size bigger than buffer}}
159 EXPECT_FORTIFY_DEATH(memcpy(small_buffer, large_buffer, sizeof(large_buffer)));
160 // expected-error@+1{{size bigger than buffer}}
161 EXPECT_FORTIFY_DEATH(memmove(small_buffer, large_buffer, sizeof(large_buffer)));
162 // FIXME: this should be EXPECT_FORTIFY_DEATH
163#if 0
164 // expected-error@+1{{called with bigger length than the destination}}
165#endif
166 EXPECT_NO_DEATH(mempcpy(small_buffer, large_buffer, sizeof(large_buffer)));
167 // expected-error@+1{{size bigger than buffer}}
168 EXPECT_FORTIFY_DEATH(memset(small_buffer, 0, sizeof(large_buffer)));
169 // expected-warning@+1{{arguments got flipped?}}
170 EXPECT_NO_DEATH(memset(small_buffer, sizeof(small_buffer), 0));
171 // FIXME: Should these be warnings?
172 // expected-warning@+1{{will always overflow}}
173 EXPECT_FORTIFY_DEATH(bcopy(large_buffer, small_buffer, sizeof(large_buffer)));
174 // expected-warning@+1{{will always overflow}}
175 EXPECT_FORTIFY_DEATH(bzero(small_buffer, sizeof(large_buffer)));
176 }
177
178 {
179 const char large_string[] = "Hello!!!";
180 static_assert(sizeof(large_string) > sizeof(small_buffer), "");
181
182 // expected-error@+1{{string bigger than buffer}}
183 EXPECT_FORTIFY_DEATH(strcpy(small_buffer, large_string));
184 // expected-error@+1{{string bigger than buffer}}
185 EXPECT_FORTIFY_DEATH(stpcpy(small_buffer, large_string));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700186 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700187 EXPECT_FORTIFY_DEATH(strncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700188 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700189 EXPECT_FORTIFY_DEATH(stpncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700190 // expected-error@+1{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700191 EXPECT_FORTIFY_DEATH(strcat(small_buffer, large_string));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700192 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700193 EXPECT_FORTIFY_DEATH(strncat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700194 // expected-error@+1{{size bigger than buffer}}
195 EXPECT_FORTIFY_DEATH(strlcpy(small_buffer, large_string, sizeof(large_string)));
196 // expected-error@+1{{size bigger than buffer}}
197 EXPECT_FORTIFY_DEATH(strlcat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700198 }
199
200 {
201 struct {
202 char tiny_buffer[4];
203 char tiny_buffer2[4];
204 } split = {};
205
206 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
207 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
208 EXPECT_NO_DEATH(memmove(split.tiny_buffer, &split, sizeof(split)));
209 EXPECT_NO_DEATH(mempcpy(split.tiny_buffer, &split, sizeof(split)));
210 EXPECT_NO_DEATH(memset(split.tiny_buffer, 0, sizeof(split)));
211
212 EXPECT_NO_DEATH(bcopy(&split, split.tiny_buffer, sizeof(split)));
213 EXPECT_NO_DEATH(bzero(split.tiny_buffer, sizeof(split)));
214
215 const char small_string[] = "Hi!!";
216 static_assert(sizeof(small_string) > sizeof(split.tiny_buffer), "");
217
218#if _FORTIFY_SOURCE > 1
219 // expected-error@+2{{string bigger than buffer}}
220#endif
221 EXPECT_FORTIFY_DEATH_STRUCT(strcpy(split.tiny_buffer, small_string));
222
223#if _FORTIFY_SOURCE > 1
224 // expected-error@+2{{string bigger than buffer}}
225#endif
226 EXPECT_FORTIFY_DEATH_STRUCT(stpcpy(split.tiny_buffer, small_string));
227
228#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700229 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700230#endif
231 EXPECT_FORTIFY_DEATH_STRUCT(strncpy(split.tiny_buffer, small_string, sizeof(small_string)));
232
233#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700234 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700235#endif
236 EXPECT_FORTIFY_DEATH_STRUCT(stpncpy(split.tiny_buffer, small_string, sizeof(small_string)));
237
238#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700239 // expected-error@+2{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700240#endif
241 EXPECT_FORTIFY_DEATH_STRUCT(strcat(split.tiny_buffer, small_string));
242
243#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700244 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700245#endif
246 EXPECT_FORTIFY_DEATH_STRUCT(strncat(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700247
248#if _FORTIFY_SOURCE > 1
249 // expected-error@+2{{size bigger than buffer}}
250#endif
251 EXPECT_FORTIFY_DEATH_STRUCT(strlcat(split.tiny_buffer, small_string, sizeof(small_string)));
252
253#if _FORTIFY_SOURCE > 1
254 // expected-error@+2{{size bigger than buffer}}
255#endif
256 EXPECT_FORTIFY_DEATH_STRUCT(strlcpy(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700257 }
258}
259
George Burgess IV2356c932019-06-06 17:18:13 -0700260FORTIFY_TEST(fcntl) {
261 const char target[] = "/dev/null";
262 int dirfd = 0;
263
264 // These all emit hard errors without diagnose_if, so running them is a bit
265 // more involved.
266#ifdef COMPILATION_TESTS
267 // expected-error@+1{{too many arguments}}
268 open("/", 0, 0, 0);
269 // expected-error@+1{{too many arguments}}
270 open64("/", 0, 0, 0);
271 // expected-error@+1{{too many arguments}}
272 openat(0, "/", 0, 0, 0);
273 // expected-error@+1{{too many arguments}}
274 openat64(0, "/", 0, 0, 0);
275#endif
276
277 // expected-error@+1{{missing mode}}
278 EXPECT_FORTIFY_DEATH(open(target, O_CREAT));
279 // expected-error@+1{{missing mode}}
280 EXPECT_FORTIFY_DEATH(open(target, O_TMPFILE));
281 // expected-error@+1{{missing mode}}
282 EXPECT_FORTIFY_DEATH(open64(target, O_CREAT));
283 // expected-error@+1{{missing mode}}
284 EXPECT_FORTIFY_DEATH(open64(target, O_TMPFILE));
285 // expected-error@+1{{missing mode}}
286 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_CREAT));
287 // expected-error@+1{{missing mode}}
288 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_TMPFILE));
289 // expected-error@+1{{missing mode}}
290 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_CREAT));
291 // expected-error@+1{{missing mode}}
292 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_TMPFILE));
293
294 // expected-warning@+1{{superfluous mode bits}}
295 EXPECT_NO_DEATH(open(target, O_RDONLY, 0777));
296 // expected-warning@+1{{superfluous mode bits}}
297 EXPECT_NO_DEATH(open64(target, O_RDONLY, 0777));
298 // expected-warning@+1{{superfluous mode bits}}
299 EXPECT_NO_DEATH(openat(dirfd, target, O_RDONLY, 0777));
300 // expected-warning@+1{{superfluous mode bits}}
301 EXPECT_NO_DEATH(openat64(dirfd, target, O_RDONLY, 0777));
302}
303
George Burgess IV9a274102019-06-04 15:39:52 -0700304// Since these emit hard errors, it's sort of hard to run them...
305#ifdef COMPILATION_TESTS
306namespace compilation_tests {
307template <typename T>
308static T declval() {
309 __builtin_unreachable();
310}
311
George Burgess IV9a274102019-06-04 15:39:52 -0700312static void testFormatStrings() {
313 const auto unsigned_value = declval<unsigned long long>();
314 const auto* unknown_string = declval<const char*>();
George Burgess IV06bb4ce2019-06-13 15:13:02 -0700315 const auto va = *declval<va_list*>();
George Burgess IV9a274102019-06-04 15:39:52 -0700316
317 {
318 auto some_fd = declval<int>();
319 // expected-warning@+1{{format specifies type 'int'}}
320 dprintf(some_fd, "%d", unsigned_value);
321 // expected-warning@+1{{format string is not a string literal}}
322 dprintf(some_fd, unknown_string, unsigned_value);
323 // expected-warning@+1{{format string is not a string literal}}
324 vdprintf(1, unknown_string, va);
325 }
326
327 {
328 auto* retval = declval<char*>();
329#if 0
330 // expected-error@+2{{ignoring return value}}
331#endif
332 // expected-warning@+1{{format specifies type 'int'}}
333 asprintf(&retval, "%d", unsigned_value);
334#if 0
335 // expected-error@+2{{ignoring return value}}
336#endif
337 // expected-warning@+1{{format string is not a string literal}}
338 asprintf(&retval, unknown_string, unsigned_value);
339#if 0
340 // expected-error@+2{{ignoring return value}}
341#endif
342 // expected-warning@+1{{format string is not a string literal}}
343 vasprintf(&retval, unknown_string, va);
344 }
345
346 // expected-warning@+1{{format specifies type 'int'}}
347 syslog(0, "%d", unsigned_value);
348 // expected-warning@+1{{format string is not a string literal}}
349 syslog(0, unknown_string, unsigned_value);
350 // expected-warning@+1{{format string is not a string literal}}
351 vsyslog(0, unknown_string, va);
352
353 {
354 auto* file = declval<FILE*>();
355 // expected-warning@+1{{format specifies type 'int'}}
356 fprintf(file, "%d", unsigned_value);
357 // expected-warning@+1{{format string is not a string literal}}
358 fprintf(file, unknown_string, unsigned_value);
359 // expected-warning@+1{{format string is not a string literal}}
360 vfprintf(file, unknown_string, va);
361 }
362
363 // expected-warning@+1{{format specifies type 'int'}}
364 printf("%d", unsigned_value);
365 // expected-warning@+1{{format string is not a string literal}}
366 printf(unknown_string, unsigned_value);
367 // expected-warning@+1{{format string is not a string literal}}
368 vprintf(unknown_string, va);
369
370 {
371 char buf[128];
372 // expected-warning@+1{{format specifies type 'int'}}
373 sprintf(buf, "%d", unsigned_value);
374 // expected-warning@+1{{format string is not a string literal}}
375 sprintf(buf, unknown_string, unsigned_value);
376 // expected-warning@+1{{format string is not a string literal}}
377 sprintf(buf, unknown_string, va);
378
379 // expected-warning@+1{{format specifies type 'int'}}
380 snprintf(buf, sizeof(buf), "%d", unsigned_value);
381 // expected-warning@+1{{format string is not a string literal}}
382 snprintf(buf, sizeof(buf), unknown_string, unsigned_value);
383 // expected-warning@+1{{format string is not a string literal}}
384 vsnprintf(buf, sizeof(buf), unknown_string, va);
385 }
386
387 // FIXME: below are general format string cases where clang should probably try to warn.
388 {
389 char buf[4];
390 sprintf(buf, "%s", "1234");
391 sprintf(buf, "1%s4", "23");
392 sprintf(buf, "%d", 1234);
393
394 // Similar thoughts for strncpy, etc.
395 }
396}
397
398static void testStdlib() {
399 char path_buffer[PATH_MAX - 1];
George Burgess IV8c0ec112019-06-06 17:23:32 -0700400 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700401 // expected-error@+1{{must be NULL or a pointer to a buffer with >= PATH_MAX bytes}}
402 realpath("/", path_buffer);
George Burgess IV8c0ec112019-06-06 17:23:32 -0700403 // expected-warning@+1{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700404 realpath("/", nullptr);
405
George Burgess IV8c0ec112019-06-06 17:23:32 -0700406 // expected-warning@+2{{ignoring return value of function}}
407 // expected-error@+1{{flipped arguments?}}
George Burgess IV9a274102019-06-04 15:39:52 -0700408 realpath(nullptr, path_buffer);
409
George Burgess IV8c0ec112019-06-06 17:23:32 -0700410 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700411 // expected-error@+1{{flipped arguments?}}
412 realpath(nullptr, nullptr);
413}
414} // namespace compilation_tests
415#endif
416
417FORTIFY_TEST(poll) {
418 int pipe_fds[2];
419 if (pipe(pipe_fds)) err(1, "pipe failed");
420
421 // after this, pipe_fds[0] should always report RDHUP
422 if (close(pipe_fds[1])) err(1, "close failed");
423
424 struct pollfd poll_fd = { pipe_fds[0], POLLRDHUP, 0 };
425 {
426 struct pollfd few_fds[] = { poll_fd, poll_fd };
427 // expected-error@+1{{fd_count is larger than the given buffer}}
428 EXPECT_FORTIFY_DEATH(poll(few_fds, 3, 0));
429 // expected-error@+1{{fd_count is larger than the given buffer}}
430 EXPECT_FORTIFY_DEATH(ppoll(few_fds, 3, 0, 0));
431 // expected-error@+1{{fd_count is larger than the given buffer}}
432 EXPECT_FORTIFY_DEATH(ppoll64(few_fds, 3, 0, nullptr));
433 }
434
435 {
436 struct {
437 struct pollfd few[2];
438 struct pollfd extra[1];
439 } fds = { { poll_fd, poll_fd }, { poll_fd } };
440 static_assert(sizeof(fds) >= sizeof(struct pollfd) * 3, "");
441
442#if _FORTIFY_SOURCE > 1
443 // expected-error@+2{{fd_count is larger than the given buffer}}
444#endif
445 EXPECT_FORTIFY_DEATH_STRUCT(poll(fds.few, 3, 0));
446
447 struct timespec timeout = {};
448#if _FORTIFY_SOURCE > 1
449 // expected-error@+2{{fd_count is larger than the given buffer}}
450#endif
451 EXPECT_FORTIFY_DEATH_STRUCT(ppoll(fds.few, 3, &timeout, 0));
452
453#if _FORTIFY_SOURCE > 1
454 // expected-error@+2{{fd_count is larger than the given buffer}}
455#endif
456 EXPECT_FORTIFY_DEATH_STRUCT(ppoll64(fds.few, 3, 0, nullptr));
457 }
458}
459
460FORTIFY_TEST(socket) {
461 {
462 char small_buffer[8];
463 // expected-error@+1{{size bigger than buffer}}
464 EXPECT_FORTIFY_DEATH(recv(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
465 // expected-error@+1{{size bigger than buffer}}
466 EXPECT_FORTIFY_DEATH(recvfrom(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
467
468 // expected-error@+1{{size bigger than buffer}}
469 EXPECT_FORTIFY_DEATH(send(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
470 // expected-error@+1{{size bigger than buffer}}
471 EXPECT_FORTIFY_DEATH(sendto(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
472 }
473
474 {
475 struct {
476 char tiny_buffer[4];
477 char tiny_buffer2;
478 } split = {};
479
480 EXPECT_NO_DEATH(recv(kBogusFD, split.tiny_buffer, sizeof(split), 0));
481 EXPECT_NO_DEATH(recvfrom(kBogusFD, split.tiny_buffer, sizeof(split), 0, 0, 0));
482 }
483}
484
485FORTIFY_TEST(sys_stat) {
486 // expected-error@+1{{'umask' called with invalid mode}}
487 EXPECT_FORTIFY_DEATH(umask(01777));
488}
489
490FORTIFY_TEST(stdio) {
491 char small_buffer[8] = {};
492 {
George Burgess IV26d25a22019-06-06 17:45:05 -0700493 // expected-error@+1{{size is larger than the destination buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700494 EXPECT_FORTIFY_DEATH(snprintf(small_buffer, sizeof(small_buffer) + 1, ""));
495
496 va_list va;
George Burgess IV26d25a22019-06-06 17:45:05 -0700497 // expected-error@+2{{size is larger than the destination buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700498 // expected-warning@+1{{format string is empty}}
499 EXPECT_FORTIFY_DEATH(vsnprintf(small_buffer, sizeof(small_buffer) + 1, "", va));
George Burgess IV26d25a22019-06-06 17:45:05 -0700500
501 const char *SOMETIMES_CONST format_string = "aaaaaaaaa";
502
503 // expected-error@+1{{format string will always overflow}}
504 EXPECT_FORTIFY_DEATH(sprintf(small_buffer, format_string));
George Burgess IV9a274102019-06-04 15:39:52 -0700505 }
506
507 // expected-error@+1{{size should not be negative}}
508 EXPECT_FORTIFY_DEATH(fgets(small_buffer, -1, stdin));
509 // expected-error@+1{{size is larger than the destination buffer}}
510 EXPECT_FORTIFY_DEATH(fgets(small_buffer, sizeof(small_buffer) + 1, stdin));
511
512 // expected-error@+1{{size * count overflows}}
513 EXPECT_NO_DEATH(fread(small_buffer, 2, (size_t)-1, stdin));
514 // expected-error@+1{{size * count is too large for the given buffer}}
515 EXPECT_FORTIFY_DEATH(fread(small_buffer, 1, sizeof(small_buffer) + 1, stdin));
516
517 // expected-error@+1{{size * count overflows}}
518 EXPECT_NO_DEATH(fwrite(small_buffer, 2, (size_t)-1, stdout));
519 // expected-error@+1{{size * count is too large for the given buffer}}
520 EXPECT_FORTIFY_DEATH(fwrite(small_buffer, 1, sizeof(small_buffer) + 1, stdout));
521}
522
523FORTIFY_TEST(unistd) {
524 char small_buffer[8];
525
526 // Return value warnings are (sort of) a part of FORTIFY, so we don't ignore them.
527#if 0
528 // expected-error@+2{{ignoring return value of function}}
529#endif
530 // expected-error@+1{{bytes overflows the given object}}
531 EXPECT_FORTIFY_DEATH(read(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
532#if 0
533 // expected-error@+2{{ignoring return value of function}}
534#endif
535 // expected-error@+1{{bytes overflows the given object}}
536 EXPECT_FORTIFY_DEATH(pread(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
537#if 0
538 // expected-error@+2{{ignoring return value of function}}
539#endif
540 // expected-error@+1{{bytes overflows the given object}}
541 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
542#if 0
543 // expected-error@+2{{ignoring return value of function}}
544#endif
545 // expected-error@+1{{bytes overflows the given object}}
546 EXPECT_FORTIFY_DEATH(write(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
547#if 0
548 // expected-error@+2{{ignoring return value of function}}
549#endif
550 // expected-error@+1{{bytes overflows the given object}}
551 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
552#if 0
553 // expected-error@+2{{ignoring return value of function}}
554#endif
555 // expected-error@+1{{bytes overflows the given object}}
556 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
557#if 0
558 // expected-error@+2{{ignoring return value of function}}
559#endif
560 // expected-error@+1{{bytes overflows the given object}}
561 EXPECT_FORTIFY_DEATH(readlink("/", small_buffer, sizeof(small_buffer) + 1));
562#if 0
563 // expected-error@+2{{ignoring return value of function}}
564#endif
565 // expected-error@+1{{bytes overflows the given object}}
566 EXPECT_FORTIFY_DEATH(getcwd(small_buffer, sizeof(small_buffer) + 1));
567
568 // getcwd allocates and returns a buffer if you pass null to getcwd
569 EXPECT_NO_DEATH(getcwd(nullptr, 0));
570 EXPECT_NO_DEATH(getcwd(nullptr, 4096));
571
572 struct {
573 char tiny_buffer[4];
574 char tiny_buffer2[4];
575 } split;
576
577 EXPECT_NO_DEATH(read(kBogusFD, split.tiny_buffer, sizeof(split)));
578 EXPECT_NO_DEATH(pread(kBogusFD, split.tiny_buffer, sizeof(split), 0));
579 EXPECT_NO_DEATH(pread64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
580 EXPECT_NO_DEATH(write(kBogusFD, split.tiny_buffer, sizeof(split)));
581 EXPECT_NO_DEATH(pwrite(kBogusFD, split.tiny_buffer, sizeof(split), 0));
582 EXPECT_NO_DEATH(pwrite64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
583
584#if _FORTIFY_SOURCE > 1
585 // expected-error@+2{{bytes overflows the given object}}
586#endif
587 EXPECT_FORTIFY_DEATH_STRUCT(readlink("/", split.tiny_buffer, sizeof(split)));
588#if _FORTIFY_SOURCE > 1
589 // expected-error@+2{{bytes overflows the given object}}
590#endif
591 EXPECT_FORTIFY_DEATH_STRUCT(getcwd(split.tiny_buffer, sizeof(split)));
592
593 {
George Burgess IV9a274102019-06-04 15:39:52 -0700594 char* volatile unknown = small_buffer;
595 const size_t count = static_cast<size_t>(SSIZE_MAX) + 1;
596 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
597 EXPECT_FORTIFY_DEATH(read(kBogusFD, unknown, count));
598 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
599 EXPECT_FORTIFY_DEATH(pread(kBogusFD, unknown, count, 0));
600 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
601 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, unknown, count, 0));
602 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
603 EXPECT_FORTIFY_DEATH(write(kBogusFD, unknown, count));
604 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
605 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, unknown, count, 0));
606 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
607 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, unknown, count, 0));
George Burgess IV9a274102019-06-04 15:39:52 -0700608 }
609}
610
611#endif // defined(__BIONIC__)