2 * Copyright (c) 2013-2015 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.
18 * @file tests_common.cpp
19 * @author Lukasz Kostyra (l.kostyra@partner.samsung.com)
21 * @brief Common functions and macros used in security-tests package.
24 #include "tests_common.h"
28 #include <sys/types.h>
36 bool smack_check(void)
38 #ifndef WRT_SMACK_ENABLED
41 static int smack_present = -1;
42 if (-1 == smack_present)
43 smack_present = smack_smackfs_path() == nullptr ? 0 : 1;
44 return smack_present == 1;
49 * Dropping root privileges
50 * returns 0 on success, 1 on error
52 int drop_root_privileges(uid_t appUid, gid_t appGid)
55 /* process is running as root, drop privileges */
56 if (setgid(appGid) != 0)
58 if (setuid(appUid) != 0)
68 void setLabelForSelf(const int line, const char *label)
70 int ret = smack_set_label_for_self(label);
71 RUNNER_ASSERT_MSG(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
75 * Add a new group to the current process groups.
77 void add_process_group(const char* group_name)
79 // get group ID by group name
80 group *gr = getgrnam(group_name);
81 RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
82 const gid_t new_group_id = gr->gr_gid;
84 // get number of groups that the current process belongs to
85 int ngroups = getgroups(0, nullptr);
87 //allocate groups table + space for new group entry
88 std::vector<gid_t> groups(ngroups + 1);
89 getgroups(ngroups, groups.data());
91 // check if the process already belongs to the group
92 if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
94 // add new group & apply change
95 groups[ngroups] = new_group_id;
96 int ret = setgroups(groups.size(), groups.data());
97 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
101 * Remove specific group from the current process groups.
103 void remove_process_group(const char* group_name)
105 // get group ID by group name
106 group *gr = getgrnam(group_name);
107 RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
108 const gid_t new_group_id = gr->gr_gid;
110 int ngroups = getgroups(0, nullptr);
111 std::vector<gid_t> groups(ngroups);
112 getgroups(ngroups, groups.data());
114 // remove group from the list
115 groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
117 if (groups.size() != (size_t)ngroups) {
119 int ret = setgroups(groups.size(), groups.data());
120 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
124 std::string formatCstr(const char *cstr)
127 return std::string("nullptr");
128 return std::string("\"") + cstr + "\"";
131 int files_compare(int fd1, int fd2)
133 //for getting files sizes
134 struct stat fs1, fs2;
136 //handlers for mmap()
137 void *h1 = MAP_FAILED;
138 void *h2 = MAP_FAILED;
140 //getting files information
141 RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
142 RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
144 if (fs1.st_size < fs2.st_size) {
148 if (fs1.st_size > fs2.st_size) {
152 //since Linux 2.6.12, mmap returns EINVAL if length is 0
153 //if both lengths are 0, files are actually the same
154 if (0 == fs1.st_size && 0 == fs2.st_size) {
158 //mapping files to process memory
159 RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
160 "mmap failed for fd=" << fd1);
162 if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
163 munmap(h1, fs1.st_size);
164 RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
165 << ". " << strerror(errno));
168 int result = memcmp(h1, h2, fs1.st_size);
169 munmap(h1, fs1.st_size);
170 munmap(h2, fs2.st_size);
175 void mkdirSafe(const std::string &path, mode_t mode)
177 RUNNER_ASSERT_ERRNO_MSG(0 == mkdir(path.c_str(), mode) || errno == EEXIST,
178 "mkdir for <" << path << "> with mode <" << mode << "> failed");
181 void mktreeSafe(const std::string &path, mode_t mode)
183 // Create subsequent parent directories
184 // Assume that path is absolute - i.e. starts with '/'
185 for (size_t pos = 0; (pos = path.find("/", pos + 1)) != std::string::npos; )
186 mkdirSafe(path.substr(0, pos).c_str(), mode);
188 mkdirSafe(path, mode);
191 void creatSafe(const std::string &path, mode_t mode)
193 RUNNER_ASSERT_ERRNO_MSG(-1 != creat(path.c_str(), mode),
194 "creat for <" << path << "> with mode <" << mode << "> failed");
197 void symlinkSafe(const std::string &targetPath, const std::string &linkPath)
199 RUNNER_ASSERT_ERRNO_MSG(0 == symlink(targetPath.c_str(), linkPath.c_str()),
200 "symlink for <" << linkPath << "> to <" << targetPath << "> failed");
203 void removeDir(const std::string &path)
205 DIR *d = opendir(path.c_str());
208 RUNNER_ASSERT_ERRNO_MSG(errno == ENOENT, "opendir of <" << path << "> failed");
212 struct dirent *dirEntry;
213 while (nullptr != (dirEntry = readdir(d))) {
214 std::string entryName(dirEntry->d_name);
215 if (entryName == "." || entryName == "..")
218 std::string entryPath(path + "/" + entryName);
221 RUNNER_ASSERT_ERRNO_MSG(0 == lstat(entryPath.c_str(), &st),
222 "stat for <" << entryPath << "> failed");
223 if (S_ISDIR(st.st_mode))
224 removeDir(entryPath);
226 RUNNER_ASSERT_ERRNO_MSG(0 == unlink(entryPath.c_str()),
227 "unlink for <" << entryPath << "> failed");
232 RUNNER_ASSERT_ERRNO_MSG(0 == rmdir(path.c_str()), "rmdir for <" << path << "> failed");
235 void waitPid(pid_t pid)
238 pid_t ret = waitpid(pid, &status, 0);
239 RUNNER_ASSERT_MSG((ret != -1) && WIFEXITED(status) && WEXITSTATUS(status) == 0,
240 "Child process exited abnormally" <<
241 ": ret=" << ret << ", errno=" << errno << ", status=" << status);
243 // changes process label
244 void change_label(const char* label)
246 int ret = smack_set_label_for_self(label);
247 RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self("<<label<<"). Error: " << ret);