blob: fb3f73dc3f8ed7f4ef99f60e6a610d96f5facc99 [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>
26
Josh Gaof6e5b582018-06-01 15:30:54 -070027#if defined(__BIONIC__)
28#include <android/fdsan.h>
29#endif
30
Josh Gao7266e912018-08-08 17:31:19 -070031#include <unordered_map>
32
33#include <android-base/unique_fd.h>
34
Josh Gaof6e5b582018-06-01 15:30:54 -070035#define FDSAN_TEST(test_name) TEST_F(FdsanTest, test_name)
36#define EXPECT_FDSAN_DEATH(expression, regex) \
37 EXPECT_DEATH((android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL), expression), \
38 (regex))
39
40struct FdsanTest : public ::testing::Test {
41 void SetUp() override {
42#if defined(__BIONIC__)
43 // The bionic unit test running forks for each test by default, which turns
44 // fdsan off as a side-effect, so we need to turn it back on.
45 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
46#endif
47 }
48};
49
50TEST_F(FdsanTest, unowned_untagged_close) {
51#if defined(__BIONIC__)
52 int fd = open("/dev/null", O_RDONLY);
53 ASSERT_EQ(0, close(fd));
54#endif
55}
56
57TEST_F(FdsanTest, unowned_tagged_close) {
58#if defined(__BIONIC__)
59 int fd = open("/dev/null", O_RDONLY);
60 ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0));
61#endif
62}
63
64TEST_F(FdsanTest, unowned_improperly_tagged_close) {
65#if defined(__BIONIC__)
66 int fd = open("/dev/null", O_RDONLY);
67 EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadbeef), "actually unowned");
68#endif
69}
70
71TEST_F(FdsanTest, unowned_incorrect_exchange) {
72#if defined(__BIONIC__)
73 int fd = open("/dev/null", O_RDONLY);
74 EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
Josh Gao08b7a402018-08-03 14:31:37 -070075 "failed to exchange ownership");
Josh Gaof6e5b582018-06-01 15:30:54 -070076#endif
77}
78
79TEST_F(FdsanTest, owned_untagged_close) {
80#if defined(__BIONIC__)
81 int fd = open("/dev/null", O_RDONLY);
82 android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
83 EXPECT_FDSAN_DEATH(close(fd), "expected to be unowned, actually owned");
84#endif
85}
86
87TEST_F(FdsanTest, owned_tagged_close) {
88#if defined(__BIONIC__)
89 int fd = open("/dev/null", O_RDONLY);
90 android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
91 ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0xdeadbeef));
92#endif
93}
94
95TEST_F(FdsanTest, owned_improperly_tagged_close) {
96#if defined(__BIONIC__)
97 int fd = open("/dev/null", O_RDONLY);
98 android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
99 EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadc0de), "expected to be owned");
100#endif
101}
102
103TEST_F(FdsanTest, owned_incorrect_exchange) {
104#if defined(__BIONIC__)
105 int fd = open("/dev/null", O_RDONLY);
106 android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
107 EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
108 "failed to exchange");
109#endif
110}
111
112TEST_F(FdsanTest, fopen) {
113#if defined(__BIONIC__)
114 FILE* f = fopen("/dev/null", "r");
115 ASSERT_TRUE(f);
116 EXPECT_FDSAN_DEATH(close(fileno(f)), "actually owned by FILE");
117#endif
118}
119
120TEST_F(FdsanTest, closedir) {
121#if defined(__BIONIC__)
122 DIR* dir = opendir("/dev/");
123 ASSERT_TRUE(dir);
124 EXPECT_FDSAN_DEATH(close(dirfd(dir)), "actually owned by DIR");
125#endif
126}
127
128TEST_F(FdsanTest, overflow) {
129#if defined(__BIONIC__)
130 std::unordered_map<int, uint64_t> fds;
131 for (int i = 0; i < 4096; ++i) {
132 int fd = open("/dev/null", O_RDONLY);
133 auto tag = 0xdead00000000ULL | i;
134 android_fdsan_exchange_owner_tag(fd, 0, tag);
135 fds[fd] = tag;
136 }
137
138 for (auto [fd, tag] : fds) {
139 android_fdsan_close_with_tag(fd, tag);
140 }
141#endif
142}
Josh Gao08b7a402018-08-03 14:31:37 -0700143
144TEST_F(FdsanTest, owner_value_high) {
145#if defined(__BIONIC__)
146 int fd = open("/dev/null", O_RDONLY);
147 uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, ~0ULL);
148 android_fdsan_exchange_owner_tag(fd, 0, tag);
149 EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
150 "0xffffffffffffffff");
151#endif
152}
153
154TEST_F(FdsanTest, owner_value_low) {
155#if defined(__BIONIC__)
156 int fd = open("/dev/null", O_RDONLY);
157 uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, 1);
158 android_fdsan_exchange_owner_tag(fd, 0, tag);
159 EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
160 "0x1");
161#endif
162}
Josh Gao7266e912018-08-08 17:31:19 -0700163
164TEST_F(FdsanTest, unique_fd_unowned_close) {
165#if defined(__BIONIC__)
166 android::base::unique_fd fd(open("/dev/null", O_RDONLY));
167 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
168 EXPECT_FDSAN_DEATH(close(fd.get()), "expected to be unowned, actually owned by unique_fd");
169#endif
170}
171
172TEST_F(FdsanTest, unique_fd_untag_on_release) {
173 android::base::unique_fd fd(open("/dev/null", O_RDONLY));
174 close(fd.release());
175}
176
177TEST_F(FdsanTest, unique_fd_move) {
178 android::base::unique_fd fd(open("/dev/null", O_RDONLY));
179 android::base::unique_fd fd_moved = std::move(fd);
180 ASSERT_EQ(-1, fd.get());
181 ASSERT_GT(fd_moved.get(), -1);
182}
183
184TEST_F(FdsanTest, unique_fd_unowned_close_after_move) {
185#if defined(__BIONIC__)
186 android::base::unique_fd fd(open("/dev/null", O_RDONLY));
187 android::base::unique_fd fd_moved = std::move(fd);
188 ASSERT_EQ(-1, fd.get());
189 ASSERT_GT(fd_moved.get(), -1);
190
191 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
192 EXPECT_FDSAN_DEATH(close(fd_moved.get()), "expected to be unowned, actually owned by unique_fd");
193#endif
194}