RDMA/mana_ib: hardening: Clamp adapter capability values from MANA_IB_GET_ADAPTER_CAP
PATCH v2 1/1 RDMA/mana_ib: hardening: Clamp adapter capability values from MANA_IB_GET_ADAPTER_CAP
As part of MANA hardening for CVM, clamp hardware-reported adapter capability values from the MANA_IB_GET_ADAPTER_CAP response before they are used by the IB subsystem.
The response fields (max_qp_count, max_cq_count, max_mr_count, max_pd_count, max_inbound_read_limit, max_outbound_read_limit, max_qp_wr, max_send_sge_count, max_recv_sge_count) are u32 but are assigned to signed int members in struct ib_device_attr. If hardware returns a value exceeding INT_MAX, the implicit u32-to-int conversion produces a negative value, which can cause incorrect behavior in the IB core and userspace applications.
Clamp these fields to INT_MAX in mana_ib_gd_query_adapter_caps() so all downstream consumers receive safe values.
Additionally, fix an integer overflow in mana_ib_query_device() where max_res_rd_atom is computed as max_qp_rd_atom * max_qp. Both operands are int and the multiplication can overflow. Widen to s64 before multiplying and clamp the result to INT_MAX.
---
Changes in v2:
* Update patch title.
--- drivers/infiniband/hw/mana/main.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/infiniband/hw/mana/main.c +12 -9
@@ -599,7 +599,8 @@ int mana_ib_query_device(struct ib_device *ibdev, struct ib_device_attr *props, props->max_mr = dev->adapter_caps.max_mr_count; props->max_pd = dev->adapter_caps.max_pd_count; props->max_qp_rd_atom = dev->adapter_caps.max_inbound_read_limit; - props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; + props->max_res_rd_atom = +min_t (s64, (s64 )props->max_qp_rd_atom * props->max_qp,INT_MAX ); props->max_qp_init_rd_atom = dev->adapter_caps.max_outbound_read_limit; props->atomic_cap =IB_ATOMIC_NONE ; props->masked_atomic_cap =IB_ATOMIC_NONE ;
@@ -694,20 +695,22 @@ int mana_ib_gd_query_adapter_caps(struct mana_ib_dev *dev) caps->max_sq_id = resp.max_sq_id; caps->max_rq_id = resp.max_rq_id; caps->max_cq_id = resp.max_cq_id; - caps->max_qp_count = resp.max_qp_count; - caps->max_cq_count = resp.max_cq_count; - caps->max_mr_count = resp.max_mr_count; - caps->max_pd_count = resp.max_pd_count; - caps->max_inbound_read_limit = resp.max_inbound_read_limit; - caps->max_outbound_read_limit = resp.max_outbound_read_limit; + caps->max_qp_count =min_t (u32, resp.max_qp_count,INT_MAX ); + caps->max_cq_count =min_t (u32, resp.max_cq_count,INT_MAX ); + caps->max_mr_count =min_t (u32, resp.max_mr_count,INT_MAX ); + caps->max_pd_count =min_t (u32, resp.max_pd_count,INT_MAX ); + caps->max_inbound_read_limit =min_t (u32, resp.max_inbound_read_limit, +INT_MAX ); + caps->max_outbound_read_limit =min_t (u32, resp.max_outbound_read_limit, +INT_MAX ); caps->mw_count = resp.mw_count; caps->max_srq_count = resp.max_srq_count; caps->max_qp_wr =min_t (u32, resp.max_requester_sq_size /GDMA_MAX_SQE_SIZE , resp.max_requester_rq_size /GDMA_MAX_RQE_SIZE ); caps->max_inline_data_size = resp.max_inline_data_size; - caps->max_send_sge_count = resp.max_send_sge_count; - caps->max_recv_sge_count = resp.max_recv_sge_count; + caps->max_send_sge_count =min_t (u32, resp.max_send_sge_count,INT_MAX ); + caps->max_recv_sge_count =min_t (u32, resp.max_recv_sge_count,INT_MAX ); caps->feature_flags = resp.feature_flags; caps->page_size_cap =PAGE_SZ_BM ;
Signature · 1 line
2.34.1
Quoted 18 lines
Subject: [PATCH rdma-next v2] RDMA/mana_ib: hardening: Clamp adapter capability values from
MANA_IB_GET_ADAPTER_CAPAs part of MANA hardening for CVM, clamp hardware-reported adapter capability values from the
MANA_IB_GET_ADAPTER_CAPresponse before they are used by the IB subsystem.The response fields (
max_qp_count,max_cq_count,max_mr_count,max_pd_count,max_inbound_read_limit,max_outbound_read_limit,max_qp_wr,max_send_sge_count,max_recv_sge_count) are u32 but are assigned to signed int members in structib_device_attr. If hardware returns a value exceedingINT_MAX, the implicit u32-to-int conversion produces a negative value, which can cause incorrect behavior in the IB core and userspace applications.Clamp these fields to
INT_MAXinmana_ib_gd_query_adapter_caps()so all downstream consumers receive safe values.
Quoted 6 lines
Additionally, fix an integer overflow in
mana_ib_query_device()wheremax_res_rd_atomis computed asmax_qp_rd_atom*max_qp. Both operands are int and the multiplication can overflow. Widen to s64 before multiplying and clamp the result toINT_MAX.Signed-off-by Erni Sri Satya Vennela <[email protected]>
Quoted 64 lines
---
Changes in v2:
* Update patch title.--- drivers/infiniband/hw/mana/main.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)drivers/infiniband/hw/mana/main.c +2
-1@@ -599,7 +599,8 @@ int mana_ib_query_device(struct ib_device *ibdev, struct ib_device_attr *props, props->max_mr = dev->adapter_caps.max_mr_count; props->max_pd = dev->adapter_caps.max_pd_count; props->max_qp_rd_atom = dev->adapter_caps.max_inbound_read_limit; - props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; + props->max_res_rd_atom = +min_t (s64, (s64 )props->max_qp_rd_atom * props->max_qp,INT_MAX ); props->max_qp_init_rd_atom = dev-Quoted 1 line
adapter_caps.max_outbound_read_limit;
drivers/infiniband/hw/mana/main.c +0
-0props->atomic_cap =IB_ATOMIC_NONE ; props->masked_atomic_cap = IB_ATOMIC_NONE; @@ -694,20 +695,22@@ int
mana_ib_gd_query_adapter_caps(struct mana_ib_dev *dev)drivers/infiniband/hw/mana/main.c +5
-6caps->max_sq_id = resp.max_sq_id; caps->max_rq_id = resp.max_rq_id; caps->max_cq_id = resp.max_cq_id; - caps->max_qp_count = resp.max_qp_count; - caps->max_cq_count = resp.max_cq_count; - caps->max_mr_count = resp.max_mr_count; - caps->max_pd_count = resp.max_pd_count; - caps->max_inbound_read_limit = resp.max_inbound_read_limit; - caps->max_outbound_read_limit = resp.max_outbound_read_limit; + caps->max_qp_count =min_t (u32, resp.max_qp_count,INT_MAX ); + caps->max_cq_count =min_t (u32, resp.max_cq_count,INT_MAX ); + caps->max_mr_count =min_t (u32, resp.max_mr_count,INT_MAX ); + caps->max_pd_count =min_t (u32, resp.max_pd_count,INT_MAX ); + caps->max_inbound_read_limit = min_t(u32,resp.max_inbound_read_limit,
drivers/infiniband/hw/mana/main.c +2
-0+ INT_MAX); + caps->max_outbound_read_limit = min_t(u32,resp.max_outbound_read_limit,
drivers/infiniband/hw/mana/main.c +1
-0+ INT_MAX); caps->mw_count = resp.mw_count; caps->max_srq_count = resp.max_srq_count; caps->max_qp_wr = min_t(u32, resp.max_requester_sq_size /
GDMA_MAX_SQE_SIZE,
resp.max_requester_rq_size /GDMA_MAX_RQE_SIZE);drivers/infiniband/hw/mana/main.c +1
-2caps->max_inline_data_size = resp.max_inline_data_size; - caps->max_send_sge_count = resp.max_send_sge_count; - caps->max_recv_sge_count = resp.max_recv_sge_count; + caps->max_send_sge_count = min_t(u32, resp.max_send_sge_count,
INT_MAX);drivers/infiniband/hw/mana/main.c +2
-1+ caps->max_recv_sge_count =min_t (u32, resp.max_recv_sge_count, +INT_MAX ); caps->feature_flags = resp.feature_flags; caps->page_size_cap =PAGE_SZ_BM ; --2.34.1
Quoted 4 lines · Erni Sri Satya Vennela
The response fields (
max_qp_count,max_cq_count,max_mr_count,max_pd_count,max_inbound_read_limit,max_outbound_read_limit,max_qp_wr,max_send_sge_count,max_recv_sge_count) are u32 but are assigned to signed int members in structib_device_attr.
There is no reason they should be signed, you should just fix the type.
I'm also not convinced clamping to such a high value has any value whatsoever, as it probably still triggers maths overflows elsewhere. I think you should clamp to reasonable limits for your device if you want to do this.
Jason
Quoted 11 lines · Erni Sri Satya Vennela
As part of MANA hardening for CVM, clamp hardware-reported adapter capability values from the
MANA_IB_GET_ADAPTER_CAPresponse before they are used by the IB subsystem.The response fields (
max_qp_count,max_cq_count,max_mr_count,max_pd_count,max_inbound_read_limit,max_outbound_read_limit,max_qp_wr,max_send_sge_count,max_recv_sge_count) are u32 but are assigned to signed int members in structib_device_attr. If hardware returns a value exceedingINT_MAX, the implicit u32-to-int conversion produces a negative value, which can cause incorrect behavior in the IB core and userspace applications.
This sentence does not make sense in the context of the Linux kernel. The fundamental assumption is that the underlying hardware behaves correctly, and driver code should not attempt to guard against purely hypothetical failures. The kernel only implements such self‑protection when there is a documented hardware issue accompanied by official errata.
Thanks
Quoted 13 lines · Erni Sri Satya Vennela
Quoted 11 lines
As part of MANA hardening for CVM, clamp hardware-reported adapter capability values from the
MANA_IB_GET_ADAPTER_CAPresponse before they are used by the IB subsystem.The response fields (
max_qp_count,max_cq_count,max_mr_count,max_pd_count,max_inbound_read_limit,max_outbound_read_limit,max_qp_wr,max_send_sge_count,max_recv_sge_count) are u32 but are assigned to signed int members in structib_device_attr. If hardware returns a value exceedingINT_MAX, the implicit u32-to-int conversion produces a negative value, which can cause incorrect behavior in the IB core and userspace applications.
Quoted 7 lines
This sentence does not make sense in the context of the Linux kernel. The fundamental assumption is that the underlying hardware behaves correctly, and driver code should not attempt to guard against purely hypothetical failures. The kernel only implements such self‑protection when there is a documented hardware issue accompanied by official errata.
Thanks
The idea is that a malicious hardware can't corrupt and steal other data from the kernel.
The assumption is that in a public cloud environment, you can't trust the hardware 100%.
Quoted 19 lines · Long Li
Quoted 19 lines · Erni Sri Satya Vennela
Quoted 11 lines
As part of MANA hardening for CVM, clamp hardware-reported adapter capability values from the
MANA_IB_GET_ADAPTER_CAPresponse before they are used by the IB subsystem.The response fields (
max_qp_count,max_cq_count,max_mr_count,max_pd_count,max_inbound_read_limit,max_outbound_read_limit,max_qp_wr,max_send_sge_count,max_recv_sge_count) are u32 but are assigned to signed int members in structib_device_attr. If hardware returns a value exceedingINT_MAX, the implicit u32-to-int conversion produces a negative value, which can cause incorrect behavior in the IB core and userspace applications.This sentence does not make sense in the context of the Linux kernel. The fundamental assumption is that the underlying hardware behaves correctly, and driver code should not attempt to guard against purely hypothetical failures. The kernel only implements such self‑protection when there is a documented hardware issue accompanied by official errata.
Quoted 5 lines
Quoted 1 line
Thanks
The idea is that a malicious hardware can't corrupt and steal other data from the kernel.
The assumption is that in a public cloud environment, you can't trust the hardware 100%.
You cannot separate functionality and claim that one line of code is trusted while another is not.
Thanks
-next v2] RDMA/mana_ib: hardening:
Quoted 28 lines
Clamp adapter capability values from
MANA_IB_GET_ADAPTER_CAPQuoted 13 lines · Long Li
Quoted 13 lines · Erni Sri Satya Vennela
Quoted 11 lines
As part of MANA hardening for CVM, clamp hardware-reported adapter capability values from the
MANA_IB_GET_ADAPTER_CAPresponse before they are used by the IB subsystem.The response fields (
max_qp_count,max_cq_count,max_mr_count,max_pd_count,max_inbound_read_limit,max_outbound_read_limit,max_qp_wr,max_send_sge_count,max_recv_sge_count) are u32 but are assigned to signed int members in structib_device_attr. If hardware returns a value exceedingINT_MAX, the implicit u32-to-int conversion produces a negative value, which can cause incorrect behavior in the IB core and userspace applications.Quoted 5 lines
Quoted 5 lines
This sentence does not make sense in the context of the Linux kernel. The fundamental assumption is that the underlying hardware behaves correctly, and driver code should not attempt to guard against purely hypothetical failures. The kernel only implements such self‑protection when there is a documented hardware issue accompanied by
official errata.
Quoted 4 lines
Quoted 2 lines
Thanks
The idea is that a malicious hardware can't corrupt and steal other data from
the kernel.
Quoted 1 line
Quoted 7 lines
Quoted 1 line
The assumption is that in a public cloud environment, you can't trust the
hardware 100%.
You cannot separate functionality and claim that one line of code is trusted while another is not.
Thanks
How we rephrase this in this way: the driver should not corrupt or overflow other parts of the kernel if its device is misbehaving (or has a bug).
Long
Quoted 30 lines · Long Li
-next v2] RDMA/mana_ib: hardening:
Quoted 29 lines
Clamp adapter capability values from
MANA_IB_GET_ADAPTER_CAPQuoted 13 lines · Long Li
Quoted 13 lines · Erni Sri Satya Vennela
Quoted 11 lines
As part of MANA hardening for CVM, clamp hardware-reported adapter capability values from the
MANA_IB_GET_ADAPTER_CAPresponse before they are used by the IB subsystem.The response fields (
max_qp_count,max_cq_count,max_mr_count,max_pd_count,max_inbound_read_limit,max_outbound_read_limit,max_qp_wr,max_send_sge_count,max_recv_sge_count) are u32 but are assigned to signed int members in structib_device_attr. If hardware returns a value exceedingINT_MAX, the implicit u32-to-int conversion produces a negative value, which can cause incorrect behavior in the IB core and userspace applications.Quoted 5 lines
Quoted 5 lines
This sentence does not make sense in the context of the Linux kernel. The fundamental assumption is that the underlying hardware behaves correctly, and driver code should not attempt to guard against purely hypothetical failures. The kernel only implements such self‑protection when there is a documented hardware issue accompanied by
official errata.
Quoted 4 lines
Quoted 2 lines
Thanks
The idea is that a malicious hardware can't corrupt and steal other data from
the kernel.
Quoted 2 lines
The assumption is that in a public cloud environment, you can't trust the
Quoted 8 lines
Quoted 6 lines
hardware 100%.
You cannot separate functionality and claim that one line of code is trusted while another is not.
Thanks
How we rephrase this in this way: the driver should not corrupt or overflow other parts of the kernel if its device is misbehaving (or has a bug).
It shouldn't be theoretical claim, do you have errata?
Thanks
Quoted 2 lines
Long
Quoted 3 lines · Long Li
How we rephrase this in this way: the driver should not corrupt or overflow other parts of the kernel if its device is misbehaving (or has a bug).
If we are going to do this CC hardening stuff I think I want to see a more comphrensive approach, like if we detect an attack then the kernel instantly crashes or something. Or at least an approach in general agreed to by the CC and kernel community.
Igoring the issue and continuing seems just wrong.
This sprinkling of random checks in this series doesn't feel comprehensive or cohesive to me.
Jason
Quoted 11 lines · Long Li
Quoted 4 lines
How we rephrase this in this way: the driver should not corrupt or overflow other parts of the kernel if its device is misbehaving (or has a bug).
If we are going to do this CC hardening stuff I think I want to see a more comphrensive approach, like if we detect an attack then the kernel instantly crashes or something. Or at least an approach in general agreed to by the CC and kernel community.
Quoted 6 lines
Igoring the issue and continuing seems just wrong.
This sprinkling of random checks in this series doesn't feel comprehensive or cohesive to me.
Jason
Can we follow the virtio BAD_RING()/vq->broken pattern in https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/virtio/virtio_ring.c#n57.
Add a broken flag to mana_ib_dev. When any hardware response contains out-of-range values, mark the device broken and fail the operation - during probe this prevents device registration entirely, at runtime all subsequent operations return -EIO.
Long
Quoted 18 lines · Long Li
Quoted 11 lines · Long Li
Quoted 4 lines
How we rephrase this in this way: the driver should not corrupt or overflow other parts of the kernel if its device is misbehaving (or has a bug).
If we are going to do this CC hardening stuff I think I want to see a more comphrensive approach, like if we detect an attack then the kernel instantly crashes or something. Or at least an approach in general agreed to by the CC and kernel community.
Quoted 6 lines
Igoring the issue and continuing seems just wrong.
This sprinkling of random checks in this series doesn't feel comprehensive or cohesive to me.
Jason
Quoted 7 lines
Can we follow the virtio BAD_RING()/vq->broken pattern in
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/virtio/virtio_ring.c#n57.Add a broken flag to
mana_ib_dev. When any hardware response contains out-of-range values, mark the device broken and fail the operation - during probe this prevents device registration entirely, at runtime all subsequent operations return-EIO.
If that's the plan I would think it should be struct device based, but yeah, I'm more comfortable with this sort of direction as a CC hardening plan.
Jason
Quoted 35 lines · Long Li
Quoted 11 lines
Quoted 11 lines · Long Li
Quoted 4 lines
How we rephrase this in this way: the driver should not corrupt or overflow other parts of the kernel if its device is misbehaving (or has a bug).
If we are going to do this CC hardening stuff I think I want to see a more comphrensive approach, like if we detect an attack then the kernel instantly crashes or something. Or at least an approach in general agreed to by the CC and kernel community.
Quoted 9 lines
Quoted 6 lines
Igoring the issue and continuing seems just wrong.
This sprinkling of random checks in this series doesn't feel comprehensive or cohesive to me.
Jason
Can we follow the virtio BAD_RING()/vq->broken pattern in
https://git.kernel/
.org%2Fpub%2Fscm%2Flinux%2Fkernel%2Fgit%2Ftorvalds%2Flinux.git%2Ftree%2 Fdrivers%2Fvirtio%2Fvirtio_ring.c%23n57&data=05%7C02%7Clongli%40microsoft .com%7C698adb98daa64e20184708de996302b5%7C72f988bf86f141af91ab2d7c d011db47%7C1%7C0%7C639116847704528406%7CUnknown%7CTWFpbGZsb3d 8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoi TWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=HkjUfCDysbQKTuiCsiQai gySStd%2BI3VrHUnfMC%2FBORc%3D&reserved=0.Quoted 5 lines
Add a broken flag to
mana_ib_dev. When any hardware response contains out-of-range values, mark the device broken and fail the operation - during probe this prevents device registration entirely, at runtime all subsequent operations return-EIO.
Quoted 4 lines
If that's the plan I would think it should be struct device based, but yeah, I'm more comfortable with this sort of direction as a CC hardening plan.
Jason
Will do, thank you.
Long
Quoted 22 lines · Jason Gunthorpe
Quoted 21 lines · Long Li
Quoted 11 lines · Long Li
Quoted 4 lines
How we rephrase this in this way: the driver should not corrupt or overflow other parts of the kernel if its device is misbehaving (or has a bug).
If we are going to do this CC hardening stuff I think I want to see a more comphrensive approach, like if we detect an attack then the kernel instantly crashes or something. Or at least an approach in general agreed to by the CC and kernel community.
Quoted 6 lines
Igoring the issue and continuing seems just wrong.
This sprinkling of random checks in this series doesn't feel comprehensive or cohesive to me.
Jason
Can we follow the virtio BAD_RING()/vq->broken pattern in
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/virtio/virtio_ring.c#n57.
Quoted 9 lines
Quoted 4 lines
Add a broken flag to
mana_ib_dev. When any hardware response contains out-of-range values, mark the device broken and fail the operation - during probe this prevents device registration entirely, at runtime all subsequent operations return-EIO.If that's the plan I would think it should be struct device based, but yeah, I'm more comfortable with this sort of direction as a CC hardening plan.
Hi Jason,
Our team is not aligned with marking the device broken, after multiple discussions, since both the values that are received from hardware and stored in mana_ib_gd_query_adapter_caps are u32.
I'm planning to send v3 as a non-hardening patch with only clamping the values at mana_ib_query_device to INT_MAX when out-of-bound.
Your previous concerns:
Quoted 4 lines
“I'm also not convinced clamping to such a high value has any value whatsoever, as it probably still triggers maths overflows elsewhere. I think you should clamp to reasonable limits for your device if you want to do this.”
We plan to clamp it to INT_MAX since it is the max in props.
Quoted 2 lines
“There is no reason they should be signed, you should just fix the type.”
It is not allowed to change sign in props, so clamping is the best bet.
Thanks,
Vennela
Quoted 4 lines · Erni Sri Satya Vennela
Quoted 2 lines
“There is no reason they should be signed, you should just fix the type.”
It is not allowed to change sign in props, so clamping is the best bet.
Why not? Fix the core code, it is just old junk they are signed, they should't never have been.
Jason
Quoted 10 lines · Jason Gunthorpe
Quoted 4 lines · Erni Sri Satya Vennela
Quoted 2 lines
“There is no reason they should be signed, you should just fix the type.”
It is not allowed to change sign in props, so clamping is the best bet.
Why not? Fix the core code, it is just old junk they are signed, they should't never have been.
Jason
Thanks for the feedback, Jason.
I sent the v3 before your comments in v2.
I'll be sending a v4 which drops the clamping entirely.
- Vennela