-# Copyright (c) 2013-2019 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2013-2020 Samsung Electronics Co., Ltd All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
)
SET(CKM_SOURCES
- access_provider2.cpp
async-api.cpp
capi-access_control.cpp
ckm-privileged-common.cpp
initial-values.cpp
main.cpp
+ scoped-app-context.cpp
system-db.cpp
)
+++ /dev/null
-/*
- * Copyright (c) 2013 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * @file access_provider.cpp
- * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
- * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
- * @version 1.0
- * @brief Common functions and macros used in security-tests package.
- */
-#include <sys/types.h>
-#include <unistd.h>
-#include <sys/smack.h>
-
-#include <access_provider2.h>
-#include <tests_common.h>
-#include <ckm-common.h>
-#include <scoped_process_label.h>
-
-namespace {
-
-std::string toSmackLabel(const std::string &ownerId) {
- if (ownerId.empty())
- return ownerId;
-
- if (ownerId[0] == '/') {
- return ownerId.substr(1, std::string::npos);
- }
-
- return SMACK_USER_APP_PREFIX + ownerId;
-}
-
-} // anonymous namespace
-
-AccessProvider::AccessProvider(const std::string &ownerId, int uid, int gid)
- : m_mySubject(toSmackLabel(ownerId))
- , m_inSwitchContext(false)
-{
- RUNNER_ASSERT_MSG(m_mySubject.size() > 0, "No smack label provided to AccessProvider!");
- allowJournaldLogs();
- applyAndSwithToUser(uid, gid);
-}
-
-AccessProvider::~AccessProvider()
-{
-
-}
-
-void AccessProvider::allowAPI(const std::string &api, const std::string &rule) {
- m_smackAccess.add(m_mySubject, api, rule);
-}
-
-void AccessProvider::apply() {
- // This should be done by security-manager
- m_smackAccess.add("System", m_mySubject, "w");
- m_smackAccess.add(m_mySubject, "System", "w");
- m_smackAccess.apply();
-}
-
-void AccessProvider::applyAndSwithToUser(int uid, int gid)
-{
- RUNNER_ASSERT_MSG(m_inSwitchContext == false, "already switched context");
-
- clear();
- apply();
-
- m_processLabel.reset(new ScopedProcessLabel(m_mySubject));
-
- m_origUid = getuid();
- m_origGid = getgid();
- RUNNER_ASSERT_MSG(0 == setegid(gid),
- "Error in setgid.");
- RUNNER_ASSERT_MSG(0 == seteuid(uid),
- "Error in setuid.");
- m_inSwitchContext = true;
-}
-
-void AccessProvider::clear() {
- m_smackAccess.clear();
-}
-
-void AccessProvider::allowJournaldLogs() {
- allowAPI("System::Run","wx"); // necessary for logging with journald
-}
-
-ScopedAccessProvider::~ScopedAccessProvider()
-{
- if(m_inSwitchContext == true)
- {
- RUNNER_ASSERT_MSG(0 == setegid(m_origGid), "Error in setgid.");
- RUNNER_ASSERT_MSG(0 == seteuid(m_origUid), "Error in setuid.");
- clear();
- m_processLabel.reset();
- m_inSwitchContext = false;
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2013 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * @file access_provider2.h
- * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
- * @version 1.0
- * @brief Common functions and macros used in security-tests package.
- */
-#ifndef _ACCESS_FOR_DUMMIES_H_
-#define _ACCESS_FOR_DUMMIES_H_
-
-#include <string>
-#include <memory>
-
-#include <smack_access.h>
-
-class ScopedProcessLabel;
-
-class AccessProvider {
-public:
- AccessProvider(const std::string &ownerId, int uid, int gid);
- virtual ~AccessProvider();
-
- AccessProvider(const AccessProvider &second) = delete;
- AccessProvider& operator=(const AccessProvider &second) = delete;
-
- void allowAPI(const std::string &api, const std::string &rules);
- void apply();
- void applyAndSwithToUser(int uid, int gid);
- void clear();
-
-private:
- void allowJournaldLogs();
-
- SmackAccess m_smackAccess;
-protected:
- std::string m_mySubject;
- uid_t m_origUid;
- gid_t m_origGid;
- std::unique_ptr<ScopedProcessLabel> m_processLabel;
- bool m_inSwitchContext;
-};
-
-class ScopedAccessProvider : public AccessProvider {
-public:
- ScopedAccessProvider(const std::string &mySubject, int uid, int gid)
- : AccessProvider(mySubject, uid, gid) {}
- virtual ~ScopedAccessProvider();
-};
-
-#endif // _ACCESS_FOR_DUMMIES_H_
/*
- * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Contact: Bumjin Im <bj.im@samsung.com>
*
#include <tests_common.h>
#include <test-certs.h>
#include <ckm-common.h>
-#include <access_provider2.h>
+#include <scoped-app-context.h>
#include <random>
using namespace CKM;
void init(const std::string & str) {
RemoveDataEnv<APP_UID>::init(str);
unlock_user_data(APP_UID, TEST_PASS);
- m_ap.reset(new ScopedAccessProvider(TEST_LABEL, APP_UID, APP_GID));
+ m_ctx.reset(new ScopedAppContext(TEST_LABEL, APP_UID, APP_GID));
}
void finish() {
- m_ap.reset();
+ m_ctx.reset();
// lock is performed by remove_user_data() in RemoveDataEnv
RemoveDataEnv<APP_UID>::finish();
}
- std::unique_ptr<ScopedAccessProvider> m_ap;
+ std::unique_ptr<ScopedAppContext> m_ctx;
};
} // namespace anonymous
std::string alias2 = aliasWithLabel(TEST_LABEL, "alias-2");
std::string alias3 = aliasWithLabel(TEST_LABEL, "alias-3");
{
- ScopedAccessProvider ap(TEST_LABEL, APP_UID, APP_GID);
+ ScopedAppContext ctx(TEST_LABEL, APP_UID, APP_GID);
save_data(alias1.c_str(), TEST_DATA);
save_data(alias2.c_str(), TEST_DATA);
save_data(alias3.c_str(), TEST_DATA);
}
{
- ScopedAccessProvider ap(TEST_LABEL_2, APP_UID, APP_GID);
+ ScopedAppContext ctx(TEST_LABEL_2, APP_UID, APP_GID);
test_negative(&ManagerAsync::getData, CKM_API_ERROR_DB_ALIAS_UNKNOWN, alias1, "");
test_negative(&ManagerAsync::removeAlias, CKM_API_ERROR_DB_ALIAS_UNKNOWN, alias1);
// prepare: add data
std::string alias1 = aliasWithLabel(TEST_LABEL, "alias-1");
{
- ScopedAccessProvider ap(TEST_LABEL, APP_UID, APP_GID);
+ ScopedAppContext ctx(TEST_LABEL, APP_UID, APP_GID);
save_data(alias1.c_str(), TEST_DATA);
test_positive(&ManagerAsync::setPermission,
}
{
- ScopedAccessProvider ap(TEST_LABEL_2, APP_UID, APP_GID);
+ ScopedAppContext ctx(TEST_LABEL_2, APP_UID, APP_GID);
test_negative(&ManagerAsync::getData, CKM_API_ERROR_DB_ALIAS_UNKNOWN, alias1, "");
test_negative(&ManagerAsync::removeAlias, CKM_API_ERROR_DB_ALIAS_UNKNOWN, alias1);
#include <tests_common.h>
#include <ckm-common.h>
-#include <access_provider2.h>
+#include <scoped-app-context.h>
#include <ckmc/ckmc-manager.h>
#include <ckmc/ckmc-control.h>
// invalid arguments check
RUNNER_TEST(T3001_manager_allow_access_invalid)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
RUNNER_ASSERT(
CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission(NULL, "accessor", CKMC_PERMISSION_READ));
// invalid arguments check
RUNNER_TEST(T3002_manager_deny_access_invalid)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission(NULL, "accessor", CKMC_PERMISSION_NONE));
RUNNER_ASSERT(CKMC_ERROR_INVALID_PARAMETER == ckmc_set_permission("alias", NULL, CKMC_PERMISSION_NONE));
// tries to allow access for non existing alias
RUNNER_CHILD_TEST(T3003_manager_allow_access_non_existing)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
int ret = ckmc_set_permission(NO_ALIAS, "label", CKMC_PERMISSION_READ);
RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
// tries to deny access for non existing alias
RUNNER_CHILD_TEST(T3004_manager_deny_access_non_existing)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
int ret = ckmc_set_permission(NO_ALIAS, "label", CKMC_PERMISSION_NONE);
RUNNER_ASSERT_MSG(CKMC_ERROR_DB_ALIAS_UNKNOWN == ret,
// tries to deny access that does not exist in database
RUNNER_CHILD_TEST(T3005_manager_deny_access_non_existing_access)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
// tries to allow access to application own data
RUNNER_CHILD_TEST(T3006_manager_allow_access_to_myself)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
// verifies that alias can not contain forbidden characters
RUNNER_CHILD_TEST(T3007_manager_check_alias_valid)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
// verifies that label can not contain forbidden characters
RUNNER_CHILD_TEST(T3008_manager_check_label_valid)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
check_read_not_visible(TEST_ALIAS_adr.c_str());
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
}
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
}
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
}
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
}
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
}
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
// access should be overwritten
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
// prepare: add data
std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_denied(TEST_ALIAS_adr.c_str());
check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
// remove permission
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
deny_access(TEST_ALIAS, APP_LABEL_2);
}
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_not_visible(TEST_ALIAS_adr.c_str());
check_read_not_visible(TEST_ALIAS_adr.c_str());
// prepare: add data
const char *additional_data = "label-2-data";
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
// add data as app 2
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
save_data(TEST_ALIAS, additional_data);
allow_access(TEST_ALIAS, APP_LABEL_1, CKMC_PERMISSION_READ);
// test accessibility to app 2 from app 1
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
// test if can access label2 alias from label1 domain - should succeed
check_read_allowed(aliasWithLabel(APP_LABEL_2, TEST_ALIAS).c_str(), additional_data);
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access(TEST_ALIAS, APP_LABEL_3, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
allow_access_negative(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_4, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE, CKMC_ERROR_PERMISSION_DENIED);
deny_access_negative (aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), APP_LABEL_4, CKMC_ERROR_PERMISSION_DENIED);
// prepare: add data
size_t count;
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
save_data(TEST_ALIAS2, TEST_DATA);
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
// check that app can access other aliases when it has permission
check_alias_count(count - 1);
// remove permission
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
deny_access(TEST_ALIAS, APP_LABEL_2);
}
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
// check that app can't access other aliases for which permission has been revoked
check_alias_count(count - 2);
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
allow_access_deprecated(TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
// test accessibility from another label
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
RUNNER_TEST(T3106_control_allow_access_to_myself, RemoveDataEnv<APP_1>)
{
// prepare: add data
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
// test
RUNNER_IGNORED_MSG("Disabled until labeled sockets not available");
// prepare: add data
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
// test
RUNNER_IGNORED_MSG("Disabled until labeled sockets not available");
// prepare: add data
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
// test
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_denied(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE);
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
check_remove_denied(TEST_ALIAS_adr.c_str());
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
std::string TEST_ALIAS_adr = aliasWithLabel(APP_LABEL_1, TEST_ALIAS);
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_denied(TEST_ALIAS_adr.c_str());
check_read_allowed(TEST_ALIAS_adr.c_str(), TEST_DATA);
deny_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_not_visible(TEST_ALIAS_adr.c_str());
check_read_not_visible(TEST_ALIAS_adr.c_str());
// prepare: add data
size_t count;
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
save_data(TEST_ALIAS2, TEST_DATA);
allow_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_PERMISSION_READ);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
// check that app can access other aliases when it has permission
check_alias_count(count - 1);
deny_access_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
// check that app can't access other aliases for which permission has been revoked
check_alias_count(count - 2);
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_read_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str(), TEST_DATA);
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_denied(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
}
{
// prepare: add data
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
}
allow_access_deprecated_by_adm(APP_1, APP_LABEL_1, TEST_ALIAS, APP_LABEL_2, CKMC_AR_READ_REMOVE);
{
- ScopedAccessProvider ap(APP_LABEL_2, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_2, APP_1, GROUP_1);
check_remove_allowed(aliasWithLabel(APP_LABEL_1, TEST_ALIAS).c_str());
}
RUNNER_TEST(utc_ckmc_get_data_alias_info_list_p2, RemoveDataEnv<APP_1>)
{
- ScopedAccessProvider ap(APP_LABEL_1, APP_1, GROUP_1);
+ ScopedAppContext ctx(APP_LABEL_1, APP_1, GROUP_1);
save_data(TEST_ALIAS, TEST_DATA);
ckmc_alias_info_list_s* ppalias_list = NULL;
#include <ckm/ckm-control.h>
#include <ckm/ckm-manager.h>
#include <ckmc/ckmc-manager.h>
-#include <access_provider2.h>
+#include <scoped-app-context.h>
#include <fstream>
#include <ios>
#include <unistd.h>
// [test2]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
check_key(XML_1_EXPECTED_KEY_1_RSA.c_str(), CKMC_ERROR_NOT_EXPORTABLE);
check_key_not_visible(XML_1_EXPECTED_KEY_2_RSA.c_str());
// [test3]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL_2, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_APP, GROUP_APP);
check_key_not_visible(XML_1_EXPECTED_KEY_1_RSA.c_str());
check_key_allowed(XML_1_EXPECTED_KEY_2_RSA.c_str(), CKMC_KEY_RSA_PRIVATE);
#include <tests_common.h>
#include <test-certs.h>
-#include <access_provider2.h>
+#include <scoped-app-context.h>
#include <ckm-common.h>
#include <ckm-privileged-common.h>
CKM::Alias certimAlias("CertIM");
{
ScopedDBUnlock unlock(USER_TEST, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST, GROUP_APP);
auto manager = CKM::Manager::create();
RUNNER_ASSERT(CKM_API_SUCCESS == manager->saveCertificate(certeeAlias, certee, CKM::Policy()));
// actual test
{
ScopedDBUnlock unlock(USER_TEST, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST, GROUP_APP);
auto manager = CKM::Manager::create();
int status1 = manager->saveCertificate(certeeAlias, certee, CKM::Policy());
{
unlock_user_data(USER_TEST+1, "t170-special-password");
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST+1, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST+1, GROUP_APP);
}
RUNNER_CHILD_TEST(T1702_insert_data)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST+1, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST+1, GROUP_APP);
auto certee = TestData::getTestCertificate(TestData::THIRD_PARTY_LEAF);
RUNNER_CHILD_TEST(T1704_data_test)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST+1, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST+1, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17102_prep_data_01)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST+2, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST+2, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17103_prep_data_02)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL_2, USER_TEST+2, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+2, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17104_prep_data_03)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST+3, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST+3, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17105_prep_data_04)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL_2, USER_TEST+3, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+3, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17107_check_data_01)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST+2, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST+2, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17108_check_data_02)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL_2, USER_TEST+2, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+2, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17110_check_data_03)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL, USER_TEST+3, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_TEST+3, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
RUNNER_CHILD_TEST(T17111_check_data_04)
{
int temp;
- ScopedAccessProvider ap(TEST_LABEL_2, USER_TEST+3, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_TEST+3, GROUP_APP);
CKM::AliasVector av;
auto manager = CKM::Manager::create();
--- /dev/null
+/*
+ * Copyright (c) 2013 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file scoped-app-context.cpp
+ * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version 1.0
+ * @brief RAII guard for pretending to be an app (Smack, uid, gid).
+ */
+
+#include <ckm-common.h>
+#include <dpl/test/test_exception.h>
+#include <dpl/test/test_runner.h>
+#include <scoped-app-context.h>
+
+#include <unistd.h>
+
+ScopedAppContext::ScopedAppContext(const std::string& owner, uid_t user, gid_t group)
+ : m_context(SMACK_USER_APP_PREFIX + owner)
+ , m_oldUser(getuid())
+ , m_oldGroup(getgid())
+{
+ m_context.allowAccessFrom("System", "w");
+ m_context.allowAccessTo("System", "w");
+ m_context.allowAccessTo("System::Run", "wx"); // Necessary for logging with journald
+
+ m_context.applyRules();
+ m_processLabel = std::make_unique<ScopedProcessLabel>(m_context.applyLabelScoped());
+ m_context.applyUserSwitchEffective(user, group);
+}
+
+ScopedAppContext::~ScopedAppContext()
+{
+ try {
+ m_context.applyUserSwitchEffective(m_oldUser, m_oldGroup);
+ m_context.revokeRules();
+ } catch (const DPL::Test::TestException& e) {
+ RUNNER_ERROR_MSG("Exception in ScopedAppContext destructor: " << e.GetMessage());
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2013 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file scoped-app-context.h
+ * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version 1.0
+ * @brief RAII guard for pretending to be an app (Smack, uid, gid).
+ */
+
+#pragma once
+
+#include <app_context.h>
+#include <scoped_process_label.h>
+
+#include <memory>
+#include <string>
+
+#include <sys/types.h>
+
+class ScopedAppContext {
+public:
+ ScopedAppContext(const std::string& owner, uid_t user, gid_t group);
+ ScopedAppContext(const ScopedAppContext&) = delete;
+ ScopedAppContext& operator=(const ScopedAppContext&) = delete;
+ ~ScopedAppContext();
+
+private:
+ AppContext m_context;
+ std::unique_ptr<ScopedProcessLabel> m_processLabel;
+ uid_t m_oldUser;
+ gid_t m_oldGroup;
+};
#include <ckm/ckm-control.h>
#include <ckmc/ckmc-manager.h>
#include <ckmc/ckmc-type.h>
-#include <access_provider2.h>
+#include <scoped-app-context.h>
#include <unistd.h>
#include <sys/types.h>
// [test]
{
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_LOCKED);
}
{
remove_user_data(USER_APP);
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
ScopedSaveData ssd(TEST_ALIAS, TEST_DATA);
check_read(TEST_ALIAS, TEST_LABEL, TEST_DATA);
// [test]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
save_data(aliasWithLabel(SYSTEM_LABEL, TEST_ALIAS).c_str(), TEST_DATA, CKMC_ERROR_PERMISSION_DENIED);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
// [test]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
}
// [test]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
}
// [test]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
}
// [test2]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_ALIAS_UNKNOWN);
}
// [test]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
ScopedSaveData ssdsystem_user(TEST_ALIAS, TEST_DATA);
ScopedSaveData ssdsystem_system(TEST_SYSTEM_ALIAS.c_str(), TEST_DATA, CKMC_ERROR_PERMISSION_DENIED);
// [test]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
check_remove_denied(TEST_SYSTEM_ALIAS.c_str());
}
// [test2]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
ScopedSaveData user_data(TEST_ALIAS, TEST_DATA);
check_alias_list({TEST_SYSTEM_ALIAS.c_str(),
// [test]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
std::string private_key_alias = aliasWithLabel(SYSTEM_LABEL, "sys-db-priv");
std::string public_key_alias = aliasWithLabel(SYSTEM_LABEL, "sys-db-pub");
// [test2]
{
ScopedDBUnlock unlock(USER_APP, APP_PASS);
- ScopedAccessProvider ap(TEST_LABEL, USER_APP, GROUP_APP);
+ ScopedAppContext ctx(TEST_LABEL, USER_APP, GROUP_APP);
ckmc_hash_algo_e hash_algo = CKMC_HASH_SHA256;
ckmc_rsa_padding_algo_e pad_algo = CKMC_PKCS1_PADDING;
// [test]
{
- ScopedAccessProvider ap(TEST_LABEL_2, USER_SERVICE_2, GROUP_SERVICE_2);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_SERVICE_2, GROUP_SERVICE_2);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
}
// [prepare]
{
- ScopedAccessProvider ap(TEST_LABEL_2, USER_SERVICE_2, GROUP_SERVICE_2);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_SERVICE_2, GROUP_SERVICE_2);
// [test]
ScopedSaveData ssd(TEST_SYSTEM_ALIAS.c_str(), TEST_DATA);
// [test]
{
- ScopedAccessProvider ap(TEST_LABEL_2, USER_SERVICE_MAX, GROUP_SERVICE_MAX);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_SERVICE_MAX, GROUP_SERVICE_MAX);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA);
}
// [test]
{
- ScopedAccessProvider ap(TEST_LABEL_2, USER_SERVICE_FAIL, GROUP_SERVICE_FAIL);
+ ScopedAppContext ctx(TEST_LABEL_2, USER_SERVICE_FAIL, GROUP_SERVICE_FAIL);
check_read(TEST_ALIAS, SYSTEM_LABEL, TEST_DATA, CKMC_ERROR_DB_LOCKED);
}
#files to compile
SET(COMMON_TARGET_TEST_SOURCES
${PROJECT_SOURCE_DIR}/src/common/tests_common.cpp
- ${PROJECT_SOURCE_DIR}/src/common/access_provider.cpp
+ ${PROJECT_SOURCE_DIR}/src/common/app_context.cpp
${PROJECT_SOURCE_DIR}/src/common/smack_access.cpp
${PROJECT_SOURCE_DIR}/src/common/dbus_connection.cpp
${PROJECT_SOURCE_DIR}/src/common/dbus_message_in.cpp
+++ /dev/null
-/*
- * Copyright (c) 2013 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * @file access_provider.cpp
- * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
- * @version 1.0
- * @brief Common functions and macros used in security-tests package.
- */
-#include <sys/types.h>
-#include <unistd.h>
-#include <sys/smack.h>
-
-#include <map>
-
-#include <tests_common.h>
-
-#include <access_provider.h>
-#include <scoped_process_label.h>
-
-namespace SecurityServer {
-
-AccessProvider::AccessProvider(const std::string &myLabel)
- : m_myLabel(myLabel)
-{}
-
-void AccessProvider::allowSS() {
- m_smackAccess.add(m_myLabel, "System::Run", "x");
-}
-
-void AccessProvider::addSubjectRule(const std::string &subject, const std::string &rule) {
- m_smackAccess.add(subject, m_myLabel, rule);
-}
-
-void AccessProvider::addObjectRule(const std::string &object, const std::string &rule) {
- m_smackAccess.add(m_myLabel, object, rule);
-}
-
-void AccessProvider::apply() {
- m_smackAccess.apply();
-}
-
-void AccessProvider::applyAndSwithToUser(int uid, int gid) {
- RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_myLabel.c_str()),
- "Error in smack_revoke_subject(" << m_myLabel << ")");
- apply();
- ScopedProcessLabel spl(m_myLabel, false);
- RUNNER_ASSERT_MSG(0 == setgid(gid),
- "Error in setgid.");
- RUNNER_ASSERT_MSG(0 == setuid(uid),
- "Error in setuid.");
-}
-
-} // namespace SecurityServer
-
--- /dev/null
+/*
+ * Copyright (c) 2013 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file app_context.cpp
+ * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version 1.0
+ * @brief Common functions and macros used in security-tests package.
+ */
+
+#include <app_context.h>
+#include <scoped_process_label.h>
+#include <tests_common.h>
+
+#include <map>
+
+#include <sys/smack.h>
+#include <unistd.h>
+
+AppContext::AppContext(const std::string& label)
+ : m_label(label)
+{
+}
+
+void AppContext::allowAccessFrom(const std::string& subject, const std::string& rule)
+{
+ m_smackAccess.add(subject, m_label, rule);
+}
+
+void AppContext::allowAccessTo(const std::string& object, const std::string& rule)
+{
+ m_smackAccess.add(m_label, object, rule);
+}
+
+void AppContext::apply(uid_t user, gid_t group)
+{
+ revokeAccessToAll();
+ applyRules();
+ applyLabel();
+ applyUserSwitch(user, group);
+}
+
+void AppContext::applyLabel()
+{
+ ScopedProcessLabel spl(m_label, false);
+}
+
+ScopedProcessLabel AppContext::applyLabelScoped()
+{
+ return ScopedProcessLabel(m_label, true);
+}
+
+void AppContext::applyRules()
+{
+ m_smackAccess.apply();
+}
+
+void AppContext::applyUserSwitch(uid_t user, gid_t group)
+{
+ RUNNER_ASSERT_MSG(0 == setgid(group), "Error in setgid.");
+ RUNNER_ASSERT_MSG(0 == setuid(user), "Error in setuid.");
+}
+
+void AppContext::applyUserSwitchEffective(uid_t user, gid_t group)
+{
+ RUNNER_ASSERT_MSG(0 == setegid(group), "Error in setegid.");
+ RUNNER_ASSERT_MSG(0 == seteuid(user), "Error in seteuid.");
+}
+
+void AppContext::revokeAccessToAll()
+{
+ RUNNER_ASSERT_MSG(0 == smack_revoke_subject(m_label.c_str()),
+ "Error in smack_revoke_subject(" << m_label << ")");
+}
+
+void AppContext::revokeRules()
+{
+ m_smackAccess.clear();
+}
/*
- * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2013 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
/*
- * @file access_provider.h
+ * @file app_context.h
* @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
* @version 1.0
* @brief Common functions and macros used in security-tests package.
*/
-#ifndef _ACCESS_FOR_DUMMIES_H_
-#define _ACCESS_FOR_DUMMIES_H_
-#include <string>
+#pragma once
+#include <scoped_process_label.h>
#include <smack_access.h>
-namespace SecurityServer {
-
-class AccessProvider {
-public:
- AccessProvider(const std::string &myLabel);
+#include <string>
- AccessProvider(const AccessProvider &second) = delete;
- AccessProvider& operator=(const AccessProvider &second) = delete;
+#include <sys/types.h>
- void addSubjectRule(const std::string &subject, const std::string &rule);
- void addObjectRule(const std::string &object, const std::string &rule);
- void allowSS();
- void apply();
- void applyAndSwithToUser(int uid, int gid);
+class AppContext {
+public:
+ AppContext(const std::string& label);
+ AppContext(const AppContext&) = delete;
+ AppContext& operator=(const AppContext&) = delete;
+
+ void allowAccessFrom(const std::string& subject, const std::string& rule);
+ void allowAccessTo(const std::string& object, const std::string& rule);
+
+ void apply(uid_t user, gid_t group);
+ void applyLabel();
+ ScopedProcessLabel applyLabelScoped();
+ void applyRules();
+ void applyUserSwitch(uid_t user, gid_t group);
+ void applyUserSwitchEffective(uid_t user, gid_t group);
+ void revokeAccessToAll();
+ void revokeRules();
- virtual ~AccessProvider(){}
private:
- std::string m_myLabel;;
+ std::string m_label;
SmackAccess m_smackAccess;
};
-
-} // namespace SecurityServer
-
-#endif // _ACCESS_FOR_DUMMIES_H_
-
/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
smackSetLabelForSelf(m_label);
}
+ScopedProcessLabel::ScopedProcessLabel(ScopedProcessLabel&& other)
+ : m_label(std::move(other.m_label))
+ , m_originalLabel(std::move(other.m_originalLabel))
+ , m_originalOnlycap(std::move(other.m_originalOnlycap))
+{
+ other.m_originalLabel.clear();
+}
+
+ScopedProcessLabel& ScopedProcessLabel::operator=(ScopedProcessLabel&& other)
+{
+ m_label = std::move(other.m_label);
+ m_originalLabel = std::move(other.m_originalLabel);
+ m_originalOnlycap = std::move(other.m_originalOnlycap);
+
+ other.m_originalLabel.clear();
+ return *this;
+}
+
ScopedProcessLabel::~ScopedProcessLabel()
{
// it has to be restored
/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
public:
// if restore == true the original label will be restored
explicit ScopedProcessLabel(std::string label, bool restore = true);
+ ScopedProcessLabel(ScopedProcessLabel&& other);
+ ScopedProcessLabel& operator=(ScopedProcessLabel&& other);
~ScopedProcessLabel();
private:
/*
- * Copyright (c) 2015-2019 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <systemd/sd-bus.h>
#include <tests_common.h>
-#include <access_provider.h>
+#include <app_context.h>
#include <dpl/test/test_runner.h>
#include <memory.h>
#include <synchronization_pipe.h>
void udsServer(SynchronizationPipe &pipe, const struct sockaddr_un &sockaddr,
const struct ProcessCredentials &peerCredentials) {
- SecurityServer::AccessProvider ap(peerCredentials.label());
- ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
+ AppContext ctx(peerCredentials.label());
+ ctx.apply(peerCredentials.uid(), peerCredentials.gid());
pipe.claimChildEp();
int sock = UDSHelpers::createServer(&sockaddr);
void dbusServer(SynchronizationPipe &pipe, const std::string &requestedName,
const ProcessCredentials &peerCredentials) {
- // for DBus connection, System must have access to our peer creds as well.
- SecurityServer::AccessProvider systemAp("System");
- systemAp.addObjectRule(peerCredentials.label(), "rwx");
- systemAp.apply();
-
- SecurityServer::AccessProvider ap(peerCredentials.label());
- ap.addObjectRule("System", "w");
- ap.addObjectRule("System::Run", "x");
- ap.addObjectRule("System::Shared", "rwx"); // for GDB
- ap.addSubjectRule("System::Privileged", "rwx"); // for piping
- ap.addObjectRule("System::Privileged", "rwx"); // for GDB and piping
- ap.addObjectRule("User", "r"); // for /usr/lib/debug access
- ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
+ AppContext ctx(peerCredentials.label());
+ ctx.allowAccessFrom("System", "rwx"); // for DBus connection
+ ctx.allowAccessFrom("System::Privileged", "rwx"); // for piping
+ ctx.allowAccessTo("System", "w");
+ ctx.allowAccessTo("System::Privileged", "rwx"); // for GDB and piping
+ ctx.allowAccessTo("System::Run", "x");
+ ctx.allowAccessTo("System::Shared", "rwx"); // for GDB
+ ctx.allowAccessTo("User", "r"); // for /usr/lib/debug access
+ ctx.apply(peerCredentials.uid(), peerCredentials.gid());
pipe.claimChildEp();
auto conn = createDBusConnection(requestedName);
/*
- * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <sys/un.h>
#include <unistd.h>
-#include <access_provider.h>
+#include <app_context.h>
#include <cynara_helpers_creds.h>
#include <dpl/test/test_runner.h>
#include <label_generator.h>
void udsServer(SynchronizationPipe &pipe, const struct sockaddr_un &sockaddr,
const struct ProcessCredentials &peerCredentials) {
- SecurityServer::AccessProvider ap(peerCredentials.label());
- ap.applyAndSwithToUser(peerCredentials.uid(), peerCredentials.gid());
+ AppContext ctx(peerCredentials.label());
+ ctx.apply(peerCredentials.uid(), peerCredentials.gid());
pipe.claimChildEp();
int sock = UDSHelpers::createServer(&sockaddr);