Skip to contentSkip to conversation

RDMA/core: Check invalid QP state for ib_modify_qp_is_ok()

PATCH In review Weihang Li 2 participants 8 messages Mar 19, 2021 – Mar 22, 2021 InfiniBand

lore
Share & tools
lore

Weihang Li

author
#0 lore raw

PATCH 1/1 RDMA/core: Check invalid QP state for ib_modify_qp_is_ok()

From: Xi Wang <[email protected]>

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

---
 drivers/infiniband/core/verbs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/verbs.c +4 -0
index 28464c5..66ba4e6 100644
@@ -1613,6 +1613,10 @@ bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
 		return false;
 
+	if (cur_state >= ARRAY_SIZE(qp_state_table) ||
+	    next_state >= ARRAY_SIZE(qp_state_table[0]))
+		return false;
+
 	if (!qp_state_table[cur_state][next_state].valid)
 		return false;
 
Signature · 1 line
2.8.1
#1 lore raw
Quoted 4 lines · Weihang Li

From: Xi Wang <[email protected]>

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

How is it possible? Do you have call stack to support it?

Thanks

Quoted 25 lines
---
 drivers/infiniband/core/verbs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/verbs.c +4 -0
index 28464c5..66ba4e6 100644
@@ -1613,6 +1613,10 @@ bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
 		return false;
 
+	if (cur_state >= ARRAY_SIZE(qp_state_table) ||
+	    next_state >= ARRAY_SIZE(qp_state_table[0]))
+		return false;
+
 	if (!qp_state_table[cur_state][next_state].valid)
 		return false;
 
Signature · 1 line
2.8.1
author
#2 lore raw
Quoted 10 lines · Leon Romanovsky
Quoted 4 lines · Weihang Li

From: Xi Wang <[email protected]>

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

How is it possible? Do you have call stack to support it?

Thanks

ib_modify_qp_is_ok() is exported, I think any kernel modules can pass in invalid QP state. Should we check it in such case?

Thanks
Weihang

Quoted 25 lines
Quoted 25 lines
---
 drivers/infiniband/core/verbs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/verbs.c +4 -0
index 28464c5..66ba4e6 100644
@@ -1613,6 +1613,10 @@ bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
 		return false;
 
+	if (cur_state >= ARRAY_SIZE(qp_state_table) ||
+	    next_state >= ARRAY_SIZE(qp_state_table[0]))
+		return false;
+
 	if (!qp_state_table[cur_state][next_state].valid)
 		return false;
 
Signature · 1 line
2.8.1
#3 lore raw
Quoted 7 lines · liweihang
Quoted 6 lines · Leon Romanovsky
Quoted 4 lines · Weihang Li

From: Xi Wang <[email protected]>

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

Quoted 7 lines
Quoted 4 lines

How is it possible? Do you have call stack to support it?

Thanks

ib_modify_qp_is_ok() is exported, I think any kernel modules can pass in invalid QP state. Should we check it in such case?

No, it is caller responsibility to supply valid input.
In general case, for the kernel code, it can be seen as anti-pattern if in-kernel API performs input sanity check.

You can add WARN_ON() if you want to catch programmers errors earlier. However, I'm skeptical if it is really needed here.

Thanks

Quoted 30 lines

Thanks
Weihang

Quoted 25 lines
Quoted 25 lines
---
 drivers/infiniband/core/verbs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/verbs.c +4 -0
index 28464c5..66ba4e6 100644
@@ -1613,6 +1613,10 @@ bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
 		return false;
 
+	if (cur_state >= ARRAY_SIZE(qp_state_table) ||
+	    next_state >= ARRAY_SIZE(qp_state_table[0]))
+		return false;
+
 	if (!qp_state_table[cur_state][next_state].valid)
 		return false;
 
Signature · 1 line
2.8.1
author
#4 lore raw
Quoted 13 lines · Leon Romanovsky
Quoted 4 lines · liweihang
Quoted 3 lines · Leon Romanovsky
Quoted 2 lines · Weihang Li

From: Xi Wang <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

How is it possible? Do you have call stack to support it?

Thanks

ib_modify_qp_is_ok() is exported, I think any kernel modules can pass in invalid QP state. Should we check it in such case?

Quoted 9 lines

No, it is caller responsibility to supply valid input.
In general case, for the kernel code, it can be seen as anti-pattern if in-kernel API performs input sanity check.

You can add WARN_ON() if you want to catch programmers errors earlier. However, I'm skeptical if it is really needed here.

Thanks

I see, thank you for the explanation. In that case, we don't need this patch.

Weihang

author
#5 lore raw
In reply to Leon Romanovsky
Quoted 13 lines · Leon Romanovsky
Quoted 4 lines · liweihang
Quoted 3 lines · Leon Romanovsky
Quoted 2 lines · Weihang Li

From: Xi Wang <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

How is it possible? Do you have call stack to support it?

Thanks

ib_modify_qp_is_ok() is exported, I think any kernel modules can pass in invalid QP state. Should we check it in such case?

Quoted 9 lines

No, it is caller responsibility to supply valid input.
In general case, for the kernel code, it can be seen as anti-pattern if in-kernel API performs input sanity check.

You can add WARN_ON() if you want to catch programmers errors earlier. However, I'm skeptical if it is really needed here.

Thanks

Hi Leon,

By the way, we made this change because we noticed that ib_event_msg() and ib_wc_status_msg() that tries to access an array performs input check in the same file. Is there anything different between these kernel APIs? Or there is some other reasons?

Thanks,
Weihang

#6 lore raw
Quoted 24 lines · liweihang
Quoted 13 lines · Leon Romanovsky
Quoted 4 lines · liweihang
Quoted 3 lines · Leon Romanovsky
Quoted 2 lines · Weihang Li

From: Xi Wang <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

How is it possible? Do you have call stack to support it?

Thanks

ib_modify_qp_is_ok() is exported, I think any kernel modules can pass in invalid QP state. Should we check it in such case?

Quoted 9 lines

No, it is caller responsibility to supply valid input.
In general case, for the kernel code, it can be seen as anti-pattern if in-kernel API performs input sanity check.

You can add WARN_ON() if you want to catch programmers errors earlier. However, I'm skeptical if it is really needed here.

Thanks

Quoted 6 lines

Hi Leon,

By the way, we made this change because we noticed that ib_event_msg() and ib_wc_status_msg() that tries to access an array performs input check in the same file. Is there anything different between these kernel APIs? Or there is some other reasons?

The main difference between them is the execution flow.
* ib_modify_qp_is_ok() is called from the drivers, after verbs layer sanitized everything already and at this stage we are pretty safe.
* ib_event_msg()/ib_wc_status_ms() are used by ULPs and maybe they can send invalid event. I personally don't know if it is possible or not.

Thanks

Quoted 4 lines

Thanks,
Weihang

author
#7 lore raw
Quoted 32 lines · Leon Romanovsky
Quoted 24 lines · liweihang
Quoted 13 lines · Leon Romanovsky
Quoted 4 lines · liweihang
Quoted 3 lines · Leon Romanovsky
Quoted 2 lines · Weihang Li

From: Xi Wang <[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Out-of-bounds may occur in 'qp_state_table' when the caller passing wrong QP state value.

How is it possible? Do you have call stack to support it?

Thanks

ib_modify_qp_is_ok() is exported, I think any kernel modules can pass in invalid QP state. Should we check it in such case?

Quoted 9 lines

No, it is caller responsibility to supply valid input.
In general case, for the kernel code, it can be seen as anti-pattern if in-kernel API performs input sanity check.

You can add WARN_ON() if you want to catch programmers errors earlier. However, I'm skeptical if it is really needed here.

Thanks

Quoted 6 lines

Hi Leon,

By the way, we made this change because we noticed that ib_event_msg() and ib_wc_status_msg() that tries to access an array performs input check in the same file. Is there anything different between these kernel APIs? Or there is some other reasons?

Quoted 8 lines

The main difference between them is the execution flow.
* ib_modify_qp_is_ok() is called from the drivers, after verbs layer sanitized everything already and at this stage we are pretty safe.
* ib_event_msg()/ib_wc_status_ms() are used by ULPs and maybe they can send invalid event. I personally don't know if it is possible or not.

Thanks

Thank you, this is helpful.

Weihang