SM: Add pkgName and isHybrid params to label generation
[platform/core/test/security-tests.git] / src / security-manager-tests / test_cases_credentials.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 <cstdlib>
18 #include <functional>
19 #include <string>
20 #include <sys/types.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23
24 #include <access_provider.h>
25 #include <dpl/test/test_runner.h>
26 #include <memory.h>
27 #include <passwd_access.h>
28 #include <sm_api.h>
29 #include <sm_commons.h>
30 #include <sm_request.h>
31 #include <synchronization_pipe.h>
32 #include <tests_common.h>
33 #include <tzplatform.h>
34 #include <uds.h>
35
36 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER_CREDENTIAL_API)
37
38 using namespace SecurityManagerTest;
39
40 class ProcessCredentials {
41 public:
42     ProcessCredentials(const std::string &smackLabel) : m_label(smackLabel) {}
43
44     const std::string &label(void) const {
45         return m_label;
46     }
47
48     uid_t uid(void) const {
49         return TzPlatformConfig::getGlobalUserId();
50     }
51
52     gid_t gid(void) const {
53         return PasswdAccess::gid("users");
54     }
55
56 private:
57     std::string m_label;
58 };
59
60 pid_t runInChild(const std::function<void(void)> &process) {
61     pid_t pid = fork();
62     RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
63
64     if (pid == 0) {
65         process();
66         exit(EXIT_SUCCESS);
67     }
68     return pid;
69 }
70
71 void udsServer(SynchronizationPipe &pipe, const struct sockaddr_un &sockaddr,
72                const struct ProcessCredentials &peerCredentials) {
73     SecurityServer::AccessProvider ap(peerCredentials.label());
74     ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
75     pipe.claimChildEp();
76
77     int sock = UDSHelpers::createServer(&sockaddr);
78     SockUniquePtr sockPtr(&sock);
79     pipe.post();
80     int clientSock = UDSHelpers::acceptClient(sock);
81
82     UDSHelpers::waitForDisconnect(clientSock);
83 }
84
85 typedef std::function<void(int sock, pid_t pid)> SocketAssertionFn;
86
87 void clientTestTemplate(SocketAssertionFn assertion, const std::string &scope, const std::string &smackLabel) {
88     const auto sockaddr = UDSHelpers::makeAbstractAddress("test_sm_" + scope + ".socket");
89     const ProcessCredentials peerCredentials(smackLabel);
90
91     SynchronizationPipe pipe;
92
93     pid_t pid = runInChild(std::bind(udsServer, std::ref(pipe), std::cref(sockaddr),
94                            std::cref(peerCredentials)));
95
96     pipe.claimParentEp();
97     pipe.wait();
98     int sock = UDSHelpers::createClient(&sockaddr);
99     SockUniquePtr sockPtr(&sock);
100
101     assertion(sock, pid);
102 }
103
104 RUNNER_CHILD_TEST(security_manager_51a_get_id_by_socket)
105 {
106     const char *const sm_app_id = "sm_test_51a_app";
107     const char *const sm_pkg_id = "sm_test_51a_pkg";
108
109     InstallRequest requestInst;
110     requestInst.setAppId(sm_app_id);
111     requestInst.setPkgId(sm_pkg_id);
112
113     Api::install(requestInst);
114
115     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
116
117     clientTestTemplate([&] (int sock, pid_t) {
118         std::string rcvPkgId, rcvAppId;
119         Api::getPkgIdBySocket(sock, &rcvPkgId, &rcvAppId);
120         RUNNER_ASSERT_MSG(rcvPkgId == sm_pkg_id, "pkgIds don't match ret = " << rcvPkgId
121                           << "; expected = " << sm_pkg_id);
122         RUNNER_ASSERT_MSG(rcvAppId == sm_app_id, "appIds don't match ret = " << rcvAppId
123                           << "; expected = " << sm_app_id);
124     }, "tcsm27a", smackLabel);
125
126     InstallRequest requestUninst;
127     requestUninst.setAppId(sm_app_id);
128
129     Api::uninstall(requestUninst);
130 }
131
132 RUNNER_CHILD_TEST(security_manager_51b_get_id_by_socket)
133 {
134     const char *const sm_app_id = "sm_test_51b_app";
135     const char *const sm_pkg_id = "sm_test_51b_pkg";
136
137     InstallRequest requestInst;
138     requestInst.setAppId(sm_app_id);
139     requestInst.setPkgId(sm_pkg_id);
140
141     Api::install(requestInst);
142
143     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
144
145     clientTestTemplate([&] (int sock, pid_t) {
146         std::string rcvPkgId, rcvAppId;
147         Api::getPkgIdBySocket(sock + 1, &rcvPkgId, &rcvAppId, SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT);
148     }, "tcsm27b", smackLabel);
149
150     InstallRequest requestUninst;
151     requestUninst.setAppId(sm_app_id);
152
153     Api::uninstall(requestUninst);
154 }
155
156 RUNNER_CHILD_TEST(security_manager_51c_get_id_by_socket)
157 {
158     const char *const sm_app_id = "sm_test_51c_app";
159     const char *const sm_pkg_id = "sm_test_51c_pkg";
160
161     InstallRequest requestInst;
162     requestInst.setAppId(sm_app_id);
163     requestInst.setPkgId(sm_pkg_id);
164
165     Api::install(requestInst);
166
167     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
168
169     clientTestTemplate([&] (int sock, pid_t) {
170         std::string rcvPkgId;
171         Api::getPkgIdBySocket(sock, &rcvPkgId, nullptr);
172         RUNNER_ASSERT_MSG(rcvPkgId == sm_pkg_id, "pkgIds don't match ret = " << rcvPkgId
173                           << "; expected = " << sm_pkg_id);
174     }, "tcsm27c", smackLabel);
175
176     InstallRequest requestUninst;
177     requestUninst.setAppId(sm_app_id);
178
179     Api::uninstall(requestUninst);
180 }
181
182 RUNNER_CHILD_TEST(security_manager_51d_get_id_by_socket)
183 {
184     const char *const sm_app_id = "sm_test_51d_app";
185     const char *const sm_pkg_id = "sm_test_51d_pkg";
186
187     InstallRequest requestInst;
188     requestInst.setAppId(sm_app_id);
189     requestInst.setPkgId(sm_pkg_id);
190
191     Api::install(requestInst);
192
193     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
194
195     clientTestTemplate([&] (int sock, pid_t) {
196         std::string rcvAppId;
197         Api::getPkgIdBySocket(sock, nullptr, &rcvAppId);
198         RUNNER_ASSERT_MSG(rcvAppId == sm_app_id, "appIds don't match ret = " << rcvAppId
199                           << "; expected = " << sm_app_id);
200     }, "tcsm27d", smackLabel);
201
202     InstallRequest requestUninst;
203     requestUninst.setAppId(sm_app_id);
204
205     Api::uninstall(requestUninst);
206 }
207
208 RUNNER_CHILD_TEST(security_manager_51e_get_id_by_socket)
209 {
210     const char *const sm_app_id = "sm_test_51e_app";
211     const char *const sm_pkg_id = "sm_test_51e_pkg";
212
213     InstallRequest requestInst;
214     requestInst.setAppId(sm_app_id);
215     requestInst.setPkgId(sm_pkg_id);
216
217     Api::install(requestInst);
218
219     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
220
221     clientTestTemplate([&] (int sock, pid_t) {
222         Api::getPkgIdBySocket(sock, nullptr, nullptr, SECURITY_MANAGER_ERROR_INPUT_PARAM);
223     }, "tcsm27e", smackLabel);
224
225     InstallRequest requestUninst;
226     requestUninst.setAppId(sm_app_id);
227
228     Api::uninstall(requestUninst);
229 }
230
231 RUNNER_CHILD_TEST(security_manager_52a_get_id_by_pid)
232 {
233     const char *const sm_app_id = "sm_test_52a_app";
234     const char *const sm_pkg_id = "sm_test_52a_pkg";
235
236     InstallRequest requestInst;
237     requestInst.setAppId(sm_app_id);
238     requestInst.setPkgId(sm_pkg_id);
239
240     Api::install(requestInst);
241
242     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
243
244     clientTestTemplate([&] (int, pid_t pid) {
245         std::string rcvPkgId, rcvAppId;
246         Api::getPkgIdByPid(pid, &rcvPkgId, &rcvAppId);
247         RUNNER_ASSERT_MSG(rcvPkgId == sm_pkg_id, "pkgIds don't match ret = " << rcvPkgId
248                           << "; expected = " << sm_pkg_id);
249         RUNNER_ASSERT_MSG(rcvAppId == sm_app_id, "appIds don't match ret = " << rcvAppId
250                           << "; expected = " << sm_app_id);
251     }, "tcsm28a", smackLabel);
252
253     InstallRequest requestUninst;
254     requestUninst.setAppId(sm_app_id);
255
256     Api::uninstall(requestUninst);
257 }
258
259 RUNNER_CHILD_TEST(security_manager_52b_get_id_by_pid)
260 {
261     const char *const sm_app_id = "sm_test_52b_app";
262     const char *const sm_pkg_id = "sm_test_52b_pkg";
263
264     InstallRequest requestInst;
265     requestInst.setAppId(sm_app_id);
266     requestInst.setPkgId(sm_pkg_id);
267
268     Api::install(requestInst);
269
270     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
271
272     clientTestTemplate([&] (int, pid_t pid) {
273         std::string rcvPkgId, rcvAppId;
274         Api::getPkgIdByPid(pid + 1, &rcvPkgId, &rcvAppId, SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT);
275     }, "tcsm28b", smackLabel);
276
277     InstallRequest requestUninst;
278     requestUninst.setAppId(sm_app_id);
279
280     Api::uninstall(requestUninst);
281 }
282
283 RUNNER_CHILD_TEST(security_manager_52c_get_id_by_pid)
284 {
285     const char *const sm_app_id = "sm_test_52c_app";
286     const char *const sm_pkg_id = "sm_test_52c_pkg";
287
288     InstallRequest requestInst;
289     requestInst.setAppId(sm_app_id);
290     requestInst.setPkgId(sm_pkg_id);
291
292     Api::install(requestInst);
293
294     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
295
296     clientTestTemplate([&] (int, pid_t pid) {
297         std::string rcvPkgId;
298         Api::getPkgIdByPid(pid, &rcvPkgId, nullptr);
299         RUNNER_ASSERT_MSG(rcvPkgId == sm_pkg_id, "pkgIds don't match ret = " << rcvPkgId
300                           << "; expected = " << sm_pkg_id);
301     }, "tcsm28c", smackLabel);
302
303     InstallRequest requestUninst;
304     requestUninst.setAppId(sm_app_id);
305
306     Api::uninstall(requestUninst);
307 }
308
309 RUNNER_CHILD_TEST(security_manager_52d_get_id_by_pid)
310 {
311     const char *const sm_app_id = "sm_test_52d_app";
312     const char *const sm_pkg_id = "sm_test_52d_pkg";
313
314     InstallRequest requestInst;
315     requestInst.setAppId(sm_app_id);
316     requestInst.setPkgId(sm_pkg_id);
317
318     Api::install(requestInst);
319
320     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
321
322     clientTestTemplate([&] (int, pid_t pid) {
323         std::string rcvAppId;
324         Api::getPkgIdByPid(pid, nullptr, &rcvAppId);
325         RUNNER_ASSERT_MSG(rcvAppId == sm_app_id, "appIds don't match ret = " << rcvAppId
326                           << "; expected = " << sm_app_id);
327     }, "tcsm28d", smackLabel);
328
329     InstallRequest requestUninst;
330     requestUninst.setAppId(sm_app_id);
331
332     Api::uninstall(requestUninst);
333 }
334
335 RUNNER_CHILD_TEST(security_manager_52e_get_id_by_pid)
336 {
337     const char *const sm_app_id = "sm_test_52e_app";
338     const char *const sm_pkg_id = "sm_test_52e_pkg";
339
340     InstallRequest requestInst;
341     requestInst.setAppId(sm_app_id);
342     requestInst.setPkgId(sm_pkg_id);
343
344     Api::install(requestInst);
345
346     std::string smackLabel = generateProcessLabel(sm_app_id, sm_pkg_id);
347
348     clientTestTemplate([&] (int sock, pid_t) {
349         Api::getPkgIdByPid(sock, nullptr, nullptr, SECURITY_MANAGER_ERROR_INPUT_PARAM);
350     }, "tcsm28e", smackLabel);
351
352     InstallRequest requestUninst;
353     requestUninst.setAppId(sm_app_id);
354
355     Api::uninstall(requestUninst);
356 }