Skip to content

Commit 6248a06

Browse files
committed
pstore: Add proper unregister lock checking
The pstore backend lock wasn't being used during pstore_unregister(). Add sanity check and locking. Link: https://lore.kernel.org/lkml/[email protected]/ Signed-off-by: Kees Cook <[email protected]>
1 parent db23491 commit 6248a06

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

fs/pstore/platform.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ static void pstore_dowork(struct work_struct *);
6969
static DECLARE_WORK(pstore_work, pstore_dowork);
7070

7171
/*
72-
* psinfo_lock just protects "psinfo" during
73-
* calls to pstore_register()
72+
* psinfo_lock protects "psinfo" during calls to
73+
* pstore_register(), pstore_unregister(), and
74+
* the filesystem mount/unmount routines.
7475
*/
7576
static DEFINE_MUTEX(psinfo_lock);
7677
struct pstore_info *psinfo;
@@ -587,8 +588,6 @@ int pstore_register(struct pstore_info *psi)
587588
psinfo = psi;
588589
mutex_init(&psinfo->read_mutex);
589590
sema_init(&psinfo->buf_lock, 1);
590-
mutex_unlock(&psinfo_lock);
591-
592591

593592
if (psi->flags & PSTORE_FLAGS_DMESG)
594593
allocate_buf_for_compression();
@@ -620,12 +619,25 @@ int pstore_register(struct pstore_info *psi)
620619

621620
pr_info("Registered %s as persistent store backend\n", psi->name);
622621

622+
mutex_unlock(&psinfo_lock);
623623
return 0;
624624
}
625625
EXPORT_SYMBOL_GPL(pstore_register);
626626

627627
void pstore_unregister(struct pstore_info *psi)
628628
{
629+
/* It's okay to unregister nothing. */
630+
if (!psi)
631+
return;
632+
633+
mutex_lock(&psinfo_lock);
634+
635+
/* Only one backend can be registered at a time. */
636+
if (WARN_ON(psi != psinfo)) {
637+
mutex_unlock(&psinfo_lock);
638+
return;
639+
}
640+
629641
/* Stop timer and make sure all work has finished. */
630642
pstore_update_ms = -1;
631643
del_timer_sync(&pstore_timer);
@@ -644,6 +656,7 @@ void pstore_unregister(struct pstore_info *psi)
644656

645657
psinfo = NULL;
646658
backend = NULL;
659+
mutex_unlock(&psinfo_lock);
647660
}
648661
EXPORT_SYMBOL_GPL(pstore_unregister);
649662

0 commit comments

Comments
 (0)