Remove key-manager::api-storage privilege use in CKM tests
[platform/core/test/security-tests.git] / src / ckm / privileged / access_provider2.cpp
1 /*
2  * Copyright (c) 2013 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        access_provider.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @author      Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
20  * @version     1.0
21  * @brief       Common functions and macros used in security-tests package.
22  */
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <sys/smack.h>
26
27 #include <access_provider2.h>
28 #include <tests_common.h>
29 #include <ckm-common.h>
30 #include <scoped_process_label.h>
31
32 namespace {
33
34 std::string toSmackLabel(const std::string &ownerId) {
35     if (ownerId.empty())
36         return ownerId;
37
38     if (ownerId[0] == '/') {
39         return ownerId.substr(1, std::string::npos);
40     }
41
42     return SMACK_USER_APP_PREFIX + ownerId;
43 }
44
45 } // anonymous namespace
46
47 AccessProvider::AccessProvider(const std::string &ownerId, int uid, int gid)
48   : m_mySubject(toSmackLabel(ownerId))
49   , m_inSwitchContext(false)
50 {
51     RUNNER_ASSERT_MSG(m_mySubject.size() > 0, "No smack label provided to AccessProvider!");
52     allowJournaldLogs();
53     applyAndSwithToUser(uid, gid);
54 }
55
56 AccessProvider::~AccessProvider()
57 {
58
59 }
60
61 void AccessProvider::allowAPI(const std::string &api, const std::string &rule) {
62     m_smackAccess.add(m_mySubject, api, rule);
63 }
64
65 void AccessProvider::apply() {
66     // This should be done by security-manager
67     m_smackAccess.add("System", m_mySubject, "w");
68     m_smackAccess.add(m_mySubject, "System", "w");
69     m_smackAccess.apply();
70 }
71
72 void AccessProvider::applyAndSwithToUser(int uid, int gid)
73 {
74     RUNNER_ASSERT_MSG(m_inSwitchContext == false, "already switched context");
75
76     clear();
77     apply();
78
79     m_processLabel.reset(new ScopedProcessLabel(m_mySubject));
80
81     m_origUid = getuid();
82     m_origGid = getgid();
83     RUNNER_ASSERT_MSG(0 == setegid(gid),
84         "Error in setgid.");
85     RUNNER_ASSERT_MSG(0 == seteuid(uid),
86         "Error in setuid.");
87     m_inSwitchContext = true;
88 }
89
90 void AccessProvider::clear() {
91     m_smackAccess.clear();
92 }
93
94 void AccessProvider::allowJournaldLogs() {
95     allowAPI("System::Run","wx"); // necessary for logging with journald
96 }
97
98 ScopedAccessProvider::~ScopedAccessProvider()
99 {
100     if(m_inSwitchContext == true)
101     {
102         RUNNER_ASSERT_MSG(0 == setegid(m_origGid), "Error in setgid.");
103         RUNNER_ASSERT_MSG(0 == seteuid(m_origUid), "Error in setuid.");
104         clear();
105         m_processLabel.reset();
106         m_inSwitchContext = false;
107     }
108 }