Skip to contentSkip to conversation

nfs_super_set_maxbytes patch

PATCH Alexey Dvoichenkov 3 participants 5 messages Mar 17, 2016 – Mar 23, 2016

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

Hello. I've found a small bug in what appears to be the maximum file size handling code.

The problem here, as far as I understand, is that casting from an unsigned type to a signed type, when the latter cannot represent the arithmetic value of the former, is UB. In practice, under the PaX size overflow protection, this code crashes when mounting from FreeBSD servers that send "all ones" in the size field.

Not sure I'm doing things right with the list and I'm not subscribed, so please CC.

The fix should look something like this:

nfs/internal.h renamed from nfs/internal.h.orig +2 -2
@@ -612,9 +612,9 @@
 static inline
 void nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
 {
+	if (maxfilesize > MAX_LFS_FILESIZE || maxfilesize == 0)
+		maxfilesize = MAX_LFS_FILESIZE;
 	sb->s_maxbytes = (loff_t)maxfilesize;
-	if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
-		sb->s_maxbytes = MAX_LFS_FILESIZE;
 }
 
 /*

-- AD

#1 lore raw
Quoted 17 lines · Alexey Dvoichenkov

Hello. I've found a small bug in what appears to be the maximum file size handling code.

The problem here, as far as I understand, is that casting from an unsigned type to a signed type, when the latter cannot represent the arithmetic value of the former, is UB. In practice, under the PaX size overflow protection, this code crashes when mounting from FreeBSD servers that send "all ones" in the size field.

Not sure I'm doing things right with the list and I'm not subscribed, so please CC.

The fix should look something like this:

nfs/internal.h 2016-01-02 03:19:04.599120855 +1000 line 619

Quoted 10 lines
nfs/internal.h 2016-01-02 03:19:04.599120855 +1000 renamed from nfs/internal.h.orig 2015-11-02 10:05:25.000000000 +1000 +2 -2
@@ -612,9 +612,9 @@
 static inline
 void nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
 {
+       if (maxfilesize > MAX_LFS_FILESIZE || maxfilesize == 0)
+               maxfilesize = MAX_LFS_FILESIZE;
        sb->s_maxbytes = (loff_t)maxfilesize;
-       if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
-               sb->s_maxbytes = MAX_LFS_FILESIZE;
 }
 

Why are we having to change _correct_ code in order to work with a checking tool?

Trond

#2 lore raw
Quoted 15 lines · On Thu, 17 Mar 2016 09:20:14 -0400 Trond Myklebust…
Quoted 14 lines · Alexey Dvoichenkov

Hello. I've found a small bug in what appears to be the maximum file size handling code.

The problem here, as far as I understand, is that casting from an unsigned type to a signed type, when the latter cannot represent the arithmetic value of the former, is UB. In practice, under the PaX size overflow protection, this code crashes when mounting from FreeBSD servers that send "all ones" in the size field.

Not sure I'm doing things right with the list and I'm not subscribed, so please CC.

The fix should look something like this:

Quoted 16 lines

nfs/internal.h 2016-01-02 03:19:04.599120855 +1000 line 619

Quoted 13 lines
nfs/internal.h 2016-01-02 03:19:04.599120855 +1000 renamed from nfs/internal.h.orig 2015-11-02 10:05:25.000000000 +1000 +2 -2
@@ -612,9 +612,9 @@
 static inline
 void nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
 {
+       if (maxfilesize > MAX_LFS_FILESIZE || maxfilesize == 0)
+               maxfilesize = MAX_LFS_FILESIZE;
        sb->s_maxbytes = (loff_t)maxfilesize;
-       if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
-               sb->s_maxbytes = MAX_LFS_FILESIZE;
 }
 

Why are we having to change _correct_ code in order to work with a checking tool?

Well, as far as I understand the standard, this cast may result in undefined behaviour, which in my case it does. The fact that the code crashes only under PaX, but not when compiled "normally", is a compiler-dependent detail. So I'm not sure, in what sense is the code correct?

On the other hand, I understand it may be annoying to fix this sort of virtually harmless stuff. Then, perhaps, another reason to change it is that in practice people use GRSEC with NFS and things will break.

-- AD

#3 lore raw
Quoted 19 lines · Alexey Dvoichenkov
Quoted 8 lines · On Thu, 17 Mar 2016 09:20:14 -0400 Trond Myklebust…
Quoted 8 lines
nfs/internal.h 2016-01-02 03:19:04.599120855 +1000 renamed from nfs/internal.h.orig 2015-11-02 10:05:25.000000000 +1000 +2 -0
@@ -612,9 +612,9 @@
 static inline
 void nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
 {
+       if (maxfilesize > MAX_LFS_FILESIZE || maxfilesize == 0)
+               maxfilesize = MAX_LFS_FILESIZE;
Quoted 8 lines

nfs/internal.h 2016-01-02 03:19:04.599120855 +1000 line 619

Quoted 5 lines
nfs/internal.h 2016-01-02 03:19:04.599120855 +1000 renamed from nfs/internal.h.orig 2015-11-02 10:05:25.000000000 +1000 +0 -2
        sb->s_maxbytes = (loff_t)maxfilesize;
-       if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
-               sb->s_maxbytes = MAX_LFS_FILESIZE;
 }
 

Why are we having to change _correct_ code in order to work with a checking tool?

first of all, it's correct in that it relies on implementation defined behaviour and not UB. C99 6.3.1.3 says:

Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation- defined signal is raised.

it's still unfortunate of course as the reliance on the above is trivially avoidable (and would also satisfy the size overflow plugin which otherwise chooses the other possible behaviour). all that said, i think the bigger problem with this code pattern is that it's putting the cart before the horses so to speak, so it'd be better if it was changed for that reason alone.

#4 lore raw
In reply to Trond Myklebust
Quoted 2 lines · On Thu, 17 Mar 2016 09:20:14 -0400 Trond Myklebust…

Why are we having to change _correct_ code in order to work with a checking tool?

Oh, and I guess it could be fixed in GRSEC instead.

-- AD