Skip to content

Commit ce42363

Browse files
committed
audit: track the owner of the command mutex ourselves
Evidently the __mutex_owner() function was never intended for use outside the core mutex code, so build a thing locking wrapper around the mutex code which allows us to track the mutex owner. One, arguably positive, side effect is that this allows us to hide the audit_cmd_mutex inside of kernel/audit.c behind the lock/unlock functions. Reported-by: Peter Zijlstra <peterz@infradead.org> Reviewed-by: Richard Guy Briggs <rgb@redhat.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 23138ea commit ce42363

3 files changed

Lines changed: 61 additions & 16 deletions

File tree

‎kernel/audit.c‎

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,21 @@ static char *audit_feature_names[2] = {
181181
"loginuid_immutable",
182182
};
183183

184-
185-
/* Serialize requests from userspace. */
186-
DEFINE_MUTEX(audit_cmd_mutex);
184+
/**
185+
* struct audit_ctl_mutex - serialize requests from userspace
186+
* @lock: the mutex used for locking
187+
* @owner: the task which owns the lock
188+
*
189+
* Description:
190+
* This is the lock struct used to ensure we only process userspace requests
191+
* in an orderly fashion. We can't simply use a mutex/lock here because we
192+
* need to track lock ownership so we don't end up blocking the lock owner in
193+
* audit_log_start() or similar.
194+
*/
195+
static struct audit_ctl_mutex {
196+
struct mutex lock;
197+
void *owner;
198+
} audit_cmd_mutex;
187199

188200
/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
189201
* audit records. Since printk uses a 1024 byte buffer, this buffer
@@ -227,6 +239,36 @@ int auditd_test_task(struct task_struct *task)
227239
return rc;
228240
}
229241

242+
/**
243+
* audit_ctl_lock - Take the audit control lock
244+
*/
245+
void audit_ctl_lock(void)
246+
{
247+
mutex_lock(&audit_cmd_mutex.lock);
248+
audit_cmd_mutex.owner = current;
249+
}
250+
251+
/**
252+
* audit_ctl_unlock - Drop the audit control lock
253+
*/
254+
void audit_ctl_unlock(void)
255+
{
256+
audit_cmd_mutex.owner = NULL;
257+
mutex_unlock(&audit_cmd_mutex.lock);
258+
}
259+
260+
/**
261+
* audit_ctl_owner_current - Test to see if the current task owns the lock
262+
*
263+
* Description:
264+
* Return true if the current task owns the audit control lock, false if it
265+
* doesn't own the lock.
266+
*/
267+
static bool audit_ctl_owner_current(void)
268+
{
269+
return (current == audit_cmd_mutex.owner);
270+
}
271+
230272
/**
231273
* auditd_pid_vnr - Return the auditd PID relative to the namespace
232274
*
@@ -861,8 +903,8 @@ int audit_send_list(void *_dest)
861903
struct sock *sk = audit_get_sk(dest->net);
862904

863905
/* wait for parent to finish and send an ACK */
864-
mutex_lock(&audit_cmd_mutex);
865-
mutex_unlock(&audit_cmd_mutex);
906+
audit_ctl_lock();
907+
audit_ctl_unlock();
866908

867909
while ((skb = __skb_dequeue(&dest->q)) != NULL)
868910
netlink_unicast(sk, skb, dest->portid, 0);
@@ -903,8 +945,8 @@ static int audit_send_reply_thread(void *arg)
903945
struct audit_reply *reply = (struct audit_reply *)arg;
904946
struct sock *sk = audit_get_sk(reply->net);
905947

906-
mutex_lock(&audit_cmd_mutex);
907-
mutex_unlock(&audit_cmd_mutex);
948+
audit_ctl_lock();
949+
audit_ctl_unlock();
908950

909951
/* Ignore failure. It'll only happen if the sender goes away,
910952
because our timeout is set to infinite. */
@@ -1469,7 +1511,7 @@ static void audit_receive(struct sk_buff *skb)
14691511
nlh = nlmsg_hdr(skb);
14701512
len = skb->len;
14711513

1472-
mutex_lock(&audit_cmd_mutex);
1514+
audit_ctl_lock();
14731515
while (nlmsg_ok(nlh, len)) {
14741516
err = audit_receive_msg(skb, nlh);
14751517
/* if err or if this message says it wants a response */
@@ -1478,7 +1520,7 @@ static void audit_receive(struct sk_buff *skb)
14781520

14791521
nlh = nlmsg_next(nlh, &len);
14801522
}
1481-
mutex_unlock(&audit_cmd_mutex);
1523+
audit_ctl_unlock();
14821524
}
14831525

14841526
/* Run custom bind function on netlink socket group connect or bind requests. */
@@ -1550,6 +1592,9 @@ static int __init audit_init(void)
15501592
for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
15511593
INIT_LIST_HEAD(&audit_inode_hash[i]);
15521594

1595+
mutex_init(&audit_cmd_mutex.lock);
1596+
audit_cmd_mutex.owner = NULL;
1597+
15531598
pr_info("initializing netlink subsys (%s)\n",
15541599
audit_default ? "enabled" : "disabled");
15551600
register_pernet_subsys(&audit_net_ops);
@@ -1713,8 +1758,7 @@ struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
17131758
* using a PID anchored in the caller's namespace
17141759
* 2. generator holding the audit_cmd_mutex - we don't want to block
17151760
* while holding the mutex */
1716-
if (!(auditd_test_task(current) ||
1717-
(current == __mutex_owner(&audit_cmd_mutex)))) {
1761+
if (!(auditd_test_task(current) || audit_ctl_owner_current())) {
17181762
long stime = audit_backlog_wait_time;
17191763

17201764
while (audit_backlog_limit &&

‎kernel/audit.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,5 @@ extern struct list_head *audit_killed_trees(void);
341341
#define audit_filter_inodes(t,c) AUDIT_DISABLED
342342
#endif
343343

344-
extern struct mutex audit_cmd_mutex;
344+
extern void audit_ctl_lock(void);
345+
extern void audit_ctl_unlock(void);

‎kernel/audit_tree.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ static int prune_tree_thread(void *unused)
709709
schedule();
710710
}
711711

712-
mutex_lock(&audit_cmd_mutex);
712+
audit_ctl_lock();
713713
mutex_lock(&audit_filter_mutex);
714714

715715
while (!list_empty(&prune_list)) {
@@ -727,7 +727,7 @@ static int prune_tree_thread(void *unused)
727727
}
728728

729729
mutex_unlock(&audit_filter_mutex);
730-
mutex_unlock(&audit_cmd_mutex);
730+
audit_ctl_unlock();
731731
}
732732
return 0;
733733
}
@@ -924,7 +924,7 @@ static void audit_schedule_prune(void)
924924
*/
925925
void audit_kill_trees(struct list_head *list)
926926
{
927-
mutex_lock(&audit_cmd_mutex);
927+
audit_ctl_lock();
928928
mutex_lock(&audit_filter_mutex);
929929

930930
while (!list_empty(list)) {
@@ -942,7 +942,7 @@ void audit_kill_trees(struct list_head *list)
942942
}
943943

944944
mutex_unlock(&audit_filter_mutex);
945-
mutex_unlock(&audit_cmd_mutex);
945+
audit_ctl_unlock();
946946
}
947947

948948
/*

0 commit comments

Comments
 (0)