Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include "BionicDeathTest.h" |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <unordered_map> |
| 28 | |
| 29 | #if defined(__BIONIC__) |
| 30 | #include <android/fdsan.h> |
| 31 | #endif |
| 32 | |
| 33 | #define FDSAN_TEST(test_name) TEST_F(FdsanTest, test_name) |
| 34 | #define EXPECT_FDSAN_DEATH(expression, regex) \ |
| 35 | EXPECT_DEATH((android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL), expression), \ |
| 36 | (regex)) |
| 37 | |
| 38 | struct FdsanTest : public ::testing::Test { |
| 39 | void SetUp() override { |
| 40 | #if defined(__BIONIC__) |
| 41 | // The bionic unit test running forks for each test by default, which turns |
| 42 | // fdsan off as a side-effect, so we need to turn it back on. |
| 43 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); |
| 44 | #endif |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | TEST_F(FdsanTest, unowned_untagged_close) { |
| 49 | #if defined(__BIONIC__) |
| 50 | int fd = open("/dev/null", O_RDONLY); |
| 51 | ASSERT_EQ(0, close(fd)); |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | TEST_F(FdsanTest, unowned_tagged_close) { |
| 56 | #if defined(__BIONIC__) |
| 57 | int fd = open("/dev/null", O_RDONLY); |
| 58 | ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0)); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | TEST_F(FdsanTest, unowned_improperly_tagged_close) { |
| 63 | #if defined(__BIONIC__) |
| 64 | int fd = open("/dev/null", O_RDONLY); |
| 65 | EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadbeef), "actually unowned"); |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | TEST_F(FdsanTest, unowned_incorrect_exchange) { |
| 70 | #if defined(__BIONIC__) |
| 71 | int fd = open("/dev/null", O_RDONLY); |
| 72 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame^] | 73 | "failed to exchange ownership"); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 74 | #endif |
| 75 | } |
| 76 | |
| 77 | TEST_F(FdsanTest, owned_untagged_close) { |
| 78 | #if defined(__BIONIC__) |
| 79 | int fd = open("/dev/null", O_RDONLY); |
| 80 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 81 | EXPECT_FDSAN_DEATH(close(fd), "expected to be unowned, actually owned"); |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | TEST_F(FdsanTest, owned_tagged_close) { |
| 86 | #if defined(__BIONIC__) |
| 87 | int fd = open("/dev/null", O_RDONLY); |
| 88 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 89 | ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0xdeadbeef)); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | TEST_F(FdsanTest, owned_improperly_tagged_close) { |
| 94 | #if defined(__BIONIC__) |
| 95 | int fd = open("/dev/null", O_RDONLY); |
| 96 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 97 | EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadc0de), "expected to be owned"); |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | TEST_F(FdsanTest, owned_incorrect_exchange) { |
| 102 | #if defined(__BIONIC__) |
| 103 | int fd = open("/dev/null", O_RDONLY); |
| 104 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 105 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
| 106 | "failed to exchange"); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | TEST_F(FdsanTest, fopen) { |
| 111 | #if defined(__BIONIC__) |
| 112 | FILE* f = fopen("/dev/null", "r"); |
| 113 | ASSERT_TRUE(f); |
| 114 | EXPECT_FDSAN_DEATH(close(fileno(f)), "actually owned by FILE"); |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | TEST_F(FdsanTest, closedir) { |
| 119 | #if defined(__BIONIC__) |
| 120 | DIR* dir = opendir("/dev/"); |
| 121 | ASSERT_TRUE(dir); |
| 122 | EXPECT_FDSAN_DEATH(close(dirfd(dir)), "actually owned by DIR"); |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | TEST_F(FdsanTest, overflow) { |
| 127 | #if defined(__BIONIC__) |
| 128 | std::unordered_map<int, uint64_t> fds; |
| 129 | for (int i = 0; i < 4096; ++i) { |
| 130 | int fd = open("/dev/null", O_RDONLY); |
| 131 | auto tag = 0xdead00000000ULL | i; |
| 132 | android_fdsan_exchange_owner_tag(fd, 0, tag); |
| 133 | fds[fd] = tag; |
| 134 | } |
| 135 | |
| 136 | for (auto [fd, tag] : fds) { |
| 137 | android_fdsan_close_with_tag(fd, tag); |
| 138 | } |
| 139 | #endif |
| 140 | } |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame^] | 141 | |
| 142 | TEST_F(FdsanTest, owner_value_high) { |
| 143 | #if defined(__BIONIC__) |
| 144 | int fd = open("/dev/null", O_RDONLY); |
| 145 | uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, ~0ULL); |
| 146 | android_fdsan_exchange_owner_tag(fd, 0, tag); |
| 147 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
| 148 | "0xffffffffffffffff"); |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | TEST_F(FdsanTest, owner_value_low) { |
| 153 | #if defined(__BIONIC__) |
| 154 | int fd = open("/dev/null", O_RDONLY); |
| 155 | uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, 1); |
| 156 | android_fdsan_exchange_owner_tag(fd, 0, tag); |
| 157 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
| 158 | "0x1"); |
| 159 | #endif |
| 160 | } |