Skip to content

Commit f15f084

Browse files
Jakub Kicinskidavem330
authored andcommitted
pktgen: convert safe uses of strncpy() to strcpy() to avoid string truncation warning
GCC 8 complains: net/core/pktgen.c: In function ‘pktgen_if_write’: net/core/pktgen.c:1419:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation] strncpy(pkt_dev->src_max, buf, len); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ net/core/pktgen.c:1399:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation] strncpy(pkt_dev->src_min, buf, len); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ net/core/pktgen.c:1290:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation] strncpy(pkt_dev->dst_max, buf, len); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ net/core/pktgen.c:1268:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation] strncpy(pkt_dev->dst_min, buf, len); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ There is no bug here, but the code is not perfect either. It copies sizeof(pkt_dev->/member/) - 1 from user space into buf, and then does a strcmp(pkt_dev->/member/, buf) hence assuming buf will be null-terminated and shorter than pkt_dev->/member/ (pkt_dev->/member/ is never explicitly null-terminated, and strncpy() doesn't have to null-terminate so the assumption must be on buf). The use of strncpy() without explicit null-termination looks suspicious. Convert to use straight strcpy(). strncpy() would also null-pad the output, but that's clearly unnecessary since the author calls memset(pkt_dev->/member/, 0, sizeof(..)); prior to strncpy(), anyway. While at it format the code for "dst_min", "dst_max", "src_min" and "src_max" in the same way by removing extra new lines in one case. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Jiong Wang <jiong.wang@netronome.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 088cbac commit f15f084

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

‎net/core/pktgen.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ static ssize_t pktgen_if_write(struct file *file,
12651265
buf[len] = 0;
12661266
if (strcmp(buf, pkt_dev->dst_min) != 0) {
12671267
memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1268-
strncpy(pkt_dev->dst_min, buf, len);
1268+
strcpy(pkt_dev->dst_min, buf);
12691269
pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
12701270
pkt_dev->cur_daddr = pkt_dev->daddr_min;
12711271
}
@@ -1280,14 +1280,12 @@ static ssize_t pktgen_if_write(struct file *file,
12801280
if (len < 0)
12811281
return len;
12821282

1283-
12841283
if (copy_from_user(buf, &user_buffer[i], len))
12851284
return -EFAULT;
1286-
12871285
buf[len] = 0;
12881286
if (strcmp(buf, pkt_dev->dst_max) != 0) {
12891287
memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1290-
strncpy(pkt_dev->dst_max, buf, len);
1288+
strcpy(pkt_dev->dst_max, buf);
12911289
pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
12921290
pkt_dev->cur_daddr = pkt_dev->daddr_max;
12931291
}
@@ -1396,7 +1394,7 @@ static ssize_t pktgen_if_write(struct file *file,
13961394
buf[len] = 0;
13971395
if (strcmp(buf, pkt_dev->src_min) != 0) {
13981396
memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1399-
strncpy(pkt_dev->src_min, buf, len);
1397+
strcpy(pkt_dev->src_min, buf);
14001398
pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
14011399
pkt_dev->cur_saddr = pkt_dev->saddr_min;
14021400
}
@@ -1416,7 +1414,7 @@ static ssize_t pktgen_if_write(struct file *file,
14161414
buf[len] = 0;
14171415
if (strcmp(buf, pkt_dev->src_max) != 0) {
14181416
memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1419-
strncpy(pkt_dev->src_max, buf, len);
1417+
strcpy(pkt_dev->src_max, buf);
14201418
pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
14211419
pkt_dev->cur_saddr = pkt_dev->saddr_max;
14221420
}

0 commit comments

Comments
 (0)