Skip to contentSkip to conversation

tty: fix when iov_iter_count() returns 0 in tty_write()

PATCH Superseded Sabyrzhan Tasbolatov 3 participants 5 messages Feb 17, 2021 – Feb 19, 2021 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: fix when iov_iter_count() returns 0 in tty_write()

syzbot found WARNING in iov_iter_revert[1] when iov_iter_count() returns 0, therefore INT_MAX is passed to iov_iter_revert() causing > MAX_RW_COUNT warning.

static inline ssize_t do_tty_write()
{
..

	size_t count = iov_iter_count(from);
..
		size_t size = count;
		if (ret != size)
			iov_iter_revert(from, size-ret);

[1] WARNING: lib/iov_iter.c:1090

Call Trace:
 do_tty_write drivers/tty/tty_io.c:967 [inline]
 file_tty_write.constprop.0+0x55f/0x8f0 drivers/tty/tty_io.c:1048
 call_write_iter include/linux/fs.h:1901 [inline]
 new_sync_write+0x426/0x650 fs/read_write.c:518
 vfs_write+0x791/0xa30 fs/read_write.c:605
 ksys_write+0x12d/0x250 fs/read_write.c:658
Fixes 494e63ee9c("tty: implement write_iter")
Reported-by [email protected]
---
 drivers/tty/tty_io.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/tty_io.c +3 -0
index 816e709afa56..8d6d579ecc3b 100644
@@ -905,6 +905,9 @@ static inline ssize_t do_tty_write(
 	ssize_t ret, written = 0;
 	unsigned int chunk;
 
+	if (!count)
+		return -EINVAL;
+
 	ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 22 lines · Sabyrzhan Tasbolatov

syzbot found WARNING in iov_iter_revert[1] when iov_iter_count() returns 0, therefore INT_MAX is passed to iov_iter_revert() causing > MAX_RW_COUNT warning.

static inline ssize_t do_tty_write()
{
..

	size_t count = iov_iter_count(from);
..
		size_t size = count;
		if (ret != size)
			iov_iter_revert(from, size-ret);

[1] WARNING: lib/iov_iter.c:1090

Call Trace:
 do_tty_write drivers/tty/tty_io.c:967 [inline]
 file_tty_write.constprop.0+0x55f/0x8f0 drivers/tty/tty_io.c:1048
 call_write_iter include/linux/fs.h:1901 [inline]
 new_sync_write+0x426/0x650 fs/read_write.c:518
 vfs_write+0x791/0xa30 fs/read_write.c:605
 ksys_write+0x12d/0x250 fs/read_write.c:658
Quoted 1 line
Fixes 494e63ee9c("tty: implement write_iter")

Nit, you need a ' ' before your '(' character here, otherwise the linux-next scripts will complain.

Quoted 11 lines
---
 drivers/tty/tty_io.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/tty_io.c +0 -0
index 816e709afa56..8d6d579ecc3b 100644

drivers/tty/tty_io.c static inline ssize_t do_tty_write( · line 909

Quoted 5 lines
drivers/tty/tty_io.c +2 -0
@@ -905,6 +905,9 @@ static inline ssize_t do_tty_write(
 	ssize_t ret, written = 0;
 	unsigned int chunk;
 
+	if (!count)
+		return -EINVAL;

According to the man page for write(2), I think this is the wrong error value to return, unless that is the value that was returned on kernels before the commit listed above.

Can you verify this?

thanks,

greg k-h

maintainer
#2 lore raw
Quoted 23 lines · Greg KH
Quoted 22 lines · Sabyrzhan Tasbolatov

syzbot found WARNING in iov_iter_revert[1] when iov_iter_count() returns 0, therefore INT_MAX is passed to iov_iter_revert() causing > MAX_RW_COUNT warning.

static inline ssize_t do_tty_write()
{
..

	size_t count = iov_iter_count(from);
..
		size_t size = count;
		if (ret != size)
			iov_iter_revert(from, size-ret);

[1] WARNING: lib/iov_iter.c:1090

Call Trace:
 do_tty_write drivers/tty/tty_io.c:967 [inline]
 file_tty_write.constprop.0+0x55f/0x8f0 drivers/tty/tty_io.c:1048
 call_write_iter include/linux/fs.h:1901 [inline]
 new_sync_write+0x426/0x650 fs/read_write.c:518
 vfs_write+0x791/0xa30 fs/read_write.c:605
 ksys_write+0x12d/0x250 fs/read_write.c:658
Quoted 4 lines
Quoted 1 line
Fixes 494e63ee9c("tty: implement write_iter")

Nit, you need a ' ' before your '(' character here, otherwise the linux-next scripts will complain.

Also, you got the git commit id wrong, so this needs to be fixed up anyway. You are pointing to a merge point, I doubt that's what you want to point to here, right?

thanks,

greg k-h

#3 lore raw

PATCH v2 1/1 tty: fix when iov_iter_count() returns 0 in tty_write()

syzbot found WARNING in iov_iter_revert[1] when iov_iter_count() returns 0, therefore INT_MAX is passed to iov_iter_revert() causing > MAX_RW_COUNT warning.

static inline ssize_t do_tty_write()
{
..

	size_t count = iov_iter_count(from);
..
		size_t size = count;
		if (ret != size)
			iov_iter_revert(from, size-ret);

[1] WARNING: lib/iov_iter.c:1090

Call Trace:
 do_tty_write drivers/tty/tty_io.c:967 [inline]
 file_tty_write.constprop.0+0x55f/0x8f0 drivers/tty/tty_io.c:1048
 call_write_iter include/linux/fs.h:1901 [inline]
 new_sync_write+0x426/0x650 fs/read_write.c:518
 vfs_write+0x791/0xa30 fs/read_write.c:605
 ksys_write+0x12d/0x250 fs/read_write.c:658
Fixes 9bb48c82aced ("tty: implement write_iter")
Reported-by [email protected]

---

v2: Fixed "Fixed" tag to proper commit and changed write return to -EFAULT as this statement is valid, tested via strace:

write(3, NULL, 0) = -1 EFAULT (Bad address)

Updated to -EFAULT, should be a valid exit code as copy_from_iter(.., .., from) returns -EFAULT as well if *from is invalid address.

Quoted 7 lines

Nit, you need a ' ' before your '(' character here, otherwise the linux-next scripts will complain.

Also, you got the git commit id wrong, so this needs to be fixed up anyway. You are pointing to a merge point, I doubt that's what you want to point to here, right?

Thanks!

---
 drivers/tty/tty_io.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/tty_io.c +3 -0
index 816e709afa56..e1460cad8b7d 100644
@@ -905,6 +905,9 @@ static inline ssize_t do_tty_write(
 	ssize_t ret, written = 0;
 	unsigned int chunk;
 
+	if (!count)
+		return -EFAULT;
+
 	ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.25.1
maintainer
#4 lore raw
Quoted 27 lines · Sabyrzhan Tasbolatov

syzbot found WARNING in iov_iter_revert[1] when iov_iter_count() returns 0, therefore INT_MAX is passed to iov_iter_revert() causing > MAX_RW_COUNT warning.

static inline ssize_t do_tty_write()
{
..

	size_t count = iov_iter_count(from);
..
		size_t size = count;
		if (ret != size)
			iov_iter_revert(from, size-ret);

[1] WARNING: lib/iov_iter.c:1090

Call Trace:
  do_tty_write drivers/tty/tty_io.c:967 [inline]
  file_tty_write.constprop.0+0x55f/0x8f0 drivers/tty/tty_io.c:1048
  call_write_iter include/linux/fs.h:1901 [inline]
  new_sync_write+0x426/0x650 fs/read_write.c:518
  vfs_write+0x791/0xa30 fs/read_write.c:605
  ksys_write+0x12d/0x250 fs/read_write.c:658
Fixes 9bb48c82aced ("tty: implement write_iter")
Reported-by [email protected]

---

Quoted 8 lines

v2: Fixed "Fixed" tag to proper commit and changed write return to -EFAULT as this statement is valid, tested via strace:

write(3, NULL, 0) = -1 EFAULT (Bad address)

Updated to -EFAULT, should be a valid exit code as copy_from_iter(.., .., from) returns -EFAULT as well if *from is invalid address.

Exactly, EFAULT is for invalid memory accesses. But this should be IMO EINVAL as it's an invalid argument.

BTW what's the reason vfs calls ->write with zero count of iter?

Quoted 27 lines
Quoted 6 lines

Nit, you need a ' ' before your '(' character here, otherwise the linux-next scripts will complain.

Also, you got the git commit id wrong, so this needs to be fixed up anyway. You are pointing to a merge point, I doubt that's what you want to point to here, right?

Thanks!

---
  drivers/tty/tty_io.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/tty/tty_io.c +3 -0
index 816e709afa56..e1460cad8b7d 100644
@@ -905,6 +905,9 @@ static inline ssize_t do_tty_write(
  	ssize_t ret, written = 0;
  	unsigned int chunk;
 
+	if (!count)
+		return -EFAULT;
+
  	ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
  	if (ret < 0)
  		return ret;
Signature · 1 line
js