7b68a45aad0f34cd7ed562f0812eb1faac6a7272
[platform/core/security/security-manager.git] / src / common / smack-labels.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /**
19  * @file        smack-labels.cpp
20  * @author      Jan Cybulski <j.cybulski@samsung.com>
21  * @author      Rafal Krypa <r.krypa@samsung.com>
22  * @version     1.0
23  * @brief       Implementation of functions managing smack labels
24  *
25  */
26
27 #include <sys/stat.h>
28 #include <sys/smack.h>
29 #include <sys/xattr.h>
30 #include <linux/xattr.h>
31 #include <memory>
32 #include <fts.h>
33 #include <cstring>
34 #include <string>
35
36 #include <dpl/log/log.h>
37
38 #include "security-manager.h"
39 #include "smack-labels.h"
40
41 namespace SecurityManager {
42
43 /* Const defined below is used to label links to executables */
44 const char *const LABEL_FOR_PUBLIC_APP_PATH = "User";
45
46 enum class FileDecision {
47     SKIP = 0,
48     LABEL = 1,
49     ERROR = -1
50 };
51
52 typedef std::function<FileDecision(const FTSENT*)> LabelDecisionFn;
53
54 static FileDecision labelAll(const FTSENT *ftsent __attribute__((unused)))
55 {
56     return FileDecision::LABEL;
57 }
58
59 static FileDecision labelDirs(const FTSENT *ftsent)
60 {
61     // label only directories
62     if (S_ISDIR(ftsent->fts_statp->st_mode))
63         return FileDecision::LABEL;
64     return FileDecision::SKIP;
65 }
66
67 static FileDecision labelExecs(const FTSENT *ftsent)
68 {
69     // LogDebug("Mode = " << ftsent->fts_statp->st_mode); // this could be helpfull in debugging
70     // label only regular executable files
71     if (S_ISREG(ftsent->fts_statp->st_mode) && (ftsent->fts_statp->st_mode & S_IXUSR))
72         return FileDecision::LABEL;
73     return FileDecision::SKIP;
74 }
75
76 static bool dirSetSmack(const std::string &path, const std::string &label,
77         const char *xattr_name, LabelDecisionFn fn)
78 {
79     char *const path_argv[] = {const_cast<char *>(path.c_str()), NULL};
80     FTSENT *ftsent;
81     FileDecision ret;
82
83     std::unique_ptr<FTS, std::function<void(FTS*)> > fts(
84             fts_open(path_argv, FTS_PHYSICAL | FTS_NOCHDIR, NULL),
85             fts_close);
86
87     if (fts.get() == NULL) {
88         LogError("fts_open failed.");
89         return false;
90     }
91
92     while ((ftsent = fts_read(fts.get())) != NULL) {
93         /* Check for error (FTS_ERR) or failed stat(2) (FTS_NS) */
94         if (ftsent->fts_info == FTS_ERR || ftsent->fts_info == FTS_NS) {
95             LogError("FTS_ERR error or failed stat(2) (FTS_NS)");
96             return false;
97         }
98
99         /* avoid to tag directories two times */
100         if (ftsent->fts_info == FTS_D)
101             continue;
102
103         ret = fn(ftsent);
104         if (ret == FileDecision::ERROR) {
105             LogError("fn(ftsent) failed.");
106             return false;
107         }
108
109         if (ret == FileDecision::LABEL) {
110             if (lsetxattr(ftsent->fts_path, xattr_name, label.c_str(), label.length(), 0) != 0) {
111                 LogError("lsetxattr failed.");
112                 return false;
113             }
114         }
115
116     }
117
118     /* If last call to fts_read() set errno, we need to return error. */
119     if ((errno != 0) && (ftsent == NULL)) {
120         LogError("Last errno from fts_read: " << strerror(errno));
121         return false;
122     }
123     return true;
124 }
125
126
127 static bool labelDir(const std::string &path, const std::string &label,
128         bool set_transmutable, bool set_executables)
129 {
130     bool ret = true;
131
132     // setting access label on everything in given directory and below
133     ret = dirSetSmack(path, label, XATTR_NAME_SMACK, labelAll);
134     if (!ret) {
135         LogError("dirSetSmack failed (access label)");
136         return ret;
137     }
138
139     if (set_transmutable) {
140         // setting transmute on dirs
141         ret = dirSetSmack(path, "TRUE", XATTR_NAME_SMACKTRANSMUTE, labelDirs);
142         if (!ret) {
143             LogError("dirSetSmack failed (transmute)");
144             return ret;
145         }
146     }
147
148     if (set_executables) {
149         ret = dirSetSmack(path, label, XATTR_NAME_SMACKEXEC, &labelExecs);
150         if (!ret)
151         {
152             LogError("dirSetSmack failed (execs).");
153             return ret;
154         }
155     }
156
157     return ret;
158 }
159
160 bool setupPath(const std::string &pkgId, const std::string &path,
161     app_install_path_type pathType)
162 {
163     std::string label;
164     bool label_executables, label_transmute;
165
166     switch (pathType) {
167     case SECURITY_MANAGER_PATH_PRIVATE:
168         if (!generatePkgLabel(pkgId, label))
169             return false;
170         label_executables = true;
171         label_transmute = false;
172         break;
173     case SECURITY_MANAGER_PATH_PUBLIC:
174         label.assign(LABEL_FOR_PUBLIC_APP_PATH);
175         label_executables = false;
176         label_transmute = true;
177         break;
178     case SECURITY_MANAGER_PATH_PUBLIC_RO:
179         label.assign("_");
180         label_executables = false;
181         label_transmute = false;
182         break;
183     default:
184         LogError("Path type not known.");
185         return false;
186     }
187     return labelDir(path, label, label_transmute, label_executables);
188 }
189
190 bool generateAppLabel(const std::string &appId, std::string &label)
191 {
192     (void) appId;
193     label = "User";
194     return true;
195 }
196
197 bool generatePkgLabel(const std::string &pkgId, std::string &label)
198 {
199     (void) pkgId;
200     label = "User";
201     return (true);
202 }
203
204 } // namespace SecurityManager