Skip to content

mlx5: Add adversarial mode for untrusted devices - #1844

Open
yishaih wants to merge 34 commits into
linux-rdma:masterfrom
yishaih:mlx5_adv
Open

yishaih wants to merge 34 commits into
linux-rdma:masterfrom
yishaih:mlx5_adv

Conversation

@yishaih

@yishaih yishaih commented Nov 3, 2026

Copy link
Copy Markdown
Member

This series adds an adversarial mode to mlx5 for devices the host does not trust.

In this mode the provider treats every value it reads from the device, such as CQEs and context response fields, as potentially hostile, and validates it before use.

The kernel decides when to use the mode. mlx5 advertises support through a new GET_CONTEXT flag, and for a device that requires it, the kernel reports IB_UVERBS_CORE_SUPPORT_ADVERSARIAL. mlx5 then enables adversarial mode for the context.

What the series does:

  • Reduce the attack surface. The mode supports RC QPs using RDMA_WRITE, RDMA_WRITE_WITH_IMM, RDMA_READ and
    ATOMIC_FETCH_AND_ADD. It refuses everything else, including:
    • SRQ, non-RC QPs, MW bind, ODP, DR and AH;
    • CQ resize, CQE compression and scatter-to-CQE;
    • The extended poll and WR APIs.
  • Adversarial data path. It adds dedicated poll_cq, post_send and post_recv implementations. They record each posted WR in SW
    shadow state, cross-check every CQE against that state, and fill the WC from SW state rather than from the CQE.
  • Harden device-reported values. It bound-checks resource IDs, UAR, bfreg and WQ limits relayed by the device before using them
    as indexes, and zeroes DMA buffers and doorbell pages so stale data isn't exposed.

Further details exist as part of the commit logs of the series.

To commit: ef80dabf12dc ("RDMA/mlx5: Advertise adversarial mode via
ib_device::adversarial_mode").

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
calc_sig() reads the WQE linearly from the ctrl segment for up to
(qpn_ds & 0x3f) << 4 bytes, ignoring that the SQ is a cyclic buffer.
When the WQE wraps, it reads past the end of the SQ buffer, producing a
wrong signature or a crash if the following page is not mapped.

Add calc_sig_cyclic(), which wraps the read pointer back to the SQ
start, and use it from wq_sig() at all its call sites.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The loop that zeroes uidx_table[].refcnt used MLX5_QP_TABLE_SIZE, which
happens to have the same numerical value but names the wrong constant for
the array being initialised.  Switch to MLX5_UIDX_TABLE_SIZE to make the
intent clear.  No functional change.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Add a flags argument to ibv_cmd_get_context() so providers can pass
GET_CONTEXT input flags to the kernel. All providers pass 0 for now.

The attribute is optional, so older kernels ignore it.

mlx5 will use it in a later patch to advertise adversarial mode support.

This changes the private provider ABI, so bump IBVERBS_PABI_VERSION.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Move get_cqe() and get_sw_cqe() from cq.c to mlx5.h as
mlx5_get_cqe() and mlx5_get_sw_cqe(), and make mlx5_handle_error_cqe()
non-static, taking the syndrome instead of the error CQE.

This lets the upcoming adversarial poll_cq code reuse them instead of
duplicating them. No functional change.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
With an untrusted device, every CQE byte the provider reads is
device-controlled. Add an adversarial poll_cq for RC workloads using
RDMA_WRITE, RDMA_WRITE_WITH_IMM, RDMA_READ and ATOMIC_FETCH_AND_ADD.

It reads each CQE field once, accepts only the CQE opcodes and
requester sub-opcodes these workloads produce, checks that the CQE qpn
matches the resolved QP, and reports only the wc fields they need.
Anything else fails the poll.

The mode is process-wide: when the kernel reports
IB_UVERBS_CORE_SUPPORT_ADVERSARIAL, mlx5 sets
MLX5_CTX_FLAGS_ADVERSARIAL_DP and overrides the context's poll_cq op.
It stays inactive until mlx5 advertises support to the kernel at the
end of this series.

wc->byte_len of RECV_RDMA_WITH_IMM is reported as given by the device
and cannot be validated by the provider; applications must bound it.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The extended poll API (ibv_start_poll(), ibv_next_poll() and the
ibv_wc_read_*() callbacks) reads CQE fields directly and repeatedly,
bypassing the checks of the adversarial poll_cq.

When MLX5_CTX_FLAGS_ADVERSARIAL_DP is set, make start_poll and
next_poll return EOPNOTSUPP and leave the read callbacks unset.
Extended CQs can still be polled with ibv_poll_cq().

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The extended WR API (ibv_wr_*()) builds WQEs without going through
ibv_post_send(), so it would bypass the adversarial post_send added
later in this series.

Fail QP creation with EOPNOTSUPP when send_ops_flags are requested in
adversarial mode. QPs created without them are not affected.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
mlx5_bind_mw() posts its WQE by calling _mlx5_post_send() directly, so
it would bypass the adversarial post_send added later in this series.
Memory window binding is not supported in adversarial mode, so fail it
with EOPNOTSUPP.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The adversarial data path supports only RC QPs with their own RQ: its
post_send builds RC WQEs only, and its poll_cq tracks receives on the
QP's RQ.

Fail creation of any other QP type, or of an RC QP attached to an SRQ,
with EOPNOTSUPP in adversarial mode.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
QPs attached to an SRQ are already refused in adversarial mode, so an
SRQ has no use there. Fail SRQ creation, both basic and extended, with
EOPNOTSUPP.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
WQE signatures, enabled by the MLX5_QP_SIGNATURE environment variable,
detect bus errors with a trusted device but provide no protection
against an untrusted one. Fail QP creation with EOPNOTSUPP when they
are requested in adversarial mode.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
With scatter-to-CQE the device places small payloads and atomic
results in the CQE instead of the receive buffer. The adversarial
poll_cq does not read inline CQE data, so that data would be lost, and
it would come from untrusted CQE memory anyway.

Clear MLX5_QP_FLAG_SCATTER_CQE for every QP in adversarial mode, and
fail QP creation with EOPNOTSUPP if the application explicitly
requested MLX5DV_QP_CREATE_ALLOW_SCATTER_TO_CQE.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The adversarial poll_cq does not handle the RESIZE_CQ CQE and treats it
as a device error. Fail ibv_resize_cq() with EOPNOTSUPP in adversarial
mode, so that any RESIZE_CQ CQE seen there is known to be forged.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The adversarial poll_cq resolves only QP completions, so every
completion of a WQ (created with ibv_create_wq() or mlx5dv_create_wq())
would fail the poll. Fail WQ creation with EOPNOTSUPP in adversarial
mode instead.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The adversarial poll_cq does not expand compressed (mini) CQEs. Fail CQ
creation with EOPNOTSUPP when CQE compression is requested in
adversarial mode.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
ibv_open_qp opens an XRC target QP, possibly created by another process
through a shared XRCD. This bypasses the RC-only check in create_qp(),
while the adversarial poll_cq and post paths support only RC QPs.

Fail it with EOPNOTSUPP in adversarial mode.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
With ODP, the device raises page faults that the kernel resolves using
fault data supplied by the device. Refuse ODP in adversarial mode to
reduce the attack surface.

Fail with EOPNOTSUPP any MR registration or access change that requests
IBV_ACCESS_ON_DEMAND, and ibv_advise_mr(), which applies only to ODP MRs.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Address handles are used only by UD and DC QPs, which are refused in
adversarial mode. Fail ibv_create_ah() with EOPNOTSUPP.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
DR creates its own RC QP through DevX and parses that QP's CQEs itself,
bypassing the adversarial poll_cq checks. Every DR object requires a
domain, so fail mlx5dv_dr_domain_create() with EOPNOTSUPP in adversarial
mode to block the whole DR API.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
With MLX5DV_QP_CREATE_OOO_DP the device may consume receive WQEs out of
order, so a receive CQE cannot be validated against rq.tail. Add a
per-slot in-flight array to struct mlx5_wq for adversarial QPs created
with OOO_DP. The adversarial post_recv and poll_cq, added in later
patches, set a slot when it is posted and clear it when it completes.
QPs without OOO_DP complete receives in order and keep using rq.tail.

Use a byte per slot rather than a bitmap: post_recv and poll_cq run
under different locks, so a word shared by two slots would lose updates.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Add mlx5_adversarial_post_recv() and install it with the adversarial
poll_cq in adversarial mode.

To avoid duplicating the WQE build loop, move the mlx5_post_recv() body
to mlx5_post_recv_body.h. qp.c and mlx5_adversarial.c both include it,
the latter with ADVERSARIAL_POST_RECV defined. The body is always_inline,
so neither variant pays for an extra call. mlx5_get_recv_wqe(),
mlx5_set_data_ptr_seg() and mlx5_wq_overflow() move to mlx5.h for the
same reason.

Compared with the generic variant, the adversarial one:
- marks each posted slot in rq.in_flight on OOO_DP QPs;
- fails the post with ENOMEM if the target slot is still in flight, as
  the device may complete receives out of order and leave an old slot
  outstanding past an rq.head wrap;
- omits WQE signature and RAW_PACKET doorbell handling, which are not
  supported in adversarial mode.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Add per-QP send-side state that the adversarial post_send records and
the adversarial poll_cq checks completions against, both added in later
patches. Allocate it for every QP in adversarial mode:
- shadow_queue: wqe_counter of each in-flight WR, to validate
  cqe->wqe_counter on REQ and REQ_ERR CQEs;
- signaled_ring: shadow_queue positions of the in-flight signaled WRs,
  to find the oldest one in O(1) on a successful REQ CQE;
- adv_mlx5_opcodes: MLX5_OPCODE_* of each posted WQE, to validate the
  CQE opcode and report wc->opcode from SW state;
- adv_read_len: length of each RDMA_READ, reported as wc->byte_len
  instead of the device-provided byte count.

Reset the ring indices in mlx5_init_qp_indices() so a QP reused after
RESET does not validate completions against stale entries.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Add mlx5_adversarial_post_send() and install it in adversarial mode.

As done for post_recv, move the _mlx5_post_send() body and the helpers
it uses to mlx5_post_send_body.h. qp.c and mlx5_adversarial.c both
include it, the latter with ADVERSARIAL_POST_SEND defined. The moved
helpers get an mlx5_ prefix, mlx5_get_send_wqe() becomes a static
inline in mlx5.h, and dump_wqe() is exported as mlx5_dump_wqe().

Compared with the generic variant, the adversarial one:
- fails a WR with EOPNOTSUPP unless it is RDMA_WRITE,
  RDMA_WRITE_WITH_IMM, RDMA_READ or ATOMIC_FETCH_AND_ADD;
- records each WR in the SQ shadow rings: its wqe_counter in
  shadow_queue, its position in signaled_ring if it generates a CQE,
  its opcode in adv_mlx5_opcodes and, for RDMA_READ, its length in
  adv_read_len;
- omits the UC, UD, XRC and RAW_PACKET arms, the BIND_MW, LOCAL_INV and
  ATOMIC_CMP_AND_SWP arms, and TSO and WQE signature handling, none of
  which are supported in adversarial mode.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Validate each CQE in the adversarial poll_cq against the state the
adversarial post paths record, and fill the wc from that state rather
than from the CQE:
- REQ: wqe_counter must match the oldest in-flight signaled WR, the
  CQE opcode must match the posted opcode, and an RDMA_READ byte count
  must match the requested length. wc->opcode, wr_id and byte_len come
  from SW state.
- REQ_ERR: wqe_counter must match any in-flight WR, since the device
  completes unsignaled WRs too on error, and the opcode must match.
- RESP_WR_IMM and RESP_ERR: wqe_counter must match a posted slot on
  OOO_DP QPs, or the next expected slot otherwise, and at least one
  receive must be outstanding.
- Each completion must arrive on the QP's send_cq or recv_cq, as
  appropriate.

A slot is released only after the wc is filled from it, so a
concurrent post_send or post_recv, which hold a different lock, cannot
reuse it in between. On weakly ordered CPUs, the RQ slot is released
with a release store that pairs with an acquire load in post_recv.

All rejections go through adv_nic_violation(), which logs the reason
and fails the poll.

A completion still does not prove the device is done with the WR's
buffers, as the device can forge a CQE for an in-flight WR.
Applications need their own end-to-end acknowledgments.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The adversarial poll_cq looks up a CQE's QP in the process-wide QP or
uidx table while holding only the polled CQ's lock. A device can forge
a CQE on one CQ that names a QP bound to another CQ. If that QP is
destroyed concurrently, under the other CQ's lock, the poll reads freed
memory before the CQ-binding check rejects the CQE.

Give each CQ in adversarial mode a qpn -> QP table of the QPs bound to
it, and resolve CQEs only against the polled CQ's table. QPs are added
in create_qp() and removed in mlx5_destroy_qp(), both under
mlx5_lock_cqs(), which takes the same CQ locks as the poll. So a QP the
poll finds cannot be freed while it is used, and a QP bound to another
CQ is not found.

The table is keyed by qpn, which identifies an RC QP, the only type
adversarial mode supports. This removes the uidx lookup and the
cqe_version dependency from the adversarial poll. create_qp() refuses a
qp_num that does not fit the 24-bit CQE field, as it could never match.

Entries are embedded in the QP, so registration cannot fail. The table
has 256 buckets and chains on collision, so any number of QPs per CQ is
supported.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
CQ, QP, SRQ and RWQ buffers are allocated in page or chunk granularity,
but only the used part of the CQ, QP and SRQ buffers was cleared, and
the RWQ buffer not at all. The rest of the DMA-mapped region kept stale
heap data that the device can read.

Clear the whole allocated buffer (ibv_buf.size) in mlx5_alloc_cq_buf(),
mlx5_alloc_qp_buf(), mlx5_alloc_srq_buf() and mlx5_alloc_rwq_buf().
Buffers from a custom allocator are left to their owner, as before.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Doorbell record pages are allocated with posix_memalign() and never
cleared, so apart from the records in use they hold stale heap data
the device can read. Chunks reused from the hugepage pool hold the
previous user's data. Clear both on allocation.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
mlx5_store_qp() and mlx5_store_srq() index a two-level table covering
a 24-bit ID space with the QP or SRQ number returned by the kernel from
the device. Nothing checks that the number fits, so a faulty or
malicious device can make them write past qp_table or srq_table when
the resource is created.

Fail such an ID with EINVAL. The store helpers now set errno
themselves, EINVAL or ENOMEM, and modify_dct() no longer overrides it
with ENOMEM. Lookups need no check, as they use 24-bit CQE fields.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
The device-reported num_uars_per_page and bf_reg_size are used without
validation:
- num_uars_per_page == 0 with a non-zero log_uar_size causes a division
  by zero during context setup. Treat it as 1, as for old kernels.
- A num_uars_per_page above page_size / MLX5_ADAPTER_PAGE_SIZE, or a
  bf_reg_size too large for NUM_BFREGS_PER_UAR registers to fit in a
  UAR, places doorbell register pointers outside the mapped page. Fail
  the context with EINVAL.

Store both as uint32_t, matching the response fields, so the checks
are done unsigned.

Also remove context->uar_size. It is never read, and computing it from
the device-reported log_uar_size can overflow the shift.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Without dynamic UAR, map_uuar() indexes ctx->bfs[] with the bfreg_index
from the create QP response, and that entry's register becomes the QP's
doorbell address. Nothing checks the index, so a faulty or malicious
device can select an entry beyond bfs[], or an uninitialized one whose
register is NULL.

Record the number of initialized entries in ctx->num_bfs, and fail QP
creation with EINVAL if bfreg_index is not below it.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
num_sys_page_map is derived from the device-reported tot_bfregs and
bounds the loop that maps context->uar[], a fixed array of
MLX5_MAX_UARS entries. Nothing checks it, so a large tot_bfregs makes
that loop write past uar[].

Compute num_sys_page_map before allocating bfs[], which is sized from
the same value, and fail the context with EINVAL unless it is between
1 and MLX5_MAX_UARS.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
max_recv_wr, max_send_wqebb, max_rq_desc_sz and max_sq_desc_sz come
from __u32 fields of the alloc ucontext response but are stored as int,
so a value of 2^31 or more becomes negative in the QP and SRQ size
checks. Store them as uint32_t.

Also drop the always-false wqe_size <= 0 test in mlx5_calc_rwq_size().

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Pass UVERBS_ATTR_GET_CONTEXT_FLAG_ADVERSARIAL_SUPPORTED to
ibv_cmd_get_context() to tell the kernel that mlx5 implements
adversarial mode. For a device that requires it, the kernel then sets
IB_UVERBS_CORE_SUPPORT_ADVERSARIAL in the response, and mlx5 enables
adversarial mode for the context.

The fallback calls for older kernels keep passing 0, as those kernels
predate the flag.

Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
EdwardSro pushed a commit to Mellanox/nic-kernel that referenced this pull request Nov 19, 2026
In a confidential computing (CC) guest, the hypervisor and any device
assigned to the guest are outside its trust boundary. The mlx5 drivers,
like most drivers, trust the NIC: they use the capabilities, events and
completions it reports to size buffers, index arrays and pick the memory
they work on. A malicious or compromised NIC can turn that trust into
memory corruption or information leaks inside the guest.

This series adds an adversarial mode, in which the mlx5 drivers treat
the NIC as untrusted. It is enabled automatically in CC guests, based on
cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT), with no administrator
configuration. Outside a CC guest the behaviour is unchanged.

The approach is to keep the attack surface small rather than harden
every path. In adversarial mode only the uverbs path is exposed: kernel
ULPs don't bind to the device and unneeded features are not offered.
The data path is owned by rdma-core, which must be aware of the mode.
When it opens a context, rdma-core declares that it supports adversarial
mode, and the kernel reports back that the device is in it, so rdma-core
validates what the NIC writes to the user queues. An rdma-core that
doesn't declare support is refused a context, so it can't use the device
while trusting the NIC. What remains in the kernel validates what it
gets from the NIC. A NIC can always stop working, so denial of service
is out of scope.

We believe that this series provides comprehensive security against an
adversarial device within rdma. Many teams at NVIDIA have been involved
in evaluating the various attack vectors and developing a comprehensive
mitigation, including AI analysis of the driver, fuzzing, and first
principles architecture review. It chooses to block and disable
functions that may open risk, and results in a reduction in driver
functionality rather than leave open potential security holes.

The series is organized as follows:

mlx5_core (patches 1-5)
- Derive adversarial mode from CC guest memory encryption, and keep it
  per device.
- Filter the FW capabilities: start from none, pass through an allow
  list, and validate or clamp the numeric ones, failing the load on
  unexpected values.
- Fail the load when the FW reports fewer EQs than the async EQs need.
- Add helpers to read log-encoded capabilities without an undefined
  shift.
- Add a forced health recovery, for faults the health sensors can't see,
  such as a NIC that keeps answering while forging completions.

Attack surface reduction (patches 6-9)
- Check the FW capability for every QP transport type, and accept the DC
  vendor flags only on IB_QPT_DRIVER.
- Add ib_device::adversarial_mode, set by mlx5_ib from mlx5_core, and
  keep kernel ULPs off such a device by clearing kverbs_provider, so only
  uverbs binds.

UMR completion verification (patches 10-13)
- Verify each UMR CQE against a shadow FIFO of the posted requests,
  instead of trusting its wqe_counter. The UMR CQ is the only CQ the
  kernel polls, so the generic CQ poll path, which trusts the CQE, is not
  used in adversarial mode.
- On a forged CQE, keep the UMR path failed and force the health
  recovery.

Validation of FW reported values (patches 14-20)
- Set the CQ completion handler only once its poll context is set up,
  and ignore completion events for a kernel CQ that is not ready yet.
- Bound port numbers from EQEs and caps.
- Bound RQ/SQ states from QUERY_RQ and QUERY_SQ.
- Clamp the log_max_* shifts used for the CQ, QP and device limits.

Userspace interface (patch 21)
The GET_CONTEXT handshake: the new
UVERBS_ATTR_GET_CONTEXT_FLAG_ADVERSARIAL_SUPPORTED input flag and the
IB_UVERBS_CORE_SUPPORT_ADVERSARIAL core_support bit, also reported by
QUERY_CONTEXT.

The matching rdma-core series [1] declares the support and validates
what it reads from the NIC.

[1] linux-rdma/rdma-core#1844

Changes in V2:
V1: https://lore.kernel.org/r/20261110134320.34408-1-yishaih@nvidia.com
- Patch #2: Keep the mode per device in mlx5_core_dev, set when the PCI
  function is probed. An SF inherits it from its parent function.
  mlx5_is_adversarial() takes the device.
- Patch #4: Reject only a negative count, as 0 completion EQs is valid,
  e.g. for an SF whose max_io_eqs is 0.
- Patch #9: Set ib_device::adversarial_mode from mlx5_core during
  capability init, instead of a mlx5_ib global, and read it in the
  mlx5_ib checks. It replaces V1 patches #8 and #24, and comes after the
  RDMA/core kverbs_provider patch.
- Drop V1 patches #11 and #15. mlx5_ib_poll_cq() checks adversarial mode
  once per call, and then polls only the UMR CQ, by its verifying poller.
  The generic poll path is unchanged, with no indirect call. Patch #12
  carries the rationale.
- Patch #20: Saturate max_res_rd_atom and max_total_mcast_qp_attach with
  check_mul_overflow() instead of min_t(), and cap max_cqe with min().
  Make srq->msrq.max a u32, so the rounded up SRQ size always fits, and
  drop the check after rounding.

Changes in V1:
V0: https://lore.kernel.org/r/20261103163949.1554-1-yishaih@nvidia.com
- Patch #1: Do nothing in the forced work when the command interface is
  down, as the path that brought it down owns the flush and the
  recovery. Fall back to force teardown when fast teardown fails, as
  mlx5_try_fast_unload() does.
- Patch #3: Bound log_min_mkey_entity_size by PAGE_SHIFT, and the max
  mkey entity size caps by BITS_PER_LONG - 1.
- Patch #4: New patch from Dragos, fail the load when the FW reports too
  few EQs to leave a completion EQ.
- Patch #7: Add a Fixes tag.
- Patches #8, #12, #15, #21, #24: Commit message improvements.
- Patch #13: Accept a non-flush UMR error CQE only by moving the UMR
  state from ACTIVE to RECOVER with cmpxchg, and treat it as forged
  otherwise, so a single recovery runs at a time. Narrow the
  mlx5_ib_cq_stop_polling() kernel-doc.
- Patch #15: Fix the comment, the uverbs POLL_CQ command doesn't reach
  this path.
- Patch #17: Cover kernel CQs that are not set up yet. In adversarial
  mode, kernel CQ events are ignored until the first arm, published with
  release/acquire, so a weakly ordered CPU can't see the handler without
  the poll context. Renamed from "Ignore completion events for a CQ
  without a handler".
- Patch #18: Move the port check into handle_port_change(), before
  devr.ports[] is indexed, and drop the now dead check in
  mlx5_ib_handle_event().
- Patch #20: Use check_mul_overflow() for the kernel and user CQ buffer
  sizes.
- Patch #22: Cap max_cqe to the largest CQ size the create path accepts,
  and reject SRQ sizes and buffer sizes that don't fit in an int.

Yishai

Change-Id: Idfdbbc754348ad82f32474995ef13c3bd36ab803
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
EdwardSro pushed a commit to Mellanox/nic-kernel that referenced this pull request Nov 23, 2026
In a confidential computing (CC) guest, the hypervisor and any device
assigned to the guest are outside its trust boundary. The mlx5 drivers,
like most drivers, trust the NIC: they use the capabilities, events and
completions it reports to size buffers, index arrays and pick the memory
they work on. A malicious or compromised NIC can turn that trust into
memory corruption or information leaks inside the guest.

This series adds an adversarial mode, in which the mlx5 drivers treat
the NIC as untrusted. It is enabled automatically in CC guests, based on
cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT), with no administrator
configuration. Outside a CC guest the behaviour is unchanged.

The approach is to keep the attack surface small rather than harden
every path. In adversarial mode only the uverbs path is exposed: kernel
ULPs don't bind to the device and unneeded features are not offered.
The data path is owned by rdma-core, which must be aware of the mode.
When it opens a context, rdma-core declares that it supports adversarial
mode, and the kernel reports back that the device is in it, so rdma-core
validates what the NIC writes to the user queues. An rdma-core that
doesn't declare support is refused a context, so it can't use the device
while trusting the NIC. What remains in the kernel validates what it
gets from the NIC. A NIC can always stop working, so denial of service
is out of scope.

We believe that this series provides comprehensive security against an
adversarial device within rdma. Many teams at NVIDIA have been involved
in evaluating the various attack vectors and developing a comprehensive
mitigation, including AI analysis of the driver, fuzzing, and first
principles architecture review. It chooses to block and disable
functions that may open risk, and results in a reduction in driver
functionality rather than leave open potential security holes.

The series is organized as follows:

mlx5_core (patches 1-5)
- Derive adversarial mode from CC guest memory encryption, and keep it
  per device.
- Filter the FW capabilities: start from none, pass through an allow
  list, and validate or clamp the numeric ones, failing the load on
  unexpected values.
- Fail the load when the FW reports fewer EQs than the async EQs need.
- Add helpers to read log-encoded capabilities without an undefined
  shift.
- Add a forced health recovery, for faults the health sensors can't see,
  such as a NIC that keeps answering while forging completions.

Attack surface reduction (patches 6-9)
- Check the FW capability for every QP transport type, and accept the DC
  vendor flags only on IB_QPT_DRIVER.
- Add ib_device::adversarial_mode, set by mlx5_ib from mlx5_core, and
  keep kernel ULPs off such a device by clearing kverbs_provider, so only
  uverbs binds.

UMR completion verification (patches 10-13)
- Verify each UMR CQE against a shadow FIFO of the posted requests,
  instead of trusting its wqe_counter. The UMR CQ is the only CQ the
  kernel polls, so the generic CQ poll path, which trusts the CQE, is not
  used in adversarial mode.
- On a forged CQE, keep the UMR path failed and force the health
  recovery.

Validation of FW reported values (patches 14-20)
- Set the CQ completion handler only once its poll context is set up,
  and ignore completion events for a kernel CQ that is not ready yet.
- Bound port numbers from EQEs and caps.
- Bound RQ/SQ states from QUERY_RQ and QUERY_SQ.
- Clamp the log_max_* shifts used for the CQ, QP and device limits.

Userspace interface (patch 21)
The GET_CONTEXT handshake: the new
UVERBS_ATTR_GET_CONTEXT_FLAG_ADVERSARIAL_SUPPORTED input flag and the
IB_UVERBS_CORE_SUPPORT_ADVERSARIAL core_support bit, also reported by
QUERY_CONTEXT.

The matching rdma-core series [1] declares the support and validates
what it reads from the NIC.

[1] linux-rdma/rdma-core#1844

Changes in V2:
V1: https://lore.kernel.org/r/20261110134320.34408-1-yishaih@nvidia.com
- Patch #2: Keep the mode per device in mlx5_core_dev, set when the PCI
  function is probed. An SF inherits it from its parent function.
  mlx5_is_adversarial() takes the device.
- Patch #4: Reject only a negative count, as 0 completion EQs is valid,
  e.g. for an SF whose max_io_eqs is 0.
- Patch #9: Set ib_device::adversarial_mode from mlx5_core during
  capability init, instead of a mlx5_ib global, and read it in the
  mlx5_ib checks. It replaces V1 patches #8 and #24, and comes after the
  RDMA/core kverbs_provider patch.
- Drop V1 patches #11 and #15. mlx5_ib_poll_cq() checks adversarial mode
  once per call, and then polls only the UMR CQ, by its verifying poller.
  The generic poll path is unchanged, with no indirect call. Patch #12
  carries the rationale.
- Patch #20: Saturate max_res_rd_atom and max_total_mcast_qp_attach with
  check_mul_overflow() instead of min_t(), and cap max_cqe with min().
  Make srq->msrq.max a u32, so the rounded up SRQ size always fits, and
  drop the check after rounding.

Changes in V1:
V0: https://lore.kernel.org/r/20261103163949.1554-1-yishaih@nvidia.com
- Patch #1: Do nothing in the forced work when the command interface is
  down, as the path that brought it down owns the flush and the
  recovery. Fall back to force teardown when fast teardown fails, as
  mlx5_try_fast_unload() does.
- Patch #3: Bound log_min_mkey_entity_size by PAGE_SHIFT, and the max
  mkey entity size caps by BITS_PER_LONG - 1.
- Patch #4: New patch from Dragos, fail the load when the FW reports too
  few EQs to leave a completion EQ.
- Patch #7: Add a Fixes tag.
- Patches #8, #12, #15, #21, #24: Commit message improvements.
- Patch #13: Accept a non-flush UMR error CQE only by moving the UMR
  state from ACTIVE to RECOVER with cmpxchg, and treat it as forged
  otherwise, so a single recovery runs at a time. Narrow the
  mlx5_ib_cq_stop_polling() kernel-doc.
- Patch #15: Fix the comment, the uverbs POLL_CQ command doesn't reach
  this path.
- Patch #17: Cover kernel CQs that are not set up yet. In adversarial
  mode, kernel CQ events are ignored until the first arm, published with
  release/acquire, so a weakly ordered CPU can't see the handler without
  the poll context. Renamed from "Ignore completion events for a CQ
  without a handler".
- Patch #18: Move the port check into handle_port_change(), before
  devr.ports[] is indexed, and drop the now dead check in
  mlx5_ib_handle_event().
- Patch #20: Use check_mul_overflow() for the kernel and user CQ buffer
  sizes.
- Patch #22: Cap max_cqe to the largest CQ size the create path accepts,
  and reject SRQ sizes and buffer sizes that don't fit in an int.

Yishai

Change-Id: Idfdbbc754348ad82f32474995ef13c3bd36ab803
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Yishai Hadas <yishaih@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant