Skip to content

Commit 0f05406

Browse files
Yuval Bassongregkh
authored andcommitted
qed: Fix use after free in qed_chain_free
commit 8063f76 upstream. The qed_chain data structure was modified in commit 1a4a697 ("qed: Chain support for external PBL") to support receiving an external pbl (due to iWARP FW requirements). The pages pointed to by the pbl are allocated in qed_chain_alloc and their virtual address are stored in an virtual addresses array to enable accessing and freeing the data. The physical addresses however weren't stored and were accessed directly from the external-pbl during free. Destroy-qp flow, leads to freeing the external pbl before the chain is freed, when the chain is freed it tries accessing the already freed external pbl, leading to a use-after-free. Therefore we need to store the physical addresses in additional to the virtual addresses in a new data structure. Fixes: 1a4a697 ("qed: Chain support for external PBL") Signed-off-by: Michal Kalderon <mkalderon@marvell.com> Signed-off-by: Yuval Bason <ybason@marvell.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9779f8d commit 0f05406

2 files changed

Lines changed: 31 additions & 31 deletions

File tree

‎drivers/net/ethernet/qlogic/qed/qed_dev.c‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4691,26 +4691,20 @@ static void qed_chain_free_single(struct qed_dev *cdev,
46914691

46924692
static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
46934693
{
4694-
void **pp_virt_addr_tbl = p_chain->pbl.pp_virt_addr_tbl;
4694+
struct addr_tbl_entry *pp_addr_tbl = p_chain->pbl.pp_addr_tbl;
46954695
u32 page_cnt = p_chain->page_cnt, i, pbl_size;
4696-
u8 *p_pbl_virt = p_chain->pbl_sp.p_virt_table;
46974696

4698-
if (!pp_virt_addr_tbl)
4697+
if (!pp_addr_tbl)
46994698
return;
47004699

4701-
if (!p_pbl_virt)
4702-
goto out;
4703-
47044700
for (i = 0; i < page_cnt; i++) {
4705-
if (!pp_virt_addr_tbl[i])
4701+
if (!pp_addr_tbl[i].virt_addr || !pp_addr_tbl[i].dma_map)
47064702
break;
47074703

47084704
dma_free_coherent(&cdev->pdev->dev,
47094705
QED_CHAIN_PAGE_SIZE,
4710-
pp_virt_addr_tbl[i],
4711-
*(dma_addr_t *)p_pbl_virt);
4712-
4713-
p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
4706+
pp_addr_tbl[i].virt_addr,
4707+
pp_addr_tbl[i].dma_map);
47144708
}
47154709

47164710
pbl_size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
@@ -4720,9 +4714,9 @@ static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
47204714
pbl_size,
47214715
p_chain->pbl_sp.p_virt_table,
47224716
p_chain->pbl_sp.p_phys_table);
4723-
out:
4724-
vfree(p_chain->pbl.pp_virt_addr_tbl);
4725-
p_chain->pbl.pp_virt_addr_tbl = NULL;
4717+
4718+
vfree(p_chain->pbl.pp_addr_tbl);
4719+
p_chain->pbl.pp_addr_tbl = NULL;
47264720
}
47274721

47284722
void qed_chain_free(struct qed_dev *cdev, struct qed_chain *p_chain)
@@ -4823,19 +4817,19 @@ qed_chain_alloc_pbl(struct qed_dev *cdev,
48234817
{
48244818
u32 page_cnt = p_chain->page_cnt, size, i;
48254819
dma_addr_t p_phys = 0, p_pbl_phys = 0;
4826-
void **pp_virt_addr_tbl = NULL;
4820+
struct addr_tbl_entry *pp_addr_tbl;
48274821
u8 *p_pbl_virt = NULL;
48284822
void *p_virt = NULL;
48294823

4830-
size = page_cnt * sizeof(*pp_virt_addr_tbl);
4831-
pp_virt_addr_tbl = vzalloc(size);
4832-
if (!pp_virt_addr_tbl)
4824+
size = page_cnt * sizeof(*pp_addr_tbl);
4825+
pp_addr_tbl = vzalloc(size);
4826+
if (!pp_addr_tbl)
48334827
return -ENOMEM;
48344828

48354829
/* The allocation of the PBL table is done with its full size, since it
48364830
* is expected to be successive.
48374831
* qed_chain_init_pbl_mem() is called even in a case of an allocation
4838-
* failure, since pp_virt_addr_tbl was previously allocated, and it
4832+
* failure, since tbl was previously allocated, and it
48394833
* should be saved to allow its freeing during the error flow.
48404834
*/
48414835
size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
@@ -4849,8 +4843,7 @@ qed_chain_alloc_pbl(struct qed_dev *cdev,
48494843
p_chain->b_external_pbl = true;
48504844
}
48514845

4852-
qed_chain_init_pbl_mem(p_chain, p_pbl_virt, p_pbl_phys,
4853-
pp_virt_addr_tbl);
4846+
qed_chain_init_pbl_mem(p_chain, p_pbl_virt, p_pbl_phys, pp_addr_tbl);
48544847
if (!p_pbl_virt)
48554848
return -ENOMEM;
48564849

@@ -4869,7 +4862,8 @@ qed_chain_alloc_pbl(struct qed_dev *cdev,
48694862
/* Fill the PBL table with the physical address of the page */
48704863
*(dma_addr_t *)p_pbl_virt = p_phys;
48714864
/* Keep the virtual address of the page */
4872-
p_chain->pbl.pp_virt_addr_tbl[i] = p_virt;
4865+
p_chain->pbl.pp_addr_tbl[i].virt_addr = p_virt;
4866+
p_chain->pbl.pp_addr_tbl[i].dma_map = p_phys;
48734867

48744868
p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
48754869
}

‎include/linux/qed/qed_chain.h‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ struct qed_chain_u32 {
9797
u32 cons_idx;
9898
};
9999

100+
struct addr_tbl_entry {
101+
void *virt_addr;
102+
dma_addr_t dma_map;
103+
};
104+
100105
struct qed_chain {
101106
/* fastpath portion of the chain - required for commands such
102107
* as produce / consume.
@@ -107,10 +112,11 @@ struct qed_chain {
107112

108113
/* Fastpath portions of the PBL [if exists] */
109114
struct {
110-
/* Table for keeping the virtual addresses of the chain pages,
111-
* respectively to the physical addresses in the pbl table.
115+
/* Table for keeping the virtual and physical addresses of the
116+
* chain pages, respectively to the physical addresses
117+
* in the pbl table.
112118
*/
113-
void **pp_virt_addr_tbl;
119+
struct addr_tbl_entry *pp_addr_tbl;
114120

115121
union {
116122
struct qed_chain_pbl_u16 u16;
@@ -287,7 +293,7 @@ qed_chain_advance_page(struct qed_chain *p_chain,
287293
*(u32 *)page_to_inc = 0;
288294
page_index = *(u32 *)page_to_inc;
289295
}
290-
*p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
296+
*p_next_elem = p_chain->pbl.pp_addr_tbl[page_index].virt_addr;
291297
}
292298
}
293299

@@ -537,7 +543,7 @@ static inline void qed_chain_init_params(struct qed_chain *p_chain,
537543

538544
p_chain->pbl_sp.p_phys_table = 0;
539545
p_chain->pbl_sp.p_virt_table = NULL;
540-
p_chain->pbl.pp_virt_addr_tbl = NULL;
546+
p_chain->pbl.pp_addr_tbl = NULL;
541547
}
542548

543549
/**
@@ -575,11 +581,11 @@ static inline void qed_chain_init_mem(struct qed_chain *p_chain,
575581
static inline void qed_chain_init_pbl_mem(struct qed_chain *p_chain,
576582
void *p_virt_pbl,
577583
dma_addr_t p_phys_pbl,
578-
void **pp_virt_addr_tbl)
584+
struct addr_tbl_entry *pp_addr_tbl)
579585
{
580586
p_chain->pbl_sp.p_phys_table = p_phys_pbl;
581587
p_chain->pbl_sp.p_virt_table = p_virt_pbl;
582-
p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
588+
p_chain->pbl.pp_addr_tbl = pp_addr_tbl;
583589
}
584590

585591
/**
@@ -644,7 +650,7 @@ static inline void *qed_chain_get_last_elem(struct qed_chain *p_chain)
644650
break;
645651
case QED_CHAIN_MODE_PBL:
646652
last_page_idx = p_chain->page_cnt - 1;
647-
p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
653+
p_virt_addr = p_chain->pbl.pp_addr_tbl[last_page_idx].virt_addr;
648654
break;
649655
}
650656
/* p_virt_addr points at this stage to the last page of the chain */
@@ -716,7 +722,7 @@ static inline void qed_chain_pbl_zero_mem(struct qed_chain *p_chain)
716722
page_cnt = qed_chain_get_page_cnt(p_chain);
717723

718724
for (i = 0; i < page_cnt; i++)
719-
memset(p_chain->pbl.pp_virt_addr_tbl[i], 0,
725+
memset(p_chain->pbl.pp_addr_tbl[i].virt_addr, 0,
720726
QED_CHAIN_PAGE_SIZE);
721727
}
722728

0 commit comments

Comments
 (0)