return SECURITY_MANAGER_SUCCESS;
}
-static inline int security_manager_create_namespace_internal(const std::string &app_label)
+SECURITY_MANAGER_API
+int security_manager_prepare_app_candidate(void)
+{
+ return try_catch([&]() -> int {
+ LogDebug("security_manager_prepare_app_candidate() called");
+ if (!MountNS::isMountNamespaceEnabled())
+ return SECURITY_MANAGER_SUCCESS;
+
+ FS::FileNameVector files = FS::getSubDirectoriesFromDirectory("/proc/self/task");
+ if (files.size() > 3) { // 3 because we have ., we have .. and we should have only one thread here
+ LogError("Too many threads in current process, can't switch to new namespace safely in multithreaded program");
+ return SECURITY_MANAGER_ERROR_INPUT_PARAM;
+ }
+ return MountNS::createMountNamespace();
+ });
+}
+
+static inline int security_manager_setup_namespace_internal(const std::string &app_label)
{
if (!MountNS::isMountNamespaceEnabled())
return SECURITY_MANAGER_SUCCESS;
- int ret = MountNS::createMountNamespace();
- if (ret != SECURITY_MANAGER_SUCCESS)
- return ret;
-
- ret = MountNS::makeMountSlave("/");
+ int ret = MountNS::makeMountSlave("/");
if (ret != SECURITY_MANAGER_SUCCESS)
return ret;
return ret;
}
- ret = security_manager_create_namespace_internal(app_label);
+ ret = security_manager_setup_namespace_internal(app_label);
if (ret != SECURITY_MANAGER_SUCCESS) {
LogError("Unable to setup namespace for application " << app_name);
return ret;
/*
- * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
*
* Contact: Rafal Krypa <r.krypa@samsung.com>
*
/**
* A convenience function for launchers for preparing security context for an
- * application process. It should be called after fork in the new process, before
- * running the application in it.
- * It is aimed to cover most common cases and will internally call other, more
- * specialized security-manager functions for launchers.
- * Currently it just calls:
- * - security_manager_set_process_label_from_appid
- * - security_manager_set_process_groups_from_appid
- * - security_manager_drop_process_privileges
+ * application candidate process. It is aimed to cover these actions that
+ * are generic to all kinds of applications and MUST be called
+ * in single-threaded environment, after fork(), before the application launcher
+ * calls exec() on app candidate process binary (custom app loader).
+ *
+ * On systems with configured mount namespaces for privilege access control
+ * (non-empty file privilege-mount.list), this currently only sets up the new
+ * mount namespace.
+ *
+ * On systems without mount namespaces-controlled privileges, this is currently
+ * a no-op.
+ *
+ * \return API return code or error code
+ */
+int security_manager_prepare_app_candidate(void);
+
+/**
+ * A convenience function for launchers for preparing security context for an
+ * application process. It should be called before running actual application code.
+ *
+ * This function has to be called in a process where security_manager_prepare_app_candidate() was
+ * already called and all existing threads are already in the same namespaces.
+ *
+ * This function can be called in multithreaded environment.
*
* \param[in] app_id Application identifier
* \return API return code or error code