logging to dmesg from userland

Ian Smith smithi at nimnet.asn.au
Mon Mar 14 15:03:36 UTC 2011


In freebsd-questions Digest, Vol 354, Issue 1, Message: 15
On Sun, 13 Mar 2011 19:08:20 -0700 perryh at pluto.rain.com wrote:

 > I am looking for a way to write into the kernel message buffer --
 > the one that dmesg prints out -- from a userland program, to help
 > in relating kernel printf messages to the userland operations which
 > provoked them.  (Yes, I am aware of the potential DoS implications:
 > the capability should be restricted to root, or at least to the
 > "operator" group.  I expect to use it only in single-user mode.)

Perry, interesting to see that unprivileged users can use logger to spam 
/var/log/messages (by default), on 5.5 <cough> and 7.4-PRE anyway.  I've 
long assumed that I could do that just because I'm in wheel, but not so.

 > Is there a program, or a system call, which can do this?  logger(1)
 > seemed a likely prospect, but either it doesn't have this capability
 > or I haven't found the formula.

Had a bit of a play around earlier, and as an unprivileged user can do:

%who am i
subs             ttyv6    Mar 14 18:06
%id -p
uid     subs
groups  subs
%logger -p kern.notice hello from subs at kern.notice
%logger -p kern.crit hello from subs at kern.crit

logger(1) without -p writes to user.notice, which writes only to 
/var/log/messages (with standardish syslog.conf settings), but of the 
two above, only the latter one to kern.crit wound up in 'dmesg -a'

sola# dmesg | grep subs
sola# dmesg -a | grep subs
Mar 15 00:07:35 sola subs: hello from subs at kern.crit
Mar 15 00:07:35 sola subs: hello from subs at kern.crit

but twice!

Both appear in /var/log/messages, one of each, but only the latter also 
appeared - again twice - in /var/log/console.log .. not sure why twice, 
but syslog.conf can be tricky .. anyway, later trying other kern.levels:

%logger -p kern.err hello from subs at kern.err
%logger -p kern.alert hello from subs at kern.alert
%logger -p kern.warning hello from subs at kern.warning

All three go to messages, but just these two added to dmesg -a output:

Mar 15 00:44:54 sola subs: hello from subs at kern.err
Mar 15 00:45:37 sola subs: hello from subs at kern.alert

Moreover on my 7.4 system I tested also with kern.emerg, which indeed 
sent the emerg message to all open consoles, including root's!

Other kern. levels may work too, as may other facilities? and YMMV.

Colour me very surprised not having to be root to do any of those, 
especially those that do write to the kernel message buffer ..

cheers, Ian


More information about the freebsd-questions mailing list