blob: f08fd1ff8bad4802234a8d23ee10d39965038e72 [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
Elliott Hughescfd8f582020-07-23 13:40:39 -070017//
George Burgess IV9a274102019-06-04 15:39:52 -070018// Clang compile-time and run-time tests for Bionic's FORTIFY.
19//
Elliott Hughescfd8f582020-07-23 13:40:39 -070020
21// This file is compiled in two configurations to give us reasonable coverage of clang's
22// FORTIFY implementation:
George Burgess IV9a274102019-06-04 15:39:52 -070023//
Elliott Hughescfd8f582020-07-23 13:40:39 -070024// 1. For compile-time checks, we use clang's diagnostic consumer
George Burgess IV9a274102019-06-04 15:39:52 -070025// (https://clang.llvm.org/doxygen/classclang_1_1VerifyDiagnosticConsumer.html#details)
26// to check diagnostics (e.g. the expected-* comments everywhere).
27//
Elliott Hughescfd8f582020-07-23 13:40:39 -070028// 2. For run-time checks, we build and run as regular gtests.
George Burgess IV9a274102019-06-04 15:39:52 -070029
Elliott Hughescfd8f582020-07-23 13:40:39 -070030// Note that these tests do things like leaking memory. That's WAI.
31
32//
33// Configuration for the compile-time checks. (These comments have side effects!)
34//
George Burgess IV9a274102019-06-04 15:39:52 -070035// Silence all "from 'diagnose_if'" `note`s from anywhere, including headers; they're uninteresting
36// for this test case, and their line numbers may change over time.
37// expected-note@* 0+{{from 'diagnose_if'}}
38//
39// Similarly, there are a few overload tricks we have to emit errors. Ignore any notes from those.
40// expected-note@* 0+{{candidate function}}
George Burgess IV36926f42019-09-15 16:57:00 -070041//
42// And finally, all explicitly-unavailable-here complaints from headers are
43// uninteresting
Yi Kongbf67ea52019-08-03 18:26:05 -070044// expected-note@* 0+{{has been explicitly marked unavailable here}}
Elliott Hughescfd8f582020-07-23 13:40:39 -070045//
46// Note that some of these diagnostics come from clang itself, while others come from
George Burgess IV36926f42019-09-15 16:57:00 -070047// `diagnose_if`s sprinkled throughout Bionic.
48
George Burgess IV9a274102019-06-04 15:39:52 -070049#ifndef _FORTIFY_SOURCE
50#error "_FORTIFY_SOURCE must be defined"
51#endif
52
53#include <sys/cdefs.h>
54
55// This is a test specifically of bionic's FORTIFY machinery. Other stdlibs need not apply.
56#ifndef __BIONIC__
57// expected-no-diagnostics
58#else
59
60// As alluded to above, we're going to be doing some obviously very broken things in this file.
61// FORTIFY helpfully flags a lot of it at compile-time, but we want it to *actually* crash, too. So
62// let's wipe out any build-time errors.
63#ifndef COMPILATION_TESTS
64#undef __clang_error_if
65#define __clang_error_if(...)
66#undef __clang_warning_if
67#define __clang_warning_if(...)
George Burgess IV36926f42019-09-15 16:57:00 -070068#pragma clang diagnostic ignored "-Wfortify-source"
George Burgess IV26d25a22019-06-06 17:45:05 -070069
70// SOMETIMES_CONST allows clang to emit eager diagnostics when we're doing compilation tests, but
71// blocks them otherwise. This is needed for diagnostics emitted with __enable_if.
72#define SOMETIMES_CONST volatile
73#else
74#define SOMETIMES_CONST const
George Burgess IV9a274102019-06-04 15:39:52 -070075#endif
76
77#include <err.h>
78#include <fcntl.h>
79#include <limits.h>
80#include <poll.h>
81#include <signal.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <string.h>
85#include <sys/socket.h>
86#include <sys/stat.h>
87#include <sys/wait.h>
88#include <syslog.h>
89#include <unistd.h>
90#include <wchar.h>
91
92#ifndef COMPILATION_TESTS
Elliott Hughes141b9172021-04-09 17:13:09 -070093#include <android-base/silent_death_test.h>
George Burgess IV9a274102019-06-04 15:39:52 -070094#include <gtest/gtest.h>
George Burgess IV9a274102019-06-04 15:39:52 -070095
96#define CONCAT2(x, y) x##y
97#define CONCAT(x, y) CONCAT2(x, y)
Elliott Hughes61d07062021-02-18 17:17:27 -080098#define FORTIFY_TEST_NAME CONCAT(CONCAT(clang_fortify_test_, _FORTIFY_SOURCE), _DeathTest)
George Burgess IV9a274102019-06-04 15:39:52 -070099
Elliott Hughes141b9172021-04-09 17:13:09 -0700100using FORTIFY_TEST_NAME = SilentDeathTest;
George Burgess IV9a274102019-06-04 15:39:52 -0700101
102template <typename Fn>
103__attribute__((noreturn)) static void ExitAfter(Fn&& f) {
104 f();
105 // No need to tear things down; our parent process should handle that.
106 _exit(0);
107}
108
109// In any case (including failing tests), we always want to die after this.
110#define DIE_WITH(expr, cond, regex) EXPECT_EXIT(ExitAfter([&] { (expr); }), cond, regex)
111
112// EXPECT_NO_DEATH forks so that the test remains alive on a bug, and so that the environment
113// doesn't get modified on no bug. (Environment modification is especially tricky to deal with given
114// the *_STRUCT variants below.)
115#define EXPECT_NO_DEATH(expr) DIE_WITH(expr, testing::ExitedWithCode(0), "")
116#define EXPECT_FORTIFY_DEATH(expr) DIE_WITH(expr, testing::KilledBySignal(SIGABRT), "FORTIFY")
117// Expecting death, but only if we're doing a "strict" struct-checking mode.
118#if _FORTIFY_SOURCE > 1
119#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
120#else
121#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_NO_DEATH
122#endif
123
Elliott Hughes61d07062021-02-18 17:17:27 -0800124#define FORTIFY_TEST(test_name) TEST_F(FORTIFY_TEST_NAME, test_name)
George Burgess IV9a274102019-06-04 15:39:52 -0700125
126#else // defined(COMPILATION_TESTS)
127
128#define EXPECT_NO_DEATH(expr) expr
129#define EXPECT_FORTIFY_DEATH(expr) expr
130#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
131#define FORTIFY_TEST(test_name) void test_name()
132#endif
133
134const static int kBogusFD = -1;
135
136FORTIFY_TEST(string) {
137 char small_buffer[8] = {};
138
139 {
140 char large_buffer[sizeof(small_buffer) + 1] = {};
George Burgess IV36926f42019-09-15 16:57:00 -0700141 // expected-error@+1{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700142 EXPECT_FORTIFY_DEATH(memcpy(small_buffer, large_buffer, sizeof(large_buffer)));
George Burgess IV36926f42019-09-15 16:57:00 -0700143 // expected-error@+1{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700144 EXPECT_FORTIFY_DEATH(memmove(small_buffer, large_buffer, sizeof(large_buffer)));
Yabin Cuiae1745d2020-04-16 15:07:28 -0700145 // FIXME(gbiv): look into removing mempcpy's diagnose_if bits once the b/149839606 roll sticks.
146 // expected-error@+2{{will always overflow}}
George Burgess IV849c0b92019-06-10 16:22:09 -0700147 // expected-error@+1{{size bigger than buffer}}
148 EXPECT_FORTIFY_DEATH(mempcpy(small_buffer, large_buffer, sizeof(large_buffer)));
George Burgess IV36926f42019-09-15 16:57:00 -0700149 // expected-error@+1{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700150 EXPECT_FORTIFY_DEATH(memset(small_buffer, 0, sizeof(large_buffer)));
151 // expected-warning@+1{{arguments got flipped?}}
152 EXPECT_NO_DEATH(memset(small_buffer, sizeof(small_buffer), 0));
George Burgess IV261b7f42019-06-10 16:32:07 -0700153 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700154 EXPECT_FORTIFY_DEATH(bcopy(large_buffer, small_buffer, sizeof(large_buffer)));
George Burgess IV261b7f42019-06-10 16:32:07 -0700155 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700156 EXPECT_FORTIFY_DEATH(bzero(small_buffer, sizeof(large_buffer)));
157 }
158
159 {
160 const char large_string[] = "Hello!!!";
161 static_assert(sizeof(large_string) > sizeof(small_buffer), "");
162
Elliott Hughesa8cd7312024-05-01 22:43:31 +0000163 // expected-error@+2{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700164 // expected-error@+1{{string bigger than buffer}}
165 EXPECT_FORTIFY_DEATH(strcpy(small_buffer, large_string));
166 // expected-error@+1{{string bigger than buffer}}
167 EXPECT_FORTIFY_DEATH(stpcpy(small_buffer, large_string));
George Burgess IV36926f42019-09-15 16:57:00 -0700168 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700169 EXPECT_FORTIFY_DEATH(strncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV36926f42019-09-15 16:57:00 -0700170 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700171 EXPECT_FORTIFY_DEATH(stpncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700172 // expected-error@+1{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700173 EXPECT_FORTIFY_DEATH(strcat(small_buffer, large_string));
George Burgess IV36926f42019-09-15 16:57:00 -0700174 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700175 EXPECT_FORTIFY_DEATH(strncat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700176 // expected-error@+1{{size bigger than buffer}}
177 EXPECT_FORTIFY_DEATH(strlcpy(small_buffer, large_string, sizeof(large_string)));
178 // expected-error@+1{{size bigger than buffer}}
179 EXPECT_FORTIFY_DEATH(strlcat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700180 }
181
182 {
183 struct {
184 char tiny_buffer[4];
185 char tiny_buffer2[4];
186 } split = {};
187
188 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
189 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
190 EXPECT_NO_DEATH(memmove(split.tiny_buffer, &split, sizeof(split)));
191 EXPECT_NO_DEATH(mempcpy(split.tiny_buffer, &split, sizeof(split)));
192 EXPECT_NO_DEATH(memset(split.tiny_buffer, 0, sizeof(split)));
193
194 EXPECT_NO_DEATH(bcopy(&split, split.tiny_buffer, sizeof(split)));
195 EXPECT_NO_DEATH(bzero(split.tiny_buffer, sizeof(split)));
196
197 const char small_string[] = "Hi!!";
198 static_assert(sizeof(small_string) > sizeof(split.tiny_buffer), "");
199
200#if _FORTIFY_SOURCE > 1
Elliott Hughesa8cd7312024-05-01 22:43:31 +0000201 // expected-error@+3{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700202 // expected-error@+2{{string bigger than buffer}}
203#endif
204 EXPECT_FORTIFY_DEATH_STRUCT(strcpy(split.tiny_buffer, small_string));
205
206#if _FORTIFY_SOURCE > 1
207 // expected-error@+2{{string bigger than buffer}}
208#endif
209 EXPECT_FORTIFY_DEATH_STRUCT(stpcpy(split.tiny_buffer, small_string));
210
211#if _FORTIFY_SOURCE > 1
George Burgess IV36926f42019-09-15 16:57:00 -0700212 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700213#endif
214 EXPECT_FORTIFY_DEATH_STRUCT(strncpy(split.tiny_buffer, small_string, sizeof(small_string)));
215
216#if _FORTIFY_SOURCE > 1
George Burgess IV36926f42019-09-15 16:57:00 -0700217 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700218#endif
219 EXPECT_FORTIFY_DEATH_STRUCT(stpncpy(split.tiny_buffer, small_string, sizeof(small_string)));
220
221#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700222 // expected-error@+2{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700223#endif
224 EXPECT_FORTIFY_DEATH_STRUCT(strcat(split.tiny_buffer, small_string));
225
226#if _FORTIFY_SOURCE > 1
George Burgess IV36926f42019-09-15 16:57:00 -0700227 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700228#endif
229 EXPECT_FORTIFY_DEATH_STRUCT(strncat(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700230
231#if _FORTIFY_SOURCE > 1
232 // expected-error@+2{{size bigger than buffer}}
233#endif
234 EXPECT_FORTIFY_DEATH_STRUCT(strlcat(split.tiny_buffer, small_string, sizeof(small_string)));
235
236#if _FORTIFY_SOURCE > 1
237 // expected-error@+2{{size bigger than buffer}}
238#endif
239 EXPECT_FORTIFY_DEATH_STRUCT(strlcpy(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700240 }
241}
242
George Burgess IV2356c932019-06-06 17:18:13 -0700243FORTIFY_TEST(fcntl) {
244 const char target[] = "/dev/null";
245 int dirfd = 0;
246
247 // These all emit hard errors without diagnose_if, so running them is a bit
248 // more involved.
249#ifdef COMPILATION_TESTS
250 // expected-error@+1{{too many arguments}}
251 open("/", 0, 0, 0);
252 // expected-error@+1{{too many arguments}}
253 open64("/", 0, 0, 0);
254 // expected-error@+1{{too many arguments}}
255 openat(0, "/", 0, 0, 0);
256 // expected-error@+1{{too many arguments}}
257 openat64(0, "/", 0, 0, 0);
258#endif
259
260 // expected-error@+1{{missing mode}}
261 EXPECT_FORTIFY_DEATH(open(target, O_CREAT));
262 // expected-error@+1{{missing mode}}
263 EXPECT_FORTIFY_DEATH(open(target, O_TMPFILE));
264 // expected-error@+1{{missing mode}}
265 EXPECT_FORTIFY_DEATH(open64(target, O_CREAT));
266 // expected-error@+1{{missing mode}}
267 EXPECT_FORTIFY_DEATH(open64(target, O_TMPFILE));
268 // expected-error@+1{{missing mode}}
269 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_CREAT));
270 // expected-error@+1{{missing mode}}
271 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_TMPFILE));
272 // expected-error@+1{{missing mode}}
273 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_CREAT));
274 // expected-error@+1{{missing mode}}
275 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_TMPFILE));
276
277 // expected-warning@+1{{superfluous mode bits}}
278 EXPECT_NO_DEATH(open(target, O_RDONLY, 0777));
279 // expected-warning@+1{{superfluous mode bits}}
280 EXPECT_NO_DEATH(open64(target, O_RDONLY, 0777));
281 // expected-warning@+1{{superfluous mode bits}}
282 EXPECT_NO_DEATH(openat(dirfd, target, O_RDONLY, 0777));
283 // expected-warning@+1{{superfluous mode bits}}
284 EXPECT_NO_DEATH(openat64(dirfd, target, O_RDONLY, 0777));
285}
286
George Burgess IV9a274102019-06-04 15:39:52 -0700287// Since these emit hard errors, it's sort of hard to run them...
288#ifdef COMPILATION_TESTS
289namespace compilation_tests {
290template <typename T>
291static T declval() {
292 __builtin_unreachable();
293}
294
George Burgess IV9a274102019-06-04 15:39:52 -0700295static void testFormatStrings() {
296 const auto unsigned_value = declval<unsigned long long>();
297 const auto* unknown_string = declval<const char*>();
George Burgess IV06bb4ce2019-06-13 15:13:02 -0700298 const auto va = *declval<va_list*>();
George Burgess IV9a274102019-06-04 15:39:52 -0700299
300 {
301 auto some_fd = declval<int>();
302 // expected-warning@+1{{format specifies type 'int'}}
303 dprintf(some_fd, "%d", unsigned_value);
304 // expected-warning@+1{{format string is not a string literal}}
305 dprintf(some_fd, unknown_string, unsigned_value);
306 // expected-warning@+1{{format string is not a string literal}}
307 vdprintf(1, unknown_string, va);
308 }
309
310 {
311 auto* retval = declval<char*>();
312#if 0
313 // expected-error@+2{{ignoring return value}}
314#endif
315 // expected-warning@+1{{format specifies type 'int'}}
316 asprintf(&retval, "%d", unsigned_value);
317#if 0
318 // expected-error@+2{{ignoring return value}}
319#endif
320 // expected-warning@+1{{format string is not a string literal}}
321 asprintf(&retval, unknown_string, unsigned_value);
322#if 0
323 // expected-error@+2{{ignoring return value}}
324#endif
325 // expected-warning@+1{{format string is not a string literal}}
326 vasprintf(&retval, unknown_string, va);
327 }
328
329 // expected-warning@+1{{format specifies type 'int'}}
330 syslog(0, "%d", unsigned_value);
331 // expected-warning@+1{{format string is not a string literal}}
332 syslog(0, unknown_string, unsigned_value);
333 // expected-warning@+1{{format string is not a string literal}}
334 vsyslog(0, unknown_string, va);
335
336 {
337 auto* file = declval<FILE*>();
338 // expected-warning@+1{{format specifies type 'int'}}
339 fprintf(file, "%d", unsigned_value);
340 // expected-warning@+1{{format string is not a string literal}}
341 fprintf(file, unknown_string, unsigned_value);
342 // expected-warning@+1{{format string is not a string literal}}
343 vfprintf(file, unknown_string, va);
344 }
345
346 // expected-warning@+1{{format specifies type 'int'}}
347 printf("%d", unsigned_value);
348 // expected-warning@+1{{format string is not a string literal}}
349 printf(unknown_string, unsigned_value);
350 // expected-warning@+1{{format string is not a string literal}}
351 vprintf(unknown_string, va);
352
353 {
354 char buf[128];
355 // expected-warning@+1{{format specifies type 'int'}}
356 sprintf(buf, "%d", unsigned_value);
357 // expected-warning@+1{{format string is not a string literal}}
358 sprintf(buf, unknown_string, unsigned_value);
359 // expected-warning@+1{{format string is not a string literal}}
360 sprintf(buf, unknown_string, va);
361
362 // expected-warning@+1{{format specifies type 'int'}}
363 snprintf(buf, sizeof(buf), "%d", unsigned_value);
364 // expected-warning@+1{{format string is not a string literal}}
365 snprintf(buf, sizeof(buf), unknown_string, unsigned_value);
366 // expected-warning@+1{{format string is not a string literal}}
367 vsnprintf(buf, sizeof(buf), unknown_string, va);
368 }
369
370 // FIXME: below are general format string cases where clang should probably try to warn.
371 {
372 char buf[4];
373 sprintf(buf, "%s", "1234");
374 sprintf(buf, "1%s4", "23");
375 sprintf(buf, "%d", 1234);
376
377 // Similar thoughts for strncpy, etc.
378 }
379}
380
381static void testStdlib() {
zijunzhao5a918d92022-11-28 21:05:55 +0000382#pragma clang diagnostic push
383#pragma clang diagnostic ignored "-Wnonnull"
George Burgess IV9a274102019-06-04 15:39:52 -0700384 char path_buffer[PATH_MAX - 1];
George Burgess IV8c0ec112019-06-06 17:23:32 -0700385 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700386 // expected-error@+1{{must be NULL or a pointer to a buffer with >= PATH_MAX bytes}}
387 realpath("/", path_buffer);
George Burgess IV8c0ec112019-06-06 17:23:32 -0700388 // expected-warning@+1{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700389 realpath("/", nullptr);
390
George Burgess IV8c0ec112019-06-06 17:23:32 -0700391 // expected-warning@+2{{ignoring return value of function}}
392 // expected-error@+1{{flipped arguments?}}
George Burgess IV9a274102019-06-04 15:39:52 -0700393 realpath(nullptr, path_buffer);
394
George Burgess IV8c0ec112019-06-06 17:23:32 -0700395 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700396 // expected-error@+1{{flipped arguments?}}
397 realpath(nullptr, nullptr);
zijunzhao5a918d92022-11-28 21:05:55 +0000398#pragma clang diagnostic pop
George Burgess IV9a274102019-06-04 15:39:52 -0700399}
400} // namespace compilation_tests
401#endif
402
403FORTIFY_TEST(poll) {
404 int pipe_fds[2];
405 if (pipe(pipe_fds)) err(1, "pipe failed");
406
407 // after this, pipe_fds[0] should always report RDHUP
408 if (close(pipe_fds[1])) err(1, "close failed");
409
410 struct pollfd poll_fd = { pipe_fds[0], POLLRDHUP, 0 };
411 {
412 struct pollfd few_fds[] = { poll_fd, poll_fd };
413 // expected-error@+1{{fd_count is larger than the given buffer}}
414 EXPECT_FORTIFY_DEATH(poll(few_fds, 3, 0));
415 // expected-error@+1{{fd_count is larger than the given buffer}}
416 EXPECT_FORTIFY_DEATH(ppoll(few_fds, 3, 0, 0));
417 // expected-error@+1{{fd_count is larger than the given buffer}}
418 EXPECT_FORTIFY_DEATH(ppoll64(few_fds, 3, 0, nullptr));
419 }
420
421 {
422 struct {
423 struct pollfd few[2];
424 struct pollfd extra[1];
425 } fds = { { poll_fd, poll_fd }, { poll_fd } };
426 static_assert(sizeof(fds) >= sizeof(struct pollfd) * 3, "");
427
428#if _FORTIFY_SOURCE > 1
429 // expected-error@+2{{fd_count is larger than the given buffer}}
430#endif
431 EXPECT_FORTIFY_DEATH_STRUCT(poll(fds.few, 3, 0));
432
433 struct timespec timeout = {};
434#if _FORTIFY_SOURCE > 1
435 // expected-error@+2{{fd_count is larger than the given buffer}}
436#endif
437 EXPECT_FORTIFY_DEATH_STRUCT(ppoll(fds.few, 3, &timeout, 0));
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(ppoll64(fds.few, 3, 0, nullptr));
443 }
444}
445
446FORTIFY_TEST(socket) {
447 {
448 char small_buffer[8];
449 // expected-error@+1{{size bigger than buffer}}
450 EXPECT_FORTIFY_DEATH(recv(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
451 // expected-error@+1{{size bigger than buffer}}
452 EXPECT_FORTIFY_DEATH(recvfrom(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
453
454 // expected-error@+1{{size bigger than buffer}}
455 EXPECT_FORTIFY_DEATH(send(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
456 // expected-error@+1{{size bigger than buffer}}
457 EXPECT_FORTIFY_DEATH(sendto(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
458 }
459
460 {
461 struct {
462 char tiny_buffer[4];
463 char tiny_buffer2;
464 } split = {};
465
466 EXPECT_NO_DEATH(recv(kBogusFD, split.tiny_buffer, sizeof(split), 0));
467 EXPECT_NO_DEATH(recvfrom(kBogusFD, split.tiny_buffer, sizeof(split), 0, 0, 0));
468 }
469}
470
471FORTIFY_TEST(sys_stat) {
472 // expected-error@+1{{'umask' called with invalid mode}}
473 EXPECT_FORTIFY_DEATH(umask(01777));
474}
475
476FORTIFY_TEST(stdio) {
477 char small_buffer[8] = {};
478 {
George Burgess IV36926f42019-09-15 16:57:00 -0700479 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700480 EXPECT_FORTIFY_DEATH(snprintf(small_buffer, sizeof(small_buffer) + 1, ""));
481
482 va_list va;
George Burgess IV36926f42019-09-15 16:57:00 -0700483 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700484 // expected-warning@+1{{format string is empty}}
485 EXPECT_FORTIFY_DEATH(vsnprintf(small_buffer, sizeof(small_buffer) + 1, "", va));
George Burgess IV26d25a22019-06-06 17:45:05 -0700486
487 const char *SOMETIMES_CONST format_string = "aaaaaaaaa";
488
489 // expected-error@+1{{format string will always overflow}}
490 EXPECT_FORTIFY_DEATH(sprintf(small_buffer, format_string));
George Burgess IV9a274102019-06-04 15:39:52 -0700491 }
492
493 // expected-error@+1{{size should not be negative}}
494 EXPECT_FORTIFY_DEATH(fgets(small_buffer, -1, stdin));
495 // expected-error@+1{{size is larger than the destination buffer}}
496 EXPECT_FORTIFY_DEATH(fgets(small_buffer, sizeof(small_buffer) + 1, stdin));
497
498 // expected-error@+1{{size * count overflows}}
499 EXPECT_NO_DEATH(fread(small_buffer, 2, (size_t)-1, stdin));
500 // expected-error@+1{{size * count is too large for the given buffer}}
501 EXPECT_FORTIFY_DEATH(fread(small_buffer, 1, sizeof(small_buffer) + 1, stdin));
502
503 // expected-error@+1{{size * count overflows}}
504 EXPECT_NO_DEATH(fwrite(small_buffer, 2, (size_t)-1, stdout));
505 // expected-error@+1{{size * count is too large for the given buffer}}
506 EXPECT_FORTIFY_DEATH(fwrite(small_buffer, 1, sizeof(small_buffer) + 1, stdout));
507}
508
509FORTIFY_TEST(unistd) {
510 char small_buffer[8];
511
512 // Return value warnings are (sort of) a part of FORTIFY, so we don't ignore them.
513#if 0
514 // expected-error@+2{{ignoring return value of function}}
515#endif
516 // expected-error@+1{{bytes overflows the given object}}
517 EXPECT_FORTIFY_DEATH(read(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
518#if 0
519 // expected-error@+2{{ignoring return value of function}}
520#endif
521 // expected-error@+1{{bytes overflows the given object}}
522 EXPECT_FORTIFY_DEATH(pread(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
523#if 0
524 // expected-error@+2{{ignoring return value of function}}
525#endif
526 // expected-error@+1{{bytes overflows the given object}}
527 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
528#if 0
529 // expected-error@+2{{ignoring return value of function}}
530#endif
531 // expected-error@+1{{bytes overflows the given object}}
532 EXPECT_FORTIFY_DEATH(write(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
533#if 0
534 // expected-error@+2{{ignoring return value of function}}
535#endif
536 // expected-error@+1{{bytes overflows the given object}}
537 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
538#if 0
539 // expected-error@+2{{ignoring return value of function}}
540#endif
541 // expected-error@+1{{bytes overflows the given object}}
542 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
543#if 0
544 // expected-error@+2{{ignoring return value of function}}
545#endif
546 // expected-error@+1{{bytes overflows the given object}}
547 EXPECT_FORTIFY_DEATH(readlink("/", small_buffer, sizeof(small_buffer) + 1));
548#if 0
549 // expected-error@+2{{ignoring return value of function}}
550#endif
551 // expected-error@+1{{bytes overflows the given object}}
552 EXPECT_FORTIFY_DEATH(getcwd(small_buffer, sizeof(small_buffer) + 1));
553
554 // getcwd allocates and returns a buffer if you pass null to getcwd
555 EXPECT_NO_DEATH(getcwd(nullptr, 0));
556 EXPECT_NO_DEATH(getcwd(nullptr, 4096));
557
558 struct {
559 char tiny_buffer[4];
560 char tiny_buffer2[4];
561 } split;
562
563 EXPECT_NO_DEATH(read(kBogusFD, split.tiny_buffer, sizeof(split)));
564 EXPECT_NO_DEATH(pread(kBogusFD, split.tiny_buffer, sizeof(split), 0));
565 EXPECT_NO_DEATH(pread64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
566 EXPECT_NO_DEATH(write(kBogusFD, split.tiny_buffer, sizeof(split)));
567 EXPECT_NO_DEATH(pwrite(kBogusFD, split.tiny_buffer, sizeof(split), 0));
568 EXPECT_NO_DEATH(pwrite64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
569
570#if _FORTIFY_SOURCE > 1
571 // expected-error@+2{{bytes overflows the given object}}
572#endif
573 EXPECT_FORTIFY_DEATH_STRUCT(readlink("/", split.tiny_buffer, sizeof(split)));
574#if _FORTIFY_SOURCE > 1
575 // expected-error@+2{{bytes overflows the given object}}
576#endif
577 EXPECT_FORTIFY_DEATH_STRUCT(getcwd(split.tiny_buffer, sizeof(split)));
578
579 {
George Burgess IV9a274102019-06-04 15:39:52 -0700580 char* volatile unknown = small_buffer;
581 const size_t count = static_cast<size_t>(SSIZE_MAX) + 1;
582 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
583 EXPECT_FORTIFY_DEATH(read(kBogusFD, unknown, count));
584 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
585 EXPECT_FORTIFY_DEATH(pread(kBogusFD, unknown, count, 0));
586 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
587 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, unknown, count, 0));
588 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
589 EXPECT_FORTIFY_DEATH(write(kBogusFD, unknown, count));
590 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
591 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, unknown, count, 0));
592 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
593 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, unknown, count, 0));
George Burgess IV9a274102019-06-04 15:39:52 -0700594 }
595}
596
597#endif // defined(__BIONIC__)