blob: 86d7055c7a4e4f1e70e8eaa4fd8842075fdc542f [file] [log] [blame]
Tom Cherryc2e181c2017-07-14 16:29:23 -07001/*
2 * Copyright (C) 2017 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 <linux/futex.h>
18#include <pthread.h>
19#include <sys/stat.h>
20#include <unistd.h>
21
22#include <string>
23#include <thread>
24#include <vector>
25
26#include <android-base/file.h>
27#include <android-base/scopeguard.h>
28#include <android-base/test_utils.h>
29#include <gtest/gtest.h>
30#include <selinux/selinux.h>
31
32using namespace std::string_literals;
33
34template <typename T, typename F>
35void WriteFromMultipleThreads(std::vector<std::pair<std::string, T>>& files_and_parameters,
36 F function) {
37 auto num_threads = files_and_parameters.size();
38 pthread_barrier_t barrier;
39 pthread_barrier_init(&barrier, nullptr, num_threads);
40 auto barrier_destroy =
41 android::base::make_scope_guard([&barrier]() { pthread_barrier_destroy(&barrier); });
42
43 auto make_thread_function = [&function, &barrier](const auto& file, const auto& parameter) {
44 return [&]() {
45 function(parameter);
46 pthread_barrier_wait(&barrier);
47 android::base::WriteStringToFile("<empty>", file);
48 };
49 };
50
51 std::vector<std::thread> threads;
52 // TODO(b/63712782): Structured bindings + templated containers are broken in clang :(
53 // for (const auto& [file, parameter] : files_and_parameters) {
54 for (const auto& pair : files_and_parameters) {
55 const auto& file = pair.first;
56 const auto& parameter = pair.second;
57 threads.emplace_back(std::thread(make_thread_function(file, parameter)));
58 }
59
60 for (auto& thread : threads) {
61 thread.join();
62 }
63}
64
65TEST(ueventd, setegid_IsPerThread) {
66 if (getuid() != 0) return;
67
68 TemporaryDir dir;
69
70 gid_t gid = 0;
71 std::vector<std::pair<std::string, gid_t>> files_and_gids;
72 std::generate_n(std::back_inserter(files_and_gids), 100, [&gid, &dir]() {
73 gid++;
74 return std::pair(dir.path + "/gid_"s + std::to_string(gid), gid);
75 });
76
77 WriteFromMultipleThreads(files_and_gids, [](gid_t gid) { EXPECT_EQ(0, setegid(gid)); });
78
79 for (const auto& [file, expected_gid] : files_and_gids) {
80 struct stat info;
81 EXPECT_EQ(0, stat(file.c_str(), &info));
82 EXPECT_EQ(expected_gid, info.st_gid);
83 }
84}
85
86TEST(ueventd, setfscreatecon_IsPerThread) {
87 if (getuid() != 0) return;
88
89 const char* const contexts[] = {
90 "u:object_r:audio_device:s0",
91 "u:object_r:sensors_device:s0",
92 "u:object_r:video_device:s0"
93 "u:object_r:zero_device:s0",
94 };
95
96 TemporaryDir dir;
97 std::vector<std::pair<std::string, std::string>> files_and_contexts;
98 for (const char* context : contexts) {
99 files_and_contexts.emplace_back(dir.path + "/context_"s + context, context);
100 }
101
102 WriteFromMultipleThreads(files_and_contexts, [](const std::string& context) {
103 EXPECT_EQ(0, setfscreatecon(context.c_str()));
104 });
105
106 for (const auto& [file, expected_context] : files_and_contexts) {
107 char* file_context;
108 EXPECT_GT(getfilecon(file.c_str(), &file_context), 0);
109 EXPECT_EQ(expected_context, file_context);
110 freecon(file_context);
111 }
112}