Skip to content

Commit 7762fbf

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Add pathname grouping support.
This patch adds pathname grouping support, which is useful for grouping pathnames that cannot be represented using /\{dir\}/ pattern. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent ba0c170 commit 7762fbf

6 files changed

Lines changed: 433 additions & 54 deletions

File tree

‎security/tomoyo/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
obj-y = common.o realpath.o tomoyo.o domain.o file.o gc.o
1+
obj-y = common.o realpath.o tomoyo.o domain.o file.o gc.o path_group.o

‎security/tomoyo/common.c‎

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,49 @@ static int tomoyo_read_control(struct file *file, char __user *buffer,
7575
static int tomoyo_write_control(struct file *file, const char __user *buffer,
7676
const int buffer_len);
7777

78+
/**
79+
* tomoyo_parse_name_union - Parse a tomoyo_name_union.
80+
*
81+
* @filename: Name or name group.
82+
* @ptr: Pointer to "struct tomoyo_name_union".
83+
*
84+
* Returns true on success, false otherwise.
85+
*/
86+
bool tomoyo_parse_name_union(const char *filename,
87+
struct tomoyo_name_union *ptr)
88+
{
89+
if (!tomoyo_is_correct_path(filename, 0, 0, 0))
90+
return false;
91+
if (filename[0] == '@') {
92+
ptr->group = tomoyo_get_path_group(filename + 1);
93+
ptr->is_group = true;
94+
return ptr->group != NULL;
95+
}
96+
ptr->filename = tomoyo_get_name(filename);
97+
ptr->is_group = false;
98+
return ptr->filename != NULL;
99+
}
100+
101+
/**
102+
* tomoyo_print_name_union - Print a tomoyo_name_union.
103+
*
104+
* @head: Pointer to "struct tomoyo_io_buffer".
105+
* @ptr: Pointer to "struct tomoyo_name_union".
106+
*
107+
* Returns true on success, false otherwise.
108+
*/
109+
static bool tomoyo_print_name_union(struct tomoyo_io_buffer *head,
110+
const struct tomoyo_name_union *ptr)
111+
{
112+
int pos = head->read_avail;
113+
if (pos && head->read_buf[pos - 1] == ' ')
114+
head->read_avail--;
115+
if (ptr->is_group)
116+
return tomoyo_io_printf(head, " @%s",
117+
ptr->group->group_name->name);
118+
return tomoyo_io_printf(head, " %s", ptr->filename->name);
119+
}
120+
78121
/**
79122
* tomoyo_is_byte_range - Check whether the string isa \ooo style octal value.
80123
*
@@ -171,6 +214,33 @@ static void tomoyo_normalize_line(unsigned char *buffer)
171214
*dp = '\0';
172215
}
173216

217+
/**
218+
* tomoyo_tokenize - Tokenize string.
219+
*
220+
* @buffer: The line to tokenize.
221+
* @w: Pointer to "char *".
222+
* @size: Sizeof @w .
223+
*
224+
* Returns true on success, false otherwise.
225+
*/
226+
bool tomoyo_tokenize(char *buffer, char *w[], size_t size)
227+
{
228+
int count = size / sizeof(char *);
229+
int i;
230+
for (i = 0; i < count; i++)
231+
w[i] = "";
232+
for (i = 0; i < count; i++) {
233+
char *cp = strchr(buffer, ' ');
234+
if (cp)
235+
*cp = '\0';
236+
w[i] = buffer;
237+
if (!cp)
238+
break;
239+
buffer = cp + 1;
240+
}
241+
return i < count || !*buffer;
242+
}
243+
174244
/**
175245
* tomoyo_is_correct_path - Validate a pathname.
176246
* @filename: The pathname to check.
@@ -1368,21 +1438,20 @@ static bool tomoyo_print_path_acl(struct tomoyo_io_buffer *head,
13681438
{
13691439
int pos;
13701440
u8 bit;
1371-
const char *filename;
13721441
const u32 perm = ptr->perm | (((u32) ptr->perm_high) << 16);
13731442

1374-
filename = ptr->filename->name;
13751443
for (bit = head->read_bit; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
1376-
const char *msg;
13771444
if (!(perm & (1 << bit)))
13781445
continue;
13791446
/* Print "read/write" instead of "read" and "write". */
13801447
if ((bit == TOMOYO_TYPE_READ || bit == TOMOYO_TYPE_WRITE)
13811448
&& (perm & (1 << TOMOYO_TYPE_READ_WRITE)))
13821449
continue;
1383-
msg = tomoyo_path2keyword(bit);
13841450
pos = head->read_avail;
1385-
if (!tomoyo_io_printf(head, "allow_%s %s\n", msg, filename))
1451+
if (!tomoyo_io_printf(head, "allow_%s ",
1452+
tomoyo_path2keyword(bit)) ||
1453+
!tomoyo_print_name_union(head, &ptr->name) ||
1454+
!tomoyo_io_printf(head, "\n"))
13861455
goto out;
13871456
}
13881457
head->read_bit = 0;
@@ -1405,21 +1474,18 @@ static bool tomoyo_print_path2_acl(struct tomoyo_io_buffer *head,
14051474
struct tomoyo_path2_acl *ptr)
14061475
{
14071476
int pos;
1408-
const char *filename1;
1409-
const char *filename2;
14101477
const u8 perm = ptr->perm;
14111478
u8 bit;
14121479

1413-
filename1 = ptr->filename1->name;
1414-
filename2 = ptr->filename2->name;
14151480
for (bit = head->read_bit; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
1416-
const char *msg;
14171481
if (!(perm & (1 << bit)))
14181482
continue;
1419-
msg = tomoyo_path22keyword(bit);
14201483
pos = head->read_avail;
1421-
if (!tomoyo_io_printf(head, "allow_%s %s %s\n", msg,
1422-
filename1, filename2))
1484+
if (!tomoyo_io_printf(head, "allow_%s ",
1485+
tomoyo_path22keyword(bit)) ||
1486+
!tomoyo_print_name_union(head, &ptr->name1) ||
1487+
!tomoyo_print_name_union(head, &ptr->name2) ||
1488+
!tomoyo_io_printf(head, "\n"))
14231489
goto out;
14241490
}
14251491
head->read_bit = 0;
@@ -1682,6 +1748,8 @@ static int tomoyo_write_exception_policy(struct tomoyo_io_buffer *head)
16821748
return tomoyo_write_pattern_policy(data, is_delete);
16831749
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_DENY_REWRITE))
16841750
return tomoyo_write_no_rewrite_policy(data, is_delete);
1751+
if (tomoyo_str_starts(&data, TOMOYO_KEYWORD_PATH_GROUP))
1752+
return tomoyo_write_path_group_policy(data, is_delete);
16851753
return -EINVAL;
16861754
}
16871755

@@ -1738,6 +1806,12 @@ static int tomoyo_read_exception_policy(struct tomoyo_io_buffer *head)
17381806
head->read_var2 = NULL;
17391807
head->read_step = 9;
17401808
case 9:
1809+
if (!tomoyo_read_path_group_policy(head))
1810+
break;
1811+
head->read_var1 = NULL;
1812+
head->read_var2 = NULL;
1813+
head->read_step = 10;
1814+
case 10:
17411815
head->read_eof = true;
17421816
break;
17431817
default:

‎security/tomoyo/common.h‎

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct linux_binprm;
5454
#define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
5555
#define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
5656
#define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
57+
#define TOMOYO_KEYWORD_PATH_GROUP "path_group "
5758
#define TOMOYO_KEYWORD_SELECT "select "
5859
#define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
5960
#define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
@@ -204,6 +205,27 @@ struct tomoyo_path_info_with_data {
204205
char barrier2[16]; /* Safeguard for overrun. */
205206
};
206207

208+
struct tomoyo_name_union {
209+
const struct tomoyo_path_info *filename;
210+
struct tomoyo_path_group *group;
211+
u8 is_group;
212+
};
213+
214+
/* Structure for "path_group" directive. */
215+
struct tomoyo_path_group {
216+
struct list_head list;
217+
const struct tomoyo_path_info *group_name;
218+
struct list_head member_list;
219+
atomic_t users;
220+
};
221+
222+
/* Structure for "path_group" directive. */
223+
struct tomoyo_path_group_member {
224+
struct list_head list;
225+
bool is_deleted;
226+
const struct tomoyo_path_info *member_name;
227+
};
228+
207229
/*
208230
* tomoyo_acl_info is a structure which is used for holding
209231
*
@@ -274,7 +296,7 @@ struct tomoyo_domain_info {
274296
*
275297
* (1) "head" which is a "struct tomoyo_acl_info".
276298
* (2) "perm" which is a bitmask of permitted operations.
277-
* (3) "filename" is the pathname.
299+
* (3) "name" is the pathname.
278300
*
279301
* Directives held by this structure are "allow_read/write", "allow_execute",
280302
* "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir",
@@ -287,8 +309,7 @@ struct tomoyo_path_acl {
287309
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
288310
u8 perm_high;
289311
u16 perm;
290-
/* Pointer to single pathname. */
291-
const struct tomoyo_path_info *filename;
312+
struct tomoyo_name_union name;
292313
};
293314

294315
/*
@@ -298,19 +319,17 @@ struct tomoyo_path_acl {
298319
*
299320
* (1) "head" which is a "struct tomoyo_acl_info".
300321
* (2) "perm" which is a bitmask of permitted operations.
301-
* (3) "filename1" is the source/old pathname.
302-
* (4) "filename2" is the destination/new pathname.
322+
* (3) "name1" is the source/old pathname.
323+
* (4) "name2" is the destination/new pathname.
303324
*
304325
* Directives held by this structure are "allow_rename", "allow_link" and
305326
* "allow_pivot_root".
306327
*/
307328
struct tomoyo_path2_acl {
308329
struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
309330
u8 perm;
310-
/* Pointer to single pathname. */
311-
const struct tomoyo_path_info *filename1;
312-
/* Pointer to single pathname. */
313-
const struct tomoyo_path_info *filename2;
331+
struct tomoyo_name_union name1;
332+
struct tomoyo_name_union name2;
314333
};
315334

316335
/*
@@ -514,6 +533,9 @@ struct tomoyo_policy_manager_entry {
514533

515534
/********** Function prototypes. **********/
516535

536+
/* Check whether the given name matches the given name_union. */
537+
bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
538+
const struct tomoyo_name_union *ptr);
517539
/* Check whether the domain has too many ACL entries to hold. */
518540
bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
519541
/* Transactional sprintf() for policy dump. */
@@ -526,6 +548,12 @@ bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
526548
const s8 pattern_type, const s8 end_type);
527549
/* Check whether the token can be a domainname. */
528550
bool tomoyo_is_domain_def(const unsigned char *buffer);
551+
bool tomoyo_parse_name_union(const char *filename,
552+
struct tomoyo_name_union *ptr);
553+
/* Check whether the given filename matches the given path_group. */
554+
bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
555+
const struct tomoyo_path_group *group,
556+
const bool may_use_pattern);
529557
/* Check whether the given filename matches the given pattern. */
530558
bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
531559
const struct tomoyo_path_info *pattern);
@@ -540,10 +568,14 @@ bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
540568
bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
541569
/* Read "file_pattern" entry in exception policy. */
542570
bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
571+
/* Read "path_group" entry in exception policy. */
572+
bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
543573
/* Read "allow_read" entry in exception policy. */
544574
bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
545575
/* Read "deny_rewrite" entry in exception policy. */
546576
bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
577+
/* Tokenize a line. */
578+
bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
547579
/* Write domain policy violation warning message to console? */
548580
bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
549581
/* Convert double path operation to operation name. */
@@ -580,12 +612,18 @@ int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
580612
int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
581613
/* Create "file_pattern" entry in exception policy. */
582614
int tomoyo_write_pattern_policy(char *data, const bool is_delete);
615+
/* Create "path_group" entry in exception policy. */
616+
int tomoyo_write_path_group_policy(char *data, const bool is_delete);
583617
/* Find a domain by the given name. */
584618
struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
585619
/* Find or create a domain by the given name. */
586620
struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
587621
domainname,
588622
const u8 profile);
623+
624+
/* Allocate memory for "struct tomoyo_path_group". */
625+
struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
626+
589627
/* Check mode for specified functionality. */
590628
unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
591629
const u8 index);
@@ -642,6 +680,9 @@ int tomoyo_path2_perm(const u8 operation, struct path *path1,
642680
int tomoyo_check_rewrite_permission(struct file *filp);
643681
int tomoyo_find_next_domain(struct linux_binprm *bprm);
644682

683+
/* Drop refcount on tomoyo_name_union. */
684+
void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
685+
645686
/* Run garbage collector. */
646687
void tomoyo_run_gc(void);
647688

@@ -655,6 +696,7 @@ extern struct srcu_struct tomoyo_ss;
655696
/* The list for "struct tomoyo_domain_info". */
656697
extern struct list_head tomoyo_domain_list;
657698

699+
extern struct list_head tomoyo_path_group_list;
658700
extern struct list_head tomoyo_domain_initializer_list;
659701
extern struct list_head tomoyo_domain_keeper_list;
660702
extern struct list_head tomoyo_alias_list;
@@ -725,6 +767,12 @@ static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
725767
}
726768
}
727769

770+
static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
771+
{
772+
if (group)
773+
atomic_dec(&group->users);
774+
}
775+
728776
static inline struct tomoyo_domain_info *tomoyo_domain(void)
729777
{
730778
return current_cred()->security;
@@ -736,6 +784,34 @@ static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
736784
return task_cred_xxx(task, security);
737785
}
738786

787+
static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
788+
const struct tomoyo_acl_info *p2)
789+
{
790+
return p1->type == p2->type;
791+
}
792+
793+
static inline bool tomoyo_is_same_name_union
794+
(const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
795+
{
796+
return p1->filename == p2->filename && p1->group == p2->group &&
797+
p1->is_group == p2->is_group;
798+
}
799+
800+
static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
801+
const struct tomoyo_path_acl *p2)
802+
{
803+
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
804+
tomoyo_is_same_name_union(&p1->name, &p2->name);
805+
}
806+
807+
static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
808+
const struct tomoyo_path2_acl *p2)
809+
{
810+
return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
811+
tomoyo_is_same_name_union(&p1->name1, &p2->name1) &&
812+
tomoyo_is_same_name_union(&p1->name2, &p2->name2);
813+
}
814+
739815
static inline bool tomoyo_is_same_domain_initializer_entry
740816
(const struct tomoyo_domain_initializer_entry *p1,
741817
const struct tomoyo_domain_initializer_entry *p2)

0 commit comments

Comments
 (0)