Skip to contentSkip to conversation

n_gsm: Use array_index_nospec() with index that comes from userspace

PATCH In review Ilpo Järvinen 2 participants 2 messages Apr 11, 2023 – Apr 12, 2023 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 n_gsm: Use array_index_nospec() with index that comes from userspace

dc.channel used for indexing comes directly from copy_from_user(). Use array_index_nospec() to mitigate speculative side-channel.

---
 drivers/tty/n_gsm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/tty/n_gsm.c +8 -5
index b7e1369a035c..21985432794f 100644
@@ -42,6 +42,7 @@
 #include <linux/ctype.h>
 #include <linux/mm.h>
 #include <linux/math.h>
+#include <linux/nospec.h>
 #include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/poll.h>
@@ -3718,8 +3719,8 @@ static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
 	struct gsm_config_ext ce;
 	struct gsm_dlci_config dc;
 	struct gsm_mux *gsm = tty->disc_data;
+	unsigned int base, addr;
 	struct gsm_dlci *dlci;
-	unsigned int base;
 
 	switch (cmd) {
 	case GSMIOC_GETCONF:
@@ -3748,9 +3749,10 @@ static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
 			return -EFAULT;
 		if (dc.channel == 0 || dc.channel >= NUM_DLCI)
 			return -EINVAL;
-		dlci = gsm->dlci[dc.channel];
+		addr = array_index_nospec(dc.channel, NUM_DLCI);
+		dlci = gsm->dlci[addr];
 		if (!dlci) {
-			dlci = gsm_dlci_alloc(gsm, dc.channel);
+			dlci = gsm_dlci_alloc(gsm, addr);
 			if (!dlci)
 				return -ENOMEM;
 		}
@@ -3763,9 +3765,10 @@ static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
 			return -EFAULT;
 		if (dc.channel == 0 || dc.channel >= NUM_DLCI)
 			return -EINVAL;
-		dlci = gsm->dlci[dc.channel];
+		addr = array_index_nospec(dc.channel, NUM_DLCI);
+		dlci = gsm->dlci[addr];
 		if (!dlci) {
-			dlci = gsm_dlci_alloc(gsm, dc.channel);
+			dlci = gsm_dlci_alloc(gsm, addr);
 			if (!dlci)
 				return -ENOMEM;
 		}
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 49 lines

dc.channel used for indexing comes directly from copy_from_user(). Use array_index_nospec() to mitigate speculative side-channel.

---
 drivers/tty/n_gsm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/tty/n_gsm.c +5 -3
index b7e1369a035c..21985432794f 100644
@@ -42,6 +42,7 @@
 #include <linux/ctype.h>
 #include <linux/mm.h>
 #include <linux/math.h>
+#include <linux/nospec.h>
 #include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/poll.h>
@@ -3718,8 +3719,8 @@ static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
 	struct gsm_config_ext ce;
 	struct gsm_dlci_config dc;
 	struct gsm_mux *gsm = tty->disc_data;
+	unsigned int base, addr;
 	struct gsm_dlci *dlci;
-	unsigned int base;
 
 	switch (cmd) {
 	case GSMIOC_GETCONF:
@@ -3748,9 +3749,10 @@ static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
 			return -EFAULT;
 		if (dc.channel == 0 || dc.channel >= NUM_DLCI)
 			return -EINVAL;
-		dlci = gsm->dlci[dc.channel];
+		addr = array_index_nospec(dc.channel, NUM_DLCI);
+		dlci = gsm->dlci[addr];
 		if (!dlci) {
-			dlci = gsm_dlci_alloc(gsm, dc.channel);
+			dlci = gsm_dlci_alloc(gsm, addr);
 			if (!dlci)
 				return -ENOMEM;
 		}
@@ -3763,9 +3765,10 @@ static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
 			return -EFAULT;
 		if (dc.channel == 0 || dc.channel >= NUM_DLCI)

drivers/tty/n_gsm.c static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd, · line 3774

Quoted 10 lines
drivers/tty/n_gsm.c +3 -2
 			return -EINVAL;
-		dlci = gsm->dlci[dc.channel];
+		addr = array_index_nospec(dc.channel, NUM_DLCI);
+		dlci = gsm->dlci[addr];
 		if (!dlci) {
-			dlci = gsm_dlci_alloc(gsm, dc.channel);
+			dlci = gsm_dlci_alloc(gsm, addr);
 			if (!dlci)
 				return -ENOMEM;
 		}