Skip to content

Commit 0df7e8b

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Cleanup part 3.
Use common structure for ACL with "struct list_head" + "atomic_t". Use array/struct where possible. Remove is_group from "struct tomoyo_name_union"/"struct tomoyo_number_union". Pass "struct file"->private_data rather than "struct file". Update some of comments. Bring tomoyo_same_acl_head() from common.h to domain.c . Bring tomoyo_invalid()/tomoyo_valid() from common.h to util.c . Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent b5bc60b commit 0df7e8b

9 files changed

Lines changed: 410 additions & 153 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static void tomoyo_print_name_union(struct tomoyo_io_buffer *head,
192192
const struct tomoyo_name_union *ptr)
193193
{
194194
tomoyo_set_space(head);
195-
if (ptr->is_group) {
195+
if (ptr->group) {
196196
tomoyo_set_string(head, "@");
197197
tomoyo_set_string(head, ptr->group->group_name->name);
198198
} else {
@@ -210,15 +210,15 @@ static void tomoyo_print_number_union(struct tomoyo_io_buffer *head,
210210
const struct tomoyo_number_union *ptr)
211211
{
212212
tomoyo_set_space(head);
213-
if (ptr->is_group) {
213+
if (ptr->group) {
214214
tomoyo_set_string(head, "@");
215215
tomoyo_set_string(head, ptr->group->group_name->name);
216216
} else {
217217
int i;
218218
unsigned long min = ptr->values[0];
219219
const unsigned long max = ptr->values[1];
220-
u8 min_type = ptr->min_type;
221-
const u8 max_type = ptr->max_type;
220+
u8 min_type = ptr->value_type[0];
221+
const u8 max_type = ptr->value_type[1];
222222
char buffer[128];
223223
buffer[0] = '\0';
224224
for (i = 0; i < 2; i++) {
@@ -769,7 +769,7 @@ static bool tomoyo_select_one(struct tomoyo_io_buffer *head, const char *data)
769769
domain = tomoyo_find_domain(data + 7);
770770
} else
771771
return false;
772-
head->write_var1 = domain;
772+
head->w.domain = domain;
773773
/* Accessing read_buf is safe because head->io_sem is held. */
774774
if (!head->read_buf)
775775
return true; /* Do nothing if open(O_WRONLY). */
@@ -847,7 +847,7 @@ static int tomoyo_write_domain2(char *data, struct tomoyo_domain_info *domain,
847847
static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
848848
{
849849
char *data = head->write_buf;
850-
struct tomoyo_domain_info *domain = head->write_var1;
850+
struct tomoyo_domain_info *domain = head->w.domain;
851851
bool is_delete = false;
852852
bool is_select = false;
853853
unsigned int profile;
@@ -869,7 +869,7 @@ static int tomoyo_write_domain(struct tomoyo_io_buffer *head)
869869
domain = tomoyo_find_domain(data);
870870
else
871871
domain = tomoyo_assign_domain(data, 0);
872-
head->write_var1 = domain;
872+
head->w.domain = domain;
873873
return 0;
874874
}
875875
if (!domain)
@@ -1250,7 +1250,7 @@ static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx)
12501250
{
12511251
list_for_each_cookie(head->r.group, &tomoyo_group_list[idx]) {
12521252
struct tomoyo_group *group =
1253-
list_entry(head->r.group, typeof(*group), list);
1253+
list_entry(head->r.group, typeof(*group), head.list);
12541254
list_for_each_cookie(head->r.acl, &group->member_list) {
12551255
struct tomoyo_acl_head *ptr =
12561256
list_entry(head->r.acl, typeof(*ptr), list);
@@ -1874,19 +1874,18 @@ int tomoyo_poll_control(struct file *file, poll_table *wait)
18741874
/**
18751875
* tomoyo_read_control - read() for /sys/kernel/security/tomoyo/ interface.
18761876
*
1877-
* @file: Pointer to "struct file".
1877+
* @head: Pointer to "struct tomoyo_io_buffer".
18781878
* @buffer: Poiner to buffer to write to.
18791879
* @buffer_len: Size of @buffer.
18801880
*
18811881
* Returns bytes read on success, negative value otherwise.
18821882
*
18831883
* Caller holds tomoyo_read_lock().
18841884
*/
1885-
int tomoyo_read_control(struct file *file, char __user *buffer,
1885+
int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
18861886
const int buffer_len)
18871887
{
18881888
int len;
1889-
struct tomoyo_io_buffer *head = file->private_data;
18901889

18911890
if (!head->read)
18921891
return -ENOSYS;
@@ -1906,18 +1905,17 @@ int tomoyo_read_control(struct file *file, char __user *buffer,
19061905
/**
19071906
* tomoyo_write_control - write() for /sys/kernel/security/tomoyo/ interface.
19081907
*
1909-
* @file: Pointer to "struct file".
1908+
* @head: Pointer to "struct tomoyo_io_buffer".
19101909
* @buffer: Pointer to buffer to read from.
19111910
* @buffer_len: Size of @buffer.
19121911
*
19131912
* Returns @buffer_len on success, negative value otherwise.
19141913
*
19151914
* Caller holds tomoyo_read_lock().
19161915
*/
1917-
int tomoyo_write_control(struct file *file, const char __user *buffer,
1918-
const int buffer_len)
1916+
int tomoyo_write_control(struct tomoyo_io_buffer *head,
1917+
const char __user *buffer, const int buffer_len)
19191918
{
1920-
struct tomoyo_io_buffer *head = file->private_data;
19211919
int error = buffer_len;
19221920
int avail_len = buffer_len;
19231921
char *cp0 = head->write_buf;
@@ -1935,7 +1933,7 @@ int tomoyo_write_control(struct file *file, const char __user *buffer,
19351933
/* Read a line and dispatch it to the policy handler. */
19361934
while (avail_len > 0) {
19371935
char c;
1938-
if (head->write_avail >= head->writebuf_size - 1) {
1936+
if (head->w.avail >= head->writebuf_size - 1) {
19391937
error = -ENOMEM;
19401938
break;
19411939
} else if (get_user(c, buffer)) {
@@ -1944,11 +1942,11 @@ int tomoyo_write_control(struct file *file, const char __user *buffer,
19441942
}
19451943
buffer++;
19461944
avail_len--;
1947-
cp0[head->write_avail++] = c;
1945+
cp0[head->w.avail++] = c;
19481946
if (c != '\n')
19491947
continue;
1950-
cp0[head->write_avail - 1] = '\0';
1951-
head->write_avail = 0;
1948+
cp0[head->w.avail - 1] = '\0';
1949+
head->w.avail = 0;
19521950
tomoyo_normalize_line(cp0);
19531951
head->write(head);
19541952
}
@@ -1959,15 +1957,14 @@ int tomoyo_write_control(struct file *file, const char __user *buffer,
19591957
/**
19601958
* tomoyo_close_control - close() for /sys/kernel/security/tomoyo/ interface.
19611959
*
1962-
* @file: Pointer to "struct file".
1960+
* @head: Pointer to "struct tomoyo_io_buffer".
19631961
*
19641962
* Releases memory and returns 0.
19651963
*
19661964
* Caller looses tomoyo_read_lock().
19671965
*/
1968-
int tomoyo_close_control(struct file *file)
1966+
int tomoyo_close_control(struct tomoyo_io_buffer *head)
19691967
{
1970-
struct tomoyo_io_buffer *head = file->private_data;
19711968
const bool is_write = !!head->write_buf;
19721969

19731970
/*
@@ -1984,8 +1981,6 @@ int tomoyo_close_control(struct file *file)
19841981
kfree(head->write_buf);
19851982
head->write_buf = NULL;
19861983
kfree(head);
1987-
head = NULL;
1988-
file->private_data = NULL;
19891984
if (is_write)
19901985
tomoyo_run_gc();
19911986
return 0;

‎security/tomoyo/common.h‎

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ struct tomoyo_acl_head {
219219
bool is_deleted;
220220
} __packed;
221221

222+
/* Common header for shared entries. */
223+
struct tomoyo_shared_acl_head {
224+
struct list_head list;
225+
atomic_t users;
226+
} __packed;
227+
222228
/* Structure for request info. */
223229
struct tomoyo_request_info {
224230
struct tomoyo_domain_info *domain;
@@ -281,8 +287,7 @@ struct tomoyo_path_info {
281287

282288
/* Structure for holding string data. */
283289
struct tomoyo_name {
284-
struct list_head list;
285-
atomic_t users;
290+
struct tomoyo_shared_acl_head head;
286291
struct tomoyo_path_info entry;
287292
};
288293

@@ -291,27 +296,21 @@ struct tomoyo_name_union {
291296
/* Either @filename or @group is NULL. */
292297
const struct tomoyo_path_info *filename;
293298
struct tomoyo_group *group;
294-
/* True if @group != NULL, false if @filename != NULL. */
295-
u8 is_group;
296299
};
297300

298301
/* Structure for holding a number. */
299302
struct tomoyo_number_union {
300303
unsigned long values[2];
301304
struct tomoyo_group *group; /* Maybe NULL. */
302305
/* One of values in "enum tomoyo_value_type". */
303-
u8 min_type;
304-
u8 max_type;
305-
/* True if @group != NULL, false otherwise. */
306-
u8 is_group;
306+
u8 value_type[2];
307307
};
308308

309309
/* Structure for "path_group"/"number_group" directive. */
310310
struct tomoyo_group {
311-
struct list_head list;
311+
struct tomoyo_shared_acl_head head;
312312
const struct tomoyo_path_info *group_name;
313313
struct list_head member_list;
314-
atomic_t users;
315314
};
316315

317316
/* Structure for "path_group" directive. */
@@ -429,16 +428,18 @@ struct tomoyo_io_buffer {
429428
bool print_execute_only;
430429
const char *w[TOMOYO_MAX_IO_READ_QUEUE];
431430
} r;
432-
/* The position currently writing to. */
433-
struct tomoyo_domain_info *write_var1;
431+
struct {
432+
/* The position currently writing to. */
433+
struct tomoyo_domain_info *domain;
434+
/* Bytes available for writing. */
435+
int avail;
436+
} w;
434437
/* Buffer for reading. */
435438
char *read_buf;
436439
/* Size of read buffer. */
437440
int readbuf_size;
438441
/* Buffer for writing. */
439442
char *write_buf;
440-
/* Bytes available for writing. */
441-
int write_avail;
442443
/* Size of write buffer. */
443444
int writebuf_size;
444445
/* Type of this interface. */
@@ -500,12 +501,12 @@ void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
500501
__attribute__ ((format(printf, 2, 3)));
501502
void tomoyo_check_profile(void);
502503
int tomoyo_open_control(const u8 type, struct file *file);
503-
int tomoyo_close_control(struct file *file);
504+
int tomoyo_close_control(struct tomoyo_io_buffer *head);
504505
int tomoyo_poll_control(struct file *file, poll_table *wait);
505-
int tomoyo_read_control(struct file *file, char __user *buffer,
506+
int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
506507
const int buffer_len);
507-
int tomoyo_write_control(struct file *file, const char __user *buffer,
508-
const int buffer_len);
508+
int tomoyo_write_control(struct tomoyo_io_buffer *head,
509+
const char __user *buffer, const int buffer_len);
509510
bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
510511
void tomoyo_warn_oom(const char *function);
511512
const struct tomoyo_path_info *
@@ -671,30 +672,6 @@ static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
671672
return a->hash != b->hash || strcmp(a->name, b->name);
672673
}
673674

674-
/**
675-
* tomoyo_valid - Check whether the character is a valid char.
676-
*
677-
* @c: The character to check.
678-
*
679-
* Returns true if @c is a valid character, false otherwise.
680-
*/
681-
static inline bool tomoyo_valid(const unsigned char c)
682-
{
683-
return c > ' ' && c < 127;
684-
}
685-
686-
/**
687-
* tomoyo_invalid - Check whether the character is an invalid char.
688-
*
689-
* @c: The character to check.
690-
*
691-
* Returns true if @c is an invalid character, false otherwise.
692-
*/
693-
static inline bool tomoyo_invalid(const unsigned char c)
694-
{
695-
return c && (c <= ' ' || c >= 127);
696-
}
697-
698675
/**
699676
* tomoyo_put_name - Drop reference on "struct tomoyo_name".
700677
*
@@ -707,7 +684,7 @@ static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
707684
if (name) {
708685
struct tomoyo_name *ptr =
709686
container_of(name, typeof(*ptr), entry);
710-
atomic_dec(&ptr->users);
687+
atomic_dec(&ptr->head.users);
711688
}
712689
}
713690

@@ -721,7 +698,7 @@ static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
721698
static inline void tomoyo_put_group(struct tomoyo_group *group)
722699
{
723700
if (group)
724-
atomic_dec(&group->users);
701+
atomic_dec(&group->head.users);
725702
}
726703

727704
/**
@@ -747,12 +724,6 @@ static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
747724
return task_cred_xxx(task, security);
748725
}
749726

750-
static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *p1,
751-
const struct tomoyo_acl_info *p2)
752-
{
753-
return p1->type == p2->type;
754-
}
755-
756727
/**
757728
* tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
758729
*
@@ -764,8 +735,7 @@ static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *p1,
764735
static inline bool tomoyo_same_name_union
765736
(const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
766737
{
767-
return a->filename == b->filename && a->group == b->group &&
768-
a->is_group == b->is_group;
738+
return a->filename == b->filename && a->group == b->group;
769739
}
770740

771741
/**
@@ -780,8 +750,8 @@ static inline bool tomoyo_same_number_union
780750
(const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
781751
{
782752
return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
783-
a->group == b->group && a->min_type == b->min_type &&
784-
a->max_type == b->max_type && a->is_group == b->is_group;
753+
a->group == b->group && a->value_type[0] == b->value_type[0] &&
754+
a->value_type[1] == b->value_type[1];
785755
}
786756

787757
/**

‎security/tomoyo/domain.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
5858
return error;
5959
}
6060

61+
/**
62+
* tomoyo_same_acl_head - Check for duplicated "struct tomoyo_acl_info" entry.
63+
*
64+
* @a: Pointer to "struct tomoyo_acl_info".
65+
* @b: Pointer to "struct tomoyo_acl_info".
66+
*
67+
* Returns true if @a == @b, false otherwise.
68+
*/
69+
static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a,
70+
const struct tomoyo_acl_info *b)
71+
{
72+
return a->type == b->type;
73+
}
74+
6175
/**
6276
* tomoyo_update_domain - Update an entry for domain policy.
6377
*
@@ -88,7 +102,8 @@ int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
88102
if (mutex_lock_interruptible(&tomoyo_policy_lock))
89103
return error;
90104
list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
91-
if (!check_duplicate(entry, new_entry))
105+
if (!tomoyo_same_acl_head(entry, new_entry) ||
106+
!check_duplicate(entry, new_entry))
92107
continue;
93108
if (merge_duplicate)
94109
entry->is_deleted = merge_duplicate(entry, new_entry,

0 commit comments

Comments
 (0)