2013-07-31 09:05:51 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013
|
|
|
|
* 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>
|
2014-01-06 14:46:10 -08:00
|
|
|
#include <sys/apparmor.h>
|
2013-07-31 09:05:51 -07:00
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
#include <iomanip>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
2013-07-31 09:05:51 -07:00
|
|
|
#include "parser.h"
|
2013-09-27 16:16:37 -07:00
|
|
|
#include "profile.h"
|
2013-07-31 09:05:51 -07:00
|
|
|
#include "parser_yacc.h"
|
|
|
|
#include "dbus.h"
|
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
|
2021-06-09 00:56:59 -07:00
|
|
|
int parse_dbus_perms(const char *str_perms, perms_t *perms, int fail)
|
2014-04-07 03:16:50 -07:00
|
|
|
{
|
2021-06-09 00:56:59 -07:00
|
|
|
return parse_X_perms("DBus", AA_VALID_DBUS_PERMS, str_perms, perms, fail);
|
2013-07-31 09:05:51 -07:00
|
|
|
}
|
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
void dbus_rule::move_conditionals(struct cond_entry *conds)
|
2013-07-31 09:05:51 -07:00
|
|
|
{
|
|
|
|
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 dbus rules\n");
|
|
|
|
if (list_len(cond_ent->vals) > 1)
|
|
|
|
yyerror("dbus conditional \"%s\" only supports a single value\n",
|
|
|
|
cond_ent->name);
|
|
|
|
|
|
|
|
if (strcmp(cond_ent->name, "bus") == 0) {
|
2014-04-23 11:36:26 -07:00
|
|
|
move_conditional_value("dbus", &bus, cond_ent);
|
2013-07-31 09:05:51 -07:00
|
|
|
} else if (strcmp(cond_ent->name, "name") == 0) {
|
2014-04-23 11:36:26 -07:00
|
|
|
move_conditional_value("dbus", &name, cond_ent);
|
2013-07-31 09:05:51 -07:00
|
|
|
} else if (strcmp(cond_ent->name, "label") == 0) {
|
2014-04-23 11:36:26 -07:00
|
|
|
move_conditional_value("dbus", &peer_label, cond_ent);
|
2013-07-31 09:05:51 -07:00
|
|
|
} else if (strcmp(cond_ent->name, "path") == 0) {
|
2014-04-23 11:36:26 -07:00
|
|
|
move_conditional_value("dbus", &path, cond_ent);
|
2013-07-31 09:05:51 -07:00
|
|
|
} else if (strcmp(cond_ent->name, "interface") == 0) {
|
2014-04-23 11:36:26 -07:00
|
|
|
move_conditional_value("dbus", &interface, cond_ent);
|
2013-07-31 09:05:51 -07:00
|
|
|
} else if (strcmp(cond_ent->name, "member") == 0) {
|
2014-04-23 11:36:26 -07:00
|
|
|
move_conditional_value("dbus", &member, cond_ent);
|
2013-07-31 09:05:51 -07:00
|
|
|
} else {
|
|
|
|
yyerror("invalid dbus conditional \"%s\"\n",
|
|
|
|
cond_ent->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:56:59 -07:00
|
|
|
dbus_rule::dbus_rule(perms_t perms_p, struct cond_entry *conds,
|
2014-04-07 03:16:50 -07:00
|
|
|
struct cond_entry *peer_conds):
|
2021-09-04 03:28:18 -07:00
|
|
|
bus(NULL), name(NULL), peer_label(NULL), path(NULL), interface(NULL), member(NULL)
|
2013-07-31 09:05:51 -07:00
|
|
|
{
|
|
|
|
int name_is_subject_cond = 0, message_rule = 0, service_rule = 0;
|
|
|
|
|
|
|
|
/* Move the global/subject conditionals over & check the results */
|
2014-04-07 03:16:50 -07:00
|
|
|
move_conditionals(conds);
|
|
|
|
if (name)
|
2013-07-31 09:05:51 -07:00
|
|
|
name_is_subject_cond = 1;
|
2014-04-07 03:16:50 -07:00
|
|
|
if (peer_label)
|
2013-07-31 09:05:51 -07:00
|
|
|
yyerror("dbus \"label\" conditional can only be used inside of the \"peer=()\" grouping\n");
|
|
|
|
|
|
|
|
/* Move the peer conditionals */
|
2014-04-07 03:16:50 -07:00
|
|
|
move_conditionals(peer_conds);
|
2013-07-31 09:05:51 -07:00
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
if (path || interface || member || peer_label ||
|
|
|
|
(name && !name_is_subject_cond))
|
2013-07-31 09:05:51 -07:00
|
|
|
message_rule = 1;
|
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
if (name && name_is_subject_cond)
|
2013-07-31 09:05:51 -07:00
|
|
|
service_rule = 1;
|
|
|
|
|
|
|
|
if (message_rule && service_rule)
|
|
|
|
yyerror("dbus rule contains message conditionals and service conditionals\n");
|
|
|
|
|
2021-06-09 00:56:59 -07:00
|
|
|
/* Copy perms. If no perms was specified, assign an implied perms. */
|
|
|
|
if (perms_p) {
|
|
|
|
perms = perms_p;
|
|
|
|
if (perms & ~AA_VALID_DBUS_PERMS)
|
|
|
|
yyerror("perms contains unknown dbus access\n");
|
|
|
|
else if (message_rule && (perms & AA_DBUS_BIND))
|
2013-07-31 09:05:51 -07:00
|
|
|
yyerror("dbus \"bind\" access cannot be used with message rule conditionals\n");
|
2021-06-09 00:56:59 -07:00
|
|
|
else if (service_rule && (perms & (AA_DBUS_SEND | AA_DBUS_RECEIVE)))
|
2013-07-31 09:05:51 -07:00
|
|
|
yyerror("dbus \"send\" and/or \"receive\" accesses cannot be used with service rule conditionals\n");
|
2021-06-09 00:56:59 -07:00
|
|
|
else if (perms & AA_DBUS_EAVESDROP &&
|
2014-04-07 03:16:50 -07:00
|
|
|
(path || interface || member ||
|
|
|
|
peer_label || name)) {
|
parser: Add dbus eavesdrop permission support to apparmor_parser
Allows for the policy writer to grant permission to eavesdrop on the
specified bus. Some example rules for granting the eavesdrop permission
are:
# Grant send, receive, bind, and eavesdrop
dbus,
# Grant send, receive, bind, and eavesdrop on the session bus
dbus bus=session,
# Grant send and eavesdrop on the system bus
dbus (send eavesdrop) bus=system,
# Grant eavesdrop on any bus
dbus eavesdrop,
Eavesdropping rules can contain the bus conditional. Any other
conditionals are not compatible with eavesdropping rules and the parser
will return an error.
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2013-12-06 11:17:43 -08:00
|
|
|
yyerror("dbus \"eavesdrop\" access can only contain a bus conditional\n");
|
|
|
|
}
|
2013-07-31 09:05:51 -07:00
|
|
|
} else {
|
|
|
|
if (message_rule)
|
2021-06-09 00:56:59 -07:00
|
|
|
perms = (AA_DBUS_SEND | AA_DBUS_RECEIVE);
|
2013-07-31 09:05:51 -07:00
|
|
|
else if (service_rule)
|
2021-06-09 00:56:59 -07:00
|
|
|
perms = (AA_DBUS_BIND);
|
parser: Add dbus eavesdrop permission support to apparmor_parser
Allows for the policy writer to grant permission to eavesdrop on the
specified bus. Some example rules for granting the eavesdrop permission
are:
# Grant send, receive, bind, and eavesdrop
dbus,
# Grant send, receive, bind, and eavesdrop on the session bus
dbus bus=session,
# Grant send and eavesdrop on the system bus
dbus (send eavesdrop) bus=system,
# Grant eavesdrop on any bus
dbus eavesdrop,
Eavesdropping rules can contain the bus conditional. Any other
conditionals are not compatible with eavesdropping rules and the parser
will return an error.
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
2013-12-06 11:17:43 -08:00
|
|
|
else
|
2021-06-09 00:56:59 -07:00
|
|
|
perms = AA_VALID_DBUS_PERMS;
|
2013-07-31 09:05:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
free_cond_list(conds);
|
2013-09-06 13:41:03 -07:00
|
|
|
free_cond_list(peer_conds);
|
2013-07-31 09:05:51 -07:00
|
|
|
}
|
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
ostream &dbus_rule::dump(ostream &os)
|
2013-08-29 12:34:13 -07:00
|
|
|
{
|
2021-09-04 03:28:18 -07:00
|
|
|
prefix_rule_t::dump(os);
|
2014-04-07 03:16:50 -07:00
|
|
|
|
|
|
|
os << "dbus ( ";
|
|
|
|
|
2021-09-04 03:28:18 -07:00
|
|
|
/* override default perms */
|
2021-06-09 00:56:59 -07:00
|
|
|
if (perms & AA_DBUS_SEND)
|
2014-04-07 03:16:50 -07:00
|
|
|
os << "send ";
|
2021-06-09 00:56:59 -07:00
|
|
|
if (perms & AA_DBUS_RECEIVE)
|
2014-04-07 03:16:50 -07:00
|
|
|
os << "receive ";
|
2021-06-09 00:56:59 -07:00
|
|
|
if (perms & AA_DBUS_BIND)
|
2014-04-07 03:16:50 -07:00
|
|
|
os << "bind ";
|
2021-06-09 00:56:59 -07:00
|
|
|
if (perms & AA_DBUS_EAVESDROP)
|
2014-04-07 03:16:50 -07:00
|
|
|
os << "eavesdrop ";
|
|
|
|
os << ")";
|
|
|
|
|
|
|
|
if (bus)
|
|
|
|
os << " bus=\"" << bus << "\"";
|
2021-06-09 00:56:59 -07:00
|
|
|
if ((perms & AA_DBUS_BIND) && name)
|
2014-04-07 03:16:50 -07:00
|
|
|
os << " name=\"" << name << "\"";
|
|
|
|
if (path)
|
|
|
|
os << " path=\"" << path << "\"";
|
|
|
|
if (interface)
|
|
|
|
os << " interface=\"" << interface << "\"";
|
|
|
|
if (member)
|
2015-02-12 13:20:57 -08:00
|
|
|
os << " member=\"" << member << "\"";
|
2014-04-07 03:16:50 -07:00
|
|
|
|
2021-06-09 00:56:59 -07:00
|
|
|
if (!(perms & AA_DBUS_BIND) && (peer_label || name)) {
|
2014-04-07 03:16:50 -07:00
|
|
|
os << " peer=( ";
|
|
|
|
if (peer_label)
|
|
|
|
os << "label=\"" << peer_label << "\" ";
|
|
|
|
if (name)
|
|
|
|
os << "name=\"" << name << "\" ";
|
|
|
|
os << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
os << ",\n";
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dbus_rule::expand_variables(void)
|
|
|
|
{
|
|
|
|
int error = expand_entry_variables(&bus);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = expand_entry_variables(&name);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = expand_entry_variables(&peer_label);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = expand_entry_variables(&path);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2020-08-31 21:41:07 -07:00
|
|
|
filter_slashes(path);
|
2014-04-07 03:16:50 -07:00
|
|
|
error = expand_entry_variables(&interface);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = expand_entry_variables(&member);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-09 14:51:55 -04:00
|
|
|
void dbus_rule::warn_once(const char *name)
|
2014-04-07 03:16:50 -07:00
|
|
|
{
|
2020-08-09 14:51:55 -04:00
|
|
|
rule_t::warn_once(name, "dbus rules not enforced");
|
2013-08-29 12:34:13 -07:00
|
|
|
}
|
2013-07-31 09:05:51 -07:00
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
int dbus_rule::gen_policy_re(Profile &prof)
|
2013-07-31 09:05:51 -07:00
|
|
|
{
|
2014-04-07 03:16:50 -07:00
|
|
|
std::string busbuf;
|
|
|
|
std::string namebuf;
|
|
|
|
std::string peer_labelbuf;
|
|
|
|
std::string pathbuf;
|
|
|
|
std::string ifacebuf;
|
|
|
|
std::string memberbuf;
|
|
|
|
std::ostringstream buffer;
|
|
|
|
const char *vec[6];
|
|
|
|
|
|
|
|
pattern_t ptype;
|
|
|
|
int pos;
|
|
|
|
|
2020-04-29 02:18:24 -07:00
|
|
|
if (!features_supports_dbus) {
|
2014-04-07 03:16:50 -07:00
|
|
|
warn_once(prof.name);
|
|
|
|
return RULE_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer << "\\x" << std::setfill('0') << std::setw(2) << std::hex << AA_CLASS_DBUS;
|
|
|
|
busbuf.append(buffer.str());
|
|
|
|
|
|
|
|
if (bus) {
|
2015-02-12 10:19:16 -08:00
|
|
|
ptype = convert_aaregex_to_pcre(bus, 0, glob_default, busbuf, &pos);
|
2014-04-07 03:16:50 -07:00
|
|
|
if (ptype == ePatternInvalid)
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
/* match any char except \000 0 or more times */
|
|
|
|
busbuf.append(default_match_pattern);
|
|
|
|
}
|
|
|
|
vec[0] = busbuf.c_str();
|
|
|
|
|
|
|
|
if (name) {
|
2015-02-12 10:19:16 -08:00
|
|
|
ptype = convert_aaregex_to_pcre(name, 0, glob_default, namebuf, &pos);
|
2014-04-07 03:16:50 -07:00
|
|
|
if (ptype == ePatternInvalid)
|
|
|
|
goto fail;
|
|
|
|
vec[1] = namebuf.c_str();
|
|
|
|
} else {
|
|
|
|
/* match any char except \000 0 or more times */
|
|
|
|
vec[1] = default_match_pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer_label) {
|
2015-02-12 10:19:16 -08:00
|
|
|
ptype = convert_aaregex_to_pcre(peer_label, 0, glob_default,
|
2014-04-07 03:16:50 -07:00
|
|
|
peer_labelbuf, &pos);
|
|
|
|
if (ptype == ePatternInvalid)
|
|
|
|
goto fail;
|
|
|
|
vec[2] = peer_labelbuf.c_str();
|
|
|
|
} else {
|
|
|
|
/* match any char except \000 0 or more times */
|
|
|
|
vec[2] = default_match_pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
2015-02-12 10:19:16 -08:00
|
|
|
ptype = convert_aaregex_to_pcre(path, 0, glob_default, pathbuf, &pos);
|
2014-04-07 03:16:50 -07:00
|
|
|
if (ptype == ePatternInvalid)
|
|
|
|
goto fail;
|
|
|
|
vec[3] = pathbuf.c_str();
|
|
|
|
} else {
|
|
|
|
/* match any char except \000 0 or more times */
|
|
|
|
vec[3] = default_match_pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interface) {
|
2015-02-12 10:19:16 -08:00
|
|
|
ptype = convert_aaregex_to_pcre(interface, 0, glob_default, ifacebuf, &pos);
|
2014-04-07 03:16:50 -07:00
|
|
|
if (ptype == ePatternInvalid)
|
|
|
|
goto fail;
|
|
|
|
vec[4] = ifacebuf.c_str();
|
|
|
|
} else {
|
|
|
|
/* match any char except \000 0 or more times */
|
|
|
|
vec[4] = default_match_pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (member) {
|
2015-02-12 10:19:16 -08:00
|
|
|
ptype = convert_aaregex_to_pcre(member, 0, glob_default, memberbuf, &pos);
|
2014-04-07 03:16:50 -07:00
|
|
|
if (ptype == ePatternInvalid)
|
|
|
|
goto fail;
|
|
|
|
vec[5] = memberbuf.c_str();
|
|
|
|
} else {
|
|
|
|
/* match any char except \000 0 or more times */
|
|
|
|
vec[5] = default_match_pattern;
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:56:59 -07:00
|
|
|
if (perms & AA_DBUS_BIND) {
|
2021-09-09 01:42:51 -07:00
|
|
|
if (!prof.policy.rules->add_rule_vec(rule_mode == RULE_DENY, perms & AA_DBUS_BIND,
|
2021-08-30 14:31:03 -07:00
|
|
|
audit == AUDIT_FORCE ? perms & AA_DBUS_BIND : 0,
|
2019-08-11 06:18:27 -07:00
|
|
|
2, vec, dfaflags, false))
|
2014-04-07 03:16:50 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
2021-06-09 00:56:59 -07:00
|
|
|
if (perms & (AA_DBUS_SEND | AA_DBUS_RECEIVE)) {
|
2021-09-09 01:42:51 -07:00
|
|
|
if (!prof.policy.rules->add_rule_vec(rule_mode == RULE_DENY,
|
2021-06-09 00:56:59 -07:00
|
|
|
perms & (AA_DBUS_SEND | AA_DBUS_RECEIVE),
|
2021-08-30 14:31:03 -07:00
|
|
|
audit == AUDIT_FORCE ? perms & (AA_DBUS_SEND | AA_DBUS_RECEIVE) : 0,
|
2019-08-11 06:18:27 -07:00
|
|
|
6, vec, dfaflags, false))
|
2014-04-07 03:16:50 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
2021-06-09 00:56:59 -07:00
|
|
|
if (perms & AA_DBUS_EAVESDROP) {
|
2021-09-09 01:42:51 -07:00
|
|
|
if (!prof.policy.rules->add_rule_vec(rule_mode == RULE_DENY,
|
2021-06-09 00:56:59 -07:00
|
|
|
perms & AA_DBUS_EAVESDROP,
|
2021-08-30 14:31:03 -07:00
|
|
|
audit == AUDIT_FORCE ? perms & AA_DBUS_EAVESDROP : 0,
|
2019-08-11 06:18:27 -07:00
|
|
|
1, vec, dfaflags, false))
|
2014-04-07 03:16:50 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return RULE_ERROR;
|
2013-07-31 09:05:51 -07:00
|
|
|
}
|