Skip to content

Commit 2e503bb

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Fix lockdep warning.
Currently TOMOYO holds SRCU lock upon open() and releases it upon close() because list elements stored in the "struct tomoyo_io_buffer" instances are accessed until close() is called. However, such SRCU usage causes lockdep to complain about leaving the kernel with SRCU lock held. This patch solves the warning by holding/releasing SRCU upon each read()/write(). This patch is doing something similar to calling kfree() without calling synchronize_srcu(), by selectively deferring kfree() by keeping track of the "struct tomoyo_io_buffer" instances. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 5625f2e commit 2e503bb

3 files changed

Lines changed: 276 additions & 51 deletions

File tree

‎security/tomoyo/common.c‎

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,9 +1820,7 @@ static void tomoyo_read_self_domain(struct tomoyo_io_buffer *head)
18201820
* @type: Type of interface.
18211821
* @file: Pointer to "struct file".
18221822
*
1823-
* Associates policy handler and returns 0 on success, -ENOMEM otherwise.
1824-
*
1825-
* Caller acquires tomoyo_read_lock().
1823+
* Returns 0 on success, negative value otherwise.
18261824
*/
18271825
int tomoyo_open_control(const u8 type, struct file *file)
18281826
{
@@ -1921,9 +1919,6 @@ int tomoyo_open_control(const u8 type, struct file *file)
19211919
return -ENOMEM;
19221920
}
19231921
}
1924-
if (type != TOMOYO_QUERY && type != TOMOYO_AUDIT)
1925-
head->reader_idx = tomoyo_read_lock();
1926-
file->private_data = head;
19271922
/*
19281923
* If the file is /sys/kernel/security/tomoyo/query , increment the
19291924
* observer counter.
@@ -1932,6 +1927,8 @@ int tomoyo_open_control(const u8 type, struct file *file)
19321927
*/
19331928
if (type == TOMOYO_QUERY)
19341929
atomic_inc(&tomoyo_query_observers);
1930+
file->private_data = head;
1931+
tomoyo_notify_gc(head, true);
19351932
return 0;
19361933
}
19371934

@@ -2000,27 +1997,28 @@ static inline bool tomoyo_has_more_namespace(struct tomoyo_io_buffer *head)
20001997
* @buffer_len: Size of @buffer.
20011998
*
20021999
* Returns bytes read on success, negative value otherwise.
2003-
*
2004-
* Caller holds tomoyo_read_lock().
20052000
*/
20062001
int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
20072002
const int buffer_len)
20082003
{
20092004
int len;
2005+
int idx;
20102006

20112007
if (!head->read)
20122008
return -ENOSYS;
20132009
if (mutex_lock_interruptible(&head->io_sem))
20142010
return -EINTR;
20152011
head->read_user_buf = buffer;
20162012
head->read_user_buf_avail = buffer_len;
2013+
idx = tomoyo_read_lock();
20172014
if (tomoyo_flush(head))
20182015
/* Call the policy handler. */
20192016
do {
20202017
tomoyo_set_namespace_cursor(head);
20212018
head->read(head);
20222019
} while (tomoyo_flush(head) &&
20232020
tomoyo_has_more_namespace(head));
2021+
tomoyo_read_unlock(idx);
20242022
len = head->read_user_buf - buffer;
20252023
mutex_unlock(&head->io_sem);
20262024
return len;
@@ -2071,21 +2069,21 @@ static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line)
20712069
* @buffer_len: Size of @buffer.
20722070
*
20732071
* Returns @buffer_len on success, negative value otherwise.
2074-
*
2075-
* Caller holds tomoyo_read_lock().
20762072
*/
20772073
int tomoyo_write_control(struct tomoyo_io_buffer *head,
20782074
const char __user *buffer, const int buffer_len)
20792075
{
20802076
int error = buffer_len;
20812077
size_t avail_len = buffer_len;
20822078
char *cp0 = head->write_buf;
2079+
int idx;
20832080
if (!head->write)
20842081
return -ENOSYS;
20852082
if (!access_ok(VERIFY_READ, buffer, buffer_len))
20862083
return -EFAULT;
20872084
if (mutex_lock_interruptible(&head->io_sem))
20882085
return -EINTR;
2086+
idx = tomoyo_read_lock();
20892087
/* Read a line and dispatch it to the policy handler. */
20902088
while (avail_len > 0) {
20912089
char c;
@@ -2148,6 +2146,7 @@ int tomoyo_write_control(struct tomoyo_io_buffer *head,
21482146
}
21492147
}
21502148
out:
2149+
tomoyo_read_unlock(idx);
21512150
mutex_unlock(&head->io_sem);
21522151
return error;
21532152
}
@@ -2157,30 +2156,18 @@ int tomoyo_write_control(struct tomoyo_io_buffer *head,
21572156
*
21582157
* @head: Pointer to "struct tomoyo_io_buffer".
21592158
*
2160-
* Releases memory and returns 0.
2161-
*
2162-
* Caller looses tomoyo_read_lock().
2159+
* Returns 0.
21632160
*/
21642161
int tomoyo_close_control(struct tomoyo_io_buffer *head)
21652162
{
2166-
const bool is_write = !!head->write_buf;
2167-
21682163
/*
21692164
* If the file is /sys/kernel/security/tomoyo/query , decrement the
21702165
* observer counter.
21712166
*/
2172-
if (head->type == TOMOYO_QUERY)
2173-
atomic_dec(&tomoyo_query_observers);
2174-
else if (head->type != TOMOYO_AUDIT)
2175-
tomoyo_read_unlock(head->reader_idx);
2176-
/* Release memory used for policy I/O. */
2177-
kfree(head->read_buf);
2178-
head->read_buf = NULL;
2179-
kfree(head->write_buf);
2180-
head->write_buf = NULL;
2181-
kfree(head);
2182-
if (is_write)
2183-
tomoyo_run_gc();
2167+
if (head->type == TOMOYO_QUERY &&
2168+
atomic_dec_and_test(&tomoyo_query_observers))
2169+
wake_up_all(&tomoyo_answer_wait);
2170+
tomoyo_notify_gc(head, false);
21842171
return 0;
21852172
}
21862173

‎security/tomoyo/common.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,6 @@ struct tomoyo_io_buffer {
441441
int (*poll) (struct file *file, poll_table *wait);
442442
/* Exclusive lock for this structure. */
443443
struct mutex io_sem;
444-
/* Index returned by tomoyo_read_lock(). */
445-
int reader_idx;
446444
char __user *read_user_buf;
447445
int read_user_buf_avail;
448446
struct {
@@ -480,6 +478,10 @@ struct tomoyo_io_buffer {
480478
int writebuf_size;
481479
/* Type of this interface. */
482480
u8 type;
481+
/* Users counter protected by tomoyo_io_buffer_list_lock. */
482+
u8 users;
483+
/* List for telling GC not to kfree() elements. */
484+
struct list_head list;
483485
};
484486

485487
/*
@@ -651,7 +653,7 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm);
651653
void tomoyo_print_ulong(char *buffer, const int buffer_len,
652654
const unsigned long value, const u8 type);
653655
void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
654-
void tomoyo_run_gc(void);
656+
void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
655657
void tomoyo_memory_free(void *ptr);
656658
int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
657659
struct tomoyo_acl_param *param,

0 commit comments

Comments
 (0)