TOMOYO: Use callback for updating entries.

Use common "struct list_head" + "bool" + "u8" structure and
use common code for elements using that structure.

Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: James Morris <jmorris@namei.org>
This commit is contained in:
Tetsuo Handa 2010-06-12 20:46:22 +09:00 committed by James Morris
parent 927942aabb
commit 237ab459f1
7 changed files with 253 additions and 312 deletions

View file

@ -950,8 +950,6 @@ static bool tomoyo_print_mount_acl(struct tomoyo_io_buffer *head,
struct tomoyo_mount_acl *ptr) struct tomoyo_mount_acl *ptr)
{ {
const int pos = head->read_avail; const int pos = head->read_avail;
if (ptr->is_deleted)
return true;
if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) || if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_MOUNT) ||
!tomoyo_print_name_union(head, &ptr->dev_name) || !tomoyo_print_name_union(head, &ptr->dev_name) ||
!tomoyo_print_name_union(head, &ptr->dir_name) || !tomoyo_print_name_union(head, &ptr->dir_name) ||
@ -977,6 +975,8 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
{ {
const u8 acl_type = ptr->type; const u8 acl_type = ptr->type;
if (ptr->is_deleted)
return true;
if (acl_type == TOMOYO_TYPE_PATH_ACL) { if (acl_type == TOMOYO_TYPE_PATH_ACL) {
struct tomoyo_path_acl *acl struct tomoyo_path_acl *acl
= container_of(ptr, struct tomoyo_path_acl, head); = container_of(ptr, struct tomoyo_path_acl, head);

View file

@ -112,6 +112,8 @@ enum tomoyo_path_acl_index {
TOMOYO_MAX_PATH_OPERATION TOMOYO_MAX_PATH_OPERATION
}; };
#define TOMOYO_RW_MASK ((1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE))
enum tomoyo_path_number3_acl_index { enum tomoyo_path_number3_acl_index {
TOMOYO_TYPE_MKBLOCK, TOMOYO_TYPE_MKBLOCK,
TOMOYO_TYPE_MKCHAR, TOMOYO_TYPE_MKCHAR,
@ -289,17 +291,19 @@ struct tomoyo_number_group_member {
* *
* (1) "list" which is linked to the ->acl_info_list of * (1) "list" which is linked to the ->acl_info_list of
* "struct tomoyo_domain_info" * "struct tomoyo_domain_info"
* (2) "type" which tells type of the entry (either * (2) "is_deleted" is a bool which is true if this domain is marked as
* "struct tomoyo_path_acl" or "struct tomoyo_path2_acl"). * "deleted", false otherwise.
* (3) "type" which tells type of the entry.
* *
* Packing "struct tomoyo_acl_info" allows * Packing "struct tomoyo_acl_info" allows
* "struct tomoyo_path_acl" to embed "u8" + "u16" and * "struct tomoyo_path_acl" to embed "u16" and "struct tomoyo_path2_acl"
* "struct tomoyo_path2_acl" to embed "u8" * "struct tomoyo_path_number_acl" "struct tomoyo_path_number3_acl" to embed
* without enlarging their structure size. * "u8" without enlarging their structure size.
*/ */
struct tomoyo_acl_info { struct tomoyo_acl_info {
struct list_head list; struct list_head list;
u8 type; bool is_deleted;
u8 type; /* = one of values in "enum tomoyo_acl_entry_type_index". */
} __packed; } __packed;
/* /*
@ -438,17 +442,15 @@ struct tomoyo_path2_acl {
* It has following fields. * It has following fields.
* *
* (1) "head" which is a "struct tomoyo_acl_info". * (1) "head" which is a "struct tomoyo_acl_info".
* (2) "is_deleted" is boolean. * (2) "dev_name" is the device name.
* (3) "dev_name" is the device name. * (3) "dir_name" is the mount point.
* (4) "dir_name" is the mount point. * (4) "fs_type" is the filesystem type.
* (5) "flags" is the mount flags. * (5) "flags" is the mount flags.
* *
* Directives held by this structure are "allow_rename", "allow_link" and * Directive held by this structure is "allow_mount".
* "allow_pivot_root".
*/ */
struct tomoyo_mount_acl { struct tomoyo_mount_acl {
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */ struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
bool is_deleted;
struct tomoyo_name_union dev_name; struct tomoyo_name_union dev_name;
struct tomoyo_name_union dir_name; struct tomoyo_name_union dir_name;
struct tomoyo_name_union fs_type; struct tomoyo_name_union fs_type;
@ -914,6 +916,16 @@ void tomoyo_run_gc(void);
void tomoyo_memory_free(void *ptr); void tomoyo_memory_free(void *ptr);
int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
bool is_delete, struct tomoyo_domain_info *domain,
bool (*check_duplicate) (const struct tomoyo_acl_info
*,
const struct tomoyo_acl_info
*),
bool (*merge_duplicate) (struct tomoyo_acl_info *,
struct tomoyo_acl_info *,
const bool));
/********** External variable definitions. **********/ /********** External variable definitions. **********/
/* Lock for GC. */ /* Lock for GC. */
@ -1042,52 +1054,6 @@ static inline bool tomoyo_is_same_number_union
p1->max_type == p2->max_type && p1->is_group == p2->is_group; p1->max_type == p2->max_type && p1->is_group == p2->is_group;
} }
static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
const struct tomoyo_path_acl *p2)
{
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
tomoyo_is_same_name_union(&p1->name, &p2->name);
}
static inline bool tomoyo_is_same_path_number3_acl
(const struct tomoyo_path_number3_acl *p1,
const struct tomoyo_path_number3_acl *p2)
{
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
&& tomoyo_is_same_number_union(&p1->mode, &p2->mode)
&& tomoyo_is_same_number_union(&p1->major, &p2->major)
&& tomoyo_is_same_number_union(&p1->minor, &p2->minor);
}
static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
const struct tomoyo_path2_acl *p2)
{
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
tomoyo_is_same_name_union(&p1->name1, &p2->name1) &&
tomoyo_is_same_name_union(&p1->name2, &p2->name2);
}
static inline bool tomoyo_is_same_path_number_acl
(const struct tomoyo_path_number_acl *p1,
const struct tomoyo_path_number_acl *p2)
{
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
&& tomoyo_is_same_number_union(&p1->number, &p2->number);
}
static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
const struct tomoyo_mount_acl *p2)
{
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
tomoyo_is_same_number_union(&p1->flags, &p2->flags);
}
static inline bool tomoyo_is_same_domain_initializer_entry static inline bool tomoyo_is_same_domain_initializer_entry
(const struct tomoyo_domain_initializer_entry *p1, (const struct tomoyo_domain_initializer_entry *p1,
const struct tomoyo_domain_initializer_entry *p2) const struct tomoyo_domain_initializer_entry *p2)

View file

@ -15,6 +15,57 @@
/* The initial domain. */ /* The initial domain. */
struct tomoyo_domain_info tomoyo_kernel_domain; struct tomoyo_domain_info tomoyo_kernel_domain;
/**
* tomoyo_update_domain - Update an entry for domain policy.
*
* @new_entry: Pointer to "struct tomoyo_acl_info".
* @size: Size of @new_entry in bytes.
* @is_delete: True if it is a delete request.
* @domain: Pointer to "struct tomoyo_domain_info".
* @check_duplicate: Callback function to find duplicated entry.
* @merge_duplicate: Callback function to merge duplicated entry.
*
* Returns 0 on success, negative value otherwise.
*
* Caller holds tomoyo_read_lock().
*/
int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
bool is_delete, struct tomoyo_domain_info *domain,
bool (*check_duplicate) (const struct tomoyo_acl_info
*,
const struct tomoyo_acl_info
*),
bool (*merge_duplicate) (struct tomoyo_acl_info *,
struct tomoyo_acl_info *,
const bool))
{
int error = is_delete ? -ENOENT : -ENOMEM;
struct tomoyo_acl_info *entry;
if (mutex_lock_interruptible(&tomoyo_policy_lock))
return error;
list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
if (!check_duplicate(entry, new_entry))
continue;
if (merge_duplicate)
entry->is_deleted = merge_duplicate(entry, new_entry,
is_delete);
else
entry->is_deleted = is_delete;
error = 0;
break;
}
if (error && !is_delete) {
entry = tomoyo_commit_ok(new_entry, size);
if (entry) {
list_add_tail_rcu(&entry->list, &domain->acl_info_list);
error = 0;
}
}
mutex_unlock(&tomoyo_policy_lock);
return error;
}
/* /*
* tomoyo_domain_list is used for holding list of domains. * tomoyo_domain_list is used for holding list of domains.
* The ->acl_info_list of "struct tomoyo_domain_info" is used for holding * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding

View file

@ -664,50 +664,6 @@ bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
return done; return done;
} }
/**
* tomoyo_update_file_acl - Update file's read/write/execute ACL.
*
* @perm: Permission (between 1 to 7).
* @filename: Filename.
* @domain: Pointer to "struct tomoyo_domain_info".
* @is_delete: True if it is a delete request.
*
* Returns 0 on success, negative value otherwise.
*
* This is legacy support interface for older policy syntax.
* Current policy syntax uses "allow_read/write" instead of "6",
* "allow_read" instead of "4", "allow_write" instead of "2",
* "allow_execute" instead of "1".
*
* Caller holds tomoyo_read_lock().
*/
static int tomoyo_update_file_acl(u8 perm, const char *filename,
struct tomoyo_domain_info * const domain,
const bool is_delete)
{
if (perm > 7 || !perm) {
printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
__func__, perm, filename);
return -EINVAL;
}
if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
/*
* Only 'allow_mkdir' and 'allow_rmdir' are valid for
* directory permissions.
*/
return 0;
if (perm & 4)
tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
is_delete);
if (perm & 2)
tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
is_delete);
if (perm & 1)
tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
is_delete);
return 0;
}
/** /**
* tomoyo_path_acl - Check permission for single path operation. * tomoyo_path_acl - Check permission for single path operation.
* *
@ -797,6 +753,40 @@ static int tomoyo_file_perm(struct tomoyo_request_info *r,
return error; return error;
} }
static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
const struct tomoyo_acl_info *b)
{
const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
tomoyo_is_same_name_union(&p1->name, &p2->name);
}
static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
struct tomoyo_acl_info *b,
const bool is_delete)
{
u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
->perm;
u16 perm = *a_perm;
const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
if (is_delete) {
perm &= ~b_perm;
if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK)
perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE)))
perm &= ~TOMOYO_RW_MASK;
} else {
perm |= b_perm;
if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK)
perm |= (1 << TOMOYO_TYPE_READ_WRITE);
else if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
perm |= TOMOYO_RW_MASK;
}
*a_perm = perm;
return !perm;
}
/** /**
* tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list. * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
* *
@ -810,63 +800,56 @@ static int tomoyo_file_perm(struct tomoyo_request_info *r,
* Caller holds tomoyo_read_lock(). * Caller holds tomoyo_read_lock().
*/ */
static int tomoyo_update_path_acl(const u8 type, const char *filename, static int tomoyo_update_path_acl(const u8 type, const char *filename,
struct tomoyo_domain_info *const domain, struct tomoyo_domain_info * const domain,
const bool is_delete) const bool is_delete)
{ {
static const u16 tomoyo_rw_mask =
(1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
const u16 perm = 1 << type;
struct tomoyo_acl_info *ptr;
struct tomoyo_path_acl e = { struct tomoyo_path_acl e = {
.head.type = TOMOYO_TYPE_PATH_ACL, .head.type = TOMOYO_TYPE_PATH_ACL,
.perm = perm .perm = 1 << type
}; };
int error = is_delete ? -ENOENT : -ENOMEM; int error;
if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE))
if (type == TOMOYO_TYPE_READ_WRITE) e.perm |= TOMOYO_RW_MASK;
e.perm |= tomoyo_rw_mask;
if (!domain)
return -EINVAL;
if (!tomoyo_parse_name_union(filename, &e.name)) if (!tomoyo_parse_name_union(filename, &e.name))
return -EINVAL; return -EINVAL;
if (mutex_lock_interruptible(&tomoyo_policy_lock)) error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
goto out; tomoyo_same_path_acl,
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { tomoyo_merge_path_acl);
struct tomoyo_path_acl *acl =
container_of(ptr, struct tomoyo_path_acl, head);
if (!tomoyo_is_same_path_acl(acl, &e))
continue;
if (is_delete) {
acl->perm &= ~perm;
if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
acl->perm &= ~tomoyo_rw_mask;
} else {
acl->perm |= perm;
if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
acl->perm |= tomoyo_rw_mask;
}
error = 0;
break;
}
if (!is_delete && error) {
struct tomoyo_path_acl *entry =
tomoyo_commit_ok(&e, sizeof(e));
if (entry) {
list_add_tail_rcu(&entry->head.list,
&domain->acl_info_list);
error = 0;
}
}
mutex_unlock(&tomoyo_policy_lock);
out:
tomoyo_put_name_union(&e.name); tomoyo_put_name_union(&e.name);
return error; return error;
} }
static bool tomoyo_same_path_number3_acl(const struct tomoyo_acl_info *a,
const struct tomoyo_acl_info *b)
{
const struct tomoyo_path_number3_acl *p1 = container_of(a, typeof(*p1),
head);
const struct tomoyo_path_number3_acl *p2 = container_of(b, typeof(*p2),
head);
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
&& tomoyo_is_same_number_union(&p1->mode, &p2->mode)
&& tomoyo_is_same_number_union(&p1->major, &p2->major)
&& tomoyo_is_same_number_union(&p1->minor, &p2->minor);
}
static bool tomoyo_merge_path_number3_acl(struct tomoyo_acl_info *a,
struct tomoyo_acl_info *b,
const bool is_delete)
{
u8 *const a_perm = &container_of(a, struct tomoyo_path_number3_acl,
head)->perm;
u8 perm = *a_perm;
const u8 b_perm = container_of(b, struct tomoyo_path_number3_acl, head)
->perm;
if (is_delete)
perm &= ~b_perm;
else
perm |= b_perm;
*a_perm = perm;
return !perm;
}
/** /**
* tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list. * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
* *
@ -879,20 +862,17 @@ static int tomoyo_update_path_acl(const u8 type, const char *filename,
* @is_delete: True if it is a delete request. * @is_delete: True if it is a delete request.
* *
* Returns 0 on success, negative value otherwise. * Returns 0 on success, negative value otherwise.
*
* Caller holds tomoyo_read_lock().
*/ */
static inline int tomoyo_update_path_number3_acl(const u8 type, static int tomoyo_update_path_number3_acl(const u8 type, const char *filename,
const char *filename, char *mode, char *major, char *minor,
char *mode, struct tomoyo_domain_info * const
char *major, char *minor, domain, const bool is_delete)
struct tomoyo_domain_info *
const domain,
const bool is_delete)
{ {
const u8 perm = 1 << type;
struct tomoyo_acl_info *ptr;
struct tomoyo_path_number3_acl e = { struct tomoyo_path_number3_acl e = {
.head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL, .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
.perm = perm .perm = 1 << type
}; };
int error = is_delete ? -ENOENT : -ENOMEM; int error = is_delete ? -ENOENT : -ENOMEM;
if (!tomoyo_parse_name_union(filename, &e.name) || if (!tomoyo_parse_name_union(filename, &e.name) ||
@ -900,30 +880,9 @@ static inline int tomoyo_update_path_number3_acl(const u8 type,
!tomoyo_parse_number_union(major, &e.major) || !tomoyo_parse_number_union(major, &e.major) ||
!tomoyo_parse_number_union(minor, &e.minor)) !tomoyo_parse_number_union(minor, &e.minor))
goto out; goto out;
if (mutex_lock_interruptible(&tomoyo_policy_lock)) error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
goto out; tomoyo_same_path_number3_acl,
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { tomoyo_merge_path_number3_acl);
struct tomoyo_path_number3_acl *acl =
container_of(ptr, struct tomoyo_path_number3_acl, head);
if (!tomoyo_is_same_path_number3_acl(acl, &e))
continue;
if (is_delete)
acl->perm &= ~perm;
else
acl->perm |= perm;
error = 0;
break;
}
if (!is_delete && error) {
struct tomoyo_path_number3_acl *entry =
tomoyo_commit_ok(&e, sizeof(e));
if (entry) {
list_add_tail_rcu(&entry->head.list,
&domain->acl_info_list);
error = 0;
}
}
mutex_unlock(&tomoyo_policy_lock);
out: out:
tomoyo_put_name_union(&e.name); tomoyo_put_name_union(&e.name);
tomoyo_put_number_union(&e.mode); tomoyo_put_number_union(&e.mode);
@ -932,6 +891,32 @@ static inline int tomoyo_update_path_number3_acl(const u8 type,
return error; return error;
} }
static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
const struct tomoyo_acl_info *b)
{
const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
&& tomoyo_is_same_name_union(&p1->name1, &p2->name1)
&& tomoyo_is_same_name_union(&p1->name2, &p2->name2);
}
static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
struct tomoyo_acl_info *b,
const bool is_delete)
{
u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
->perm;
u8 perm = *a_perm;
const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
if (is_delete)
perm &= ~b_perm;
else
perm |= b_perm;
*a_perm = perm;
return !perm;
}
/** /**
* tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list. * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
* *
@ -947,46 +932,20 @@ static inline int tomoyo_update_path_number3_acl(const u8 type,
*/ */
static int tomoyo_update_path2_acl(const u8 type, const char *filename1, static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
const char *filename2, const char *filename2,
struct tomoyo_domain_info *const domain, struct tomoyo_domain_info * const domain,
const bool is_delete) const bool is_delete)
{ {
const u8 perm = 1 << type;
struct tomoyo_path2_acl e = { struct tomoyo_path2_acl e = {
.head.type = TOMOYO_TYPE_PATH2_ACL, .head.type = TOMOYO_TYPE_PATH2_ACL,
.perm = perm .perm = 1 << type
}; };
struct tomoyo_acl_info *ptr;
int error = is_delete ? -ENOENT : -ENOMEM; int error = is_delete ? -ENOENT : -ENOMEM;
if (!domain)
return -EINVAL;
if (!tomoyo_parse_name_union(filename1, &e.name1) || if (!tomoyo_parse_name_union(filename1, &e.name1) ||
!tomoyo_parse_name_union(filename2, &e.name2)) !tomoyo_parse_name_union(filename2, &e.name2))
goto out; goto out;
if (mutex_lock_interruptible(&tomoyo_policy_lock)) error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
goto out; tomoyo_same_path2_acl,
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { tomoyo_merge_path2_acl);
struct tomoyo_path2_acl *acl =
container_of(ptr, struct tomoyo_path2_acl, head);
if (!tomoyo_is_same_path2_acl(acl, &e))
continue;
if (is_delete)
acl->perm &= ~perm;
else
acl->perm |= perm;
error = 0;
break;
}
if (!is_delete && error) {
struct tomoyo_path2_acl *entry =
tomoyo_commit_ok(&e, sizeof(e));
if (entry) {
list_add_tail_rcu(&entry->head.list,
&domain->acl_info_list);
error = 0;
}
}
mutex_unlock(&tomoyo_policy_lock);
out: out:
tomoyo_put_name_union(&e.name1); tomoyo_put_name_union(&e.name1);
tomoyo_put_name_union(&e.name2); tomoyo_put_name_union(&e.name2);
@ -1157,6 +1116,35 @@ static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
return error; return error;
} }
static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
const struct tomoyo_acl_info *b)
{
const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
head);
const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
head);
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
&& tomoyo_is_same_number_union(&p1->number, &p2->number);
}
static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
struct tomoyo_acl_info *b,
const bool is_delete)
{
u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
head)->perm;
u8 perm = *a_perm;
const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
->perm;
if (is_delete)
perm &= ~b_perm;
else
perm |= b_perm;
*a_perm = perm;
return !perm;
}
/** /**
* tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL. * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
* *
@ -1168,50 +1156,24 @@ static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
* *
* Returns 0 on success, negative value otherwise. * Returns 0 on success, negative value otherwise.
*/ */
static inline int tomoyo_update_path_number_acl(const u8 type, static int tomoyo_update_path_number_acl(const u8 type, const char *filename,
const char *filename, char *number,
char *number, struct tomoyo_domain_info * const
struct tomoyo_domain_info * domain,
const domain, const bool is_delete)
const bool is_delete)
{ {
const u8 perm = 1 << type;
struct tomoyo_acl_info *ptr;
struct tomoyo_path_number_acl e = { struct tomoyo_path_number_acl e = {
.head.type = TOMOYO_TYPE_PATH_NUMBER_ACL, .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
.perm = perm .perm = 1 << type
}; };
int error = is_delete ? -ENOENT : -ENOMEM; int error = is_delete ? -ENOENT : -ENOMEM;
if (!domain)
return -EINVAL;
if (!tomoyo_parse_name_union(filename, &e.name)) if (!tomoyo_parse_name_union(filename, &e.name))
return -EINVAL; return -EINVAL;
if (!tomoyo_parse_number_union(number, &e.number)) if (!tomoyo_parse_number_union(number, &e.number))
goto out; goto out;
if (mutex_lock_interruptible(&tomoyo_policy_lock)) error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
goto out; tomoyo_same_path_number_acl,
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { tomoyo_merge_path_number_acl);
struct tomoyo_path_number_acl *acl =
container_of(ptr, struct tomoyo_path_number_acl, head);
if (!tomoyo_is_same_path_number_acl(acl, &e))
continue;
if (is_delete)
acl->perm &= ~perm;
else
acl->perm |= perm;
error = 0;
break;
}
if (!is_delete && error) {
struct tomoyo_path_number_acl *entry =
tomoyo_commit_ok(&e, sizeof(e));
if (entry) {
list_add_tail_rcu(&entry->head.list,
&domain->acl_info_list);
error = 0;
}
}
mutex_unlock(&tomoyo_policy_lock);
out: out:
tomoyo_put_name_union(&e.name); tomoyo_put_name_union(&e.name);
tomoyo_put_number_union(&e.number); tomoyo_put_number_union(&e.number);
@ -1585,13 +1547,8 @@ int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
u8 type; u8 type;
if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0]) if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
return -EINVAL; return -EINVAL;
if (strncmp(w[0], "allow_", 6)) { if (strncmp(w[0], "allow_", 6))
unsigned int perm;
if (sscanf(w[0], "%u", &perm) == 1)
return tomoyo_update_file_acl((u8) perm, w[1], domain,
is_delete);
goto out; goto out;
}
w[0] += 6; w[0] += 6;
for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) { for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
if (strcmp(w[0], tomoyo_path_keyword[type])) if (strcmp(w[0], tomoyo_path_keyword[type]))

View file

@ -310,34 +310,8 @@ static void tomoyo_collect_entry(void)
struct tomoyo_acl_info *acl; struct tomoyo_acl_info *acl;
list_for_each_entry_rcu(acl, &domain->acl_info_list, list_for_each_entry_rcu(acl, &domain->acl_info_list,
list) { list) {
switch (acl->type) { if (!acl->is_deleted)
case TOMOYO_TYPE_PATH_ACL:
if (container_of(acl,
struct tomoyo_path_acl,
head)->perm)
continue;
break;
case TOMOYO_TYPE_PATH2_ACL:
if (container_of(acl,
struct tomoyo_path2_acl,
head)->perm)
continue;
break;
case TOMOYO_TYPE_PATH_NUMBER_ACL:
if (container_of(acl,
struct tomoyo_path_number_acl,
head)->perm)
continue;
break;
case TOMOYO_TYPE_PATH_NUMBER3_ACL:
if (container_of(acl,
struct tomoyo_path_number3_acl,
head)->perm)
continue;
break;
default:
continue; continue;
}
if (tomoyo_add_to_gc(TOMOYO_ID_ACL, acl)) if (tomoyo_add_to_gc(TOMOYO_ID_ACL, acl))
list_del_rcu(&acl->list); list_del_rcu(&acl->list);
else else

View file

@ -114,11 +114,10 @@ static int tomoyo_mount_acl2(struct tomoyo_request_info *r, char *dev_name,
tomoyo_fill_path_info(&rdev); tomoyo_fill_path_info(&rdev);
list_for_each_entry_rcu(ptr, &r->domain->acl_info_list, list) { list_for_each_entry_rcu(ptr, &r->domain->acl_info_list, list) {
struct tomoyo_mount_acl *acl; struct tomoyo_mount_acl *acl;
if (ptr->type != TOMOYO_TYPE_MOUNT_ACL) if (ptr->is_deleted || ptr->type != TOMOYO_TYPE_MOUNT_ACL)
continue; continue;
acl = container_of(ptr, struct tomoyo_mount_acl, head); acl = container_of(ptr, struct tomoyo_mount_acl, head);
if (acl->is_deleted || if (!tomoyo_compare_number_union(flags, &acl->flags) ||
!tomoyo_compare_number_union(flags, &acl->flags) ||
!tomoyo_compare_name_union(&rtype, &acl->fs_type) || !tomoyo_compare_name_union(&rtype, &acl->fs_type) ||
!tomoyo_compare_name_union(&rdir, &acl->dir_name) || !tomoyo_compare_name_union(&rdir, &acl->dir_name) ||
(need_dev && (need_dev &&
@ -259,6 +258,18 @@ int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
return error; return error;
} }
static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
const struct tomoyo_acl_info *b)
{
const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
tomoyo_is_same_number_union(&p1->flags, &p2->flags);
}
/** /**
* tomoyo_write_mount_policy - Write "struct tomoyo_mount_acl" list. * tomoyo_write_mount_policy - Write "struct tomoyo_mount_acl" list.
* *
@ -267,11 +278,12 @@ int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
* @is_delete: True if it is a delete request. * @is_delete: True if it is a delete request.
* *
* Returns 0 on success, negative value otherwise. * Returns 0 on success, negative value otherwise.
*
* Caller holds tomoyo_read_lock().
*/ */
int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain, int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
const bool is_delete) const bool is_delete)
{ {
struct tomoyo_acl_info *ptr;
struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL }; struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
int error = is_delete ? -ENOENT : -ENOMEM; int error = is_delete ? -ENOENT : -ENOMEM;
char *w[4]; char *w[4];
@ -282,27 +294,8 @@ int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
!tomoyo_parse_name_union(w[2], &e.fs_type) || !tomoyo_parse_name_union(w[2], &e.fs_type) ||
!tomoyo_parse_number_union(w[3], &e.flags)) !tomoyo_parse_number_union(w[3], &e.flags))
goto out; goto out;
if (mutex_lock_interruptible(&tomoyo_policy_lock)) error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
goto out; tomoyo_same_mount_acl, NULL);
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
struct tomoyo_mount_acl *acl =
container_of(ptr, struct tomoyo_mount_acl, head);
if (!tomoyo_is_same_mount_acl(acl, &e))
continue;
acl->is_deleted = is_delete;
error = 0;
break;
}
if (!is_delete && error) {
struct tomoyo_mount_acl *entry =
tomoyo_commit_ok(&e, sizeof(e));
if (entry) {
list_add_tail_rcu(&entry->head.list,
&domain->acl_info_list);
error = 0;
}
}
mutex_unlock(&tomoyo_policy_lock);
out: out:
tomoyo_put_name_union(&e.dev_name); tomoyo_put_name_union(&e.dev_name);
tomoyo_put_name_union(&e.dir_name); tomoyo_put_name_union(&e.dir_name);

View file

@ -911,6 +911,8 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
if (!domain) if (!domain)
return true; return true;
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
if (!ptr->is_deleted)
continue;
switch (ptr->type) { switch (ptr->type) {
u16 perm; u16 perm;
u8 i; u8 i;
@ -944,10 +946,8 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
if (perm & (1 << i)) if (perm & (1 << i))
count++; count++;
break; break;
case TOMOYO_TYPE_MOUNT_ACL: default:
if (!container_of(ptr, struct tomoyo_mount_acl, head)-> count++;
is_deleted)
count++;
} }
} }
if (count < tomoyo_profile(domain->profile)->learning-> if (count < tomoyo_profile(domain->profile)->learning->