Skip to contentSkip to conversation

vt_ioctl: fix potential spectre v1 in VT_DISALLOCATE

PATCH In review Xiaomeng Tong 2 participants 3 messages Feb 22, 2022 – Feb 23, 2022 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vt_ioctl: fix potential spectre v1 in VT_DISALLOCATE

In VT_ACTIVATE an almost identical code path has been patched with array_index_nospec. In the VT_DISALLOCATE path, the arg is the user input from a system call argument and lately used as a index for vc_cons[index].d access, which can be reached through path like vt_disallocate->vc_busy or vt_disallocate->vc_deallocate. For consistency both code paths should have the same mitigations applied.

---
 drivers/tty/vt/vt_ioctl.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/tty/vt/vt_ioctl.c +6 -3
index 580136986..acd53c1c9 100644
@@ -898,10 +898,13 @@ int vt_ioctl(struct tty_struct *tty,
 		if (arg > MAX_NR_CONSOLES)
 			return -ENXIO;
 
-		if (arg == 0)
+		if (arg == 0) {
 			vt_disallocate_all();
-		else
-			return vt_disallocate(--arg);
+		} else {
+			--arg;
+			arg = array_index_nospec(arg, MAX_NR_CONSOLES);
+			return vt_disallocate(arg);
+		}
 		break;
 
 	case VT_RESIZE:
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 18 lines · Xiaomeng Tong

In VT_ACTIVATE an almost identical code path has been patched with array_index_nospec. In the VT_DISALLOCATE path, the arg is the user input from a system call argument and lately used as a index for vc_cons[index].d access, which can be reached through path like vt_disallocate->vc_busy or vt_disallocate->vc_deallocate. For consistency both code paths should have the same mitigations applied.

---
  drivers/tty/vt/vt_ioctl.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/tty/vt/vt_ioctl.c +0 -0
index 580136986..acd53c1c9 100644

drivers/tty/vt/vt_ioctl.c int vt_ioctl(struct tty_struct *tty, · line 902

Quoted 6 lines
drivers/tty/vt/vt_ioctl.c +1 -1
@@ -898,10 +898,13 @@ int vt_ioctl(struct tty_struct *tty,
  		if (arg > MAX_NR_CONSOLES)
  			return -ENXIO;
 
-		if (arg == 0)
+		if (arg == 0) {
  			vt_disallocate_all();

Could you add a break; here and remove the else branching completely?

drivers/tty/vt/vt_ioctl.c int vt_ioctl(struct tty_struct *tty, · line 905

Quoted 5 lines
drivers/tty/vt/vt_ioctl.c +3 -2
-		else
-			return vt_disallocate(--arg);
+		} else {
+			--arg;
+			arg = array_index_nospec(arg, MAX_NR_CONSOLES);

You could simply do:
arg = array_index_nospec(arg - 1, MAX_NR_CONSOLES);
and remove
--arg;
completely.

drivers/tty/vt/vt_ioctl.c int vt_ioctl(struct tty_struct *tty, · line 908

Quoted 3 lines
drivers/tty/vt/vt_ioctl.c +2 -0
+			return vt_disallocate(arg);
+		}
  		break;

When you do the above, this break is superfluous.

thanks,

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 7 lines · Jiri Slaby

Could you add a break; here and remove the else branching completely?
...
You could simply do:
arg = array_index_nospec(arg - 1, MAX_NR_CONSOLES);
and remove
--arg;
completely.

Already adjusted as suggested in Patch v2, thanks!

Best regards,
--
Xiaomeng Tong