1 /*
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 "service.h"
18
19 #include <algorithm>
20 #include <memory>
21 #include <type_traits>
22 #include <vector>
23
24 #include <gtest/gtest.h>
25
26 #include "lmkd_service.h"
27 #include "util.h"
28
29 namespace android {
30 namespace init {
31
TEST(service,pod_initialized)32 TEST(service, pod_initialized) {
33 constexpr auto memory_size = sizeof(Service);
34 alignas(alignof(Service)) unsigned char old_memory[memory_size];
35
36 for (std::size_t i = 0; i < memory_size; ++i) {
37 old_memory[i] = 0xFF;
38 }
39
40 std::vector<std::string> dummy_args{"/bin/test"};
41 Service* service_in_old_memory =
42 new (old_memory) Service("test_old_memory", nullptr, dummy_args);
43
44 EXPECT_EQ(0U, service_in_old_memory->flags());
45 EXPECT_EQ(0, service_in_old_memory->pid());
46 EXPECT_EQ(0, service_in_old_memory->crash_count());
47 EXPECT_EQ(0U, service_in_old_memory->uid());
48 EXPECT_EQ(0U, service_in_old_memory->gid());
49 EXPECT_EQ(0, service_in_old_memory->namespace_flags());
50 EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory->ioprio_class());
51 EXPECT_EQ(0, service_in_old_memory->ioprio_pri());
52 EXPECT_EQ(0, service_in_old_memory->priority());
53 EXPECT_EQ(DEFAULT_OOM_SCORE_ADJUST, service_in_old_memory->oom_score_adjust());
54 EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
55
56 for (std::size_t i = 0; i < memory_size; ++i) {
57 old_memory[i] = 0xFF;
58 }
59
60 Service* service_in_old_memory2 = new (old_memory) Service(
61 "test_old_memory", 0U, 0U, 0U, std::vector<gid_t>(), 0U, "", nullptr, dummy_args);
62
63 EXPECT_EQ(0U, service_in_old_memory2->flags());
64 EXPECT_EQ(0, service_in_old_memory2->pid());
65 EXPECT_EQ(0, service_in_old_memory2->crash_count());
66 EXPECT_EQ(0U, service_in_old_memory2->uid());
67 EXPECT_EQ(0U, service_in_old_memory2->gid());
68 EXPECT_EQ(0, service_in_old_memory2->namespace_flags());
69 EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory2->ioprio_class());
70 EXPECT_EQ(0, service_in_old_memory2->ioprio_pri());
71 EXPECT_EQ(0, service_in_old_memory2->priority());
72 EXPECT_EQ(DEFAULT_OOM_SCORE_ADJUST, service_in_old_memory2->oom_score_adjust());
73 EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
74 }
75
TEST(service,make_temporary_oneshot_service_invalid_syntax)76 TEST(service, make_temporary_oneshot_service_invalid_syntax) {
77 std::vector<std::string> args;
78 // Nothing.
79 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args).ok());
80
81 // No arguments to 'exec'.
82 args.push_back("exec");
83 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args).ok());
84
85 // No command in "exec --".
86 args.push_back("--");
87 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args).ok());
88 }
89
TEST(service,make_temporary_oneshot_service_too_many_supplementary_gids)90 TEST(service, make_temporary_oneshot_service_too_many_supplementary_gids) {
91 std::vector<std::string> args;
92 args.push_back("exec");
93 args.push_back("seclabel");
94 args.push_back("root"); // uid.
95 args.push_back("root"); // gid.
96 for (int i = 0; i < NR_SVC_SUPP_GIDS; ++i) {
97 args.push_back("root"); // Supplementary gid.
98 }
99 args.push_back("--");
100 args.push_back("/system/bin/id");
101 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args).ok());
102 }
103
Test_make_temporary_oneshot_service(bool dash_dash,bool seclabel,bool uid,bool gid,bool supplementary_gids)104 static void Test_make_temporary_oneshot_service(bool dash_dash, bool seclabel, bool uid, bool gid,
105 bool supplementary_gids) {
106 std::vector<std::string> args;
107 args.push_back("exec");
108 if (seclabel) {
109 args.push_back("u:r:su:s0"); // seclabel
110 if (uid) {
111 args.push_back("log"); // uid
112 if (gid) {
113 args.push_back("shell"); // gid
114 if (supplementary_gids) {
115 args.push_back("system"); // supplementary gid 0
116 args.push_back("adb"); // supplementary gid 1
117 }
118 }
119 }
120 }
121 if (dash_dash) {
122 args.push_back("--");
123 }
124 args.push_back("/system/bin/toybox");
125 args.push_back("id");
126 auto service_ret = Service::MakeTemporaryOneshotService(args);
127 ASSERT_RESULT_OK(service_ret);
128 auto svc = std::move(*service_ret);
129
130 if (seclabel) {
131 ASSERT_EQ("u:r:su:s0", svc->seclabel());
132 } else {
133 ASSERT_EQ("", svc->seclabel());
134 }
135 if (uid) {
136 auto decoded_uid = DecodeUid("log");
137 ASSERT_RESULT_OK(decoded_uid);
138 ASSERT_EQ(*decoded_uid, svc->uid());
139 } else {
140 ASSERT_EQ(0U, svc->uid());
141 }
142 if (gid) {
143 auto decoded_uid = DecodeUid("shell");
144 ASSERT_RESULT_OK(decoded_uid);
145 ASSERT_EQ(*decoded_uid, svc->gid());
146 } else {
147 ASSERT_EQ(0U, svc->gid());
148 }
149 if (supplementary_gids) {
150 ASSERT_EQ(2U, svc->supp_gids().size());
151
152 auto decoded_uid = DecodeUid("system");
153 ASSERT_RESULT_OK(decoded_uid);
154 ASSERT_EQ(*decoded_uid, svc->supp_gids()[0]);
155
156 decoded_uid = DecodeUid("adb");
157 ASSERT_RESULT_OK(decoded_uid);
158 ASSERT_EQ(*decoded_uid, svc->supp_gids()[1]);
159 } else {
160 ASSERT_EQ(0U, svc->supp_gids().size());
161 }
162
163 ASSERT_EQ(static_cast<std::size_t>(2), svc->args().size());
164 ASSERT_EQ("/system/bin/toybox", svc->args()[0]);
165 ASSERT_EQ("id", svc->args()[1]);
166 }
167
TEST(service,make_temporary_oneshot_service_with_everything)168 TEST(service, make_temporary_oneshot_service_with_everything) {
169 Test_make_temporary_oneshot_service(true, true, true, true, true);
170 }
171
TEST(service,make_temporary_oneshot_service_with_seclabel_uid_gid)172 TEST(service, make_temporary_oneshot_service_with_seclabel_uid_gid) {
173 Test_make_temporary_oneshot_service(true, true, true, true, false);
174 }
175
TEST(service,make_temporary_oneshot_service_with_seclabel_uid)176 TEST(service, make_temporary_oneshot_service_with_seclabel_uid) {
177 Test_make_temporary_oneshot_service(true, true, true, false, false);
178 }
179
TEST(service,make_temporary_oneshot_service_with_seclabel)180 TEST(service, make_temporary_oneshot_service_with_seclabel) {
181 Test_make_temporary_oneshot_service(true, true, false, false, false);
182 }
183
TEST(service,make_temporary_oneshot_service_with_just_command)184 TEST(service, make_temporary_oneshot_service_with_just_command) {
185 Test_make_temporary_oneshot_service(true, false, false, false, false);
186 }
187
TEST(service,make_temporary_oneshot_service_with_just_command_no_dash)188 TEST(service, make_temporary_oneshot_service_with_just_command_no_dash) {
189 Test_make_temporary_oneshot_service(false, false, false, false, false);
190 }
191
192 } // namespace init
193 } // namespace android
194