2022-10-29 07:26:26

by Gaosheng Cui

[permalink] [raw]
Subject: [PATCH] fs: fix undefined behavior in bit shift for SB_NOUSER

Shifting signed 32-bit value by 31 bits is undefined, so changing most
significant bit to unsigned. The UBSAN warning calltrace like below:

UBSAN: shift-out-of-bounds in fs/namespace.c:2330:33
left shift of 1 by 31 places cannot be represented in type 'int'
Call Trace:
<TASK>
dump_stack_lvl+0x7d/0xa5
dump_stack+0x15/0x1b
ubsan_epilogue+0xe/0x4e
__ubsan_handle_shift_out_of_bounds+0x1e7/0x20c
graft_tree+0x36/0xf0
do_add_mount+0x98/0x100
path_mount+0xbd6/0xd50
init_mount+0x6a/0xa3
devtmpfs_setup+0x47/0x7e
devtmpfsd+0x1a/0x50
kthread+0x126/0x160
ret_from_fork+0x1f/0x30
</TASK>

Fixes: e462ec50cb5f ("VFS: Differentiate mount flags (MS_*) from internal superblock flags")
Signed-off-by: Gaosheng Cui <[email protected]>
---
include/linux/fs.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 85015e21b755..a68d5310be7b 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1396,7 +1396,7 @@ extern int send_sigurg(struct fown_struct *fown);
#define SB_NOSEC (1<<28)
#define SB_BORN (1<<29)
#define SB_ACTIVE (1<<30)
-#define SB_NOUSER (1<<31)
+#define SB_NOUSER (1U<<31)

/* These flags relate to encoding and casefolding */
#define SB_ENC_STRICT_MODE_FL (1 << 0)
--
2.25.1



2022-10-31 13:27:54

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH] fs: fix undefined behavior in bit shift for SB_NOUSER

On Sat, Oct 29, 2022 at 03:17:45PM +0800, Gaosheng Cui wrote:
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -1396,7 +1396,7 @@ extern int send_sigurg(struct fown_struct *fown);
> #define SB_NOSEC (1<<28)
> #define SB_BORN (1<<29)
> #define SB_ACTIVE (1<<30)
> -#define SB_NOUSER (1<<31)
> +#define SB_NOUSER (1U<<31)

Let's mark all of the flags as unsigned instead of just one so that
we don't mix types. s_flags is already unsigned (although for some
reason long) already.

And while you touch this please add the proper whitespaces around the
shift operator everywhere.

2022-10-31 14:48:32

by Gaosheng Cui

[permalink] [raw]
Subject: Re: [PATCH] fs: fix undefined behavior in bit shift for SB_NOUSER

> Let's mark all of the flags as unsigned instead of just one so that
> we don't mix types. s_flags is already unsigned (although for some
> reason long) already.
>
> And while you touch this please add the proper whitespaces around the
> shift operator everywhere.

Thanks for taking time to review this patch, I have made patch v2 and submit it.

On 2022/10/31 21:15, Christoph Hellwig wrote:
> Let's mark all of the flags as unsigned instead of just one so that
> we don't mix types. s_flags is already unsigned (although for some
> reason long) already.
>
> And while you touch this please add the proper whitespaces around the
> shift operator everywhere.