if (READ_ONCE(cqe->flags) & SIW_WQE_VALID) {
                memset(wc, 0, sizeof(*wc));
                wc->wr_id = cqe->id;
-               wc->status = map_cqe_status[cqe->status].ib;
-               wc->opcode = map_wc_opcode[cqe->opcode];
                wc->byte_len = cqe->bytes;
 
                /*
                                wc->wc_flags = IB_WC_WITH_INVALIDATE;
                        }
                        wc->qp = cqe->base_qp;
+                       wc->opcode = map_wc_opcode[cqe->opcode];
+                       wc->status = map_cqe_status[cqe->status].ib;
                        siw_dbg_cq(cq,
                                   "idx %u, type %d, flags %2x, id 0x%pK\n",
                                   cq->cq_get % cq->num_cqe, cqe->opcode,
                                   cqe->flags, (void *)(uintptr_t)cqe->id);
+               } else {
+                       /*
+                        * A malicious user may set invalid opcode or
+                        * status in the user mmapped CQE array.
+                        * Sanity check and correct values in that case
+                        * to avoid out-of-bounds access to global arrays
+                        * for opcode and status mapping.
+                        */
+                       u8 opcode = cqe->opcode;
+                       u16 status = cqe->status;
+
+                       if (opcode >= SIW_NUM_OPCODES) {
+                               opcode = 0;
+                               status = IB_WC_GENERAL_ERR;
+                       } else if (status >= SIW_NUM_WC_STATUS) {
+                               status = IB_WC_GENERAL_ERR;
+                       }
+                       wc->opcode = map_wc_opcode[opcode];
+                       wc->status = map_cqe_status[status].ib;
+
                }
                WRITE_ONCE(cqe->flags, 0);
                cq->cq_get++;
 
 static int siw_sq_flush_wr(struct siw_qp *qp, const struct ib_send_wr *wr,
                           const struct ib_send_wr **bad_wr)
 {
-       struct siw_sqe sqe = {};
        int rv = 0;
 
        while (wr) {
-               sqe.id = wr->wr_id;
-               sqe.opcode = wr->opcode;
-               rv = siw_sqe_complete(qp, &sqe, 0, SIW_WC_WR_FLUSH_ERR);
+               struct siw_sqe sqe = {};
+
+               switch (wr->opcode) {
+               case IB_WR_RDMA_WRITE:
+                       sqe.opcode = SIW_OP_WRITE;
+                       break;
+               case IB_WR_RDMA_READ:
+                       sqe.opcode = SIW_OP_READ;
+                       break;
+               case IB_WR_RDMA_READ_WITH_INV:
+                       sqe.opcode = SIW_OP_READ_LOCAL_INV;
+                       break;
+               case IB_WR_SEND:
+                       sqe.opcode = SIW_OP_SEND;
+                       break;
+               case IB_WR_SEND_WITH_IMM:
+                       sqe.opcode = SIW_OP_SEND_WITH_IMM;
+                       break;
+               case IB_WR_SEND_WITH_INV:
+                       sqe.opcode = SIW_OP_SEND_REMOTE_INV;
+                       break;
+               case IB_WR_LOCAL_INV:
+                       sqe.opcode = SIW_OP_INVAL_STAG;
+                       break;
+               case IB_WR_REG_MR:
+                       sqe.opcode = SIW_OP_REG_MR;
+                       break;
+               default:
+                       rv = -EINVAL;
+                       break;
+               }
+               if (!rv) {
+                       sqe.id = wr->wr_id;
+                       rv = siw_sqe_complete(qp, &sqe, 0,
+                                             SIW_WC_WR_FLUSH_ERR);
+               }
                if (rv) {
                        if (bad_wr)
                                *bad_wr = wr;