libfs: make get_sb_pseudo set s_maxbytes to value that can be cast to signed
PATCH 1/1 libfs: make get_sb_pseudo set s_maxbytes to value that can be cast to signed
This patch is a counterpart to the CIFS patch posted yesterday. I believe either patch will fix the problem seen in do_sendfile since the smaller s_maxbytes value for the two superblocks is used there.
get_sb_pseudo sets s_maxbytes to ~0ULL, which becomes negative when cast to a signed value. Fix it to use MAX_LFS_FILESIZE which casts properly to a positive signed value.
--- fs/libfs.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
fs/libfs.c +1 -1
@@ -217,7 +217,7 @@ int get_sb_pseudo(struct file_system_type *fs_type, char *name,return PTR_ERR (s); s->s_flags =MS_NOUSER ; - s->s_maxbytes = ~0ULL ; + s->s_maxbytes =MAX_LFS_FILESIZE ; s->s_blocksize =PAGE_SIZE ; s->s_blocksize_bits =PAGE_SHIFT ; s->s_magic = magic;
Signature · 1 line
1.6.0.6
PATCH 1/1 fix offset checks in do_sendfile to use unsigned values
If do_sendfile is called with a "max" value of 0, it grabs the lesser s_maxbytes value of the two superblocks to use instead. There's a problem here however. s_maxbytes is an unsigned long long and it gets cast to a signed value. If both s_maxbytes values are large enough, max will end up being negative and the comparisons in this code won't work correctly.
Change do_sendfile to use unsigned values internally for the offset checks.
--- fs/read_write.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
fs/read_write.c +3 -3
@@ -788,11 +788,11 @@ SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, }static ssize_t do_sendfile (int out_fd,int in_fd,loff_t *ppos, -size_t count,loff_t max) +size_t count,unsigned long long max) {struct file * in_file, * out_file;struct inode * in_inode, * out_inode; -loff_t pos; +unsigned long long pos;ssize_t retval;int fput_needed_in, fput_needed_out, fl;
@@ -838,7 +838,7 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,if (!max) max =min (in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); - pos = *ppos; + pos = (unsigned long long ) *ppos; retval = -EINVAL ;if (unlikely (pos <0 )) goto fput_out;
Signature · 1 line
1.6.0.6
Re: [PATCH] fix offset checks in do_sendfile to use unsigned values
Quoted 34 lines · Jeff Layton
If
do_sendfileis called with a "max" value of 0, it grabs the lessers_maxbytesvalue of the two superblocks to use instead. There's a problem here however.s_maxbytesis an unsigned long long and it gets cast to a signed value. If boths_maxbytesvalues are large enough, max will end up being negative and the comparisons in this code won't work correctly.Change
do_sendfileto use unsigned values internally for the offset checks.Signed-off-by Jeff Layton <[email protected]>--- fs/read_write.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)fs/read_write.c +2
-2@@ -788,11 +788,11 @@ SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, }static ssize_t do_sendfile (int out_fd,int in_fd,loff_t *ppos, -size_t count,loff_t max) +size_t count,unsigned long long max) {struct file * in_file, * out_file;struct inode * in_inode, * out_inode; -loff_t pos; +unsigned long long pos;ssize_t retval;int fput_needed_in, fput_needed_out, fl;
fs/read_write.c static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, · line 844
Quoted 8 lines
fs/read_write.c +1
-1@@ -838,7 +838,7 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,if (!max) max =min (in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); - pos = *ppos; + pos = (unsigned long long ) *ppos; retval = -EINVAL ;if (unlikely (pos <0 )) goto fput_out;
May it be possible that cifs is the only fs that sets sb->sb_maxbytes to exceed loff_t? It seems the others clamp it to MAX_LFS_FILESIZE while CIFS exceeds this by one. And then max is -1.
So, isn't the correct fix something similar to this?
fs/cifs/connect.c +4 -4
@@ -2452,10 +2452,10 @@ try_mount_again: tcon->local_lease = volume_info->local_lease; }if (pSesInfo) { -if (pSesInfo->capabilities &CAP_LARGE_FILES ) { - sb->s_maxbytes = (u64 )1 <<63 ; - }else - sb->s_maxbytes = (u64 )1 <<31 ;/* 2 GB */ +if (pSesInfo->capabilities &CAP_LARGE_FILES ) + sb->s_maxbytes =MAX_LFS_FILESIZE ; +else + sb->s_maxbytes =MAX_NON_LFS ; }/* BB FIXME fix time_gran to be larger for LANMAN sessions */
Re: [PATCH] fix offset checks in do_sendfile to use unsigned values
Quoted 59 lines · Johannes Weiner
Quoted 34 lines · Jeff Layton
If
do_sendfileis called with a "max" value of 0, it grabs the lessers_maxbytesvalue of the two superblocks to use instead. There's a problem here however.s_maxbytesis an unsigned long long and it gets cast to a signed value. If boths_maxbytesvalues are large enough, max will end up being negative and the comparisons in this code won't work correctly.Change
do_sendfileto use unsigned values internally for the offset checks.Signed-off-by Jeff Layton <[email protected]>--- fs/read_write.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)fs/read_write.c +2
-2@@ -788,11 +788,11 @@ SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, }static ssize_t do_sendfile (int out_fd,int in_fd,loff_t *ppos, -size_t count,loff_t max) +size_t count,unsigned long long max) {struct file * in_file, * out_file;struct inode * in_inode, * out_inode; -loff_t pos; +unsigned long long pos;ssize_t retval;int fput_needed_in, fput_needed_out, fl;fs/read_write.c static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, · line 844
Quoted 8 lines
fs/read_write.c +1
-1@@ -838,7 +838,7 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,if (!max) max =min (in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); - pos = *ppos; + pos = (unsigned long long ) *ppos; retval = -EINVAL ;if (unlikely (pos <0 )) goto fput_out;May it be possible that cifs is the only fs that sets
sb->sb_maxbytesto exceedloff_t? It seems the others clamp it toMAX_LFS_FILESIZEwhile CIFS exceeds this by one. And then max is -1.So, isn't the correct fix something similar to this?
fs/cifs/connect.c +0
-1@@ -2452,10 +2452,10 @@ try_mount_again: tcon->local_lease = volume_info->local_lease; }if (pSesInfo) { -if (pSesInfo->capabilities &CAP_LARGE_FILES ) {
fs/cifs/connect.c try_mount_again: · line 2461
Quoted 10 lines
fs/cifs/connect.c +4
-3- sb->s_maxbytes = (u64 )1 <<63 ; - }else - sb->s_maxbytes = (u64 )1 <<31 ;/* 2 GB */ +if (pSesInfo->capabilities &CAP_LARGE_FILES ) + sb->s_maxbytes =MAX_LFS_FILESIZE ; +else + sb->s_maxbytes =MAX_NON_LFS ; }/* BB FIXME fix time_gran to be larger for LANMAN sessions */
Yes and I posted that exact same cifs patch yesterday. I think we also need to do a similar fix for get_sb_pseudo. It currently sets s_maxbytes to ~0ULL...
Any of these patches will fix the immediate problem, but I think this code in do_sendfile should still account for the possibility that someone can set the value larger than MAX_LFS_FILESIZE. An alternative is to consider a WARN at mount time when filesystems set s_maxbytes larger than that value (that might help catch out of tree filesystems that get this wrong and prevent this sort of silent bug in the future).
Either way, the patch I posted for this isn't sufficient since there are some checks that need to be done against the signed values (the (pos < 0) check, for instance). I'll post a respun patch in a bit that should fix up those problems.
Thanks,
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH] fix offset checks in do_sendfile to use unsigned values
Quoted 69 lines · Jeff Layton
Quoted 68 lines · Johannes Weiner
Quoted 34 lines · Jeff Layton
If
do_sendfileis called with a "max" value of 0, it grabs the lessers_maxbytesvalue of the two superblocks to use instead. There's a problem here however.s_maxbytesis an unsigned long long and it gets cast to a signed value. If boths_maxbytesvalues are large enough, max will end up being negative and the comparisons in this code won't work correctly.Change
do_sendfileto use unsigned values internally for the offset checks.Signed-off-by Jeff Layton <[email protected]>--- fs/read_write.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)fs/read_write.c +2
-2@@ -788,11 +788,11 @@ SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, }static ssize_t do_sendfile (int out_fd,int in_fd,loff_t *ppos, -size_t count,loff_t max) +size_t count,unsigned long long max) {struct file * in_file, * out_file;struct inode * in_inode, * out_inode; -loff_t pos; +unsigned long long pos;ssize_t retval;int fput_needed_in, fput_needed_out, fl;fs/read_write.c static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, · line 844
Quoted 8 lines
fs/read_write.c +1
-1@@ -838,7 +838,7 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,if (!max) max =min (in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); - pos = *ppos; + pos = (unsigned long long ) *ppos; retval = -EINVAL ;if (unlikely (pos <0 )) goto fput_out;May it be possible that cifs is the only fs that sets
sb->sb_maxbytesto exceedloff_t? It seems the others clamp it toMAX_LFS_FILESIZEwhile CIFS exceeds this by one. And then max is -1.So, isn't the correct fix something similar to this?
fs/cifs/connect.c +4
-4@@ -2452,10 +2452,10 @@ try_mount_again: tcon->local_lease = volume_info->local_lease; }if (pSesInfo) { -if (pSesInfo->capabilities &CAP_LARGE_FILES ) { - sb->s_maxbytes = (u64 )1 <<63 ; - }else - sb->s_maxbytes = (u64 )1 <<31 ;/* 2 GB */ +if (pSesInfo->capabilities &CAP_LARGE_FILES ) + sb->s_maxbytes =MAX_LFS_FILESIZE ; +else + sb->s_maxbytes =MAX_NON_LFS ; }
Quoted 3 lines
fs/cifs/connect.c try_mount_again: · line 2461
Quoted 1 line
fs/cifs/connect.c +0
-0/* BB FIXME fix time_gran to be larger for LANMAN sessions */ Yes and I posted that exact same cifs patch yesterday.
Sorry, I missed it.
Quoted 2 lines
I think we also need to do a similar fix for
get_sb_pseudo. It currently setss_maxbytesto ~0ULL...
Yes, I saw it and agree with it.
Quoted 6 lines
Any of these patches will fix the immediate problem, but I think this code in
do_sendfileshould still account for the possibility that someone can set the value larger thanMAX_LFS_FILESIZE. An alternative is to consider a WARN at mount time when filesystems sets_maxbyteslarger than that value (that might help catch out of tree filesystems that get this wrong and prevent this sort of silent bug in the future).
Isn't MAX_LFS_FILESIZE by definition the maximum sensible value for s_maxbytes?
Quoted 4 lines
Either way, the patch I posted for this isn't sufficient since there are some checks that need to be done against the signed values (the (pos < 0) check, for instance). I'll post a respun patch in a bit that should fix up those problems.
That is already handled in rw_verify_area(), I think, so we should be able to drop it completely.
Hannes
Re: [PATCH] fix offset checks in do_sendfile to use unsigned values
Quoted 10 lines · Johannes Weiner
Quoted 6 lines
Any of these patches will fix the immediate problem, but I think this code in
do_sendfileshould still account for the possibility that someone can set the value larger thanMAX_LFS_FILESIZE. An alternative is to consider a WARN at mount time when filesystems sets_maxbyteslarger than that value (that might help catch out of tree filesystems that get this wrong and prevent this sort of silent bug in the future).Isn't
MAX_LFS_FILESIZEby definition the maximum sensible value fors_maxbytes?
Pretty much, but nothing seems to enforce it or let you know when you've exceeded it. It sort of seems like s_maxbytes ought to be loff_t or something instead of an unsigned long long. A negative value there wouldn't make much sense, but no one would be as tempted to set it higher than MAX_LFS_FILESIZE.
Quoted 7 lines
Quoted 4 lines
Either way, the patch I posted for this isn't sufficient since there are some checks that need to be done against the signed values (the (pos < 0) check, for instance). I'll post a respun patch in a bit that should fix up those problems.
That is already handled in
rw_verify_area(), I think, so we should be able to drop it completely.
If we get rid of those checks altogether, then "max" will become unused. Is that really OK here?
For discussion purposes, I've attached a replacement patch that I'm working with now.
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 1 line
From
00a22f2f1e34ba0765ca49120499e681477a265aMon Sep 17 00:00:00 2001
From: Jeff Layton <[email protected]>
Date: Wed, 22 Jul 2009 08:36:22 -0400
Subject: [PATCH] fix offset checks in do_sendfile to use unsigned values (try #2)
This is the second version of this patch. Some of the checks do need to use signed values. This patch should be more correct in that regard. This also adds a check to make sure that "pos + count" doesn't overflow.
If do_sendfile is called with a "max" value of 0, it grabs the lesser s_maxbytes value of the two superblocks to use instead. There's a problem here however. s_maxbytes is an unsigned long long and it gets cast to a signed value. If both s_maxbytes values are large enough, max will end up being negative and the comparisons in this code won't work correctly.
Change do_sendfile to use unsigned values internally for the offset checks against "max".
--- fs/read_write.c | 21 ++++++++++++--------- 1 files changed, 12 insertions(+), 9 deletions(-)
fs/read_write.c +12 -9
@@ -788,11 +788,11 @@ SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, }static ssize_t do_sendfile (int out_fd,int in_fd,loff_t *ppos, -size_t count,loff_t max) +size_t count,unsigned long long max) {struct file * in_file, * out_file;struct inode * in_inode, * out_inode; -loff_t pos; +unsigned long long pos, newpos;ssize_t retval;int fput_needed_in, fput_needed_out, fl;
@@ -835,14 +835,16 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, goto fput_out; count = retval; -if (!max) - max =min (in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); - - pos = *ppos; retval = -EINVAL ; -if (unlikely (pos <0 )) +if (unlikely (*ppos <0 )) goto fput_out; -if (unlikely (pos + count > max)) { + +if (!max) + max =min (in_inode->i_sb->s_maxbytes, + out_inode->i_sb->s_maxbytes); + pos = (unsigned long long ) *ppos; + newpos = pos + count; +if (unlikely (newpos > max || newpos < count)) { retval = -EOVERFLOW ;if (pos >= max) goto fput_out;
@@ -869,7 +871,8 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,inc_syscr (current);inc_syscw (current); -if (*ppos > max) + pos = (unsigned long long ) *ppos; +if (pos > max) retval = -EOVERFLOW ; fput_out:
Signature · 1 line
1.6.2.5
Re: [PATCH] fix offset checks in do_sendfile to use unsigned values
Quoted 24 lines · Jeff Layton
Quoted 10 lines · Johannes Weiner
Quoted 6 lines
Any of these patches will fix the immediate problem, but I think this code in
do_sendfileshould still account for the possibility that someone can set the value larger thanMAX_LFS_FILESIZE. An alternative is to consider a WARN at mount time when filesystems sets_maxbyteslarger than that value (that might help catch out of tree filesystems that get this wrong and prevent this sort of silent bug in the future).Isn't
MAX_LFS_FILESIZEby definition the maximum sensible value fors_maxbytes?Pretty much, but nothing seems to enforce it or let you know when you've exceeded it. It sort of seems like
s_maxbytesought to beloff_tor something instead of an unsigned long long. A negative value there wouldn't make much sense, but no one would be as tempted to set it higher thanMAX_LFS_FILESIZE.Quoted 5 lines
Quoted 4 lines
Either way, the patch I posted for this isn't sufficient since there are some checks that need to be done against the signed values (the (pos < 0) check, for instance). I'll post a respun patch in a bit that should fix up those problems.
Quoted 8 lines
Quoted 2 lines
That is already handled in
rw_verify_area(), I think, so we should be able to drop it completely.If we get rid of those checks altogether, then "max" will become unused. Is that really OK here?
For discussion purposes, I've attached a replacement patch that I'm working with now.
Looks fine to me
Signature · 3 lines
Thanks, Steve
Re: [PATCH] fix offset checks in do_sendfile to use unsigned values
In reply to Jeff LaytonQuoted 9 lines · Jeff Layton
Quoted 7 lines · Johannes Weiner
Quoted 6 lines
Any of these patches will fix the immediate problem, but I think this code in
do_sendfileshould still account for the possibility that someone can set the value larger thanMAX_LFS_FILESIZE. An alternative is to consider a WARN at mount time when filesystems sets_maxbyteslarger than that value (that might help catch out of tree filesystems that get this wrong and prevent this sort of silent bug in the future).
Quoted 6 lines
Quoted 3 lines
Isn't
MAX_LFS_FILESIZEby definition the maximum sensible value fors_maxbytes?Pretty much, but nothing seems to enforce it or let you know when you've exceeded it.
Kernel code shoots from the hips. We're faster that way. We might shoot granny in the process.
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Either way, the patch I posted for this isn't sufficient since there are some checks that need to be done against the signed values (the (pos < 0) check, for instance). I'll post a respun patch in a bit that should fix up those problems.
That is already handled in
rw_verify_area(), I think, so we should be able to drop it completely.If we get rid of those checks altogether, then "max" will become unused. Is that really OK here?
We still need to check for exceeding s_maxbytes, the other checks are redundant.
Quoted 48 lines
For discussion purposes, I've attached a replacement patch that I'm working with now.
Quoted 1 line
Quoted 1 line
From
00a22f2f1e34ba0765ca49120499e681477a265aMon Sep 17 00:00:00 2001From: Jeff Layton <[email protected]>
Date: Wed, 22 Jul 2009 08:36:22 -0400
Subject: [PATCH] fix offset checks indo_sendfileto use unsigned values (try #2)This is the second version of this patch. Some of the checks do need to use signed values. This patch should be more correct in that regard. This also adds a check to make sure that "pos + count" doesn't overflow.
If
do_sendfileis called with a "max" value of 0, it grabs the lessers_maxbytesvalue of the two superblocks to use instead. There's a problem here however.s_maxbytesis an unsigned long long and it gets cast to a signed value. If boths_maxbytesvalues are large enough, max will end up being negative and the comparisons in this code won't work correctly.Change
do_sendfileto use unsigned values internally for the offset checks against "max".Signed-off-by Jeff Layton <[email protected]>--- fs/read_write.c | 21 ++++++++++++--------- 1 files changed, 12 insertions(+), 9 deletions(-)fs/read_write.c +2
-2@@ -788,11 +788,11 @@ SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, }static ssize_t do_sendfile (int out_fd,int in_fd,loff_t *ppos, -size_t count,loff_t max) +size_t count,unsigned long long max) {struct file * in_file, * out_file;struct inode * in_inode, * out_inode; -loff_t pos; +unsigned long long pos, newpos;ssize_t retval;int fput_needed_in, fput_needed_out, fl;@@ -835,14 +835,16 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, goto fput_out;
fs/read_write.c static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, · line 840
Quoted 10 lines
fs/read_write.c +1
-5count = retval; -if (!max) - max =min (in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); - - pos = *ppos; retval = -EINVAL ; -if (unlikely (pos <0 )) +if (unlikely (*ppos <0 )) goto fput_out;
That check is done in rw_verify_area().
fs/read_write.c static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, · line 847
Quoted 8 lines
fs/read_write.c +7
-1-if (unlikely (pos + count > max)) { + +if (!max) + max =min (in_inode->i_sb->s_maxbytes, + out_inode->i_sb->s_maxbytes); + pos = (unsigned long long ) *ppos; + newpos = pos + count; +if (unlikely (newpos > max || newpos < count)) {
pos + count overflow is checked in rw_verify_area() as well.
fs/read_write.c static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, · line 876
Quoted 11 lines
fs/read_write.c +2
-1retval = -EOVERFLOW ;if (pos >= max) goto fput_out;@@ -869,7 +871,8 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,inc_syscr (current);inc_syscw (current); -if (*ppos > max) + pos = (unsigned long long ) *ppos; +if (pos > max) retval = -EOVERFLOW ;
This one is needed.
But frankly, I really don't like the approach of catching a bogus s_maxbytes in do_sendfile(). If we want to sanity check s_maxbytes, we should do it at mount time.
And allowing for a bigger s_maxbytes makes no sense if the data types involved when accessing the file can not handle these offsets at all, no?
Hannes
Ack
Quoted 30 lines · Jeff Layton
This patch is a counterpart to the CIFS patch posted yesterday. I believe either patch will fix the problem seen in
do_sendfilesince the smallers_maxbytesvalue for the two superblocks is used there.
get_sb_pseudosetss_maxbytesto ~0ULL, which becomes negative when cast to a signed value. Fix it to useMAX_LFS_FILESIZEwhich casts properly to a positive signed value.Signed-off-by Jeff Layton <[email protected]>---
fs/libfs.c | 2 +-1 files changed, 1 insertions(+), 1 deletions(-)fs/libfs.c +1
-2@@ -217,7 +217,7 @@ int get_sb_pseudo(struct file_system_type *fs_type, char *name, return PTR_ERR(s); s->s_flags = MS_NOUSER; - s->s_maxbytes = ~0ULL; + s->s_maxbytes = MAX_LFS_FILESIZE; s->s_blocksize = PAGE_SIZE; s->s_blocksize_bits = PAGE_SHIFT; s->s_magic = magic; --1.6.0.6
Signature · 7 lines
Thanks, Steve -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html