Skip to contentSkip to conversation

xfs: fix signed calculation related to XFS_LITINO(mp)

PATCH Superseded Gao Xiang 5 participants 11 messages Nov 12, 2020 – Nov 14, 2020

Subsystems (2)
lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 xfs: fix signed calculation related to XFS_LITINO(mp)

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

so offset becomes a large number on 32-bit platform, and cause xfs_attr_shortform_bytesfit() returns maxforkoff rather than 0

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));" assertion failure in xfs_idata_realloc().

, which can be triggered with the following commands:
touch a;
setfattr -n user.0 -v "seq 0 80" a; setfattr -n user.1 -v "seq 0 80" a
on 32-bit platform.

Fix it by restoring (int) decorator to XFS_LITINO(mp) since int type for XFS_LITINO(mp) is safe and all pre-exist signed calculations are correct.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
I'm not sure this is the preferred way or just simply fix xfs_attr_shortform_bytesfit() since I don't look into the rest of XFS_LITINO(mp) users. Add (int) to XFS_LITINO(mp) will avoid all potential regression at least.

 fs/xfs/libxfs/xfs_format.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/libxfs/xfs_format.h +1 -1
index dd764da08f6f..f58f0a44c024 100644
@@ -1061,7 +1061,7 @@ enum xfs_dinode_fmt {
 		sizeof(struct xfs_dinode) : \
 		offsetof(struct xfs_dinode, di_crc))
 #define XFS_LITINO(mp) \
-	((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb))
+	((int)((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb)))
 
 /*
  * Inode data & attribute fork sizes, per inode.
Signature · 1 line
2.18.4
#1 lore raw
Quoted 4 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Thanks for finding this! Let me think through it a little.

Quoted 7 lines

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

This should be >> 3, right.

Quoted 4 lines
           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

and >> 3 here as well.

Quoted 2 lines
           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

Ok. But wow, that magical cast to (int) in XFS_LITINO isn't at all clear to me.

XFS_LITINO() indicates a /size/ - fixing this problem by making it a signed value feels very strange, but I'm not sure what would be better, yet.

Quoted 9 lines

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));" assertion failure in xfs_idata_realloc().

, which can be triggered with the following commands:
touch a;
setfattr -n user.0 -v "seq 0 80" a; setfattr -n user.1 -v "seq 0 80" a
on 32-bit platform.

Can you please write an xfstest for this? :)

Quoted 21 lines

Fix it by restoring (int) decorator to XFS_LITINO(mp) since int type for XFS_LITINO(mp) is safe and all pre-exist signed calculations are correct.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
I'm not sure this is the preferred way or just simply fix xfs_attr_shortform_bytesfit() since I don't look into the rest of XFS_LITINO(mp) users. Add (int) to XFS_LITINO(mp) will avoid all potential regression at least.

 fs/xfs/libxfs/xfs_format.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/libxfs/xfs_format.h +0 -0
index dd764da08f6f..f58f0a44c024 100644

fs/xfs/libxfs/xfs_format.h enum xfs_dinode_fmt { · line 1063

Quoted 4 lines
fs/xfs/libxfs/xfs_format.h +1 -1
@@ -1061,7 +1061,7 @@ enum xfs_dinode_fmt {
 		sizeof(struct xfs_dinode) : \
 		offsetof(struct xfs_dinode, di_crc))>  #define XFS_LITINO(mp) \
-	((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb))
+	((int)((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb)))

If we do keep the (int) cast here we at least need a comment explaining why it cannot be removed, unless there is a better way to solve the problem.

I wonder if explicitly making XFS_LITINO() cast to a size_t would be best, and then in xfs_attr_shortform_bytesfit() we just quickly reject the query if the bytes are larger than the literal area:

fs/xfs/libxfs/xfs_attr_leaf.c +4 -0
index bb128db..5588c2e 100644
@@ -535,6 +535,10 @@ STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
        int                     maxforkoff;
        int                     offset;
 
+       /* Is there no chance we can fit? */
+       if (bytes > XFS_LITINO(mp))
+               return 0;
+
        /* rounded down */
        offset = (XFS_LITINO(mp) - bytes) >> 3;
 

or, maybe simply:

- offset = (XFS_LITINO(mp) - bytes) >> 3;
+ offset = (int)(XFS_LITINO(mp) - bytes) >> 3;

to be sure that the arithmetic doesn't get promoted to unsigned before the shift?

or maybe others have better ideas.

-Eric

Quoted 3 lines
fs/xfs/libxfs/xfs_format.h +0 -0
 /*
  * Inode data & attribute fork sizes, per inode.
 
#2 lore raw
Quoted 28 lines · Eric Sandeen
Quoted 4 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Thanks for finding this! Let me think through it a little.

Quoted 7 lines

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

This should be >> 3, right.

Quoted 4 lines
           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

and >> 3 here as well.

Quoted 2 lines
           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

Quoted 6 lines

Ok. But wow, that magical cast to (int) in XFS_LITINO isn't at all clear to me.

XFS_LITINO() indicates a /size/ - fixing this problem by making it a signed value feels very strange, but I'm not sure what would be better, yet.

TBH I think this needs to be cleaned up -- what is "LITINO", anyway?

I'm pretty sure it's the size of the literal area, aka everything after the inode core, where the forks live?

And, uh, can these things get turned into static inline helpers instead of weird macros? Separate patches, obviously.

Quoted 5 lines
Quoted 5 lines

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));" assertion failure in xfs_idata_realloc().

Quoted 7 lines
Quoted 5 lines

, which can be triggered with the following commands:
touch a;
setfattr -n user.0 -v "seq 0 80" a; setfattr -n user.1 -v "seq 0 80" a
on 32-bit platform.

Can you please write an xfstest for this? :)

Seconded.

If this is the fix for the corruption report that dgilmore reported on IRC, this should credit him as the reporter and/or tester. Especially because I thought this was just a "oh I found this via code review" until someone else pointed out that this was actually a fix for something that a user hit in the field.

The difference is that we're headed towards -rc4 and I'm much more willing to hold up posting the xfs-5.11-merge branch to get in fixes for user-reported problems.

Quoted 16 lines
Quoted 16 lines

Fix it by restoring (int) decorator to XFS_LITINO(mp) since int type for XFS_LITINO(mp) is safe and all pre-exist signed calculations are correct.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
I'm not sure this is the preferred way or just simply fix xfs_attr_shortform_bytesfit() since I don't look into the rest of XFS_LITINO(mp) users. Add (int) to XFS_LITINO(mp) will avoid all potential regression at least.

 fs/xfs/libxfs/xfs_format.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

fs/xfs/libxfs/xfs_format.h enum xfs_dinode_fmt { · line 1063

Quoted 9 lines
fs/xfs/libxfs/xfs_format.h +1 -1
index dd764da08f6f..f58f0a44c024 100644
@@ -1061,7 +1061,7 @@ enum xfs_dinode_fmt {
 		sizeof(struct xfs_dinode) : \
 		offsetof(struct xfs_dinode, di_crc))>  #define XFS_LITINO(mp) \
-	((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb))
+	((int)((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb)))

If we do keep the (int) cast here we at least need a comment explaining why it cannot be removed, unless there is a better way to solve the problem.

It's still weird, because "size of literal inode area" isn't a signed quantity because you can't have a negative size.

Quoted 9 lines

I wonder if explicitly making XFS_LITINO() cast to a size_t would be best, and then in xfs_attr_shortform_bytesfit() we just quickly reject the query if the bytes are larger than the literal area:

fs/xfs/libxfs/xfs_attr_leaf.c +0 -0
index bb128db..5588c2e 100644

fs/xfs/libxfs/xfs_attr_leaf.c STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args, · line 543

Quoted 9 lines
fs/xfs/libxfs/xfs_attr_leaf.c +4 -0
@@ -535,6 +535,10 @@ STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
        int                     maxforkoff;
        int                     offset;
 
+       /* Is there no chance we can fit? */
+       if (bytes > XFS_LITINO(mp))
+               return 0;
+
        /* rounded down */
        offset = (XFS_LITINO(mp) - bytes) >> 3;

So if LITINO is 336 and the caller asked for 350 bytes, offset will be negative here. However, offset is the proposed forkoff, right? It doesn't make any sense to have a negative forkoff, so I think Eric's (bytes > LITINO) suggestion above is correct.

This patch was hard to review because the comment for xfs_attr_shortform_bytesfit mentions "...the requested number of additional bytes", but the bytes parameter represents the total number of attr fork bytes we want, not a delta over what we have right now. Can someone please fix that comment too?

--D

Quoted 16 lines

or, maybe simply:

fs/xfs/libxfs/xfs_attr_leaf.c +1 -1
-        offset = (XFS_LITINO(mp) - bytes) >> 3;
+        offset = (int)(XFS_LITINO(mp) - bytes) >> 3;

to be sure that the arithmetic doesn't get promoted to unsigned before the shift?

or maybe others have better ideas.

-Eric

Quoted 3 lines
fs/xfs/libxfs/xfs_format.h +0 -0
 /*
  * Inode data & attribute fork sizes, per inode.
 
author
#3 lore raw

Hi,

Quoted 36 lines · Darrick J. Wong
Quoted 28 lines · Eric Sandeen
Quoted 4 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Thanks for finding this! Let me think through it a little.

Quoted 7 lines

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

This should be >> 3, right.

Quoted 4 lines
           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

and >> 3 here as well.

Quoted 2 lines
           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

Quoted 6 lines

Ok. But wow, that magical cast to (int) in XFS_LITINO isn't at all clear to me.

XFS_LITINO() indicates a /size/ - fixing this problem by making it a signed value feels very strange, but I'm not sure what would be better, yet.

Quoted 7 lines

TBH I think this needs to be cleaned up -- what is "LITINO", anyway?

I'm pretty sure it's the size of the literal area, aka everything after the inode core, where the forks live?

And, uh, can these things get turned into static inline helpers instead of weird macros? Separate patches, obviously.

Thanks, I've addressed all comments in these two reviews in v2:
https://lore.kernel.org/r/[email protected]

As for LITINO itself, btw, what would be the suggested name for this? I'm not good at naming, and will seek time working on cleaning up this.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));" assertion failure in xfs_idata_realloc().

, which can be triggered with the following commands:
touch a;
setfattr -n user.0 -v "seq 0 80" a; setfattr -n user.1 -v "seq 0 80" a
on 32-bit platform.

Quoted 3 lines
Quoted 1 line

Can you please write an xfstest for this? :)

Seconded.

Will seek time on this later as well.

Quoted 10 lines

If this is the fix for the corruption report that dgilmore reported on IRC, this should credit him as the reporter and/or tester. Especially because I thought this was just a "oh I found this via code review" until someone else pointed out that this was actually a fix for something that a user hit in the field.

The difference is that we're headed towards -rc4 and I'm much more willing to hold up posting the xfs-5.11-merge branch to get in fixes for user-reported problems.

Yeah, sorry for ignoring this original bugreport, since I thought the original bugzilla couldn't open publicly.
https://bugzilla.redhat.com/show_bug.cgi?id=1894177

It would be better to get a "Tested-by:" tag to check the original case for v2. :)

Quoted 30 lines
Quoted 17 lines
Quoted 17 lines

Fix it by restoring (int) decorator to XFS_LITINO(mp) since int type for XFS_LITINO(mp) is safe and all pre-exist signed calculations are correct.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
I'm not sure this is the preferred way or just simply fix xfs_attr_shortform_bytesfit() since I don't look into the rest of XFS_LITINO(mp) users. Add (int) to XFS_LITINO(mp) will avoid all potential regression at least.

 fs/xfs/libxfs/xfs_format.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

fs/xfs/libxfs/xfs_format.h enum xfs_dinode_fmt { · line 1063

Quoted 9 lines
fs/xfs/libxfs/xfs_format.h +1 -1
index dd764da08f6f..f58f0a44c024 100644
@@ -1061,7 +1061,7 @@ enum xfs_dinode_fmt {
 		sizeof(struct xfs_dinode) : \
 		offsetof(struct xfs_dinode, di_crc))>  #define XFS_LITINO(mp) \
-	((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb))
+	((int)((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb)))

If we do keep the (int) cast here we at least need a comment explaining why it cannot be removed, unless there is a better way to solve the problem.

Quoted 2 lines

It's still weird, because "size of literal inode area" isn't a signed quantity because you can't have a negative size.

I'm fine with either way, since my starting point was to address the regression of e9e2eae89ddb as I mentioned on IRC. And it can also be simply fixed directly.

Thanks,
Gao Xiang

Quoted 50 lines
Quoted 10 lines

I wonder if explicitly making XFS_LITINO() cast to a size_t would be best, and then in xfs_attr_shortform_bytesfit() we just quickly reject the query if the bytes are larger than the literal area:

fs/xfs/libxfs/xfs_attr_leaf.c +0 -0
index bb128db..5588c2e 100644

fs/xfs/libxfs/xfs_attr_leaf.c STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args, · line 543

Quoted 9 lines
fs/xfs/libxfs/xfs_attr_leaf.c +4 -0
@@ -535,6 +535,10 @@ STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
        int                     maxforkoff;
        int                     offset;
 
+       /* Is there no chance we can fit? */
+       if (bytes > XFS_LITINO(mp))
+               return 0;
+
        /* rounded down */
        offset = (XFS_LITINO(mp) - bytes) >> 3;

So if LITINO is 336 and the caller asked for 350 bytes, offset will be negative here. However, offset is the proposed forkoff, right? It doesn't make any sense to have a negative forkoff, so I think Eric's (bytes > LITINO) suggestion above is correct.

This patch was hard to review because the comment for xfs_attr_shortform_bytesfit mentions "...the requested number of additional bytes", but the bytes parameter represents the total number of attr fork bytes we want, not a delta over what we have right now. Can someone please fix that comment too?

--D

Quoted 16 lines

or, maybe simply:

fs/xfs/libxfs/xfs_attr_leaf.c +1 -1
-        offset = (XFS_LITINO(mp) - bytes) >> 3;
+        offset = (int)(XFS_LITINO(mp) - bytes) >> 3;

to be sure that the arithmetic doesn't get promoted to unsigned before the shift?

or maybe others have better ideas.

-Eric

Quoted 3 lines
fs/xfs/libxfs/xfs_format.h +0 -0
 /*
  * Inode data & attribute fork sizes, per inode.
 
author
#4 lore raw

[ sorry for forgetting to Cc dgilmore on this reply, it would be
much better/helpful with a "Tested-by:" for v2... Let me resend, sorry for annoying. ]

Hi,

Quoted 36 lines · Darrick J. Wong
Quoted 28 lines · Eric Sandeen
Quoted 4 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Thanks for finding this! Let me think through it a little.

Quoted 7 lines

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

This should be >> 3, right.

Quoted 4 lines
           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 8 =

and >> 3 here as well.

Quoted 2 lines
           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

Quoted 6 lines

Ok. But wow, that magical cast to (int) in XFS_LITINO isn't at all clear to me.

XFS_LITINO() indicates a /size/ - fixing this problem by making it a signed value feels very strange, but I'm not sure what would be better, yet.

Quoted 7 lines

TBH I think this needs to be cleaned up -- what is "LITINO", anyway?

I'm pretty sure it's the size of the literal area, aka everything after the inode core, where the forks live?

And, uh, can these things get turned into static inline helpers instead of weird macros? Separate patches, obviously.

Thanks, I've addressed all comments in these two reviews in v2:
https://lore.kernel.org/r/[email protected]

As for LITINO itself, btw, what would be the suggested name for this? I'm not good at naming, and will seek time working on cleaning up this.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));" assertion failure in xfs_idata_realloc().

, which can be triggered with the following commands:
touch a;
setfattr -n user.0 -v "seq 0 80" a; setfattr -n user.1 -v "seq 0 80" a
on 32-bit platform.

Quoted 3 lines
Quoted 1 line

Can you please write an xfstest for this? :)

Seconded.

Will seek time on this later as well.

Quoted 10 lines

If this is the fix for the corruption report that dgilmore reported on IRC, this should credit him as the reporter and/or tester. Especially because I thought this was just a "oh I found this via code review" until someone else pointed out that this was actually a fix for something that a user hit in the field.

The difference is that we're headed towards -rc4 and I'm much more willing to hold up posting the xfs-5.11-merge branch to get in fixes for user-reported problems.

Yeah, sorry for ignoring this original bugreport, since I thought the original bugzilla couldn't open publicly.
https://bugzilla.redhat.com/show_bug.cgi?id=1894177

It would be better to get a "Tested-by:" tag to check the original case for v2. :)

Quoted 30 lines
Quoted 17 lines
Quoted 17 lines

Fix it by restoring (int) decorator to XFS_LITINO(mp) since int type for XFS_LITINO(mp) is safe and all pre-exist signed calculations are correct.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
I'm not sure this is the preferred way or just simply fix xfs_attr_shortform_bytesfit() since I don't look into the rest of XFS_LITINO(mp) users. Add (int) to XFS_LITINO(mp) will avoid all potential regression at least.

 fs/xfs/libxfs/xfs_format.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

fs/xfs/libxfs/xfs_format.h enum xfs_dinode_fmt { · line 1063

Quoted 9 lines
fs/xfs/libxfs/xfs_format.h +1 -1
index dd764da08f6f..f58f0a44c024 100644
@@ -1061,7 +1061,7 @@ enum xfs_dinode_fmt {
 		sizeof(struct xfs_dinode) : \
 		offsetof(struct xfs_dinode, di_crc))>  #define XFS_LITINO(mp) \
-	((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb))
+	((int)((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb)))

If we do keep the (int) cast here we at least need a comment explaining why it cannot be removed, unless there is a better way to solve the problem.

Quoted 2 lines

It's still weird, because "size of literal inode area" isn't a signed quantity because you can't have a negative size.

I'm fine with either way, since my starting point was to address the regression of e9e2eae89ddb as I mentioned on IRC. And it can also be simply fixed directly.

Thanks,
Gao Xiang

Quoted 50 lines
Quoted 10 lines

I wonder if explicitly making XFS_LITINO() cast to a size_t would be best, and then in xfs_attr_shortform_bytesfit() we just quickly reject the query if the bytes are larger than the literal area:

fs/xfs/libxfs/xfs_attr_leaf.c +0 -0
index bb128db..5588c2e 100644

fs/xfs/libxfs/xfs_attr_leaf.c STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args, · line 543

Quoted 9 lines
fs/xfs/libxfs/xfs_attr_leaf.c +4 -0
@@ -535,6 +535,10 @@ STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
        int                     maxforkoff;
        int                     offset;
 
+       /* Is there no chance we can fit? */
+       if (bytes > XFS_LITINO(mp))
+               return 0;
+
        /* rounded down */
        offset = (XFS_LITINO(mp) - bytes) >> 3;

So if LITINO is 336 and the caller asked for 350 bytes, offset will be negative here. However, offset is the proposed forkoff, right? It doesn't make any sense to have a negative forkoff, so I think Eric's (bytes > LITINO) suggestion above is correct.

This patch was hard to review because the comment for xfs_attr_shortform_bytesfit mentions "...the requested number of additional bytes", but the bytes parameter represents the total number of attr fork bytes we want, not a delta over what we have right now. Can someone please fix that comment too?

--D

Quoted 16 lines

or, maybe simply:

fs/xfs/libxfs/xfs_attr_leaf.c +1 -1
-        offset = (XFS_LITINO(mp) - bytes) >> 3;
+        offset = (int)(XFS_LITINO(mp) - bytes) >> 3;

to be sure that the arithmetic doesn't get promoted to unsigned before the shift?

or maybe others have better ideas.

-Eric

Quoted 3 lines
fs/xfs/libxfs/xfs_format.h +0 -0
 /*
  * Inode data & attribute fork sizes, per inode.
 
author
#5 lore raw

PATCH v2 1/1 xfs: fix forkoff miscalculation related to XFS_LITINO(mp)

In reply to Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

so offset becomes a large positive number on 32-bit platform, and cause xfs_attr_shortform_bytesfit() returns maxforkoff rather than 0.

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));"

assertion failure in xfs_idata_realloc(), which was also the root cause of the original bugreport from Dennis, see:
https://bugzilla.redhat.com/show_bug.cgi?id=1894177

And it can also be manually triggered with the following commands:

  $ touch a;
  $ setfattr -n user.0 -v "`seq 0 80`" a;
  $ setfattr -n user.1 -v "`seq 0 80`" a

on 32-bit platform.

Fix the case in xfs_attr_shortform_bytesfit() by bailing out "XFS_LITINO(mp) < bytes" in advance suggested by Eric and a misleading comment together with this bugfix suggested by Darrick. It seems the other users of XFS_LITINO(mp) are not impacted.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
changes since v1:
- fix 2 typos ">> 8" to ">> 3" mentioned by Eric;
- directly bail out "XFS_LITINO(mp) < bytes" suggested by Eric and Darrick;
- fix a misleading comment together suggested by Darrick;
- since (int) decorator doesn't need to be added, so update the patch subject as well.

 fs/xfs/libxfs/xfs_attr_leaf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr_leaf.c +5 -1
index bb128db220ac..c8d91034850b 100644
@@ -515,7 +515,7 @@ xfs_attr_copy_value(
  *========================================================================*/
 
 /*
- * Query whether the requested number of additional bytes of extended
+ * Query whether the total requested number of attr fork bytes of extended
  * attribute space will be able to fit inline.
  *
  * Returns zero if not, else the di_forkoff fork offset to be used in the
@@ -535,6 +535,10 @@ xfs_attr_shortform_bytesfit(
 	int			maxforkoff;
 	int			offset;
 
+	/* there is no chance we can fit */
+	if (bytes > XFS_LITINO(mp))
+		return 0;
+
 	/* rounded down */
 	offset = (XFS_LITINO(mp) - bytes) >> 3;
 
Signature · 1 line
2.18.4
Tested-by on v2 1/1
#6 lore raw

Re: [PATCH v2] xfs: fix forkoff miscalculation related to XFS_LITINO(mp)

Quoted 85 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

so offset becomes a large positive number on 32-bit platform, and cause xfs_attr_shortform_bytesfit() returns maxforkoff rather than 0.

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));"

assertion failure in xfs_idata_realloc(), which was also the root cause of the original bugreport from Dennis, see:

   https://bugzilla.redhat.com/show_bug.cgi?id=1894177

And it can also be manually triggered with the following commands:

  $ touch a;
  $ setfattr -n user.0 -v "`seq 0 80`" a;
  $ setfattr -n user.1 -v "`seq 0 80`" a

on 32-bit platform.

Fix the case in xfs_attr_shortform_bytesfit() by bailing out "XFS_LITINO(mp) < bytes" in advance suggested by Eric and a misleading comment together with this bugfix suggested by Darrick. It seems the other users of XFS_LITINO(mp) are not impacted.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
changes since v1:
- fix 2 typos ">> 8" to ">> 3" mentioned by Eric;
- directly bail out "XFS_LITINO(mp) < bytes" suggested by Eric and Darrick;
- fix a misleading comment together suggested by Darrick;
- since (int) decorator doesn't need to be added, so update the patch subject as well.

 fs/xfs/libxfs/xfs_attr_leaf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr_leaf.c +5 -2
index bb128db220ac..c8d91034850b 100644
@@ -515,7 +515,7 @@ xfs_attr_copy_value(
  *========================================================================*/
 
 /*
- * Query whether the requested number of additional bytes of extended
+ * Query whether the total requested number of attr fork bytes of extended
  * attribute space will be able to fit inline.
  *
  * Returns zero if not, else the di_forkoff fork offset to be used in the
@@ -535,6 +535,10 @@ xfs_attr_shortform_bytesfit(
        int                     maxforkoff;
        int                     offset;
 
+       /* there is no chance we can fit */
+       if (bytes > XFS_LITINO(mp))
+               return 0;
+
        /* rounded down */
        offset = (XFS_LITINO(mp) - bytes) >> 3;
 
--

2.18.4

Signature · 3 lines
Dennis Gilmore
Multiple Architecture Portfolio Enablement
T: +1-312-660-3523
Reviewed-by on v2 1/1
#7 lore raw

Re: [PATCH v2] xfs: fix forkoff miscalculation related to XFS_LITINO(mp)

In reply to Gao Xiang
Quoted 71 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

so offset becomes a large positive number on 32-bit platform, and cause xfs_attr_shortform_bytesfit() returns maxforkoff rather than 0.

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));"

assertion failure in xfs_idata_realloc(), which was also the root cause of the original bugreport from Dennis, see:

   https://bugzilla.redhat.com/show_bug.cgi?id=1894177

And it can also be manually triggered with the following commands:

  $ touch a;
  $ setfattr -n user.0 -v "`seq 0 80`" a;
  $ setfattr -n user.1 -v "`seq 0 80`" a

on 32-bit platform.

Fix the case in xfs_attr_shortform_bytesfit() by bailing out "XFS_LITINO(mp) < bytes" in advance suggested by Eric and a misleading comment together with this bugfix suggested by Darrick. It seems the other users of XFS_LITINO(mp) are not impacted.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
changes since v1:
- fix 2 typos ">> 8" to ">> 3" mentioned by Eric;
- directly bail out "XFS_LITINO(mp) < bytes" suggested by Eric and Darrick;
- fix a misleading comment together suggested by Darrick;
- since (int) decorator doesn't need to be added, so update the patch subject as well.

 fs/xfs/libxfs/xfs_attr_leaf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr_leaf.c +1 -1
index bb128db220ac..c8d91034850b 100644
@@ -515,7 +515,7 @@ xfs_attr_copy_value(
  *========================================================================*/
 
 /*
- * Query whether the requested number of additional bytes of extended
+ * Query whether the total requested number of attr fork bytes of extended
  * attribute space will be able to fit inline.
  *
  * Returns zero if not, else the di_forkoff fork offset to be used in the

fs/xfs/libxfs/xfs_attr_leaf.c xfs_attr_shortform_bytesfit( · line 538

Quoted 4 lines
fs/xfs/libxfs/xfs_attr_leaf.c +1 -0
@@ -535,6 +535,10 @@ xfs_attr_shortform_bytesfit(
 	int			maxforkoff;
 	int			offset;
 
+	/* there is no chance we can fit */

Maybe:

	/* 
	 * Check if the new size could fit at all first:
	 */

Otherwise looks good:

author
#8 lore raw

Re: [PATCH v2] xfs: fix forkoff miscalculation related to XFS_LITINO(mp)

Quoted 75 lines · Christoph Hellwig
Quoted 74 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

so offset becomes a large positive number on 32-bit platform, and cause xfs_attr_shortform_bytesfit() returns maxforkoff rather than 0.

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));"

assertion failure in xfs_idata_realloc(), which was also the root cause of the original bugreport from Dennis, see:

   https://bugzilla.redhat.com/show_bug.cgi?id=1894177

And it can also be manually triggered with the following commands:

  $ touch a;
  $ setfattr -n user.0 -v "`seq 0 80`" a;
  $ setfattr -n user.1 -v "`seq 0 80`" a

on 32-bit platform.

Fix the case in xfs_attr_shortform_bytesfit() by bailing out "XFS_LITINO(mp) < bytes" in advance suggested by Eric and a misleading comment together with this bugfix suggested by Darrick. It seems the other users of XFS_LITINO(mp) are not impacted.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
changes since v1:
- fix 2 typos ">> 8" to ">> 3" mentioned by Eric;
- directly bail out "XFS_LITINO(mp) < bytes" suggested by Eric and Darrick;
- fix a misleading comment together suggested by Darrick;
- since (int) decorator doesn't need to be added, so update the patch subject as well.

 fs/xfs/libxfs/xfs_attr_leaf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr_leaf.c +1 -1
index bb128db220ac..c8d91034850b 100644
@@ -515,7 +515,7 @@ xfs_attr_copy_value(
  *========================================================================*/
 
 /*
- * Query whether the requested number of additional bytes of extended
+ * Query whether the total requested number of attr fork bytes of extended
  * attribute space will be able to fit inline.
  *
  * Returns zero if not, else the di_forkoff fork offset to be used in the
@@ -535,6 +535,10 @@ xfs_attr_shortform_bytesfit(
 	int			maxforkoff;
 	int			offset;
 
Quoted 7 lines

fs/xfs/libxfs/xfs_attr_leaf.c xfs_attr_shortform_bytesfit( · line 538

Quoted 1 line
fs/xfs/libxfs/xfs_attr_leaf.c +1 -0
+	/* there is no chance we can fit */

Maybe:

	/* 
	 * Check if the new size could fit at all first:
	 */

ok, let me quick revise it as the next version.

Quoted 4 lines

Otherwise looks good:

Thanks!

Thanks,
Gao Xiang

Quoted 1 line
author
#9 lore raw

PATCH v3 1/1 xfs: fix forkoff miscalculation related to XFS_LITINO(mp)

In reply to Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

so offset becomes a large positive number on 32-bit platform, and cause xfs_attr_shortform_bytesfit() returns maxforkoff rather than 0.

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));"

assertion failure in xfs_idata_realloc(), which was also the root cause of the original bugreport from Dennis, see:
https://bugzilla.redhat.com/show_bug.cgi?id=1894177

And it can also be manually triggered with the following commands:

  $ touch a;
  $ setfattr -n user.0 -v "`seq 0 80`" a;
  $ setfattr -n user.1 -v "`seq 0 80`" a

on 32-bit platform.

Fix the case in xfs_attr_shortform_bytesfit() by bailing out "XFS_LITINO(mp) < bytes" in advance suggested by Eric and a misleading comment together with this bugfix suggested by Darrick. It seems the other users of XFS_LITINO(mp) are not impacted.

Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

---
changes since v2:
- collect more tags from the replies of v2;
- refine a comment suggested by Christoph.

 fs/xfs/libxfs/xfs_attr_leaf.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr_leaf.c +7 -1
index bb128db220ac..d6ef69ab1c67 100644
@@ -515,7 +515,7 @@ xfs_attr_copy_value(
  *========================================================================*/
 
 /*
- * Query whether the requested number of additional bytes of extended
+ * Query whether the total requested number of attr fork bytes of extended
  * attribute space will be able to fit inline.
  *
  * Returns zero if not, else the di_forkoff fork offset to be used in the
@@ -535,6 +535,12 @@ xfs_attr_shortform_bytesfit(
 	int			maxforkoff;
 	int			offset;
 
+	/*
+	 * Check if the new size could fit at all first:
+	 */
+	if (bytes > XFS_LITINO(mp))
+		return 0;
+
 	/* rounded down */
 	offset = (XFS_LITINO(mp) - bytes) >> 3;
 
Signature · 1 line
2.18.4
Reviewed-by on v3 1/1
#10 lore raw

Re: [PATCH v3] xfs: fix forkoff miscalculation related to XFS_LITINO(mp)

Quoted 41 lines · Gao Xiang

Currently, commit e9e2eae89ddb dropped a (int) decoration from XFS_LITINO(mp), and since sizeof() expression is also involved, the result of XFS_LITINO(mp) is simply as the size_t type (commonly unsigned long).

Considering the expression in xfs_attr_shortform_bytesfit():
offset = (XFS_LITINO(mp) - bytes) >> 3;
let "bytes" be (int)340, and

    "XFS_LITINO(mp)" be (unsigned long)336.

on 64-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffffffffffcUL >> 3) = -1

but on 32-bit platform, the expression is
offset = ((unsigned long)336 - (int)340) >> 3 =

           (int)(0xfffffffcUL >> 3) = 0x1fffffff

instead.

so offset becomes a large positive number on 32-bit platform, and cause xfs_attr_shortform_bytesfit() returns maxforkoff rather than 0.

Therefore, one result is
"ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));"

assertion failure in xfs_idata_realloc(), which was also the root cause of the original bugreport from Dennis, see:

   https://bugzilla.redhat.com/show_bug.cgi?id=1894177

And it can also be manually triggered with the following commands:

  $ touch a;
  $ setfattr -n user.0 -v "`seq 0 80`" a;
  $ setfattr -n user.1 -v "`seq 0 80`" a

on 32-bit platform.

Fix the case in xfs_attr_shortform_bytesfit() by bailing out "XFS_LITINO(mp) < bytes" in advance suggested by Eric and a misleading comment together with this bugfix suggested by Darrick. It seems the other users of XFS_LITINO(mp) are not impacted.

Quoted 5 lines
Fixes e9e2eae89ddb ("xfs: only check the superblock version for dinode size calculation")
Cc <[email protected]> # 5.7+

Looks good to me,

--D

Quoted 37 lines

---
changes since v2:
- collect more tags from the replies of v2;
- refine a comment suggested by Christoph.

 fs/xfs/libxfs/xfs_attr_leaf.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr_leaf.c +7 -1
index bb128db220ac..d6ef69ab1c67 100644
@@ -515,7 +515,7 @@ xfs_attr_copy_value(
  *========================================================================*/
 
 /*
- * Query whether the requested number of additional bytes of extended
+ * Query whether the total requested number of attr fork bytes of extended
  * attribute space will be able to fit inline.
  *
  * Returns zero if not, else the di_forkoff fork offset to be used in the
@@ -535,6 +535,12 @@ xfs_attr_shortform_bytesfit(
 	int			maxforkoff;
 	int			offset;
 
+	/*
+	 * Check if the new size could fit at all first:
+	 */
+	if (bytes > XFS_LITINO(mp))
+		return 0;
+
 	/* rounded down */
 	offset = (XFS_LITINO(mp) - bytes) >> 3;
 
Signature · 1 line
2.18.4