Skip to content

Commit be29d20

Browse files
jankarapcmoore
authored andcommitted
audit: Fix sleep in atomic
Audit tree code was happily adding new notification marks while holding spinlocks. Since fsnotify_add_mark() acquires group->mark_mutex this can lead to sleeping while holding a spinlock, deadlocks due to lock inversion, and probably other fun. Fix the problem by acquiring group->mark_mutex earlier. CC: Paul Moore <paul@paul-moore.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent e3ba730 commit be29d20

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎kernel/audit_tree.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,11 @@ static void untag_chunk(struct node *p)
231231
if (size)
232232
new = alloc_chunk(size);
233233

234+
mutex_lock(&entry->group->mark_mutex);
234235
spin_lock(&entry->lock);
235236
if (chunk->dead || !entry->inode) {
236237
spin_unlock(&entry->lock);
238+
mutex_unlock(&entry->group->mark_mutex);
237239
if (new)
238240
free_chunk(new);
239241
goto out;
@@ -251,15 +253,16 @@ static void untag_chunk(struct node *p)
251253
list_del_rcu(&chunk->hash);
252254
spin_unlock(&hash_lock);
253255
spin_unlock(&entry->lock);
256+
mutex_unlock(&entry->group->mark_mutex);
254257
fsnotify_destroy_mark(entry, audit_tree_group);
255258
goto out;
256259
}
257260

258261
if (!new)
259262
goto Fallback;
260263

261-
if (fsnotify_add_mark(&new->mark,
262-
entry->group, entry->inode, NULL, 1)) {
264+
if (fsnotify_add_mark_locked(&new->mark, entry->group, entry->inode,
265+
NULL, 1)) {
263266
fsnotify_put_mark(&new->mark);
264267
goto Fallback;
265268
}
@@ -293,6 +296,7 @@ static void untag_chunk(struct node *p)
293296
owner->root = new;
294297
spin_unlock(&hash_lock);
295298
spin_unlock(&entry->lock);
299+
mutex_unlock(&entry->group->mark_mutex);
296300
fsnotify_destroy_mark(entry, audit_tree_group);
297301
fsnotify_put_mark(&new->mark); /* drop initial reference */
298302
goto out;
@@ -309,6 +313,7 @@ static void untag_chunk(struct node *p)
309313
put_tree(owner);
310314
spin_unlock(&hash_lock);
311315
spin_unlock(&entry->lock);
316+
mutex_unlock(&entry->group->mark_mutex);
312317
out:
313318
fsnotify_put_mark(entry);
314319
spin_lock(&hash_lock);
@@ -386,18 +391,21 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
386391

387392
chunk_entry = &chunk->mark;
388393

394+
mutex_lock(&old_entry->group->mark_mutex);
389395
spin_lock(&old_entry->lock);
390396
if (!old_entry->inode) {
391397
/* old_entry is being shot, lets just lie */
392398
spin_unlock(&old_entry->lock);
399+
mutex_unlock(&old_entry->group->mark_mutex);
393400
fsnotify_put_mark(old_entry);
394401
free_chunk(chunk);
395402
return -ENOENT;
396403
}
397404

398-
if (fsnotify_add_mark(chunk_entry,
399-
old_entry->group, old_entry->inode, NULL, 1)) {
405+
if (fsnotify_add_mark_locked(chunk_entry, old_entry->group,
406+
old_entry->inode, NULL, 1)) {
400407
spin_unlock(&old_entry->lock);
408+
mutex_unlock(&old_entry->group->mark_mutex);
401409
fsnotify_put_mark(chunk_entry);
402410
fsnotify_put_mark(old_entry);
403411
return -ENOSPC;
@@ -413,6 +421,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
413421
chunk->dead = 1;
414422
spin_unlock(&chunk_entry->lock);
415423
spin_unlock(&old_entry->lock);
424+
mutex_unlock(&old_entry->group->mark_mutex);
416425

417426
fsnotify_destroy_mark(chunk_entry, audit_tree_group);
418427

@@ -445,6 +454,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
445454
spin_unlock(&hash_lock);
446455
spin_unlock(&chunk_entry->lock);
447456
spin_unlock(&old_entry->lock);
457+
mutex_unlock(&old_entry->group->mark_mutex);
448458
fsnotify_destroy_mark(old_entry, audit_tree_group);
449459
fsnotify_put_mark(chunk_entry); /* drop initial reference */
450460
fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */

0 commit comments

Comments
 (0)