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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* Copyright (c) 2014
* Canonical, Ltd. (All rights reserved)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License 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, contact Novell, Inc. or Canonical
* Ltd.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/apparmor.h>
#include <iomanip>
#include <string>
#include <sstream>
#include <map>
#include "parser.h"
#include "profile.h"
#include "parser_yacc.h"
#include "signal.h"
#define MAXMAPPED_SIG 35
#define MINRT_SIG 128 /* base of RT sigs */
#define MAXRT_SIG 32 /* Max RT above MINRT_SIG */
/* Signal names mapped to and internal ordering */
static struct signal_map { const char *name; int num; } signal_map[] = {
{"hup", 1},
{"int", 2},
{"quit", 3},
{"ill", 4},
{"trap", 5},
{"abrt", 6},
{"bus", 7},
{"fpe", 8},
{"kill", 9},
{"usr1", 10},
{"segv", 11},
{"usr2", 12},
{"pipe", 13},
{"alrm", 14},
{"term", 15},
{"stkflt", 16},
{"chld", 17},
{"cont", 18},
{"stop", 19},
{"stp", 20},
{"ttin", 21},
{"ttou", 22},
{"urg", 23},
{"xcpu", 24},
{"xfsz", 25},
{"vtalrm", 26},
{"prof", 27},
{"winch", 28},
{"io", 29},
{"pwr", 30},
{"sys", 31},
{"emt", 32},
{"exists", 35},
/* terminate */
{NULL, 0}
};
/* this table is ordered post sig_map[sig] mapping */
static const char *const sig_names[MAXMAPPED_SIG + 1] = {
"unknown",
"hup",
"int",
"quit",
"ill",
"trap",
"abrt",
"bus",
"fpe",
"kill",
"usr1",
"segv",
"usr2",
"pipe",
"alrm",
"term",
"stkflt",
"chld",
"cont",
"stop",
"stp",
"ttin",
"ttou",
"urg",
"xcpu",
"xfsz",
"vtalrm",
"prof",
"winch",
"io",
"pwr",
"sys",
"emt",
"lost",
"unused",
"exists", /* always last existence test mapped to MAXMAPPED_SIG */
};
int parse_signal_perms(const char *str_perms, perm32_t *perms, int fail)
{
return parse_X_perms("signal", AA_VALID_SIGNAL_PERMS, str_perms, perms, fail);
}
int find_signal_mapping(const char *sig)
{
if (strncmp("rtmin+", sig, 6) == 0) {
char *end;
unsigned long n = strtoul(sig + 6, &end, 10);
if (end == sig || n > MAXRT_SIG)
return -1;
return MINRT_SIG + n;
} else {
for (int i = 0; signal_map[i].name; i++) {
if (strcmp(sig, signal_map[i].name) == 0)
return signal_map[i].num;
}
}
return -1;
}
void signal_rule::extract_sigs(struct value_list **list)
{
struct value_list *entry, *tmp, *prev = NULL;
list_for_each_safe(*list, entry, tmp) {
int i = find_signal_mapping(entry->value);
if (i != -1) {
signals.insert(i);
list_remove_at(*list, prev, entry);
free_value_list(entry);
} else {
yyerror("unknown signal \"%s\"\n", entry->value);
prev = entry;
}
}
}
void signal_rule::move_conditionals(struct cond_entry *conds)
{
struct cond_entry *cond_ent;
list_for_each(conds, cond_ent) {
/* for now disallow keyword 'in' (list) */
if (!cond_ent->eq)
yyerror("keyword \"in\" is not allowed in signal rules\n");
if (strcmp(cond_ent->name, "set") == 0) {
extract_sigs(&cond_ent->vals);
} else if (strcmp(cond_ent->name, "peer") == 0) {
move_conditional_value("signal", &peer_label, cond_ent);
} else {
yyerror("invalid signal rule conditional \"%s\"\n",
cond_ent->name);
}
}
}
signal_rule::signal_rule(perm32_t perms_p, struct cond_entry *conds):
perms_rule_t(AA_CLASS_SIGNAL), signals(), peer_label(NULL)
{
if (perms_p) {
perms = perms_p;
if (perms & ~AA_VALID_SIGNAL_PERMS)
yyerror("perms contains invalid permission for signals\n");
} else {
perms = AA_VALID_SIGNAL_PERMS;
}
move_conditionals(conds);
free_cond_list(conds);
}
ostream &signal_rule::dump(ostream &os)
{
class_rule_t::dump(os);
if (perms != AA_VALID_SIGNAL_PERMS) {
os << " (";
if (perms & AA_MAY_SEND)
os << "send ";
if (perms & AA_MAY_RECEIVE)
os << "receive ";
os << ")";
}
if (!signals.empty()) {
os << " set=(";
for (Signals::iterator i = signals.begin(); i != signals.end();
i++) {
if (i != signals.begin())
os << ", ";
if (*i < MINRT_SIG)
os << sig_names[*i];
else
os << "rtmin+" << (*i - MINRT_SIG);
}
os << ")";
}
if (peer_label)
os << " " << peer_label;
os << ",\n";
return os;
}
int signal_rule::expand_variables(void)
{
return expand_entry_variables(&peer_label);
}
static int cmp_set_int(Signals const &lhs, Signals const &rhs)
{
int res = lhs.size() - rhs.size();
if (res)
return res;
for (Signals::iterator i = lhs.begin(),
j = rhs.begin();
i != lhs.end(); i++, j++) {
res = *i - *j;
if (res)
return res;
}
return 0;
}
int signal_rule::cmp(rule_t const &rhs) const
{
int res = perms_rule_t::cmp(rhs);
if (res)
return res;
signal_rule const &trhs = rule_cast<signal_rule const &>(rhs);
res = null_strcmp(peer_label, trhs.peer_label);
if (res)
return res;
return cmp_set_int(signals, trhs.signals);
}
void signal_rule::warn_once(const char *name)
{
rule_t::warn_once(name, "signal rules not enforced");
}
int signal_rule::gen_policy_re(Profile &prof)
{
std::ostringstream buffer;
std::string buf;
pattern_t ptype;
int pos;
/* Currently do not generate the rules if the kernel doesn't support
* it. We may want to switch this so that a compile could be
* used for full support on kernels that don't support the feature
*/
if (!features_supports_signal) {
warn_once(prof.name);
return RULE_NOT_SUPPORTED;
}
if (signals.size() == 0) {
/* not conditional on signal set, so will generate a label
* rule as well
*/
buffer << "(" << "\\x" << std::setfill('0') << std::setw(2) << std::hex << AA_CLASS_LABEL << "\\x" << AA_CLASS_SIGNAL << "|";
}
buffer << "\\x" << std::setfill('0') << std::setw(2) << std::hex << AA_CLASS_SIGNAL;
if (signals.size()) {
buffer << "[";
for (Signals::iterator i = signals.begin(); i != signals.end(); i++) {
buffer << "\\x" << std::setfill('0') << std::setw(2) << std::hex << *i;
}
buffer << "]";
} else {
/* match any char */
buffer << ".";
}
if (signals.size() == 0) {
/* close alternation */
buffer << ")";
}
if (peer_label) {
ptype = convert_aaregex_to_pcre(peer_label, 0, glob_default, buf, &pos);
if (ptype == ePatternInvalid)
goto fail;
buffer << buf;
} else {
buffer << anyone_match_pattern;
}
buf = buffer.str();
if (perms & (AA_MAY_SEND | AA_MAY_RECEIVE)) {
if (!prof.policy.rules->add_rule(buf.c_str(), priority,
rule_mode,
perms, audit == AUDIT_FORCE ? perms : 0,
parseopts))
goto fail;
}
return RULE_OK;
fail:
return RULE_ERROR;
}
|