Fix no smack 82/316582/8
authorZofia Abramowska <z.abramowska@samsung.com>
Fri, 13 Dec 2024 14:49:46 +0000 (15:49 +0100)
committerKrzysztof Malysa <k.malysa@samsung.com>
Mon, 24 Feb 2025 15:27:22 +0000 (16:27 +0100)
Change-Id: Ie8b12301f2ed12a76dfa9eb3b13a16da01b33080

13 files changed:
src/common/no_smack/no_smack.cpp
src/common/no_smack/no_smack.h
src/helpers/creds-commons/CMakeLists.txt
src/helpers/creds-commons/copyStr.cpp [new file with mode: 0644]
src/helpers/creds-commons/copyStr.h [new file with mode: 0644]
src/helpers/creds-dbus/creds-dbus.cpp
src/helpers/creds-gdbus/creds-gdbus.cpp
src/helpers/creds-pid/creds-pid-inner.cpp
src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp
src/helpers/creds-self/CMakeLists.txt
src/helpers/creds-self/creds-self.cpp
src/helpers/creds-socket/CMakeLists.txt
src/helpers/creds-socket/creds-socket-inner.cpp

index d5ee0ef51e325b3cc1fdddc15e19bfe8fe98c2b5..2215e26316d995a89943bbd4bc5487b3f8a83d4b 100644 (file)
 
 namespace Cynara {
 
-    bool smack_enabled() {
+bool smack_enabled() noexcept {
 #ifdef CYNARA_PERMISSIVE_MODE
-        return false;
+    return false;
 #else
-        return true;
+    return true;
 #endif
-    }
+}
 
 } // namespace Cynara
index 3f28f8c33153a2737d986819729714c1369dad2a..50196ceeaf01bfeb7585abcfeabffe5c1a3d2fbc 100644 (file)
@@ -29,6 +29,8 @@
 
 namespace Cynara {
 
-    bool smack_enabled();
+[[nodiscard]] bool smack_enabled() noexcept;
+
+inline constexpr char NO_SMACK_LABEL[] = "User::Pkg::default_app_no_Smack_mode";
 
 } // namespace Cynara
index 3fb8d6c0889e94dae012821ccbbf62804a536c0c..04659b4469d4c78bc9a655d33f1d4e3cf15aa69d 100644 (file)
@@ -29,6 +29,7 @@ SET(LIB_CREDS_COMMONS_VERSION ${LIB_CREDS_COMMONS_VERSION_MAJOR}.20.0)
 SET(LIB_CREDS_COMMONS_PATH ${CYNARA_PATH}/helpers/creds-commons)
 
 SET(LIB_CREDS_COMMONS_SOURCES
+    ${LIB_CREDS_COMMONS_PATH}/copyStr.cpp
     ${LIB_CREDS_COMMONS_PATH}/creds-commons.cpp
     ${LIB_CREDS_COMMONS_PATH}/CredsCommonsInner.cpp
     )
diff --git a/src/helpers/creds-commons/copyStr.cpp b/src/helpers/creds-commons/copyStr.cpp
new file mode 100644 (file)
index 0000000..2de3491
--- /dev/null
@@ -0,0 +1,43 @@
+/*\r
+ * Copyright (c) 2025 Samsung Electronics Co., Ltd All Rights Reserved\r
+ *\r
+ * This file is licensed under the terms of MIT License or the Apache License\r
+ * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.\r
+ * See the LICENSE file or the notice below for Apache License Version 2.0\r
+ * details.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License\r
+ */\r
+/**\r
+ * @file        src/helpers/creds-commons/copyStr.cpp\r
+ * @version     1.0\r
+ * @brief       Definition of copyStr()\r
+ */\r
+\r
+#include <cstring>\r
+#include <cynara-error.h>\r
+#include <log/log.h>\r
+#include <string_view>\r
+\r
+// Without this attribute, the symbol gets stripped by f****** RPM and is missing after installation\r
+// even though the built shared library contains it.\r
+__attribute__((visibility("default"))) int\r
+copyStr(char** client, const std::string_view& str) noexcept {\r
+    char *clientTmp = strndup(str.data(), str.size());\r
+    if (!clientTmp) {\r
+        LOGE("strndup failed");\r
+        return CYNARA_API_OUT_OF_MEMORY;\r
+    }\r
+    *client = clientTmp;\r
+    return CYNARA_API_SUCCESS;\r
+}\r
diff --git a/src/helpers/creds-commons/copyStr.h b/src/helpers/creds-commons/copyStr.h
new file mode 100644 (file)
index 0000000..6564502
--- /dev/null
@@ -0,0 +1,31 @@
+/*\r
+ * Copyright (c) 2025 Samsung Electronics Co., Ltd All Rights Reserved\r
+ *\r
+ * This file is licensed under the terms of MIT License or the Apache License\r
+ * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.\r
+ * See the LICENSE file or the notice below for Apache License Version 2.0\r
+ * details.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License\r
+ */\r
+/**\r
+ * @file        src/helpers/creds-commons/copyStr.h\r
+ * @version     1.0\r
+ * @brief       Declaration of copyStr()\r
+ */\r
+\r
+#pragma once\r
+\r
+#include <string_view>\r
+\r
+int copyStr(char** client, const std::string_view& str) noexcept;\r
index 162fd08fc01a57664eac19ca96e060231fbc6593..04bfae6775622d5315506efee682e89f316cf4da 100644 (file)
@@ -132,11 +132,12 @@ public:
         dbus_message_unref(reply);
 
         if (!Cynara::smack_enabled()) {
-            LOGW("Using default Smack label User::Pkg::default_app_no_Smack_mode, no-smack mode in Cynara");
+            LOGW("Using default Smack label " << Cynara::NO_SMACK_LABEL
+                 << ", no-smack mode in Cynara");
             if (m_securityLabel)
                 free(m_securityLabel);
             m_isSecuritySet = false;
-            m_securityLabel = strdup("User::Pkg::default_app_no_Smack_mode");
+            m_securityLabel = strdup(Cynara::NO_SMACK_LABEL);
             if (m_securityLabel == nullptr)
                 return CYNARA_API_OUT_OF_MEMORY;
             m_isSecuritySet = true;
index 6bae17cc408112fc245e2358dd53670b2d1ca3d8..ba9fbb6bc4792346204480a35be364e3fa9076fa 100644 (file)
@@ -91,11 +91,12 @@ public:
         g_variant_unref(reply);
 
         if (!Cynara::smack_enabled()) {
-            LOGW("Using default Smack label User::Pkg::default_app_no_Smack_mode, no-smack mode in Cynara");
+            LOGW("Using default Smack label " << Cynara::NO_SMACK_LABEL
+                 << ", no-smack mode in Cynara");
             if (m_securityLabel)
                 free(m_securityLabel);
             m_isSecuritySet = false;
-            m_securityLabel = g_strdup("User::Pkg::default_app_no_Smack_mode");
+            m_securityLabel = g_strdup(Cynara::NO_SMACK_LABEL);
             if (m_securityLabel == nullptr)
                 return CYNARA_API_OUT_OF_MEMORY;
             m_isSecuritySet = true;
index 4cd662f79b15d7fed8590205717f6973d0d13cd8..ab0334346c22503755218d2d1c8e048a172868b3 100644 (file)
 
 #include <no_smack/no_smack.h>
 
+#include <copyStr.h>
+
 #include "creds-pid-inner.h"
 
 namespace {
 
-int dupStr(const char *src, char **dest) {
-    char *destTmp = ::strdup(src);
-    if (!destTmp) {
-        LOGE("strdup failed");
-        return CYNARA_API_OUT_OF_MEMORY;
-    }
-    *dest = destTmp;
-    return CYNARA_API_SUCCESS;
-}
-
 int getStat(pid_t pid, struct stat *buf) {
     std::string path = "/proc/" + std::to_string(pid) + "/";
 
@@ -79,8 +71,8 @@ namespace client {
 
 int getCurrentSecurity(pid_t pid, char**client) {
     if (!Cynara::smack_enabled()) {
-        LOGW("Using default Smack label User::Pkg::default_app_no_Smack_mode, no-smack mode in Cynara");
-        return dupStr("User::Pkg::default_app_no_Smack_mode", client);
+        LOGW("Using default Smack label " << Cynara::NO_SMACK_LABEL << ", no-smack mode in Cynara");
+        return copyStr(client, Cynara::NO_SMACK_LABEL);
     }
 
     std::string path = "/proc/" + std::to_string(pid) + "/attr/current";
@@ -96,11 +88,11 @@ int getCurrentSecurity(pid_t pid, char**client) {
         return CYNARA_API_UNKNOWN_ERROR;
     }
 
-    return dupStr(label.c_str(), client);
+    return copyStr(client, label.c_str());
 }
 
 int getPidStr(pid_t pid, char **client) {
-    return dupStr(std::to_string(pid).c_str(), client);
+    return copyStr(client, std::to_string(pid).c_str());
 }
 
 }
@@ -116,7 +108,7 @@ int getUserId(pid_t pid, char **user) {
         return ret;
     }
 
-    return dupStr(std::to_string(buf.st_uid).c_str(), user);
+    return copyStr(user, std::to_string(buf.st_uid).c_str());
 }
 
 int getGroupId(pid_t pid, char **user) {
@@ -127,7 +119,7 @@ int getGroupId(pid_t pid, char **user) {
         LOGE("Failed to fetch process: " << pid << " status");
     }
 
-    return dupStr(std::to_string(buf.st_gid).c_str(), user);
+    return copyStr(user, std::to_string(buf.st_gid).c_str());
 }
 
 }
index 7651f77930d95ae43ae3984ca1a7914e7e92367d..26e2d6fb568bdc09b5a830cb2ebb7578f3a5706f 100644 (file)
@@ -86,8 +86,9 @@ int getClientPid(sd_bus *bus, const char *name, char **client) {
 int getClientSmackLabel(sd_bus *bus, const char *name, char **client) {
     return Cynara::tryCatch([&bus, &name, &client]() {
         if (!Cynara::smack_enabled()) {
-            LOGW("Using default Smack label User::Pkg::default_app_no_Smack_mode, no-smack mode in Cynara");
-            return copyStr(client, "User::Pkg::default_app_no_Smack_mode");
+            LOGW("Using default Smack label" << Cynara::NO_SMACK_LABEL
+                 << ", no-smack mode in Cynara");
+            return copyStr(client, Cynara::NO_SMACK_LABEL);
         }
 
         sd_bus_creds *creds;
index bca6c6279007014582ac0a067ee897ed6cfec86d..c42574c36264d8d2bec26323b926850999e8dbf8 100644 (file)
@@ -25,6 +25,7 @@ SET(LIB_CREDS_SELF_VERSION_MAJOR 0)
 SET(LIB_CREDS_SELF_VERSION ${LIB_CREDS_SELF_VERSION_MAJOR}.20.0)
 
 SET(LIB_CREDS_SELF_PATH ${CYNARA_PATH}/helpers/creds-self)
+SET(LIB_CREDS_COMMONS_PATH ${CYNARA_PATH}/helpers/creds-commons)
 
 SET(LIB_CREDS_SELF_SOURCES
     ${LIB_CREDS_SELF_PATH}/creds-self.cpp
@@ -33,6 +34,7 @@ SET(LIB_CREDS_SELF_SOURCES
 INCLUDE_DIRECTORIES(
     ${CYNARA_PATH}/include
     ${LIB_CREDS_SELF_PATH}
+    ${LIB_CREDS_COMMONS_PATH}
     )
 
 ADD_LIBRARY(${TARGET_LIB_CREDS_SELF} SHARED ${LIB_CREDS_SELF_SOURCES})
index 103e35d22f69f11ce4ca00c1696bfe6aad1210fc..74c9ae58479a88ecc5b8046746cbafab8e7ece01 100644 (file)
@@ -28,7 +28,7 @@
 
 #include <cstring>
 #include <fstream>
-#include <string>
+#include <string_view>
 #include <sys/types.h>
 #include <unistd.h>
 #include <no_smack/no_smack.h>
 #include <exceptions/TryCatch.h>
 #include <log/log.h>
 
+#include <copyStr.h>
 #include <cynara-creds-self.h>
 
 namespace {
-    int copyStr(char **client, const std::string &str) {
-        char *clientTmp = strdup(str.c_str());
-        if (!clientTmp) {
-            LOGE("strdup failed");
-            return CYNARA_API_OUT_OF_MEMORY;
-        }
-        *client = clientTmp;
-        return CYNARA_API_SUCCESS;
-    }
-
     int getSelfSmackLabel(char **client) {
         return Cynara::tryCatch([&client](){
             if (!Cynara::smack_enabled()) {
-                LOGW("Using default Smack label User::Pkg::default_app_no_Smack_mode, no-smack mode in Cynara");
-                *client = strdup("User::Pkg::default_app_no_Smack_mode");
-                if (!(*client)) {
-                    LOGE("strdup failed");
-                    return CYNARA_API_OUT_OF_MEMORY;
-                }
-                return CYNARA_API_SUCCESS;
+                LOGW("Using default Smack label "<< Cynara::NO_SMACK_LABEL
+                     << ", no-smack mode in Cynara");
+                return copyStr(client, Cynara::NO_SMACK_LABEL);
             }
 
             std::ifstream current("/proc/self/attr/current");
@@ -138,4 +125,3 @@ int cynara_creds_self_get_user(enum cynara_user_creds method, char **user) {
             return CYNARA_API_METHOD_NOT_SUPPORTED;
     }
 }
-
index 9489a72d7cee2ca66730b3d8293c081762293435..47b581b13b8b0a52058497f736ed7ffd4fe8ce93 100644 (file)
@@ -27,6 +27,7 @@ SET(LIB_CREDS_SOCKET_VERSION_MAJOR 0)
 SET(LIB_CREDS_SOCKET_VERSION ${LIB_CREDS_SOCKET_VERSION_MAJOR}.20.0)
 
 SET(LIB_CREDS_SOCKET_PATH ${CYNARA_PATH}/helpers/creds-socket)
+SET(LIB_CREDS_COMMONS_PATH ${CYNARA_PATH}/helpers/creds-commons)
 
 SET(LIB_CREDS_SOCKET_SOURCES
     ${LIB_CREDS_SOCKET_PATH}/creds-socket.cpp
@@ -36,6 +37,7 @@ SET(LIB_CREDS_SOCKET_SOURCES
 INCLUDE_DIRECTORIES(
     ${CYNARA_PATH}/include
     ${LIB_CREDS_SOCKET_PATH}
+    ${LIB_CREDS_COMMONS_PATH}
     )
 
 ADD_LIBRARY(${TARGET_LIB_CREDS_SOCKET} SHARED ${LIB_CREDS_SOCKET_SOURCES})
index 1d310087f84ef368779846392dd819d2b28ce3c1..3c5dba510abc9ee956f186de5689c7af8896fa5e 100644 (file)
@@ -29,7 +29,7 @@
 
 #include <cerrno>
 #include <cstring>
-#include <string>
+#include <cstdlib>
 #include <sys/socket.h>
 #include <sys/types.h>
 
 
 #include <cynara-error.h>
 
+#include <copyStr.h>
+
 #include "creds-socket-inner.h"
 
 int getClientSmackLabel(int socketFd, char **client) {
     if (!Cynara::smack_enabled()) {
-        LOGW("Using default Smack label User::Pkg::default_app_no_Smack_mode, no-smack mode in Cynara");
-        *client = strdup("User::Pkg::default_app_no_Smack_mode");
-        if (!(*client)) {
-            LOGE("strdup failed");
-            return CYNARA_API_OUT_OF_MEMORY;
-        }
-        return CYNARA_API_SUCCESS;
+        LOGW("Using default Smack label " << Cynara::NO_SMACK_LABEL << ", no-smack mode in Cynara");
+        return copyStr(client, Cynara::NO_SMACK_LABEL);
     }
     char dummy;
     int ret;