Re: [PATCH v2] tty: n_gsm: restrict tty devices to attach

From: Linus Torvalds
Date: Sat Apr 20 2024 - 13:34:48 EST


On Sat, 20 Apr 2024 at 04:12, Tetsuo Handa
<penguin-kernel@xxxxxxxxxxxxxxxxxxx> wrote:
>
> Since n_gsm is designed to be used for serial port [1], reject attaching to
> virtual consoles and PTY devices, by checking tty's device major/minor
> numbers at gsmld_open().

If we really just want to restrict it to serial devices, then do
something like, this:

drivers/tty/n_gsm.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 4036566febcb..24425ef35b2b 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -3629,6 +3629,8 @@ static int gsmld_open(struct tty_struct *tty)

if (tty->ops->write == NULL)
return -EINVAL;
+ if (tty->ops->set_serial == NULL)
+ return -EINVAL;

/* Attach our ldisc data */
gsm = gsm_alloc_mux();

which at least matches the current (largely useless) pattern of
checking for a write function.

I think all real serial sub-drivers already have that 'set_serial()'
function, and if there are some that don't, we could just add a dummy
for them. No?

Alternatively, we could go the opposite way, and have some flag in the
line discipline that says "I can be a console", and just check that in
tty_set_ldisc() for the console.

That would probably be a good idea regardless, but likely requires more effort.

But this kind of random major number testing seems wrong. It's trying
to deal with the _symptoms_, not some deeper truth.

Linus