Fixed expected return value & improper usage of getUserStruct 09/73009/4
authorTomasz Swierczek <t.swierczek@samsung.com>
Fri, 3 Jun 2016 12:12:51 +0000 (14:12 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Tue, 14 Jun 2016 10:52:11 +0000 (03:52 -0700)
Accessing another user's policy without privilege should be treated as error.
getUserStruct needs to be run in privileged environment (otherwise getpwnam
returns EACCESS).

Change-Id: Id1185deddcded420a409386605d20954d58b6bb0

src/security-manager-tests/security_manager_tests.cpp

index b20979a..5de34ba 100644 (file)
@@ -1202,9 +1202,9 @@ RUNNER_CHILD_TEST(security_manager_10_privacy_manager_fetch_whole_policy_for_sel
     } else { //child process
         pipe.claimChildEp();
         pipe.wait();
-        //the above call, registers 1 new privilege for the given user, hence the incrementation of below variable
 
         struct passwd *pw = getUserStruct(username);
+        std::string uidStr = std::to_string(pw->pw_uid);
         register_current_process_as_privilege_manager(pw->pw_uid);
         int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
         RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
@@ -1221,15 +1221,14 @@ RUNNER_CHILD_TEST(security_manager_10_privacy_manager_fetch_whole_policy_for_sel
             std::string app = policyEntry.getAppId();
             std::string privilege = policyEntry.getPrivilege();
 
+            RUNNER_ASSERT_MSG(user == uidStr, "Unexpected user: " << user);
+
             try {
-                struct passwd *pw_current = getUserStruct(static_cast<uid_t>(std::stoul(user)));
-                std::set<std::string>::iterator tmp = users2AppsMap.at(pw_current->pw_name).at(app).find(privilege);
-                if (tmp == users2AppsMap.at(pw_current->pw_name).at(app).end())
+                std::set<std::string>::iterator tmp = users2AppsMap.at(username).at(app).find(privilege);
+                if (tmp == users2AppsMap.at(username).at(app).end())
                     RUNNER_FAIL_MSG("Unexpected policy entry: unexpected privilege: " << policyEntry);
             } catch (const std::out_of_range &e) {
-                RUNNER_FAIL_MSG("Unexpected policy entry: unexpected user or app: " << policyEntry << ". Exception: " << e.what());
-            } catch (const std::invalid_argument& e) {
-                RUNNER_FAIL_MSG("Incorrect UID: " << user << ". Exception: " << e.what());
+                RUNNER_FAIL_MSG("Unexpected policy entry: unexpected app: " << policyEntry << ". Exception: " << e.what());
             };
         };
         exit(0);
@@ -1313,6 +1312,7 @@ RUNNER_CHILD_TEST(security_manager_11_privacy_manager_fetch_whole_policy_for_adm
         pipe.wait();
 
         struct passwd *pw = getUserStruct(usernames.at(0));
+        std::string uidStr = std::to_string(pw->pw_uid);
         register_current_process_as_privilege_manager(pw->pw_uid);
 
         //change uid to normal user
@@ -1334,15 +1334,14 @@ RUNNER_CHILD_TEST(security_manager_11_privacy_manager_fetch_whole_policy_for_adm
             std::string app = policyEntry.getAppId();
             std::string privilege = policyEntry.getPrivilege();
 
+            RUNNER_ASSERT_MSG(uidStr == user, "Unexpected user: " << user);
+
             try {
-                struct passwd *pw_current = getUserStruct(static_cast<uid_t>(std::stoul(user)));
-                std::set<std::string>::iterator tmp = users2AppsMap.at(pw_current->pw_name).at(app).find(privilege);
-                if (tmp == users2AppsMap.at(pw_current->pw_name).at(app).end())
+                std::set<std::string>::iterator tmp = users2AppsMap.at(usernames.at(0)).at(app).find(privilege);
+                if (tmp == users2AppsMap.at(usernames.at(0)).at(app).end())
                     RUNNER_FAIL_MSG("Unexpected policy entry: unexpected privilege: " << policyEntry);
             } catch (const std::out_of_range &e) {
-                RUNNER_FAIL_MSG("Unexpected policy entry: unexpected user or app: " << policyEntry << ". Exception: " << e.what());
-            } catch (const std::invalid_argument& e) {
-                RUNNER_FAIL_MSG("Incorrect UID: " << user << ". Exception: " << e.what());
+                RUNNER_FAIL_MSG("Unexpected policy entry: app: " << policyEntry << ". Exception: " << e.what());
             };
         };
         exit(0);
@@ -1426,12 +1425,18 @@ RUNNER_CHILD_TEST(security_manager_12_privacy_manager_fetch_whole_policy_for_adm
         pipe.wait();
 
         struct passwd *pw = getUserStruct(usernames.at(1));
-        register_current_process_as_privilege_manager(pw->pw_uid, true);
+        uid_t myUid = pw->pw_uid;
+        gid_t myGid = pw->pw_gid;
+        std::string uidStr1 = std::to_string(myUid);
+        pw = getUserStruct(usernames.at(0));
+        std::string uidStr0 = std::to_string(pw->pw_uid);
+        register_current_process_as_privilege_manager(myUid, true);
 
         //change uid to normal user
-        int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
+        int result = drop_root_privileges(myUid, myGid);
         RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
 
+
         std::vector<PolicyEntry> policyEntries;
         //this call should succeed as the calling user is privileged
         Api::getPolicy(PolicyEntry(), policyEntries);
@@ -1447,13 +1452,16 @@ RUNNER_CHILD_TEST(security_manager_12_privacy_manager_fetch_whole_policy_for_adm
             std::string app = policyEntry.getAppId();
             std::string privilege = policyEntry.getPrivilege();
 
+            RUNNER_ASSERT_MSG(user == uidStr0 || user == uidStr1, "Unexpected user: " << user);
+
+            std::string uidStrToLook = user == uidStr0 ? usernames.at(0) : usernames.at(1);
+
             try {
-                struct passwd *pw_current = getUserStruct(static_cast<uid_t>(std::stoul(user)));
-                std::set<std::string>::iterator tmp = users2AppsMap.at(pw_current->pw_name).at(app).find(privilege);
-                if (tmp == users2AppsMap.at(pw_current->pw_name).at(app).end())
+                std::set<std::string>::iterator tmp = users2AppsMap.at(uidStrToLook).at(app).find(privilege);
+                if (tmp == users2AppsMap.at(uidStrToLook).at(app).end())
                     RUNNER_FAIL_MSG("Unexpected policy entry: unexpected privilege: " << policyEntry);
             } catch (const std::out_of_range &e) {
-                RUNNER_FAIL_MSG("Unexpected policy entry: unexpected user or app: " << policyEntry << ". Exception: " << e.what());
+                RUNNER_FAIL_MSG("Unexpected policy entry: unexpected app: " << policyEntry << ". Exception: " << e.what());
             } catch (const std::invalid_argument& e) {
                 RUNNER_FAIL_MSG("Incorrect UID: " << user << ". Exception: " << e.what());
             };
@@ -1534,22 +1542,27 @@ RUNNER_CHILD_TEST(security_manager_13_privacy_manager_fetch_policy_after_update_
         if (pid[1] == 0) { //child #2 process
             sync[1].claimChildEp();
             sync[1].wait();
-            struct passwd *pw_target = getUserStruct(usernames.at(0));
-            struct passwd *pw = getUserStruct(usernames.at(1));
-            register_current_process_as_privilege_manager(pw->pw_uid);
+
+            struct passwd *pw = getUserStruct(usernames.at(0));
+            uid_t target_uid = pw->pw_uid;
+            pw = getUserStruct(usernames.at(1));
+            uid_t my_uid = pw->pw_uid;
+            gid_t my_gid = pw->pw_gid;
+
+            register_current_process_as_privilege_manager(my_uid);
 
             //change uid to normal user
-            int result = drop_root_privileges(pw->pw_uid, pw->pw_gid);
+            int result = drop_root_privileges(my_uid, my_gid);
             RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
 
             PolicyEntry filter = PolicyEntry(
                         SECURITY_MANAGER_ANY,
-                        std::to_string(pw_target->pw_uid),
+                        std::to_string(target_uid),
                         SECURITY_MANAGER_ANY
                         );
 
             //U2 requests contents of U1 privacy manager - should fail
-            Api::getPolicyForSelf(filter, policyEntries);
+            Api::getPolicyForSelf(filter, policyEntries, SECURITY_MANAGER_ERROR_ACCESS_DENIED);
             RUNNER_ASSERT_MSG(policyEntries.size() == 0, "Policy is not empty, but is " << policyEntries.size());
 
             filter = PolicyEntry(
@@ -1568,7 +1581,7 @@ RUNNER_CHILD_TEST(security_manager_13_privacy_manager_fetch_policy_after_update_
             sync[1].claimParentEp();
             std::vector<TemporaryTestUser> users = {
                 TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
-                TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
+                TemporaryTestUser(usernames.at(1), GUM_USERTYPE_NORMAL, false)
                 };
 
             users.at(0).create();