Use smack_check() helper function instead of manually calling libsmack 13/90513/2
authorRafal Krypa <r.krypa@samsung.com>
Fri, 30 Sep 2016 09:36:27 +0000 (11:36 +0200)
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>
Wed, 12 Oct 2016 09:29:40 +0000 (02:29 -0700)
The smack_check() helper provides functionality for checking whether Smack
is available on the platform. It properly wraps libsmack check function and
remembers the result in static variable.
Use it where applicable, replacing custom checks.

Change-Id: Ie8ee27c700831c4fea8a8d837271f2604ca0b588

src/client/client-security-manager.cpp
src/common/smack-rules.cpp

index f7925ea196c70768f6c7b982fb1f7c86170036b7..322c6941ac20db288d0f9ddea8cb9b77fbb2c2ed 100644 (file)
@@ -48,6 +48,7 @@
 
 #include <dpl/log/log.h>
 #include <dpl/exception.h>
+#include <smack-check.h>
 #include <smack-labels.h>
 #include <message-buffer.h>
 #include <client-common.h>
@@ -83,7 +84,7 @@ static std::map<enum lib_retcode, std::string> lib_retcode_string_map = {
 static std::string g_app_label;
 static std::atomic<int> g_threads_count;
 static std::map<uid_t, std::string> g_tid_attr_current_map;
-static bool g_smack_fs_path;
+static bool g_smack_present;
 static cap_t g_cap;
 #define MAX_SIG_WAIT_TIME   1000
 
@@ -404,7 +405,7 @@ int security_manager_set_process_label_from_appid(const char *app_name)
 
     LogDebug("security_manager_set_process_label_from_appid() called");
 
-    if (smack_smackfs_path() == NULL)
+    if (!smack_check())
         return SECURITY_MANAGER_SUCCESS;
 
     try {
@@ -572,7 +573,7 @@ static inline int security_manager_sync_threads_internal(const char *app_name)
         return ret;
     g_threads_count = 0;
     g_tid_attr_current_map.clear();
-    g_smack_fs_path = smack_smackfs_path() != NULL;
+    g_smack_present = smack_check();
     g_cap = cap_init();
 
     if (!g_cap) {
@@ -598,7 +599,7 @@ static inline int security_manager_sync_threads_internal(const char *app_name)
 
         std::atomic_thread_fence(std::memory_order_acquire);
 
-        if (g_smack_fs_path)
+        if (g_smack_present)
             if(label_for_self_internal() != 0)
                 return;
 
@@ -651,12 +652,11 @@ static inline int security_manager_sync_threads_internal(const char *app_name)
         return SECURITY_MANAGER_ERROR_UNKNOWN;
     }
 
-    if (g_smack_fs_path)
-        if (smack_set_label_for_self(g_app_label.c_str()) != 0) {
-            LogError("smack_set_label_for_self failed");
-            cap_free(g_cap);
-            return SECURITY_MANAGER_ERROR_UNKNOWN;
-        }
+    if (g_smack_present && smack_set_label_for_self(g_app_label.c_str()) != 0) {
+        LogError("smack_set_label_for_self failed");
+        cap_free(g_cap);
+        return SECURITY_MANAGER_ERROR_UNKNOWN;
+    }
 
     if (cap_set_proc(g_cap)) {
         LogError("Can't drop main thread capabilities");
index 797677c9db6bff6bca6c17ebfaffe7fc81a9fb96..762b4222a3c56029266c9f90239067107e714f56 100644 (file)
@@ -42,6 +42,7 @@
 #include "smack-labels.h"
 #include "tzplatform-config.h"
 
+#include "smack-check.h"
 #include "smack-rules.h"
 
 namespace SecurityManager {
@@ -278,7 +279,7 @@ void SmackRules::generateSharedRORules(PkgsLabels &pkgsLabels, std::vector<PkgIn
         }
     }
 
-    if (smack_smackfs_path() != NULL)
+    if (smack_check())
         rules.apply();
 
     rules.saveToFile(SMACK_RULES_SHARED_RO_PATH);
@@ -288,7 +289,7 @@ void SmackRules::revokeSharedRORules(PkgsLabels &pkgsLabels, const std::string &
 {
     LogDebug("Revoking SharedRO rules for target pkg " << revokePkg);
 
-    if (smack_smackfs_path() == NULL)
+    if (!smack_check())
         return;
 
     SmackRules rules;
@@ -402,7 +403,7 @@ void SmackRules::useTemplate(
     SmackRules smackRules;
     smackRules.addFromTemplateFile(templatePath, appProcessLabel, pkgName, authorId);
 
-    if (smack_smackfs_path() != NULL)
+    if (smack_check())
         smackRules.apply();
 
     smackRules.saveToFile(outputPath);
@@ -438,7 +439,7 @@ void SmackRules::updatePackageRules(
 
     smackRules.generatePackageCrossDeps(pkgLabels);
 
-    if (smack_smackfs_path() != NULL)
+    if (smack_check())
         smackRules.apply();
 
     smackRules.saveToFile(getPackageRulesFilePath(pkgName));
@@ -477,7 +478,7 @@ void SmackRules::uninstallRules(const std::string &path)
     try {
         SmackRules rules;
         rules.loadFromFile(path);
-        if (smack_smackfs_path())
+        if (smack_check())
             rules.clear();
     } catch (const SmackException::Base &e) {
         LogWarning("Failed to clear smack kernel rules from file: " << path);