Fix security_manager_set_identity with author id 45/320445/6
authorFilip Skrzeczkowski <f.skrzeczkow@samsung.com>
Fri, 28 Feb 2025 13:48:25 +0000 (14:48 +0100)
committerKrzysztof Malysa <k.malysa@samsung.com>
Wed, 5 Mar 2025 09:13:11 +0000 (10:13 +0100)
Change-Id: Ia5e0712817c74704d6943eae6ed79e8d29713cea

src/client/client-security-manager.cpp
src/common/utils.cpp

index 5b863f5e5d7724ba1b4526506ee255fa66f91456..d5c3265b7457d31feb813838843c93c8ca9f4c5c 100644 (file)
@@ -2444,21 +2444,17 @@ int security_manager_set_identity(
         return SECURITY_MANAGER_ERROR_SERVER_ERROR;
     }
 
-    auto setSytemUid = [&]() -> int {
-        return try_catch([&]() -> int {
-            auto systemUid = getSystemUid();
+    switch (type) {
+    case (process_type::SM_PROCESS_TYPE_SYSTEM):
+    case (process_type::SM_PROCESS_TYPE_SYSTEM_PRIVILEGED):
+        return try_catch([&] {
+            uid_t systemUid = getSystemUid();
             if (setuid(systemUid)) {
-                LogError("Failed to set UID " << systemUid << " for current process");
+                LogErrno("Failed to set UID " << systemUid << " for current process");
                 return SECURITY_MANAGER_ERROR_UNKNOWN;
             }
             return SECURITY_MANAGER_SUCCESS;
         });
-    };
-
-    switch (type) {
-    case (process_type::SM_PROCESS_TYPE_SYSTEM):
-    case (process_type::SM_PROCESS_TYPE_SYSTEM_PRIVILEGED):
-        return setSytemUid();
     case (process_type::SM_PROCESS_TYPE_APP):
         if (app_id == nullptr) {
             LogError("app_id is NULL");
@@ -2490,20 +2486,22 @@ int security_manager_set_identity(
             return ret;
         }
 
-        if (setuid(puid)) {
-            LogError("Failed to set UID " << puid << "for current process");
-            return SECURITY_MANAGER_ERROR_UNKNOWN;
+        // It's important to assign the group first and only then change the PUID
+        if (!agidString.empty())
+        {
+            gid_t groups[] = { agid };
+            if (setgroups(1, groups)) {
+                LogErrno("Failed to set supplementary group " << agid << " for current process");
+                return SECURITY_MANAGER_ERROR_UNKNOWN;
+            }
         }
 
-        if (agidString.empty())
-            return SECURITY_MANAGER_SUCCESS;
-
-        gid_t groups[] = { agid };
-        if (setgroups(1, groups)) {
-            LogError("Failed to set supplementary group " << agid << "for current process");
+        if (setuid(puid)) {
+            LogErrno("Failed to set UID " << puid << " for current process");
             return SECURITY_MANAGER_ERROR_UNKNOWN;
         }
+
         return SECURITY_MANAGER_SUCCESS;
     }
-    return SECURITY_MANAGER_SUCCESS;
+    return SECURITY_MANAGER_ERROR_SERVER_ERROR;
 }
index 3ebca7e5dc8009391a21b81d74181b0188c57bdf..3e5590b3fe029c1a6ae2a30aa001bdcc5a1c9494 100644 (file)
@@ -133,7 +133,7 @@ uid_t getUidByName(const std::string& name) {
 }
 
 uid_t getSystemUid() {
-    const static uid_t uid = getGidByName("system");
+    const static uid_t uid = getUidByName("system");
     return uid;
 }