Skip to content

Commit 13228d9

Browse files
yonghuahwenlingz
authored andcommitted
dm: refine 'assert' usage in irq.c and wdt_i6300esb.c
cleanup 'assert' usage to avoid possible software vulnerabilities Tracked-On: #3252 Signed-off-by: Yonghua Huang <yonghua.huang@intel.com> Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
1 parent e6eef9b commit 13228d9

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

devicemodel/hw/pci/irq.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
*/
2727

2828

29-
#include <assert.h>
3029
#include <pthread.h>
3130
#include <stdbool.h>
3231
#include <stdio.h>
@@ -80,7 +79,9 @@ pirq_valid_irq(int reg)
8079
uint8_t
8180
pirq_read(int pin)
8281
{
83-
assert(pin > 0 && pin <= nitems(pirqs));
82+
if (pin <= 0 || pin > nitems(pirqs))
83+
return PIRQ_DIS;
84+
8485
return pirqs[pin - 1].reg;
8586
}
8687

@@ -89,7 +90,9 @@ pirq_write(struct vmctx *ctx, int pin, uint8_t val)
8990
{
9091
struct pirq *pirq;
9192

92-
assert(pin > 0 && pin <= nitems(pirqs));
93+
if (pin <= 0 || pin > nitems(pirqs))
94+
return;
95+
9396
pirq = &pirqs[pin - 1];
9497
pthread_mutex_lock(&pirq->lock);
9598
if (pirq->reg != (val & (PIRQ_DIS | PIRQ_IRQ))) {
@@ -103,21 +106,18 @@ pirq_write(struct vmctx *ctx, int pin, uint8_t val)
103106
}
104107

105108
void
106-
pci_irq_reserve(int irq)
107-
{
108-
assert(irq >= 0 && irq < nitems(irq_counts));
109-
assert(pirq_cold);
110-
assert(irq_counts[irq] == 0 || irq_counts[irq] == IRQ_DISABLED);
111-
irq_counts[irq] = IRQ_DISABLED;
109+
pci_irq_reserve(int irq) {
110+
if ((irq >= 0 && irq < nitems(irq_counts)) && pirq_cold
111+
&& (irq_counts[irq] == 0 || irq_counts[irq] == IRQ_DISABLED))
112+
irq_counts[irq] = IRQ_DISABLED;
112113
}
113114

114115
void
115116
pci_irq_use(int irq)
116117
{
117-
assert(irq >= 0 && irq < nitems(irq_counts));
118-
assert(pirq_cold);
119-
assert(irq_counts[irq] != IRQ_DISABLED);
120-
irq_counts[irq]++;
118+
if ((irq >= 0 && irq < nitems(irq_counts)) && pirq_cold
119+
&& (irq_counts[irq] != IRQ_DISABLED))
120+
irq_counts[irq]++;
121121
}
122122

123123
void
@@ -186,7 +186,9 @@ pirq_alloc_pin(struct pci_vdev *dev)
186186
best_count = irq_counts[irq];
187187
}
188188
}
189-
assert(best_irq >= 0);
189+
if (best_irq < 0)
190+
return -1;
191+
190192
irq_counts[best_irq]++;
191193
pirqs[best_pin].reg = best_irq;
192194
}
@@ -197,7 +199,9 @@ pirq_alloc_pin(struct pci_vdev *dev)
197199
int
198200
pirq_irq(int pin)
199201
{
200-
assert(pin > 0 && pin <= nitems(pirqs));
202+
if (pin <= 0 || pin > nitems(pirqs))
203+
return 0xFF;
204+
201205
return (pirqs[pin - 1].reg & PIRQ_IRQ);
202206
}
203207

devicemodel/hw/pci/wdt_i6300esb.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <stdio.h>
1414
#include <stdlib.h>
1515
#include <string.h>
16-
#include <assert.h>
1716
#include <stdbool.h>
1817

1918
#include "vmmapi.h"
@@ -252,8 +251,6 @@ static void
252251
pci_wdt_bar_write(struct vmctx *ctx, int vcpu, struct pci_vdev *dev,
253252
int baridx, uint64_t offset, int size, uint64_t value)
254253
{
255-
assert(baridx == 0);
256-
257254
DPRINTF("%s: addr = 0x%x, val = 0x%x, size=%d\n",
258255
__func__, (int) offset, (int)value, size);
259256

@@ -269,7 +266,8 @@ pci_wdt_bar_write(struct vmctx *ctx, int vcpu, struct pci_vdev *dev,
269266
}
270267
}
271268
} else if (offset == ESB_RELOAD_REG) {
272-
assert(size == 2);
269+
if (size != 2)
270+
return;
273271

274272
if (value == ESB_UNLOCK1)
275273
wdt_state.unlock_state = 1;
@@ -306,7 +304,6 @@ pci_wdt_bar_read(struct vmctx *ctx, int vcpu, struct pci_vdev *dev,
306304
{
307305
uint64_t ret = 0;
308306

309-
assert(baridx == 0);
310307
DPRINTF("%s: addr = 0x%x, size=%d\n\r", __func__, (int) offset, size);
311308

312309
if (offset == ESB_GIS_REG) {
@@ -315,7 +312,8 @@ pci_wdt_bar_read(struct vmctx *ctx, int vcpu, struct pci_vdev *dev,
315312
ret |= ESB_WDT_INT_ACT;
316313

317314
} else if (offset == ESB_RELOAD_REG) {
318-
assert(size == 2);
315+
if (size != 2)
316+
return 0;
319317

320318
DPRINTF("%s: timeout: %d\n\r", __func__, wdt_timeout);
321319
if (wdt_timeout != 0)

0 commit comments

Comments
 (0)