Skip to content

Commit 34d99af

Browse files
rgbriggspcmoore
authored andcommitted
audit: implement audit by executable
This adds the ability audit the actions of a not-yet-running process. This patch implements the ability to filter on the executable path. Instead of just hard coding the ino and dev of the executable we care about at the moment the rule is inserted into the kernel, use the new audit_fsnotify infrastructure to manage this dynamically. This means that if the filename does not yet exist but the containing directory does, or if the inode in question is unlinked and creat'd (aka updated) the rule will just continue to work. If the containing directory is moved or deleted or the filesystem is unmounted, the rule is deleted automatically. A future enhancement would be to have the rule survive across directory disruptions. This is a heavily modified version of a patch originally submitted by Eric Paris with some ideas from Peter Moody. Cc: Peter Moody <peter@hda3.com> Cc: Eric Paris <eparis@redhat.com> Signed-off-by: Richard Guy Briggs <rgb@redhat.com> [PM: minor whitespace clean to satisfy ./scripts/checkpatch] Signed-off-by: Paul Moore <pmoore@redhat.com>
1 parent 7f49294 commit 34d99af

7 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct audit_krule {
6262
struct audit_field *inode_f; /* quick access to an inode field */
6363
struct audit_watch *watch; /* associated watch */
6464
struct audit_tree *tree; /* associated watched tree */
65+
struct audit_fsnotify_mark *exe;
6566
struct list_head rlist; /* entry in audit_{watch,tree}.rules list */
6667
struct list_head list; /* for AUDIT_LIST* purposes only */
6768
u64 prio;

‎include/uapi/linux/audit.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
#define AUDIT_OBJ_UID 109
267267
#define AUDIT_OBJ_GID 110
268268
#define AUDIT_FIELD_COMPARE 111
269+
#define AUDIT_EXE 112
269270

270271
#define AUDIT_ARG0 200
271272
#define AUDIT_ARG1 (AUDIT_ARG0+1)
@@ -324,8 +325,10 @@ enum {
324325

325326
#define AUDIT_FEATURE_BITMAP_BACKLOG_LIMIT 0x00000001
326327
#define AUDIT_FEATURE_BITMAP_BACKLOG_WAIT_TIME 0x00000002
328+
#define AUDIT_FEATURE_BITMAP_EXECUTABLE_PATH 0x00000004
327329
#define AUDIT_FEATURE_BITMAP_ALL (AUDIT_FEATURE_BITMAP_BACKLOG_LIMIT | \
328-
AUDIT_FEATURE_BITMAP_BACKLOG_WAIT_TIME)
330+
AUDIT_FEATURE_BITMAP_BACKLOG_WAIT_TIME | \
331+
AUDIT_FEATURE_BITMAP_EXECUTABLE_PATH)
329332

330333
/* deprecated: AUDIT_VERSION_* */
331334
#define AUDIT_VERSION_LATEST AUDIT_FEATURE_BITMAP_ALL

‎kernel/audit.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ extern char *audit_mark_path(struct audit_fsnotify_mark *mark);
274274
extern void audit_remove_mark(struct audit_fsnotify_mark *audit_mark);
275275
extern void audit_remove_mark_rule(struct audit_krule *krule);
276276
extern int audit_mark_compare(struct audit_fsnotify_mark *mark, unsigned long ino, dev_t dev);
277+
extern int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old);
278+
extern int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark);
277279

278280
#else
279281
#define audit_put_watch(w) {}
@@ -289,6 +291,8 @@ extern int audit_mark_compare(struct audit_fsnotify_mark *mark, unsigned long in
289291
#define audit_remove_mark(m)
290292
#define audit_remove_mark_rule(k)
291293
#define audit_mark_compare(m, i, d) 0
294+
#define audit_exe_compare(t, m) (-EINVAL)
295+
#define audit_dupe_exe(n, o) (-EINVAL)
292296
#endif /* CONFIG_AUDIT_WATCH */
293297

294298
#ifdef CONFIG_AUDIT_TREE

‎kernel/audit_tree.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ static void kill_rules(struct audit_tree *tree)
478478
if (rule->tree) {
479479
/* not a half-baked one */
480480
audit_tree_log_remove_rule(rule);
481+
if (entry->rule.exe)
482+
audit_remove_mark(entry->rule.exe);
481483
rule->tree = NULL;
482484
list_del_rcu(&entry->list);
483485
list_del(&entry->rule.list);

‎kernel/audit_watch.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ static void audit_update_watch(struct audit_parent *parent,
312312
list_replace(&oentry->rule.list,
313313
&nentry->rule.list);
314314
}
315+
if (oentry->rule.exe)
316+
audit_remove_mark(oentry->rule.exe);
315317

316318
audit_watch_log_rule_change(r, owatch, "updated_rules");
317319

@@ -342,6 +344,8 @@ static void audit_remove_parent_watches(struct audit_parent *parent)
342344
list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
343345
e = container_of(r, struct audit_entry, rule);
344346
audit_watch_log_rule_change(r, w, "remove_rule");
347+
if (e->rule.exe)
348+
audit_remove_mark(e->rule.exe);
345349
list_del(&r->rlist);
346350
list_del(&r->list);
347351
list_del_rcu(&e->list);
@@ -514,3 +518,30 @@ static int __init audit_watch_init(void)
514518
return 0;
515519
}
516520
device_initcall(audit_watch_init);
521+
522+
int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old)
523+
{
524+
struct audit_fsnotify_mark *audit_mark;
525+
char *pathname;
526+
527+
pathname = kstrdup(audit_mark_path(old->exe), GFP_KERNEL);
528+
if (!pathname)
529+
return -ENOMEM;
530+
531+
audit_mark = audit_alloc_mark(new, pathname, strlen(pathname));
532+
if (IS_ERR(audit_mark)) {
533+
kfree(pathname);
534+
return PTR_ERR(audit_mark);
535+
}
536+
new->exe = audit_mark;
537+
538+
return 0;
539+
}
540+
541+
int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)
542+
{
543+
unsigned long ino = tsk->mm->exe_file->f_inode->i_ino;
544+
dev_t dev = tsk->mm->exe_file->f_inode->i_sb->s_dev;
545+
546+
return audit_mark_compare(mark, ino, dev);
547+
}

‎kernel/auditfilter.c‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ static int audit_field_valid(struct audit_entry *entry, struct audit_field *f)
405405
if (f->val > AUDIT_MAX_FIELD_COMPARE)
406406
return -EINVAL;
407407
break;
408+
case AUDIT_EXE:
409+
if (f->op != Audit_equal)
410+
return -EINVAL;
411+
if (entry->rule.listnr != AUDIT_FILTER_EXIT)
412+
return -EINVAL;
413+
break;
408414
};
409415
return 0;
410416
}
@@ -419,6 +425,7 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
419425
size_t remain = datasz - sizeof(struct audit_rule_data);
420426
int i;
421427
char *str;
428+
struct audit_fsnotify_mark *audit_mark;
422429

423430
entry = audit_to_entry_common(data);
424431
if (IS_ERR(entry))
@@ -539,6 +546,24 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
539546
entry->rule.buflen += f->val;
540547
entry->rule.filterkey = str;
541548
break;
549+
case AUDIT_EXE:
550+
if (entry->rule.exe || f->val > PATH_MAX)
551+
goto exit_free;
552+
str = audit_unpack_string(&bufp, &remain, f->val);
553+
if (IS_ERR(str)) {
554+
err = PTR_ERR(str);
555+
goto exit_free;
556+
}
557+
entry->rule.buflen += f->val;
558+
559+
audit_mark = audit_alloc_mark(&entry->rule, str, f->val);
560+
if (IS_ERR(audit_mark)) {
561+
kfree(str);
562+
err = PTR_ERR(audit_mark);
563+
goto exit_free;
564+
}
565+
entry->rule.exe = audit_mark;
566+
break;
542567
}
543568
}
544569

@@ -551,6 +576,8 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
551576
exit_free:
552577
if (entry->rule.tree)
553578
audit_put_tree(entry->rule.tree); /* that's the temporary one */
579+
if (entry->rule.exe)
580+
audit_remove_mark(entry->rule.exe); /* that's the template one */
554581
audit_free_rule(entry);
555582
return ERR_PTR(err);
556583
}
@@ -615,6 +642,10 @@ static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
615642
data->buflen += data->values[i] =
616643
audit_pack_string(&bufp, krule->filterkey);
617644
break;
645+
case AUDIT_EXE:
646+
data->buflen += data->values[i] =
647+
audit_pack_string(&bufp, audit_mark_path(krule->exe));
648+
break;
618649
case AUDIT_LOGINUID_SET:
619650
if (krule->pflags & AUDIT_LOGINUID_LEGACY && !f->val) {
620651
data->fields[i] = AUDIT_LOGINUID;
@@ -678,6 +709,12 @@ static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
678709
if (strcmp(a->filterkey, b->filterkey))
679710
return 1;
680711
break;
712+
case AUDIT_EXE:
713+
/* both paths exist based on above type compare */
714+
if (strcmp(audit_mark_path(a->exe),
715+
audit_mark_path(b->exe)))
716+
return 1;
717+
break;
681718
case AUDIT_UID:
682719
case AUDIT_EUID:
683720
case AUDIT_SUID:
@@ -799,8 +836,14 @@ struct audit_entry *audit_dupe_rule(struct audit_krule *old)
799836
err = -ENOMEM;
800837
else
801838
new->filterkey = fk;
839+
break;
840+
case AUDIT_EXE:
841+
err = audit_dupe_exe(new, old);
842+
break;
802843
}
803844
if (err) {
845+
if (new->exe)
846+
audit_remove_mark(new->exe);
804847
audit_free_rule(entry);
805848
return ERR_PTR(err);
806849
}
@@ -963,6 +1006,9 @@ int audit_del_rule(struct audit_entry *entry)
9631006
if (e->rule.tree)
9641007
audit_remove_tree_rule(&e->rule);
9651008

1009+
if (e->rule.exe)
1010+
audit_remove_mark_rule(&e->rule);
1011+
9661012
#ifdef CONFIG_AUDITSYSCALL
9671013
if (!dont_count)
9681014
audit_n_rules--;
@@ -1067,8 +1113,11 @@ int audit_rule_change(int type, __u32 portid, int seq, void *data,
10671113
WARN_ON(1);
10681114
}
10691115

1070-
if (err || type == AUDIT_DEL_RULE)
1116+
if (err || type == AUDIT_DEL_RULE) {
1117+
if (entry->rule.exe)
1118+
audit_remove_mark(entry->rule.exe);
10711119
audit_free_rule(entry);
1120+
}
10721121

10731122
return err;
10741123
}
@@ -1360,6 +1409,8 @@ static int update_lsm_rule(struct audit_krule *r)
13601409
return 0;
13611410

13621411
nentry = audit_dupe_rule(r);
1412+
if (entry->rule.exe)
1413+
audit_remove_mark(entry->rule.exe);
13631414
if (IS_ERR(nentry)) {
13641415
/* save the first error encountered for the
13651416
* return value */

‎kernel/auditsc.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ static int audit_filter_rules(struct task_struct *tsk,
466466
result = audit_comparator(ctx->ppid, f->op, f->val);
467467
}
468468
break;
469+
case AUDIT_EXE:
470+
result = audit_exe_compare(tsk, rule->exe);
471+
break;
469472
case AUDIT_UID:
470473
result = audit_uid_comparator(cred->uid, f->op, f->uid);
471474
break;

0 commit comments

Comments
 (0)