1 From 7c0cca7c847e6e019d67b7d793efbbe3b947d004 Mon Sep 17 00:00:00 2001
2 From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
3 Date: Mon, 21 Jan 2019 17:26:42 +0100
4 Subject: tty: ldisc: add sysctl to prevent autoloading of ldiscs
6 From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
8 commit 7c0cca7c847e6e019d67b7d793efbbe3b947d004 upstream.
10 By default, the kernel will automatically load the module of any line
11 dicipline that is asked for. As this sometimes isn't the safest thing
12 to do, provide a sysctl to disable this feature.
14 By default, we set this to 'y' as that is the historical way that Linux
15 has worked, and we do not want to break working systems. But in the
16 future, perhaps this can default to 'n' to prevent this functionality.
18 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
19 Reviewed-by: Theodore Ts'o <tytso@mit.edu>
20 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
23 drivers/tty/Kconfig | 23 +++++++++++++++++++++++
24 drivers/tty/tty_io.c | 3 +++
25 drivers/tty/tty_ldisc.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
26 3 files changed, 73 insertions(+)
28 --- a/drivers/tty/Kconfig
29 +++ b/drivers/tty/Kconfig
30 @@ -466,4 +466,27 @@ config MIPS_EJTAG_FDC_KGDB_CHAN
32 FDC channel number to use for KGDB.
34 +config LDISC_AUTOLOAD
35 + bool "Automatically load TTY Line Disciplines"
38 + Historically the kernel has always automatically loaded any
39 + line discipline that is in a kernel module when a user asks
40 + for it to be loaded with the TIOCSETD ioctl, or through other
41 + means. This is not always the best thing to do on systems
42 + where you know you will not be using some of the more
43 + "ancient" line disciplines, so prevent the kernel from doing
44 + this unless the request is coming from a process with the
45 + CAP_SYS_MODULE permissions.
47 + Say 'Y' here if you trust your userspace users to do the right
48 + thing, or if you have only provided the line disciplines that
49 + you know you will be using, or if you wish to continue to use
50 + the traditional method of on-demand loading of these modules
53 + This functionality can be changed at runtime with the
54 + dev.tty.ldisc_autoload sysctl, this configuration option will
55 + only set the default value of this functionality.
58 --- a/drivers/tty/tty_io.c
59 +++ b/drivers/tty/tty_io.c
60 @@ -513,6 +513,8 @@ void proc_clear_tty(struct task_struct *
64 +extern void tty_sysctl_init(void);
67 * proc_set_tty - set the controlling terminal
69 @@ -3689,6 +3691,7 @@ void console_sysfs_notify(void)
71 int __init tty_init(void)
74 cdev_init(&tty_cdev, &tty_fops);
75 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
76 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
77 --- a/drivers/tty/tty_ldisc.c
78 +++ b/drivers/tty/tty_ldisc.c
79 @@ -148,6 +148,13 @@ static void put_ldops(struct tty_ldisc_o
80 * takes tty_ldiscs_lock to guard against ldisc races
83 +#if defined(CONFIG_LDISC_AUTOLOAD)
84 + #define INITIAL_AUTOLOAD_STATE 1
86 + #define INITIAL_AUTOLOAD_STATE 0
88 +static int tty_ldisc_autoload = INITIAL_AUTOLOAD_STATE;
90 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
93 @@ -162,6 +169,8 @@ static struct tty_ldisc *tty_ldisc_get(s
95 ldops = get_ldops(disc);
97 + if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
98 + return ERR_PTR(-EPERM);
99 request_module("tty-ldisc-%d", disc);
100 ldops = get_ldops(disc);
102 @@ -830,3 +839,41 @@ void tty_ldisc_begin(void)
103 /* Setup the default TTY line discipline. */
104 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
109 +static struct ctl_table tty_table[] = {
111 + .procname = "ldisc_autoload",
112 + .data = &tty_ldisc_autoload,
113 + .maxlen = sizeof(tty_ldisc_autoload),
115 + .proc_handler = proc_dointvec,
122 +static struct ctl_table tty_dir_table[] = {
126 + .child = tty_table,
131 +static struct ctl_table tty_root_table[] = {
135 + .child = tty_dir_table,
140 +void tty_sysctl_init(void)
142 + register_sysctl_table(tty_root_table);