Re: [GIT PULL] /dev/random bug fixes for 4.12

From: Linus Torvalds
Date: Fri Jun 02 2017 - 19:30:44 EST


On Fri, Jun 2, 2017 at 2:30 PM, Theodore Ts'o <tytso@xxxxxxx> wrote:
>
> Fix a race on architectures with prioritized interrupts (such as m68k)
> which can causes crashes in drivers/char/random.c:get_reg().

I don't think this has anything to do with prioritized interrupts,
just possibility of nesting (which can happen anywhere).

I've pulled this, but I really am not all that happy about it. It adds
a interrupt disable/enable in something that might be pretty
timing-ciritcal.

And I really don't think it needs it. We don't actually care about the
reg_idx value being reliable, so the code could easily have just done
some READ_ONCE/WRITE_ONCE thing. Something like the attached
(UNTESTED!) patch instead.

But it's in my tree now in your form.

Linus
drivers/char/random.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index a561f0c2f428..473ad34378f2 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1097,15 +1097,15 @@ static void add_interrupt_bench(cycles_t start)
static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
{
__u32 *ptr = (__u32 *) regs;
- unsigned long flags;
+ unsigned int idx;

if (regs == NULL)
return 0;
- local_irq_save(flags);
- if (f->reg_idx >= sizeof(struct pt_regs) / sizeof(__u32))
- f->reg_idx = 0;
- ptr += f->reg_idx++;
- local_irq_restore(flags);
+ idx = READ_ONCE(f->reg_idx);
+ if (idx >= sizeof(struct pt_regs) / sizeof(__u32))
+ idx = 0;
+ ptr += idx++;
+ WRITE_ONCE(f->reg_idx, idx);
return *ptr;
}