Skip to content

Commit 9fad9d5

Browse files
JustinStittmartinkpetersen
authored andcommitted
scsi: sr: Fix unintentional arithmetic wraparound
Running syzkaller with the newly reintroduced signed integer overflow sanitizer produces this report: [ 65.194362] ------------[ cut here ]------------ [ 65.197752] UBSAN: signed-integer-overflow in ../drivers/scsi/sr_ioctl.c:436:9 [ 65.203607] -2147483648 * 177 cannot be represented in type 'int' [ 65.207911] CPU: 2 PID: 10416 Comm: syz-executor.1 Not tainted 6.8.0-rc2-00035-gb3ef86b5a957 #1 [ 65.213585] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 65.219923] Call Trace: [ 65.221556] <TASK> [ 65.223029] dump_stack_lvl+0x93/0xd0 [ 65.225573] handle_overflow+0x171/0x1b0 [ 65.228219] sr_select_speed+0xeb/0xf0 [ 65.230786] ? __pm_runtime_resume+0xe6/0x130 [ 65.233606] sr_block_ioctl+0x15d/0x1d0 ... Historically, the signed integer overflow sanitizer did not work in the kernel due to its interaction with `-fwrapv` but this has since been changed [1] in the newest version of Clang. It was re-enabled in the kernel with Commit 557f8c5 ("ubsan: Reintroduce signed overflow sanitizer"). Firstly, let's change the type of "speed" to unsigned long as sr_select_speed()'s only caller passes in an unsigned long anyways. $ git grep '\.select_speed' | drivers/scsi/sr.c: .select_speed = sr_select_speed, ... | static int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi, | unsigned long arg) | { | ... | return cdi->ops->select_speed(cdi, arg); | } Next, let's add an extra check to make sure we don't exceed 0xffff/177 (350) since 0xffff is the max speed. This has two benefits: 1) we deal with integer overflow before it happens and 2) we properly respect the max speed of 0xffff. There are some "magic" numbers here but I did not want to change more than what was necessary. Link: llvm/llvm-project#82432 [1] Closes: KSPP#357 Cc: [email protected] Signed-off-by: Justin Stitt <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 10157b1 commit 9fad9d5

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

Documentation/cdrom/cdrom-standard.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ current *struct* is::
217217
int (*media_changed)(struct cdrom_device_info *, int);
218218
int (*tray_move)(struct cdrom_device_info *, int);
219219
int (*lock_door)(struct cdrom_device_info *, int);
220-
int (*select_speed)(struct cdrom_device_info *, int);
220+
int (*select_speed)(struct cdrom_device_info *, unsigned long);
221221
int (*get_last_session) (struct cdrom_device_info *,
222222
struct cdrom_multisession *);
223223
int (*get_mcn)(struct cdrom_device_info *, struct cdrom_mcn *);
@@ -396,7 +396,7 @@ action need be taken, and the return value should be 0.
396396

397397
::
398398

399-
int select_speed(struct cdrom_device_info *cdi, int speed)
399+
int select_speed(struct cdrom_device_info *cdi, unsigned long speed)
400400

401401
Some CD-ROM drives are capable of changing their head-speed. There
402402
are several reasons for changing the speed of a CD-ROM drive. Badly

drivers/scsi/sr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int sr_disk_status(struct cdrom_device_info *);
6565
int sr_get_last_session(struct cdrom_device_info *, struct cdrom_multisession *);
6666
int sr_get_mcn(struct cdrom_device_info *, struct cdrom_mcn *);
6767
int sr_reset(struct cdrom_device_info *);
68-
int sr_select_speed(struct cdrom_device_info *cdi, int speed);
68+
int sr_select_speed(struct cdrom_device_info *cdi, unsigned long speed);
6969
int sr_audio_ioctl(struct cdrom_device_info *, unsigned int, void *);
7070

7171
int sr_is_xa(Scsi_CD *);

drivers/scsi/sr_ioctl.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,14 @@ int sr_reset(struct cdrom_device_info *cdi)
425425
return 0;
426426
}
427427

428-
int sr_select_speed(struct cdrom_device_info *cdi, int speed)
428+
int sr_select_speed(struct cdrom_device_info *cdi, unsigned long speed)
429429
{
430430
Scsi_CD *cd = cdi->handle;
431431
struct packet_command cgc;
432432

433+
/* avoid exceeding the max speed or overflowing integer bounds */
434+
speed = clamp(0, speed, 0xffff / 177);
435+
433436
if (speed == 0)
434437
speed = 0xffff; /* set to max */
435438
else

include/linux/cdrom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct cdrom_device_ops {
7777
unsigned int clearing, int slot);
7878
int (*tray_move) (struct cdrom_device_info *, int);
7979
int (*lock_door) (struct cdrom_device_info *, int);
80-
int (*select_speed) (struct cdrom_device_info *, int);
80+
int (*select_speed) (struct cdrom_device_info *, unsigned long);
8181
int (*get_last_session) (struct cdrom_device_info *,
8282
struct cdrom_multisession *);
8383
int (*get_mcn) (struct cdrom_device_info *,

0 commit comments

Comments
 (0)