blob: 291fc5a2a78902cf305e5581baa2e302b5246076 [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)));
George Burgess IV849c0b92019-06-10 16:22:09 -0700162 // expected-error@+1{{size bigger than buffer}}
163 EXPECT_FORTIFY_DEATH(mempcpy(small_buffer, large_buffer, sizeof(large_buffer)));
George Burgess IV9a274102019-06-04 15:39:52 -0700164 // expected-error@+1{{size bigger than buffer}}
165 EXPECT_FORTIFY_DEATH(memset(small_buffer, 0, sizeof(large_buffer)));
166 // expected-warning@+1{{arguments got flipped?}}
167 EXPECT_NO_DEATH(memset(small_buffer, sizeof(small_buffer), 0));
168 // FIXME: Should these be warnings?
169 // expected-warning@+1{{will always overflow}}
170 EXPECT_FORTIFY_DEATH(bcopy(large_buffer, small_buffer, sizeof(large_buffer)));
171 // expected-warning@+1{{will always overflow}}
172 EXPECT_FORTIFY_DEATH(bzero(small_buffer, sizeof(large_buffer)));
173 }
174
175 {
176 const char large_string[] = "Hello!!!";
177 static_assert(sizeof(large_string) > sizeof(small_buffer), "");
178
179 // expected-error@+1{{string bigger than buffer}}
180 EXPECT_FORTIFY_DEATH(strcpy(small_buffer, large_string));
181 // expected-error@+1{{string bigger than buffer}}
182 EXPECT_FORTIFY_DEATH(stpcpy(small_buffer, large_string));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700183 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700184 EXPECT_FORTIFY_DEATH(strncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700185 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700186 EXPECT_FORTIFY_DEATH(stpncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700187 // expected-error@+1{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700188 EXPECT_FORTIFY_DEATH(strcat(small_buffer, large_string));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700189 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700190 EXPECT_FORTIFY_DEATH(strncat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700191 // expected-error@+1{{size bigger than buffer}}
192 EXPECT_FORTIFY_DEATH(strlcpy(small_buffer, large_string, sizeof(large_string)));
193 // expected-error@+1{{size bigger than buffer}}
194 EXPECT_FORTIFY_DEATH(strlcat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700195 }
196
197 {
198 struct {
199 char tiny_buffer[4];
200 char tiny_buffer2[4];
201 } split = {};
202
203 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
204 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
205 EXPECT_NO_DEATH(memmove(split.tiny_buffer, &split, sizeof(split)));
206 EXPECT_NO_DEATH(mempcpy(split.tiny_buffer, &split, sizeof(split)));
207 EXPECT_NO_DEATH(memset(split.tiny_buffer, 0, sizeof(split)));
208
209 EXPECT_NO_DEATH(bcopy(&split, split.tiny_buffer, sizeof(split)));
210 EXPECT_NO_DEATH(bzero(split.tiny_buffer, sizeof(split)));
211
212 const char small_string[] = "Hi!!";
213 static_assert(sizeof(small_string) > sizeof(split.tiny_buffer), "");
214
215#if _FORTIFY_SOURCE > 1
216 // expected-error@+2{{string bigger than buffer}}
217#endif
218 EXPECT_FORTIFY_DEATH_STRUCT(strcpy(split.tiny_buffer, small_string));
219
220#if _FORTIFY_SOURCE > 1
221 // expected-error@+2{{string bigger than buffer}}
222#endif
223 EXPECT_FORTIFY_DEATH_STRUCT(stpcpy(split.tiny_buffer, small_string));
224
225#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700226 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700227#endif
228 EXPECT_FORTIFY_DEATH_STRUCT(strncpy(split.tiny_buffer, small_string, sizeof(small_string)));
229
230#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700231 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700232#endif
233 EXPECT_FORTIFY_DEATH_STRUCT(stpncpy(split.tiny_buffer, small_string, sizeof(small_string)));
234
235#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700236 // expected-error@+2{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700237#endif
238 EXPECT_FORTIFY_DEATH_STRUCT(strcat(split.tiny_buffer, small_string));
239
240#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700241 // expected-error@+2{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700242#endif
243 EXPECT_FORTIFY_DEATH_STRUCT(strncat(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700244
245#if _FORTIFY_SOURCE > 1
246 // expected-error@+2{{size bigger than buffer}}
247#endif
248 EXPECT_FORTIFY_DEATH_STRUCT(strlcat(split.tiny_buffer, small_string, sizeof(small_string)));
249
250#if _FORTIFY_SOURCE > 1
251 // expected-error@+2{{size bigger than buffer}}
252#endif
253 EXPECT_FORTIFY_DEATH_STRUCT(strlcpy(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700254 }
255}
256
George Burgess IV2356c932019-06-06 17:18:13 -0700257FORTIFY_TEST(fcntl) {
258 const char target[] = "/dev/null";
259 int dirfd = 0;
260
261 // These all emit hard errors without diagnose_if, so running them is a bit
262 // more involved.
263#ifdef COMPILATION_TESTS
264 // expected-error@+1{{too many arguments}}
265 open("/", 0, 0, 0);
266 // expected-error@+1{{too many arguments}}
267 open64("/", 0, 0, 0);
268 // expected-error@+1{{too many arguments}}
269 openat(0, "/", 0, 0, 0);
270 // expected-error@+1{{too many arguments}}
271 openat64(0, "/", 0, 0, 0);
272#endif
273
274 // expected-error@+1{{missing mode}}
275 EXPECT_FORTIFY_DEATH(open(target, O_CREAT));
276 // expected-error@+1{{missing mode}}
277 EXPECT_FORTIFY_DEATH(open(target, O_TMPFILE));
278 // expected-error@+1{{missing mode}}
279 EXPECT_FORTIFY_DEATH(open64(target, O_CREAT));
280 // expected-error@+1{{missing mode}}
281 EXPECT_FORTIFY_DEATH(open64(target, O_TMPFILE));
282 // expected-error@+1{{missing mode}}
283 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_CREAT));
284 // expected-error@+1{{missing mode}}
285 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_TMPFILE));
286 // expected-error@+1{{missing mode}}
287 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_CREAT));
288 // expected-error@+1{{missing mode}}
289 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_TMPFILE));
290
291 // expected-warning@+1{{superfluous mode bits}}
292 EXPECT_NO_DEATH(open(target, O_RDONLY, 0777));
293 // expected-warning@+1{{superfluous mode bits}}
294 EXPECT_NO_DEATH(open64(target, O_RDONLY, 0777));
295 // expected-warning@+1{{superfluous mode bits}}
296 EXPECT_NO_DEATH(openat(dirfd, target, O_RDONLY, 0777));
297 // expected-warning@+1{{superfluous mode bits}}
298 EXPECT_NO_DEATH(openat64(dirfd, target, O_RDONLY, 0777));
299}
300
George Burgess IV9a274102019-06-04 15:39:52 -0700301// Since these emit hard errors, it's sort of hard to run them...
302#ifdef COMPILATION_TESTS
303namespace compilation_tests {
304template <typename T>
305static T declval() {
306 __builtin_unreachable();
307}
308
George Burgess IV9a274102019-06-04 15:39:52 -0700309static void testFormatStrings() {
310 const auto unsigned_value = declval<unsigned long long>();
311 const auto* unknown_string = declval<const char*>();
George Burgess IV06bb4ce2019-06-13 15:13:02 -0700312 const auto va = *declval<va_list*>();
George Burgess IV9a274102019-06-04 15:39:52 -0700313
314 {
315 auto some_fd = declval<int>();
316 // expected-warning@+1{{format specifies type 'int'}}
317 dprintf(some_fd, "%d", unsigned_value);
318 // expected-warning@+1{{format string is not a string literal}}
319 dprintf(some_fd, unknown_string, unsigned_value);
320 // expected-warning@+1{{format string is not a string literal}}
321 vdprintf(1, unknown_string, va);
322 }
323
324 {
325 auto* retval = declval<char*>();
326#if 0
327 // expected-error@+2{{ignoring return value}}
328#endif
329 // expected-warning@+1{{format specifies type 'int'}}
330 asprintf(&retval, "%d", unsigned_value);
331#if 0
332 // expected-error@+2{{ignoring return value}}
333#endif
334 // expected-warning@+1{{format string is not a string literal}}
335 asprintf(&retval, unknown_string, unsigned_value);
336#if 0
337 // expected-error@+2{{ignoring return value}}
338#endif
339 // expected-warning@+1{{format string is not a string literal}}
340 vasprintf(&retval, unknown_string, va);
341 }
342
343 // expected-warning@+1{{format specifies type 'int'}}
344 syslog(0, "%d", unsigned_value);
345 // expected-warning@+1{{format string is not a string literal}}
346 syslog(0, unknown_string, unsigned_value);
347 // expected-warning@+1{{format string is not a string literal}}
348 vsyslog(0, unknown_string, va);
349
350 {
351 auto* file = declval<FILE*>();
352 // expected-warning@+1{{format specifies type 'int'}}
353 fprintf(file, "%d", unsigned_value);
354 // expected-warning@+1{{format string is not a string literal}}
355 fprintf(file, unknown_string, unsigned_value);
356 // expected-warning@+1{{format string is not a string literal}}
357 vfprintf(file, unknown_string, va);
358 }
359
360 // expected-warning@+1{{format specifies type 'int'}}
361 printf("%d", unsigned_value);
362 // expected-warning@+1{{format string is not a string literal}}
363 printf(unknown_string, unsigned_value);
364 // expected-warning@+1{{format string is not a string literal}}
365 vprintf(unknown_string, va);
366
367 {
368 char buf[128];
369 // expected-warning@+1{{format specifies type 'int'}}
370 sprintf(buf, "%d", unsigned_value);
371 // expected-warning@+1{{format string is not a string literal}}
372 sprintf(buf, unknown_string, unsigned_value);
373 // expected-warning@+1{{format string is not a string literal}}
374 sprintf(buf, unknown_string, va);
375
376 // expected-warning@+1{{format specifies type 'int'}}
377 snprintf(buf, sizeof(buf), "%d", unsigned_value);
378 // expected-warning@+1{{format string is not a string literal}}
379 snprintf(buf, sizeof(buf), unknown_string, unsigned_value);
380 // expected-warning@+1{{format string is not a string literal}}
381 vsnprintf(buf, sizeof(buf), unknown_string, va);
382 }
383
384 // FIXME: below are general format string cases where clang should probably try to warn.
385 {
386 char buf[4];
387 sprintf(buf, "%s", "1234");
388 sprintf(buf, "1%s4", "23");
389 sprintf(buf, "%d", 1234);
390
391 // Similar thoughts for strncpy, etc.
392 }
393}
394
395static void testStdlib() {
396 char path_buffer[PATH_MAX - 1];
George Burgess IV8c0ec112019-06-06 17:23:32 -0700397 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700398 // expected-error@+1{{must be NULL or a pointer to a buffer with >= PATH_MAX bytes}}
399 realpath("/", path_buffer);
George Burgess IV8c0ec112019-06-06 17:23:32 -0700400 // expected-warning@+1{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700401 realpath("/", nullptr);
402
George Burgess IV8c0ec112019-06-06 17:23:32 -0700403 // expected-warning@+2{{ignoring return value of function}}
404 // expected-error@+1{{flipped arguments?}}
George Burgess IV9a274102019-06-04 15:39:52 -0700405 realpath(nullptr, path_buffer);
406
George Burgess IV8c0ec112019-06-06 17:23:32 -0700407 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700408 // expected-error@+1{{flipped arguments?}}
409 realpath(nullptr, nullptr);
410}
411} // namespace compilation_tests
412#endif
413
414FORTIFY_TEST(poll) {
415 int pipe_fds[2];
416 if (pipe(pipe_fds)) err(1, "pipe failed");
417
418 // after this, pipe_fds[0] should always report RDHUP
419 if (close(pipe_fds[1])) err(1, "close failed");
420
421 struct pollfd poll_fd = { pipe_fds[0], POLLRDHUP, 0 };
422 {
423 struct pollfd few_fds[] = { poll_fd, poll_fd };
424 // expected-error@+1{{fd_count is larger than the given buffer}}
425 EXPECT_FORTIFY_DEATH(poll(few_fds, 3, 0));
426 // expected-error@+1{{fd_count is larger than the given buffer}}
427 EXPECT_FORTIFY_DEATH(ppoll(few_fds, 3, 0, 0));
428 // expected-error@+1{{fd_count is larger than the given buffer}}
429 EXPECT_FORTIFY_DEATH(ppoll64(few_fds, 3, 0, nullptr));
430 }
431
432 {
433 struct {
434 struct pollfd few[2];
435 struct pollfd extra[1];
436 } fds = { { poll_fd, poll_fd }, { poll_fd } };
437 static_assert(sizeof(fds) >= sizeof(struct pollfd) * 3, "");
438
439#if _FORTIFY_SOURCE > 1
440 // expected-error@+2{{fd_count is larger than the given buffer}}
441#endif
442 EXPECT_FORTIFY_DEATH_STRUCT(poll(fds.few, 3, 0));
443
444 struct timespec timeout = {};
445#if _FORTIFY_SOURCE > 1
446 // expected-error@+2{{fd_count is larger than the given buffer}}
447#endif
448 EXPECT_FORTIFY_DEATH_STRUCT(ppoll(fds.few, 3, &timeout, 0));
449
450#if _FORTIFY_SOURCE > 1
451 // expected-error@+2{{fd_count is larger than the given buffer}}
452#endif
453 EXPECT_FORTIFY_DEATH_STRUCT(ppoll64(fds.few, 3, 0, nullptr));
454 }
455}
456
457FORTIFY_TEST(socket) {
458 {
459 char small_buffer[8];
460 // expected-error@+1{{size bigger than buffer}}
461 EXPECT_FORTIFY_DEATH(recv(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
462 // expected-error@+1{{size bigger than buffer}}
463 EXPECT_FORTIFY_DEATH(recvfrom(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
464
465 // expected-error@+1{{size bigger than buffer}}
466 EXPECT_FORTIFY_DEATH(send(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
467 // expected-error@+1{{size bigger than buffer}}
468 EXPECT_FORTIFY_DEATH(sendto(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
469 }
470
471 {
472 struct {
473 char tiny_buffer[4];
474 char tiny_buffer2;
475 } split = {};
476
477 EXPECT_NO_DEATH(recv(kBogusFD, split.tiny_buffer, sizeof(split), 0));
478 EXPECT_NO_DEATH(recvfrom(kBogusFD, split.tiny_buffer, sizeof(split), 0, 0, 0));
479 }
480}
481
482FORTIFY_TEST(sys_stat) {
483 // expected-error@+1{{'umask' called with invalid mode}}
484 EXPECT_FORTIFY_DEATH(umask(01777));
485}
486
487FORTIFY_TEST(stdio) {
488 char small_buffer[8] = {};
489 {
George Burgess IV26d25a22019-06-06 17:45:05 -0700490 // expected-error@+1{{size is larger than the destination buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700491 EXPECT_FORTIFY_DEATH(snprintf(small_buffer, sizeof(small_buffer) + 1, ""));
492
493 va_list va;
George Burgess IV26d25a22019-06-06 17:45:05 -0700494 // expected-error@+2{{size is larger than the destination buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700495 // expected-warning@+1{{format string is empty}}
496 EXPECT_FORTIFY_DEATH(vsnprintf(small_buffer, sizeof(small_buffer) + 1, "", va));
George Burgess IV26d25a22019-06-06 17:45:05 -0700497
498 const char *SOMETIMES_CONST format_string = "aaaaaaaaa";
499
500 // expected-error@+1{{format string will always overflow}}
501 EXPECT_FORTIFY_DEATH(sprintf(small_buffer, format_string));
George Burgess IV9a274102019-06-04 15:39:52 -0700502 }
503
504 // expected-error@+1{{size should not be negative}}
505 EXPECT_FORTIFY_DEATH(fgets(small_buffer, -1, stdin));
506 // expected-error@+1{{size is larger than the destination buffer}}
507 EXPECT_FORTIFY_DEATH(fgets(small_buffer, sizeof(small_buffer) + 1, stdin));
508
509 // expected-error@+1{{size * count overflows}}
510 EXPECT_NO_DEATH(fread(small_buffer, 2, (size_t)-1, stdin));
511 // expected-error@+1{{size * count is too large for the given buffer}}
512 EXPECT_FORTIFY_DEATH(fread(small_buffer, 1, sizeof(small_buffer) + 1, stdin));
513
514 // expected-error@+1{{size * count overflows}}
515 EXPECT_NO_DEATH(fwrite(small_buffer, 2, (size_t)-1, stdout));
516 // expected-error@+1{{size * count is too large for the given buffer}}
517 EXPECT_FORTIFY_DEATH(fwrite(small_buffer, 1, sizeof(small_buffer) + 1, stdout));
518}
519
520FORTIFY_TEST(unistd) {
521 char small_buffer[8];
522
523 // Return value warnings are (sort of) a part of FORTIFY, so we don't ignore them.
524#if 0
525 // expected-error@+2{{ignoring return value of function}}
526#endif
527 // expected-error@+1{{bytes overflows the given object}}
528 EXPECT_FORTIFY_DEATH(read(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
529#if 0
530 // expected-error@+2{{ignoring return value of function}}
531#endif
532 // expected-error@+1{{bytes overflows the given object}}
533 EXPECT_FORTIFY_DEATH(pread(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
534#if 0
535 // expected-error@+2{{ignoring return value of function}}
536#endif
537 // expected-error@+1{{bytes overflows the given object}}
538 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
539#if 0
540 // expected-error@+2{{ignoring return value of function}}
541#endif
542 // expected-error@+1{{bytes overflows the given object}}
543 EXPECT_FORTIFY_DEATH(write(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
544#if 0
545 // expected-error@+2{{ignoring return value of function}}
546#endif
547 // expected-error@+1{{bytes overflows the given object}}
548 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
549#if 0
550 // expected-error@+2{{ignoring return value of function}}
551#endif
552 // expected-error@+1{{bytes overflows the given object}}
553 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
554#if 0
555 // expected-error@+2{{ignoring return value of function}}
556#endif
557 // expected-error@+1{{bytes overflows the given object}}
558 EXPECT_FORTIFY_DEATH(readlink("/", small_buffer, sizeof(small_buffer) + 1));
559#if 0
560 // expected-error@+2{{ignoring return value of function}}
561#endif
562 // expected-error@+1{{bytes overflows the given object}}
563 EXPECT_FORTIFY_DEATH(getcwd(small_buffer, sizeof(small_buffer) + 1));
564
565 // getcwd allocates and returns a buffer if you pass null to getcwd
566 EXPECT_NO_DEATH(getcwd(nullptr, 0));
567 EXPECT_NO_DEATH(getcwd(nullptr, 4096));
568
569 struct {
570 char tiny_buffer[4];
571 char tiny_buffer2[4];
572 } split;
573
574 EXPECT_NO_DEATH(read(kBogusFD, split.tiny_buffer, sizeof(split)));
575 EXPECT_NO_DEATH(pread(kBogusFD, split.tiny_buffer, sizeof(split), 0));
576 EXPECT_NO_DEATH(pread64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
577 EXPECT_NO_DEATH(write(kBogusFD, split.tiny_buffer, sizeof(split)));
578 EXPECT_NO_DEATH(pwrite(kBogusFD, split.tiny_buffer, sizeof(split), 0));
579 EXPECT_NO_DEATH(pwrite64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
580
581#if _FORTIFY_SOURCE > 1
582 // expected-error@+2{{bytes overflows the given object}}
583#endif
584 EXPECT_FORTIFY_DEATH_STRUCT(readlink("/", split.tiny_buffer, sizeof(split)));
585#if _FORTIFY_SOURCE > 1
586 // expected-error@+2{{bytes overflows the given object}}
587#endif
588 EXPECT_FORTIFY_DEATH_STRUCT(getcwd(split.tiny_buffer, sizeof(split)));
589
590 {
George Burgess IV9a274102019-06-04 15:39:52 -0700591 char* volatile unknown = small_buffer;
592 const size_t count = static_cast<size_t>(SSIZE_MAX) + 1;
593 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
594 EXPECT_FORTIFY_DEATH(read(kBogusFD, unknown, count));
595 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
596 EXPECT_FORTIFY_DEATH(pread(kBogusFD, unknown, count, 0));
597 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
598 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, unknown, count, 0));
599 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
600 EXPECT_FORTIFY_DEATH(write(kBogusFD, unknown, count));
601 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
602 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, unknown, count, 0));
603 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
604 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, unknown, count, 0));
George Burgess IV9a274102019-06-04 15:39:52 -0700605 }
606}
607
608#endif // defined(__BIONIC__)