2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/capability.h>
24 #include <sys/types.h>
28 #include <unordered_set>
31 #include <security-manager-types.h>
32 #include <sys/smack.h>
34 #include <cynara_test_client.h>
35 #include <dpl/test/test_runner.h>
38 #include <sm_commons.h>
40 #include <synchronization_pipe.h>
41 #include <sm_request.h>
42 #include <tests_common.h>
43 #include "tzplatform.h"
45 using namespace SecurityManagerTest;
47 // Common const values
49 const privileges_t SM_ALLOWED_PRIVILEGES = {
50 "http://tizen.org/privilege/display",
51 "http://tizen.org/privilege/nfc"
54 const privileges_t SM_DENIED_PRIVILEGES = {
55 "http://tizen.org/privilege/bluetooth",
56 "http://tizen.org/privilege/power"
59 const privileges_t SM_NO_PRIVILEGES = {
62 const std::vector<std::string> SM_ALLOWED_GROUPS = {"db_browser", "db_alarm"};
64 const std::string uidToStr(const uid_t uid)
66 return std::to_string(static_cast<unsigned int>(uid));
69 // Common implementation details
71 std::string generateProcessLabel(const std::string &appId, const std::string &pkgId, bool isHybrid)
75 return "User::App::" + appId;
78 std::string generatePathRWLabel(const std::string &pkgId)
80 return "User::Pkg::" + pkgId;
83 static std::string genPath(int app_num, const char *postfix) {
85 sprintf(buf, "%02d", app_num);
86 return TzPlatformConfig::globalAppDir() + "/sm_test_" + std::string(buf) + "_pkg_id_full/" + std::string(postfix);
89 std::string genRWPath(int app_num) {
90 return genPath(app_num, "app_dir");
92 std::string genROPath(int app_num) {
93 return genPath(app_num, "app_dir_ro");
95 std::string genPublicROPath(int app_num) {
96 return genPath(app_num, "app_dir_public_ro");
99 std::string genOwnerRWOthersROPath(int app_num) {
100 return genPath(app_num, "app_dir_rw_others_ro");
103 static std::string generatePkgLabelOwnerRWothersRO(const std::string &pkgId)
105 return "User::Pkg::" + pkgId + "::SharedRO";
108 // Common DB/nftw checks
110 // nftw doesn't allow passing user data to functions. Work around by using global variable
111 static std::string nftw_expected_label;
112 bool nftw_expected_transmute;
113 bool nftw_expected_exec;
115 static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb,
116 const char* correctLabel, bool transmute_test, bool exec_test)
120 char* label = nullptr;
123 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
124 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
125 labelPtr.reset(label);
126 RUNNER_ASSERT_MSG(label != nullptr, "ACCESS label on " << fpath << " is not set");
127 result = strcmp(correctLabel, label);
128 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect"
129 " (should be '" << correctLabel << "' and is '" << label << "')");
133 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
134 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
135 labelPtr.reset(label);
137 if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR) && exec_test) {
138 RUNNER_ASSERT_MSG(label != nullptr, "EXEC label on " << fpath << " is not set");
139 result = strcmp(correctLabel, label);
140 RUNNER_ASSERT_MSG(result == 0, "Incorrect EXEC label on executable file " << fpath);
142 RUNNER_ASSERT_MSG(label == nullptr, "EXEC label on " << fpath << " is set");
146 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
147 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
148 labelPtr.reset(label);
150 if (S_ISDIR(sb->st_mode) && transmute_test == true) {
151 RUNNER_ASSERT_MSG(label != nullptr, "TRANSMUTE label on " << fpath << " is not set at all");
152 RUNNER_ASSERT_MSG(strcmp(label,"TRUE") == 0,
153 "TRANSMUTE label on " << fpath << " is not set properly: '"<<label<<"'");
155 RUNNER_ASSERT_MSG(label == nullptr, "TRANSMUTE label on " << fpath << " is set");
161 static int nftw_check_sm_labels(const char *fpath, const struct stat *sb,
162 int /*typeflag*/, struct FTW* /*ftwbuf*/)
164 return nftw_check_sm_labels_app_dir(fpath, sb,
165 nftw_expected_label.c_str(), nftw_expected_transmute, nftw_expected_exec);
168 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
169 int /*typeflag*/, struct FTW* /*ftwbuf*/)
173 char* label = nullptr;
176 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
177 labelPtr.reset(label);
178 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
179 result = strcmp("canary_label", labelPtr.get());
180 RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is overwritten");
183 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
184 labelPtr.reset(label);
185 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
186 result = strcmp("canary_label", labelPtr.get());
187 RUNNER_ASSERT_MSG(result == 0, "EXEC label on " << fpath << " is overwritten");
190 result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
191 labelPtr.reset(label);
192 RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
193 RUNNER_ASSERT_MSG(labelPtr.get() == nullptr, "TRANSMUTE label on " << fpath << " is set");
198 static int nftw_set_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
199 int /*typeflag*/, struct FTW* /*ftwbuf*/)
201 smack_lsetlabel(fpath, "canary_label", SMACK_LABEL_ACCESS);
202 smack_lsetlabel(fpath, "canary_label", SMACK_LABEL_EXEC);
203 smack_lsetlabel(fpath, nullptr, SMACK_LABEL_TRANSMUTE);
208 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
209 int /*typeflag*/, struct FTW* /*ftwbuf*/)
211 smack_lsetlabel(fpath, nullptr, SMACK_LABEL_ACCESS);
212 smack_lsetlabel(fpath, nullptr, SMACK_LABEL_EXEC);
213 smack_lsetlabel(fpath, nullptr, SMACK_LABEL_TRANSMUTE);
218 static const std::string SM_DENIED_PATH = TzPlatformConfig::globalAppDir() + "/non_app_dir";
220 void check_app_permissions(const char *const app_id, const char *const pkg_id,
221 const char *const user, const privileges_t &allowed_privs,
222 const privileges_t &denied_privs)
225 std::string smackLabel = generateProcessLabel(app_id, pkg_id);
227 CynaraTestClient::Client ctc;
229 for (auto &priv : allowed_privs) {
230 ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_ACCESS_ALLOWED);
233 for (auto &priv : denied_privs) {
234 ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_ACCESS_DENIED);
239 void check_app_after_install(const char *const app_id, const char *const pkg_id)
241 TestSecurityManagerDatabase dbtest;
242 dbtest.test_db_after__app_install(app_id, pkg_id);
245 static void check_app_gids(const char *const app_id, const std::vector<gid_t> &allowed_gids)
248 gid_t main_gid = getgid();
249 std::unordered_set<gid_t> reference_gids(allowed_gids.begin(), allowed_gids.end());
251 // Reset supplementary groups
252 ret = setgroups(0, NULL);
253 RUNNER_ASSERT_MSG(ret != -1, "Unable to set supplementary groups");
255 Api::setProcessGroups(app_id);
257 ret = getgroups(0, nullptr);
258 RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
260 std::vector<gid_t> actual_gids(ret);
261 ret = getgroups(ret, actual_gids.data());
262 RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
264 for (const auto &gid : actual_gids) {
265 RUNNER_ASSERT_MSG(gid == main_gid || reference_gids.count(gid) > 0,
266 "Application shouldn't get access to group " << gid);
267 reference_gids.erase(gid);
270 RUNNER_ASSERT_MSG(reference_gids.empty(), "Application didn't get access to some groups");
273 static const char *const ANY_USER_REPRESENTATION = "anyuser";/*this may be actually any string*/
275 void check_app_after_install(const char *const app_id, const char *const pkg_id,
276 const privileges_t &allowed_privs,
277 const privileges_t &denied_privs,
278 const std::vector<std::string> &allowed_groups)
280 TestSecurityManagerDatabase dbtest;
281 dbtest.test_db_after__app_install(app_id, pkg_id);
283 /*Privileges should be granted to all users if root installs app*/
284 check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, allowed_privs, denied_privs);
286 /* Setup mapping of gids to privileges */
287 /* Do this for each privilege for extra check */
288 for (const auto &privilege : allowed_privs) {
289 dbtest.setup_privilege_groups(privilege, allowed_groups);
292 std::vector<gid_t> allowed_gids;
294 for (const auto &groupName : allowed_groups) {
296 struct group* grp = getgrnam(groupName.c_str());
297 RUNNER_ASSERT_ERRNO_MSG(grp, "Group: " << groupName << " not found");
298 allowed_gids.push_back(grp->gr_gid);
301 check_app_gids(app_id, allowed_gids);
304 void check_path(const std::string &path, const std::string &label) {
305 nftw_expected_label = label;
306 nftw_expected_transmute = true;
307 nftw_expected_exec = false;
310 int result = nftw(path.c_str(), &nftw_check_sm_labels, FTW_MAX_FDS, FTW_PHYS);
311 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << path);
314 void check_app_path_after_install(int app_num, const char *pkgId, bool others_enabled)
316 std::string SM_RW_PATH = genRWPath(app_num);
317 std::string SM_RO_PATH = genROPath(app_num);
318 std::string SM_PUBLIC_RO_PATH = genPublicROPath(app_num);
321 nftw_expected_label = generatePathRWLabel(pkgId);
322 nftw_expected_transmute = true;
323 nftw_expected_exec = false;
325 result = nftw(SM_RW_PATH.c_str(), &nftw_check_sm_labels, FTW_MAX_FDS, FTW_PHYS);
326 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_RW_PATH);
328 nftw_expected_label = generatePathRWLabel(pkgId) + "::RO";
329 nftw_expected_transmute = false;
330 nftw_expected_exec = false;
332 result = nftw(SM_RO_PATH.c_str(), &nftw_check_sm_labels, FTW_MAX_FDS, FTW_PHYS);
333 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_RO_PATH);
335 nftw_expected_label = "User::Home";
336 nftw_expected_transmute = true;
337 nftw_expected_exec = false;
339 result = nftw(SM_PUBLIC_RO_PATH.c_str(), &nftw_check_sm_labels, FTW_MAX_FDS, FTW_PHYS);
340 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
342 result = nftw(SM_DENIED_PATH.c_str(), &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
343 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
345 // owner RW, others RO
347 std::string SM_OWNER_RW_OTHERS_RO_PATH = genOwnerRWOthersROPath(app_num);
348 nftw_expected_label = generatePkgLabelOwnerRWothersRO(pkgId);
349 nftw_expected_transmute = true;
350 nftw_expected_exec = false;
352 result = nftw(SM_OWNER_RW_OTHERS_RO_PATH.c_str(), &nftw_check_sm_labels, FTW_MAX_FDS, FTW_PHYS);
353 RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_OWNER_RW_OTHERS_RO_PATH);
357 void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
358 const bool is_pkg_removed)
360 TestSecurityManagerDatabase dbtest;
361 dbtest.test_db_after__app_uninstall(app_id, pkg_id, is_pkg_removed);
364 void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
365 const privileges_t &privileges, const bool is_pkg_removed)
367 TestSecurityManagerDatabase dbtest;
368 dbtest.test_db_after__app_uninstall(app_id, pkg_id, is_pkg_removed);
371 /*Privileges should not be granted anymore to any user*/
372 check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, SM_NO_PRIVILEGES, privileges);
375 std::string access_opposite(std::string &access) {
376 static const std::map<char, int> access_mapping = {{'r', 0}, {'w', 1}, {'x', 2}, {'a', 3},
378 //May write implies may lock
379 if (access.find('w') != std::string::npos && access.find('l') == std::string::npos) {
382 std::string access_opposite = "rwxatl";
383 for (char c : access) {
384 access_opposite[access_mapping.at(c)] = '-';
386 auto it = std::remove_if(access_opposite.begin(), access_opposite.end(), [](char c) {return c == '-';});
387 access_opposite.erase(it, access_opposite.end());
388 return access_opposite;
391 void check_exact_smack_accesses(const std::string &subject, const std::string &object,
392 const std::string &access) {
393 std::string access_str(access);
394 auto no_access = access_opposite(access_str);
395 for (char c : access_str) {
396 int ret = smack_have_access(subject.c_str(), object.c_str(), std::string(1, c).c_str());
397 RUNNER_ASSERT_MSG(ret >= 0, "smack_have_access failed: <" << subject << ">, <" << object
398 << ">, <" << c << "> errno=" << strerror(errno));
399 RUNNER_ASSERT_MSG(ret == 1, "Access " << c << " from " << subject << " to "
400 << object << " not given");
403 for (char c : no_access) {
404 int ret = smack_have_access(subject.c_str(), object.c_str(), std::string(1, c).c_str());
405 RUNNER_ASSERT_MSG(ret >= 0, "smack_have_access failed: <" << subject << ">, <" << object
406 << ">, <" << c << "> errno=" << strerror(errno));
407 RUNNER_ASSERT_MSG(ret == 0, "Access " << c << " from " << subject << " to "
408 << object << " unnecessarily given");
412 CapsSetsUniquePtr setCaps(const char *cap_string)
414 CapsSetsUniquePtr caps(cap_init());
415 caps.reset(cap_from_text(cap_string));
416 RUNNER_ASSERT_MSG(caps, "can't convert capabilities from text");
417 int result = cap_set_proc(caps.get());
418 RUNNER_ASSERT_MSG(result == 0, "can't set capabilities. Result: " << result);
422 static void prepare_app_path(int app_num, bool others_enabled = false)
424 std::string SM_RW_PATH = genRWPath(app_num);
425 std::string SM_RO_PATH = genROPath(app_num);
426 std::string SM_PUBLIC_RO_PATH = genPublicROPath(app_num);
429 result = nftw(SM_RW_PATH.c_str(), &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
430 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_RW_PATH);
432 result = nftw(SM_RO_PATH.c_str(), &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
433 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_RO_PATH);
435 result = nftw(SM_PUBLIC_RO_PATH.c_str(), &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
436 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
439 std::string SM_OWNER_RW_OTHERS_RO_PATH = genOwnerRWOthersROPath(app_num);
440 result = nftw(SM_OWNER_RW_OTHERS_RO_PATH.c_str(), &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
441 RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_OWNER_RW_OTHERS_RO_PATH);
444 result = nftw(SM_DENIED_PATH.c_str(), &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
445 RUNNER_ASSERT_MSG(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
448 void prepare_app_env(int app_num, bool others_enabled)
450 prepare_app_path(app_num, others_enabled);
453 void install_app(const char *app_id, const char *pkg_id, uid_t uid, app_install_type type,
456 InstallRequest request;
457 request.setAppId(app_id);
458 request.setPkgId(pkg_id);
460 if (type != SM_APP_INSTALL_NONE)
461 request.setInstallType(type);
462 Api::install(request);
465 check_app_after_install(app_id, pkg_id);
468 void uninstall_app(const char *app_id, const char *pkg_id, bool expect_pkg_removed,
469 app_install_type type, bool check_after)
471 InstallRequest request;
472 request.setAppId(app_id);
473 if (type != SM_APP_INSTALL_NONE)
474 request.setInstallType(type);
475 Api::uninstall(request);
478 check_app_after_uninstall(app_id, pkg_id, expect_pkg_removed);
481 static const std::string EXEC_FILE("exec");
482 static const std::string NORMAL_FILE("normal");
483 static const std::string LINK_PREFIX("link_to_");
485 static void createTestDir(const std::string &dir)
487 mode_t dirMode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
488 mode_t execFileMode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
489 mode_t normalFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
491 mktreeSafe(dir, dirMode);
492 creatSafe(dir + "/" + EXEC_FILE, execFileMode);
493 creatSafe(dir + "/" + NORMAL_FILE, normalFileMode);
494 symlinkSafe(dir + "/" + EXEC_FILE, dir + "/" + LINK_PREFIX + EXEC_FILE);
495 symlinkSafe(dir + "/" + NORMAL_FILE, dir + "/" + LINK_PREFIX + NORMAL_FILE);
498 static void createInnerAppDir(const std::string &dir, const std::string &nonAppDir)
502 symlinkSafe(nonAppDir, dir + "/" + LINK_PREFIX + "non_app_dir");
503 symlinkSafe(nonAppDir + "/" + EXEC_FILE,
504 dir + "/" + LINK_PREFIX + "non_app_" + EXEC_FILE);
505 symlinkSafe(nonAppDir + "/" + NORMAL_FILE,
506 dir + "/" + LINK_PREFIX + "non_app_" + NORMAL_FILE);
509 static const std::string nonAppDirPath(const TemporaryTestUser &user)
511 return TMP_DIR + "/" + user.getUserName();
514 static void generateAppDir(const TemporaryTestUser &user,
515 const std::string &appId, const std::string &pkgId)
517 const std::string dir = TzPlatformConfig::appDirPath(user, appId, pkgId);
518 const std::string nonAppDir = nonAppDirPath(user);
520 createInnerAppDir(dir, nonAppDir);
521 createInnerAppDir(dir + "/.inner_dir", nonAppDir);
522 createInnerAppDir(dir + "/inner_dir", nonAppDir);
525 static void generateNonAppDir(const TemporaryTestUser &user)
527 const std::string dir = nonAppDirPath(user);
530 createTestDir(dir + "/.inner_dir");
531 createTestDir(dir + "/inner_dir");
534 void createTestDirs(const TemporaryTestUser &user,
535 const std::string &appId, const std::string &pkgId)
537 generateAppDir(user, appId, pkgId);
538 generateNonAppDir(user);
541 void removeTestDirs(const TemporaryTestUser &user,
542 const std::string &appId, const std::string &pkgId)
544 removeDir(TzPlatformConfig::appDirPath(user, appId, pkgId));
545 removeDir(nonAppDirPath(user));
548 pid_t runInChild(const std::function<void(void)> &process) {
550 RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
559 void runInChildParentWait(const std::function<void(void)> &process) {
560 SynchronizationPipe pipe;
562 RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
572 pipe.claimParentEp();