Skip to content

Commit 1e1244c

Browse files
tianhuaswenlingz
authored andcommitted
dm: use strncpy to replace strcpy
Use strncpy instead of strcpy to avoid buf overflow. Fix strncpy null-terminated issues. Tracked-On: #3245 Signed-off-by: Tianhua Sun <tianhuax.s.sun@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent 0ea788b commit 1e1244c

File tree

5 files changed

+10
-3
lines changed

5 files changed

+10
-3
lines changed

devicemodel/hw/pci/virtio/virtio_audio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ virtio_audio_kernel_dev_set(struct vbs_dev_info *kdev, const char *name,
121121
{
122122
/* init kdev */
123123
strncpy(kdev->name, name, VBS_NAME_LEN);
124+
kdev->name[VBS_NAME_LEN - 1] = '\0';
124125
kdev->vmid = vmid;
125126
kdev->nvq = nvq;
126127
kdev->negotiated_features = feature;

devicemodel/hw/pci/virtio/virtio_console.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ virtio_console_accept_new_connection(int fd __attribute__((unused)),
625625

626626
memset(&addr, 0, sizeof(addr));
627627
addr.sun_family = AF_UNIX;
628-
strcpy(addr.sun_path, be->portpath);
628+
strncpy(addr.sun_path, be->portpath, sizeof(addr.sun_path));
629+
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
629630

630631
len = sizeof(addr);
631632
accepted_fd = accept(be->fd, (struct sockaddr *)&addr, &len);
@@ -728,7 +729,8 @@ virtio_console_config_backend(struct virtio_console_backend *be)
728729

729730
memset(&addr, 0, sizeof(addr));
730731
addr.sun_family = AF_UNIX;
731-
strcpy(addr.sun_path, be->portpath);
732+
strncpy(addr.sun_path, be->portpath, sizeof(addr.sun_path));
733+
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
732734

733735
if (be->socket_type == NULL || !strcmp(be->socket_type,"server")) {
734736
unlink(be->portpath);

devicemodel/hw/pci/virtio/virtio_hyper_dmabuf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ virtio_hyper_dmabuf_k_dev_set(const char *name, int vmid, int nvq,
116116
{
117117
/* init kdev */
118118
strncpy(kdev.name, name, VBS_NAME_LEN);
119+
kdev.name[VBS_NAME_LEN - 1] = '\0';
119120
kdev.vmid = vmid;
120121
kdev.nvq = nvq;
121122
kdev.negotiated_features = feature;

devicemodel/hw/pci/virtio/virtio_net.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,10 @@ virtio_net_tap_open(char *devname)
652652
memset(&ifr, 0, sizeof(ifr));
653653
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
654654

655-
if (*devname)
655+
if (*devname) {
656656
strncpy(ifr.ifr_name, devname, IFNAMSIZ);
657+
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
658+
}
657659

658660
rc = ioctl(tunfd, TUNSETIFF, (void *)&ifr);
659661
if (rc < 0) {

devicemodel/hw/pci/virtio/virtio_rnd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ virtio_rnd_kernel_dev_set(struct vbs_dev_info *kdev, const char *name,
217217

218218
/* init kdev */
219219
strncpy(kdev->name, name, VBS_NAME_LEN);
220+
kdev->name[VBS_NAME_LEN - 1] = '\0';
220221
kdev->vmid = vmid;
221222
kdev->nvq = nvq;
222223
kdev->negotiated_features = feature;

0 commit comments

Comments
 (0)