Fix smack_check() function
[platform/core/test/security-tests.git] / src / common / tests_common.cpp
1 /*
2  * Copyright (c) 2013-2015 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 /*
18  * @file        tests_common.cpp
19  * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
20  * @version     1.0
21  * @brief       Common functions and macros used in security-tests package.
22  */
23
24 #include "tests_common.h"
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <grp.h>
30 #include <errno.h>
31 #include <vector>
32 #include <algorithm>
33
34 int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
35
36 const char *WGT_APP_ID = "QwCqJ0ttyS";
37
38 bool smack_check(void)
39 {
40 #ifndef WRT_SMACK_ENABLED
41     return false;
42 #else
43     static int smack_present = -1;
44     if (-1 == smack_present)
45         smack_present = smack_smackfs_path() == nullptr ? 0 : 1;
46     return smack_present == 1;
47 #endif
48 }
49
50 /**
51  * Dropping root privileges
52  * returns 0 on success, 1 on error
53  */
54 int drop_root_privileges(uid_t appUid, gid_t appGid)
55 {
56     if (getuid() == 0) {
57         /* process is running as root, drop privileges */
58         if (setgid(appGid) != 0)
59             return 1;
60         if (setuid(appUid) != 0)
61             return 1;
62     }
63     uid_t uid = getuid();
64     if (uid == appUid)
65         return 0;
66
67     return 1;
68 }
69
70 void setLabelForSelf(const int line, const char *label)
71 {
72     int ret = smack_set_label_for_self(label);
73     RUNNER_ASSERT_MSG(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
74 }
75
76 /*
77  * Add a new group to the current process groups.
78  */
79 void add_process_group(const char* group_name)
80 {
81     // get group ID by group name
82     group *gr = getgrnam(group_name);
83     RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
84     const gid_t new_group_id = gr->gr_gid;
85
86     // get number of groups that the current process belongs to
87     int ngroups = getgroups(0, nullptr);
88
89     //allocate groups table + space for new group entry
90     std::vector<gid_t> groups(ngroups + 1);
91     getgroups(ngroups, groups.data());
92
93     // check if the process already belongs to the group
94     if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
95
96     // add new group & apply change
97     groups[ngroups] = new_group_id;
98     int ret = setgroups(groups.size(), groups.data());
99     RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
100 }
101
102 /*
103  * Remove specific group from the current process groups.
104  */
105 void remove_process_group(const char* group_name)
106 {
107     // get group ID by group name
108     group *gr = getgrnam(group_name);
109     RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
110     const gid_t new_group_id = gr->gr_gid;
111
112     int ngroups = getgroups(0, nullptr);
113     std::vector<gid_t> groups(ngroups);
114     getgroups(ngroups, groups.data());
115
116     // remove group from the list
117     groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
118
119     if (groups.size() != (size_t)ngroups) {
120         // apply change
121         int ret = setgroups(groups.size(), groups.data());
122         RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
123     }
124 }
125
126 std::string formatCstr(const char *cstr)
127 {
128     if (!cstr)
129         return std::string("nullptr");
130     return std::string("\"") + cstr + "\"";
131 }
132
133 int files_compare(int fd1, int fd2)
134 {
135     //for getting files sizes
136     struct stat fs1, fs2;
137
138     //handlers for mmap()
139     void *h1 = MAP_FAILED;
140     void *h2 = MAP_FAILED;
141
142     //getting files information
143     RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
144     RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
145
146     if (fs1.st_size < fs2.st_size) {
147         return -1;
148     }
149
150     if (fs1.st_size > fs2.st_size) {
151         return 1;
152     }
153
154     //since Linux 2.6.12, mmap returns EINVAL if length is 0
155     //if both lengths are 0, files are actually the same
156     if (0 == fs1.st_size && 0 == fs2.st_size) {
157         return 0;
158     }
159
160     //mapping files to process memory
161     RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
162                                "mmap failed for fd=" << fd1);
163
164     if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
165         munmap(h1, fs1.st_size);
166         RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
167                                             << ". " << strerror(errno));
168     }
169
170     int result = memcmp(h1, h2, fs1.st_size);
171     munmap(h1, fs1.st_size);
172     munmap(h2, fs2.st_size);
173
174     return result;
175 }