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
|
/*
* Copyright (C) 2015 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program 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, see <http://www.gnu.org/licenses/>.
*
* Authored by: James Henstridge <james.henstridge@canonical.com>
*/
#include <internal/check_access.h>
#include <internal/safe_strerror.h>
#include <errno.h>
#include <stdexcept>
#include <sys/apparmor.h>
using namespace std;
namespace
{
enum class Access
{
write = (1 << 1),
read = (1 << 2)
};
#ifndef AA_CLASS_FILE
// Prior to Wily, /usr/include/sys/apparmor.h did not define this.
#define AA_CLASS_FILE 2
#endif
char const aa_class = AA_CLASS_FILE;
bool query_file(Access access, string const& label, string const& path)
{
static bool enabled = aa_is_enabled();
if (!enabled)
{
// If AppArmor is not enabled, assume access is granted.
return true; // LCOV_EXCL_LINE
}
string query(AA_QUERY_CMD_LABEL, AA_QUERY_CMD_LABEL_SIZE);
query += label;
query += '\0';
query += aa_class;
query += path;
int allowed = 0, audited = 0;
if (aa_query_label(static_cast<uint32_t>(access), const_cast<char*>(query.data()), query.size(), &allowed, &audited) < 0)
{
using namespace unity::thumbnailer::internal;
throw runtime_error("query_file(): Could not query AppArmor access: " + safe_strerror(errno)); // LCOV_EXCL_LINE
}
return allowed;
}
}
namespace unity
{
namespace thumbnailer
{
namespace internal
{
bool apparmor_can_read(string const& apparmor_label, string const& path)
{
return query_file(Access::read, apparmor_label, path);
}
} // namespace internal
} // namespace thumbnailer
} // namespace unity
|