[apparmor] wrong loggingtime in apparmorlog

Seth Arnold seth.arnold at canonical.com
Wed Feb 4 23:11:42 UTC 2015


On Wed, Feb 04, 2015 at 10:47:58AM +0100, Hajo Locke wrote:
> Feb  4 09:55:01 hostname CRON[3898]: (root)...
> Feb  4 11:30:50 hostname kernel: type=1400
> audit(1423040101.577:7785293): apparmor="DENIED"...
> Feb  4 09:55:01 hostname CRON[3911]: (root)...
> 
> a relation to audit seems possible.  i found more kernel loglines in
> syslog with wrong date which are not in direct relation to apparmor.
> i think complete kern.log is wrong.
> may be more kernel related?  we upgrade regularly, current version
> on this machine is 3.13.0-43-generic (Ubuntu 14.04 LTS)
> Currently only rebooting solves this problem. after reboot
> timestamps in loglines are identical. But by increasing uptime the
> difference between realtime and kernel-log-time grows.
> one server with uptime ~2 days has kernel-time 53 seconds in future
> (3.13.0-45-generic).  server from loglines above has uptime 48 days
> and difference is more than 1,5 hours.

I've got a theory that's not very well thought through yet; to be honest,
this is an interesting problem that I've never seen before.

Here's some source code that's very similar to the rsyslog daemon used in
Ubuntu 14.04 LTS:

http://sources.debian.net/src/rsyslog/7.4.8-1%7Ebpo70%2B1/plugins/imklog/bsd.c/#L58

Note this section:

/* ... */
	clock_gettime(CLOCK_MONOTONIC, &monotonic);
	clock_gettime(CLOCK_REALTIME, &realtime);
	secOffs = realtime.tv_sec - monotonic.tv_sec;
	usecOffs = (realtime.tv_nsec - monotonic.tv_nsec) / 1000;
	if(usecOffs < 0) {
		secOffs--;
		usecOffs += 1000000l;
	}
	
	usecs += usecOffs;
	if(usecs > 999999l) {
		secs++;
		usecs -= 1000000l;
	}
	secs += secOffs;
	tv.tv_sec = secs;
	tv.tv_usec = usecs;
	tp = &tv;

done:
	Syslog(pri, buf, tp);
/* ... */

rsyslog is changing the timestamp supplied to its internal Syslog()
routine based on the time difference between the CLOCK_MONOTONIC and
CLOCK_REALTIME clocks. The CLOCK_MONOTONIC clock will only ever move
forward, while CLOCK_REALTIME can jump backwards if e.g. ntpdate(8) or
date(1) sets an entirely new time.

So, my theory -- this system is using ntpdate(8) in a cronjob to set the
time rather than using ntpd(8), which would _skew_ the time in a fashion
that's more amenable to keeping CLOCK_MONOTONIC and CLOCK_REALTIME closer
to synchronized. (ntpd(8) can ask the system clock to speed up or slow
down so that seconds take more or less time, but when skewing it won't
jump the time forwards or backwards abruptly.)

I've attached a little program that will output all the clocks documented
in clock_gettime(3) as well as the difference between the MONOTONIC clock
and REALTIME clock. If you've installed build-essential, you should be
able to compile it and run with just "make clocks ; ./clocks".

If I'm correct, the difference between the MONOTONIC and the REALTIME
clock will get worse over the uptime of the system. And if I'm correct,
you can fix the issue by switching to ntpd rather than using ntpdate
periodically. You may also be able to use the klogParseKernelTimestamp
configuration variable from rsyslog:
http://www.rsyslog.com/doc/master/configuration/modules/imklog.html

I hope this helps solve this issue.

Thanks
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clocks.c
Type: text/x-csrc
Size: 2069 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20150204/4b0850ee/attachment.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20150204/4b0850ee/attachment.pgp>


More information about the AppArmor mailing list