Skip to contentSkip to conversation

PCI/proc: check __get_user() return value in proc_bus_pci_write()

PATCH In review Deepanshu Kartikey 2 participants 3 messages May 2 – May 4 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/proc: check __get_user() return value in proc_bus_pci_write()

proc_bus_pci_write() does not check the return value of __get_user(). On a faulting user pointer the extable fixup zeros the destination, and the function writes those zeros to PCI configuration space.

syzbot exploits this by writev()-ing a NULL iov_base to /proc/bus/pci/00/03.0 (the virtio-blk controller in the syzkaller VM):
zero is written to the Command register, clearing Bus Master Enable, and the disk stops responding. In-flight journal writes never complete and jbd2 hangs in wait_on_buffer() indefinitely.

Check __get_user() and return -EFAULT on failure.

---
 drivers/pci/proc.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
drivers/pci/proc.c +25 -8
index ce36e35681e8..54052157c276 100644
@@ -136,7 +136,10 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
 
 	if ((pos & 1) && cnt) {
 		unsigned char val;
-		__get_user(val, buf);
+		if (__get_user(val, buf)) {
+			ret = -EFAULT;
+			goto out;
+		}
 		pci_user_write_config_byte(dev, pos, val);
 		buf++;
 		pos++;
@@ -145,7 +148,10 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
 
 	if ((pos & 3) && cnt > 2) {
 		__le16 val;
-		__get_user(val, (__le16 __user *) buf);
+		if (__get_user(val, (__le16 __user *) buf)) {
+			ret = -EFAULT;
+			goto out;
+		}
 		pci_user_write_config_word(dev, pos, le16_to_cpu(val));
 		buf += 2;
 		pos += 2;
@@ -154,7 +160,10 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
 
 	while (cnt >= 4) {
 		__le32 val;
-		__get_user(val, (__le32 __user *) buf);
+		if (__get_user(val, (__le32 __user *) buf)) {
+			ret = -EFAULT;
+			goto out;
+		}
 		pci_user_write_config_dword(dev, pos, le32_to_cpu(val));
 		buf += 4;
 		pos += 4;
@@ -163,7 +172,10 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
 
 	if (cnt >= 2) {
 		__le16 val;
-		__get_user(val, (__le16 __user *) buf);
+		if (__get_user(val, (__le16 __user *) buf)) {
+			ret = -EFAULT;
+			goto out;
+		}
 		pci_user_write_config_word(dev, pos, le16_to_cpu(val));
 		buf += 2;
 		pos += 2;
@@ -172,16 +184,21 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
 
 	if (cnt) {
 		unsigned char val;
-		__get_user(val, buf);
+		if (__get_user(val, buf)) {
+			ret = -EFAULT;
+			goto out;
+		}
 		pci_user_write_config_byte(dev, pos, val);
 		pos++;
 	}
 
+	ret = nbytes;
+out:
 	pci_config_pm_runtime_put(dev);
-
 	*ppos = pos;
-	i_size_write(ino, dev->cfg_size);
-	return nbytes;
+	if (ret > 0)
+		i_size_write(ino, dev->cfg_size);
+	return ret;
 }
 
 #ifdef HAVE_PCI_MMAP
Signature · 1 line
2.43.0
#1 lore raw

Hello,

Quoted 1 line

Check __get_user() and return -EFAULT on failure.

[...]

drivers/pci/proc.c static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, · line 142

Quoted 9 lines
drivers/pci/proc.c +4 -1
@@ -136,7 +136,10 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
 
 	if ((pos & 1) && cnt) {
 		unsigned char val;
-		__get_user(val, buf);
+		if (__get_user(val, buf)) {
+			ret = -EFAULT;
+			goto out;
+		}

We could move to get_user() here. This would allow you to drop the access_ok(), too, as get_user() would return -EFAULT on error.

So, something simple, like:

if (get_user(val, buf))
goto err;

drivers/pci/proc.c static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, · line 196

Quoted 1 line
drivers/pci/proc.c +1 -0
+out:

Use "err" for a single goto label for the error path.

drivers/pci/proc.c static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, · line 201

Quoted 8 lines
drivers/pci/proc.c +3 -3
 	pci_config_pm_runtime_put(dev);
-
 	*ppos = pos;
-	i_size_write(ino, dev->cfg_size);
-	return nbytes;
+	if (ret > 0)
+		i_size_write(ino, dev->cfg_size);
+	return ret;

This can be kept simple:

  err:
  	pci_config_pm_runtime_put(dev);
  	return -EFAULT;

The i_size_write() is such an unfortunate band-aid, but unless we have a way to set the size before the procfs entry is created/made visible, then the problem that this aims to fix is here to stay for now, see:

ecb3908046ce ("pci: write file size to inode on proc bus file write")

Having said all that, since you are looking at proc_bus_pci_write(), then perhaps an update to proc_bus_pci_read() to use put_user() would also be prudent.

Thank you!

Krzysztof

#2 lore raw
Quoted 48 lines · Krzysztof Wilczyński

Hello,

Quoted 1 line

Check __get_user() and return -EFAULT on failure.

[...]

drivers/pci/proc.c static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, · line 142

Quoted 9 lines
drivers/pci/proc.c +4 -1
@@ -136,7 +136,10 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
 
      if ((pos & 1) && cnt) {
              unsigned char val;
-             __get_user(val, buf);
+             if (__get_user(val, buf)) {
+                     ret = -EFAULT;
+                     goto out;
+             }

We could move to get_user() here. This would allow you to drop the access_ok(), too, as get_user() would return -EFAULT on error.

So, something simple, like:

if (get_user(val, buf))

        goto err;

drivers/pci/proc.c static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, · line 196

Quoted 1 line
drivers/pci/proc.c +1 -0
+out:

Use "err" for a single goto label for the error path.

drivers/pci/proc.c static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, · line 201

Quoted 8 lines
drivers/pci/proc.c +3 -3
      pci_config_pm_runtime_put(dev);
-
      *ppos = pos;
-     i_size_write(ino, dev->cfg_size);
-     return nbytes;
+     if (ret > 0)
+             i_size_write(ino, dev->cfg_size);
+     return ret;

This can be kept simple:

  err:
        pci_config_pm_runtime_put(dev);
        return -EFAULT;

The i_size_write() is such an unfortunate band-aid, but unless we have a way to set the size before the procfs entry is created/made visible, then the problem that this aims to fix is here to stay for now, see:

ecb3908046ce ("pci: write file size to inode on proc bus file write")

Quoted 7 lines

Having said all that, since you are looking at proc_bus_pci_write(), then perhaps an update to proc_bus_pci_read() to use put_user() would also be prudent.

Thank you!

Krzysztof

Thanks for the detailed feedback. I will send patch v2 shortly.

Thanks

Deepanshu Kartikey