Skip to content

Commit 7e8eda7

Browse files
Ondrej Mosnacekpcmoore
authored andcommitted
ntp: Audit NTP parameters adjustment
Emit an audit record every time selected NTP parameters are modified from userspace (via adjtimex(2) or clock_adjtime(2)). These parameters may be used to indirectly change system clock, and thus their modifications should be audited. Such events will now generate records of type AUDIT_TIME_ADJNTPVAL containing the following fields: - op -- which value was adjusted: - offset -- corresponding to the time_offset variable - freq -- corresponding to the time_freq variable - status -- corresponding to the time_status variable - adjust -- corresponding to the time_adjust variable - tick -- corresponding to the tick_usec variable - tai -- corresponding to the timekeeping's TAI offset - old -- the old value - new -- the new value Example records: type=TIME_ADJNTPVAL msg=audit(1530616044.507:7): op=status old=64 new=8256 type=TIME_ADJNTPVAL msg=audit(1530616044.511:11): op=freq old=0 new=49180377088000 The records of this type will be associated with the corresponding syscall records. An overview of parameter changes that can be done via do_adjtimex() (based on information from Miroslav Lichvar) and whether they are audited: __timekeeping_set_tai_offset() -- sets the offset from the International Atomic Time (AUDITED) NTP variables: time_offset -- can adjust the clock by up to 0.5 seconds per call and also speed it up or slow down by up to about 0.05% (43 seconds per day) (AUDITED) time_freq -- can speed up or slow down by up to about 0.05% (AUDITED) time_status -- can insert/delete leap seconds and it also enables/ disables synchronization of the hardware real-time clock (AUDITED) time_maxerror, time_esterror -- change error estimates used to inform userspace applications (NOT AUDITED) time_constant -- controls the speed of the clock adjustments that are made when time_offset is set (NOT AUDITED) time_adjust -- can temporarily speed up or slow down the clock by up to 0.05% (AUDITED) tick_usec -- a more extreme version of time_freq; can speed up or slow down the clock by up to 10% (AUDITED) Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com> Reviewed-by: Richard Guy Briggs <rgb@redhat.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 2d87a06 commit 7e8eda7

6 files changed

Lines changed: 112 additions & 5 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ struct audit_field {
8686
u32 op;
8787
};
8888

89+
enum audit_ntp_type {
90+
AUDIT_NTP_OFFSET,
91+
AUDIT_NTP_FREQ,
92+
AUDIT_NTP_STATUS,
93+
AUDIT_NTP_TAI,
94+
AUDIT_NTP_TICK,
95+
AUDIT_NTP_ADJUST,
96+
97+
AUDIT_NTP_NVALS /* count */
98+
};
99+
100+
#ifdef CONFIG_AUDITSYSCALL
101+
struct audit_ntp_val {
102+
long long oldval, newval;
103+
};
104+
105+
struct audit_ntp_data {
106+
struct audit_ntp_val vals[AUDIT_NTP_NVALS];
107+
};
108+
#else
109+
struct audit_ntp_data {};
110+
#endif
111+
89112
extern int is_audit_feature_set(int which);
90113

91114
extern int __init audit_register_class(int class, unsigned *list);
@@ -366,6 +389,7 @@ extern void __audit_mmap_fd(int fd, int flags);
366389
extern void __audit_log_kern_module(char *name);
367390
extern void __audit_fanotify(unsigned int response);
368391
extern void __audit_tk_injoffset(struct timespec64 offset);
392+
extern void __audit_ntp_log(const struct audit_ntp_data *ad);
369393

370394
static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
371395
{
@@ -478,6 +502,29 @@ static inline void audit_tk_injoffset(struct timespec64 offset)
478502
__audit_tk_injoffset(offset);
479503
}
480504

505+
static inline void audit_ntp_init(struct audit_ntp_data *ad)
506+
{
507+
memset(ad, 0, sizeof(*ad));
508+
}
509+
510+
static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
511+
enum audit_ntp_type type, long long val)
512+
{
513+
ad->vals[type].oldval = val;
514+
}
515+
516+
static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
517+
enum audit_ntp_type type, long long val)
518+
{
519+
ad->vals[type].newval = val;
520+
}
521+
522+
static inline void audit_ntp_log(const struct audit_ntp_data *ad)
523+
{
524+
if (!audit_dummy_context())
525+
__audit_ntp_log(ad);
526+
}
527+
481528
extern int audit_n_rules;
482529
extern int audit_signals;
483530
#else /* CONFIG_AUDITSYSCALL */
@@ -594,6 +641,20 @@ static inline void audit_fanotify(unsigned int response)
594641
static inline void audit_tk_injoffset(struct timespec64 offset)
595642
{ }
596643

644+
static inline void audit_ntp_init(struct audit_ntp_data *ad)
645+
{ }
646+
647+
static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
648+
enum audit_ntp_type type, long long val)
649+
{ }
650+
651+
static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
652+
enum audit_ntp_type type, long long val)
653+
{ }
654+
655+
static inline void audit_ntp_log(const struct audit_ntp_data *ad)
656+
{ }
657+
597658
static inline void audit_ptrace(struct task_struct *t)
598659
{ }
599660
#define audit_n_rules 0

‎include/uapi/linux/audit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
#define AUDIT_KERN_MODULE 1330 /* Kernel Module events */
116116
#define AUDIT_FANOTIFY 1331 /* Fanotify access decision */
117117
#define AUDIT_TIME_INJOFFSET 1332 /* Timekeeping offset injected */
118+
#define AUDIT_TIME_ADJNTPVAL 1333 /* NTP value adjustment */
118119

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

‎kernel/auditsc.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,28 @@ void __audit_tk_injoffset(struct timespec64 offset)
25192519
(long long)offset.tv_sec, offset.tv_nsec);
25202520
}
25212521

2522+
static void audit_log_ntp_val(const struct audit_ntp_data *ad,
2523+
const char *op, enum audit_ntp_type type)
2524+
{
2525+
const struct audit_ntp_val *val = &ad->vals[type];
2526+
2527+
if (val->newval == val->oldval)
2528+
return;
2529+
2530+
audit_log(audit_context(), GFP_KERNEL, AUDIT_TIME_ADJNTPVAL,
2531+
"op=%s old=%lli new=%lli", op, val->oldval, val->newval);
2532+
}
2533+
2534+
void __audit_ntp_log(const struct audit_ntp_data *ad)
2535+
{
2536+
audit_log_ntp_val(ad, "offset", AUDIT_NTP_OFFSET);
2537+
audit_log_ntp_val(ad, "freq", AUDIT_NTP_FREQ);
2538+
audit_log_ntp_val(ad, "status", AUDIT_NTP_STATUS);
2539+
audit_log_ntp_val(ad, "tai", AUDIT_NTP_TAI);
2540+
audit_log_ntp_val(ad, "tick", AUDIT_NTP_TICK);
2541+
audit_log_ntp_val(ad, "adjust", AUDIT_NTP_ADJUST);
2542+
}
2543+
25222544
static void audit_log_task(struct audit_buffer *ab)
25232545
{
25242546
kuid_t auid, uid;

‎kernel/time/ntp.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/mm.h>
1818
#include <linux/module.h>
1919
#include <linux/rtc.h>
20+
#include <linux/audit.h>
2021

2122
#include "ntp_internal.h"
2223
#include "timekeeping_internal.h"
@@ -709,7 +710,7 @@ static inline void process_adjtimex_modes(const struct __kernel_timex *txc,
709710
* kernel time-keeping variables. used by xntpd.
710711
*/
711712
int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts,
712-
s32 *time_tai)
713+
s32 *time_tai, struct audit_ntp_data *ad)
713714
{
714715
int result;
715716

@@ -720,14 +721,29 @@ int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts,
720721
/* adjtime() is independent from ntp_adjtime() */
721722
time_adjust = txc->offset;
722723
ntp_update_frequency();
724+
725+
audit_ntp_set_old(ad, AUDIT_NTP_ADJUST, save_adjust);
726+
audit_ntp_set_new(ad, AUDIT_NTP_ADJUST, time_adjust);
723727
}
724728
txc->offset = save_adjust;
725729
} else {
726-
727730
/* If there are input parameters, then process them: */
728-
if (txc->modes)
731+
if (txc->modes) {
732+
audit_ntp_set_old(ad, AUDIT_NTP_OFFSET, time_offset);
733+
audit_ntp_set_old(ad, AUDIT_NTP_FREQ, time_freq);
734+
audit_ntp_set_old(ad, AUDIT_NTP_STATUS, time_status);
735+
audit_ntp_set_old(ad, AUDIT_NTP_TAI, *time_tai);
736+
audit_ntp_set_old(ad, AUDIT_NTP_TICK, tick_usec);
737+
729738
process_adjtimex_modes(txc, time_tai);
730739

740+
audit_ntp_set_new(ad, AUDIT_NTP_OFFSET, time_offset);
741+
audit_ntp_set_new(ad, AUDIT_NTP_FREQ, time_freq);
742+
audit_ntp_set_new(ad, AUDIT_NTP_STATUS, time_status);
743+
audit_ntp_set_new(ad, AUDIT_NTP_TAI, *time_tai);
744+
audit_ntp_set_new(ad, AUDIT_NTP_TICK, tick_usec);
745+
}
746+
731747
txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
732748
NTP_SCALE_SHIFT);
733749
if (!(time_status & STA_NANO))

‎kernel/time/ntp_internal.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern void ntp_clear(void);
88
extern u64 ntp_tick_length(void);
99
extern ktime_t ntp_get_next_leap(void);
1010
extern int second_overflow(time64_t secs);
11-
extern int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts, s32 *time_tai);
11+
extern int __do_adjtimex(struct __kernel_timex *txc,
12+
const struct timespec64 *ts,
13+
s32 *time_tai, struct audit_ntp_data *ad);
1214
extern void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts);
1315
#endif /* _LINUX_NTP_INTERNAL_H */

‎kernel/time/timekeeping.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,7 @@ static int timekeeping_validate_timex(const struct __kernel_timex *txc)
23072307
int do_adjtimex(struct __kernel_timex *txc)
23082308
{
23092309
struct timekeeper *tk = &tk_core.timekeeper;
2310+
struct audit_ntp_data ad;
23102311
unsigned long flags;
23112312
struct timespec64 ts;
23122313
s32 orig_tai, tai;
@@ -2330,13 +2331,15 @@ int do_adjtimex(struct __kernel_timex *txc)
23302331
audit_tk_injoffset(delta);
23312332
}
23322333

2334+
audit_ntp_init(&ad);
2335+
23332336
ktime_get_real_ts64(&ts);
23342337

23352338
raw_spin_lock_irqsave(&timekeeper_lock, flags);
23362339
write_seqcount_begin(&tk_core.seq);
23372340

23382341
orig_tai = tai = tk->tai_offset;
2339-
ret = __do_adjtimex(txc, &ts, &tai);
2342+
ret = __do_adjtimex(txc, &ts, &tai, &ad);
23402343

23412344
if (tai != orig_tai) {
23422345
__timekeeping_set_tai_offset(tk, tai);
@@ -2347,6 +2350,8 @@ int do_adjtimex(struct __kernel_timex *txc)
23472350
write_seqcount_end(&tk_core.seq);
23482351
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
23492352

2353+
audit_ntp_log(&ad);
2354+
23502355
/* Update the multiplier immediately if frequency was set directly */
23512356
if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
23522357
timekeeping_advance(TK_ADV_FREQ);

0 commit comments

Comments
 (0)