summaryrefslogtreecommitdiff
path: root/src/common/permissible-set.cpp
blob: cf53100abda7f1a979b1e54e90b3d0029b854162 (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
/*
 * Copyright (c) 2016-2023 Samsung Electronics Co., Ltd. All rights reserved
 *
 * This file is licensed under the terms of MIT License or the Apache License
 * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
 * See the LICENSE file or the notice below for Apache License Version 2.0
 * details.
 *
 * 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        permissible-set.cpp
 * @author      Rafal Krypa <r.krypa@samsung.com>
 * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
 * @version     1.0
 * @brief       Implementation of API for adding, deleting and reading permissible names
 * @brief       (names of installed applications)
 */
#ifndef _GNU_SOURCE //for TEMP_FAILURE_RETRY
#define _GNU_SOURCE
#endif

#include <cstdio>
#include <cstring>
#include <fstream>
#include <linux/xattr.h>
#include <memory>
#include <pwd.h>
#include <string>
#include <sys/file.h>
#include <sys/smack.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <config.h>
#include <dpl/errno_string.h>
#include <dpl/exception.h>
#include <dpl/fstream_accessors.h>
#include <dpl/log/log.h>
#include <filesystem.h>
#include <permissible-set.h>
#include <privilege_db.h>
#include <security-manager-types.h>
#include <smack-labels.h>
#include <tzplatform_config.h>

#include "tzplatform-config.h"

namespace SecurityManager {
namespace PermissibleSet {

template <typename T>
static inline int getFd(T &fstream)
{
    return DPL::FstreamAccessors<T>::GetFd(fstream);
}

template <typename T>
static void openAndLockNameFile(const std::string &nameFile, T &fstream)
{
    fstream.open(nameFile);
    if (!fstream.is_open())
        LogAndThrowErrno(PermissibleSetException::FileOpenError, "open file " << nameFile);

    if (TEMP_FAILURE_RETRY(flock(getFd(fstream), LOCK_EX)))
        LogAndThrowErrno(PermissibleSetException::FileLockError, "lock file " << nameFile);
}

std::string getPermissibleFileLocation(uid_t uid, int installationType)
{
    TizenPlatformConfig tpc(uid);
    if ((installationType == SM_APP_INSTALL_GLOBAL)
            || (installationType == SM_APP_INSTALL_PRELOADED))
        return std::string(LOCAL_STATE_INSTALL_DIR) + "/" + APPS_LABELS_FILE;
    else
        return std::string(LOCAL_STATE_INSTALL_DIR) + "/" + std::to_string(uid)
            + "/" + APPS_LABELS_FILE;
}

static void markPermissibleFileValid(int fd, const std::string &nameFile, bool valid)
{
    const auto mode = valid ? 00444 : 00000;
    if (TEMP_FAILURE_RETRY(fchmod(fd, mode)))
        LogAndThrowErrno(PermissibleSetException::FileWriteError, "fchmod " << nameFile);
}

void updatePermissibleFile(uid_t uid, int installationType,
                           const std::vector<std::string> &labelsToAddForUser,
                           const std::vector<std::string> &labelsToRemoveForUser)
{
    std::string nameFile = getPermissibleFileLocation(uid, installationType);
    std::vector<std::string> labelsForUser;
    {
        std::ifstream fstream;
        try {
            openAndLockNameFile(nameFile, fstream);

            std::string line;
            while (std::getline(fstream, line)) {
                bool flag = true;
                for (auto &l : labelsToRemoveForUser)
                    if (l == line) {
                        flag = false;
                        break;
                    }
                if (flag)
                    labelsForUser.push_back(line);
            }
        } catch (const PermissibleSet::PermissibleSetException::FileOpenError &e) {
            LogDebug("File doesn't exist yet, needs to be created - just continue; error:" << e.DumpToString());
        }
        for (auto &l : labelsToAddForUser)
            labelsForUser.push_back(l);
    }

    std::ofstream fstream;
    openAndLockNameFile(nameFile, fstream);
    markPermissibleFileValid(getFd(fstream), nameFile, false);

    for (auto &label : labelsForUser) {
        fstream << label << '\n';
        if (fstream.bad())
            LogAndThrowErrno(PermissibleSetException::PermissibleSetException::FileWriteError,
                    "write to file " << nameFile);
    }
    if (fstream.flush().fail())
        LogAndThrowErrno(PermissibleSetException::FileWriteError, "fflush " << nameFile);
    if (fsync(getFd(fstream)) == -1)
        LogAndThrowErrno(PermissibleSetException::FileWriteError, "fsync " << nameFile);
    markPermissibleFileValid(getFd(fstream), nameFile, true);
}

void readLabelsFromPermissibleFile(const std::string &nameFile, std::vector<std::string> &appLabels)
{
    std::ifstream fstream;
    openAndLockNameFile(nameFile, fstream);

    std::string line;
    while (std::getline(fstream, line))
        appLabels.push_back(line);

    if (fstream.bad())
        LogAndThrowErrno(PermissibleSetException::FileReadError, "reading file " << nameFile);
}

void initializeUserPermissibleFile(uid_t uid)
{
    std::string nameFile = getPermissibleFileLocation(uid, SM_APP_INSTALL_LOCAL);
    std::string nameDir = FS::getDirectoryName(nameFile);

    if (mkdir(nameDir.c_str(), 0755) != 0 && errno != EEXIST)
        ThrowErrno(PermissibleSetException::FileInitError,
            "create directory for user permissible file");

    std::ofstream fstream;
    openAndLockNameFile(nameFile, fstream);
    SmackLabels::setSmackLabelForFd(getFd(fstream), "_");

    markPermissibleFileValid(getFd(fstream), nameFile, true);
}

void removeUserPermissibleFile(uid_t uid)
{
    std::string nameFile = getPermissibleFileLocation(uid, SM_APP_INSTALL_LOCAL);
    std::string nameDir = FS::getDirectoryName(nameFile);

    if (unlink(nameFile.c_str()) != 0 && errno != ENOENT)
        ThrowErrno(PermissibleSetException::FileRemoveError,
            "remove user permissible file");

    if (rmdir(nameFile.c_str()) != 0 && errno != ENOENT)
        ThrowErrno(PermissibleSetException::FileRemoveError,
            "remove directory for user permissible file");
}

} // PermissibleSet
} // SecurityManager