@@ -27,7 +27,6 @@ struct audit_chunk {
2727 unsigned long key ;
2828 struct fsnotify_mark * mark ;
2929 struct list_head trees ; /* with root here */
30- int dead ;
3130 int count ;
3231 atomic_long_t refs ;
3332 struct rcu_head head ;
@@ -48,8 +47,15 @@ static LIST_HEAD(prune_list);
4847static struct task_struct * prune_thread ;
4948
5049/*
51- * One struct chunk is attached to each inode of interest.
52- * We replace struct chunk on tagging/untagging.
50+ * One struct chunk is attached to each inode of interest through
51+ * audit_tree_mark (fsnotify mark). We replace struct chunk on tagging /
52+ * untagging, the mark is stable as long as there is chunk attached. The
53+ * association between mark and chunk is protected by hash_lock and
54+ * audit_tree_group->mark_mutex. Thus as long as we hold
55+ * audit_tree_group->mark_mutex and check that the mark is alive by
56+ * FSNOTIFY_MARK_FLAG_ATTACHED flag check, we are sure the mark points to
57+ * the current chunk.
58+ *
5359 * Rules have pointer to struct audit_tree.
5460 * Rules have struct list_head rlist forming a list of rules over
5561 * the same tree.
@@ -68,8 +74,12 @@ static struct task_struct *prune_thread;
6874 * tree is refcounted; one reference for "some rules on rules_list refer to
6975 * it", one for each chunk with pointer to it.
7076 *
71- * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount
72- * of watch contributes 1 to .refs).
77+ * chunk is refcounted by embedded .refs. Mark associated with the chunk holds
78+ * one chunk reference. This reference is dropped either when a mark is going
79+ * to be freed (corresponding inode goes away) or when chunk attached to the
80+ * mark gets replaced. This reference must be dropped using
81+ * audit_mark_put_chunk() to make sure the reference is dropped only after RCU
82+ * grace period as it protects RCU readers of the hash table.
7383 *
7484 * node.index allows to get from node.list to containing chunk.
7585 * MSB of that sucker is stolen to mark taggings that we might have to
@@ -160,8 +170,6 @@ static struct audit_chunk *mark_chunk(struct fsnotify_mark *mark)
160170
161171static void audit_tree_destroy_watch (struct fsnotify_mark * entry )
162172{
163- struct audit_chunk * chunk = mark_chunk (entry );
164- audit_mark_put_chunk (chunk );
165173 kmem_cache_free (audit_tree_mark_cachep , audit_mark (entry ));
166174}
167175
@@ -188,13 +196,6 @@ static struct audit_chunk *alloc_chunk(int count)
188196 if (!chunk )
189197 return NULL ;
190198
191- chunk -> mark = alloc_mark ();
192- if (!chunk -> mark ) {
193- kfree (chunk );
194- return NULL ;
195- }
196- audit_mark (chunk -> mark )-> chunk = chunk ;
197-
198199 INIT_LIST_HEAD (& chunk -> hash );
199200 INIT_LIST_HEAD (& chunk -> trees );
200201 chunk -> count = count ;
@@ -277,6 +278,20 @@ static struct audit_chunk *find_chunk(struct node *p)
277278 return container_of (p , struct audit_chunk , owners [0 ]);
278279}
279280
281+ static void replace_mark_chunk (struct fsnotify_mark * entry ,
282+ struct audit_chunk * chunk )
283+ {
284+ struct audit_chunk * old ;
285+
286+ assert_spin_locked (& hash_lock );
287+ old = mark_chunk (entry );
288+ audit_mark (entry )-> chunk = chunk ;
289+ if (chunk )
290+ chunk -> mark = entry ;
291+ if (old )
292+ old -> mark = NULL ;
293+ }
294+
280295static void replace_chunk (struct audit_chunk * new , struct audit_chunk * old )
281296{
282297 struct audit_tree * owner ;
@@ -299,6 +314,7 @@ static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old)
299314 get_tree (owner );
300315 list_replace_init (& old -> owners [j ].list , & new -> owners [i ].list );
301316 }
317+ replace_mark_chunk (old -> mark , new );
302318 /*
303319 * Make sure chunk is fully initialized before making it visible in the
304320 * hash. Pairs with a data dependency barrier in READ_ONCE() in
@@ -339,21 +355,23 @@ static void untag_chunk(struct audit_chunk *chunk, struct fsnotify_mark *entry)
339355
340356 mutex_lock (& audit_tree_group -> mark_mutex );
341357 /*
342- * mark_mutex protects mark from getting detached and thus also from
343- * mark->connector->obj getting NULL .
358+ * mark_mutex stabilizes chunk attached to the mark so we can check
359+ * whether it didn't change while we've dropped hash_lock .
344360 */
345- if (chunk -> dead || !(entry -> flags & FSNOTIFY_MARK_FLAG_ATTACHED ))
361+ if (!(entry -> flags & FSNOTIFY_MARK_FLAG_ATTACHED ) ||
362+ mark_chunk (entry ) != chunk )
346363 goto out_mutex ;
347364
348365 size = chunk_count_trees (chunk );
349366 if (!size ) {
350- chunk -> dead = 1 ;
351367 spin_lock (& hash_lock );
352368 list_del_init (& chunk -> trees );
353369 list_del_rcu (& chunk -> hash );
370+ replace_mark_chunk (entry , NULL );
354371 spin_unlock (& hash_lock );
355372 fsnotify_detach_mark (entry );
356373 mutex_unlock (& audit_tree_group -> mark_mutex );
374+ audit_mark_put_chunk (chunk );
357375 fsnotify_free_mark (entry );
358376 return ;
359377 }
@@ -362,24 +380,15 @@ static void untag_chunk(struct audit_chunk *chunk, struct fsnotify_mark *entry)
362380 if (!new )
363381 goto out_mutex ;
364382
365- if (fsnotify_add_mark_locked (new -> mark , entry -> connector -> obj ,
366- FSNOTIFY_OBJ_TYPE_INODE , 1 )) {
367- fsnotify_put_mark (new -> mark );
368- goto out_mutex ;
369- }
370-
371- chunk -> dead = 1 ;
372383 spin_lock (& hash_lock );
373384 /*
374385 * This has to go last when updating chunk as once replace_chunk() is
375386 * called, new RCU readers can see the new chunk.
376387 */
377388 replace_chunk (new , chunk );
378389 spin_unlock (& hash_lock );
379- fsnotify_detach_mark (entry );
380390 mutex_unlock (& audit_tree_group -> mark_mutex );
381- fsnotify_free_mark (entry );
382- fsnotify_put_mark (new -> mark ); /* drop initial reference */
391+ audit_mark_put_chunk (chunk );
383392 return ;
384393
385394out_mutex :
@@ -397,23 +406,31 @@ static int create_chunk(struct inode *inode, struct audit_tree *tree)
397406 return - ENOMEM ;
398407 }
399408
400- entry = chunk -> mark ;
409+ entry = alloc_mark ();
410+ if (!entry ) {
411+ mutex_unlock (& audit_tree_group -> mark_mutex );
412+ kfree (chunk );
413+ return - ENOMEM ;
414+ }
415+
401416 if (fsnotify_add_inode_mark_locked (entry , inode , 0 )) {
402417 mutex_unlock (& audit_tree_group -> mark_mutex );
403418 fsnotify_put_mark (entry );
419+ kfree (chunk );
404420 return - ENOSPC ;
405421 }
406422
407423 spin_lock (& hash_lock );
408424 if (tree -> goner ) {
409425 spin_unlock (& hash_lock );
410- chunk -> dead = 1 ;
411426 fsnotify_detach_mark (entry );
412427 mutex_unlock (& audit_tree_group -> mark_mutex );
413428 fsnotify_free_mark (entry );
414429 fsnotify_put_mark (entry );
430+ kfree (chunk );
415431 return 0 ;
416432 }
433+ replace_mark_chunk (entry , chunk );
417434 chunk -> owners [0 ].index = (1U << 31 );
418435 chunk -> owners [0 ].owner = tree ;
419436 get_tree (tree );
@@ -430,33 +447,41 @@ static int create_chunk(struct inode *inode, struct audit_tree *tree)
430447 insert_hash (chunk );
431448 spin_unlock (& hash_lock );
432449 mutex_unlock (& audit_tree_group -> mark_mutex );
433- fsnotify_put_mark (entry ); /* drop initial reference */
450+ /*
451+ * Drop our initial reference. When mark we point to is getting freed,
452+ * we get notification through ->freeing_mark callback and cleanup
453+ * chunk pointing to this mark.
454+ */
455+ fsnotify_put_mark (entry );
434456 return 0 ;
435457}
436458
437459/* the first tagged inode becomes root of tree */
438460static int tag_chunk (struct inode * inode , struct audit_tree * tree )
439461{
440- struct fsnotify_mark * old_entry , * chunk_entry ;
462+ struct fsnotify_mark * entry ;
441463 struct audit_chunk * chunk , * old ;
442464 struct node * p ;
443465 int n ;
444466
445467 mutex_lock (& audit_tree_group -> mark_mutex );
446- old_entry = fsnotify_find_mark (& inode -> i_fsnotify_marks ,
447- audit_tree_group );
448- if (!old_entry )
468+ entry = fsnotify_find_mark (& inode -> i_fsnotify_marks , audit_tree_group );
469+ if (!entry )
449470 return create_chunk (inode , tree );
450471
451- old = mark_chunk (old_entry );
452-
472+ /*
473+ * Found mark is guaranteed to be attached and mark_mutex protects mark
474+ * from getting detached and thus it makes sure there is chunk attached
475+ * to the mark.
476+ */
453477 /* are we already there? */
454478 spin_lock (& hash_lock );
479+ old = mark_chunk (entry );
455480 for (n = 0 ; n < old -> count ; n ++ ) {
456481 if (old -> owners [n ].owner == tree ) {
457482 spin_unlock (& hash_lock );
458483 mutex_unlock (& audit_tree_group -> mark_mutex );
459- fsnotify_put_mark (old_entry );
484+ fsnotify_put_mark (entry );
460485 return 0 ;
461486 }
462487 }
@@ -465,49 +490,23 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
465490 chunk = alloc_chunk (old -> count + 1 );
466491 if (!chunk ) {
467492 mutex_unlock (& audit_tree_group -> mark_mutex );
468- fsnotify_put_mark (old_entry );
493+ fsnotify_put_mark (entry );
469494 return - ENOMEM ;
470495 }
471496
472- chunk_entry = chunk -> mark ;
473-
474- /*
475- * mark_mutex protects mark from getting detached and thus also from
476- * mark->connector->obj getting NULL.
477- */
478- if (!(old_entry -> flags & FSNOTIFY_MARK_FLAG_ATTACHED )) {
479- /* old_entry is being shot, lets just lie */
480- mutex_unlock (& audit_tree_group -> mark_mutex );
481- fsnotify_put_mark (old_entry );
482- fsnotify_put_mark (chunk -> mark );
483- return - ENOENT ;
484- }
485-
486- if (fsnotify_add_mark_locked (chunk_entry , old_entry -> connector -> obj ,
487- FSNOTIFY_OBJ_TYPE_INODE , 1 )) {
488- mutex_unlock (& audit_tree_group -> mark_mutex );
489- fsnotify_put_mark (chunk_entry );
490- fsnotify_put_mark (old_entry );
491- return - ENOSPC ;
492- }
493-
494497 spin_lock (& hash_lock );
495498 if (tree -> goner ) {
496499 spin_unlock (& hash_lock );
497- chunk -> dead = 1 ;
498- fsnotify_detach_mark (chunk_entry );
499500 mutex_unlock (& audit_tree_group -> mark_mutex );
500- fsnotify_free_mark (chunk_entry );
501- fsnotify_put_mark (chunk_entry );
502- fsnotify_put_mark (old_entry );
501+ fsnotify_put_mark (entry );
502+ kfree (chunk );
503503 return 0 ;
504504 }
505505 p = & chunk -> owners [chunk -> count - 1 ];
506506 p -> index = (chunk -> count - 1 ) | (1U <<31 );
507507 p -> owner = tree ;
508508 get_tree (tree );
509509 list_add (& p -> list , & tree -> chunks );
510- old -> dead = 1 ;
511510 if (!tree -> root ) {
512511 tree -> root = chunk ;
513512 list_add (& tree -> same_root , & chunk -> trees );
@@ -518,11 +517,10 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
518517 */
519518 replace_chunk (chunk , old );
520519 spin_unlock (& hash_lock );
521- fsnotify_detach_mark (old_entry );
522520 mutex_unlock (& audit_tree_group -> mark_mutex );
523- fsnotify_free_mark ( old_entry );
524- fsnotify_put_mark ( chunk_entry ); /* drop initial reference */
525- fsnotify_put_mark ( old_entry ); /* pair to fsnotify_find mark_entry */
521+ fsnotify_put_mark ( entry ); /* pair to fsnotify_find mark_entry */
522+ audit_mark_put_chunk ( old );
523+
526524 return 0 ;
527525}
528526
@@ -585,6 +583,9 @@ static void prune_tree_chunks(struct audit_tree *victim, bool tagged)
585583 chunk = find_chunk (p );
586584 mark = chunk -> mark ;
587585 remove_chunk_node (chunk , p );
586+ /* Racing with audit_tree_freeing_mark()? */
587+ if (!mark )
588+ continue ;
588589 fsnotify_get_mark (mark );
589590 spin_unlock (& hash_lock );
590591
@@ -1007,10 +1008,6 @@ static void evict_chunk(struct audit_chunk *chunk)
10071008 int need_prune = 0 ;
10081009 int n ;
10091010
1010- if (chunk -> dead )
1011- return ;
1012-
1013- chunk -> dead = 1 ;
10141011 mutex_lock (& audit_filter_mutex );
10151012 spin_lock (& hash_lock );
10161013 while (!list_empty (& chunk -> trees )) {
@@ -1049,9 +1046,18 @@ static int audit_tree_handle_event(struct fsnotify_group *group,
10491046
10501047static void audit_tree_freeing_mark (struct fsnotify_mark * entry , struct fsnotify_group * group )
10511048{
1052- struct audit_chunk * chunk = mark_chunk ( entry ) ;
1049+ struct audit_chunk * chunk ;
10531050
1054- evict_chunk (chunk );
1051+ mutex_lock (& entry -> group -> mark_mutex );
1052+ spin_lock (& hash_lock );
1053+ chunk = mark_chunk (entry );
1054+ replace_mark_chunk (entry , NULL );
1055+ spin_unlock (& hash_lock );
1056+ mutex_unlock (& entry -> group -> mark_mutex );
1057+ if (chunk ) {
1058+ evict_chunk (chunk );
1059+ audit_mark_put_chunk (chunk );
1060+ }
10551061
10561062 /*
10571063 * We are guaranteed to have at least one reference to the mark from
0 commit comments