Skip to content

Commit 750afe7

Browse files
suryasaimadhutorvalds
authored andcommitted
printk: add kernel parameter to control writes to /dev/kmsg
Add a "printk.devkmsg" kernel command line parameter which controls how userspace writes into /dev/kmsg. It has three options: * ratelimit - ratelimit logging from userspace. * on - unlimited logging from userspace * off - logging from userspace gets ignored The default setting is to ratelimit the messages written to it. This changes the kernel default setting of "on" to "ratelimit" and we do that because we want to keep userspace spamming /dev/kmsg to sane levels. This is especially moot when a small kernel log buffer wraps around and messages get lost. So the ratelimiting setting should be a sane setting where kernel messages should have a bit higher chance of survival from all the spamming. It additionally does not limit logging to /dev/kmsg while the system is booting if we haven't disabled it on the command line. Furthermore, we can control the logging from a lower priority sysctl interface - kernel.printk_devkmsg. That interface will succeed only if printk.devkmsg *hasn't* been supplied on the command line. If it has, then printk.devkmsg is a one-time setting which remains for the duration of the system lifetime. This "locking" of the setting is to prevent userspace from changing the logging on us through sysctl(2). This patch is based on previous patches from Linus and Steven. [bp@suse.de: fixes] Link: http://lkml.kernel.org/r/20160719072344.GC25563@nazgul.tnic Link: http://lkml.kernel.org/r/20160716061745.15795-3-bp@alien8.de Signed-off-by: Borislav Petkov <bp@suse.de> Cc: Dave Young <dyoung@redhat.com> Cc: Franck Bui <fbui@suse.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Ingo Molnar <mingo@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6b1d174 commit 750afe7

5 files changed

Lines changed: 171 additions & 8 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,6 +3173,13 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
31733173
Format: <bool> (1/Y/y=enable, 0/N/n=disable)
31743174
default: disabled
31753175

3176+
printk.devkmsg={on,off,ratelimit}
3177+
Control writing to /dev/kmsg.
3178+
on - unlimited logging to /dev/kmsg from userspace
3179+
off - logging to /dev/kmsg disabled
3180+
ratelimit - ratelimit the logging
3181+
Default: ratelimit
3182+
31763183
printk.time= Show timing data prefixed to each printk message line
31773184
Format: <bool> (1/Y/y=enable, 0/N/n=disable)
31783185

‎Documentation/sysctl/kernel.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,20 @@ send before ratelimiting kicks in.
764764

765765
==============================================================
766766

767+
printk_devkmsg:
768+
769+
Control the logging to /dev/kmsg from userspace:
770+
771+
ratelimit: default, ratelimited
772+
on: unlimited logging to /dev/kmsg from userspace
773+
off: logging to /dev/kmsg disabled
774+
775+
The kernel command line parameter printk.devkmsg= overrides this and is
776+
a one-time setting until next reboot: once set, it cannot be changed by
777+
this sysctl interface anymore.
778+
779+
==============================================================
780+
767781
randomize_va_space:
768782

769783
This option can be used to select the type of process address

‎include/linux/printk.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ static inline void console_verbose(void)
6161
console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
6262
}
6363

64+
/* strlen("ratelimit") + 1 */
65+
#define DEVKMSG_STR_MAX_SIZE 10
66+
extern char devkmsg_log_str[];
67+
struct ctl_table;
68+
6469
struct va_format {
6570
const char *fmt;
6671
va_list *va;
@@ -175,6 +180,10 @@ extern int printk_delay_msec;
175180
extern int dmesg_restrict;
176181
extern int kptr_restrict;
177182

183+
extern int
184+
devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void __user *buf,
185+
size_t *lenp, loff_t *ppos);
186+
178187
extern void wake_up_klogd(void);
179188

180189
char *log_buf_addr_get(void);

‎kernel/printk/printk.c‎

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,111 @@ static struct lockdep_map console_lock_dep_map = {
8585
};
8686
#endif
8787

88+
enum devkmsg_log_bits {
89+
__DEVKMSG_LOG_BIT_ON = 0,
90+
__DEVKMSG_LOG_BIT_OFF,
91+
__DEVKMSG_LOG_BIT_LOCK,
92+
};
93+
94+
enum devkmsg_log_masks {
95+
DEVKMSG_LOG_MASK_ON = BIT(__DEVKMSG_LOG_BIT_ON),
96+
DEVKMSG_LOG_MASK_OFF = BIT(__DEVKMSG_LOG_BIT_OFF),
97+
DEVKMSG_LOG_MASK_LOCK = BIT(__DEVKMSG_LOG_BIT_LOCK),
98+
};
99+
100+
/* Keep both the 'on' and 'off' bits clear, i.e. ratelimit by default: */
101+
#define DEVKMSG_LOG_MASK_DEFAULT 0
102+
103+
static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
104+
105+
static int __control_devkmsg(char *str)
106+
{
107+
if (!str)
108+
return -EINVAL;
109+
110+
if (!strncmp(str, "on", 2)) {
111+
devkmsg_log = DEVKMSG_LOG_MASK_ON;
112+
return 2;
113+
} else if (!strncmp(str, "off", 3)) {
114+
devkmsg_log = DEVKMSG_LOG_MASK_OFF;
115+
return 3;
116+
} else if (!strncmp(str, "ratelimit", 9)) {
117+
devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
118+
return 9;
119+
}
120+
return -EINVAL;
121+
}
122+
123+
static int __init control_devkmsg(char *str)
124+
{
125+
if (__control_devkmsg(str) < 0)
126+
return 1;
127+
128+
/*
129+
* Set sysctl string accordingly:
130+
*/
131+
if (devkmsg_log == DEVKMSG_LOG_MASK_ON) {
132+
memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
133+
strncpy(devkmsg_log_str, "on", 2);
134+
} else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF) {
135+
memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
136+
strncpy(devkmsg_log_str, "off", 3);
137+
}
138+
/* else "ratelimit" which is set by default. */
139+
140+
/*
141+
* Sysctl cannot change it anymore. The kernel command line setting of
142+
* this parameter is to force the setting to be permanent throughout the
143+
* runtime of the system. This is a precation measure against userspace
144+
* trying to be a smarta** and attempting to change it up on us.
145+
*/
146+
devkmsg_log |= DEVKMSG_LOG_MASK_LOCK;
147+
148+
return 0;
149+
}
150+
__setup("printk.devkmsg=", control_devkmsg);
151+
152+
char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit";
153+
154+
int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
155+
void __user *buffer, size_t *lenp, loff_t *ppos)
156+
{
157+
char old_str[DEVKMSG_STR_MAX_SIZE];
158+
unsigned int old;
159+
int err;
160+
161+
if (write) {
162+
if (devkmsg_log & DEVKMSG_LOG_MASK_LOCK)
163+
return -EINVAL;
164+
165+
old = devkmsg_log;
166+
strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE);
167+
}
168+
169+
err = proc_dostring(table, write, buffer, lenp, ppos);
170+
if (err)
171+
return err;
172+
173+
if (write) {
174+
err = __control_devkmsg(devkmsg_log_str);
175+
176+
/*
177+
* Do not accept an unknown string OR a known string with
178+
* trailing crap...
179+
*/
180+
if (err < 0 || (err + 1 != *lenp)) {
181+
182+
/* ... and restore old setting. */
183+
devkmsg_log = old;
184+
strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE);
185+
186+
return -EINVAL;
187+
}
188+
}
189+
190+
return 0;
191+
}
192+
88193
/*
89194
* Number of registered extended console drivers.
90195
*
@@ -613,6 +718,7 @@ struct devkmsg_user {
613718
u64 seq;
614719
u32 idx;
615720
enum log_flags prev;
721+
struct ratelimit_state rs;
616722
struct mutex lock;
617723
char buf[CONSOLE_EXT_LOG_MAX];
618724
};
@@ -622,11 +728,24 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
622728
char *buf, *line;
623729
int level = default_message_loglevel;
624730
int facility = 1; /* LOG_USER */
731+
struct file *file = iocb->ki_filp;
732+
struct devkmsg_user *user = file->private_data;
625733
size_t len = iov_iter_count(from);
626734
ssize_t ret = len;
627735

628-
if (len > LOG_LINE_MAX)
736+
if (!user || len > LOG_LINE_MAX)
629737
return -EINVAL;
738+
739+
/* Ignore when user logging is disabled. */
740+
if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
741+
return len;
742+
743+
/* Ratelimit when not explicitly enabled. */
744+
if (!(devkmsg_log & DEVKMSG_LOG_MASK_ON)) {
745+
if (!___ratelimit(&user->rs, current->comm))
746+
return ret;
747+
}
748+
630749
buf = kmalloc(len+1, GFP_KERNEL);
631750
if (buf == NULL)
632751
return -ENOMEM;
@@ -799,19 +918,24 @@ static int devkmsg_open(struct inode *inode, struct file *file)
799918
struct devkmsg_user *user;
800919
int err;
801920

802-
/* write-only does not need any file context */
803-
if ((file->f_flags & O_ACCMODE) == O_WRONLY)
804-
return 0;
921+
if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
922+
return -EPERM;
805923

806-
err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
807-
SYSLOG_FROM_READER);
808-
if (err)
809-
return err;
924+
/* write-only does not need any file context */
925+
if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
926+
err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
927+
SYSLOG_FROM_READER);
928+
if (err)
929+
return err;
930+
}
810931

811932
user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
812933
if (!user)
813934
return -ENOMEM;
814935

936+
ratelimit_default_init(&user->rs);
937+
ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE);
938+
815939
mutex_init(&user->lock);
816940

817941
raw_spin_lock_irq(&logbuf_lock);
@@ -830,6 +954,8 @@ static int devkmsg_release(struct inode *inode, struct file *file)
830954
if (!user)
831955
return 0;
832956

957+
ratelimit_state_exit(&user->rs);
958+
833959
mutex_destroy(&user->lock);
834960
kfree(user);
835961
return 0;

‎kernel/sysctl.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,13 @@ static struct ctl_table kern_table[] = {
813813
.extra1 = &zero,
814814
.extra2 = &ten_thousand,
815815
},
816+
{
817+
.procname = "printk_devkmsg",
818+
.data = devkmsg_log_str,
819+
.maxlen = DEVKMSG_STR_MAX_SIZE,
820+
.mode = 0644,
821+
.proc_handler = devkmsg_sysctl_set_loglvl,
822+
},
816823
{
817824
.procname = "dmesg_restrict",
818825
.data = &dmesg_restrict,

0 commit comments

Comments
 (0)