Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933230AbcDYS4N (ORCPT ); Mon, 25 Apr 2016 14:56:13 -0400 Received: from smtprelay0210.hostedemail.com ([216.40.44.210]:38417 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754656AbcDYS4L convert rfc822-to-8bit (ORCPT ); Mon, 25 Apr 2016 14:56:11 -0400 X-Session-Marker: 726F737465647440676F6F646D69732E6F7267 X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,rostedt@goodmis.org,:::::::::::::::,RULES_HIT:41:152:355:379:541:800:960:968:973:988:989:1260:1277:1311:1313:1314:1345:1437:1513:1515:1516:1518:1521:1535:1544:1593:1594:1605:1711:1730:1747:1777:1792:2198:2199:2393:2559:2562:2691:2693:2892:2901:3138:3139:3140:3141:3142:3167:3865:3866:3867:3868:3870:3871:3872:3874:4321:5007:6117:6119:6261:7903:10004:10226:10848:11026:11232:11657:11658:11914:12043:12050:12296:12438:12517:12519:12555:13095:13161:13229:13972:14096:14097:14181:14394:14659:14721:21080:21212:21326:21433:30034:30051:30054:30075,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: waves86_52a53c207ce48 X-Filterd-Recvd-Size: 5653 Date: Mon, 25 Apr 2016 14:56:06 -0400 From: Steven Rostedt To: Linus Torvalds Cc: LKML , Ingo Molnar , Peter Zijlstra , Borislav Petkov , Greg Kroah-Hartman , Andrew Morton , Uwe =?UTF-8?B?S2xlaW5lLUvDtm5pZw==?= Subject: [PATCH v2] printk: Add kernel parameter to disable writes to /dev/kmsg Message-ID: <20160425145606.598329f2@gandalf.local.home> X-Mailer: Claws Mail 3.13.2 (GTK+ 2.24.30; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4526 Lines: 102 Over the weekend my server was acting funny. The display wasn't working well, and I assumed that a driver was going bad. I went to look at the kernel dmesg, but the buffer only had the following over and over: [226062.401405] systemd-logind[3511]: Removed session 4168. [226063.381051] systemd-logind[3511]: Removed session 4169. [226232.279412] systemd-logind[3511]: New session 4172 of user rostedt. [226295.639223] systemd-logind[3511]: Removed session 4172. [227867.920584] systemd-logind[3511]: New session 4204 of user rostedt. [227869.016023] systemd-logind[3511]: New session 4205 of user rostedt. [227927.094215] systemd-logind[3511]: Removed session 4204. [227927.905655] systemd-logind[3511]: Removed session 4205. [229740.942811] systemd-logind[3511]: New session 4237 of user rostedt. [229741.505884] systemd-logind[3511]: New session 4238 of user rostedt. [229799.710123] systemd-logind[3511]: Removed session 4237. [229800.668171] systemd-logind[3511]: Removed session 4238. [229835.378869] systemd-logind[3511]: New session 4240 of user rostedt. [229898.433560] systemd-logind[3511]: Removed session 4240. [231429.405715] systemd-logind[3511]: New session 4272 of user rostedt. [231429.964865] systemd-logind[3511]: New session 4273 of user rostedt. [231487.908190] systemd-logind[3511]: Removed session 4272. [231488.861240] systemd-logind[3511]: Removed session 4273. [233280.032816] systemd-logind[3511]: New session 4306 of user rostedt. [233280.505022] systemd-logind[3511]: New session 4307 of user rostedt. [233338.761804] systemd-logind[3511]: Removed session 4306. [233339.749970] systemd-logind[3511]: Removed session 4307. [233438.696027] systemd-logind[3511]: New session 4309 of user rostedt. [233499.959512] systemd-logind[3511]: Removed session 4309. The kernel buffer was completely overridden by useless spewing from user space. I know that people consider this a "feature" but to me it's quite annoying that I constantly have to fight to get kernel messages. I personally believe that only the kernel should have the right to write into the kernel log buffers, as user space can easily blow away any useful kernel information with useless logging. I simply propose a way to let us kernel developers keep user space from interfering, by adding a new kernel command line parameter that will disable writing to /dev/kmsg. Any attempt to open the file in write mode will return a -EPERM error. This should have no affect on distros that want to keep the feature of writing to /dev/kmsg, as it requires a kernel command line to disable. Signed-off-by: Steven Rostedt --- Changes since v1: Rename the kernel parameter disable_devkmsg_write to printk.disable_kmsg_write, to be more inline with the other printk kmsg parameters. (Suggested by Borislav Petkov). Have the internal parameter be set to "read_mostly" as it's only set on boot up. (Suggested by Uwe Kleine-König). diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt index 0b3de80ec8f6..517ebe4bb391 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt @@ -3111,6 +3111,12 @@ bytes respectively. Such letter suffixes can also be entirely omitted. Format: (1/Y/y=enable, 0/N/n=disable) default: disabled + printk.disable_kmsg_write + Disable writing to /dev/kmsg. This prevents user space + tools from writing into the kernel printk buffers. + When set, opening /dev/kmsg for write mode will return + -EPERM. + printk.time= Show timing data prefixed to each printk message line Format: (1/Y/y=enable, 0/N/n=disable) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index bfbf284e4218..e86cede893a9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -85,6 +85,15 @@ static struct lockdep_map console_lock_dep_map = { }; #endif +static bool __read_mostly devkmsg_disabled; +static int __init disable_devkmsg(char *str) +{ + devkmsg_disabled = true; + return 0; +} +__setup("printk.disable_kmsg_write", disable_devkmsg); + + /* * Number of registered extended console drivers. * @@ -799,6 +808,10 @@ static int devkmsg_open(struct inode *inode, struct file *file) struct devkmsg_user *user; int err; + /* When devkmsg_disabled is set, fail all write access */ + if (devkmsg_disabled && (file->f_flags & O_ACCMODE)) + return -EPERM; + /* write-only does not need any file context */ if ((file->f_flags & O_ACCMODE) == O_WRONLY) return 0;