2006-04-11 21:52:54 +00:00
|
|
|
/*
|
2007-04-11 08:12:51 +00:00
|
|
|
* Copyright (c) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
|
|
|
|
* NOVELL (All rights reserved)
|
2006-04-11 21:52:54 +00:00
|
|
|
*
|
2012-02-24 04:21:59 -08:00
|
|
|
* Copyright (c) 2010 - 2012
|
|
|
|
* Canonical Ltd. (All rights reserved)
|
2010-03-12 15:26:32 -08:00
|
|
|
*
|
2006-04-11 21:52:54 +00:00
|
|
|
* 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
|
2010-03-12 15:26:32 -08:00
|
|
|
* along with this program; if not, contact Novell, Inc. or Canonical,
|
|
|
|
* Ltd.
|
2006-04-11 21:52:54 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-27 16:13:22 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
2006-04-11 21:52:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <search.h>
|
|
|
|
#include <string.h>
|
2009-07-24 07:35:19 +00:00
|
|
|
#include <errno.h>
|
2015-03-25 17:09:27 -05:00
|
|
|
#include <sys/apparmor.h>
|
2006-04-11 21:52:54 +00:00
|
|
|
|
|
|
|
#include "parser.h"
|
2013-09-27 16:16:37 -07:00
|
|
|
#include "profile.h"
|
2006-04-11 21:52:54 +00:00
|
|
|
#include "parser_yacc.h"
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
#ifdef DEBUG
|
2015-03-02 09:50:17 -06:00
|
|
|
#undef PDEBUG
|
2015-03-02 09:50:11 -06:00
|
|
|
#define PDEBUG(fmt, args...) fprintf(stderr, "Lexer: " fmt, ## args)
|
2006-04-11 21:52:54 +00:00
|
|
|
#else
|
2015-03-02 09:50:17 -06:00
|
|
|
#undef PDEBUG
|
2006-04-11 21:52:54 +00:00
|
|
|
#define PDEBUG(fmt, args...) /* Do nothing */
|
|
|
|
#endif
|
|
|
|
#define NPDEBUG(fmt, args...) /* Do nothing */
|
|
|
|
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
ProfileList policy_list;
|
2007-11-16 09:18:48 +00:00
|
|
|
|
2006-04-11 21:52:54 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
void add_to_list(Profile *prof)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
pair<ProfileList::iterator, bool> res = policy_list.insert(prof);
|
|
|
|
if (!res.second) {
|
2006-04-11 21:52:54 +00:00
|
|
|
PERROR("Multiple definitions for profile %s exist,"
|
2013-09-27 16:16:37 -07:00
|
|
|
"bailing out.\n", prof->name);
|
2006-04-11 21:52:54 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
void add_hat_to_policy(Profile *prof, Profile *hat)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
hat->parent = prof;
|
2006-04-11 21:52:54 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
pair<ProfileList::iterator, bool> res = prof->hat_table.insert(hat);
|
|
|
|
if (!res.second) {
|
2006-04-11 21:52:54 +00:00
|
|
|
PERROR("Multiple definitions for hat %s in profile %s exist,"
|
2013-09-27 16:16:37 -07:00
|
|
|
"bailing out.\n", hat->name, prof->name);
|
2006-04-11 21:52:54 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
int add_entry_to_x_table(Profile *prof, char *name)
|
Add mount rules
Add the ability to control mounting and unmounting
The basic form of the rules are.
[audit] [deny] mount [conds]* [device] [ -> [conds] path],
[audit] [deny] remount [conds]* [path],
[audit] [deny] umount [conds]* [path],
[audit] [deny] pivotroot [oldroot=<value>] <path> -> <profile>
remount is just a short cut for mount options=remount
where [conds] can be
fstype=<expr>
options=<expr>
conds follow the extended conditional syntax of allowing either:
* a single value after the equals, which has the same character range as
regular IDS (ie most anything but it can't be terminated with a , (comma)
and if spaces or other characters are needed it can be quoted
eg.
options=foo
options = foo
options="foo bar"
* a list of values after the equals, the list of values is enclosed within
parenthesis () and its has a slightly reduced character set but again
elements can be quoted.
the separation between elements is whitespace and commas.
eg.
options=(foo bar)
options=(foo, bar)
options=(foo , bar)
options=(foo,bar)
The rules are flexible and follow a similar pattern as network, capability,
etc.
mount, # allow all mounts, but not umount or pivotroot
mount fstype=procfs, # allow mounting procfs anywhere
mount options=(bind, ro) /foo -> /bar, # readonly bind mount
mount /dev/sda -> /mnt,
mount /dev/sd** -> /mnt/**,
mount fstype=overlayfs options=(rw,upperdir=/tmp/upper/,lowerdir=/) overlay -> /mnt/
umount,
umount /m*,
Currently variables and regexs are are supported on the device and mount
point. ie.
mount <devince> -> <mount point>,
Regexes are supported in fstype and options. The options have a further
caveat that regexs only work if the option is fs specific option.
eg. options=(upperdir=/tmp/*,lowerdir=/)
regex's will not currently work against the standard options like ro, rw
nosuid
Conditionals (fstype) can only be applied to the device (source) at this
time and will be disregarded in situations where the mount is manipulating
an existing mount (bind, remount).
Options can be specified multiple times
mount option=rw option=(nosuid,upperdir=/foo),
and will be combined together into a single set of values
The ordering of the standard mount options (rw,ro, ...) does not matter
but the ordering of fs specific options does.
Specifying that the value of a particular option does not matter can be
acheived by providing both the positive and negative forms of and option
option=(rw,ro) options=(suid,nosuid)
For the fs specific options specifying that a particular value does not
matter is achieve using a regex with alternations.
Improvements to the syntax and order restrictions are planned for the
future.
Signed-off-by: John Johansen <john.johansen@canonical.com>
2012-02-24 04:19:38 -08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = (AA_EXEC_LOCAL >> 10) + 1; i < AA_EXEC_COUNT; i++) {
|
2013-09-27 16:16:37 -07:00
|
|
|
if (!prof->exec_table[i]) {
|
|
|
|
prof->exec_table[i] = name;
|
Add mount rules
Add the ability to control mounting and unmounting
The basic form of the rules are.
[audit] [deny] mount [conds]* [device] [ -> [conds] path],
[audit] [deny] remount [conds]* [path],
[audit] [deny] umount [conds]* [path],
[audit] [deny] pivotroot [oldroot=<value>] <path> -> <profile>
remount is just a short cut for mount options=remount
where [conds] can be
fstype=<expr>
options=<expr>
conds follow the extended conditional syntax of allowing either:
* a single value after the equals, which has the same character range as
regular IDS (ie most anything but it can't be terminated with a , (comma)
and if spaces or other characters are needed it can be quoted
eg.
options=foo
options = foo
options="foo bar"
* a list of values after the equals, the list of values is enclosed within
parenthesis () and its has a slightly reduced character set but again
elements can be quoted.
the separation between elements is whitespace and commas.
eg.
options=(foo bar)
options=(foo, bar)
options=(foo , bar)
options=(foo,bar)
The rules are flexible and follow a similar pattern as network, capability,
etc.
mount, # allow all mounts, but not umount or pivotroot
mount fstype=procfs, # allow mounting procfs anywhere
mount options=(bind, ro) /foo -> /bar, # readonly bind mount
mount /dev/sda -> /mnt,
mount /dev/sd** -> /mnt/**,
mount fstype=overlayfs options=(rw,upperdir=/tmp/upper/,lowerdir=/) overlay -> /mnt/
umount,
umount /m*,
Currently variables and regexs are are supported on the device and mount
point. ie.
mount <devince> -> <mount point>,
Regexes are supported in fstype and options. The options have a further
caveat that regexs only work if the option is fs specific option.
eg. options=(upperdir=/tmp/*,lowerdir=/)
regex's will not currently work against the standard options like ro, rw
nosuid
Conditionals (fstype) can only be applied to the device (source) at this
time and will be disregarded in situations where the mount is manipulating
an existing mount (bind, remount).
Options can be specified multiple times
mount option=rw option=(nosuid,upperdir=/foo),
and will be combined together into a single set of values
The ordering of the standard mount options (rw,ro, ...) does not matter
but the ordering of fs specific options does.
Specifying that the value of a particular option does not matter can be
acheived by providing both the positive and negative forms of and option
option=(rw,ro) options=(suid,nosuid)
For the fs specific options specifying that a particular value does not
matter is achieve using a regex with alternations.
Improvements to the syntax and order restrictions are planned for the
future.
Signed-off-by: John Johansen <john.johansen@canonical.com>
2012-02-24 04:19:38 -08:00
|
|
|
return i;
|
2013-09-27 16:16:37 -07:00
|
|
|
} else if (strcmp(prof->exec_table[i], name) == 0) {
|
Add mount rules
Add the ability to control mounting and unmounting
The basic form of the rules are.
[audit] [deny] mount [conds]* [device] [ -> [conds] path],
[audit] [deny] remount [conds]* [path],
[audit] [deny] umount [conds]* [path],
[audit] [deny] pivotroot [oldroot=<value>] <path> -> <profile>
remount is just a short cut for mount options=remount
where [conds] can be
fstype=<expr>
options=<expr>
conds follow the extended conditional syntax of allowing either:
* a single value after the equals, which has the same character range as
regular IDS (ie most anything but it can't be terminated with a , (comma)
and if spaces or other characters are needed it can be quoted
eg.
options=foo
options = foo
options="foo bar"
* a list of values after the equals, the list of values is enclosed within
parenthesis () and its has a slightly reduced character set but again
elements can be quoted.
the separation between elements is whitespace and commas.
eg.
options=(foo bar)
options=(foo, bar)
options=(foo , bar)
options=(foo,bar)
The rules are flexible and follow a similar pattern as network, capability,
etc.
mount, # allow all mounts, but not umount or pivotroot
mount fstype=procfs, # allow mounting procfs anywhere
mount options=(bind, ro) /foo -> /bar, # readonly bind mount
mount /dev/sda -> /mnt,
mount /dev/sd** -> /mnt/**,
mount fstype=overlayfs options=(rw,upperdir=/tmp/upper/,lowerdir=/) overlay -> /mnt/
umount,
umount /m*,
Currently variables and regexs are are supported on the device and mount
point. ie.
mount <devince> -> <mount point>,
Regexes are supported in fstype and options. The options have a further
caveat that regexs only work if the option is fs specific option.
eg. options=(upperdir=/tmp/*,lowerdir=/)
regex's will not currently work against the standard options like ro, rw
nosuid
Conditionals (fstype) can only be applied to the device (source) at this
time and will be disregarded in situations where the mount is manipulating
an existing mount (bind, remount).
Options can be specified multiple times
mount option=rw option=(nosuid,upperdir=/foo),
and will be combined together into a single set of values
The ordering of the standard mount options (rw,ro, ...) does not matter
but the ordering of fs specific options does.
Specifying that the value of a particular option does not matter can be
acheived by providing both the positive and negative forms of and option
option=(rw,ro) options=(suid,nosuid)
For the fs specific options specifying that a particular value does not
matter is achieve using a regex with alternations.
Improvements to the syntax and order restrictions are planned for the
future.
Signed-off-by: John Johansen <john.johansen@canonical.com>
2012-02-24 04:19:38 -08:00
|
|
|
/* name already in table */
|
|
|
|
free(name);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
static int add_named_transition(Profile *prof, struct cod_entry *entry)
|
2008-04-16 04:45:02 +00:00
|
|
|
{
|
|
|
|
char *name = NULL;
|
|
|
|
|
|
|
|
/* check to see if it is a local transition */
|
2016-03-18 17:28:51 -05:00
|
|
|
if (!label_contains_ns(entry->nt_name)) {
|
2008-04-16 06:54:51 +00:00
|
|
|
char *sub = strstr(entry->nt_name, "//");
|
|
|
|
/* does the subprofile name match the rule */
|
2008-06-04 07:24:38 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
if (sub && strncmp(prof->name, sub, sub - entry->nt_name) &&
|
2008-04-16 06:54:51 +00:00
|
|
|
strcmp(sub + 2, entry->name) == 0) {
|
|
|
|
free(entry->nt_name);
|
|
|
|
entry->nt_name = NULL;
|
|
|
|
return AA_EXEC_LOCAL >> 10;
|
|
|
|
} else if (((entry->mode & AA_USER_EXEC_MODIFIERS) ==
|
|
|
|
SHIFT_MODE(AA_EXEC_LOCAL, AA_USER_SHIFT)) ||
|
|
|
|
((entry->mode & AA_OTHER_EXEC_MODIFIERS) ==
|
|
|
|
SHIFT_MODE(AA_EXEC_LOCAL, AA_OTHER_SHIFT))) {
|
|
|
|
if (strcmp(entry->nt_name, entry->name) == 0) {
|
|
|
|
free(entry->nt_name);
|
|
|
|
entry->nt_name = NULL;
|
|
|
|
return AA_EXEC_LOCAL >> 10;
|
|
|
|
}
|
|
|
|
/* specified as cix so profile name is implicit */
|
2013-09-27 16:16:37 -07:00
|
|
|
name = (char *) malloc(strlen(prof->name) + strlen(entry->nt_name)
|
2008-04-16 06:54:51 +00:00
|
|
|
+ 3);
|
|
|
|
if (!name) {
|
|
|
|
PERROR("Memory allocation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-09-27 16:16:37 -07:00
|
|
|
sprintf(name, "%s//%s", prof->name, entry->nt_name);
|
2008-04-16 06:54:51 +00:00
|
|
|
free(entry->nt_name);
|
2016-03-18 17:28:50 -05:00
|
|
|
entry->nt_name = NULL;
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* pass control of the memory pointed to by nt_name
|
|
|
|
* from entry to add_entry_to_x_table()
|
|
|
|
*/
|
|
|
|
name = entry->nt_name;
|
|
|
|
entry->nt_name = NULL;
|
2008-04-16 04:45:02 +00:00
|
|
|
}
|
2016-03-18 17:28:50 -05:00
|
|
|
} else {
|
2016-03-18 17:28:51 -05:00
|
|
|
/**
|
|
|
|
* pass control of the memory pointed to by nt_name
|
|
|
|
* from entry to add_entry_to_x_table()
|
|
|
|
*/
|
|
|
|
name = entry->nt_name;
|
2008-04-16 06:54:51 +00:00
|
|
|
entry->nt_name = NULL;
|
2008-04-16 04:45:02 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
return add_entry_to_x_table(prof, name);
|
2008-04-16 04:45:02 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
void add_entry_to_policy(Profile *prof, struct cod_entry *entry)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
entry->next = prof->entries;
|
|
|
|
prof->entries = entry;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
void post_process_file_entries(Profile *prof)
|
2008-06-04 07:24:38 +00:00
|
|
|
{
|
|
|
|
struct cod_entry *entry;
|
2012-04-11 16:04:33 -07:00
|
|
|
int cp_mode = 0;
|
2008-06-04 07:24:38 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
list_for_each(prof->entries, entry) {
|
2008-06-04 07:24:38 +00:00
|
|
|
if (entry->nt_name) {
|
|
|
|
int mode = 0;
|
2013-09-27 16:16:37 -07:00
|
|
|
int n = add_named_transition(prof, entry);
|
2008-06-04 07:24:38 +00:00
|
|
|
if (!n) {
|
2013-09-27 16:16:37 -07:00
|
|
|
PERROR("Profile %s has too many specified profile transitions.\n", prof->name);
|
2008-06-04 07:24:38 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (entry->mode & AA_USER_EXEC)
|
|
|
|
mode |= SHIFT_MODE(n << 10, AA_USER_SHIFT);
|
|
|
|
if (entry->mode & AA_OTHER_EXEC)
|
|
|
|
mode |= SHIFT_MODE(n << 10, AA_OTHER_SHIFT);
|
|
|
|
entry->mode = ((entry->mode & ~AA_ALL_EXEC_MODIFIERS) |
|
|
|
|
(mode & AA_ALL_EXEC_MODIFIERS));
|
|
|
|
}
|
2012-04-11 16:04:33 -07:00
|
|
|
/* FIXME: currently change_profile also implies onexec */
|
|
|
|
cp_mode |= entry->mode & (AA_CHANGE_PROFILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if there are change_profile rules, this implies that we need
|
|
|
|
* access to /proc/self/attr/current
|
|
|
|
*/
|
|
|
|
if (cp_mode & AA_CHANGE_PROFILE) {
|
|
|
|
/* FIXME: should use @{PROC}/@{PID}/attr/{current,exec} */
|
|
|
|
struct cod_entry *new_ent;
|
|
|
|
char *buffer = strdup("/proc/*/attr/{current,exec}");
|
|
|
|
if (!buffer) {
|
|
|
|
PERROR("Memory allocation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-03-18 17:28:51 -05:00
|
|
|
new_ent = new_entry(buffer, AA_MAY_WRITE, NULL);
|
2012-04-11 16:04:33 -07:00
|
|
|
if (!new_ent) {
|
|
|
|
PERROR("Memory allocation error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-09-27 16:16:37 -07:00
|
|
|
add_entry_to_policy(prof, new_ent);
|
2008-06-04 07:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 03:16:50 -07:00
|
|
|
void post_process_rule_entries(Profile *prof)
|
Add mount rules
Add the ability to control mounting and unmounting
The basic form of the rules are.
[audit] [deny] mount [conds]* [device] [ -> [conds] path],
[audit] [deny] remount [conds]* [path],
[audit] [deny] umount [conds]* [path],
[audit] [deny] pivotroot [oldroot=<value>] <path> -> <profile>
remount is just a short cut for mount options=remount
where [conds] can be
fstype=<expr>
options=<expr>
conds follow the extended conditional syntax of allowing either:
* a single value after the equals, which has the same character range as
regular IDS (ie most anything but it can't be terminated with a , (comma)
and if spaces or other characters are needed it can be quoted
eg.
options=foo
options = foo
options="foo bar"
* a list of values after the equals, the list of values is enclosed within
parenthesis () and its has a slightly reduced character set but again
elements can be quoted.
the separation between elements is whitespace and commas.
eg.
options=(foo bar)
options=(foo, bar)
options=(foo , bar)
options=(foo,bar)
The rules are flexible and follow a similar pattern as network, capability,
etc.
mount, # allow all mounts, but not umount or pivotroot
mount fstype=procfs, # allow mounting procfs anywhere
mount options=(bind, ro) /foo -> /bar, # readonly bind mount
mount /dev/sda -> /mnt,
mount /dev/sd** -> /mnt/**,
mount fstype=overlayfs options=(rw,upperdir=/tmp/upper/,lowerdir=/) overlay -> /mnt/
umount,
umount /m*,
Currently variables and regexs are are supported on the device and mount
point. ie.
mount <devince> -> <mount point>,
Regexes are supported in fstype and options. The options have a further
caveat that regexs only work if the option is fs specific option.
eg. options=(upperdir=/tmp/*,lowerdir=/)
regex's will not currently work against the standard options like ro, rw
nosuid
Conditionals (fstype) can only be applied to the device (source) at this
time and will be disregarded in situations where the mount is manipulating
an existing mount (bind, remount).
Options can be specified multiple times
mount option=rw option=(nosuid,upperdir=/foo),
and will be combined together into a single set of values
The ordering of the standard mount options (rw,ro, ...) does not matter
but the ordering of fs specific options does.
Specifying that the value of a particular option does not matter can be
acheived by providing both the positive and negative forms of and option
option=(rw,ro) options=(suid,nosuid)
For the fs specific options specifying that a particular value does not
matter is achieve using a regex with alternations.
Improvements to the syntax and order restrictions are planned for the
future.
Signed-off-by: John Johansen <john.johansen@canonical.com>
2012-02-24 04:19:38 -08:00
|
|
|
{
|
2014-04-07 03:16:50 -07:00
|
|
|
for (RuleList::iterator i = prof->rule_ents.begin(); i != prof->rule_ents.end(); i++)
|
|
|
|
(*i)->post_process(*prof);
|
Add mount rules
Add the ability to control mounting and unmounting
The basic form of the rules are.
[audit] [deny] mount [conds]* [device] [ -> [conds] path],
[audit] [deny] remount [conds]* [path],
[audit] [deny] umount [conds]* [path],
[audit] [deny] pivotroot [oldroot=<value>] <path> -> <profile>
remount is just a short cut for mount options=remount
where [conds] can be
fstype=<expr>
options=<expr>
conds follow the extended conditional syntax of allowing either:
* a single value after the equals, which has the same character range as
regular IDS (ie most anything but it can't be terminated with a , (comma)
and if spaces or other characters are needed it can be quoted
eg.
options=foo
options = foo
options="foo bar"
* a list of values after the equals, the list of values is enclosed within
parenthesis () and its has a slightly reduced character set but again
elements can be quoted.
the separation between elements is whitespace and commas.
eg.
options=(foo bar)
options=(foo, bar)
options=(foo , bar)
options=(foo,bar)
The rules are flexible and follow a similar pattern as network, capability,
etc.
mount, # allow all mounts, but not umount or pivotroot
mount fstype=procfs, # allow mounting procfs anywhere
mount options=(bind, ro) /foo -> /bar, # readonly bind mount
mount /dev/sda -> /mnt,
mount /dev/sd** -> /mnt/**,
mount fstype=overlayfs options=(rw,upperdir=/tmp/upper/,lowerdir=/) overlay -> /mnt/
umount,
umount /m*,
Currently variables and regexs are are supported on the device and mount
point. ie.
mount <devince> -> <mount point>,
Regexes are supported in fstype and options. The options have a further
caveat that regexs only work if the option is fs specific option.
eg. options=(upperdir=/tmp/*,lowerdir=/)
regex's will not currently work against the standard options like ro, rw
nosuid
Conditionals (fstype) can only be applied to the device (source) at this
time and will be disregarded in situations where the mount is manipulating
an existing mount (bind, remount).
Options can be specified multiple times
mount option=rw option=(nosuid,upperdir=/foo),
and will be combined together into a single set of values
The ordering of the standard mount options (rw,ro, ...) does not matter
but the ordering of fs specific options does.
Specifying that the value of a particular option does not matter can be
acheived by providing both the positive and negative forms of and option
option=(rw,ro) options=(suid,nosuid)
For the fs specific options specifying that a particular value does not
matter is achieve using a regex with alternations.
Improvements to the syntax and order restrictions are planned for the
future.
Signed-off-by: John Johansen <john.johansen@canonical.com>
2012-02-24 04:19:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-05 05:44:44 +00:00
|
|
|
#define CHANGEHAT_PATH "/proc/[0-9]*/attr/current"
|
|
|
|
|
2008-04-06 18:52:47 +00:00
|
|
|
/* add file rules to access /proc files to call change_hat()
|
|
|
|
*/
|
2013-09-27 16:16:37 -07:00
|
|
|
static int profile_add_hat_rules(Profile *prof)
|
2008-04-05 05:44:44 +00:00
|
|
|
{
|
|
|
|
struct cod_entry *entry;
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
/* TODO: ??? fix logic for when to add to hat/base vs. local */
|
|
|
|
/* don't add hat rules for local_profiles or base profiles */
|
|
|
|
if (prof->local || prof->hat_table.empty())
|
|
|
|
return 0;
|
2008-04-16 04:45:02 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
/* add entry to hat */
|
2016-03-18 17:28:51 -05:00
|
|
|
entry = new_entry(strdup(CHANGEHAT_PATH), AA_MAY_WRITE, NULL);
|
2013-09-27 16:16:37 -07:00
|
|
|
if (!entry)
|
|
|
|
return ENOMEM;
|
2008-06-08 09:32:12 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
add_entry_to_policy(prof, entry);
|
2008-04-06 18:52:47 +00:00
|
|
|
|
2008-04-05 05:44:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:09:26 -05:00
|
|
|
int load_policy_list(ProfileList &list, int option,
|
|
|
|
aa_kernel_interface *kernel_interface, int cache_fd)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
int res = 0;
|
2006-04-11 21:52:54 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
for (ProfileList::iterator i = list.begin(); i != list.end(); i++) {
|
2015-03-25 17:09:26 -05:00
|
|
|
res = load_profile(option, kernel_interface, *i, cache_fd);
|
2013-09-27 16:16:37 -07:00
|
|
|
if (res != 0)
|
|
|
|
break;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
return res;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:09:26 -05:00
|
|
|
int load_flattened_hats(Profile *prof, int option,
|
|
|
|
aa_kernel_interface *kernel_interface, int cache_fd)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2015-03-25 17:09:26 -05:00
|
|
|
return load_policy_list(prof->hat_table, option, kernel_interface,
|
|
|
|
cache_fd);
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:09:26 -05:00
|
|
|
int load_policy(int option, aa_kernel_interface *kernel_interface, int cache_fd)
|
2007-06-26 21:09:46 +00:00
|
|
|
{
|
2015-03-25 17:09:26 -05:00
|
|
|
return load_policy_list(policy_list, option, kernel_interface, cache_fd);
|
2007-06-26 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 11:07:01 -07:00
|
|
|
int load_hats(std::ostringstream &buf, Profile *prof)
|
2007-06-26 21:09:46 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
for (ProfileList::iterator i = prof->hat_table.begin(); i != prof->hat_table.end(); i++) {
|
2014-04-23 11:07:01 -07:00
|
|
|
sd_serialize_profile(buf, *i, 0);
|
2013-09-27 16:16:37 -07:00
|
|
|
}
|
2007-06-26 21:09:46 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
return 0;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void dump_policy(void)
|
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
policy_list.dump();
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dump_policy_names(void)
|
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
policy_list.dump_profile_names(true);
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
/* merge_hats: merges hat_table into hat_table owned by prof */
|
|
|
|
static void merge_hats(Profile *prof, ProfileList &hats)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
for (ProfileList::iterator i = hats.begin(); i != hats.end(); ) {
|
|
|
|
ProfileList::iterator cur = i++;
|
|
|
|
add_hat_to_policy(prof, *cur);
|
|
|
|
hats.erase(cur);
|
|
|
|
}
|
2006-04-11 21:52:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
Profile *merge_policy(Profile *a, Profile *b)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
Profile *ret = a;
|
2006-04-11 21:52:54 +00:00
|
|
|
struct cod_entry *last;
|
2008-04-06 18:55:27 +00:00
|
|
|
|
2006-04-11 21:52:54 +00:00
|
|
|
if (!a) {
|
|
|
|
ret = b;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!b)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (a->name || b->name) {
|
|
|
|
PERROR("ASSERT: policy merges shouldn't have names %s %s\n",
|
|
|
|
a->name ? a->name : "",
|
|
|
|
b->name ? b->name : "");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->entries) {
|
2007-02-27 02:29:16 +00:00
|
|
|
list_last_entry(a->entries, last);
|
2006-04-11 21:52:54 +00:00
|
|
|
last->next = b->entries;
|
|
|
|
} else {
|
|
|
|
a->entries = b->entries;
|
|
|
|
}
|
|
|
|
b->entries = NULL;
|
|
|
|
|
|
|
|
a->flags.complain = a->flags.complain || b->flags.complain;
|
|
|
|
a->flags.audit = a->flags.audit || b->flags.audit;
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
a->caps.allow |= b->caps.allow;
|
|
|
|
a->caps.audit |= b->caps.audit;
|
|
|
|
a->caps.deny |= b->caps.deny;
|
|
|
|
a->caps.quiet |= b->caps.quiet;
|
2008-04-06 18:55:27 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
if (a->net.allow) {
|
2009-07-24 17:24:41 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < get_af_max(); i++) {
|
2013-09-27 16:16:37 -07:00
|
|
|
a->net.allow[i] |= b->net.allow[i];
|
|
|
|
a->net.audit[i] |= b->net.audit[i];
|
|
|
|
a->net.deny[i] |= b->net.deny[i];
|
|
|
|
a->net.quiet[i] |= b->net.quiet[i];
|
2008-04-06 18:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-11 21:52:54 +00:00
|
|
|
merge_hats(a, b->hat_table);
|
2013-09-27 16:16:37 -07:00
|
|
|
delete b;
|
2006-04-11 21:52:54 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
int process_profile_rules(Profile *profile)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
int error;
|
2006-04-11 21:52:54 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
error = process_profile_regex(profile);
|
|
|
|
if (error) {
|
|
|
|
PERROR(_("ERROR processing regexs for profile %s, failed to load\n"), profile->name);
|
|
|
|
exit(1);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = process_profile_policydb(profile);
|
|
|
|
if (error) {
|
|
|
|
PERROR(_("ERROR processing policydb rules for profile %s, failed to load\n"),
|
|
|
|
(profile)->name);
|
|
|
|
exit(1);
|
|
|
|
return error;
|
2008-04-05 05:44:44 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int post_process_policy_list(ProfileList &list, int debug_only);
|
|
|
|
int post_process_profile(Profile *profile, int debug_only)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
error = profile_add_hat_rules(profile);
|
|
|
|
if (error) {
|
|
|
|
PERROR(_("ERROR adding hat access rule for profile %s\n"),
|
|
|
|
profile->name);
|
|
|
|
return error;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
error = process_profile_variables(profile);
|
|
|
|
if (error) {
|
|
|
|
PERROR(_("ERROR expanding variables for profile %s, failed to load\n"), profile->name);
|
|
|
|
exit(1);
|
|
|
|
return error;
|
2008-04-09 09:03:17 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
error = replace_profile_aliases(profile);
|
|
|
|
if (error) {
|
|
|
|
PERROR(_("ERROR replacing aliases for profile %s, failed to load\n"), profile->name);
|
|
|
|
return error;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
error = profile_merge_rules(profile);
|
|
|
|
if (error) {
|
|
|
|
PERROR(_("ERROR merging rules for profile %s, failed to load\n"), profile->name);
|
|
|
|
exit(1);
|
|
|
|
return error;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 08:14:46 -08:00
|
|
|
if (!debug_only) {
|
2013-09-27 16:16:37 -07:00
|
|
|
error = process_profile_rules(profile);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2012-02-16 08:14:46 -08:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
error = post_process_policy_list(profile->hat_table, debug_only);
|
|
|
|
return error;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
int post_process_policy_list(ProfileList &list, int debug_only)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
int error = 0;
|
|
|
|
for (ProfileList::iterator i = list.begin(); i != list.end(); i++) {
|
|
|
|
error = post_process_profile(*i, debug_only);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2006-04-11 21:52:54 +00:00
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
return error;
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:16:37 -07:00
|
|
|
int post_process_policy(int debug_only)
|
2006-04-11 21:52:54 +00:00
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
return post_process_policy_list(policy_list, debug_only);
|
2006-04-11 21:52:54 +00:00
|
|
|
}
|
2009-07-24 07:34:11 +00:00
|
|
|
|
|
|
|
void free_policies(void)
|
|
|
|
{
|
2013-09-27 16:16:37 -07:00
|
|
|
policy_list.clear();
|
2009-07-24 07:34:11 +00:00
|
|
|
}
|