Skip to content

Commit 67daf27

Browse files
committed
audit: add filtering for io_uring records
This patch adds basic audit io_uring filtering, using as much of the existing audit filtering infrastructure as possible. In order to do this we reuse the audit filter rule's syscall mask for the io_uring operation and we create a new filter for io_uring operations as AUDIT_FILTER_URING_EXIT/audit_filter_list[7]. Thanks to Richard Guy Briggs for his review, feedback, and work on the corresponding audit userspace changes. Acked-by: Richard Guy Briggs <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 5bd2182 commit 67daf27

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

include/uapi/linux/audit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@
167167
#define AUDIT_FILTER_EXCLUDE 0x05 /* Apply rule before record creation */
168168
#define AUDIT_FILTER_TYPE AUDIT_FILTER_EXCLUDE /* obsolete misleading naming */
169169
#define AUDIT_FILTER_FS 0x06 /* Apply rule at __audit_inode_child */
170+
#define AUDIT_FILTER_URING_EXIT 0x07 /* Apply rule at io_uring op exit */
170171

171-
#define AUDIT_NR_FILTERS 7
172+
#define AUDIT_NR_FILTERS 8
172173

173174
#define AUDIT_FILTER_PREPEND 0x10 /* Prepend to front of list */
174175

kernel/audit_tree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
726726
{
727727

728728
if (pathname[0] != '/' ||
729-
rule->listnr != AUDIT_FILTER_EXIT ||
729+
(rule->listnr != AUDIT_FILTER_EXIT &&
730+
rule->listnr != AUDIT_FILTER_URING_EXIT) ||
730731
op != Audit_equal ||
731732
rule->inode_f || rule->watch || rule->tree)
732733
return -EINVAL;

kernel/audit_watch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
183183
return -EOPNOTSUPP;
184184

185185
if (path[0] != '/' || path[len-1] == '/' ||
186-
krule->listnr != AUDIT_FILTER_EXIT ||
186+
(krule->listnr != AUDIT_FILTER_EXIT &&
187+
krule->listnr != AUDIT_FILTER_URING_EXIT) ||
187188
op != Audit_equal ||
188189
krule->inode_f || krule->watch || krule->tree)
189190
return -EINVAL;

kernel/auditfilter.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
4444
LIST_HEAD_INIT(audit_filter_list[4]),
4545
LIST_HEAD_INIT(audit_filter_list[5]),
4646
LIST_HEAD_INIT(audit_filter_list[6]),
47-
#if AUDIT_NR_FILTERS != 7
47+
LIST_HEAD_INIT(audit_filter_list[7]),
48+
#if AUDIT_NR_FILTERS != 8
4849
#error Fix audit_filter_list initialiser
4950
#endif
5051
};
@@ -56,6 +57,7 @@ static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
5657
LIST_HEAD_INIT(audit_rules_list[4]),
5758
LIST_HEAD_INIT(audit_rules_list[5]),
5859
LIST_HEAD_INIT(audit_rules_list[6]),
60+
LIST_HEAD_INIT(audit_rules_list[7]),
5961
};
6062

6163
DEFINE_MUTEX(audit_filter_mutex);
@@ -151,7 +153,8 @@ char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
151153
static inline int audit_to_inode(struct audit_krule *krule,
152154
struct audit_field *f)
153155
{
154-
if (krule->listnr != AUDIT_FILTER_EXIT ||
156+
if ((krule->listnr != AUDIT_FILTER_EXIT &&
157+
krule->listnr != AUDIT_FILTER_URING_EXIT) ||
155158
krule->inode_f || krule->watch || krule->tree ||
156159
(f->op != Audit_equal && f->op != Audit_not_equal))
157160
return -EINVAL;
@@ -248,6 +251,7 @@ static inline struct audit_entry *audit_to_entry_common(struct audit_rule_data *
248251
pr_err("AUDIT_FILTER_ENTRY is deprecated\n");
249252
goto exit_err;
250253
case AUDIT_FILTER_EXIT:
254+
case AUDIT_FILTER_URING_EXIT:
251255
case AUDIT_FILTER_TASK:
252256
#endif
253257
case AUDIT_FILTER_USER:
@@ -332,6 +336,10 @@ static int audit_field_valid(struct audit_entry *entry, struct audit_field *f)
332336
if (entry->rule.listnr != AUDIT_FILTER_FS)
333337
return -EINVAL;
334338
break;
339+
case AUDIT_PERM:
340+
if (entry->rule.listnr == AUDIT_FILTER_URING_EXIT)
341+
return -EINVAL;
342+
break;
335343
}
336344

337345
switch (entry->rule.listnr) {
@@ -980,7 +988,8 @@ static inline int audit_add_rule(struct audit_entry *entry)
980988
}
981989

982990
entry->rule.prio = ~0ULL;
983-
if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
991+
if (entry->rule.listnr == AUDIT_FILTER_EXIT ||
992+
entry->rule.listnr == AUDIT_FILTER_URING_EXIT) {
984993
if (entry->rule.flags & AUDIT_FILTER_PREPEND)
985994
entry->rule.prio = ++prio_high;
986995
else

kernel/auditsc.c

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,34 @@ static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
805805
return rule->mask[word] & bit;
806806
}
807807

808+
/**
809+
* audit_filter_uring - apply filters to an io_uring operation
810+
* @tsk: associated task
811+
* @ctx: audit context
812+
*/
813+
static void audit_filter_uring(struct task_struct *tsk,
814+
struct audit_context *ctx)
815+
{
816+
struct audit_entry *e;
817+
enum audit_state state;
818+
819+
if (auditd_test_task(tsk))
820+
return;
821+
822+
rcu_read_lock();
823+
list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_URING_EXIT],
824+
list) {
825+
if (audit_in_mask(&e->rule, ctx->uring_op) &&
826+
audit_filter_rules(tsk, &e->rule, ctx, NULL, &state,
827+
false)) {
828+
rcu_read_unlock();
829+
ctx->current_state = state;
830+
return;
831+
}
832+
}
833+
rcu_read_unlock();
834+
}
835+
808836
/* At syscall exit time, this filter is called if the audit_state is
809837
* not low enough that auditing cannot take place, but is also not
810838
* high enough that we already know we have to write an audit record
@@ -1757,7 +1785,7 @@ static void audit_log_exit(void)
17571785
* __audit_free - free a per-task audit context
17581786
* @tsk: task whose audit context block to free
17591787
*
1760-
* Called from copy_process and do_exit
1788+
* Called from copy_process, do_exit, and the io_uring code
17611789
*/
17621790
void __audit_free(struct task_struct *tsk)
17631791
{
@@ -1775,15 +1803,21 @@ void __audit_free(struct task_struct *tsk)
17751803
* random task_struct that doesn't doesn't have any meaningful data we
17761804
* need to log via audit_log_exit().
17771805
*/
1778-
if (tsk == current && !context->dummy &&
1779-
context->context == AUDIT_CTX_SYSCALL) {
1806+
if (tsk == current && !context->dummy) {
17801807
context->return_valid = AUDITSC_INVALID;
17811808
context->return_code = 0;
1782-
1783-
audit_filter_syscall(tsk, context);
1784-
audit_filter_inodes(tsk, context);
1785-
if (context->current_state == AUDIT_STATE_RECORD)
1786-
audit_log_exit();
1809+
if (context->context == AUDIT_CTX_SYSCALL) {
1810+
audit_filter_syscall(tsk, context);
1811+
audit_filter_inodes(tsk, context);
1812+
if (context->current_state == AUDIT_STATE_RECORD)
1813+
audit_log_exit();
1814+
} else if (context->context == AUDIT_CTX_URING) {
1815+
/* TODO: verify this case is real and valid */
1816+
audit_filter_uring(tsk, context);
1817+
audit_filter_inodes(tsk, context);
1818+
if (context->current_state == AUDIT_STATE_RECORD)
1819+
audit_log_uring(context);
1820+
}
17871821
}
17881822

17891823
audit_set_context(tsk, NULL);
@@ -1867,12 +1901,6 @@ void __audit_uring_exit(int success, long code)
18671901
{
18681902
struct audit_context *ctx = audit_context();
18691903

1870-
/*
1871-
* TODO: At some point we will likely want to filter on io_uring ops
1872-
* and other things similar to what we do for syscalls, but that
1873-
* is something for another day; just record what we can here.
1874-
*/
1875-
18761904
if (ctx->context == AUDIT_CTX_SYSCALL) {
18771905
/*
18781906
* NOTE: See the note in __audit_uring_entry() about the case
@@ -1895,6 +1923,8 @@ void __audit_uring_exit(int success, long code)
18951923
* the behavior here.
18961924
*/
18971925
audit_filter_syscall(current, ctx);
1926+
if (ctx->current_state != AUDIT_STATE_RECORD)
1927+
audit_filter_uring(current, ctx);
18981928
audit_filter_inodes(current, ctx);
18991929
if (ctx->current_state != AUDIT_STATE_RECORD)
19001930
return;
@@ -1907,6 +1937,8 @@ void __audit_uring_exit(int success, long code)
19071937
if (!list_empty(&ctx->killed_trees))
19081938
audit_kill_trees(ctx);
19091939

1940+
/* run through both filters to ensure we set the filterkey properly */
1941+
audit_filter_uring(current, ctx);
19101942
audit_filter_inodes(current, ctx);
19111943
if (ctx->current_state != AUDIT_STATE_RECORD)
19121944
goto out;

0 commit comments

Comments
 (0)