]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iommu/amd: Fix illegal device-id access in IOMMU debugfs
authorGuanghui Feng <guanghuifeng@linux.alibaba.com>
Thu, 19 Mar 2026 07:37:53 +0000 (15:37 +0800)
committerJoerg Roedel <joerg.roedel@amd.com>
Fri, 27 Mar 2026 08:26:58 +0000 (09:26 +0100)
In the current AMD IOMMU debugFS, when multiple processes use the IOMMU
debugFS process simultaneously, illegal access issues can occur in the
following execution flow:

1. CPU1: Sets a valid sbdf via devid_write, then checks the sbdf's
validity in execution flows such as devid_show, iommu_devtbl_show,
and iommu_irqtbl_show.

2. CPU2: Sets an invalid sbdf via devid_write, at which point the sbdf
value is -1.

3. CPU1: accesses the IOMMU device table, IRQ table, based on the
invalid SBDF value of -1, resulting in illegal access.

This is especially problematic in monitoring scripts, where multiple
scripts may access debugFS simultaneously, and some scripts may
unexpectedly set invalid values, which triggers illegal access in
debugfs.

This patch modifies the execution flow of devid_show,
iommu_devtbl_show, and iommu_irqtbl_show to ensure that these
processes determine the validity and access based on the
same device-id, thus guaranteeing correctness and robustness.

Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com>
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
drivers/iommu/amd/debugfs.c

index 20b04996441d62273a8c17d9cc152e05045f1b62..0b03e0622f67e8962e4c7c1a78b689bdca4c4817 100644 (file)
@@ -197,10 +197,11 @@ static ssize_t devid_write(struct file *filp, const char __user *ubuf,
 static int devid_show(struct seq_file *m, void *unused)
 {
        u16 devid;
+       int sbdf_shadow = sbdf;
 
-       if (sbdf >= 0) {
-               devid = PCI_SBDF_TO_DEVID(sbdf);
-               seq_printf(m, "%04x:%02x:%02x.%x\n", PCI_SBDF_TO_SEGID(sbdf),
+       if (sbdf_shadow >= 0) {
+               devid = PCI_SBDF_TO_DEVID(sbdf_shadow);
+               seq_printf(m, "%04x:%02x:%02x.%x\n", PCI_SBDF_TO_SEGID(sbdf_shadow),
                           PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid));
        } else
                seq_puts(m, "No or Invalid input provided\n");
@@ -237,13 +238,14 @@ static int iommu_devtbl_show(struct seq_file *m, void *unused)
 {
        struct amd_iommu_pci_seg *pci_seg;
        u16 seg, devid;
+       int sbdf_shadow = sbdf;
 
-       if (sbdf < 0) {
+       if (sbdf_shadow < 0) {
                seq_puts(m, "Enter a valid device ID to 'devid' file\n");
                return 0;
        }
-       seg = PCI_SBDF_TO_SEGID(sbdf);
-       devid = PCI_SBDF_TO_DEVID(sbdf);
+       seg = PCI_SBDF_TO_SEGID(sbdf_shadow);
+       devid = PCI_SBDF_TO_DEVID(sbdf_shadow);
 
        for_each_pci_segment(pci_seg) {
                if (pci_seg->id != seg)
@@ -336,19 +338,20 @@ static int iommu_irqtbl_show(struct seq_file *m, void *unused)
 {
        struct amd_iommu_pci_seg *pci_seg;
        u16 devid, seg;
+       int sbdf_shadow = sbdf;
 
        if (!irq_remapping_enabled) {
                seq_puts(m, "Interrupt remapping is disabled\n");
                return 0;
        }
 
-       if (sbdf < 0) {
+       if (sbdf_shadow < 0) {
                seq_puts(m, "Enter a valid device ID to 'devid' file\n");
                return 0;
        }
 
-       seg = PCI_SBDF_TO_SEGID(sbdf);
-       devid = PCI_SBDF_TO_DEVID(sbdf);
+       seg = PCI_SBDF_TO_SEGID(sbdf_shadow);
+       devid = PCI_SBDF_TO_DEVID(sbdf_shadow);
 
        for_each_pci_segment(pci_seg) {
                if (pci_seg->id != seg)