@@ -112,18 +112,19 @@ struct audit_net {
112112 * @pid: auditd PID
113113 * @portid: netlink portid
114114 * @net: the associated network namespace
115- * @lock: spinlock to protect write access
115+ * @rcu: RCU head
116116 *
117117 * Description:
118118 * This struct is RCU protected; you must either hold the RCU lock for reading
119- * or the included spinlock for writing.
119+ * or the associated spinlock for writing.
120120 */
121121static struct auditd_connection {
122122 struct pid * pid ;
123123 u32 portid ;
124124 struct net * net ;
125- spinlock_t lock ;
126- } auditd_conn ;
125+ struct rcu_head rcu ;
126+ } * auditd_conn = NULL ;
127+ static DEFINE_SPINLOCK (auditd_conn_lock );
127128
128129/* If audit_rate_limit is non-zero, limit the rate of sending audit records
129130 * to that number per second. This prevents DoS attacks, but results in
@@ -215,32 +216,33 @@ struct audit_reply {
215216int auditd_test_task (struct task_struct * task )
216217{
217218 int rc ;
219+ struct auditd_connection * ac ;
218220
219221 rcu_read_lock ();
220- rc = (auditd_conn .pid && auditd_conn .pid == task_tgid (task ) ? 1 : 0 );
222+ ac = rcu_dereference (auditd_conn );
223+ rc = (ac && ac -> pid == task_tgid (task ) ? 1 : 0 );
221224 rcu_read_unlock ();
222225
223226 return rc ;
224227}
225228
226229/**
227230 * auditd_pid_vnr - Return the auditd PID relative to the namespace
228- * @auditd: the auditd connection
229231 *
230232 * Description:
231- * Returns the PID in relation to the namespace, 0 on failure. This function
232- * takes the RCU read lock internally, but if the caller needs to protect the
233- * auditd_connection pointer it should take the RCU read lock as well.
233+ * Returns the PID in relation to the namespace, 0 on failure.
234234 */
235- static pid_t auditd_pid_vnr (const struct auditd_connection * auditd )
235+ static pid_t auditd_pid_vnr (void )
236236{
237237 pid_t pid ;
238+ const struct auditd_connection * ac ;
238239
239240 rcu_read_lock ();
240- if (!auditd || !auditd -> pid )
241+ ac = rcu_dereference (auditd_conn );
242+ if (!ac || !ac -> pid )
241243 pid = 0 ;
242244 else
243- pid = pid_vnr (auditd -> pid );
245+ pid = pid_vnr (ac -> pid );
244246 rcu_read_unlock ();
245247
246248 return pid ;
@@ -433,6 +435,24 @@ static int audit_set_failure(u32 state)
433435 return audit_do_config_change ("audit_failure" , & audit_failure , state );
434436}
435437
438+ /**
439+ * auditd_conn_free - RCU helper to release an auditd connection struct
440+ * @rcu: RCU head
441+ *
442+ * Description:
443+ * Drop any references inside the auditd connection tracking struct and free
444+ * the memory.
445+ */
446+ static void auditd_conn_free (struct rcu_head * rcu )
447+ {
448+ struct auditd_connection * ac ;
449+
450+ ac = container_of (rcu , struct auditd_connection , rcu );
451+ put_pid (ac -> pid );
452+ put_net (ac -> net );
453+ kfree (ac );
454+ }
455+
436456/**
437457 * auditd_set - Set/Reset the auditd connection state
438458 * @pid: auditd PID
@@ -441,27 +461,33 @@ static int audit_set_failure(u32 state)
441461 *
442462 * Description:
443463 * This function will obtain and drop network namespace references as
444- * necessary.
464+ * necessary. Returns zero on success, negative values on failure.
445465 */
446- static void auditd_set (struct pid * pid , u32 portid , struct net * net )
466+ static int auditd_set (struct pid * pid , u32 portid , struct net * net )
447467{
448468 unsigned long flags ;
469+ struct auditd_connection * ac_old , * ac_new ;
449470
450- spin_lock_irqsave (& auditd_conn .lock , flags );
451- if (auditd_conn .pid )
452- put_pid (auditd_conn .pid );
453- if (pid )
454- auditd_conn .pid = get_pid (pid );
455- else
456- auditd_conn .pid = NULL ;
457- auditd_conn .portid = portid ;
458- if (auditd_conn .net )
459- put_net (auditd_conn .net );
460- if (net )
461- auditd_conn .net = get_net (net );
462- else
463- auditd_conn .net = NULL ;
464- spin_unlock_irqrestore (& auditd_conn .lock , flags );
471+ if (!pid || !net )
472+ return - EINVAL ;
473+
474+ ac_new = kzalloc (sizeof (* ac_new ), GFP_KERNEL );
475+ if (!ac_new )
476+ return - ENOMEM ;
477+ ac_new -> pid = get_pid (pid );
478+ ac_new -> portid = portid ;
479+ ac_new -> net = get_net (net );
480+
481+ spin_lock_irqsave (& auditd_conn_lock , flags );
482+ ac_old = rcu_dereference_protected (auditd_conn ,
483+ lockdep_is_held (& auditd_conn_lock ));
484+ rcu_assign_pointer (auditd_conn , ac_new );
485+ spin_unlock_irqrestore (& auditd_conn_lock , flags );
486+
487+ if (ac_old )
488+ call_rcu (& ac_old -> rcu , auditd_conn_free );
489+
490+ return 0 ;
465491}
466492
467493/**
@@ -556,13 +582,19 @@ static void kauditd_retry_skb(struct sk_buff *skb)
556582 */
557583static void auditd_reset (void )
558584{
585+ unsigned long flags ;
559586 struct sk_buff * skb ;
587+ struct auditd_connection * ac_old ;
560588
561589 /* if it isn't already broken, break the connection */
562- rcu_read_lock ();
563- if (auditd_conn .pid )
564- auditd_set (0 , 0 , NULL );
565- rcu_read_unlock ();
590+ spin_lock_irqsave (& auditd_conn_lock , flags );
591+ ac_old = rcu_dereference_protected (auditd_conn ,
592+ lockdep_is_held (& auditd_conn_lock ));
593+ rcu_assign_pointer (auditd_conn , NULL );
594+ spin_unlock_irqrestore (& auditd_conn_lock , flags );
595+
596+ if (ac_old )
597+ call_rcu (& ac_old -> rcu , auditd_conn_free );
566598
567599 /* flush all of the main and retry queues to the hold queue */
568600 while ((skb = skb_dequeue (& audit_retry_queue )))
@@ -588,6 +620,7 @@ static int auditd_send_unicast_skb(struct sk_buff *skb)
588620 u32 portid ;
589621 struct net * net ;
590622 struct sock * sk ;
623+ struct auditd_connection * ac ;
591624
592625 /* NOTE: we can't call netlink_unicast while in the RCU section so
593626 * take a reference to the network namespace and grab local
@@ -597,15 +630,15 @@ static int auditd_send_unicast_skb(struct sk_buff *skb)
597630 * section netlink_unicast() should safely return an error */
598631
599632 rcu_read_lock ();
600- if (!auditd_conn .pid ) {
633+ ac = rcu_dereference (auditd_conn );
634+ if (!ac ) {
601635 rcu_read_unlock ();
602636 rc = - ECONNREFUSED ;
603637 goto err ;
604638 }
605- net = auditd_conn .net ;
606- get_net (net );
639+ net = get_net (ac -> net );
607640 sk = audit_get_sk (net );
608- portid = auditd_conn . portid ;
641+ portid = ac -> portid ;
609642 rcu_read_unlock ();
610643
611644 rc = netlink_unicast (sk , skb , portid , 0 );
@@ -740,21 +773,22 @@ static int kauditd_thread(void *dummy)
740773 u32 portid = 0 ;
741774 struct net * net = NULL ;
742775 struct sock * sk = NULL ;
776+ struct auditd_connection * ac ;
743777
744778#define UNICAST_RETRIES 5
745779
746780 set_freezable ();
747781 while (!kthread_should_stop ()) {
748782 /* NOTE: see the lock comments in auditd_send_unicast_skb() */
749783 rcu_read_lock ();
750- if (!auditd_conn .pid ) {
784+ ac = rcu_dereference (auditd_conn );
785+ if (!ac ) {
751786 rcu_read_unlock ();
752787 goto main_queue ;
753788 }
754- net = auditd_conn .net ;
755- get_net (net );
789+ net = get_net (ac -> net );
756790 sk = audit_get_sk (net );
757- portid = auditd_conn . portid ;
791+ portid = ac -> portid ;
758792 rcu_read_unlock ();
759793
760794 /* attempt to flush the hold queue */
@@ -1117,7 +1151,7 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
11171151 s .failure = audit_failure ;
11181152 /* NOTE: use pid_vnr() so the PID is relative to the current
11191153 * namespace */
1120- s .pid = auditd_pid_vnr (& auditd_conn );
1154+ s .pid = auditd_pid_vnr ();
11211155 s .rate_limit = audit_rate_limit ;
11221156 s .backlog_limit = audit_backlog_limit ;
11231157 s .lost = atomic_read (& audit_lost );
@@ -1160,7 +1194,7 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
11601194 /* test the auditd connection */
11611195 audit_replace (req_pid );
11621196
1163- auditd_pid = auditd_pid_vnr (& auditd_conn );
1197+ auditd_pid = auditd_pid_vnr ();
11641198 /* only the current auditd can unregister itself */
11651199 if ((!new_pid ) && (new_pid != auditd_pid )) {
11661200 audit_log_config_change ("audit_pid" , new_pid ,
@@ -1174,19 +1208,30 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
11741208 return - EEXIST ;
11751209 }
11761210
1177- if (audit_enabled != AUDIT_OFF )
1178- audit_log_config_change ("audit_pid" , new_pid ,
1179- auditd_pid , 1 );
1180-
11811211 if (new_pid ) {
11821212 /* register a new auditd connection */
1183- auditd_set (req_pid , NETLINK_CB (skb ).portid ,
1184- sock_net (NETLINK_CB (skb ).sk ));
1213+ err = auditd_set (req_pid ,
1214+ NETLINK_CB (skb ).portid ,
1215+ sock_net (NETLINK_CB (skb ).sk ));
1216+ if (audit_enabled != AUDIT_OFF )
1217+ audit_log_config_change ("audit_pid" ,
1218+ new_pid ,
1219+ auditd_pid ,
1220+ err ? 0 : 1 );
1221+ if (err )
1222+ return err ;
1223+
11851224 /* try to process any backlog */
11861225 wake_up_interruptible (& kauditd_wait );
1187- } else
1226+ } else {
1227+ if (audit_enabled != AUDIT_OFF )
1228+ audit_log_config_change ("audit_pid" ,
1229+ new_pid ,
1230+ auditd_pid , 1 );
1231+
11881232 /* unregister the auditd connection */
11891233 auditd_reset ();
1234+ }
11901235 }
11911236 if (s .mask & AUDIT_STATUS_RATE_LIMIT ) {
11921237 err = audit_set_rate_limit (s .rate_limit );
@@ -1454,10 +1499,11 @@ static void __net_exit audit_net_exit(struct net *net)
14541499{
14551500 struct audit_net * aunet = net_generic (net , audit_net_id );
14561501
1457- rcu_read_lock ();
1458- if (net == auditd_conn .net )
1459- auditd_reset ();
1460- rcu_read_unlock ();
1502+ /* NOTE: you would think that we would want to check the auditd
1503+ * connection and potentially reset it here if it lives in this
1504+ * namespace, but since the auditd connection tracking struct holds a
1505+ * reference to this namespace (see auditd_set()) we are only ever
1506+ * going to get here after that connection has been released */
14611507
14621508 netlink_kernel_release (aunet -> sk );
14631509}
@@ -1481,9 +1527,6 @@ static int __init audit_init(void)
14811527 sizeof (struct audit_buffer ),
14821528 0 , SLAB_PANIC , NULL );
14831529
1484- memset (& auditd_conn , 0 , sizeof (auditd_conn ));
1485- spin_lock_init (& auditd_conn .lock );
1486-
14871530 skb_queue_head_init (& audit_queue );
14881531 skb_queue_head_init (& audit_retry_queue );
14891532 skb_queue_head_init (& audit_hold_queue );
0 commit comments