blob: c294ff3530baf904ea3d379db077b7a9bc8739f2 [file] [log] [blame]
Josh Gaof6e5b582018-06-01 15:30:54 -07001/*
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 Gao65fb2a72020-05-07 19:40:14 -070026#include <unistd.h>
Josh Gaof6e5b582018-06-01 15:30:54 -070027
Josh Gaof6e5b582018-06-01 15:30:54 -070028#if defined(__BIONIC__)
29#include <android/fdsan.h>
Elliott Hughesd76dd142021-02-18 17:27:16 -080030#include <bionic/reserved_signals.h>
Josh Gaof6e5b582018-06-01 15:30:54 -070031#endif
32
Josh Gao7266e912018-08-08 17:31:19 -070033#include <unordered_map>
34
35#include <android-base/unique_fd.h>
36
Josh Gaof6e5b582018-06-01 15:30:54 -070037#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 Hughesd76dd142021-02-18 17:27:16 -080042struct fdsan : public ::testing::Test {
Josh Gaof6e5b582018-06-01 15:30:54 -070043 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 Hughesd76dd142021-02-18 17:27:16 -080052struct 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
62TEST_F(fdsan, unowned_untagged_close) {
Josh Gaof6e5b582018-06-01 15:30:54 -070063#if defined(__BIONIC__)
64 int fd = open("/dev/null", O_RDONLY);
65 ASSERT_EQ(0, close(fd));
66#endif
67}
68
Elliott Hughesd76dd142021-02-18 17:27:16 -080069TEST_F(fdsan, unowned_tagged_close) {
Josh Gaof6e5b582018-06-01 15:30:54 -070070#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 Hughesd76dd142021-02-18 17:27:16 -080076TEST_F(fdsan_DeathTest, unowned_improperly_tagged_close) {
Josh Gaof6e5b582018-06-01 15:30:54 -070077#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 Hughesd76dd142021-02-18 17:27:16 -080083TEST_F(fdsan_DeathTest, unowned_incorrect_exchange) {
Josh Gaof6e5b582018-06-01 15:30:54 -070084#if defined(__BIONIC__)
85 int fd = open("/dev/null", O_RDONLY);
86 EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
Josh Gao08b7a402018-08-03 14:31:37 -070087 "failed to exchange ownership");
Josh Gaof6e5b582018-06-01 15:30:54 -070088#endif
89}
90
Elliott Hughesd76dd142021-02-18 17:27:16 -080091TEST_F(fdsan_DeathTest, owned_untagged_close) {
Josh Gaof6e5b582018-06-01 15:30:54 -070092#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 Hughesd76dd142021-02-18 17:27:16 -080099TEST_F(fdsan, owned_tagged_close) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700100#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 Hughesd76dd142021-02-18 17:27:16 -0800107TEST_F(fdsan_DeathTest, owned_improperly_tagged_close) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700108#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 Hughesd76dd142021-02-18 17:27:16 -0800115TEST_F(fdsan_DeathTest, owned_incorrect_exchange) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700116#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 Hughesd76dd142021-02-18 17:27:16 -0800124TEST_F(fdsan_DeathTest, fopen) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700125#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 Hughesd76dd142021-02-18 17:27:16 -0800132TEST_F(fdsan_DeathTest, closedir) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700133#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 Hughesd76dd142021-02-18 17:27:16 -0800140TEST_F(fdsan, overflow) {
Josh Gaof6e5b582018-06-01 15:30:54 -0700141#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 Gao08b7a402018-08-03 14:31:37 -0700155
Elliott Hughesd76dd142021-02-18 17:27:16 -0800156TEST_F(fdsan_DeathTest, owner_value_high) {
Josh Gao08b7a402018-08-03 14:31:37 -0700157#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 Hughesd76dd142021-02-18 17:27:16 -0800166TEST_F(fdsan_DeathTest, owner_value_low) {
Josh Gao08b7a402018-08-03 14:31:37 -0700167#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 Gao7266e912018-08-08 17:31:19 -0700175
Elliott Hughesd76dd142021-02-18 17:27:16 -0800176TEST_F(fdsan_DeathTest, unique_fd_unowned_close) {
Josh Gao7266e912018-08-08 17:31:19 -0700177#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 Hughesd76dd142021-02-18 17:27:16 -0800184TEST_F(fdsan, unique_fd_untag_on_release) {
Josh Gao7266e912018-08-08 17:31:19 -0700185 android::base::unique_fd fd(open("/dev/null", O_RDONLY));
186 close(fd.release());
187}
188
Elliott Hughesd76dd142021-02-18 17:27:16 -0800189TEST_F(fdsan, unique_fd_move) {
Josh Gao7266e912018-08-08 17:31:19 -0700190 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 Hughesd76dd142021-02-18 17:27:16 -0800196TEST_F(fdsan_DeathTest, unique_fd_unowned_close_after_move) {
Josh Gao7266e912018-08-08 17:31:19 -0700197#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 Gao65fb2a72020-05-07 19:40:14 -0700207
Elliott Hughesd76dd142021-02-18 17:27:16 -0800208TEST_F(fdsan, vfork) {
Josh Gao65fb2a72020-05-07 19:40:14 -0700209 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}