2018-06-02 00:30:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2020-05-08 04:40:14 +02:00
|
|
|
#include <unistd.h>
|
2018-06-02 00:30:54 +02:00
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
#include <android/fdsan.h>
|
2021-02-19 02:27:16 +01:00
|
|
|
#include <bionic/reserved_signals.h>
|
2018-06-02 00:30:54 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-09 02:31:19 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2021-04-10 02:13:09 +02:00
|
|
|
#include <android-base/silent_death_test.h>
|
2018-08-09 02:31:19 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
|
2018-06-02 00:30:54 +02:00
|
|
|
#define FDSAN_TEST(test_name) TEST_F(FdsanTest, test_name)
|
|
|
|
#define EXPECT_FDSAN_DEATH(expression, regex) \
|
|
|
|
EXPECT_DEATH((android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL), expression), \
|
|
|
|
(regex))
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
struct fdsan : public ::testing::Test {
|
2018-06-02 00:30:54 +02:00
|
|
|
void SetUp() override {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// The bionic unit test running forks for each test by default, which turns
|
|
|
|
// fdsan off as a side-effect, so we need to turn it back on.
|
|
|
|
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-10 02:13:09 +02:00
|
|
|
struct fdsan_DeathTest : public SilentDeathTest {
|
2021-02-19 02:27:16 +01:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
void SetUp() override {
|
|
|
|
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
|
|
|
|
signal(BIONIC_SIGNAL_DEBUGGER, SIG_DFL); // Disable debuggerd.
|
2021-04-10 02:13:09 +02:00
|
|
|
SilentDeathTest::SetUp();
|
2021-02-19 02:27:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(fdsan, unowned_untagged_close) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
ASSERT_EQ(0, close(fd));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan, unowned_tagged_close) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, unowned_improperly_tagged_close) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadbeef), "actually unowned");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, unowned_incorrect_exchange) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
|
2018-08-03 23:31:37 +02:00
|
|
|
"failed to exchange ownership");
|
2018-06-02 00:30:54 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, owned_untagged_close) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
|
|
|
|
EXPECT_FDSAN_DEATH(close(fd), "expected to be unowned, actually owned");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan, owned_tagged_close) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
|
|
|
|
ASSERT_EQ(0, android_fdsan_close_with_tag(fd, 0xdeadbeef));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, owned_improperly_tagged_close) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
|
|
|
|
EXPECT_FDSAN_DEATH(android_fdsan_close_with_tag(fd, 0xdeadc0de), "expected to be owned");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, owned_incorrect_exchange) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, 0xdeadbeef);
|
|
|
|
EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
|
|
|
|
"failed to exchange");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, fopen) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
FILE* f = fopen("/dev/null", "r");
|
|
|
|
ASSERT_TRUE(f);
|
|
|
|
EXPECT_FDSAN_DEATH(close(fileno(f)), "actually owned by FILE");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, closedir) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
DIR* dir = opendir("/dev/");
|
|
|
|
ASSERT_TRUE(dir);
|
|
|
|
EXPECT_FDSAN_DEATH(close(dirfd(dir)), "actually owned by DIR");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan, overflow) {
|
2018-06-02 00:30:54 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
std::unordered_map<int, uint64_t> fds;
|
|
|
|
for (int i = 0; i < 4096; ++i) {
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
auto tag = 0xdead00000000ULL | i;
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, tag);
|
|
|
|
fds[fd] = tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto [fd, tag] : fds) {
|
|
|
|
android_fdsan_close_with_tag(fd, tag);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2018-08-03 23:31:37 +02:00
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, owner_value_high) {
|
2018-08-03 23:31:37 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, ~0ULL);
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, tag);
|
|
|
|
EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
|
|
|
|
"0xffffffffffffffff");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, owner_value_low) {
|
2018-08-03 23:31:37 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
int fd = open("/dev/null", O_RDONLY);
|
|
|
|
uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD, 1);
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0, tag);
|
|
|
|
EXPECT_FDSAN_DEATH(android_fdsan_exchange_owner_tag(fd, 0xbadc0de, 0xdeadbeef),
|
|
|
|
"0x1");
|
|
|
|
#endif
|
|
|
|
}
|
2018-08-09 02:31:19 +02:00
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, unique_fd_unowned_close) {
|
2018-08-09 02:31:19 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
android::base::unique_fd fd(open("/dev/null", O_RDONLY));
|
|
|
|
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
|
|
|
|
EXPECT_FDSAN_DEATH(close(fd.get()), "expected to be unowned, actually owned by unique_fd");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan, unique_fd_untag_on_release) {
|
2018-08-09 02:31:19 +02:00
|
|
|
android::base::unique_fd fd(open("/dev/null", O_RDONLY));
|
|
|
|
close(fd.release());
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan, unique_fd_move) {
|
2018-08-09 02:31:19 +02:00
|
|
|
android::base::unique_fd fd(open("/dev/null", O_RDONLY));
|
|
|
|
android::base::unique_fd fd_moved = std::move(fd);
|
|
|
|
ASSERT_EQ(-1, fd.get());
|
|
|
|
ASSERT_GT(fd_moved.get(), -1);
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan_DeathTest, unique_fd_unowned_close_after_move) {
|
2018-08-09 02:31:19 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
android::base::unique_fd fd(open("/dev/null", O_RDONLY));
|
|
|
|
android::base::unique_fd fd_moved = std::move(fd);
|
|
|
|
ASSERT_EQ(-1, fd.get());
|
|
|
|
ASSERT_GT(fd_moved.get(), -1);
|
|
|
|
|
|
|
|
android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_FATAL);
|
|
|
|
EXPECT_FDSAN_DEATH(close(fd_moved.get()), "expected to be unowned, actually owned by unique_fd");
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-08 04:40:14 +02:00
|
|
|
|
2021-02-19 02:27:16 +01:00
|
|
|
TEST_F(fdsan, vfork) {
|
2020-05-08 04:40:14 +02:00
|
|
|
android::base::unique_fd fd(open("/dev/null", O_RDONLY));
|
|
|
|
|
|
|
|
pid_t rc = vfork();
|
|
|
|
ASSERT_NE(-1, rc);
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
close(fd.get());
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int status;
|
|
|
|
pid_t wait_result = waitpid(rc, &status, 0);
|
|
|
|
ASSERT_EQ(wait_result, rc);
|
|
|
|
ASSERT_TRUE(WIFEXITED(status));
|
|
|
|
ASSERT_EQ(0, WEXITSTATUS(status));
|
|
|
|
}
|