[PATCH net-next] hinic: fix bug of wq out of bound access

Qiao Ma posted 1 patch 4 years ago
drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
[PATCH net-next] hinic: fix bug of wq out of bound access
Posted by Qiao Ma 4 years ago
If wq has only one page, we need to check wqe rolling over page by
compare end_idx and curr_idx, and then copy wqe to shadow wqe to
avoid out of bound access.
This work has been done in hinic_get_wqe, but missed for hinic_read_wqe.
This patch fixes it, and removes unnecessary MASKED_WQE_IDX().

Fixes: 7dd29ee12865 ("hinic: add sriov feature support")
Signed-off-by: Qiao Ma <mqaio@linux.alibaba.com>
---
 drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
index 5dc3743f8091..f04ac00e3e70 100644
--- a/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
+++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
@@ -771,7 +771,7 @@ struct hinic_hw_wqe *hinic_get_wqe(struct hinic_wq *wq, unsigned int wqe_size,
 	/* If we only have one page, still need to get shadown wqe when
 	 * wqe rolling-over page
 	 */
-	if (curr_pg != end_pg || MASKED_WQE_IDX(wq, end_prod_idx) < *prod_idx) {
+	if (curr_pg != end_pg || end_prod_idx < *prod_idx) {
 		void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
 
 		copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *prod_idx);
@@ -841,7 +841,10 @@ struct hinic_hw_wqe *hinic_read_wqe(struct hinic_wq *wq, unsigned int wqe_size,
 
 	*cons_idx = curr_cons_idx;
 
-	if (curr_pg != end_pg) {
+	/* If we only have one page, still need to get shadown wqe when
+	 * wqe rolling-over page
+	 */
+	if (curr_pg != end_pg || end_cons_idx < curr_cons_idx) {
 		void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
 
 		copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *cons_idx);
-- 
1.8.3.1
Re: [PATCH net-next] hinic: fix bug of wq out of bound access
Posted by patchwork-bot+netdevbpf@kernel.org 4 years ago
Hello:

This patch was applied to netdev/net.git (master)
by Jakub Kicinski <kuba@kernel.org>:

On Thu, 28 Apr 2022 20:30:16 +0800 you wrote:
> If wq has only one page, we need to check wqe rolling over page by
> compare end_idx and curr_idx, and then copy wqe to shadow wqe to
> avoid out of bound access.
> This work has been done in hinic_get_wqe, but missed for hinic_read_wqe.
> This patch fixes it, and removes unnecessary MASKED_WQE_IDX().
> 
> Fixes: 7dd29ee12865 ("hinic: add sriov feature support")
> Signed-off-by: Qiao Ma <mqaio@linux.alibaba.com>
> 
> [...]

Here is the summary with links:
  - [net-next] hinic: fix bug of wq out of bound access
    https://git.kernel.org/netdev/net/c/52b2abef450a

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Re: [PATCH net-next] hinic: fix bug of wq out of bound access
Posted by maqiao 4 years ago
cc Paolo Abeni, Guangbin Huang, Kees Cook, Gustavo A. R. Silva

在 2022/4/28 PM8:30, Qiao Ma 写道:
> If wq has only one page, we need to check wqe rolling over page by
> compare end_idx and curr_idx, and then copy wqe to shadow wqe to
> avoid out of bound access.
> This work has been done in hinic_get_wqe, but missed for hinic_read_wqe.
> This patch fixes it, and removes unnecessary MASKED_WQE_IDX().
> 
> Fixes: 7dd29ee12865 ("hinic: add sriov feature support")
> Signed-off-by: Qiao Ma <mqaio@linux.alibaba.com>
> ---
>   drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
> index 5dc3743f8091..f04ac00e3e70 100644
> --- a/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
> +++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
> @@ -771,7 +771,7 @@ struct hinic_hw_wqe *hinic_get_wqe(struct hinic_wq *wq, unsigned int wqe_size,
>   	/* If we only have one page, still need to get shadown wqe when
>   	 * wqe rolling-over page
>   	 */
> -	if (curr_pg != end_pg || MASKED_WQE_IDX(wq, end_prod_idx) < *prod_idx) {
> +	if (curr_pg != end_pg || end_prod_idx < *prod_idx) {
>   		void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
>   
>   		copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *prod_idx);
> @@ -841,7 +841,10 @@ struct hinic_hw_wqe *hinic_read_wqe(struct hinic_wq *wq, unsigned int wqe_size,
>   
>   	*cons_idx = curr_cons_idx;
>   
> -	if (curr_pg != end_pg) {
> +	/* If we only have one page, still need to get shadown wqe when
> +	 * wqe rolling-over page
> +	 */
> +	if (curr_pg != end_pg || end_cons_idx < curr_cons_idx) {
>   		void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
>   
>   		copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *cons_idx);
Re: [PATCH net-next] hinic: fix bug of wq out of bound access
Posted by Xunlei Pang 4 years ago
On 2022/4/28 PM8:46, maqiao wrote:
> cc Paolo Abeni, Guangbin Huang, Kees Cook, Gustavo A. R. Silva
> 
> On 2022/4/28 PM8:30, Qiao Ma wrote:
>> If wq has only one page, we need to check wqe rolling over page by
>> compare end_idx and curr_idx, and then copy wqe to shadow wqe to
>> avoid out of bound access.
>> This work has been done in hinic_get_wqe, but missed for hinic_read_wqe.
>> This patch fixes it, and removes unnecessary MASKED_WQE_IDX().
>>
>> Fixes: 7dd29ee12865 ("hinic: add sriov feature support")
>> Signed-off-by: Qiao Ma <mqaio@linux.alibaba.com>
>> ---
>>   drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c | 7 +++++--
>>   1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c 
>> b/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
>> index 5dc3743f8091..f04ac00e3e70 100644
>> --- a/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
>> +++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_wq.c
>> @@ -771,7 +771,7 @@ struct hinic_hw_wqe *hinic_get_wqe(struct hinic_wq 
>> *wq, unsigned int wqe_size,
>>       /* If we only have one page, still need to get shadown wqe when
>>        * wqe rolling-over page
>>        */
>> -    if (curr_pg != end_pg || MASKED_WQE_IDX(wq, end_prod_idx) < 
>> *prod_idx) {
>> +    if (curr_pg != end_pg || end_prod_idx < *prod_idx) {
>>           void *shadow_addr = &wq->shadow_wqe[curr_pg * 
>> wq->max_wqe_size];
>>           copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *prod_idx);
>> @@ -841,7 +841,10 @@ struct hinic_hw_wqe *hinic_read_wqe(struct 
>> hinic_wq *wq, unsigned int wqe_size,
>>       *cons_idx = curr_cons_idx;
>> -    if (curr_pg != end_pg) {
>> +    /* If we only have one page, still need to get shadown wqe when
>> +     * wqe rolling-over page
>> +     */
>> +    if (curr_pg != end_pg || end_cons_idx < curr_cons_idx) {
>>           void *shadow_addr = &wq->shadow_wqe[curr_pg * 
>> wq->max_wqe_size];
>>           copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *cons_idx);

This is a fundamental problem, and caused kernel panic as follows:

  Unable to handle kernel paging request at virtual address ffff800041371000
  Call trace:
   hinic_sq_get_sges+0x50/0x84 [hinic]
   free_tx_poll+0x84/0x2fc [hinic]
   napi_poll+0xcc/0x270
   net_rx_action+0xd8/0x280
   __do_softirq+0x120/0x37c
   __irq_exit_rcu+0x108/0x140
   irq_exit+0x14/0x20
   __handle_domain_irq+0x84/0xe0
   gic_handle_irq+0x80/0x108

Reviewed-by: Xunlei Pang <xlpang@linux.alibaba.com>