Fix only partial rollback of Smack rules
[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)
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 }
54
55 AccessProvider::AccessProvider(const std::string &ownerId, int uid, int gid)
56   : m_mySubject(toSmackLabel(ownerId))
57   , m_inSwitchContext(false)
58 {
59     RUNNER_ASSERT_MSG(m_mySubject.size() > 0, "No smack label provided to AccessProvider!");
60     allowJournaldLogs();
61     applyAndSwithToUser(uid, gid);
62 }
63
64 AccessProvider::~AccessProvider()
65 {
66
67 }
68
69 void AccessProvider::allowAPI(const std::string &api, const std::string &rule) {
70     m_smackAccess.add(m_mySubject, api, rule);
71 }
72
73 void AccessProvider::apply() {
74     // This should be done by security-manager
75     m_smackAccess.add("System", m_mySubject, "w");
76     m_smackAccess.add(m_mySubject, "System", "w");
77     m_smackAccess.apply();
78 }
79
80 void AccessProvider::applyAndSwithToUser(int uid, int gid)
81 {
82     RUNNER_ASSERT_MSG(m_inSwitchContext == false, "already switched context");
83
84     clear();
85     apply();
86
87     m_processLabel.reset(new ScopedProcessLabel(m_mySubject));
88
89     m_origUid = getuid();
90     m_origGid = getgid();
91     RUNNER_ASSERT_MSG(0 == setegid(gid),
92         "Error in setgid.");
93     RUNNER_ASSERT_MSG(0 == seteuid(uid),
94         "Error in setuid.");
95     m_inSwitchContext = true;
96 }
97
98 void AccessProvider::clear() {
99     m_smackAccess.clear();
100 }
101
102 void AccessProvider::allowJournaldLogs() {
103     allowAPI("System::Run","wx"); // necessary for logging with journald
104 }
105
106 ScopedAccessProvider::~ScopedAccessProvider()
107 {
108     if(m_inSwitchContext == true)
109     {
110         RUNNER_ASSERT_MSG(0 == setegid(m_origGid), "Error in setgid.");
111         RUNNER_ASSERT_MSG(0 == seteuid(m_origUid), "Error in setuid.");
112         clear();
113         m_processLabel.reset();
114         m_inSwitchContext = false;
115     }
116 }