Skip to contentSkip to conversation

unprotected iteration over the IRQ action list in name_unique()

BUG: kernel/irq/proc.c Dmitry Adamushko 1 participant 2 messages Mar 14, 2007 – Mar 15, 2007

lore
Share & tools
lore
#0 lore raw

Hi,

1-st issue: unprotected iteration over the IRQ action list in name_unique()

the racing sequences:

[ 1 ] request_irq() -> setup_irq() -> register_handler_proc() -> name_unique() -> iterate over the action list (*)

setup_irq() releases a desc->lock before calling register_handler_proc().

[ 2 ] free_irq() -> delete some element while (*) is still in progress -> bum!

something like this should make it safe :

irq/proc.c 2007-03-14 16:59:47.236950000 +0100 renamed from irq/proc-old.c 2007-03-14 16:34:52.828981000 +0100 +0 -0
@@ -66,11 +66,18 @@ static int name_unique(unsigned int irq,
{
       struct irq_desc *desc = irq_desc + irq;
       struct irqaction *action;

+ unsigned long flags;
+
+ spin_lock_irqsave(&desc->lock, flags);

       for (action = desc->action ; action; action = action->next)
               if ((action != new_action) && action->name &&
-                               !strcmp(new_action->name, action->name))
+                               !strcmp(new_action->name, action->name)) {
+                       spin_unlock_irqrestore(&desc->lock, flags);
                       return 0;
+               }
+
+       spin_unlock_irqrestore(&desc->lock, flags);
       return 1;
}

2-nd issue: now it's about register_irq_proc() which is also called by setup_irq().

register_irq_proc() is called for all descriptors for which

(irq_desc[irq].chip == &no_irq_chip) != 0 (*)

at startup time from init_irq_proc().

Let's suppose (*) is not true for some desc at startup and changes at some point later.

If so, register_irq_proc() takes effect being called from setup_irq().

Now let's suppose request_irq() is called on 2 cpus for the same interrupt line and both end up in (%) below

[ request_irq() -> setup_irq() -> register_irq_proc() ]

void register_irq_proc(unsigned int irq)

{
       char name [MAX_NAMELEN];

       if (!root_irq_dir ||
               (irq_desc[irq].chip == &no_irq_chip) ||
                       irq_desc[irq].dir)
               return;

           (%)  <=== both are here

       memset(name, 0, MAX_NAMELEN);
       sprintf(name, "%d", irq);

       /* create /proc/irq/1234 */
       irq_desc[irq].dir = proc_mkdir(name, root_irq_dir);

...

Both will try to initialize "irq_desc[irq].dir" and "smp_affinity" entry... Not bum, but a possible leak indeed.

TIA,

Signature · 2 lines
Best regards,
Dmitry Adamushko
#1 lore raw
Quoted 11 lines · Dmitry Adamushko

1-st issue: unprotected iteration over the IRQ action list in name_unique()

the racing sequences:

[ 1 ] request_irq() -> setup_irq() -> register_handler_proc() -> name_unique() -> iterate over the action list (*)

setup_irq() releases a desc->lock before calling register_handler_proc().

[ 2 ] free_irq() -> delete some element while (*) is still in progress -> bum!

"delete" == remove from the list + kfree() as synchronize_irq() is not going to prevent it for obvious reasons.

Of course, request_irq() and free_irq() are called for the same /shared/ irq line but for /different/ handlers.

Looks too obvious to be true. I already expected someone prooving me wrong, at the very least by pointing out a special option of vim to activate some hidden synchronization code :o)

Signature · 2 lines
Best regards,
Dmitry Adamushko