Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1421740 > unrolled thread
| Started by | Borislav Petkov <bp@alien8.de> |
|---|---|
| First post | 2016-06-14 12:20 +0200 |
| Last post | 2016-06-16 03:50 +0200 |
| Articles | 8 — 4 participants |
Back to article view | Back to linux.kernel
[PATCH 0/2] printk.kmsg: Ratelimit it by default Borislav Petkov <bp@alien8.de> - 2016-06-14 12:20 +0200
[PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg Borislav Petkov <bp@alien8.de> - 2016-06-14 12:20 +0200
Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg Ingo Molnar <mingo@kernel.org> - 2016-06-14 12:30 +0200
Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg Steven Rostedt <rostedt@goodmis.org> - 2016-06-14 20:20 +0200
Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg Borislav Petkov <bp@alien8.de> - 2016-06-14 20:40 +0200
Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg Linus Torvalds <torvalds@linux-foundation.org> - 2016-06-16 03:50 +0200
Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg Borislav Petkov <bp@alien8.de> - 2016-06-16 12:00 +0200
Re: [PATCH 0/2] printk.kmsg: Ratelimit it by default Linus Torvalds <torvalds@linux-foundation.org> - 2016-06-16 03:50 +0200
| From | Borislav Petkov <bp@alien8.de> |
|---|---|
| Date | 2016-06-14 12:20 +0200 |
| Subject | [PATCH 0/2] printk.kmsg: Ratelimit it by default |
| Message-ID | <rJTBT-3uB-7@gated-at.bofh.it> |
From: Borislav Petkov <bp@suse.de> Ok, so how about these two? Rostedt is busy so I took Linus' old patch and Steven's last v2 and split and extended them with the comments people had on the last thread: https://lkml.kernel.org/r/20160425145606.598329f2@gandalf.local.home I hope, at least. So it is ratelimiting by default, with "on" and "off" cmdline options. I called the option somewhat a bit shorter too: "printk.kmsg" The current use cases of this and of which I'm aware are: * debug the kernel and thus shut up all interfering input from userspace, i.e. boot with "printk.kmsg=off" * debug userspace (and by that I mean systemd) by booting with "printk.kmsg=on" so that the ratelimiting is disabled and the kernel log gets all the spew. Thoughts? Thanks. Borislav Petkov (2): ratelimit: Extend to print suppressed messages on release printk: Add kernel parameter to control writes to /dev/kmsg Documentation/kernel-parameters.txt | 6 ++++ include/linux/ratelimit.h | 36 +++++++++++++++++++---- kernel/printk/printk.c | 57 +++++++++++++++++++++++++++++++------ lib/ratelimit.c | 6 ++-- 4 files changed, 90 insertions(+), 15 deletions(-) -- 2.7.3
[toc] | [next] | [standalone]
| From | Borislav Petkov <bp@alien8.de> |
|---|---|
| Date | 2016-06-14 12:20 +0200 |
| Subject | [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg |
| Message-ID | <rJTBT-3uB-17@gated-at.bofh.it> |
| In reply to | #1421740 |
From: Borislav Petkov <bp@suse.de>
Add a "printk.kmsg" kernel command line parameter which controls how
userspace writes into /dev/kmsg. It has two options:
* on - unlimited logging from userspace
* off - logging from userspace gets ignored
The default setting is to ratelimit the messages written to it.
It additionally does not limit logging to /dev/kmsg while the system is
booting if we haven't disabled it on the command line.
This patch is based on previous patches from Linus and Steven.
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
---
Documentation/kernel-parameters.txt | 6 ++++
kernel/printk/printk.c | 57 +++++++++++++++++++++++++++++++------
2 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
index 82b42c958d1c..4799c88b7258 100644
--- a/Documentation/kernel-parameters.txt
+++ b/Documentation/kernel-parameters.txt
@@ -3150,6 +3150,12 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
Format: <bool> (1/Y/y=enable, 0/N/n=disable)
default: disabled
+ printk.kmsg={on,off}
+ Control writing to /dev/kmsg.
+ on - unlimited logging to /dev/kmsg from userspace
+ off - logging to /dev/kmsg disabled
+ Default: ratelimited logging.
+
printk.time= Show timing data prefixed to each printk message line
Format: <bool> (1/Y/y=enable, 0/N/n=disable)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 60cdf6386763..33701a166f26 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -86,6 +86,29 @@ static struct lockdep_map console_lock_dep_map = {
};
#endif
+#define DEVKMSG_LOG_RATELIMIT 0
+#define DEVKMSG_LOG_ON 1
+#define DEVKMSG_LOG_OFF 2
+
+/* DEVKMSG_LOG_RATELIMIT by default */
+static unsigned int __read_mostly devkmsg_log;
+static int __init control_devkmsg(char *str)
+{
+ if (!str)
+ return -EINVAL;
+
+ if (!strncmp(str, "on", 2))
+ devkmsg_log = DEVKMSG_LOG_ON;
+ else if (!strncmp(str, "off", 3))
+ devkmsg_log = DEVKMSG_LOG_OFF;
+ else
+ return -EINVAL;
+
+ return 0;
+}
+__setup("printk.kmsg=", control_devkmsg);
+
+
/*
* Number of registered extended console drivers.
*
@@ -614,6 +637,7 @@ struct devkmsg_user {
u64 seq;
u32 idx;
enum log_flags prev;
+ struct ratelimit_state rs;
struct mutex lock;
char buf[CONSOLE_EXT_LOG_MAX];
};
@@ -623,11 +647,24 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
char *buf, *line;
int level = default_message_loglevel;
int facility = 1; /* LOG_USER */
+ struct file *file = iocb->ki_filp;
+ struct devkmsg_user *user = file->private_data;
size_t len = iov_iter_count(from);
ssize_t ret = len;
- if (len > LOG_LINE_MAX)
+ if (!user || len > LOG_LINE_MAX)
return -EINVAL;
+
+ /* Ignore when user logging is disabled. */
+ if (devkmsg_log == DEVKMSG_LOG_OFF)
+ return len;
+
+ /* Ratelimit when not explicitly enabled or when we're not booting. */
+ if ((system_state != SYSTEM_BOOTING) && (devkmsg_log != DEVKMSG_LOG_ON)) {
+ if (!___ratelimit(&user->rs, current->comm))
+ return ret;
+ }
+
buf = kmalloc(len+1, GFP_KERNEL);
if (buf == NULL)
return -ENOMEM;
@@ -801,18 +838,20 @@ static int devkmsg_open(struct inode *inode, struct file *file)
int err;
/* write-only does not need any file context */
- if ((file->f_flags & O_ACCMODE) == O_WRONLY)
- return 0;
-
- err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
- SYSLOG_FROM_READER);
- if (err)
- return err;
+ if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
+ err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
+ SYSLOG_FROM_READER);
+ if (err)
+ return err;
+ }
user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
if (!user)
return -ENOMEM;
+ ratelimit_default_init(&user->rs);
+ ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE);
+
mutex_init(&user->lock);
raw_spin_lock_irq(&logbuf_lock);
@@ -831,6 +870,8 @@ static int devkmsg_release(struct inode *inode, struct file *file)
if (!user)
return 0;
+ ratelimit_state_exit(&user->rs);
+
mutex_destroy(&user->lock);
kfree(user);
return 0;
--
2.7.3
[toc] | [prev] | [next] | [standalone]
| From | Ingo Molnar <mingo@kernel.org> |
|---|---|
| Date | 2016-06-14 12:30 +0200 |
| Subject | Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg |
| Message-ID | <rJTLz-3z3-7@gated-at.bofh.it> |
| In reply to | #1421742 |
* Borislav Petkov <bp@alien8.de> wrote: > From: Borislav Petkov <bp@suse.de> > > Add a "printk.kmsg" kernel command line parameter which controls how > userspace writes into /dev/kmsg. It has two options: > > * on - unlimited logging from userspace > * off - logging from userspace gets ignored Please also add a sysctl! I *so* want to enable this on distro kernels without having to reboot the kernel... Thanks, Ingo
[toc] | [prev] | [next] | [standalone]
| From | Steven Rostedt <rostedt@goodmis.org> |
|---|---|
| Date | 2016-06-14 20:20 +0200 |
| Subject | Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg |
| Message-ID | <rK16q-8sH-9@gated-at.bofh.it> |
| In reply to | #1421751 |
On Tue, 14 Jun 2016 12:21:35 +0200 Ingo Molnar <mingo@kernel.org> wrote: > * Borislav Petkov <bp@alien8.de> wrote: > > > From: Borislav Petkov <bp@suse.de> > > > > Add a "printk.kmsg" kernel command line parameter which controls how > > userspace writes into /dev/kmsg. It has two options: > > > > * on - unlimited logging from userspace > > * off - logging from userspace gets ignored > > Please also add a sysctl! > > I *so* want to enable this on distro kernels without having to reboot the > kernel... > But can we make it a one way feature (or a kernel parameter to do so?) that will prevent userspace from enabling it after it's been disabled. -- Steve
[toc] | [prev] | [next] | [standalone]
| From | Borislav Petkov <bp@alien8.de> |
|---|---|
| Date | 2016-06-14 20:40 +0200 |
| Subject | Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg |
| Message-ID | <rK1pM-88-25@gated-at.bofh.it> |
| In reply to | #1422164 |
On Tue, Jun 14, 2016 at 02:14:56PM -0400, Steven Rostedt wrote:
> But can we make it a one way feature (or a kernel parameter to do so?)
> that will prevent userspace from enabling it after it's been disabled.
You mean to be able to do boot with:
"printk.kmsg=off_and_i_mean_it"
which then disables the sysctl? Are you suspecting insolent userspace
reenabling it?
--
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
[toc] | [prev] | [next] | [standalone]
| From | Linus Torvalds <torvalds@linux-foundation.org> |
|---|---|
| Date | 2016-06-16 03:50 +0200 |
| Subject | Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg |
| Message-ID | <rKuBr-1S9-11@gated-at.bofh.it> |
| In reply to | #1422164 |
On Tue, Jun 14, 2016 at 8:14 AM, Steven Rostedt <rostedt@goodmis.org> wrote:
>>
>> Please also add a sysctl!
>>
>> I *so* want to enable this on distro kernels without having to reboot the
>> kernel...
>>
>
> But can we make it a one way feature (or a kernel parameter to do so?)
> that will prevent userspace from enabling it after it's been disabled.
Possibly we could just say that if a kernel command line option has
been given, that is absolute.
And then a sysctl for when you do *not* explicitly set if on the
kernel command line?
Linus
[toc] | [prev] | [next] | [standalone]
| From | Borislav Petkov <bp@alien8.de> |
|---|---|
| Date | 2016-06-16 12:00 +0200 |
| Subject | Re: [PATCH 2/2] printk: Add kernel parameter to control writes to /dev/kmsg |
| Message-ID | <rKCfE-6K5-5@gated-at.bofh.it> |
| In reply to | #1423634 |
On Wed, Jun 15, 2016 at 03:40:04PM -1000, Linus Torvalds wrote:
> Possibly we could just say that if a kernel command line option has
> been given, that is absolute.
>
> And then a sysctl for when you do *not* explicitly set if on the
> kernel command line?
Ok, how about this ontop?
It is only lightly tested in a vm but basically I'm using the second
byte of devkmsg_log to set a bit in there and the sysctl handler looks
at it.
It is also visible in sysctl and we know it has been cmdline-disabled:
$ cat /proc/sys/kernel/printk_kmsg
256
---
diff --git a/Documentation/sysctl/kernel.txt b/Documentation/sysctl/kernel.txt
index a3683ce2a2f3..02fe4562953f 100644
--- a/Documentation/sysctl/kernel.txt
+++ b/Documentation/sysctl/kernel.txt
@@ -752,6 +752,19 @@ send before ratelimiting kicks in.
==============================================================
+printk_kmsg:
+
+Control the logging to /dev/kmsg from userspace:
+
+0: default, ratelimited
+1: unlimited logging to /dev/kmsg from userspace
+2: logging to /dev/kmsg disabled
+
+The kernel command line parameter printk.kmsg= overrides this setting
+and once set, it cannot be changed by this sysctl interface anymore.
+
+==============================================================
+
randomize_va_space:
This option can be used to select the type of process address
diff --git a/include/linux/printk.h b/include/linux/printk.h
index f4da695fd615..bcf72e756122 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -171,6 +171,12 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
extern int printk_delay_msec;
extern int dmesg_restrict;
extern int kptr_restrict;
+extern unsigned int devkmsg_log;
+
+struct ctl_table;
+
+int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos);
extern void wake_up_klogd(void);
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 33701a166f26..9f0a885c2718 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -86,12 +86,15 @@ static struct lockdep_map console_lock_dep_map = {
};
#endif
-#define DEVKMSG_LOG_RATELIMIT 0
-#define DEVKMSG_LOG_ON 1
-#define DEVKMSG_LOG_OFF 2
+#define DEVKMSG_LOG_RATELIMIT 0
+#define DEVKMSG_LOG_ON 1
+#define DEVKMSG_LOG_OFF 2
+#define DEVKMSG_LOCK (1 << 8)
+#define DEVKMSG_LOG_MASK (DEVKMSG_LOCK - 1)
+#define DEVKMSG_LOCKED_MASK ~DEVKMSG_LOG_MASK
/* DEVKMSG_LOG_RATELIMIT by default */
-static unsigned int __read_mostly devkmsg_log;
+unsigned int __read_mostly devkmsg_log;
static int __init control_devkmsg(char *str)
{
if (!str)
@@ -101,14 +104,30 @@ static int __init control_devkmsg(char *str)
devkmsg_log = DEVKMSG_LOG_ON;
else if (!strncmp(str, "off", 3))
devkmsg_log = DEVKMSG_LOG_OFF;
+ else if (!strncmp(str, "ratelimit", 9))
+ devkmsg_log = DEVKMSG_LOG_RATELIMIT;
else
return -EINVAL;
+ /* Sysctl cannot change it anymore. */
+ devkmsg_log |= DEVKMSG_LOCK;
+
return 0;
}
__setup("printk.kmsg=", control_devkmsg);
+int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+ if (devkmsg_log & DEVKMSG_LOCKED_MASK) {
+ if (write)
+ return -EINVAL;
+ }
+
+ return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
+}
+
/*
* Number of registered extended console drivers.
*
@@ -656,11 +675,12 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
return -EINVAL;
/* Ignore when user logging is disabled. */
- if (devkmsg_log == DEVKMSG_LOG_OFF)
+ if ((devkmsg_log & DEVKMSG_LOG_MASK) == DEVKMSG_LOG_OFF)
return len;
/* Ratelimit when not explicitly enabled or when we're not booting. */
- if ((system_state != SYSTEM_BOOTING) && (devkmsg_log != DEVKMSG_LOG_ON)) {
+ if ((system_state != SYSTEM_BOOTING) &&
+ ((devkmsg_log & DEVKMSG_LOG_MASK) != DEVKMSG_LOG_ON)) {
if (!___ratelimit(&user->rs, current->comm))
return ret;
}
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 87b2fc38398b..a29d6c4fa86c 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -814,6 +814,15 @@ static struct ctl_table kern_table[] = {
.extra2 = &ten_thousand,
},
{
+ .procname = "printk_kmsg",
+ .data = &devkmsg_log,
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = devkmsg_sysctl_set_loglvl,
+ .extra1 = &zero,
+ .extra2 = &two,
+ },
+ {
.procname = "dmesg_restrict",
.data = &dmesg_restrict,
.maxlen = sizeof(int),
--
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
[toc] | [prev] | [next] | [standalone]
| From | Linus Torvalds <torvalds@linux-foundation.org> |
|---|---|
| Date | 2016-06-16 03:50 +0200 |
| Message-ID | <rKuBr-1S9-7@gated-at.bofh.it> |
| In reply to | #1421740 |
On Tue, Jun 14, 2016 at 12:12 AM, Borislav Petkov <bp@alien8.de> wrote:
>
> Ok, so how about these two?
Looks ok to me. Ack.
Linus
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web