1 #include <fs_label_manager.h>
3 #include <tests_common.h>
11 #include <sys/mount.h>
12 #include <sys/xattr.h>
13 #include <linux/xattr.h>
18 static const char* get_xattr_name(enum smack_label_type type)
21 case SMACK_LABEL_ACCESS:
22 return XATTR_NAME_SMACK;
23 case SMACK_LABEL_EXEC:
24 return XATTR_NAME_SMACKEXEC;
25 case SMACK_LABEL_MMAP:
26 return XATTR_NAME_SMACKMMAP;
27 case SMACK_LABEL_TRANSMUTE:
28 return XATTR_NAME_SMACKTRANSMUTE;
29 case SMACK_LABEL_IPIN:
30 return XATTR_NAME_SMACKIPIN;
31 case SMACK_LABEL_IPOUT:
32 return XATTR_NAME_SMACKIPOUT;
34 /* Should not reach this point */
40 FsLabelManager::FsLabelManager(const std::string &path, const std::string &label)
44 umount(m_path.c_str());
45 rmdir(m_path.c_str());
47 std::string data = std::string("mode=0777,uid=0,smackfsdef=") + label;
49 int ret = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
50 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "Unable to make directory");
52 ret = mount("none", path.c_str(), "tmpfs", 0, data.c_str());
53 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "Unable to mount filesystem");
55 if (m_path[m_path.length()-1] != '/')
59 FsLabelManager::~FsLabelManager()
61 umount(m_path.c_str());
62 rmdir(m_path.c_str());
65 void FsLabelManager::createFile(const std::string &relativePath)
67 std::string path = m_path + relativePath;
69 mode_t systemMask = umask(0000);
70 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
72 RUNNER_ASSERT_ERRNO_MSG(fd > -1, "Unable to create file for tests");
76 int ret = chown(path.c_str(), APP_UID, APP_GID);
77 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "Unable to change file owner");
80 void FsLabelManager::createLink(const std::string &relativeLinkPath, const std::string &relativeRealPath)
82 std::string linkPath = m_path + relativeLinkPath;
83 std::string realPath = m_path + relativeRealPath;
85 int ret = unlink(linkPath.c_str());
86 RUNNER_ASSERT_ERRNO_MSG(ret == 0 || errno == ENOENT, "Unable to unlink file");
88 ret = symlink(realPath.c_str(), linkPath.c_str());
89 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "Unable to create symlink");
91 ret = lchown(linkPath.c_str(), APP_UID, APP_GID);
92 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "Unable to change file owner");
95 void FsLabelManager::testSmackSetLabel(const std::string &relativePath,
97 enum smack_label_type labelType)
99 std::string path = m_path + relativePath;
101 int ret = smack_setlabel(path.c_str(), label, labelType);
102 RUNNER_ASSERT_MSG(ret == 0, "Error in normal setting label " << label);
104 checkLabel(path, label, labelType);
107 void FsLabelManager::testSmackLSetLabel(const std::string &relativePath,
109 enum smack_label_type labelType)
111 std::string path = m_path + relativePath;
113 int ret = smack_lsetlabel(path.c_str(), label, labelType);
114 RUNNER_ASSERT_MSG(ret == 0, "Error in link setting label " << label);
116 checkLinkLabel(path, label, labelType);
119 void FsLabelManager::testSmackFSetLabel(const std::string &relativePath,
121 enum smack_label_type labelType)
123 std::string path = m_path + relativePath;
125 int fd = open(path.c_str(), O_WRONLY);
126 RUNNER_ASSERT_ERRNO_MSG(fd > -1, "Unable to open file");
128 int ret = smack_fsetlabel(fd, label, labelType);
130 RUNNER_ASSERT_MSG(ret == 0, "Error in fd setting " << label);
132 checkLabel(path, label, labelType);
135 void FsLabelManager::testSmackGetLabel(const std::string &relativePath,
137 enum smack_label_type labelType)
139 std::string path = m_path + relativePath;
142 int ret = smack_getlabel(path.c_str(), &tmpLabel, labelType);
143 RUNNER_ASSERT_MSG(ret == 0, "Error in normal getting label");
144 SmackLabelPtr labelPtr(tmpLabel);
146 if (label == nullptr && !m_label.compare(tmpLabel))
148 RUNNER_ASSERT_MSG(label != nullptr, "Path should be related with file system default label. "
149 << tmpLabel << " != " << m_label);
151 ret = strcmp(tmpLabel, label);
152 RUNNER_ASSERT_MSG(ret == 0, "Wrong label. " << tmpLabel << " != " << label);
154 checkLabel(path, tmpLabel, labelType);
157 void FsLabelManager::testSmackLGetLabel(const std::string &relativePath,
159 enum smack_label_type labelType)
161 std::string path = m_path + relativePath;
164 int ret = smack_lgetlabel(path.c_str(), &tmpLabel, labelType);
165 RUNNER_ASSERT_MSG(ret == 0, "Error in link getting label");
166 SmackLabelPtr labelPtr(tmpLabel);
168 if (label == nullptr && !m_label.compare(tmpLabel))
170 RUNNER_ASSERT_MSG(label != nullptr, "Path should be related with file system default label. "
171 << tmpLabel << " != " << m_label);
173 ret = strcmp(tmpLabel, label);
174 RUNNER_ASSERT_MSG(ret == 0, "Wrong label. " << tmpLabel << " != " << label);
176 checkLinkLabel(path, tmpLabel, labelType);
179 void FsLabelManager::testSmackFGetLabel(const std::string &relativePath,
181 enum smack_label_type labelType)
183 std::string path = m_path + relativePath;
184 int fd = open(path.c_str(), O_WRONLY);
185 RUNNER_ASSERT_ERRNO_MSG(fd > -1, "Unable to open file");
188 int ret = smack_fgetlabel(fd, &tmpLabel, labelType);
190 RUNNER_ASSERT_MSG(ret == 0, "Error in fd getting label");
191 SmackLabelPtr labelPtr(tmpLabel);
193 if (label == nullptr && !m_label.compare(tmpLabel))
195 RUNNER_ASSERT_MSG(label != nullptr, "Fd should be related with file system default label. "
196 << tmpLabel << " != " << m_label);
198 ret = strcmp(tmpLabel, label);
199 RUNNER_ASSERT_MSG(ret == 0, "Wrong label. " << tmpLabel << " != " << label);
201 checkLabel(path, tmpLabel, labelType);
204 void FsLabelManager::testSmackClearLabels(const std::string &relativePath)
206 testSmackSetLabel(relativePath, nullptr, SMACK_LABEL_ACCESS);
207 testSmackGetLabel(relativePath, nullptr, SMACK_LABEL_ACCESS);
208 testSmackSetLabel(relativePath, nullptr, SMACK_LABEL_EXEC);
209 testSmackGetLabel(relativePath, nullptr, SMACK_LABEL_EXEC);
212 void FsLabelManager::checkLabel(const std::string &path,
214 enum smack_label_type labelType)
216 char buf[SMACK_LABEL_LEN+2] = { 0, };
217 int ret = getxattr(path.c_str(), get_xattr_name(labelType), buf, SMACK_LABEL_LEN+1);
218 RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Error in getting xattr");
220 const char *tmpLabel;
221 if (label == nullptr)
222 tmpLabel = m_label.c_str();
226 ret = strncmp(tmpLabel, buf, SMACK_LABEL_LEN+1);
227 RUNNER_ASSERT_MSG(ret == 0, "Wrong label. " << tmpLabel << " != " << buf);
230 void FsLabelManager::checkLinkLabel(const std::string &path,
232 enum smack_label_type labelType)
234 char buf[SMACK_LABEL_LEN+2] = { 0, };
235 int ret = lgetxattr(path.c_str(), get_xattr_name(labelType), buf, SMACK_LABEL_LEN+1);
236 RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Error in getting xattr");
238 const char *tmpLabel;
239 if (label == nullptr)
240 tmpLabel = m_label.c_str();
244 ret = strncmp(tmpLabel, buf, SMACK_LABEL_LEN+1);
245 RUNNER_ASSERT_MSG(ret == 0, "Wrong label. " << tmpLabel << " != " << buf);