Fetch process label from service 14/88314/4
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 6 Sep 2016 15:01:17 +0000 (17:01 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 21 Sep 2016 10:55:19 +0000 (12:55 +0200)
Change-Id: I961de3bc1aff1a98f9062c881ca75f858319551f

src/client/client-common.cpp
src/client/client-label-monitor.cpp
src/client/client-security-manager.cpp
src/client/include/client-common.h
src/common/include/protocols.h
src/common/include/service_impl.h
src/common/service_impl.cpp
src/server/service/include/service.h
src/server/service/service.cpp

index 72fb94d949c1ca9702ce9ce9646cc261b59ba140..218369359514061509461a534964dc59e4ef6852 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Rafal Krypa <r.krypa@samsung.com>
  *
@@ -37,8 +37,8 @@
 #include <dpl/singleton.h>
 #include <dpl/singleton_safe_impl.h>
 
+#include <connection.h>
 #include <message-buffer.h>
-
 #include <protocols.h>
 
 IMPLEMENT_SAFE_SINGLETON(SecurityManager::Log::LogSystem);
@@ -67,6 +67,28 @@ int try_catch(const std::function<int()>& func)
     return SECURITY_MANAGER_ERROR_UNKNOWN;
 }
 
+int fetchLabelForProcess(const std::string &appName, std::string &label)
+{
+    using namespace SecurityManager;
+
+    MessageBuffer send, recv;
+    Serialization::Serialize(send, (int) SecurityModuleCall::LABEL_FOR_PROCESS, appName);
+    int retval = sendToServer(SERVICE_SOCKET, send.Pop(), recv);
+    if (retval != SECURITY_MANAGER_SUCCESS) {
+        LogError("Error in sendToServer. Error code: " << retval);
+        return retval;
+    }
+
+    Deserialization::Deserialize(recv, retval);
+    if (retval != SECURITY_MANAGER_SUCCESS) {
+        LogError("Couldn't get label for process: " << retval);
+        return retval;
+    }
+    Deserialization::Deserialize(recv, label);
+    return SECURITY_MANAGER_SUCCESS;
+}
+
+
 } // namespace SecurityMANAGER
 
 static void init_lib(void) __attribute__ ((constructor));
index b52f655af1864eaf0a5a420c3523aba568862447..b5bb2c80b8de7f10d5334bfaf999085a9f81f8d3 100644 (file)
@@ -69,9 +69,17 @@ static lib_retcode apply_relabel_list(const std::string &global_label_file,
         PermissibleSet::readNamesFromPermissibleFile(global_label_file, names);
         PermissibleSet::readNamesFromPermissibleFile(user_label_file, names);
         std::vector<const char*> temp;
-        std::transform(names.begin(), names.end(), std::back_inserter(temp),
-                [] (std::string &label) {label = SmackLabels::generateProcessLabel(label);
-                    return label.c_str();});
+        // FIXME : monitor should store labels instead of app ids
+        for (auto &name : names) {
+            std::string label;
+            int ret = SecurityManager::fetchLabelForProcess(name, label);
+            if (ret != SECURITY_MANAGER_SUCCESS) {
+                LogError("Couldn't fetch label for process");
+                return static_cast<lib_retcode>(ret);
+            }
+            name = label;
+            temp.push_back(name.c_str());
+        }
         if (smack_set_relabel_self(const_cast<const char **>(temp.data()), temp.size()) != 0) {
             LogError("smack_set_relabel_self failed");
             return SECURITY_MANAGER_ERROR_SET_RELABEL_SELF_FAILED;
index 6cf0c351c4d09a2965a370f555cc88d3e6322da0..5f30c20344c2b7fe56c9b4343de77dc323cc97fb 100644 (file)
@@ -387,7 +387,9 @@ int security_manager_set_process_label_from_appid(const char *app_name)
         return SECURITY_MANAGER_SUCCESS;
 
     try {
-        appLabel = SecurityManager::SmackLabels::generateProcessLabel(app_name);
+        ret = fetchLabelForProcess(app_name, appLabel);
+        if (ret != SECURITY_MANAGER_SUCCESS)
+            return ret;
     } catch (...) {
         LogError("Failed to generate smack label for appName: " << app_name);
         return SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT;
@@ -544,7 +546,9 @@ static inline int security_manager_sync_threads_internal(const char *app_name)
     uid_t cur_tid = gettid();
     pid_t cur_pid = getpid();
 
-    g_app_label = SecurityManager::SmackLabels::generateProcessLabel(app_name);
+    int ret = fetchLabelForProcess(app_name, g_app_label);
+    if (ret != SECURITY_MANAGER_SUCCESS)
+        return ret;
     g_threads_count = 0;
     g_tid_attr_current_map.clear();
     g_smack_fs_path = smack_smackfs_path() != NULL;
index b82f6a53953a54813a01687e5869dafebd66e206..2d776494ee9c9d326b3d1748268b83cc55b46e39 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Rafal Krypa <r.krypa@samsung.com>
  *
@@ -39,6 +39,11 @@ namespace SecurityManager {
  */
 int try_catch(const std::function<int()>& func);
 
+/*
+ * Fetching label for application process.
+ */
+int fetchLabelForProcess(const std::string &appName, std::string &label);
+
 } // namespace SecurityManager
 
 #endif // _SECURITY_MANAGER_CLIENT_
index a8eb4afbcabbf2b047766533b01214882ae45126..0e2ab97379587b8c1b2cd81e7fabd79cfad34eab 100644 (file)
@@ -87,6 +87,7 @@ enum class SecurityModuleCall
     APP_HAS_PRIVILEGE,
     PATHS_REGISTER,
     GROUPS_FOR_UID,
+    LABEL_FOR_PROCESS,
     NOOP = 0x90,
 };
 
index 658c60b122d8a7bd451e2047b07eb4c9d032a3ac..efabf9f99acb5a1490fec049df75498b761a34c2 100644 (file)
@@ -274,6 +274,16 @@ public:
      * @return API return code, as defined in protocols.h
      */
     int pathsRegister(const Credentials &creds, path_req p_req);
+
+    /**
+     * Generate label for process.
+     *
+     * @param[in] appName application identifier
+     * @param[out] label generated label
+     *
+     * @return API return code, as defined in protocols.h
+     */
+    int labelForProcess(const std::string &appName, std::string &label);
 };
 
 } /* namespace SecurityManager */
index 188cde504f2523ba001d9ef3f2a6db6e0c00ae92..ef6cb038fd752df8d7604ed971f9a1f9ca9408f8 100644 (file)
@@ -1600,4 +1600,13 @@ int ServiceImpl::pathsRegister(const Credentials &creds, path_req req)
                       req.uid);
 }
 
+int ServiceImpl::labelForProcess(const std::string &appName, std::string &label)
+{
+    LogDebug("Requested label generation for process of application " << appName);
+
+    label = SmackLabels::generateProcessLabel(appName);
+
+    return SECURITY_MANAGER_SUCCESS;
+}
+
 } /* namespace SecurityManager */
index 8330f5b58be916cfcf65ba5a1f70e12c64fe8553..1c91c9233c9e37fbb8f8ba6ae12b2145ef19d252 100644 (file)
@@ -187,6 +187,14 @@ private:
      * @param  creds  credentials of the requesting process
      */
     void processPathsRegister(MessageBuffer &recv, MessageBuffer &send, const Credentials &creds);
+
+    /**
+     * Generate process label request
+     *
+     * @param  recv   Raw received data buffer
+     * @param  send   Raw data buffer to be sent
+     */
+    void processLabelForProcess(MessageBuffer &buffer, MessageBuffer &send);
 };
 
 } // namespace SecurityManager
index 5e57f7a011cf6848cb396abdf8983356c09bf150..ef85d8e204359c654c2e02d0884c9d180b281d0b 100644 (file)
@@ -144,6 +144,9 @@ bool Service::processOne(const ConnectionID &conn, MessageBuffer &buffer,
                 case SecurityModuleCall::PATHS_REGISTER:
                     processPathsRegister(buffer, send, creds);
                     break;
+                case SecurityModuleCall::LABEL_FOR_PROCESS:
+                    processLabelForProcess(buffer, send);
+                    break;
                 default:
                     LogError("Invalid call: " << call_type_int);
                     Throw(ServiceException::InvalidAction);
@@ -392,4 +395,15 @@ void Service::processPathsRegister(MessageBuffer &recv, MessageBuffer &send, con
     int ret = serviceImpl.pathsRegister(creds, std::move(req));
     Serialization::Serialize(send, ret);
 }
+
+void Service::processLabelForProcess(MessageBuffer &buffer, MessageBuffer &send)
+{
+    std::string appName;
+    Deserialization::Deserialize(buffer, appName);
+    std::string label;
+    int ret = serviceImpl.labelForProcess(appName, label);
+    Serialization::Serialize(send, ret);
+    if (ret == SECURITY_MANAGER_SUCCESS)
+        Serialization::Serialize(send, label);
+}
 } // namespace SecurityManager