Skip to content

Commit b6d73be

Browse files
yonghuahjren1
authored andcommitted
Enable FORTIFY and FORMAT SECURITY compile flags
1. Enable below 2 defenses in Makefile "-O2 -D_FORTIFY_SOURCE=2" "-Wformat -Wformat-security" 2. Update related source code impacted by above 2 flags Change-Id: Ib42214848f030b4cf508cd7c52a7e3cc809435d9 Signed-off-by: Yonghua Huang <yonghua.huang@intel.com>
1 parent 155be81 commit b6d73be

File tree

9 files changed

+81
-37
lines changed

9 files changed

+81
-37
lines changed

devicemodel/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ CFLAGS += -DNO_OPENSSL
1717
CFLAGS += -m64
1818
CFLAGS += -Wall -ffunction-sections
1919
CFLAGS += -Werror
20+
CFLAGS += -O2 -D_FORTIFY_SOURCE=2
21+
CFLAGS += -Wformat -Wformat-security
2022

2123
CFLAGS += -I$(BASEDIR)/include
2224
CFLAGS += -I$(BASEDIR)/include/public

devicemodel/core/consport.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ ttyread(void)
8484
char rb;
8585

8686
if (tty_char_available()) {
87-
read(STDIN_FILENO, &rb, 1);
88-
return (rb & 0xff);
89-
} else {
90-
return -1;
87+
if (read(STDIN_FILENO, &rb, 1) > 0)
88+
return (rb & 0xff);
9189
}
90+
return -1;
9291
}
9392

94-
static void
93+
94+
static int
9595
ttywrite(unsigned char wb)
9696
{
97-
(void) write(STDOUT_FILENO, &wb, 1);
97+
if (write(STDOUT_FILENO, &wb, 1) > 0)
98+
return 1;
99+
100+
return -1;
98101
}
99102

100103
static int

devicemodel/core/mevent.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ mevent_pipe_read(int fd, enum ev_type type, void *param)
109109
} while (status == MEVENT_MAX);
110110
}
111111

112-
void
112+
/*On error, -1 is returned, else return zero*/
113+
int
113114
mevent_notify(void)
114115
{
115116
char c;
@@ -119,7 +120,9 @@ mevent_notify(void)
119120
* pipe to force the i/o thread to exit the blocking epoll call.
120121
*/
121122
if (mevent_pipefd[1] != 0 && pthread_self() != mevent_tid)
122-
write(mevent_pipefd[1], &c, 1);
123+
if (write(mevent_pipefd[1], &c, 1) <= 0)
124+
return -1;
125+
return 0;
123126
}
124127

125128
static int

devicemodel/hw/pci/core.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,7 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, int baridx,
20322032
uint64_t offset, int size, uint64_t value)
20332033
{
20342034
int i;
2035+
void *offset_ptr;
20352036
struct pci_emul_dummy *dummy = dev->arg;
20362037

20372038
if (baridx == 0) {
@@ -2041,12 +2042,13 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, int baridx,
20412042
return;
20422043
}
20432044

2045+
offset_ptr = (void *) &dummy->ioregs[offset];
20442046
if (size == 1)
2045-
dummy->ioregs[offset] = value & 0xff;
2047+
*(uint8_t *)offset_ptr = value & 0xff;
20462048
else if (size == 2)
2047-
*(uint16_t *)&dummy->ioregs[offset] = value & 0xffff;
2049+
*(uint16_t *)offset_ptr = value & 0xffff;
20482050
else if (size == 4)
2049-
*(uint32_t *)&dummy->ioregs[offset] = value;
2051+
*(uint32_t *)offset = value;
20502052
else
20512053
printf("diow: iow unknown size %d\n", size);
20522054

@@ -2071,14 +2073,15 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, int baridx,
20712073

20722074
i = baridx - 1; /* 'memregs' index */
20732075

2076+
offset_ptr = (void *) &dummy->memregs[i][offset];
20742077
if (size == 1)
2075-
dummy->memregs[i][offset] = value;
2078+
*(uint8_t *)offset_ptr = value;
20762079
else if (size == 2)
2077-
*(uint16_t *)&dummy->memregs[i][offset] = value;
2080+
*(uint16_t *)offset_ptr = value;
20782081
else if (size == 4)
2079-
*(uint32_t *)&dummy->memregs[i][offset] = value;
2082+
*(uint32_t *)offset_ptr = value;
20802083
else if (size == 8)
2081-
*(uint64_t *)&dummy->memregs[i][offset] = value;
2084+
*(uint64_t *)offset_ptr = value;
20822085
else
20832086
printf("diow: memw unknown size %d\n", size);
20842087

@@ -2098,6 +2101,7 @@ pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, int baridx,
20982101
struct pci_emul_dummy *dummy = dev->arg;
20992102
uint32_t value = 0;
21002103
int i;
2104+
void *offset_ptr;
21012105

21022106
if (baridx == 0) {
21032107
if (offset + size > DIOSZ) {
@@ -2107,12 +2111,13 @@ pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, int baridx,
21072111
}
21082112

21092113
value = 0;
2114+
offset_ptr = (void *) &dummy->ioregs[offset];
21102115
if (size == 1)
2111-
value = dummy->ioregs[offset];
2116+
value = *(uint8_t *)offset_ptr;
21122117
else if (size == 2)
2113-
value = *(uint16_t *) &dummy->ioregs[offset];
2118+
value = *(uint16_t *)offset_ptr;
21142119
else if (size == 4)
2115-
value = *(uint32_t *) &dummy->ioregs[offset];
2120+
value = *(uint32_t *)offset_ptr;
21162121
else
21172122
printf("dior: ior unknown size %d\n", size);
21182123
}
@@ -2126,14 +2131,15 @@ pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_vdev *dev, int baridx,
21262131

21272132
i = baridx - 1; /* 'memregs' index */
21282133

2134+
offset_ptr = (void *) &dummy->memregs[i][offset];
21292135
if (size == 1)
2130-
value = dummy->memregs[i][offset];
2136+
value = *(uint8_t *)offset_ptr;
21312137
else if (size == 2)
2132-
value = *(uint16_t *) &dummy->memregs[i][offset];
2138+
value = *(uint16_t *)offset_ptr;
21332139
else if (size == 4)
2134-
value = *(uint32_t *) &dummy->memregs[i][offset];
2140+
value = *(uint32_t *)offset_ptr;
21352141
else if (size == 8)
2136-
value = *(uint64_t *) &dummy->memregs[i][offset];
2142+
value = *(uint64_t *)offset_ptr;
21372143
else
21382144
printf("dior: ior unknown size %d\n", size);
21392145
}

devicemodel/hw/pci/irq.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,24 @@ pirq_dsdt(void)
248248
for (irq = 0; irq < nitems(irq_counts); irq++) {
249249
if (!IRQ_PERMITTED(irq))
250250
continue;
251-
if (irq_prs == NULL)
252-
asprintf(&irq_prs, "%d", irq);
253-
else {
251+
if (irq_prs == NULL) {
252+
if (asprintf(&irq_prs, "%d", irq) < 0) {
253+
/*error*/
254+
if (irq_prs != NULL)
255+
free(irq_prs);
256+
257+
return;
258+
}
259+
} else {
254260
old = irq_prs;
255-
asprintf(&irq_prs, "%s,%d", old, irq);
261+
if (asprintf(&irq_prs, "%s,%d", old, irq) < 0) {
262+
/*error*/
263+
if (irq_prs != NULL)
264+
free(irq_prs);
265+
266+
free(old);
267+
return;
268+
}
256269
free(old);
257270
}
258271
}

devicemodel/hw/pci/lpc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,17 @@ pci_lpc_deinit(struct vmctx *ctx, struct pci_vdev *pi, char *opts)
421421
char *
422422
lpc_pirq_name(int pin)
423423
{
424-
char *name;
424+
char *name = NULL;
425425

426426
if (lpc_bridge == NULL)
427427
return NULL;
428-
asprintf(&name, "\\_SB.PCI0.ISA.LNK%c,", 'A' + pin - 1);
428+
429+
if (asprintf(&name, "\\_SB.PCI0.ISA.LNK%c,", 'A' + pin - 1) < 0) {
430+
if (name != NULL)
431+
free(name);
432+
433+
return NULL;
434+
}
429435
return name;
430436
}
431437

devicemodel/hw/pci/virtio/virtio_net.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ virtio_net_tap_tx(struct virtio_net *net, struct iovec *iov, int iovcnt,
280280
int len)
281281
{
282282
static char pad[60]; /* all zero bytes */
283+
ssize_t ret;
283284

284285
if (net->tapfd == -1)
285286
return;
@@ -294,7 +295,8 @@ virtio_net_tap_tx(struct virtio_net *net, struct iovec *iov, int iovcnt,
294295
iov[iovcnt].iov_len = 60 - len;
295296
iovcnt++;
296297
}
297-
(void) writev(net->tapfd, iov, iovcnt);
298+
ret = writev(net->tapfd, iov, iovcnt);
299+
(void)ret; /*avoid compiler warning*/
298300
}
299301

300302
/*
@@ -335,6 +337,7 @@ virtio_net_tap_rx(struct virtio_net *net)
335337
void *vrx;
336338
int len, n;
337339
uint16_t idx;
340+
ssize_t ret;
338341

339342
/*
340343
* Should never be called without a valid tap fd
@@ -349,7 +352,9 @@ virtio_net_tap_rx(struct virtio_net *net)
349352
/*
350353
* Drop the packet and try later.
351354
*/
352-
(void) read(net->tapfd, dummybuf, sizeof(dummybuf));
355+
ret = read(net->tapfd, dummybuf, sizeof(dummybuf));
356+
(void)ret; /*avoid compiler warning*/
357+
353358
return;
354359
}
355360

@@ -362,7 +367,9 @@ virtio_net_tap_rx(struct virtio_net *net)
362367
* Drop the packet and try later. Interrupt on
363368
* empty, if that's negotiated.
364369
*/
365-
(void) read(net->tapfd, dummybuf, sizeof(dummybuf));
370+
ret = read(net->tapfd, dummybuf, sizeof(dummybuf));
371+
(void)ret; /*avoid compiler warning*/
372+
366373
vq_endchains(vq, 1);
367374
return;
368375
}

devicemodel/hw/platform/uart_core.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,20 @@ ttyread(struct ttyfd *tf)
148148
{
149149
unsigned char rb;
150150

151-
if (read(tf->fd, &rb, 1) == 1)
151+
if (read(tf->fd, &rb, 1) > 0)
152152
return rb;
153-
else
154-
return -1;
153+
154+
return -1;
155155
}
156156

157-
static void
157+
static int
158158
ttywrite(struct ttyfd *tf, unsigned char wb)
159159
{
160-
(void)write(tf->fd, &wb, 1);
160+
161+
if (write(tf->fd, &wb, 1) > 0)
162+
return 1;
163+
164+
return -1;
161165
}
162166

163167
static void

devicemodel/include/mevent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int mevent_enable(struct mevent *evp);
4646
int mevent_disable(struct mevent *evp);
4747
int mevent_delete(struct mevent *evp);
4848
int mevent_delete_close(struct mevent *evp);
49-
void mevent_notify(void);
49+
int mevent_notify(void);
5050

5151
void mevent_dispatch(void);
5252

0 commit comments

Comments
 (0)