ALSA: pcm: fix undefined behavior in bit shift for SNDRV_PCM_RATE_KNOT
authorBaisong Zhong <zhongbaisong@huawei.com>
Mon, 21 Nov 2022 11:00:44 +0000 (19:00 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 31 Dec 2022 12:32:14 +0000 (13:32 +0100)
[ Upstream commit b5172e62458f8e6ff359e5f096044a488db90ac5 ]

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

UBSAN: shift-out-of-bounds in sound/core/pcm_native.c:2676:21
left shift of 1 by 31 places cannot be represented in type 'int'
...
Call Trace:
 <TASK>
 dump_stack_lvl+0x8d/0xcf
 ubsan_epilogue+0xa/0x44
 __ubsan_handle_shift_out_of_bounds+0x1e7/0x208
 snd_pcm_open_substream+0x9f0/0xa90
 snd_pcm_oss_open.part.26+0x313/0x670
 snd_pcm_oss_open+0x30/0x40
 soundcore_open+0x18b/0x2e0
 chrdev_open+0xe2/0x270
 do_dentry_open+0x2f7/0x620
 path_openat+0xd66/0xe70
 do_filp_open+0xe3/0x170
 do_sys_openat2+0x357/0x4a0
 do_sys_open+0x87/0xd0
 do_syscall_64+0x34/0x80

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Baisong Zhong <zhongbaisong@huawei.com>
Link: https://lore.kernel.org/r/20221121110044.3115686-1-zhongbaisong@huawei.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/sound/pcm.h

index 7b1a022..27040b4 100644 (file)
@@ -106,24 +106,24 @@ struct snd_pcm_ops {
 #define SNDRV_PCM_POS_XRUN             ((snd_pcm_uframes_t)-1)
 
 /* If you change this don't forget to change rates[] table in pcm_native.c */
-#define SNDRV_PCM_RATE_5512            (1<<0)          /* 5512Hz */
-#define SNDRV_PCM_RATE_8000            (1<<1)          /* 8000Hz */
-#define SNDRV_PCM_RATE_11025           (1<<2)          /* 11025Hz */
-#define SNDRV_PCM_RATE_16000           (1<<3)          /* 16000Hz */
-#define SNDRV_PCM_RATE_22050           (1<<4)          /* 22050Hz */
-#define SNDRV_PCM_RATE_32000           (1<<5)          /* 32000Hz */
-#define SNDRV_PCM_RATE_44100           (1<<6)          /* 44100Hz */
-#define SNDRV_PCM_RATE_48000           (1<<7)          /* 48000Hz */
-#define SNDRV_PCM_RATE_64000           (1<<8)          /* 64000Hz */
-#define SNDRV_PCM_RATE_88200           (1<<9)          /* 88200Hz */
-#define SNDRV_PCM_RATE_96000           (1<<10)         /* 96000Hz */
-#define SNDRV_PCM_RATE_176400          (1<<11)         /* 176400Hz */
-#define SNDRV_PCM_RATE_192000          (1<<12)         /* 192000Hz */
-#define SNDRV_PCM_RATE_352800          (1<<13)         /* 352800Hz */
-#define SNDRV_PCM_RATE_384000          (1<<14)         /* 384000Hz */
-
-#define SNDRV_PCM_RATE_CONTINUOUS      (1<<30)         /* continuous range */
-#define SNDRV_PCM_RATE_KNOT            (1<<31)         /* supports more non-continuos rates */
+#define SNDRV_PCM_RATE_5512            (1U<<0)         /* 5512Hz */
+#define SNDRV_PCM_RATE_8000            (1U<<1)         /* 8000Hz */
+#define SNDRV_PCM_RATE_11025           (1U<<2)         /* 11025Hz */
+#define SNDRV_PCM_RATE_16000           (1U<<3)         /* 16000Hz */
+#define SNDRV_PCM_RATE_22050           (1U<<4)         /* 22050Hz */
+#define SNDRV_PCM_RATE_32000           (1U<<5)         /* 32000Hz */
+#define SNDRV_PCM_RATE_44100           (1U<<6)         /* 44100Hz */
+#define SNDRV_PCM_RATE_48000           (1U<<7)         /* 48000Hz */
+#define SNDRV_PCM_RATE_64000           (1U<<8)         /* 64000Hz */
+#define SNDRV_PCM_RATE_88200           (1U<<9)         /* 88200Hz */
+#define SNDRV_PCM_RATE_96000           (1U<<10)        /* 96000Hz */
+#define SNDRV_PCM_RATE_176400          (1U<<11)        /* 176400Hz */
+#define SNDRV_PCM_RATE_192000          (1U<<12)        /* 192000Hz */
+#define SNDRV_PCM_RATE_352800          (1U<<13)        /* 352800Hz */
+#define SNDRV_PCM_RATE_384000          (1U<<14)        /* 384000Hz */
+
+#define SNDRV_PCM_RATE_CONTINUOUS      (1U<<30)        /* continuous range */
+#define SNDRV_PCM_RATE_KNOT            (1U<<31)        /* supports more non-continuos rates */
 
 #define SNDRV_PCM_RATE_8000_44100      (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_11025|\
                                         SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_22050|\