Skip to contentSkip to conversation

mm: adjust max read count in generic_file_buffered_read()

PATCH In review Chengguang Xu 3 participants 8 messages Jul 19, 2018 – Aug 8, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: adjust max read count in generic_file_buffered_read()

When we try to truncate read count in generic_file_buffered_read(), should deliver (sb->s_maxbytes - offset) as maximum count not sb->s_maxbytes itself.

---
 mm/filemap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/filemap.c +1 -1
index 52517f28e6f4..5c2d481d21cf 100644
@@ -2064,7 +2064,7 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
 
 	if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
 		return 0;
-	iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
+	iov_iter_truncate(iter, inode->i_sb->s_maxbytes - *ppos);
 
 	index = *ppos >> PAGE_SHIFT;
 	prev_index = ra->prev_pos >> PAGE_SHIFT;
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Chengguang Xu

When we try to truncate read count in generic_file_buffered_read(), should deliver (sb->s_maxbytes - offset) as maximum count not sb->s_maxbytes itself.

Looks good to me. You can add:

BTW, I can see you didn't include two (I'd say the most important ;) addresses to CC: Al Viro as a VFS maintainer and linux-fsdevel mailing list. Although this code resides in mm/ it is in fact a filesystem code. Added now.

Honza

Quoted 20 lines
---
 mm/filemap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/filemap.c +1 -1
index 52517f28e6f4..5c2d481d21cf 100644
@@ -2064,7 +2064,7 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
 
 	if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
 		return 0;
-	iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
+	iov_iter_truncate(iter, inode->i_sb->s_maxbytes - *ppos);
 
 	index = *ppos >> PAGE_SHIFT;
 	prev_index = ra->prev_pos >> PAGE_SHIFT;
Signature · 1 line
2.17.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#3 lore raw
Quoted 9 lines · Andrew Morton
Quoted 7 lines · On Thu, 19 Jul 2018 10:58:12 +0200 Jan Kara <[email protected]> wrote:
Quoted 5 lines · Chengguang Xu

When we try to truncate read count in generic_file_buffered_read(), should deliver (sb->s_maxbytes - offset) as maximum count not sb->s_maxbytes itself.

Quoted 7 lines
Quoted 3 lines

Looks good to me. You can add:

Yup.

What are the runtime effects of this bug?

Good question. I think ->readpage() could be called for index beyond maximum file size supported by the filesystem leading to weird filesystem behavior due to overflows in internal calculations.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#4 lore raw
Quoted 14 lines · On Mon, 6 Aug 2018 12:22:03 +0200 Jan Kara <[email protected]> wrote:
Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Thu, 19 Jul 2018 10:58:12 +0200 Jan Kara <[email protected]> wrote:
Quoted 5 lines · Chengguang Xu

When we try to truncate read count in generic_file_buffered_read(), should deliver (sb->s_maxbytes - offset) as maximum count not sb->s_maxbytes itself.

Looks good to me. You can add:

Quoted 8 lines
Quoted 3 lines

Yup.

What are the runtime effects of this bug?

Good question. I think ->readpage() could be called for index beyond maximum file size supported by the filesystem leading to weird filesystem behavior due to overflows in internal calculations.

Sure. But is it possible for userspace to trigger this behaviour? Possibly all callers have already sanitized the arguments by this stage in which case the statement is arguably redundant.

I guess I'll put a cc:stable on it and send it in for 4.19-rc1, so we get a bit more time to poke at it. But we should have a better understanding of the userspace impact.

maintainer
#5 lore raw
Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Mon, 6 Aug 2018 12:22:03 +0200 Jan Kara <[email protected]> wrote:
Quoted 16 lines · Andrew Morton
Quoted 10 lines · On Thu, 19 Jul 2018 10:58:12 +0200 Jan Kara <[email protected]> wrote:
Quoted 5 lines · Chengguang Xu

When we try to truncate read count in generic_file_buffered_read(), should deliver (sb->s_maxbytes - offset) as maximum count not sb->s_maxbytes itself.

Looks good to me. You can add:

Yup.

What are the runtime effects of this bug?

Quoted 8 lines
Quoted 4 lines

Good question. I think ->readpage() could be called for index beyond maximum file size supported by the filesystem leading to weird filesystem behavior due to overflows in internal calculations.

Sure. But is it possible for userspace to trigger this behaviour? Possibly all callers have already sanitized the arguments by this stage in which case the statement is arguably redundant.

So I don't think there's any sanitization going on before generic_file_buffered_read(). E.g. I don't see any s_maxbytes check on ksys_read() -> vfs_read() -> __vfs_read() -> new_sync_read() -> call_read_iter() -> generic_file_read_iter() -> generic_file_buffered_read() path... However now thinking about this again:
We are guaranteed i_size is within s_maxbytes (places modifying i_size are checking for this) and generic_file_buffered_read() stops when it should read beyond i_size. So in the end I don't think there's any breakage possible and the patch is not necessary?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#6 lore raw
Quoted 25 lines · Jan Kara
Quoted 16 lines · Andrew Morton
Quoted 14 lines · On Mon, 6 Aug 2018 12:22:03 +0200 Jan Kara <[email protected]> wrote:
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Thu, 19 Jul 2018 10:58:12 +0200 Jan Kara <[email protected]> wrote:
Quoted 5 lines · Chengguang Xu

When we try to truncate read count in generic_file_buffered_read(), should deliver (sb->s_maxbytes - offset) as maximum count not sb->s_maxbytes itself.

Looks good to me. You can add:

Yup.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

What are the runtime effects of this bug?

Good question. I think ->readpage() could be called for index beyond maximum file size supported by the filesystem leading to weird filesystem behavior due to overflows in internal calculations.

Sure. But is it possible for userspace to trigger this behaviour? Possibly all callers have already sanitized the arguments by this stage in which case the statement is arguably redundant.

Quoted 10 lines

So I don't think there's any sanitization going on before generic_file_buffered_read(). E.g. I don't see any s_maxbytes check on ksys_read() -> vfs_read() -> __vfs_read() -> new_sync_read() -> call_read_iter() -> generic_file_read_iter() -> generic_file_buffered_read() path... However now thinking about this again:
We are guaranteed i_size is within s_maxbytes (places modifying i_size are checking for this) and generic_file_buffered_read() stops when it should read beyond i_size. So in the end I don't think there's any breakage possible and the patch is not necessary?

I think most of time i_size is within s_maxbytes in local filesystem, but consider network filesystem, write big file in 64bit client and read in 32bit client, in this case maybe generic_file_buffered_read() can read more than s_maxbytes, right?

Thanks,
Chengguang

maintainer
#7 lore raw
Quoted 36 lines · On Wed 08-08-18 08:57:13, cgxu519 wrote:
Quoted 25 lines · Jan Kara
Quoted 16 lines · Andrew Morton
Quoted 14 lines · On Mon, 6 Aug 2018 12:22:03 +0200 Jan Kara <[email protected]> wrote:
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Thu, 19 Jul 2018 10:58:12 +0200 Jan Kara <[email protected]> wrote:
Quoted 5 lines · Chengguang Xu

When we try to truncate read count in generic_file_buffered_read(), should deliver (sb->s_maxbytes - offset) as maximum count not sb->s_maxbytes itself.

Looks good to me. You can add:

Yup.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

What are the runtime effects of this bug?

Good question. I think ->readpage() could be called for index beyond maximum file size supported by the filesystem leading to weird filesystem behavior due to overflows in internal calculations.

Sure. But is it possible for userspace to trigger this behaviour? Possibly all callers have already sanitized the arguments by this stage in which case the statement is arguably redundant.

Quoted 10 lines

So I don't think there's any sanitization going on before generic_file_buffered_read(). E.g. I don't see any s_maxbytes check on ksys_read() -> vfs_read() -> __vfs_read() -> new_sync_read() -> call_read_iter() -> generic_file_read_iter() -> generic_file_buffered_read() path... However now thinking about this again:
We are guaranteed i_size is within s_maxbytes (places modifying i_size are checking for this) and generic_file_buffered_read() stops when it should read beyond i_size. So in the end I don't think there's any breakage possible and the patch is not necessary?

Quoted 4 lines

I think most of time i_size is within s_maxbytes in local filesystem, but consider network filesystem, write big file in 64bit client and read in 32bit client, in this case maybe generic_file_buffered_read() can read more than s_maxbytes, right?

I'd consider this an internal problem in the implementation of the networking filesystem. Not something VFS should care about. It's similar to a normal filesystem loading corrupted file size from disk...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR