diff --git a/fcoeadm.c b/fcoeadm.c index 776b4e3..20b01b3 100644 --- a/fcoeadm.c +++ b/fcoeadm.c @@ -185,8 +185,10 @@ fcoeadm_action(enum clif_action cmd, char *ifname, enum clif_flags flags) struct clif_sock_info clif_info; int rc; - if (ifname) - strncpy(data.ifname, ifname, sizeof(data.ifname)); + if (ifname) { + strncpy(data.ifname, ifname, sizeof(data.ifname) - 1); + data.ifname[sizeof(data.ifname) - 1] = '\0'; + } else data.ifname[0] = '\0'; data.cmd = cmd; @@ -231,7 +233,8 @@ int main(int argc, char *argv[]) * This has to be first because the error print macro * expects progname to be valid. */ - strncpy(progname, basename(argv[0]), sizeof(progname)); + strncpy(progname, basename(argv[0]), sizeof(progname) - 1); + progname[sizeof(progname) - 1] = '\0'; /* check if we have sysfs */ if (fcoe_checkdir(SYSFS_MOUNT)) { diff --git a/fcoemon.c b/fcoemon.c index 9a400c5..9572836 100644 --- a/fcoemon.c +++ b/fcoemon.c @@ -518,8 +518,8 @@ static int fcm_read_config_files(void) dp->d_name); continue; } - strncpy(file, CONFIG_DIR "/", sizeof(file)); - strncat(file, dp->d_name, sizeof(file) - strlen(file)); + snprintf(file, sizeof(file), "%s/%.*s", CONFIG_DIR, + (int)(sizeof(file) - strlen(CONFIG_DIR) - 2), dp->d_name); fp = fopen(file, "r"); if (!fp) { FCM_LOG_ERR(errno, "Failed to read %s\n", file); @@ -1089,9 +1089,12 @@ static void fcm_vlan_dev_real_dev(char *vlan_ifname, char *real_ifname) memset(&ifv, 0, sizeof(ifv)); ifv.cmd = GET_VLAN_REALDEV_NAME_CMD; - strncpy(ifv.device1, vlan_ifname, strlen(vlan_ifname)+1); - if (ioctl(fd, SIOCGIFVLAN, &ifv) == 0) - strncpy(real_ifname, ifv.u.device2, strlen(ifv.u.device2)+1); + strncpy(ifv.device1, vlan_ifname, sizeof(ifv.device1) - 1); + ifv.device1[sizeof(ifv.device1) - 1] = '\0'; + if (ioctl(fd, SIOCGIFVLAN, &ifv) == 0) { + strncpy(real_ifname, ifv.u.device2, IFNAMSIZ - 1); + real_ifname[IFNAMSIZ - 1] = '\0'; + } close(fd); } @@ -1642,8 +1645,10 @@ static void fcm_process_link_msg(struct ifinfomsg *ip, int len, unsigned type) /* try to find the real device name */ real_dev[0] = '\0'; fcm_vlan_dev_real_dev(ifname, real_dev); - if (strlen(real_dev)) - strncpy(p->real_ifname, real_dev, strlen(real_dev)+1); + if (strlen(real_dev)) { + strncpy(p->real_ifname, real_dev, sizeof(p->real_ifname) - 1); + p->real_ifname[sizeof(p->real_ifname) - 1] = '\0'; + } if (p->ready) update_fcoe_port_state(p, type, operstate, FCP_CFG_IFNAME); @@ -1655,7 +1660,8 @@ static void fcm_process_link_msg(struct ifinfomsg *ip, int len, unsigned type) if (p) { p->ifindex = ifindex; memcpy(p->mac, mac, ETHER_ADDR_LEN); - strncpy(p->real_ifname, ifname, strlen(ifname)+1); + strncpy(p->real_ifname, ifname, sizeof(p->real_ifname) - 1); + p->real_ifname[sizeof(p->real_ifname) - 1] = '\0'; update_fcoe_port_state(p, type, operstate, FCP_REAL_IFNAME); } @@ -1783,7 +1789,8 @@ static void fcm_process_ieee_msg(struct nlmsghdr *nlh) if (rta_parent->rta_type != DCB_ATTR_IFNAME) return; - strncpy(ifname, NLA_DATA(rta_parent), sizeof(ifname)); + strncpy(ifname, NLA_DATA(rta_parent), sizeof(ifname) - 1); + ifname[sizeof(ifname) - 1] = '\0'; ff = fcm_netif_lookup_create(ifname); if (!ff) { FCM_LOG("Processing IEEE message: %s not found or created\n", @@ -3704,7 +3711,8 @@ int main(int argc, char **argv) memset(&fcoe_config, 0, sizeof(fcoe_config)); - strncpy(progname, basename(argv[0]), sizeof(progname)); + strncpy(progname, basename(argv[0]), sizeof(progname) - 1); + progname[sizeof(progname) - 1] = '\0'; sa_log_prefix = progname; sa_log_flags = 0; openlog(sa_log_prefix, LOG_CONS, LOG_DAEMON); diff --git a/fipvlan.c b/fipvlan.c index 7c00c7c..7dec0df 100644 --- a/fipvlan.c +++ b/fipvlan.c @@ -448,7 +448,8 @@ static void rtnl_recv_newlink(struct nlmsghdr *nh) else iff->iflink = iff->ifindex; memcpy(iff->mac_addr, RTA_DATA(ifla[IFLA_ADDRESS]), ETHER_ADDR_LEN); - strncpy(iff->ifname, RTA_DATA(ifla[IFLA_IFNAME]), IFNAMSIZ); + strncpy(iff->ifname, RTA_DATA(ifla[IFLA_IFNAME]), IFNAMSIZ - 1); + iff->ifname[IFNAMSIZ - 1] = '\0'; if (ifla[IFLA_LINKINFO]) { parse_linkinfo(linkinfo, ifla[IFLA_LINKINFO]); @@ -541,8 +542,10 @@ static void parse_cmdline(int argc, char **argv) config.start = true; break; case 'f': - if (optarg && strlen(optarg)) - strncpy(config.suffix, optarg, 256); + if (optarg && strlen(optarg)) { + strncpy(config.suffix, optarg, 255); + config.suffix[255] = '0'; + } break; case 'l': config.link_retry = strtoul(optarg, NULL, 10); diff --git a/lib/sysfs_hba.c b/lib/sysfs_hba.c index 5cb7fd3..9892c3d 100644 --- a/lib/sysfs_hba.c +++ b/lib/sysfs_hba.c @@ -255,10 +255,12 @@ static void get_module_info(const char *pcidev, struct hba_info *info) buf[err] = '\0'; - if (strstr(buf, "module")) + if (strstr(buf, "module")) { strncpy(info->driver_name, strstr(buf, "module") + strlen("module") + 1, - sizeof(info->driver_name)); + sizeof(info->driver_name) - 1); + info->driver_name[sizeof(info->driver_name) - 1] = '\0'; + } } @@ -315,7 +317,8 @@ struct port_attributes *get_rport_attribs(const char *rport) if (!pa) goto free_path; - strncpy(pa->device_name, rport, sizeof(pa->device_name)); + strncpy(pa->device_name, rport, sizeof(pa->device_name) - 1); + pa->device_name[sizeof(pa->device_name) - 1] = '\0'; sa_sys_read_line(path, "node_name", pa->node_name, sizeof(pa->node_name)); sa_sys_read_line(path, "port_name", pa->port_name, @@ -390,7 +393,8 @@ struct port_attributes *get_port_attribs(const char *host) if (!pa) goto free_path; - strncpy(pa->device_name, host, sizeof(pa->device_name)); + strncpy(pa->device_name, host, sizeof(pa->device_name) - 1); + pa->device_name[sizeof(pa->device_name) - 1] = '\0'; sa_sys_read_line(path, "symbolic_name", pa->symbolic_name, sizeof(pa->symbolic_name)); @@ -443,7 +447,7 @@ char *get_pci_dev_from_netdev(const char *netdev) return NULL; len = strlen(netdev) - strlen(subif); - strncpy(realdev, netdev, len); + strncpy(realdev, netdev, sizeof(realdev) - 1); if (realdev[len] != '\0') realdev[len] = '\0'; -- 1.8.3.1