return getAppProcessLabel(appName);
}
+struct AppMgrCheckAppsCbContext {
+ std::string appName;
+ bool isRunning;
+};
+
+bool ServiceImpl::checkRunningApps(app_context_h app_context, void *user_data)
+{
+ AppMgrCheckAppsCbContext *context = reinterpret_cast<AppMgrCheckAppsCbContext*>(user_data);
+ std::string appId = getAppIdFromContext(app_context);
+ if (appId == context->appName) {
+ context->isRunning = true;
+ return false;
+ }
+
+ return true;
+}
+
int ServiceImpl::prepareApp(const Credentials &creds, const std::string &appName, const std::vector<std::string> &privPathsVector,
Smack::Label &label, std::string &pkgName, bool &enabledSharedRO,
std::vector<gid_t> &forbiddenGroups, std::vector<gid_t> &allowedGroups, std::vector<bool> &privPathsStatusVector)
getPkgLabels(pkgName, pkgLabels);
if (m_smackRules.isPrivilegeMappingEnabled()) {
- m_appIdUidMap[appName] = creds.uid;
+ uid_t savedUid;
+ auto it = m_appIdUidMap.find(appName);
+ if (it == m_appIdUidMap.end()) {
+ m_appIdUidMap[appName] = creds.uid;
+ savedUid = creds.uid;
+ } else {
+ savedUid = it->second;
+ }
+ // We have to remove all possible privilege related Smack rules, because application
+ // policy might have changed from last prepareApp
+ // (e.g. application new version was installed)
m_smackRules.disableAllPrivilegeRules(label, pkgName, authorId);
- m_smackRules.enablePrivilegeRules(label, pkgName, authorId, allowedPrivileges);
+
+ if (savedUid != creds.uid) {
+ LogDebug("Possible second instance detected. Checking all running apps");
+
+ AppMgrCheckAppsCbContext context{appName, false};
+ int ret = app_manager_foreach_running_app_context(&ServiceImpl::checkRunningApps,
+ &context);
+ if (ret != APP_MANAGER_ERROR_NONE) {
+ LogError("Couldn't check running apps. No Smack policy will be applied for "
+ << appName);
+ } else if (context.isRunning) {
+ LogError("Application is already running! No Smack policy will be applied for "
+ << appName);
+ } else {
+ m_smackRules.enablePrivilegeRules(label, pkgName, authorId, allowedPrivileges);
+ }
+ m_appIdUidMap[appName] = creds.uid;
+ } else {
+ m_smackRules.enablePrivilegeRules(label, pkgName, authorId, allowedPrivileges);
+ }
}
ret = getForbiddenAndAllowedGroups(label, allowedPrivileges, forbiddenGroups,