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> |
Josh Gao | 65fb2a7 | 2020-05-07 19:40:14 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 27 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 28 | #if defined(__BIONIC__) |
| 29 | #include <android/fdsan.h> |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 30 | #include <bionic/reserved_signals.h> |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 31 | #endif |
| 32 | |
Josh Gao | 7266e91 | 2018-08-08 17:31:19 -0700 | [diff] [blame] | 33 | #include <unordered_map> |
| 34 | |
| 35 | #include <android-base/unique_fd.h> |
| 36 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 37 | #define FDSAN_TEST(test_name) TEST_F(FdsanTest, test_name) |
| 38 | #define EXPECT_FDSAN_DEATH(expression, regex) \ |
| 39 | EXPECT_DEATH((android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL), expression), \ |
| 40 | (regex)) |
| 41 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 42 | struct fdsan : public ::testing::Test { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 43 | void SetUp() override { |
| 44 | #if defined(__BIONIC__) |
| 45 | // The bionic unit test running forks for each test by default, which turns |
| 46 | // fdsan off as a side-effect, so we need to turn it back on. |
| 47 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); |
| 48 | #endif |
| 49 | } |
| 50 | }; |
| 51 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 52 | struct fdsan_DeathTest : public BionicDeathTest { |
| 53 | #if defined(__BIONIC__) |
| 54 | void SetUp() override { |
| 55 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); |
| 56 | signal(BIONIC_SIGNAL_DEBUGGER, SIG_DFL); // Disable debuggerd. |
| 57 | BionicDeathTest::SetUp(); |
| 58 | } |
| 59 | #endif |
| 60 | }; |
| 61 | |
| 62 | TEST_F(fdsan, unowned_untagged_close) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 63 | #if defined(__BIONIC__) |
| 64 | int fd = open("/dev/null", O_RDONLY); |
| 65 | ASSERT_EQ(0, close(fd)); |
| 66 | #endif |
| 67 | } |
| 68 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 69 | TEST_F(fdsan, unowned_tagged_close) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 70 | #if defined(__BIONIC__) |
| 71 | int fd = open("/dev/null", O_RDONLY); |
| 72 | ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0)); |
| 73 | #endif |
| 74 | } |
| 75 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 76 | TEST_F(fdsan_DeathTest, unowned_improperly_tagged_close) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 77 | #if defined(__BIONIC__) |
| 78 | int fd = open("/dev/null", O_RDONLY); |
| 79 | EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadbeef), "actually unowned"); |
| 80 | #endif |
| 81 | } |
| 82 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 83 | TEST_F(fdsan_DeathTest, unowned_incorrect_exchange) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 84 | #if defined(__BIONIC__) |
| 85 | int fd = open("/dev/null", O_RDONLY); |
| 86 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 87 | "failed to exchange ownership"); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 88 | #endif |
| 89 | } |
| 90 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 91 | TEST_F(fdsan_DeathTest, owned_untagged_close) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 92 | #if defined(__BIONIC__) |
| 93 | int fd = open("/dev/null", O_RDONLY); |
| 94 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 95 | EXPECT_FDSAN_DEATH(close(fd), "expected to be unowned, actually owned"); |
| 96 | #endif |
| 97 | } |
| 98 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 99 | TEST_F(fdsan, owned_tagged_close) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 100 | #if defined(__BIONIC__) |
| 101 | int fd = open("/dev/null", O_RDONLY); |
| 102 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 103 | ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0xdeadbeef)); |
| 104 | #endif |
| 105 | } |
| 106 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 107 | TEST_F(fdsan_DeathTest, owned_improperly_tagged_close) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 108 | #if defined(__BIONIC__) |
| 109 | int fd = open("/dev/null", O_RDONLY); |
| 110 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 111 | EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadc0de), "expected to be owned"); |
| 112 | #endif |
| 113 | } |
| 114 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 115 | TEST_F(fdsan_DeathTest, owned_incorrect_exchange) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 116 | #if defined(__BIONIC__) |
| 117 | int fd = open("/dev/null", O_RDONLY); |
| 118 | android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef); |
| 119 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
| 120 | "failed to exchange"); |
| 121 | #endif |
| 122 | } |
| 123 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 124 | TEST_F(fdsan_DeathTest, fopen) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 125 | #if defined(__BIONIC__) |
| 126 | FILE* f = fopen("/dev/null", "r"); |
| 127 | ASSERT_TRUE(f); |
| 128 | EXPECT_FDSAN_DEATH(close(fileno(f)), "actually owned by FILE"); |
| 129 | #endif |
| 130 | } |
| 131 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 132 | TEST_F(fdsan_DeathTest, closedir) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 133 | #if defined(__BIONIC__) |
| 134 | DIR* dir = opendir("/dev/"); |
| 135 | ASSERT_TRUE(dir); |
| 136 | EXPECT_FDSAN_DEATH(close(dirfd(dir)), "actually owned by DIR"); |
| 137 | #endif |
| 138 | } |
| 139 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 140 | TEST_F(fdsan, overflow) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 141 | #if defined(__BIONIC__) |
| 142 | std::unordered_map<int, uint64_t> fds; |
| 143 | for (int i = 0; i < 4096; ++i) { |
| 144 | int fd = open("/dev/null", O_RDONLY); |
| 145 | auto tag = 0xdead00000000ULL | i; |
| 146 | android_fdsan_exchange_owner_tag(fd, 0, tag); |
| 147 | fds[fd] = tag; |
| 148 | } |
| 149 | |
| 150 | for (auto [fd, tag] : fds) { |
| 151 | android_fdsan_close_with_tag(fd, tag); |
| 152 | } |
| 153 | #endif |
| 154 | } |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 155 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 156 | TEST_F(fdsan_DeathTest, owner_value_high) { |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 157 | #if defined(__BIONIC__) |
| 158 | int fd = open("/dev/null", O_RDONLY); |
| 159 | uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, ~0ULL); |
| 160 | android_fdsan_exchange_owner_tag(fd, 0, tag); |
| 161 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
| 162 | "0xffffffffffffffff"); |
| 163 | #endif |
| 164 | } |
| 165 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 166 | TEST_F(fdsan_DeathTest, owner_value_low) { |
Josh Gao | 08b7a40 | 2018-08-03 14:31:37 -0700 | [diff] [blame] | 167 | #if defined(__BIONIC__) |
| 168 | int fd = open("/dev/null", O_RDONLY); |
| 169 | uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, 1); |
| 170 | android_fdsan_exchange_owner_tag(fd, 0, tag); |
| 171 | EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef), |
| 172 | "0x1"); |
| 173 | #endif |
| 174 | } |
Josh Gao | 7266e91 | 2018-08-08 17:31:19 -0700 | [diff] [blame] | 175 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 176 | TEST_F(fdsan_DeathTest, unique_fd_unowned_close) { |
Josh Gao | 7266e91 | 2018-08-08 17:31:19 -0700 | [diff] [blame] | 177 | #if defined(__BIONIC__) |
| 178 | android::base::unique_fd fd(open("/dev/null", O_RDONLY)); |
| 179 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); |
| 180 | EXPECT_FDSAN_DEATH(close(fd.get()), "expected to be unowned, actually owned by unique_fd"); |
| 181 | #endif |
| 182 | } |
| 183 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 184 | TEST_F(fdsan, unique_fd_untag_on_release) { |
Josh Gao | 7266e91 | 2018-08-08 17:31:19 -0700 | [diff] [blame] | 185 | android::base::unique_fd fd(open("/dev/null", O_RDONLY)); |
| 186 | close(fd.release()); |
| 187 | } |
| 188 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 189 | TEST_F(fdsan, unique_fd_move) { |
Josh Gao | 7266e91 | 2018-08-08 17:31:19 -0700 | [diff] [blame] | 190 | android::base::unique_fd fd(open("/dev/null", O_RDONLY)); |
| 191 | android::base::unique_fd fd_moved = std::move(fd); |
| 192 | ASSERT_EQ(-1, fd.get()); |
| 193 | ASSERT_GT(fd_moved.get(), -1); |
| 194 | } |
| 195 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 196 | TEST_F(fdsan_DeathTest, unique_fd_unowned_close_after_move) { |
Josh Gao | 7266e91 | 2018-08-08 17:31:19 -0700 | [diff] [blame] | 197 | #if defined(__BIONIC__) |
| 198 | android::base::unique_fd fd(open("/dev/null", O_RDONLY)); |
| 199 | android::base::unique_fd fd_moved = std::move(fd); |
| 200 | ASSERT_EQ(-1, fd.get()); |
| 201 | ASSERT_GT(fd_moved.get(), -1); |
| 202 | |
| 203 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL); |
| 204 | EXPECT_FDSAN_DEATH(close(fd_moved.get()), "expected to be unowned, actually owned by unique_fd"); |
| 205 | #endif |
| 206 | } |
Josh Gao | 65fb2a7 | 2020-05-07 19:40:14 -0700 | [diff] [blame] | 207 | |
Elliott Hughes | d76dd14 | 2021-02-18 17:27:16 -0800 | [diff] [blame] | 208 | TEST_F(fdsan, vfork) { |
Josh Gao | 65fb2a7 | 2020-05-07 19:40:14 -0700 | [diff] [blame] | 209 | android::base::unique_fd fd(open("/dev/null", O_RDONLY)); |
| 210 | |
| 211 | pid_t rc = vfork(); |
| 212 | ASSERT_NE(-1, rc); |
| 213 | |
| 214 | if (rc == 0) { |
| 215 | close(fd.get()); |
| 216 | _exit(0); |
| 217 | } |
| 218 | |
| 219 | int status; |
| 220 | pid_t wait_result = waitpid(rc, &status, 0); |
| 221 | ASSERT_EQ(wait_result, rc); |
| 222 | ASSERT_TRUE(WIFEXITED(status)); |
| 223 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 224 | } |