Skip to content

Commit

Permalink
audit: Drop all unused chunk nodes during deletion
Browse files Browse the repository at this point in the history
When deleting chunk from a tree, drop all unused nodes in a chunk
instead of just the one used by the tree. This gets rid of possibly
lingering unused nodes (created due to fallback path in untag_chunk())
and also removes some special cases and will allow us to simplify
locking in untag_chunk().

Signed-off-by: Jan Kara <[email protected]>
Reviewed-by: Richard Guy Briggs <[email protected]>
Signed-off-by: Paul Moore <[email protected]>
  • Loading branch information
jankara authored and pcmoore committed Nov 12, 2018
1 parent 49a4ee7 commit c22fcde
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions kernel/audit_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,7 @@ static struct audit_chunk *find_chunk(struct node *p)
return container_of(p, struct audit_chunk, owners[0]);
}

static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old,
struct node *skip)
static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old)
{
struct audit_tree *owner;
int i, j;
Expand All @@ -288,7 +287,7 @@ static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old,
list_for_each_entry(owner, &new->trees, same_root)
owner->root = new;
for (i = j = 0; j < old->count; i++, j++) {
if (&old->owners[j] == skip) {
if (!old->owners[j].owner) {
i--;
continue;
}
Expand Down Expand Up @@ -322,20 +321,28 @@ static void remove_chunk_node(struct audit_chunk *chunk, struct node *p)
put_tree(owner);
}

static int chunk_count_trees(struct audit_chunk *chunk)
{
int i;
int ret = 0;

for (i = 0; i < chunk->count; i++)
if (chunk->owners[i].owner)
ret++;
return ret;
}

static void untag_chunk(struct node *p)
{
struct audit_chunk *chunk = find_chunk(p);
struct fsnotify_mark *entry = chunk->mark;
struct audit_chunk *new = NULL;
int size = chunk->count - 1;
int size;

remove_chunk_node(chunk, p);
fsnotify_get_mark(entry);
spin_unlock(&hash_lock);

if (size)
new = alloc_chunk(size);

mutex_lock(&entry->group->mark_mutex);
/*
* mark_mutex protects mark from getting detached and thus also from
Expand All @@ -348,6 +355,7 @@ static void untag_chunk(struct node *p)
goto out;
}

size = chunk_count_trees(chunk);
if (!size) {
chunk->dead = 1;
spin_lock(&hash_lock);
Expand All @@ -360,6 +368,7 @@ static void untag_chunk(struct node *p)
goto out;
}

new = alloc_chunk(size);
if (!new)
goto out_mutex;

Expand All @@ -375,7 +384,7 @@ static void untag_chunk(struct node *p)
* This has to go last when updating chunk as once replace_chunk() is
* called, new RCU readers can see the new chunk.
*/
replace_chunk(new, chunk, p);
replace_chunk(new, chunk);
spin_unlock(&hash_lock);
fsnotify_detach_mark(entry);
mutex_unlock(&entry->group->mark_mutex);
Expand Down Expand Up @@ -520,7 +529,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
* This has to go last when updating chunk as once replace_chunk() is
* called, new RCU readers can see the new chunk.
*/
replace_chunk(chunk, old, NULL);
replace_chunk(chunk, old);
spin_unlock(&hash_lock);
fsnotify_detach_mark(old_entry);
mutex_unlock(&audit_tree_group->mark_mutex);
Expand Down

0 comments on commit c22fcde

Please sign in to comment.