SM : AppInstallHelper, ScopedInstaller, TempTestuser fork safe 88/93088/6
authorZofia Abramowska <z.abramowska@samsung.com>
Thu, 13 Oct 2016 17:56:13 +0000 (19:56 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 29 Nov 2016 08:47:58 +0000 (09:47 +0100)
* Adjust simple test cases

Change-Id: Icc9fe4d34a32ac4c7399bdc062d42aa0f9662b52

src/common/temp_test_user.cpp
src/common/temp_test_user.h
src/security-manager-tests/common/app_install_helper.h
src/security-manager-tests/common/scoped_installer.h
src/security-manager-tests/test_cases.cpp
src/security-manager-tests/test_cases_public_sharing.cpp

index c754308..000410f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
@@ -77,7 +77,8 @@ void TemporaryTestUser::remove(void)
 
 TemporaryTestUser::~TemporaryTestUser()
 {
-    this->remove();
+    if (m_creatorPid == getpid())
+        remove();
 }
 
 TemporaryTestUser::GumdRunner TemporaryTestUser::m_runner = TemporaryTestUser::GumdRunner();
index fc45c34..a6ca03d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
 #include <sys/types.h>
 #include <gum-user.h>
 #include <common/gum-user-types.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 class TemporaryTestUser {
 public:
-    TemporaryTestUser() = delete;
     TemporaryTestUser(std::string userName, GumUserType userType, bool offline = false) :
             m_uid(0),
             m_gid(0),
             m_userName(userName),
             m_userType(userType),
-            m_offline(offline)
+            m_offline(offline),
+            m_creatorPid(getpid())
             {};
     ~TemporaryTestUser();
     void remove(void);
@@ -67,6 +69,7 @@ private:
     std::string m_userName;
     GumUserType m_userType;
     bool m_offline;
+    pid_t m_creatorPid;
 };
 
 #endif
index 1b6e690..04a1200 100644 (file)
 #include <functional>
 #include <map>
 #include <string>
+#include <sys/types.h>
 #include <vector>
 #include <unistd.h>
 
 #include <security-manager-types.h>
 
-const uid_t OWNER_UID = 5001;
-
 struct AppInstallHelper {
 
     using TypePathsMap = std::map<app_install_path_type, std::vector<std::string>>;
@@ -36,7 +35,7 @@ struct AppInstallHelper {
                      std::string version = std::string(),
                      bool isHybrid = false)
       : m_appName(appNamePrefix), m_pkgName(pkgNamePrefix), m_isLocal(isLocal), m_uidGid(uid), m_version(version),
-        m_installType(SM_APP_INSTALL_NONE), m_isHybrid(isHybrid)
+        m_installType(SM_APP_INSTALL_NONE), m_isHybrid(isHybrid), m_creatorPid(getpid())
     {
         setInstallPath();
     }
@@ -103,7 +102,8 @@ struct AppInstallHelper {
     std::string generatePkgLabel() const;
     void revokeRules() const;
     virtual ~AppInstallHelper() {
-        removePaths();
+        if (m_creatorPid == getpid())
+            removePaths();
     }
 
 protected:
@@ -121,4 +121,6 @@ protected:
     TypePathsMap m_fileTypeMap;
     std::vector<std::string> m_privileges;
     std::string m_author;
+
+    pid_t m_creatorPid;
 };
index 3dcf080..404bd2b 100644 (file)
@@ -36,7 +36,7 @@
 class ScopedInstaller {
 public:
     ScopedInstaller(const AppInstallHelper &appInstallHelper, bool requestUid = true)
-        : m_appInstallHelper(appInstallHelper), m_shouldUninstall(true)
+        : m_appInstallHelper(appInstallHelper), m_shouldUninstall(true), m_creatorPid(getpid())
     {
         SecurityManagerTest::InstallRequest instReq;
 
@@ -65,7 +65,8 @@ public:
     }
 
     virtual ~ScopedInstaller() {
-        uninstallApp();
+        if (m_creatorPid == getpid())
+            uninstallApp();
     }
 
     void uninstallApp() {
@@ -86,4 +87,5 @@ public:
 protected:
     AppInstallHelper m_appInstallHelper;
     bool m_shouldUninstall;
+    pid_t m_creatorPid;
 };
index fdfc5cb..656d397 100644 (file)
@@ -542,13 +542,6 @@ RUNNER_TEST(security_manager_20_user_cynara_policy)
 
 RUNNER_CHILD_TEST(security_manager_21_security_manager_admin_deny_user_priv)
 {
-    const int BUFFER_SIZE = 128;
-    struct message {
-        uid_t uid;
-        gid_t gid;
-        char buf[BUFFER_SIZE];
-    } msg;
-
     const privileges_t adminRequiredPrivs = {
         "http://tizen.org/privilege/notexist",
         "http://tizen.org/privilege/internal/usermanagement"
@@ -559,57 +552,40 @@ RUNNER_CHILD_TEST(security_manager_21_security_manager_admin_deny_user_priv)
     };
     const privileges_t allowedPrivsAfterChange = {"http://tizen.org/privilege/datasharing"};
     const privileges_t deniedPrivsAfterChange = {"http://tizen.org/privilege/internet"};
+    TemporaryTestUser adminUser("sm_test_21_admin_user_name", GUM_USERTYPE_ADMIN, false);
+    TemporaryTestUser normalUser("sm_test_21_normal_user_name", GUM_USERTYPE_NORMAL, false);
 
-    MessagePipe msgPipe;
-
-    pid_t pid = fork();
-    RUNNER_ASSERT_MSG(pid >= 0, "fork failed");
-    if (pid != 0)//parent process
-    {
-        msgPipe.claimParentEp();
-        TemporaryTestUser adminUser("sm_test_21_admin_user_name", GUM_USERTYPE_ADMIN, false);
-        TemporaryTestUser normalUser("sm_test_21_normal_user_name", GUM_USERTYPE_NORMAL, false);
-
-        adminUser.create();
-        normalUser.create();
-        std::string childUidStr = normalUser.getUidString();
-
-        AppInstallHelper adminApp("sm_test_21_admin", adminUser.getUid());
-        adminApp.createInstallDir();
-        adminApp.addPrivileges(adminRequiredPrivs);
-        ScopedInstaller adminAppInstall(adminApp);
+    adminUser.create();
+    normalUser.create();
+    std::string childUidStr = normalUser.getUidString();
 
-        AppInstallHelper normalApp("sm_test_21_normal", normalUser.getUid());
-        normalApp.createInstallDir();
-        normalApp.addPrivileges(manifestPrivs);
+    AppInstallHelper adminApp("sm_test_21_admin", adminUser.getUid());
+    adminApp.createInstallDir();
+    adminApp.addPrivileges(adminRequiredPrivs);
+    ScopedInstaller adminAppInstall(adminApp);
 
-        ScopedInstaller normalAppInstall(normalApp);
-        check_app_permissions(normalApp.getAppId(), normalApp.getPkgId(), childUidStr,
-                              manifestPrivs, {});
+    AppInstallHelper normalApp("sm_test_21_normal", normalUser.getUid());
+    normalApp.createInstallDir();
+    normalApp.addPrivileges(manifestPrivs);
+    ScopedInstaller normalAppInstall(normalApp);
 
-        //send info to child
-        msg.uid = adminUser.getUid();
-        msg.gid = adminUser.getGid();
-        strncpy (msg.buf, childUidStr.c_str(), BUFFER_SIZE);
-        msgPipe.write(msg);
+    check_app_permissions(normalApp.getAppId(), normalApp.getPkgId(), childUidStr,
+                          manifestPrivs, {});
 
+    pid_t pid = fork();
+    RUNNER_ASSERT_MSG(pid >= 0, "fork failed");
+    if (pid != 0) { //parent process
         waitPid(pid);
-
         check_app_permissions(normalApp.getAppId(), normalApp.getPkgId(), childUidStr,
                               allowedPrivsAfterChange, deniedPrivsAfterChange);
-    }
-    if (pid == 0) {
-        msgPipe.claimChildEp();
-        // Get created user credentials
-        msgPipe.read(msg);
-
-        AppInstallHelper adminApp("sm_test_21_admin");
+    } else {
         Api::setProcessLabel(adminApp.getAppId());
-        RUNNER_ASSERT_ERRNO_MSG(drop_root_privileges(msg.uid, msg.gid) == 0, "drop_root_privileges failed");
+        RUNNER_ASSERT_ERRNO_MSG(drop_root_privileges(adminUser.getUid(),adminUser.getGid()) == 0,
+                                "drop_root_privileges failed");
 
         PolicyRequest addPolicyReq;
         for (auto &deniedPriv : deniedPrivsAfterChange) {
-            PolicyEntry entry(SECURITY_MANAGER_ANY, msg.buf, deniedPriv);
+            PolicyEntry entry(SECURITY_MANAGER_ANY, normalUser.getUidString(), deniedPriv);
             entry.setMaxLevel("Deny");
             addPolicyReq.addEntry(entry);
         }
index 3e6f420..474a956 100644 (file)
@@ -64,6 +64,7 @@ AppInstallHelper prepAIH(const std::string &appName,
                          std::string version,
                          bool isSharedRO)
 {
+    const uid_t OWNER_UID = 5001;
     bool appIsLocal = true;
     AppInstallHelper appInstallHelper(appName, pkgName, appIsLocal, OWNER_UID, version);