@@ -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 &&
0 commit comments