Commit 5341ee0a authored by Ji-Hun Kim's avatar Ji-Hun Kim Committed by Greg Kroah-Hartman

staging: vt6655: check for memory allocation failures

There are no null pointer checking on rd_info and td_info values which
are allocated by kzalloc. It has potential null pointer dereferencing
issues. Implement error handling code on device_init_rd*, device_init_td*
and vnt_start for the allocation failures.
Signed-off-by: default avatarJi-Hun Kim <ji_hun.kim@samsung.com>
Reviewed-by: default avatarDan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 05e6557b
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
* device_print_info - print out resource * device_print_info - print out resource
* device_rx_srv - rx service function * device_rx_srv - rx service function
* device_alloc_rx_buf - rx buffer pre-allocated function * device_alloc_rx_buf - rx buffer pre-allocated function
* device_free_rx_buf - free rx buffer function
* device_free_tx_buf - free tx buffer function * device_free_tx_buf - free tx buffer function
* device_init_rd0_ring- initial rd dma0 ring * device_init_rd0_ring- initial rd dma0 ring
* device_init_rd1_ring- initial rd dma1 ring * device_init_rd1_ring- initial rd dma1 ring
...@@ -124,14 +125,15 @@ static int vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent); ...@@ -124,14 +125,15 @@ static int vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent);
static void device_free_info(struct vnt_private *priv); static void device_free_info(struct vnt_private *priv);
static void device_print_info(struct vnt_private *priv); static void device_print_info(struct vnt_private *priv);
static void device_init_rd0_ring(struct vnt_private *priv); static int device_init_rd0_ring(struct vnt_private *priv);
static void device_init_rd1_ring(struct vnt_private *priv); static int device_init_rd1_ring(struct vnt_private *priv);
static void device_init_td0_ring(struct vnt_private *priv); static int device_init_td0_ring(struct vnt_private *priv);
static void device_init_td1_ring(struct vnt_private *priv); static int device_init_td1_ring(struct vnt_private *priv);
static int device_rx_srv(struct vnt_private *priv, unsigned int idx); static int device_rx_srv(struct vnt_private *priv, unsigned int idx);
static int device_tx_srv(struct vnt_private *priv, unsigned int idx); static int device_tx_srv(struct vnt_private *priv, unsigned int idx);
static bool device_alloc_rx_buf(struct vnt_private *, struct vnt_rx_desc *); static bool device_alloc_rx_buf(struct vnt_private *, struct vnt_rx_desc *);
static void device_free_rx_buf(struct vnt_private *priv, struct vnt_rx_desc *rd);
static void device_init_registers(struct vnt_private *priv); static void device_init_registers(struct vnt_private *priv);
static void device_free_tx_buf(struct vnt_private *, struct vnt_tx_desc *); static void device_free_tx_buf(struct vnt_private *, struct vnt_tx_desc *);
static void device_free_td0_ring(struct vnt_private *priv); static void device_free_td0_ring(struct vnt_private *priv);
...@@ -528,20 +530,28 @@ static void device_free_rings(struct vnt_private *priv) ...@@ -528,20 +530,28 @@ static void device_free_rings(struct vnt_private *priv)
priv->tx0_bufs, priv->tx_bufs_dma0); priv->tx0_bufs, priv->tx_bufs_dma0);
} }
static void device_init_rd0_ring(struct vnt_private *priv) static int device_init_rd0_ring(struct vnt_private *priv)
{ {
int i; int i;
dma_addr_t curr = priv->rd0_pool_dma; dma_addr_t curr = priv->rd0_pool_dma;
struct vnt_rx_desc *desc; struct vnt_rx_desc *desc;
int ret;
/* Init the RD0 ring entries */ /* Init the RD0 ring entries */
for (i = 0; i < priv->opts.rx_descs0; for (i = 0; i < priv->opts.rx_descs0;
i ++, curr += sizeof(struct vnt_rx_desc)) { i ++, curr += sizeof(struct vnt_rx_desc)) {
desc = &priv->aRD0Ring[i]; desc = &priv->aRD0Ring[i];
desc->rd_info = kzalloc(sizeof(*desc->rd_info), GFP_KERNEL); desc->rd_info = kzalloc(sizeof(*desc->rd_info), GFP_KERNEL);
if (!desc->rd_info) {
ret = -ENOMEM;
goto err_free_desc;
}
if (!device_alloc_rx_buf(priv, desc)) if (!device_alloc_rx_buf(priv, desc)) {
dev_err(&priv->pcid->dev, "can not alloc rx bufs\n"); dev_err(&priv->pcid->dev, "can not alloc rx bufs\n");
ret = -ENOMEM;
goto err_free_rd;
}
desc->next = &priv->aRD0Ring[(i + 1) % priv->opts.rx_descs0]; desc->next = &priv->aRD0Ring[(i + 1) % priv->opts.rx_descs0];
desc->next_desc = cpu_to_le32(curr + sizeof(struct vnt_rx_desc)); desc->next_desc = cpu_to_le32(curr + sizeof(struct vnt_rx_desc));
...@@ -550,22 +560,44 @@ static void device_init_rd0_ring(struct vnt_private *priv) ...@@ -550,22 +560,44 @@ static void device_init_rd0_ring(struct vnt_private *priv)
if (i > 0) if (i > 0)
priv->aRD0Ring[i-1].next_desc = cpu_to_le32(priv->rd0_pool_dma); priv->aRD0Ring[i-1].next_desc = cpu_to_le32(priv->rd0_pool_dma);
priv->pCurrRD[0] = &priv->aRD0Ring[0]; priv->pCurrRD[0] = &priv->aRD0Ring[0];
return 0;
err_free_rd:
kfree(desc->rd_info);
err_free_desc:
while (--i) {
desc = &priv->aRD0Ring[i];
device_free_rx_buf(priv, desc);
kfree(desc->rd_info);
}
return ret;
} }
static void device_init_rd1_ring(struct vnt_private *priv) static int device_init_rd1_ring(struct vnt_private *priv)
{ {
int i; int i;
dma_addr_t curr = priv->rd1_pool_dma; dma_addr_t curr = priv->rd1_pool_dma;
struct vnt_rx_desc *desc; struct vnt_rx_desc *desc;
int ret;
/* Init the RD1 ring entries */ /* Init the RD1 ring entries */
for (i = 0; i < priv->opts.rx_descs1; for (i = 0; i < priv->opts.rx_descs1;
i ++, curr += sizeof(struct vnt_rx_desc)) { i ++, curr += sizeof(struct vnt_rx_desc)) {
desc = &priv->aRD1Ring[i]; desc = &priv->aRD1Ring[i];
desc->rd_info = kzalloc(sizeof(*desc->rd_info), GFP_KERNEL); desc->rd_info = kzalloc(sizeof(*desc->rd_info), GFP_KERNEL);
if (!desc->rd_info) {
ret = -ENOMEM;
goto err_free_desc;
}
if (!device_alloc_rx_buf(priv, desc)) if (!device_alloc_rx_buf(priv, desc)) {
dev_err(&priv->pcid->dev, "can not alloc rx bufs\n"); dev_err(&priv->pcid->dev, "can not alloc rx bufs\n");
ret = -ENOMEM;
goto err_free_rd;
}
desc->next = &priv->aRD1Ring[(i+1) % priv->opts.rx_descs1]; desc->next = &priv->aRD1Ring[(i+1) % priv->opts.rx_descs1];
desc->next_desc = cpu_to_le32(curr + sizeof(struct vnt_rx_desc)); desc->next_desc = cpu_to_le32(curr + sizeof(struct vnt_rx_desc));
...@@ -574,6 +606,20 @@ static void device_init_rd1_ring(struct vnt_private *priv) ...@@ -574,6 +606,20 @@ static void device_init_rd1_ring(struct vnt_private *priv)
if (i > 0) if (i > 0)
priv->aRD1Ring[i-1].next_desc = cpu_to_le32(priv->rd1_pool_dma); priv->aRD1Ring[i-1].next_desc = cpu_to_le32(priv->rd1_pool_dma);
priv->pCurrRD[1] = &priv->aRD1Ring[0]; priv->pCurrRD[1] = &priv->aRD1Ring[0];
return 0;
err_free_rd:
kfree(desc->rd_info);
err_free_desc:
while (--i) {
desc = &priv->aRD1Ring[i];
device_free_rx_buf(priv, desc);
kfree(desc->rd_info);
}
return ret;
} }
static void device_free_rd0_ring(struct vnt_private *priv) static void device_free_rd0_ring(struct vnt_private *priv)
...@@ -582,13 +628,8 @@ static void device_free_rd0_ring(struct vnt_private *priv) ...@@ -582,13 +628,8 @@ static void device_free_rd0_ring(struct vnt_private *priv)
for (i = 0; i < priv->opts.rx_descs0; i++) { for (i = 0; i < priv->opts.rx_descs0; i++) {
struct vnt_rx_desc *desc = &priv->aRD0Ring[i]; struct vnt_rx_desc *desc = &priv->aRD0Ring[i];
struct vnt_rd_info *rd_info = desc->rd_info;
dma_unmap_single(&priv->pcid->dev, rd_info->skb_dma,
priv->rx_buf_sz, DMA_FROM_DEVICE);
dev_kfree_skb(rd_info->skb);
device_free_rx_buf(priv, desc);
kfree(desc->rd_info); kfree(desc->rd_info);
} }
} }
...@@ -599,28 +640,28 @@ static void device_free_rd1_ring(struct vnt_private *priv) ...@@ -599,28 +640,28 @@ static void device_free_rd1_ring(struct vnt_private *priv)
for (i = 0; i < priv->opts.rx_descs1; i++) { for (i = 0; i < priv->opts.rx_descs1; i++) {
struct vnt_rx_desc *desc = &priv->aRD1Ring[i]; struct vnt_rx_desc *desc = &priv->aRD1Ring[i];
struct vnt_rd_info *rd_info = desc->rd_info;
dma_unmap_single(&priv->pcid->dev, rd_info->skb_dma,
priv->rx_buf_sz, DMA_FROM_DEVICE);
dev_kfree_skb(rd_info->skb);
device_free_rx_buf(priv, desc);
kfree(desc->rd_info); kfree(desc->rd_info);
} }
} }
static void device_init_td0_ring(struct vnt_private *priv) static int device_init_td0_ring(struct vnt_private *priv)
{ {
int i; int i;
dma_addr_t curr; dma_addr_t curr;
struct vnt_tx_desc *desc; struct vnt_tx_desc *desc;
int ret;
curr = priv->td0_pool_dma; curr = priv->td0_pool_dma;
for (i = 0; i < priv->opts.tx_descs[0]; for (i = 0; i < priv->opts.tx_descs[0];
i++, curr += sizeof(struct vnt_tx_desc)) { i++, curr += sizeof(struct vnt_tx_desc)) {
desc = &priv->apTD0Rings[i]; desc = &priv->apTD0Rings[i];
desc->td_info = kzalloc(sizeof(*desc->td_info), GFP_KERNEL); desc->td_info = kzalloc(sizeof(*desc->td_info), GFP_KERNEL);
if (!desc->td_info) {
ret = -ENOMEM;
goto err_free_desc;
}
desc->td_info->buf = priv->tx0_bufs + i * PKT_BUF_SZ; desc->td_info->buf = priv->tx0_bufs + i * PKT_BUF_SZ;
desc->td_info->buf_dma = priv->tx_bufs_dma0 + i * PKT_BUF_SZ; desc->td_info->buf_dma = priv->tx_bufs_dma0 + i * PKT_BUF_SZ;
...@@ -632,13 +673,24 @@ static void device_init_td0_ring(struct vnt_private *priv) ...@@ -632,13 +673,24 @@ static void device_init_td0_ring(struct vnt_private *priv)
if (i > 0) if (i > 0)
priv->apTD0Rings[i-1].next_desc = cpu_to_le32(priv->td0_pool_dma); priv->apTD0Rings[i-1].next_desc = cpu_to_le32(priv->td0_pool_dma);
priv->apTailTD[0] = priv->apCurrTD[0] = &priv->apTD0Rings[0]; priv->apTailTD[0] = priv->apCurrTD[0] = &priv->apTD0Rings[0];
return 0;
err_free_desc:
while (--i) {
desc = &priv->apTD0Rings[i];
kfree(desc->td_info);
}
return ret;
} }
static void device_init_td1_ring(struct vnt_private *priv) static int device_init_td1_ring(struct vnt_private *priv)
{ {
int i; int i;
dma_addr_t curr; dma_addr_t curr;
struct vnt_tx_desc *desc; struct vnt_tx_desc *desc;
int ret;
/* Init the TD ring entries */ /* Init the TD ring entries */
curr = priv->td1_pool_dma; curr = priv->td1_pool_dma;
...@@ -646,6 +698,10 @@ static void device_init_td1_ring(struct vnt_private *priv) ...@@ -646,6 +698,10 @@ static void device_init_td1_ring(struct vnt_private *priv)
i++, curr += sizeof(struct vnt_tx_desc)) { i++, curr += sizeof(struct vnt_tx_desc)) {
desc = &priv->apTD1Rings[i]; desc = &priv->apTD1Rings[i];
desc->td_info = kzalloc(sizeof(*desc->td_info), GFP_KERNEL); desc->td_info = kzalloc(sizeof(*desc->td_info), GFP_KERNEL);
if (!desc->td_info) {
ret = -ENOMEM;
goto err_free_desc;
}
desc->td_info->buf = priv->tx1_bufs + i * PKT_BUF_SZ; desc->td_info->buf = priv->tx1_bufs + i * PKT_BUF_SZ;
desc->td_info->buf_dma = priv->tx_bufs_dma1 + i * PKT_BUF_SZ; desc->td_info->buf_dma = priv->tx_bufs_dma1 + i * PKT_BUF_SZ;
...@@ -657,6 +713,16 @@ static void device_init_td1_ring(struct vnt_private *priv) ...@@ -657,6 +713,16 @@ static void device_init_td1_ring(struct vnt_private *priv)
if (i > 0) if (i > 0)
priv->apTD1Rings[i-1].next_desc = cpu_to_le32(priv->td1_pool_dma); priv->apTD1Rings[i-1].next_desc = cpu_to_le32(priv->td1_pool_dma);
priv->apTailTD[1] = priv->apCurrTD[1] = &priv->apTD1Rings[0]; priv->apTailTD[1] = priv->apCurrTD[1] = &priv->apTD1Rings[0];
return 0;
err_free_desc:
while (--i) {
desc = &priv->apTD1Rings[i];
kfree(desc->td_info);
}
return ret;
} }
static void device_free_td0_ring(struct vnt_private *priv) static void device_free_td0_ring(struct vnt_private *priv)
...@@ -745,6 +811,16 @@ static bool device_alloc_rx_buf(struct vnt_private *priv, ...@@ -745,6 +811,16 @@ static bool device_alloc_rx_buf(struct vnt_private *priv,
return true; return true;
} }
static void device_free_rx_buf(struct vnt_private *priv,
struct vnt_rx_desc *rd)
{
struct vnt_rd_info *rd_info = rd->rd_info;
dma_unmap_single(&priv->pcid->dev, rd_info->skb_dma,
priv->rx_buf_sz, DMA_FROM_DEVICE);
dev_kfree_skb(rd_info->skb);
}
static const u8 fallback_rate0[5][5] = { static const u8 fallback_rate0[5][5] = {
{RATE_18M, RATE_18M, RATE_12M, RATE_12M, RATE_12M}, {RATE_18M, RATE_18M, RATE_12M, RATE_12M, RATE_12M},
{RATE_24M, RATE_24M, RATE_18M, RATE_12M, RATE_12M}, {RATE_24M, RATE_24M, RATE_18M, RATE_12M, RATE_12M},
...@@ -1165,10 +1241,18 @@ static int vnt_start(struct ieee80211_hw *hw) ...@@ -1165,10 +1241,18 @@ static int vnt_start(struct ieee80211_hw *hw)
} }
dev_dbg(&priv->pcid->dev, "call device init rd0 ring\n"); dev_dbg(&priv->pcid->dev, "call device init rd0 ring\n");
device_init_rd0_ring(priv); ret = device_init_rd0_ring(priv);
device_init_rd1_ring(priv); if (ret)
device_init_td0_ring(priv); return ret;
device_init_td1_ring(priv); ret = device_init_rd1_ring(priv);
if (ret)
goto err_free_rd0_ring;
ret = device_init_td0_ring(priv);
if (ret)
goto err_free_rd1_ring;
ret = device_init_td1_ring(priv);
if (ret)
goto err_free_td0_ring;
device_init_registers(priv); device_init_registers(priv);
...@@ -1178,6 +1262,14 @@ static int vnt_start(struct ieee80211_hw *hw) ...@@ -1178,6 +1262,14 @@ static int vnt_start(struct ieee80211_hw *hw)
ieee80211_wake_queues(hw); ieee80211_wake_queues(hw);
return 0; return 0;
err_free_td0_ring:
device_free_td0_ring(priv);
err_free_rd1_ring:
device_free_rd1_ring(priv);
err_free_rd0_ring:
device_free_rd0_ring(priv);
return ret;
} }
static void vnt_stop(struct ieee80211_hw *hw) static void vnt_stop(struct ieee80211_hw *hw)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment