Skip to content

Commit 5e89288

Browse files
tiwaidavem330
authored andcommitted
net: sfc: Use scnprintf() for avoiding potential buffer overflow
Since snprintf() returns the would-be-output size instead of the actual output size, the succeeding calls may go beyond the given buffer limit. Fix it by replacing with scnprintf(). Cc: "David S . Miller" <[email protected]> Cc: Edward Cree <[email protected]> Cc: Martin Habets <[email protected]> Cc: Solarflare linux maintainers <[email protected]> Cc: [email protected] Signed-off-by: Takashi Iwai <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 38e0f74 commit 5e89288

File tree

1 file changed

+18
-14
lines changed
  • drivers/net/ethernet/sfc

1 file changed

+18
-14
lines changed

drivers/net/ethernet/sfc/mcdi.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,14 @@ static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd,
212212
* progress on a NIC at any one time. So no need for locking.
213213
*/
214214
for (i = 0; i < hdr_len / 4 && bytes < PAGE_SIZE; i++)
215-
bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
216-
" %08x", le32_to_cpu(hdr[i].u32[0]));
215+
bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
216+
" %08x",
217+
le32_to_cpu(hdr[i].u32[0]));
217218

218219
for (i = 0; i < inlen / 4 && bytes < PAGE_SIZE; i++)
219-
bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
220-
" %08x", le32_to_cpu(inbuf[i].u32[0]));
220+
bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
221+
" %08x",
222+
le32_to_cpu(inbuf[i].u32[0]));
221223

222224
netif_info(efx, hw, efx->net_dev, "MCDI RPC REQ:%s\n", buf);
223225
}
@@ -302,15 +304,15 @@ static void efx_mcdi_read_response_header(struct efx_nic *efx)
302304
*/
303305
for (i = 0; i < hdr_len && bytes < PAGE_SIZE; i++) {
304306
efx->type->mcdi_read_response(efx, &hdr, (i * 4), 4);
305-
bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
306-
" %08x", le32_to_cpu(hdr.u32[0]));
307+
bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
308+
" %08x", le32_to_cpu(hdr.u32[0]));
307309
}
308310

309311
for (i = 0; i < data_len && bytes < PAGE_SIZE; i++) {
310312
efx->type->mcdi_read_response(efx, &hdr,
311313
mcdi->resp_hdr_len + (i * 4), 4);
312-
bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
313-
" %08x", le32_to_cpu(hdr.u32[0]));
314+
bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
315+
" %08x", le32_to_cpu(hdr.u32[0]));
314316
}
315317

316318
netif_info(efx, hw, efx->net_dev, "MCDI RPC RESP:%s\n", buf);
@@ -1417,19 +1419,21 @@ void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
14171419
}
14181420

14191421
ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
1420-
offset = snprintf(buf, len, "%u.%u.%u.%u",
1421-
le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
1422-
le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
1422+
offset = scnprintf(buf, len, "%u.%u.%u.%u",
1423+
le16_to_cpu(ver_words[0]),
1424+
le16_to_cpu(ver_words[1]),
1425+
le16_to_cpu(ver_words[2]),
1426+
le16_to_cpu(ver_words[3]));
14231427

14241428
/* EF10 may have multiple datapath firmware variants within a
14251429
* single version. Report which variants are running.
14261430
*/
14271431
if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
14281432
struct efx_ef10_nic_data *nic_data = efx->nic_data;
14291433

1430-
offset += snprintf(buf + offset, len - offset, " rx%x tx%x",
1431-
nic_data->rx_dpcpu_fw_id,
1432-
nic_data->tx_dpcpu_fw_id);
1434+
offset += scnprintf(buf + offset, len - offset, " rx%x tx%x",
1435+
nic_data->rx_dpcpu_fw_id,
1436+
nic_data->tx_dpcpu_fw_id);
14331437

14341438
/* It's theoretically possible for the string to exceed 31
14351439
* characters, though in practice the first three version

0 commit comments

Comments
 (0)