Skip to content

Commit a1f9bb6

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Split file access control functions by type of parameters.
Check numeric parameters for operations that deal them (e.g. chmod/chown/ioctl). Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent cb0abe6 commit a1f9bb6

5 files changed

Lines changed: 752 additions & 123 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,12 +1043,11 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
10431043
return true;
10441044
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
10451045
switch (ptr->type) {
1046-
struct tomoyo_path_acl *acl;
1047-
u32 perm;
1046+
u16 perm;
10481047
u8 i;
10491048
case TOMOYO_TYPE_PATH_ACL:
1050-
acl = container_of(ptr, struct tomoyo_path_acl, head);
1051-
perm = acl->perm | (((u32) acl->perm_high) << 16);
1049+
perm = container_of(ptr, struct tomoyo_path_acl, head)
1050+
->perm;
10521051
for (i = 0; i < TOMOYO_MAX_PATH_OPERATION; i++)
10531052
if (perm & (1 << i))
10541053
count++;
@@ -1062,6 +1061,20 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r)
10621061
if (perm & (1 << i))
10631062
count++;
10641063
break;
1064+
case TOMOYO_TYPE_PATH_NUMBER_ACL:
1065+
perm = container_of(ptr, struct tomoyo_path_number_acl,
1066+
head)->perm;
1067+
for (i = 0; i < TOMOYO_MAX_PATH_NUMBER_OPERATION; i++)
1068+
if (perm & (1 << i))
1069+
count++;
1070+
break;
1071+
case TOMOYO_TYPE_PATH_NUMBER3_ACL:
1072+
perm = container_of(ptr, struct tomoyo_path_number3_acl,
1073+
head)->perm;
1074+
for (i = 0; i < TOMOYO_MAX_PATH_NUMBER3_OPERATION; i++)
1075+
if (perm & (1 << i))
1076+
count++;
1077+
break;
10651078
}
10661079
}
10671080
if (count < tomoyo_check_flags(domain, TOMOYO_MAX_ACCEPT_ENTRY))
@@ -1579,7 +1592,7 @@ static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
15791592
{
15801593
int pos;
15811594
u8 bit;
1582-
const u32 perm = ptr->perm | (((u32) ptr->perm_high) << 16);
1595+
const u16 perm = ptr->perm;
15831596

15841597
for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
15851598
if (!(perm & (1 << bit)))
@@ -1637,6 +1650,76 @@ static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
16371650
return false;
16381651
}
16391652

1653+
/**
1654+
* tomoyo_print_path_number_acl - Print a path_number ACL entry.
1655+
*
1656+
* @head: Pointer to "struct tomoyo_io_buffer".
1657+
* @ptr: Pointer to "struct tomoyo_path_number_acl".
1658+
*
1659+
* Returns true on success, false otherwise.
1660+
*/
1661+
static bool tomoyo_print_path_number_acl(struct tomoyo_io_buffer *head,
1662+
struct tomoyo_path_number_acl *ptr)
1663+
{
1664+
int pos;
1665+
u8 bit;
1666+
const u8 perm = ptr->perm;
1667+
for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION;
1668+
bit++) {
1669+
if (!(perm & (1 << bit)))
1670+
continue;
1671+
pos = head->read_avail;
1672+
if (!tomoyo_io_printf(head, "allow_%s",
1673+
tomoyo_path_number2keyword(bit)) ||
1674+
!tomoyo_print_name_union(head, &ptr->name) ||
1675+
!tomoyo_print_number_union(head, &ptr->number) ||
1676+
!tomoyo_io_printf(head, "\n"))
1677+
goto out;
1678+
}
1679+
head->read_bit = 0;
1680+
return true;
1681+
out:
1682+
head->read_bit = bit;
1683+
head->read_avail = pos;
1684+
return false;
1685+
}
1686+
1687+
/**
1688+
* tomoyo_print_path_number3_acl - Print a path_number3 ACL entry.
1689+
*
1690+
* @head: Pointer to "struct tomoyo_io_buffer".
1691+
* @ptr: Pointer to "struct tomoyo_path_number3_acl".
1692+
*
1693+
* Returns true on success, false otherwise.
1694+
*/
1695+
static bool tomoyo_print_path_number3_acl(struct tomoyo_io_buffer *head,
1696+
struct tomoyo_path_number3_acl *ptr)
1697+
{
1698+
int pos;
1699+
u8 bit;
1700+
const u16 perm = ptr->perm;
1701+
for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_NUMBER3_OPERATION;
1702+
bit++) {
1703+
if (!(perm & (1 << bit)))
1704+
continue;
1705+
pos = head->read_avail;
1706+
if (!tomoyo_io_printf(head, "allow_%s",
1707+
tomoyo_path_number32keyword(bit)) ||
1708+
!tomoyo_print_name_union(head, &ptr->name) ||
1709+
!tomoyo_print_number_union(head, &ptr->mode) ||
1710+
!tomoyo_print_number_union(head, &ptr->major) ||
1711+
!tomoyo_print_number_union(head, &ptr->minor) ||
1712+
!tomoyo_io_printf(head, "\n"))
1713+
goto out;
1714+
}
1715+
head->read_bit = 0;
1716+
return true;
1717+
out:
1718+
head->read_bit = bit;
1719+
head->read_avail = pos;
1720+
return false;
1721+
}
1722+
16401723
/**
16411724
* tomoyo_print_entry - Print an ACL entry.
16421725
*
@@ -1660,6 +1743,18 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head,
16601743
= container_of(ptr, struct tomoyo_path2_acl, head);
16611744
return tomoyo_print_path2_acl(head, acl);
16621745
}
1746+
if (acl_type == TOMOYO_TYPE_PATH_NUMBER_ACL) {
1747+
struct tomoyo_path_number_acl *acl
1748+
= container_of(ptr, struct tomoyo_path_number_acl,
1749+
head);
1750+
return tomoyo_print_path_number_acl(head, acl);
1751+
}
1752+
if (acl_type == TOMOYO_TYPE_PATH_NUMBER3_ACL) {
1753+
struct tomoyo_path_number3_acl *acl
1754+
= container_of(ptr, struct tomoyo_path_number3_acl,
1755+
head);
1756+
return tomoyo_print_path_number3_acl(head, acl);
1757+
}
16631758
BUG(); /* This must not happen. */
16641759
return false;
16651760
}

‎security/tomoyo/common.h‎

Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,52 +88,64 @@ enum tomoyo_mac_index {
8888
enum tomoyo_acl_entry_type_index {
8989
TOMOYO_TYPE_PATH_ACL,
9090
TOMOYO_TYPE_PATH2_ACL,
91+
TOMOYO_TYPE_PATH_NUMBER_ACL,
92+
TOMOYO_TYPE_PATH_NUMBER3_ACL,
9193
};
9294

9395
/* Index numbers for File Controls. */
9496

9597
/*
96-
* TYPE_READ_WRITE_ACL is special. TYPE_READ_WRITE_ACL is automatically set
97-
* if both TYPE_READ_ACL and TYPE_WRITE_ACL are set. Both TYPE_READ_ACL and
98-
* TYPE_WRITE_ACL are automatically set if TYPE_READ_WRITE_ACL is set.
99-
* TYPE_READ_WRITE_ACL is automatically cleared if either TYPE_READ_ACL or
100-
* TYPE_WRITE_ACL is cleared. Both TYPE_READ_ACL and TYPE_WRITE_ACL are
101-
* automatically cleared if TYPE_READ_WRITE_ACL is cleared.
98+
* TOMOYO_TYPE_READ_WRITE is special. TOMOYO_TYPE_READ_WRITE is automatically
99+
* set if both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are set.
100+
* Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically set if
101+
* TOMOYO_TYPE_READ_WRITE is set.
102+
* TOMOYO_TYPE_READ_WRITE is automatically cleared if either TOMOYO_TYPE_READ
103+
* or TOMOYO_TYPE_WRITE is cleared.
104+
* Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically cleared if
105+
* TOMOYO_TYPE_READ_WRITE is cleared.
102106
*/
103107

104108
enum tomoyo_path_acl_index {
105109
TOMOYO_TYPE_READ_WRITE,
106110
TOMOYO_TYPE_EXECUTE,
107111
TOMOYO_TYPE_READ,
108112
TOMOYO_TYPE_WRITE,
109-
TOMOYO_TYPE_CREATE,
110113
TOMOYO_TYPE_UNLINK,
111-
TOMOYO_TYPE_MKDIR,
112114
TOMOYO_TYPE_RMDIR,
113-
TOMOYO_TYPE_MKFIFO,
114-
TOMOYO_TYPE_MKSOCK,
115-
TOMOYO_TYPE_MKBLOCK,
116-
TOMOYO_TYPE_MKCHAR,
117115
TOMOYO_TYPE_TRUNCATE,
118116
TOMOYO_TYPE_SYMLINK,
119117
TOMOYO_TYPE_REWRITE,
120-
TOMOYO_TYPE_IOCTL,
121-
TOMOYO_TYPE_CHMOD,
122-
TOMOYO_TYPE_CHOWN,
123-
TOMOYO_TYPE_CHGRP,
124118
TOMOYO_TYPE_CHROOT,
125119
TOMOYO_TYPE_MOUNT,
126120
TOMOYO_TYPE_UMOUNT,
127121
TOMOYO_MAX_PATH_OPERATION
128122
};
129123

124+
enum tomoyo_path_number3_acl_index {
125+
TOMOYO_TYPE_MKBLOCK,
126+
TOMOYO_TYPE_MKCHAR,
127+
TOMOYO_MAX_PATH_NUMBER3_OPERATION
128+
};
129+
130130
enum tomoyo_path2_acl_index {
131131
TOMOYO_TYPE_LINK,
132132
TOMOYO_TYPE_RENAME,
133133
TOMOYO_TYPE_PIVOT_ROOT,
134134
TOMOYO_MAX_PATH2_OPERATION
135135
};
136136

137+
enum tomoyo_path_number_acl_index {
138+
TOMOYO_TYPE_CREATE,
139+
TOMOYO_TYPE_MKDIR,
140+
TOMOYO_TYPE_MKFIFO,
141+
TOMOYO_TYPE_MKSOCK,
142+
TOMOYO_TYPE_IOCTL,
143+
TOMOYO_TYPE_CHMOD,
144+
TOMOYO_TYPE_CHOWN,
145+
TOMOYO_TYPE_CHGRP,
146+
TOMOYO_MAX_PATH_NUMBER_OPERATION
147+
};
148+
137149
enum tomoyo_securityfs_interface_index {
138150
TOMOYO_DOMAINPOLICY,
139151
TOMOYO_EXCEPTIONPOLICY,
@@ -347,19 +359,61 @@ struct tomoyo_domain_info {
347359
* (3) "name" is the pathname.
348360
*
349361
* Directives held by this structure are "allow_read/write", "allow_execute",
350-
* "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir",
351-
* "allow_rmdir", "allow_mkfifo", "allow_mksock", "allow_mkblock",
352-
* "allow_mkchar", "allow_truncate", "allow_symlink", "allow_rewrite",
353-
* "allow_ioctl", "allow_chmod", "allow_chown", "allow_chgrp", "allow_chroot",
362+
* "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
363+
* "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot",
354364
* "allow_mount" and "allow_unmount".
355365
*/
356366
struct tomoyo_path_acl {
357367
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
358-
u8 perm_high;
359368
u16 perm;
360369
struct tomoyo_name_union name;
361370
};
362371

372+
/*
373+
* tomoyo_path_number_acl is a structure which is used for holding an
374+
* entry with one pathname and one number operation.
375+
* It has following fields.
376+
*
377+
* (1) "head" which is a "struct tomoyo_acl_info".
378+
* (2) "perm" which is a bitmask of permitted operations.
379+
* (3) "name" is the pathname.
380+
* (4) "number" is the numeric value.
381+
*
382+
* Directives held by this structure are "allow_create", "allow_mkdir",
383+
* "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
384+
* and "allow_chgrp".
385+
*
386+
*/
387+
struct tomoyo_path_number_acl {
388+
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
389+
u8 perm;
390+
struct tomoyo_name_union name;
391+
struct tomoyo_number_union number;
392+
};
393+
394+
/*
395+
* tomoyo_path_number3_acl is a structure which is used for holding an
396+
* entry with one pathname and three numbers operation.
397+
* It has following fields.
398+
*
399+
* (1) "head" which is a "struct tomoyo_acl_info".
400+
* (2) "perm" which is a bitmask of permitted operations.
401+
* (3) "mode" is the create mode.
402+
* (4) "major" is the major number of device node.
403+
* (5) "minor" is the minor number of device node.
404+
*
405+
* Directives held by this structure are "allow_mkchar", "allow_mkblock".
406+
*
407+
*/
408+
struct tomoyo_path_number3_acl {
409+
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER3_ACL */
410+
u8 perm;
411+
struct tomoyo_name_union name;
412+
struct tomoyo_number_union mode;
413+
struct tomoyo_number_union major;
414+
struct tomoyo_number_union minor;
415+
};
416+
363417
/*
364418
* tomoyo_path2_acl is a structure which is used for holding an
365419
* entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
@@ -639,6 +693,8 @@ bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
639693
bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
640694
/* Convert double path operation to operation name. */
641695
const char *tomoyo_path22keyword(const u8 operation);
696+
const char *tomoyo_path_number2keyword(const u8 operation);
697+
const char *tomoyo_path_number32keyword(const u8 operation);
642698
/* Get the last component of the given domainname. */
643699
const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
644700
/* Convert single path operation to operation name. */
@@ -736,11 +792,18 @@ int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
736792
const struct tomoyo_path_info *filename);
737793
int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
738794
struct path *path, const int flag);
795+
int tomoyo_path_number_perm(const u8 operation, struct path *path,
796+
unsigned long number);
797+
int tomoyo_path_number3_perm(const u8 operation, struct path *path,
798+
const unsigned int mode, unsigned int dev);
739799
int tomoyo_path_perm(const u8 operation, struct path *path);
740800
int tomoyo_path2_perm(const u8 operation, struct path *path1,
741801
struct path *path2);
742802
int tomoyo_find_next_domain(struct linux_binprm *bprm);
743803

804+
void tomoyo_print_ulong(char *buffer, const int buffer_len,
805+
const unsigned long value, const u8 type);
806+
744807
/* Drop refcount on tomoyo_name_union. */
745808
void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
746809

@@ -880,6 +943,18 @@ static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
880943
tomoyo_is_same_name_union(&p1->name, &p2->name);
881944
}
882945

946+
static inline bool tomoyo_is_same_path_number3_acl
947+
(const struct tomoyo_path_number3_acl *p1,
948+
const struct tomoyo_path_number3_acl *p2)
949+
{
950+
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
951+
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
952+
&& tomoyo_is_same_number_union(&p1->mode, &p2->mode)
953+
&& tomoyo_is_same_number_union(&p1->major, &p2->major)
954+
&& tomoyo_is_same_number_union(&p1->minor, &p2->minor);
955+
}
956+
957+
883958
static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
884959
const struct tomoyo_path2_acl *p2)
885960
{
@@ -888,6 +963,15 @@ static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
888963
tomoyo_is_same_name_union(&p1->name2, &p2->name2);
889964
}
890965

966+
static inline bool tomoyo_is_same_path_number_acl
967+
(const struct tomoyo_path_number_acl *p1,
968+
const struct tomoyo_path_number_acl *p2)
969+
{
970+
return tomoyo_is_same_acl_head(&p1->head, &p2->head)
971+
&& tomoyo_is_same_name_union(&p1->name, &p2->name)
972+
&& tomoyo_is_same_number_union(&p1->number, &p2->number);
973+
}
974+
891975
static inline bool tomoyo_is_same_domain_initializer_entry
892976
(const struct tomoyo_domain_initializer_entry *p1,
893977
const struct tomoyo_domain_initializer_entry *p2)

0 commit comments

Comments
 (0)