• Omar Sandoval's avatar
    kyber: fix out of bounds access when preempted · efed9a33
    Omar Sandoval authored
    __blk_mq_sched_bio_merge() gets the ctx and hctx for the current CPU and
    passes the hctx to ->bio_merge(). kyber_bio_merge() then gets the ctx
    for the current CPU again and uses that to get the corresponding Kyber
    context in the passed hctx. However, the thread may be preempted between
    the two calls to blk_mq_get_ctx(), and the ctx returned the second time
    may no longer correspond to the passed hctx. This "works" accidentally
    most of the time, but it can cause us to read garbage if the second ctx
    came from an hctx with more ctx's than the first one (i.e., if
    ctx->index_hw[hctx->type] > hctx->nr_ctx).
    
    This manifested as this UBSAN array index out of bounds error reported
    by Jakub:
    
    UBSAN: array-index-out-of-bounds in ../kernel/locking/qspinlock.c:130:9
    index 13106 is out of range for type 'long unsigned int [128]'
    Call Trace:
     dump_stack+0xa4/0xe5
     ubsan_epilogue+0x5/0x40
     __ubsan_handle_out_of_bounds.cold.13+0x2a/0x34
     queued_spin_lock_slowpath+0x476/0x480
     ...
    efed9a33
mq-deadline.c 20.5 KB