blob: 105c261ef6919e6a58016a6b763ce64860f4eafc [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
George Burgess IV86da38c2024-08-05 13:02:51 -060092#include <array>
93
George Burgess IV9a274102019-06-04 15:39:52 -070094#ifndef COMPILATION_TESTS
Elliott Hughes141b9172021-04-09 17:13:09 -070095#include <android-base/silent_death_test.h>
George Burgess IV9a274102019-06-04 15:39:52 -070096#include <gtest/gtest.h>
George Burgess IV9a274102019-06-04 15:39:52 -070097
98#define CONCAT2(x, y) x##y
99#define CONCAT(x, y) CONCAT2(x, y)
Elliott Hughes61d07062021-02-18 17:17:27 -0800100#define FORTIFY_TEST_NAME CONCAT(CONCAT(clang_fortify_test_, _FORTIFY_SOURCE), _DeathTest)
George Burgess IV9a274102019-06-04 15:39:52 -0700101
Elliott Hughes141b9172021-04-09 17:13:09 -0700102using FORTIFY_TEST_NAME = SilentDeathTest;
George Burgess IV9a274102019-06-04 15:39:52 -0700103
104template <typename Fn>
105__attribute__((noreturn)) static void ExitAfter(Fn&& f) {
106 f();
107 // No need to tear things down; our parent process should handle that.
108 _exit(0);
109}
110
111// In any case (including failing tests), we always want to die after this.
112#define DIE_WITH(expr, cond, regex) EXPECT_EXIT(ExitAfter([&] { (expr); }), cond, regex)
113
114// EXPECT_NO_DEATH forks so that the test remains alive on a bug, and so that the environment
115// doesn't get modified on no bug. (Environment modification is especially tricky to deal with given
116// the *_STRUCT variants below.)
117#define EXPECT_NO_DEATH(expr) DIE_WITH(expr, testing::ExitedWithCode(0), "")
118#define EXPECT_FORTIFY_DEATH(expr) DIE_WITH(expr, testing::KilledBySignal(SIGABRT), "FORTIFY")
119// Expecting death, but only if we're doing a "strict" struct-checking mode.
120#if _FORTIFY_SOURCE > 1
121#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
122#else
123#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_NO_DEATH
124#endif
125
Elliott Hughes61d07062021-02-18 17:17:27 -0800126#define FORTIFY_TEST(test_name) TEST_F(FORTIFY_TEST_NAME, test_name)
George Burgess IV9a274102019-06-04 15:39:52 -0700127
128#else // defined(COMPILATION_TESTS)
129
130#define EXPECT_NO_DEATH(expr) expr
131#define EXPECT_FORTIFY_DEATH(expr) expr
132#define EXPECT_FORTIFY_DEATH_STRUCT EXPECT_FORTIFY_DEATH
133#define FORTIFY_TEST(test_name) void test_name()
134#endif
135
136const static int kBogusFD = -1;
137
George Burgess IV86da38c2024-08-05 13:02:51 -0600138FORTIFY_TEST(strlen) {
139 auto run_strlen_with_contents = [&](std::array<char, 3> contents) {
140 // A lot of cruft is necessary to make this test DTRT. LLVM and Clang love to fold/optimize
141 // strlen calls, and that's the opposite of what we want to happen.
142
143 // Loop to convince LLVM that `contents` can never be known (since `xor volatile_value` can flip
144 // any bit in each elem of `contents`).
145 volatile char always_zero = 0;
146 for (char& c : contents) {
147 c ^= always_zero;
148 }
Liana Kazanovaf5d5f832024-09-30 19:20:35 +0000149 // Store in a volatile, so the strlen itself cannot be optimized out.
150 volatile size_t _strlen_result = strlen(&contents.front());
George Burgess IV86da38c2024-08-05 13:02:51 -0600151 };
152
153 EXPECT_NO_DEATH(run_strlen_with_contents({'f', 'o', '\0'}));
154 EXPECT_FORTIFY_DEATH(run_strlen_with_contents({'f', 'o', 'o'}));
155}
156
George Burgess IV9a274102019-06-04 15:39:52 -0700157FORTIFY_TEST(string) {
158 char small_buffer[8] = {};
159
160 {
161 char large_buffer[sizeof(small_buffer) + 1] = {};
George Burgess IV36926f42019-09-15 16:57:00 -0700162 // expected-error@+1{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700163 EXPECT_FORTIFY_DEATH(memcpy(small_buffer, large_buffer, sizeof(large_buffer)));
George Burgess IV36926f42019-09-15 16:57:00 -0700164 // expected-error@+1{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700165 EXPECT_FORTIFY_DEATH(memmove(small_buffer, large_buffer, sizeof(large_buffer)));
Yabin Cuiae1745d2020-04-16 15:07:28 -0700166 // FIXME(gbiv): look into removing mempcpy's diagnose_if bits once the b/149839606 roll sticks.
167 // expected-error@+2{{will always overflow}}
George Burgess IV849c0b92019-06-10 16:22:09 -0700168 // expected-error@+1{{size bigger than buffer}}
169 EXPECT_FORTIFY_DEATH(mempcpy(small_buffer, large_buffer, sizeof(large_buffer)));
George Burgess IV36926f42019-09-15 16:57:00 -0700170 // expected-error@+1{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700171 EXPECT_FORTIFY_DEATH(memset(small_buffer, 0, sizeof(large_buffer)));
172 // expected-warning@+1{{arguments got flipped?}}
173 EXPECT_NO_DEATH(memset(small_buffer, sizeof(small_buffer), 0));
George Burgess IV261b7f42019-06-10 16:32:07 -0700174 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700175 EXPECT_FORTIFY_DEATH(bcopy(large_buffer, small_buffer, sizeof(large_buffer)));
George Burgess IV261b7f42019-06-10 16:32:07 -0700176 // expected-error@+1{{size bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700177 EXPECT_FORTIFY_DEATH(bzero(small_buffer, sizeof(large_buffer)));
178 }
179
180 {
181 const char large_string[] = "Hello!!!";
182 static_assert(sizeof(large_string) > sizeof(small_buffer), "");
183
Elliott Hughesa8cd7312024-05-01 22:43:31 +0000184 // expected-error@+2{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700185 // expected-error@+1{{string bigger than buffer}}
186 EXPECT_FORTIFY_DEATH(strcpy(small_buffer, large_string));
187 // expected-error@+1{{string bigger than buffer}}
188 EXPECT_FORTIFY_DEATH(stpcpy(small_buffer, large_string));
George Burgess IV36926f42019-09-15 16:57:00 -0700189 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700190 EXPECT_FORTIFY_DEATH(strncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV36926f42019-09-15 16:57:00 -0700191 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700192 EXPECT_FORTIFY_DEATH(stpncpy(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700193 // expected-error@+1{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700194 EXPECT_FORTIFY_DEATH(strcat(small_buffer, large_string));
George Burgess IV36926f42019-09-15 16:57:00 -0700195 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700196 EXPECT_FORTIFY_DEATH(strncat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700197 // expected-error@+1{{size bigger than buffer}}
198 EXPECT_FORTIFY_DEATH(strlcpy(small_buffer, large_string, sizeof(large_string)));
199 // expected-error@+1{{size bigger than buffer}}
200 EXPECT_FORTIFY_DEATH(strlcat(small_buffer, large_string, sizeof(large_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700201 }
202
203 {
204 struct {
205 char tiny_buffer[4];
206 char tiny_buffer2[4];
207 } split = {};
208
209 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
210 EXPECT_NO_DEATH(memcpy(split.tiny_buffer, &split, sizeof(split)));
211 EXPECT_NO_DEATH(memmove(split.tiny_buffer, &split, sizeof(split)));
212 EXPECT_NO_DEATH(mempcpy(split.tiny_buffer, &split, sizeof(split)));
213 EXPECT_NO_DEATH(memset(split.tiny_buffer, 0, sizeof(split)));
214
215 EXPECT_NO_DEATH(bcopy(&split, split.tiny_buffer, sizeof(split)));
216 EXPECT_NO_DEATH(bzero(split.tiny_buffer, sizeof(split)));
217
218 const char small_string[] = "Hi!!";
219 static_assert(sizeof(small_string) > sizeof(split.tiny_buffer), "");
220
221#if _FORTIFY_SOURCE > 1
Elliott Hughesa8cd7312024-05-01 22:43:31 +0000222 // expected-error@+3{{will always overflow}}
George Burgess IV9a274102019-06-04 15:39:52 -0700223 // expected-error@+2{{string bigger than buffer}}
224#endif
225 EXPECT_FORTIFY_DEATH_STRUCT(strcpy(split.tiny_buffer, small_string));
226
227#if _FORTIFY_SOURCE > 1
228 // expected-error@+2{{string bigger than buffer}}
229#endif
230 EXPECT_FORTIFY_DEATH_STRUCT(stpcpy(split.tiny_buffer, small_string));
231
232#if _FORTIFY_SOURCE > 1
George Burgess IV36926f42019-09-15 16:57:00 -0700233 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700234#endif
235 EXPECT_FORTIFY_DEATH_STRUCT(strncpy(split.tiny_buffer, small_string, sizeof(small_string)));
236
237#if _FORTIFY_SOURCE > 1
George Burgess IV36926f42019-09-15 16:57:00 -0700238 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700239#endif
240 EXPECT_FORTIFY_DEATH_STRUCT(stpncpy(split.tiny_buffer, small_string, sizeof(small_string)));
241
242#if _FORTIFY_SOURCE > 1
George Burgess IV77f99aa2019-06-06 14:14:52 -0700243 // expected-error@+2{{string bigger than buffer}}
George Burgess IV9a274102019-06-04 15:39:52 -0700244#endif
245 EXPECT_FORTIFY_DEATH_STRUCT(strcat(split.tiny_buffer, small_string));
246
247#if _FORTIFY_SOURCE > 1
George Burgess IV36926f42019-09-15 16:57:00 -0700248 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700249#endif
250 EXPECT_FORTIFY_DEATH_STRUCT(strncat(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV77f99aa2019-06-06 14:14:52 -0700251
252#if _FORTIFY_SOURCE > 1
253 // expected-error@+2{{size bigger than buffer}}
254#endif
255 EXPECT_FORTIFY_DEATH_STRUCT(strlcat(split.tiny_buffer, small_string, sizeof(small_string)));
256
257#if _FORTIFY_SOURCE > 1
258 // expected-error@+2{{size bigger than buffer}}
259#endif
260 EXPECT_FORTIFY_DEATH_STRUCT(strlcpy(split.tiny_buffer, small_string, sizeof(small_string)));
George Burgess IV9a274102019-06-04 15:39:52 -0700261 }
262}
263
George Burgess IV2356c932019-06-06 17:18:13 -0700264FORTIFY_TEST(fcntl) {
265 const char target[] = "/dev/null";
266 int dirfd = 0;
267
268 // These all emit hard errors without diagnose_if, so running them is a bit
269 // more involved.
270#ifdef COMPILATION_TESTS
271 // expected-error@+1{{too many arguments}}
272 open("/", 0, 0, 0);
273 // expected-error@+1{{too many arguments}}
274 open64("/", 0, 0, 0);
275 // expected-error@+1{{too many arguments}}
276 openat(0, "/", 0, 0, 0);
277 // expected-error@+1{{too many arguments}}
278 openat64(0, "/", 0, 0, 0);
279#endif
280
281 // expected-error@+1{{missing mode}}
282 EXPECT_FORTIFY_DEATH(open(target, O_CREAT));
283 // expected-error@+1{{missing mode}}
284 EXPECT_FORTIFY_DEATH(open(target, O_TMPFILE));
285 // expected-error@+1{{missing mode}}
286 EXPECT_FORTIFY_DEATH(open64(target, O_CREAT));
287 // expected-error@+1{{missing mode}}
288 EXPECT_FORTIFY_DEATH(open64(target, O_TMPFILE));
289 // expected-error@+1{{missing mode}}
290 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_CREAT));
291 // expected-error@+1{{missing mode}}
292 EXPECT_FORTIFY_DEATH(openat(dirfd, target, O_TMPFILE));
293 // expected-error@+1{{missing mode}}
294 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_CREAT));
295 // expected-error@+1{{missing mode}}
296 EXPECT_FORTIFY_DEATH(openat64(dirfd, target, O_TMPFILE));
297
298 // expected-warning@+1{{superfluous mode bits}}
299 EXPECT_NO_DEATH(open(target, O_RDONLY, 0777));
300 // expected-warning@+1{{superfluous mode bits}}
301 EXPECT_NO_DEATH(open64(target, O_RDONLY, 0777));
302 // expected-warning@+1{{superfluous mode bits}}
303 EXPECT_NO_DEATH(openat(dirfd, target, O_RDONLY, 0777));
304 // expected-warning@+1{{superfluous mode bits}}
305 EXPECT_NO_DEATH(openat64(dirfd, target, O_RDONLY, 0777));
306}
307
George Burgess IV9a274102019-06-04 15:39:52 -0700308// Since these emit hard errors, it's sort of hard to run them...
309#ifdef COMPILATION_TESTS
310namespace compilation_tests {
311template <typename T>
312static T declval() {
313 __builtin_unreachable();
314}
315
George Burgess IV9a274102019-06-04 15:39:52 -0700316static void testFormatStrings() {
317 const auto unsigned_value = declval<unsigned long long>();
318 const auto* unknown_string = declval<const char*>();
George Burgess IV06bb4ce2019-06-13 15:13:02 -0700319 const auto va = *declval<va_list*>();
George Burgess IV9a274102019-06-04 15:39:52 -0700320
321 {
322 auto some_fd = declval<int>();
323 // expected-warning@+1{{format specifies type 'int'}}
324 dprintf(some_fd, "%d", unsigned_value);
325 // expected-warning@+1{{format string is not a string literal}}
326 dprintf(some_fd, unknown_string, unsigned_value);
327 // expected-warning@+1{{format string is not a string literal}}
328 vdprintf(1, unknown_string, va);
329 }
330
331 {
332 auto* retval = declval<char*>();
333#if 0
334 // expected-error@+2{{ignoring return value}}
335#endif
336 // expected-warning@+1{{format specifies type 'int'}}
337 asprintf(&retval, "%d", unsigned_value);
338#if 0
339 // expected-error@+2{{ignoring return value}}
340#endif
341 // expected-warning@+1{{format string is not a string literal}}
342 asprintf(&retval, unknown_string, unsigned_value);
343#if 0
344 // expected-error@+2{{ignoring return value}}
345#endif
346 // expected-warning@+1{{format string is not a string literal}}
347 vasprintf(&retval, unknown_string, va);
348 }
349
350 // expected-warning@+1{{format specifies type 'int'}}
351 syslog(0, "%d", unsigned_value);
352 // expected-warning@+1{{format string is not a string literal}}
353 syslog(0, unknown_string, unsigned_value);
354 // expected-warning@+1{{format string is not a string literal}}
355 vsyslog(0, unknown_string, va);
356
357 {
358 auto* file = declval<FILE*>();
359 // expected-warning@+1{{format specifies type 'int'}}
360 fprintf(file, "%d", unsigned_value);
361 // expected-warning@+1{{format string is not a string literal}}
362 fprintf(file, unknown_string, unsigned_value);
363 // expected-warning@+1{{format string is not a string literal}}
364 vfprintf(file, unknown_string, va);
365 }
366
367 // expected-warning@+1{{format specifies type 'int'}}
368 printf("%d", unsigned_value);
369 // expected-warning@+1{{format string is not a string literal}}
370 printf(unknown_string, unsigned_value);
371 // expected-warning@+1{{format string is not a string literal}}
372 vprintf(unknown_string, va);
373
374 {
375 char buf[128];
376 // expected-warning@+1{{format specifies type 'int'}}
377 sprintf(buf, "%d", unsigned_value);
378 // expected-warning@+1{{format string is not a string literal}}
379 sprintf(buf, unknown_string, unsigned_value);
380 // expected-warning@+1{{format string is not a string literal}}
381 sprintf(buf, unknown_string, va);
382
383 // expected-warning@+1{{format specifies type 'int'}}
384 snprintf(buf, sizeof(buf), "%d", unsigned_value);
385 // expected-warning@+1{{format string is not a string literal}}
386 snprintf(buf, sizeof(buf), unknown_string, unsigned_value);
387 // expected-warning@+1{{format string is not a string literal}}
388 vsnprintf(buf, sizeof(buf), unknown_string, va);
389 }
390
391 // FIXME: below are general format string cases where clang should probably try to warn.
392 {
393 char buf[4];
394 sprintf(buf, "%s", "1234");
395 sprintf(buf, "1%s4", "23");
396 sprintf(buf, "%d", 1234);
397
398 // Similar thoughts for strncpy, etc.
399 }
400}
401
402static void testStdlib() {
zijunzhao5a918d92022-11-28 21:05:55 +0000403#pragma clang diagnostic push
404#pragma clang diagnostic ignored "-Wnonnull"
George Burgess IV9a274102019-06-04 15:39:52 -0700405 char path_buffer[PATH_MAX - 1];
George Burgess IV8c0ec112019-06-06 17:23:32 -0700406 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700407 // expected-error@+1{{must be NULL or a pointer to a buffer with >= PATH_MAX bytes}}
408 realpath("/", path_buffer);
George Burgess IV8c0ec112019-06-06 17:23:32 -0700409 // expected-warning@+1{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700410 realpath("/", nullptr);
411
George Burgess IV8c0ec112019-06-06 17:23:32 -0700412 // expected-warning@+2{{ignoring return value of function}}
413 // expected-error@+1{{flipped arguments?}}
George Burgess IV9a274102019-06-04 15:39:52 -0700414 realpath(nullptr, path_buffer);
415
George Burgess IV8c0ec112019-06-06 17:23:32 -0700416 // expected-warning@+2{{ignoring return value of function}}
George Burgess IV9a274102019-06-04 15:39:52 -0700417 // expected-error@+1{{flipped arguments?}}
418 realpath(nullptr, nullptr);
zijunzhao5a918d92022-11-28 21:05:55 +0000419#pragma clang diagnostic pop
George Burgess IV9a274102019-06-04 15:39:52 -0700420}
421} // namespace compilation_tests
422#endif
423
424FORTIFY_TEST(poll) {
425 int pipe_fds[2];
426 if (pipe(pipe_fds)) err(1, "pipe failed");
427
428 // after this, pipe_fds[0] should always report RDHUP
429 if (close(pipe_fds[1])) err(1, "close failed");
430
431 struct pollfd poll_fd = { pipe_fds[0], POLLRDHUP, 0 };
432 {
433 struct pollfd few_fds[] = { poll_fd, poll_fd };
434 // expected-error@+1{{fd_count is larger than the given buffer}}
435 EXPECT_FORTIFY_DEATH(poll(few_fds, 3, 0));
436 // expected-error@+1{{fd_count is larger than the given buffer}}
437 EXPECT_FORTIFY_DEATH(ppoll(few_fds, 3, 0, 0));
438 // expected-error@+1{{fd_count is larger than the given buffer}}
439 EXPECT_FORTIFY_DEATH(ppoll64(few_fds, 3, 0, nullptr));
440 }
441
442 {
443 struct {
444 struct pollfd few[2];
445 struct pollfd extra[1];
446 } fds = { { poll_fd, poll_fd }, { poll_fd } };
447 static_assert(sizeof(fds) >= sizeof(struct pollfd) * 3, "");
448
449#if _FORTIFY_SOURCE > 1
450 // expected-error@+2{{fd_count is larger than the given buffer}}
451#endif
452 EXPECT_FORTIFY_DEATH_STRUCT(poll(fds.few, 3, 0));
453
454 struct timespec timeout = {};
455#if _FORTIFY_SOURCE > 1
456 // expected-error@+2{{fd_count is larger than the given buffer}}
457#endif
458 EXPECT_FORTIFY_DEATH_STRUCT(ppoll(fds.few, 3, &timeout, 0));
459
460#if _FORTIFY_SOURCE > 1
461 // expected-error@+2{{fd_count is larger than the given buffer}}
462#endif
463 EXPECT_FORTIFY_DEATH_STRUCT(ppoll64(fds.few, 3, 0, nullptr));
464 }
465}
466
467FORTIFY_TEST(socket) {
468 {
469 char small_buffer[8];
470 // expected-error@+1{{size bigger than buffer}}
471 EXPECT_FORTIFY_DEATH(recv(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
472 // expected-error@+1{{size bigger than buffer}}
473 EXPECT_FORTIFY_DEATH(recvfrom(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
474
475 // expected-error@+1{{size bigger than buffer}}
476 EXPECT_FORTIFY_DEATH(send(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
477 // expected-error@+1{{size bigger than buffer}}
478 EXPECT_FORTIFY_DEATH(sendto(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0, 0, 0));
479 }
480
481 {
482 struct {
483 char tiny_buffer[4];
484 char tiny_buffer2;
485 } split = {};
486
487 EXPECT_NO_DEATH(recv(kBogusFD, split.tiny_buffer, sizeof(split), 0));
488 EXPECT_NO_DEATH(recvfrom(kBogusFD, split.tiny_buffer, sizeof(split), 0, 0, 0));
489 }
490}
491
492FORTIFY_TEST(sys_stat) {
493 // expected-error@+1{{'umask' called with invalid mode}}
494 EXPECT_FORTIFY_DEATH(umask(01777));
495}
496
497FORTIFY_TEST(stdio) {
498 char small_buffer[8] = {};
499 {
George Burgess IV36926f42019-09-15 16:57:00 -0700500 // expected-error@+1{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700501 EXPECT_FORTIFY_DEATH(snprintf(small_buffer, sizeof(small_buffer) + 1, ""));
502
503 va_list va;
George Burgess IV36926f42019-09-15 16:57:00 -0700504 // expected-error@+2{{size argument is too large}}
George Burgess IV9a274102019-06-04 15:39:52 -0700505 // expected-warning@+1{{format string is empty}}
506 EXPECT_FORTIFY_DEATH(vsnprintf(small_buffer, sizeof(small_buffer) + 1, "", va));
George Burgess IV26d25a22019-06-06 17:45:05 -0700507
508 const char *SOMETIMES_CONST format_string = "aaaaaaaaa";
509
510 // expected-error@+1{{format string will always overflow}}
511 EXPECT_FORTIFY_DEATH(sprintf(small_buffer, format_string));
George Burgess IV9a274102019-06-04 15:39:52 -0700512 }
513
514 // expected-error@+1{{size should not be negative}}
515 EXPECT_FORTIFY_DEATH(fgets(small_buffer, -1, stdin));
516 // expected-error@+1{{size is larger than the destination buffer}}
517 EXPECT_FORTIFY_DEATH(fgets(small_buffer, sizeof(small_buffer) + 1, stdin));
518
519 // expected-error@+1{{size * count overflows}}
520 EXPECT_NO_DEATH(fread(small_buffer, 2, (size_t)-1, stdin));
521 // expected-error@+1{{size * count is too large for the given buffer}}
522 EXPECT_FORTIFY_DEATH(fread(small_buffer, 1, sizeof(small_buffer) + 1, stdin));
523
524 // expected-error@+1{{size * count overflows}}
525 EXPECT_NO_DEATH(fwrite(small_buffer, 2, (size_t)-1, stdout));
526 // expected-error@+1{{size * count is too large for the given buffer}}
527 EXPECT_FORTIFY_DEATH(fwrite(small_buffer, 1, sizeof(small_buffer) + 1, stdout));
528}
529
530FORTIFY_TEST(unistd) {
531 char small_buffer[8];
532
533 // Return value warnings are (sort of) a part of FORTIFY, so we don't ignore them.
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(read(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
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(pread(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
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(pread64(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(write(kBogusFD, small_buffer, sizeof(small_buffer) + 1));
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(pwrite(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
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(pwrite64(kBogusFD, small_buffer, sizeof(small_buffer) + 1, 0));
564#if 0
565 // expected-error@+2{{ignoring return value of function}}
566#endif
567 // expected-error@+1{{bytes overflows the given object}}
568 EXPECT_FORTIFY_DEATH(readlink("/", small_buffer, sizeof(small_buffer) + 1));
569#if 0
570 // expected-error@+2{{ignoring return value of function}}
571#endif
572 // expected-error@+1{{bytes overflows the given object}}
573 EXPECT_FORTIFY_DEATH(getcwd(small_buffer, sizeof(small_buffer) + 1));
574
575 // getcwd allocates and returns a buffer if you pass null to getcwd
576 EXPECT_NO_DEATH(getcwd(nullptr, 0));
577 EXPECT_NO_DEATH(getcwd(nullptr, 4096));
578
579 struct {
580 char tiny_buffer[4];
581 char tiny_buffer2[4];
582 } split;
583
584 EXPECT_NO_DEATH(read(kBogusFD, split.tiny_buffer, sizeof(split)));
585 EXPECT_NO_DEATH(pread(kBogusFD, split.tiny_buffer, sizeof(split), 0));
586 EXPECT_NO_DEATH(pread64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
587 EXPECT_NO_DEATH(write(kBogusFD, split.tiny_buffer, sizeof(split)));
588 EXPECT_NO_DEATH(pwrite(kBogusFD, split.tiny_buffer, sizeof(split), 0));
589 EXPECT_NO_DEATH(pwrite64(kBogusFD, split.tiny_buffer, sizeof(split), 0));
590
591#if _FORTIFY_SOURCE > 1
592 // expected-error@+2{{bytes overflows the given object}}
593#endif
594 EXPECT_FORTIFY_DEATH_STRUCT(readlink("/", split.tiny_buffer, sizeof(split)));
595#if _FORTIFY_SOURCE > 1
596 // expected-error@+2{{bytes overflows the given object}}
597#endif
598 EXPECT_FORTIFY_DEATH_STRUCT(getcwd(split.tiny_buffer, sizeof(split)));
599
600 {
George Burgess IV9a274102019-06-04 15:39:52 -0700601 char* volatile unknown = small_buffer;
602 const size_t count = static_cast<size_t>(SSIZE_MAX) + 1;
603 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
604 EXPECT_FORTIFY_DEATH(read(kBogusFD, unknown, count));
605 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
606 EXPECT_FORTIFY_DEATH(pread(kBogusFD, unknown, count, 0));
607 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
608 EXPECT_FORTIFY_DEATH(pread64(kBogusFD, unknown, count, 0));
609 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
610 EXPECT_FORTIFY_DEATH(write(kBogusFD, unknown, count));
611 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
612 EXPECT_FORTIFY_DEATH(pwrite(kBogusFD, unknown, count, 0));
613 // expected-error@+1{{'count' must be <= SSIZE_MAX}}
614 EXPECT_FORTIFY_DEATH(pwrite64(kBogusFD, unknown, count, 0));
George Burgess IV9a274102019-06-04 15:39:52 -0700615 }
616}
617
618#endif // defined(__BIONIC__)