Skip to content

Commit 9fcdb1c

Browse files
jacob-kelleranguy11
authored andcommitted
i40e: remove read access to debugfs files
The 'command' and 'netdev_ops' debugfs files are a legacy debugging interface supported by the i40e driver since its early days by commit 02e9c29 ("i40e: debugfs interface"). Both of these debugfs files provide a read handler which is mostly useless, and which is implemented with questionable logic. They both use a static 256 byte buffer which is initialized to the empty string. In the case of the 'command' file this buffer is literally never used and simply wastes space. In the case of the 'netdev_ops' file, the last command written is saved here. On read, the files contents are presented as the name of the device followed by a colon and then the contents of their respective static buffer. For 'command' this will always be "<device>: ". For 'netdev_ops', this will be "<device>: <last command written>". But note the buffer is shared between all devices operated by this module. At best, it is mostly meaningless information, and at worse it could be accessed simultaneously as there doesn't appear to be any locking mechanism. We have also recently received multiple reports for both read functions about their use of snprintf and potential overflow that could result in reading arbitrary kernel memory. For the 'command' file, this is definitely impossible, since the static buffer is always zero and never written to. For the 'netdev_ops' file, it does appear to be possible, if the user carefully crafts the command input, it will be copied into the buffer, which could be large enough to cause snprintf to truncate, which then causes the copy_to_user to read beyond the length of the buffer allocated by kzalloc. A minimal fix would be to replace snprintf() with scnprintf() which would cap the return to the number of bytes written, preventing an overflow. A more involved fix would be to drop the mostly useless static buffers, saving 512 bytes and modifying the read functions to stop needing those as input. Instead, lets just completely drop the read access to these files. These are debug interfaces exposed as part of debugfs, and I don't believe that dropping read access will break any script, as the provided output is pretty useless. You can find the netdev name through other more standard interfaces, and the 'netdev_ops' interface can easily result in garbage if you issue simultaneous writes to multiple devices at once. In order to properly remove the i40e_dbg_netdev_ops_buf, we need to refactor its write function to avoid using the static buffer. Instead, use the same logic as the i40e_dbg_command_write, with an allocated buffer. Update the code to use this instead of the static buffer, and ensure we free the buffer on exit. This fixes simultaneous writes to 'netdev_ops' on multiple devices, and allows us to remove the now unused static buffer along with removing the read access. Fixes: 02e9c29 ("i40e: debugfs interface") Reported-by: Kunwu Chan <[email protected]> Closes: https://lore.kernel.org/intel-wired-lan/[email protected]/ Reported-by: Wang Haoran <[email protected]> Closes: https://lore.kernel.org/all/CANZ3JQRRiOdtfQJoP9QM=6LS1Jto8PGBGw6y7-TL=BcnzHQn1Q@mail.gmail.com/ Reported-by: Amir Mohammad Jahangirzad <[email protected]> Closes: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Jacob Keller <[email protected]> Reviewed-by: Dawid Osuchowski <[email protected]> Reviewed-by: Aleksandr Loktionov <[email protected]> Reviewed-by: Simon Horman <[email protected]> Reviewed-by: Kunwu Chan <[email protected]> Tested-by: Rinitha S <[email protected]> (A Contingent worker at Intel) Signed-off-by: Tony Nguyen <[email protected]>
1 parent acf3a5c commit 9fcdb1c

File tree

1 file changed

+19
-104
lines changed

1 file changed

+19
-104
lines changed

drivers/net/ethernet/intel/i40e/i40e_debugfs.c

Lines changed: 19 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -40,48 +40,6 @@ static struct i40e_vsi *i40e_dbg_find_vsi(struct i40e_pf *pf, int seid)
4040
* setup, adding or removing filters, or other things. Many of
4141
* these will be useful for some forms of unit testing.
4242
**************************************************************/
43-
static char i40e_dbg_command_buf[256] = "";
44-
45-
/**
46-
* i40e_dbg_command_read - read for command datum
47-
* @filp: the opened file
48-
* @buffer: where to write the data for the user to read
49-
* @count: the size of the user's buffer
50-
* @ppos: file position offset
51-
**/
52-
static ssize_t i40e_dbg_command_read(struct file *filp, char __user *buffer,
53-
size_t count, loff_t *ppos)
54-
{
55-
struct i40e_pf *pf = filp->private_data;
56-
struct i40e_vsi *main_vsi;
57-
int bytes_not_copied;
58-
int buf_size = 256;
59-
char *buf;
60-
int len;
61-
62-
/* don't allow partial reads */
63-
if (*ppos != 0)
64-
return 0;
65-
if (count < buf_size)
66-
return -ENOSPC;
67-
68-
buf = kzalloc(buf_size, GFP_KERNEL);
69-
if (!buf)
70-
return -ENOSPC;
71-
72-
main_vsi = i40e_pf_get_main_vsi(pf);
73-
len = snprintf(buf, buf_size, "%s: %s\n", main_vsi->netdev->name,
74-
i40e_dbg_command_buf);
75-
76-
bytes_not_copied = copy_to_user(buffer, buf, len);
77-
kfree(buf);
78-
79-
if (bytes_not_copied)
80-
return -EFAULT;
81-
82-
*ppos = len;
83-
return len;
84-
}
8543

8644
static char *i40e_filter_state_string[] = {
8745
"INVALID",
@@ -1621,7 +1579,6 @@ static ssize_t i40e_dbg_command_write(struct file *filp,
16211579
static const struct file_operations i40e_dbg_command_fops = {
16221580
.owner = THIS_MODULE,
16231581
.open = simple_open,
1624-
.read = i40e_dbg_command_read,
16251582
.write = i40e_dbg_command_write,
16261583
};
16271584

@@ -1630,48 +1587,6 @@ static const struct file_operations i40e_dbg_command_fops = {
16301587
* The netdev_ops entry in debugfs is for giving the driver commands
16311588
* to be executed from the netdev operations.
16321589
**************************************************************/
1633-
static char i40e_dbg_netdev_ops_buf[256] = "";
1634-
1635-
/**
1636-
* i40e_dbg_netdev_ops_read - read for netdev_ops datum
1637-
* @filp: the opened file
1638-
* @buffer: where to write the data for the user to read
1639-
* @count: the size of the user's buffer
1640-
* @ppos: file position offset
1641-
**/
1642-
static ssize_t i40e_dbg_netdev_ops_read(struct file *filp, char __user *buffer,
1643-
size_t count, loff_t *ppos)
1644-
{
1645-
struct i40e_pf *pf = filp->private_data;
1646-
struct i40e_vsi *main_vsi;
1647-
int bytes_not_copied;
1648-
int buf_size = 256;
1649-
char *buf;
1650-
int len;
1651-
1652-
/* don't allow partal reads */
1653-
if (*ppos != 0)
1654-
return 0;
1655-
if (count < buf_size)
1656-
return -ENOSPC;
1657-
1658-
buf = kzalloc(buf_size, GFP_KERNEL);
1659-
if (!buf)
1660-
return -ENOSPC;
1661-
1662-
main_vsi = i40e_pf_get_main_vsi(pf);
1663-
len = snprintf(buf, buf_size, "%s: %s\n", main_vsi->netdev->name,
1664-
i40e_dbg_netdev_ops_buf);
1665-
1666-
bytes_not_copied = copy_to_user(buffer, buf, len);
1667-
kfree(buf);
1668-
1669-
if (bytes_not_copied)
1670-
return -EFAULT;
1671-
1672-
*ppos = len;
1673-
return len;
1674-
}
16751590

16761591
/**
16771592
* i40e_dbg_netdev_ops_write - write into netdev_ops datum
@@ -1685,35 +1600,36 @@ static ssize_t i40e_dbg_netdev_ops_write(struct file *filp,
16851600
size_t count, loff_t *ppos)
16861601
{
16871602
struct i40e_pf *pf = filp->private_data;
1603+
char *cmd_buf, *buf_tmp;
16881604
int bytes_not_copied;
16891605
struct i40e_vsi *vsi;
1690-
char *buf_tmp;
16911606
int vsi_seid;
16921607
int i, cnt;
16931608

16941609
/* don't allow partial writes */
16951610
if (*ppos != 0)
16961611
return 0;
1697-
if (count >= sizeof(i40e_dbg_netdev_ops_buf))
1698-
return -ENOSPC;
16991612

1700-
memset(i40e_dbg_netdev_ops_buf, 0, sizeof(i40e_dbg_netdev_ops_buf));
1701-
bytes_not_copied = copy_from_user(i40e_dbg_netdev_ops_buf,
1702-
buffer, count);
1703-
if (bytes_not_copied)
1613+
cmd_buf = kzalloc(count + 1, GFP_KERNEL);
1614+
if (!cmd_buf)
1615+
return count;
1616+
bytes_not_copied = copy_from_user(cmd_buf, buffer, count);
1617+
if (bytes_not_copied) {
1618+
kfree(cmd_buf);
17041619
return -EFAULT;
1705-
i40e_dbg_netdev_ops_buf[count] = '\0';
1620+
}
1621+
cmd_buf[count] = '\0';
17061622

1707-
buf_tmp = strchr(i40e_dbg_netdev_ops_buf, '\n');
1623+
buf_tmp = strchr(cmd_buf, '\n');
17081624
if (buf_tmp) {
17091625
*buf_tmp = '\0';
1710-
count = buf_tmp - i40e_dbg_netdev_ops_buf + 1;
1626+
count = buf_tmp - cmd_buf + 1;
17111627
}
17121628

1713-
if (strncmp(i40e_dbg_netdev_ops_buf, "change_mtu", 10) == 0) {
1629+
if (strncmp(cmd_buf, "change_mtu", 10) == 0) {
17141630
int mtu;
17151631

1716-
cnt = sscanf(&i40e_dbg_netdev_ops_buf[11], "%i %i",
1632+
cnt = sscanf(&cmd_buf[11], "%i %i",
17171633
&vsi_seid, &mtu);
17181634
if (cnt != 2) {
17191635
dev_info(&pf->pdev->dev, "change_mtu <vsi_seid> <mtu>\n");
@@ -1735,8 +1651,8 @@ static ssize_t i40e_dbg_netdev_ops_write(struct file *filp,
17351651
dev_info(&pf->pdev->dev, "Could not acquire RTNL - please try again\n");
17361652
}
17371653

1738-
} else if (strncmp(i40e_dbg_netdev_ops_buf, "set_rx_mode", 11) == 0) {
1739-
cnt = sscanf(&i40e_dbg_netdev_ops_buf[11], "%i", &vsi_seid);
1654+
} else if (strncmp(cmd_buf, "set_rx_mode", 11) == 0) {
1655+
cnt = sscanf(&cmd_buf[11], "%i", &vsi_seid);
17401656
if (cnt != 1) {
17411657
dev_info(&pf->pdev->dev, "set_rx_mode <vsi_seid>\n");
17421658
goto netdev_ops_write_done;
@@ -1756,8 +1672,8 @@ static ssize_t i40e_dbg_netdev_ops_write(struct file *filp,
17561672
dev_info(&pf->pdev->dev, "Could not acquire RTNL - please try again\n");
17571673
}
17581674

1759-
} else if (strncmp(i40e_dbg_netdev_ops_buf, "napi", 4) == 0) {
1760-
cnt = sscanf(&i40e_dbg_netdev_ops_buf[4], "%i", &vsi_seid);
1675+
} else if (strncmp(cmd_buf, "napi", 4) == 0) {
1676+
cnt = sscanf(&cmd_buf[4], "%i", &vsi_seid);
17611677
if (cnt != 1) {
17621678
dev_info(&pf->pdev->dev, "napi <vsi_seid>\n");
17631679
goto netdev_ops_write_done;
@@ -1775,21 +1691,20 @@ static ssize_t i40e_dbg_netdev_ops_write(struct file *filp,
17751691
dev_info(&pf->pdev->dev, "napi called\n");
17761692
}
17771693
} else {
1778-
dev_info(&pf->pdev->dev, "unknown command '%s'\n",
1779-
i40e_dbg_netdev_ops_buf);
1694+
dev_info(&pf->pdev->dev, "unknown command '%s'\n", cmd_buf);
17801695
dev_info(&pf->pdev->dev, "available commands\n");
17811696
dev_info(&pf->pdev->dev, " change_mtu <vsi_seid> <mtu>\n");
17821697
dev_info(&pf->pdev->dev, " set_rx_mode <vsi_seid>\n");
17831698
dev_info(&pf->pdev->dev, " napi <vsi_seid>\n");
17841699
}
17851700
netdev_ops_write_done:
1701+
kfree(cmd_buf);
17861702
return count;
17871703
}
17881704

17891705
static const struct file_operations i40e_dbg_netdev_ops_fops = {
17901706
.owner = THIS_MODULE,
17911707
.open = simple_open,
1792-
.read = i40e_dbg_netdev_ops_read,
17931708
.write = i40e_dbg_netdev_ops_write,
17941709
};
17951710

0 commit comments

Comments
 (0)