linux-audit.redhat.com archive mirror
 help / color / mirror / Atom feed
From: <yang.yang29@zte.com.cn>
To: <paul@paul-moore.com>
Cc: linux-audit@redhat.com, linux-kernel@vger.kernel.org
Subject: [RFC,v2,1/1] audit: speed up syscall rule match while exiting syscall
Date: Thu, 21 Jan 2021 21:54:27 +0800 (CST)	[thread overview]
Message-ID: <202101212154272626110@zte.com.cn> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 4407 bytes --]

From 72f3ecde58edb03d76cb359607fef98c1663d481 Mon Sep 17 00:00:00 2001
From: Yang Yang <yang.yang29@zte.com.cn>
Date: Thu, 21 Jan 2021 21:05:04 +0800
Subject: [PATCH] [RFC,v2,1/1] speed up syscall rule match while exiting syscall
 audit_filter_syscall() traverses struct list_head audit_filter_list to find
 out whether current syscall match one rule. This takes o(n), which is not
 necessary, specially for user who add a very few syscall rules. On the other
 hand, user may not much care about rule add/delete speed. So do o(n)
 calculate at rule changing, and ease the burden of audit_filter_syscall().

 Define audit_syscall[NR_syscalls], every element stands for one syscall.
 audit_filter_syscall() checks audit_syscall[NR_syscalls].
 audit_syscall[n] == 0 indicates no rule audit syscall n, do a quick exit.
 audit_syscall[n] > 0 indicates at least one rule audit syscall n.
 audit_syscall[n] update when syscall rule changes.

Signed-off-by: Yang Yang <yang.yang29@zte.com.cn>
---
 include/linux/audit.h |  2 ++
 kernel/audit.c        |  4 ++++
 kernel/auditfilter.c  | 30 ++++++++++++++++++++++++++++++
 kernel/auditsc.c      |  5 ++++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/include/linux/audit.h b/include/linux/audit.h
index 82b7c11..7885531 100644
--- a/include/linux/audit.h
+++ b/include/linux/audit.h
@@ -549,6 +549,8 @@ static inline void audit_log_nfcfg(const char *name, u8 af,

 extern int audit_n_rules;
 extern int audit_signals;
+extern u32 auditing_syscall[NR_syscalls];
+extern int audit_in_mask(const struct audit_krule *rule, unsigned long val);
 #else /* CONFIG_AUDITSYSCALL */
 static inline int audit_alloc(struct task_struct *task)
 {
diff --git a/kernel/audit.c b/kernel/audit.c
index 1ffc2e0..07f4590 100644
--- a/kernel/audit.c
+++ b/kernel/audit.c
@@ -208,6 +208,10 @@ struct audit_reply {
 	struct sk_buff *skb;
 };

+#ifdef CONFIG_AUDITSYSCALL
+u32 auditing_syscall[NR_syscalls] = {0};
+#endif
+
 /**
  * auditd_test_task - Check to see if a given task is an audit daemon
  * @task: the task to check
diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c
index 333b3bc..9d3e703 100644
--- a/kernel/auditfilter.c
+++ b/kernel/auditfilter.c
@@ -926,6 +926,28 @@ static struct audit_entry *audit_find_rule(struct audit_entry *entry,
 static u64 prio_low = ~0ULL/2;
 static u64 prio_high = ~0ULL/2 - 1;

+#ifdef CONFIG_AUDITSYSCALL
+static inline void update_auditing_syscall(struct audit_krule rule, bool add)
+{
+    int i;
+
+    /* syscall rule with type AUDIT_FILTER_EXIT */
+    if (rule.listnr == AUDIT_FILTER_EXIT && !rule.watch && !rule.tree) {
+        for (i = 0; i < NR_syscalls; i++) {
+            /* whether this rule include one syscall */
+            if (unlikely(audit_in_mask(&rule, i))) {
+                if (add == true)
+                    auditing_syscall[i]++;
+                else
+                    auditing_syscall[i]--;
+            }
+        }
+    } 
+
+    return;
+}
+#endif
+
 /* Add rule to given filterlist if not a duplicate. */
 static inline int audit_add_rule(struct audit_entry *entry)
 {
@@ -957,6 +979,10 @@ static inline int audit_add_rule(struct audit_entry *entry)
 		return err;
 	}

+#ifdef CONFIG_AUDITSYSCALL
+    update_auditing_syscall(entry->rule, true);
+#endif
+
 	if (watch) {
 		/* audit_filter_mutex is dropped and re-taken during this call */
 		err = audit_add_watch(&entry->rule, &list);
@@ -1035,6 +1061,10 @@ int audit_del_rule(struct audit_entry *entry)
 		goto out;
 	}

+#ifdef CONFIG_AUDITSYSCALL
+    update_auditing_syscall(e->rule, false);
+#endif
+
 	if (e->rule.watch)
 		audit_remove_watch_rule(&e->rule);

diff --git a/kernel/auditsc.c b/kernel/auditsc.c
index ce8c9e2..90490aa 100644
--- a/kernel/auditsc.c
+++ b/kernel/auditsc.c
@@ -783,7 +783,7 @@ static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
 	return AUDIT_BUILD_CONTEXT;
 }

-static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
+int audit_in_mask(const struct audit_krule *rule, unsigned long val)
 {
 	int word, bit;

@@ -814,6 +814,9 @@ static enum audit_state audit_filter_syscall(struct task_struct *tsk,
 	if (auditd_test_task(tsk))
 		return AUDIT_DISABLED;

+	if (!auditing_syscall[ctx->major])
+		return AUDIT_BUILD_CONTEXT;
+
 	rcu_read_lock();
 	list_for_each_entry_rcu(e, list, list) {
 		if (audit_in_mask(&e->rule, ctx->major) &&
-- 
2.15.2

[-- Attachment #2: Type: text/plain, Size: 102 bytes --]

--
Linux-audit mailing list
Linux-audit@redhat.com
https://www.redhat.com/mailman/listinfo/linux-audit

             reply	other threads:[~2021-01-21 14:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-21 13:54 yang.yang29 [this message]
2021-01-22 17:02 ` [RFC, v2, 1/1] audit: speed up syscall rule match while exiting syscall Paul Moore

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202101212154272626110@zte.com.cn \
    --to=yang.yang29@zte.com.cn \
    --cc=linux-audit@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paul@paul-moore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).