public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cdrom: Avoid barrier_nospec() in cdrom_ioctl_media_changed()
@ 2024-10-17 22:09 Josh Poimboeuf
  2024-10-18  0:33 ` Jens Axboe
  2024-10-18  1:47 ` Jens Axboe
  0 siblings, 2 replies; 6+ messages in thread
From: Josh Poimboeuf @ 2024-10-17 22:09 UTC (permalink / raw)
  To: Phillip Potter; +Cc: linux-kernel, Jordy Zomer, Jens Axboe

The barrier_nospec() after the array bounds check is overkill and
painfully slow for arches which implement it.

Furthermore, most arches don't implement it, so they remain exposed to
Spectre v1 (which can affect pretty much any CPU with branch
prediction).

Instead, clamp the user pointer to a valid range so it's guaranteed to
be a valid array index even when the bounds check mispredicts.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 drivers/cdrom/cdrom.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cdrom/cdrom.c b/drivers/cdrom/cdrom.c
index 9b0f37d4b9d4..6a99a459b80b 100644
--- a/drivers/cdrom/cdrom.c
+++ b/drivers/cdrom/cdrom.c
@@ -2313,7 +2313,7 @@ static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
 		return -EINVAL;
 
 	/* Prevent arg from speculatively bypassing the length check */
-	barrier_nospec();
+	arg = array_index_nospec(arg, cdi->capacity);
 
 	info = kmalloc(sizeof(*info), GFP_KERNEL);
 	if (!info)
-- 
2.47.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-10-18 10:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-17 22:09 [PATCH] cdrom: Avoid barrier_nospec() in cdrom_ioctl_media_changed() Josh Poimboeuf
2024-10-18  0:33 ` Jens Axboe
2024-10-18  0:52   ` Josh Poimboeuf
2024-10-18  1:10     ` Jens Axboe
2024-10-18  1:47 ` Jens Axboe
2024-10-18 10:31   ` Phillip Potter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox