wait: using uninitialized member of wait queue
cover wait: using uninitialized member of wait queue
Hi,
Here is patch which fixes bug in /include/linux/wait.h file. Member "flags" of "wait_queue_t" struct is used in several places in kernel code without beeing initialized. "flags" is used in bitwise operations. If it contain dummy data then incorrect flags may be used later in code.
Thanks,
Best Regards,
Evgeny
Evgeny Kuznetsov (1):
wait: using uninitialized member of wait queue
include/linux/wait.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
PATCH 1/1 wait: using uninitialized member of wait queue
From: Evgeny Kuznetsov <[email protected]>
Member "flags" of "wait_queue_t" struct is used in several places in kernel code without beeing initialized. "flags" is used in bitwise operations. If "flags" not initialized then unexpected behaviour may have place. Incorrect flags maybe used later in code.
Struct "wait_queue_t" is initialized in function "init_wait()". But "init_wait()" do not initialize "flag" member.
Added initialization of "wait_queue_t.flags" with zero value into "init_wait".
--- include/linux/wait.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
include/linux/wait.h +1 -0
@@ -614,6 +614,7 @@ int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); (wait)->private = current; \ (wait)->func = autoremove_wake_function; \INIT_LIST_HEAD (&(wait)->task_list); \ + (wait)->flags =0 ; \ }while (0 )/**
Signature · 1 line
1.6.3.3
Quoted 2 lines · Evgeny Kuznetsov
Member "flags" of "
wait_queue_t" struct is used in several places in kernel code without beeing initialized. "flags" is used in bitwise operations.
^^^^^^ -- "being"
Quoted 4 lines
If "flags" not initialized then unexpected behaviour may have place. Incorrect flags maybe used later in code.
Struct "wait_queue_t" is initialized in function "init_wait()". But "init_wait()" do not initialize "flag" member.
^^ -- does ^^^^ -- "flags"
include/linux/wait.h int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); · line 619
Quoted 13 lines
Added initialization of "wait_queue_t.flags" with zero value into "
init_wait".include/linux/wait.h +1
-0@@ -614,6 +614,7 @@ int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); (wait)->private = current; \ (wait)->func = autoremove_wake_function; \INIT_LIST_HEAD (&(wait)->task_list); \ + (wait)->flags =0 ; \ }while (0 )/**
I'd turn init_wait() into a static inline. Otherwise looks good to me. (Interestingly, init_wait() is used only in 3 places in the kernel and none uses flags.)
Signature · 4 lines
Best regards, _ _ | Humble Liege of Serenely Enlightened Majesty of o' \,=./ `o | Computer Science, Michał "mina86" Nazarewicz (o o) +----[mina86*mina86.com]---[mina86*jabber.org]----ooO--(_)--Ooo--
Hi,
'wait_queue_t' is passed to prepare_to_wait() function where 'flags' is used, e.g:
File: /mm/mempool.c
void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
{ .....
..... init_wait(&wait); prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE); ..... }
Best Regards,
Evgeny
Quoted 43 lines · ext Michał Nazarewicz
Quoted 1 line · Evgeny Kuznetsov
Member "flags" of "
wait_queue_t" struct is used in several placesin
Quoted 1 line
kernel code without beeing initialized. "flags" is used in bitwise
operations.
^^^^^^ -- "being"Quoted 4 lines
If "flags" not initialized then unexpected behaviour may have place. Incorrect flags maybe used later in code.
Struct "wait_queue_t" is initialized in function "init_wait()". But "init_wait()" do not initialize "flag" member.^^ -- does ^^^^ -- "flags"Quoted 1 line
Added initialization of "wait_queue_t.flags" with zero value into
"
init_wait".include/linux/wait.h int wake_bit_function(wait_queue_t *wait,
Quoted 5 lines
include/linux/wait.h +0
-0@@ -614,6 +614,7 @@ int wake_bit_function(wait_queue_t *wait,unsigned mode, int sync, void *key);
include/linux/wait.h int wake_bit_function(wait_queue_t *wait, · line 614
Quoted 1 line
include/linux/wait.h +0
-0(wait)->private =current; \
include/linux/wait.h int wake_bit_function(wait_queue_t *wait, · line 615
Quoted 1 line
include/linux/wait.h +0
-0(wait)->func =
autoremove_wake_function; \include/linux/wait.h int wake_bit_function(wait_queue_t *wait, · line 617
Quoted 2 lines
include/linux/wait.h +1
-0INIT_LIST_HEAD (&(wait)->task_list); \ + (wait)->flags =0; \
include/linux/wait.h int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); · line 620
Quoted 2 lines
include/linux/wait.h +0
-0}while (0 )/** I'd turn
init_wait()into a static inline. Otherwise looks good to me.
(Interestingly,init_wait()is used only in 3 places in the kernel and none uses flags.)Signature · 4 lines
Best regards, _ _ | Humble Liege of Serenely Enlightened Majesty of o' \,=./ `o | Computer Science, Michał "mina86" Nazarewicz (o o) +----[mina86*mina86.com]---[mina86*jabber.org]----ooO--(_)--Ooo--
Quoted 3 lines · ext Michał Nazarewicz
Quoted 2 lines
(Interestingly,
init_wait()is used only in 3 places in the kernel and none uses flags.)
Quoted 10 lines · Evgeny Kuznetsov
'
wait_queue_t' is passed toprepare_to_wait()function where 'flags' is used, e.g:
File: /mm/mempool.c
void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
{ .......... init_wait(&wait); prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE); ..... }
I meant that none of the callers initialized flags. I was initially concerned whether some caller that used init_wait() could set flags and expect that they won't be changed by the call to init_wait() -- this turned out not to be the case.
So essentially, I'm supporting your point. Sorry about the confusion.
Signature · 4 lines
Best regards, _ _ | Humble Liege of Serenely Enlightened Majesty of o' \,=./ `o | Computer Science, Michał "mina86" Nazarewicz (o o) +----[mina86*mina86.com]---[mina86*jabber.org]----ooO--(_)--Ooo--
Quoted 6 lines · Evgeny Kuznetsov
Here is patch which fixes bug in /include/linux/wait.h file. Member "flags" of "
wait_queue_t" struct is used in several places in kernel code without beeing initialized. "flags" is used in bitwise operations. If it contain dummy data then incorrect flags may be used later in code.
The patch looks fine, but do you have any case of this actually causing problems? Afaik, we do end up initializing the part of the flags field we care about (WQ_FLAG_EXCLUSIVE) in prepare_to_wait() (and add_wait_queue()), so it looks - from an admittedly very cursory read - as if this is a good cleanup but shouldn't actually be the cause of any actual bugs.
So I'll apply it, but I just wondered if you had actually seen any semantic changes from it?
Linus
Quoted 16 lines · ext Linus Torvalds
Quoted 6 lines · Evgeny Kuznetsov
Here is patch which fixes bug in /include/linux/wait.h file. Member "flags" of "
wait_queue_t" struct is used in several places in kernel code without beeing initialized. "flags" is used in bitwise operations. If it contain dummy data then incorrect flags may be used later in code.The patch looks fine, but do you have any case of this actually causing problems? Afaik, we do end up initializing the part of the flags field we care about (
WQ_FLAG_EXCLUSIVE) inprepare_to_wait()(andadd_wait_queue()), so it looks - from an admittedly very cursory read - as if this is a good cleanup but shouldn't actually be the cause of any actual bugs.
Quoted 4 lines
So I'll apply it, but I just wondered if you had actually seen any semantic changes from it?
Linus
I did not see any problems caused by uninitialized flag. But this is potential unsafe place (in case of future changes).
Also there is initialization of all members but not 'flags' in init_wait() function. It is logical to initialize all data members.
Thanks,
Regards,
Evgeny