Skip to content

Commit 2d87a06

Browse files
Ondrej Mosnacekpcmoore
authored andcommitted
timekeeping: Audit clock adjustments
Emit an audit record whenever the system clock is changed (i.e. shifted by a non-zero offset) by a syscall from userspace. The syscalls than can (at the time of writing) trigger such record are: - settimeofday(2), stime(2), clock_settime(2) -- via do_settimeofday64() - adjtimex(2), clock_adjtime(2) -- via do_adjtimex() The new records have type AUDIT_TIME_INJOFFSET and contain the following fields: - sec -- the 'seconds' part of the offset - nsec -- the 'nanoseconds' part of the offset Example record (time was shifted backwards by ~15.875 seconds): type=TIME_INJOFFSET msg=audit(1530616049.652:13): sec=-16 nsec=124887145 The records of this type will be associated with the corresponding syscall records. Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com> Reviewed-by: Richard Guy Briggs <rgb@redhat.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> [PM: fixed a line width problem in __audit_tk_injoffset()] Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 699c186 commit 2d87a06

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ extern void __audit_log_capset(const struct cred *new, const struct cred *old);
365365
extern void __audit_mmap_fd(int fd, int flags);
366366
extern void __audit_log_kern_module(char *name);
367367
extern void __audit_fanotify(unsigned int response);
368+
extern void __audit_tk_injoffset(struct timespec64 offset);
368369

369370
static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
370371
{
@@ -467,6 +468,16 @@ static inline void audit_fanotify(unsigned int response)
467468
__audit_fanotify(response);
468469
}
469470

471+
static inline void audit_tk_injoffset(struct timespec64 offset)
472+
{
473+
/* ignore no-op events */
474+
if (offset.tv_sec == 0 && offset.tv_nsec == 0)
475+
return;
476+
477+
if (!audit_dummy_context())
478+
__audit_tk_injoffset(offset);
479+
}
480+
470481
extern int audit_n_rules;
471482
extern int audit_signals;
472483
#else /* CONFIG_AUDITSYSCALL */
@@ -580,6 +591,9 @@ static inline void audit_log_kern_module(char *name)
580591
static inline void audit_fanotify(unsigned int response)
581592
{ }
582593

594+
static inline void audit_tk_injoffset(struct timespec64 offset)
595+
{ }
596+
583597
static inline void audit_ptrace(struct task_struct *t)
584598
{ }
585599
#define audit_n_rules 0

‎include/uapi/linux/audit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
#define AUDIT_REPLACE 1329 /* Replace auditd if this packet unanswerd */
115115
#define AUDIT_KERN_MODULE 1330 /* Kernel Module events */
116116
#define AUDIT_FANOTIFY 1331 /* Fanotify access decision */
117+
#define AUDIT_TIME_INJOFFSET 1332 /* Timekeeping offset injected */
117118

118119
#define AUDIT_AVC 1400 /* SE Linux avc denial or grant */
119120
#define AUDIT_SELINUX_ERR 1401 /* Internal SE Linux Errors */

‎kernel/auditsc.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,6 +2512,13 @@ void __audit_fanotify(unsigned int response)
25122512
AUDIT_FANOTIFY, "resp=%u", response);
25132513
}
25142514

2515+
void __audit_tk_injoffset(struct timespec64 offset)
2516+
{
2517+
audit_log(audit_context(), GFP_KERNEL, AUDIT_TIME_INJOFFSET,
2518+
"sec=%lli nsec=%li",
2519+
(long long)offset.tv_sec, offset.tv_nsec);
2520+
}
2521+
25152522
static void audit_log_task(struct audit_buffer *ab)
25162523
{
25172524
kuid_t auid, uid;

‎kernel/time/timekeeping.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/stop_machine.h>
2222
#include <linux/pvclock_gtod.h>
2323
#include <linux/compiler.h>
24+
#include <linux/audit.h>
2425

2526
#include "tick-internal.h"
2627
#include "ntp_internal.h"
@@ -1250,6 +1251,9 @@ int do_settimeofday64(const struct timespec64 *ts)
12501251
/* signal hrtimers about time change */
12511252
clock_was_set();
12521253

1254+
if (!ret)
1255+
audit_tk_injoffset(ts_delta);
1256+
12531257
return ret;
12541258
}
12551259
EXPORT_SYMBOL(do_settimeofday64);
@@ -2322,6 +2326,8 @@ int do_adjtimex(struct __kernel_timex *txc)
23222326
ret = timekeeping_inject_offset(&delta);
23232327
if (ret)
23242328
return ret;
2329+
2330+
audit_tk_injoffset(delta);
23252331
}
23262332

23272333
ktime_get_real_ts64(&ts);

0 commit comments

Comments
 (0)