aboutsummaryrefslogtreecommitdiff
path: root/paludis/selinux/security_context.cc
blob: c30798907a50b4fc171ab9fff309dad5685d5f5d (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/util/log.hh>
#include <paludis/selinux/security_context.hh>

#include "config.h"

#ifdef HAVE_SELINUX

#include <dlfcn.h>
#include <selinux/selinux.h>

// I think the name explains it. C++ is picky about casting to function pointers.
#define STUPID_CAST(type, val) reinterpret_cast<type>(reinterpret_cast<uintptr_t>(val))

namespace {
    class _libselinux {
        private:
            void *_handle;

            void (*_freecon)(security_context_t);
            int (*_getcon)(security_context_t*);
            int (*_getfscreatecon)(security_context_t*);
            int (*_setfscreatecon)(security_context_t);
            int (*_matchpathcon)(const char *, mode_t, security_context_t*);
            int (*_matchpathcon_init)(const char *);

        public:
            _libselinux() :
                _handle(NULL), _freecon(NULL), _getcon(NULL),
                _getfscreatecon(NULL), _setfscreatecon(NULL),
                _matchpathcon(NULL), _matchpathcon_init(NULL)
            {
                _handle = dlopen("libselinux.so", RTLD_LAZY | RTLD_LOCAL);
                if (NULL != _handle)
                {
                    _freecon = STUPID_CAST(void (*)(security_context_t), dlsym(_handle, "freecon"));
                    _getcon = STUPID_CAST(int (*)(security_context_t*), dlsym(_handle, "getcon"));
                    _getfscreatecon = STUPID_CAST(int (*) (security_context_t*), dlsym(_handle, "getfscreatecon"));
                    _setfscreatecon = STUPID_CAST(int (*) (security_context_t), dlsym(_handle, "setfscreatecon"));
                    _matchpathcon = STUPID_CAST(int (*) (const char *, mode_t, security_context_t *),
                            dlsym(_handle, "matchpathcon"));
                    _matchpathcon_init = STUPID_CAST(int (*) (const char *), dlsym(_handle, "matchpathcon_init"));
                }
            }

            ~_libselinux()
            {
                if (NULL != _handle)
                    dlclose(_handle);
            }

            void freecon(security_context_t c)
            {
                if (NULL != _freecon)
                    _freecon(c);
            }

            int getcon(security_context_t *c)
            {
                if (NULL != _getcon)
                    return _getcon(c);
                return 0;
            }

            int getfscreatecon(security_context_t *c)
            {
                if (NULL != _getfscreatecon)
                    return _getfscreatecon(c);
                return 0;
            }

            int setfscreatecon(security_context_t c)
            {
                if (NULL != _setfscreatecon)
                    return _setfscreatecon(c);
                return 0;
            }

            int matchpathcon(const char *path, mode_t mode, security_context_t *con)
            {
                if (NULL != _matchpathcon)
                    return _matchpathcon(path, mode, con);
                return 0;
            }

            int matchpathcon_init(const char *path)
            {
                if (NULL != _matchpathcon_init)
                    return _matchpathcon_init(path);
                return 0;
            }
    } libselinux;
}

using namespace paludis;

namespace paludis
{
    template<>
    struct Implementation<SecurityContext> : InternalCounted<Implementation<SecurityContext> >
    {
        security_context_t _context;

        Implementation(security_context_t con)
            : _context(con)
        { }

        ~Implementation()
        {
            if (NULL != _context)
                libselinux.freecon(_context);
        }

        void set(security_context_t newcon)
        {
            if (NULL != _context)
                libselinux.freecon(_context);

            _context = newcon;
        }
    };
}

SecurityContext::SecurityContext()
    : PrivateImplementationPattern<SecurityContext>(new Implementation<SecurityContext>(NULL))
{
}

SecurityContext::~SecurityContext()
{
}

SecurityContext::ConstPointer SecurityContext::current_context()
{
    SecurityContext::Pointer p(new SecurityContext);
    security_context_t con;
    if (0 != libselinux.getcon(&con))
        throw SELinuxException("Couldn't get current security context.");
    p->_imp->set(con);
    return p;
}

SecurityContext::ConstPointer SecurityContext::fs_create_context()
{
    SecurityContext::Pointer p(new SecurityContext);
    security_context_t con;
    if (0 != libselinux.getfscreatecon(&con))
        throw SELinuxException("Couldn't get current filesystem creation context.");
    p->_imp->set(con);
    return p;
}

std::ostream & paludis::operator<<(std::ostream & os, const SecurityContext & context)
{
    os << static_cast<const char *>(context._imp->_context);
    return os;
}

FSCreateCon::FSCreateCon(SecurityContext::ConstPointer newfscreatecon)
    : _context(newfscreatecon), _prev_context(SecurityContext::fs_create_context())
{
    if (0 != libselinux.setfscreatecon(_context->_imp->_context))
        throw SELinuxException("Couldn't set filesystem creation context to '" + stringify(*_context) + "'.");
}

FSCreateCon::~FSCreateCon()
{
    if (0 != libselinux.setfscreatecon(_prev_context->_imp->_context))
        throw SELinuxException("Couldn't reset filesystem creation context to '" + stringify(*_prev_context) + "'.");
}

MatchPathCon::MatchPathCon()
{
    if (0 != libselinux.matchpathcon_init(NULL))
    {
        _good=false;
//        throw SELinuxException("Failed running matchpathcon_init.");
    }
    else
        _good=true;
}

MatchPathCon::~MatchPathCon()
{
}

bool MatchPathCon::good() const
{
    return _good;
}

SecurityContext::ConstPointer MatchPathCon::match(const std::string & path, mode_t mode) const
{
    SecurityContext::Pointer p(new SecurityContext);
    security_context_t context;
    if (0 != libselinux.matchpathcon(path.c_str(), mode, &context))
    {
        Log::get_instance()->message(ll_warning, "Couldn't get default security context for '" + path + "'.");
//        throw SELinuxException("Couldn't get default security context for '" + path + "'.");
    }
    else
    {
        p->_imp->set(context);
    }
    return p;
}

#endif