summaryrefslogtreecommitdiff
path: root/src/common/include/smack-rules.h
blob: 8398ee095b0c17d9e53a12c23382ddbba27df1cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *  Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Contact: Rafal Krypa <r.krypa@samsung.com>
 *
 *  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        smack-rules.h
 * @author      Jacek Bukarewicz <j.bukarewicz@samsung.com>
 * @version     1.0
 * @brief       Header file of a class managing smack rules
 *
 */
#pragma once

#include <vector>
#include <string>
#include <smack-exceptions.h>

struct smack_accesses;

namespace SecurityManager {

class SmackRules
{
public:
    typedef std::vector<std::string> Rule;
    typedef std::vector<Rule> RuleVector;
    typedef std::vector<std::string> Pkgs;
    typedef std::vector<std::string> Labels;
    typedef std::vector<std::pair<std::string, std::vector<std::string>>> PkgsApps;

    SmackRules();
    virtual ~SmackRules();

    void add(const std::string &subject, const std::string &object,
            const std::string &permissions);
    void addModify(const std::string &subject, const std::string &object,
            const std::string &allowPermissions, const std::string &denyPermissions);

    void addFromTemplate(
            const RuleVector &templateRules,
            const std::string &appProcessLabel,
            const std::string &pkgName,
            const int authorId);

    void addFromTemplateFile(
            const std::string &templatePath,
            const std::string &appProcessLabel,
            const std::string &pkgName,
            const int authorId);

    void apply() const;
    void clear() const;

    /**
     * Create cross dependencies for all applications in a package
     *
     * This is needed for all applications within a package to have
     * correct permissions to shared data.
     *
     * @param[in] pkgLabels - a list of process labels of all applications inside this package
     */
    void generatePackageCrossDeps(const Labels &pkgLabels);

    /**
     * Install package-specific smack rules plus add rules for specified external apps.
     *
     * Function creates smack rules using predefined template. Rules are applied
     * to the kernel.
     *
     * @param[in] appProcessLabel - process label of the application
     * @param[in] pkgName - package identifier
     * @param[in] authorId - author id of application
     * @param[in] pkgLabels - a list of process labels of all applications inside this package
     */
    static void installApplicationRules(
            const std::string &appProcessLabel,
            const std::string &pkgName,
            const int authorId,
            const Labels &pkgLabels);

    /**
     * Uninstall package-specific smack rules.
     *
     * Function loads package-specific smack rules, revokes them from the kernel.
     *
     * @param[in] pkgName - package identifier
     * @param[in] pkgLabels - a list of process labels of all applications inside this package
     */
    static void uninstallPackageRules(const std::string &pkgName, const Labels &pkgLabels);

    /**
    * Uninstall application-specific smack rules.
    *
    * Function removes application specific rules from the kernel.
    *
    * @param[in] appProcessLabel - application process label
    * @param[in] pkgName - package identifier that the application is in
    * @param[in] authorId - identification (datbase key) of the author
    */
    static void uninstallApplicationRules(const std::string &appProcessLabel, const std::string &pkgName, const int authorId);

    /**
     * Update package specific rules
     *
     * This function regenerates all package rules that
     * need to exist currently for all application in that
     * package
     *
     * @param[in] pkgName - package identifier that the application is in
     * @param[in] pkgContents - list of all applications in the package
     */
    static void updatePackageRules(
            const std::string &pkgName,
            const std::vector<std::string> &pkgContents);

    /**
     * Uninstall author-specific smack rules.
     *
     * param[in] authorId - identification (datbase key) of the author
     */
    static void uninstallAuthorRules(const int authorId);

    /**
     * Add rules related to private path sharing rules
     *
     * This function generates and applies rules needed to apply private sharing.
     * If isPathSharedAlready, no rule for owner, User or System to path label will be applied.
     * If isTargetSharingAlready, no rule for directory traversing is set for target.
     *
     * @param[in] ownerPkgName - package identifier of path owner
     * @param[in] ownerPkgLabels - vector of process labels of applications contained in package
     *                             which owner application belongs to
     * @param[in] targetAppLabel - process label of the target application
     * @param[in] pathLabel - a list of all applications in the package
     * @param[in] isPathSharedAlready - flag indicated, if path has been shared before
     * @param[in] isTargetSharingAlready - flag indicated, if target is already sharing anything
     *                                     with owner
     */
    static void applyPrivateSharingRules(const std::string &ownerPkgName,
                                         const SmackRules::Labels &ownerPkgLabels,
                                         const std::string &targetAppLabel,
                                         const std::string &pathLabel,
                                         bool isPathSharedAlready,
                                         bool isTargetSharingAlready);
    /**
     * Remove rules related to private path sharing rules
     *
     * This function generates and applies rules needed to apply private sharing.
     * If isPathSharedNoMore, rules for owner package contents, User or System to path label will
     * be removed.
     * If isTargetSharingNoMore, rule for directory traversing is removed for target.
     *
     * @param[in] ownerPkgName - package identifier of path owner
     * @param[in] ownerPkgLabels - vector of process labels of applications contained in package
     *                             which owner application belongs to
     * @param[in] targetAppLabel - process label of the target application
     * @param[in] pathLabel - a list of all applications in the package
     * @param[in] isPathSharedNoMore - flag indicated, if path is not shared anymore
     * @param[in] isTargetSharingNoMore - flag indicated, if target is not sharing anything
     *                                    with owner
     */
    static void dropPrivateSharingRules(const std::string &ownerPkgName,
                                        const Labels &ownerPkgLabels,
                                        const std::string &targetAppLabel,
                                        const std::string &pathLabel,
                                        bool isPathSharedNoMore,
                                        bool isTargetSharingNoMore);

private:
    static void useTemplate(
            const std::string &templatePath,
            const std::string &appProcessLabel,
            const std::string &pkgName,
            const int authorId = -1);

    /**
     * Helper method: replace all occurrences of \ref needle in \ref haystack
     * with \ref replace.
     *
     * @param[in,out] haystack string to modify
     * @param needle string to find in \ref haystack
     * @param replace string to replace \ref needle with
     */
    static void strReplace(std::string &haystack, const std::string &needle,
            const std::string &replace);

    smack_accesses *m_handle;

    /**
     * Revoke rules for which label of given \ref appName is a subject.
     *
     * @param[in] appProcessLabel = application process label
     */
    static void revokeAppSubject(const std::string &appProcessLabel);
};

} // namespace SecurityManager