Skip to contentSkip to conversation

termios: Alpha BOTHER/IBSHIFT, tty_baudrate fix

PATCH In review H. Peter Anvin 2 participants 8 messages Oct 8, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover termios: Alpha BOTHER/IBSHIFT, tty_baudrate fix

From: "H. Peter Anvin (Intel)" <[email protected]>

It turns out that Alpha is the only architecture that never implemented BOTHER and IBSHIFT, which is otherwise ages old. This is one thing that has held up glibc support for this feature (all other architectures have supported these for about a decade, at least before the current 3.2 glibc cutoff.)

Furthermore, in the process of dealing with this, I discovered that the current code in tty_baudrate.c can read past the end of the baud_table[] on Alpha and PowerPC. The second patch in this series fixes that, but it also cleans up the code substantially by auto-generating the table and, since all architectures now have them, removing all conditionals for BOTHER and IBSHIFT existing.

Tagging for stable because these are concrete and immediate problems. I have a much bigger update in process, nearly done, which will clean up a lot of duplicated code and make the uapi headers usable for libc, but that is not critical on the same level.

Tested on x86, compile-tested on Alpha. SPARC, and PowerPC.

v2: the CBAUDEX masking-as-fallback code was wrong, but it could never
be exercised on any architecture anyway (gcc would not even emit it) so just remove it. There are thus no object code changes from v1.

---
 arch/alpha/include/asm/termios.h       |   8 +-
 arch/alpha/include/uapi/asm/ioctls.h   |   5 +
 arch/alpha/include/uapi/asm/termbits.h |  17 +++
 drivers/tty/.gitignore                 |   1 +
 drivers/tty/Makefile                   |  16 +++
 drivers/tty/bmacros.c                  |   2 +
 drivers/tty/tty_baudrate.c             | 182 ++++++++++++---------------------
 7 files changed, 114 insertions(+), 117 deletions(-)
Cc Greg Kroah-Hartman <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Al Viro <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Eugene Syromiatnikov <[email protected]>
Cc Johan Hovold <[email protected]>
Cc Alan Cox <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
#1 lore raw

PATCH v2 1/2 arch/alpha, termios: implement BOTHER, IBSHIFT and termios2

From: "H. Peter Anvin (Intel)" <[email protected]>

Alpha has had c_ispeed and c_ospeed, but still set speeds in c_cflags using arbitrary flags. Because BOTHER is not defined, the general Linux code doesn't allow setting arbitrary baud rates, and because CBAUDEX == 0, we can have an array overrun of the baud_rate[] table in drivers/tty/tty_baudrate.c if (c_cflags & CBAUD) == 037.

Resolve both problems by #defining BOTHER to 037 on Alpha.

However, userspace still needs to know if setting BOTHER is actually safe given legacy kernels (does anyone actually care about that on Alpha anymore?), so enable the TCGETS2/TCSETS*2 ioctls on Alpha, even though they use the same structure. Define struct termios2 just for compatibility; it is the exact same structure as struct termios. In a future patchset, this will be cleaned up so the uapi headers are usable from libc.

Cc Greg Kroah-Hartman <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Al Viro <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Eugene Syromiatnikov <[email protected]>
Cc Johan Hovold <[email protected]>
Cc Alan Cox <[email protected]>
---
 arch/alpha/include/asm/termios.h       |  8 +++++++-
 arch/alpha/include/uapi/asm/ioctls.h   |  5 +++++
 arch/alpha/include/uapi/asm/termbits.h | 17 +++++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
arch/alpha/include/asm/termios.h +7 -1
index 6a8c53dec57e..b7c77bb1bfd2 100644
@@ -73,9 +73,15 @@
 })
 
 #define user_termios_to_kernel_termios(k, u) \
-	copy_from_user(k, u, sizeof(struct termios))
+	copy_from_user(k, u, sizeof(struct termios2))
 
 #define kernel_termios_to_user_termios(u, k) \
+	copy_to_user(u, k, sizeof(struct termios2))
+
+#define user_termios_to_kernel_termios_1(k, u) \
+	copy_from_user(k, u, sizeof(struct termios))
+
+#define kernel_termios_to_user_termios_1(u, k) \
 	copy_to_user(u, k, sizeof(struct termios))
 
 #endif	/* _ALPHA_TERMIOS_H */
arch/alpha/include/uapi/asm/ioctls.h +5 -0
index 3729d92d3fa8..dc8c20ac7191 100644
@@ -32,6 +32,11 @@
 #define TCXONC		_IO('t', 30)
 #define TCFLSH		_IO('t', 31)
 
+#define TCGETS2		_IOR('T', 42, struct termios2)
+#define TCSETS2		_IOW('T', 43, struct termios2)
+#define TCSETSW2	_IOW('T', 44, struct termios2)
+#define TCSETSF2	_IOW('T', 45, struct termios2)
+
 #define TIOCSWINSZ	_IOW('t', 103, struct winsize)
 #define TIOCGWINSZ	_IOR('t', 104, struct winsize)
 #define	TIOCSTART	_IO('t', 110)		/* start output, like ^Q */
arch/alpha/include/uapi/asm/termbits.h +17 -0
index de6c8360fbe3..4575ba34a0ea 100644
@@ -26,6 +26,19 @@ struct termios {
 	speed_t c_ospeed;		/* output speed */
 };
 
+/* Alpha has identical termios and termios2 */
+
+struct termios2 {
+	tcflag_t c_iflag;		/* input mode flags */
+	tcflag_t c_oflag;		/* output mode flags */
+	tcflag_t c_cflag;		/* control mode flags */
+	tcflag_t c_lflag;		/* local mode flags */
+	cc_t c_cc[NCCS];		/* control characters */
+	cc_t c_line;			/* line discipline (== c_cc[19]) */
+	speed_t c_ispeed;		/* input speed */
+	speed_t c_ospeed;		/* output speed */
+};
+
 /* Alpha has matching termios and ktermios */
 
 struct ktermios {
@@ -152,6 +165,7 @@ struct ktermios {
 #define B3000000  00034
 #define B3500000  00035
 #define B4000000  00036
+#define BOTHER    00037
 
 #define CSIZE	00001400
 #define   CS5	00000000
@@ -169,6 +183,9 @@ struct ktermios {
 #define CMSPAR	  010000000000		/* mark or space (stick) parity */
 #define CRTSCTS	  020000000000		/* flow control */
 
+#define CIBAUD	07600000
+#define IBSHIFT	16
+
 /* c_lflag bits */
 #define ISIG	0x00000080
 #define ICANON	0x00000100
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH stable v2 1/2] arch/alpha, termios: implement BOTHER, IBSHIFT and termios2

Quoted 10 lines · H. Peter Anvin

From: "H. Peter Anvin (Intel)" <[email protected]>

Alpha has had c_ispeed and c_ospeed, but still set speeds in c_cflags using arbitrary flags. Because BOTHER is not defined, the general Linux code doesn't allow setting arbitrary baud rates, and because CBAUDEX == 0, we can have an array overrun of the baud_rate[] table in drivers/tty/tty_baudrate.c if (c_cflags & CBAUD) == 037.

Resolve both problems by #defining BOTHER to 037 on Alpha.

Quoted 7 lines

However, userspace still needs to know if setting BOTHER is actually safe given legacy kernels (does anyone actually care about that on Alpha anymore?), so enable the TCGETS2/TCSETS*2 ioctls on Alpha, even though they use the same structure. Define struct termios2 just for compatibility; it is the exact same structure as struct termios. In a future patchset, this will be cleaned up so the uapi headers are usable from libc.

Is this really needed? By defining BOTHER (and IBSHIFT which you forgot to mention here) you are enabling arbitrary rates also through TCSETS on alpha, right?

Johan

#3 lore raw

Re: [PATCH stable v2 1/2] arch/alpha, termios: implement BOTHER, IBSHIFT and termios2

Quoted 19 lines · Johan Hovold
Quoted 10 lines · H. Peter Anvin

From: "H. Peter Anvin (Intel)" <[email protected]>

Alpha has had c_ispeed and c_ospeed, but still set speeds in c_cflags using arbitrary flags. Because BOTHER is not defined, the general Linux code doesn't allow setting arbitrary baud rates, and because CBAUDEX == 0, we can have an array overrun of the baud_rate[] table in drivers/tty/tty_baudrate.c if (c_cflags & CBAUD) == 037.

Resolve both problems by #defining BOTHER to 037 on Alpha.

Quoted 7 lines

However, userspace still needs to know if setting BOTHER is actually safe given legacy kernels (does anyone actually care about that on Alpha anymore?), so enable the TCGETS2/TCSETS*2 ioctls on Alpha, even though they use the same structure. Define struct termios2 just for compatibility; it is the exact same structure as struct termios. In a future patchset, this will be cleaned up so the uapi headers are usable from libc.

Quoted 4 lines

Is this really needed? By defining BOTHER (and IBSHIFT which you forgot to mention here) you are enabling arbitrary rates also through TCSETS on alpha, right?

Yes, it's needed, not because the old ioctls won't work on NEW kernels, but because Alpha is so far behind the times, *and* the OLD kernels are severely broken if we pass BOTHER to them, we need a new ioctl number so we can guarantee that we won't do anything that user space doesn't intend; this is actually made far worse because if I read the code correctly, the kernel will still report back BOTHER and the speed field set on a legacy kernel in response to TCGETS, but the values will be completely bogus.

This means that glibc will need a workaround for Alpha only, and the new ioctl numbers handles support for it. gcc should be able to fold the code together, since it should be able to detect that multiple branches of execution are otherwise identical.

To micro-optimize, we could define TERMIOS_OLD as (CBAUDEX ? 8 : 0) in a future (non-stable) patch.

We don't need to worry about it on PowerPC because PowerPC implemented this so long ago, before the current glibc support threshold.

-hpa

#4 lore raw

PATCH v2 2/2 termios, tty/tty_baudrate.c: simplify, auto-generate baud table

In reply to H. Peter Anvin

From: "H. Peter Anvin (Intel)" <[email protected]>

Now when all architectures define BOTHER and IBSHIFT, we can unconditionally rely on these constants. Furthermore, the code can be significantly simplified in a number of places.

Rather than having two tables and needing to be able to keep them in sync at all times, have one auto-generated table. This also lets us avoid the fact that architectures that have CBAUDEX == 0 have BOTHER in a different location that those that don't.

The code for masking CBAUDEX as a fallback is never exercised on any architecture, because for all architectures, either the baud rate table is completely defined for all CBAUD values, or CBAUDEX == 0, so we can just remove it.

Finally, this patch avoids overrunning the baud_table[] for architectures with CBAUDEX == 0.

Cc Greg Kroah-Hartman <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Al Viro <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Eugene Syromiatnikov <[email protected]>
Cc Johan Hovold <[email protected]>
Cc Alan Cox <[email protected]>
---
 drivers/tty/.gitignore     |   1 +
 drivers/tty/Makefile       |  16 ++++
 drivers/tty/bmacros.c      |   2 +
 drivers/tty/tty_baudrate.c | 182 ++++++++++++++-----------------------
 4 files changed, 85 insertions(+), 116 deletions(-)
 create mode 100644 drivers/tty/.gitignore
 create mode 100644 drivers/tty/bmacros.c
drivers/tty/.gitignore new file +1 -0
new file mode 100644
index 000000000000..d436c18a912c
@@ -0,0 +1 @@
+bmacros.h
drivers/tty/Makefile +16 -0
index c72cafdf32b4..489b222ab1ce 100644
@@ -35,3 +35,19 @@ obj-$(CONFIG_MIPS_EJTAG_FDC_TTY) += mips_ejtag_fdc.o
 obj-$(CONFIG_VCC)		+= vcc.o
 
 obj-y += ipwireless/
+
+#
+# Rules for generating the baud rate table
+#
+CFLAGS_bmacros.o += -Wp,-dM
+
+$(obj)/tty_baudrate.o: $(obj)/bmacros.h
+
+quiet_cmd_bmacros = BMACROS $@
+      cmd_bmacros =  ( \
+	sed -nr -e 's/^.define B([0-9]+) .*$$/BTBL(B\1,\1)/p' $< \
+		| sort -n -k1.7 > $@ ) || (rm -f $@ ; echo false)
+
+targets += bmacros.h
+$(obj)/bmacros.h: $(obj)/bmacros.i FORCE
+	$(call if_changed,bmacros)
drivers/tty/bmacros.c new file +2 -0
new file mode 100644
index 000000000000..0af865ff00de
@@ -0,0 +1,2 @@
+/* This file is used to extract the B... macros from header files */
+#include <linux/termios.h>
drivers/tty/tty_baudrate.c +66 -116

242 lines not on this page · raw message

Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH stable v2 2/2] termios, tty/tty_baudrate.c: simplify, auto-generate baud table

Quoted 11 lines · H. Peter Anvin

From: "H. Peter Anvin (Intel)" <[email protected]>

Now when all architectures define BOTHER and IBSHIFT, we can unconditionally rely on these constants. Furthermore, the code can be significantly simplified in a number of places.

Rather than having two tables and needing to be able to keep them in sync at all times, have one auto-generated table. This also lets us avoid the fact that architectures that have CBAUDEX == 0 have BOTHER in a different location that those that don't.

Quoted 7 lines

The code for masking CBAUDEX as a fallback is never exercised on any architecture, because for all architectures, either the baud rate table is completely defined for all CBAUD values, or CBAUDEX == 0, so we can just remove it.

Finally, this patch avoids overrunning the baud_table[] for architectures with CBAUDEX == 0.

So we need a minimal fix for this only as this patch in particular should not be backported to stable.

I'm not sure when I'll have time to review this one thoroughly, so perhaps others can chime in meanwhile.

Johan

#6 lore raw

Re: [PATCH stable v2 2/2] termios, tty/tty_baudrate.c: simplify, auto-generate baud table

Quoted 9 lines · Johan Hovold

So we need a minimal fix for this only as this patch in particular should not be backported to stable.

I'm not sure when I'll have time to review this one thoroughly, so perhaps others can chime in meanwhile.

Johan

OK. In the past Greg has generally liked to avoid fixes which will diverge from upstream (because code in stable which is not in upstream can make debugging difficult), but this is the minimal patch as requested; which to apply is up to Greg.

As far as reviewing the cleanup patch, I strongly recommend:

a) Looking at the resulting file, not at the patch. Most of the code is simply merging the input and output rate functions into a common help function, and restructuring the code to that the utterly bizarre coding of a for loop using a do { ... } while() loop with the initial condition set at variable declaration(!!) far from the loop itself.
b) Examine bmacros.h after a build.
c) Build drivers/tty/tty_baudrate.s. You can directly examine the baud_table and verify that it is, indeed, correct for whatever architecture you build.

-hpa

From c8195635f63508f4c75ef553b4703c4cc3c750e2 Mon Sep 17 00:00:00 2001
From: "H. Peter Anvin" <[email protected]>
Date: Mon, 8 Oct 2018 09:32:00 -0700
Subject: [PATCH 02/16] termios, tty/tty_baudrate.c: fix buffer overrun

On architectures with CBAUDEX == 0 (Alpha and PowerPC), the code in tty_baudrate.c does not do any limit checking on the tty_baudrate[] array, and in fact a buffer overrun is possible on both architectures. Add a limit check to prevent that situation.

This will be followed by a much bigger cleanup/simplification patch.

Cc Greg Kroah-Hartman <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Al Viro <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Philippe Ombredanne <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Eugene Syromiatnikov <[email protected]>
Cc Alan Cox <[email protected]>
---
 drivers/tty/tty_baudrate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_baudrate.c +2 -2
index 7576ceace571..f438eaa68246 100644
@@ -77,7 +77,7 @@ speed_t tty_termios_baud_rate(struct ktermios *termios)
 		else
 			cbaud += 15;
 	}
-	return baud_table[cbaud];
+	return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
 }
 EXPORT_SYMBOL(tty_termios_baud_rate);
 
@@ -113,7 +113,7 @@ speed_t tty_termios_input_baud_rate(struct ktermios *termios)
 		else
 			cbaud += 15;
 	}
-	return baud_table[cbaud];
+	return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
 #else	/* IBSHIFT */
 	return tty_termios_baud_rate(termios);
 #endif	/* IBSHIFT */
Signature · 1 line
2.17.1
#7 lore raw
In reply to H. Peter Anvin
Quoted 15 lines · H. Peter Anvin

From: "H. Peter Anvin (Intel)" <[email protected]>

It turns out that Alpha is the only architecture that never implemented BOTHER and IBSHIFT, which is otherwise ages old. This is one thing that has held up glibc support for this feature (all other architectures have supported these for about a decade, at least before the current 3.2 glibc cutoff.)

Furthermore, in the process of dealing with this, I discovered that the current code in tty_baudrate.c can read past the end of the baud_table[] on Alpha and PowerPC. The second patch in this series fixes that, but it also cleans up the code substantially by auto-generating the table and, since all architectures now have them, removing all conditionals for BOTHER and IBSHIFT existing.

Quoted 2 lines

Tagging for stable because these are concrete and immediate problems.

This isn't stable material in its current form. If you want to plug the alpha and powerpc info leaks in the stable trees, then you need a minimal fix for that, which you can then your clean ups and new features on.

Johan