Skip to content

Commit 27e5041

Browse files
committed
pstore: Add locking around superblock changes
Nothing was protecting changes to the pstorefs superblock. Add locking and refactor away is_pstore_mounted(), instead using a helper to add a way to safely lock the pstorefs root inode during filesystem changes. Link: https://lore.kernel.org/lkml/[email protected]/ Signed-off-by: Kees Cook <[email protected]>
1 parent 7a0ad54 commit 27e5041

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

fs/pstore/inode.c

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
static DEFINE_MUTEX(records_list_lock);
3232
static LIST_HEAD(records_list);
3333

34+
static DEFINE_MUTEX(pstore_sb_lock);
35+
static struct super_block *pstore_sb;
36+
3437
struct pstore_private {
3538
struct list_head list;
3639
struct pstore_record *record;
@@ -282,11 +285,25 @@ static const struct super_operations pstore_ops = {
282285
.show_options = pstore_show_options,
283286
};
284287

285-
static struct super_block *pstore_sb;
286-
287-
bool pstore_is_mounted(void)
288+
static struct dentry *psinfo_lock_root(void)
288289
{
289-
return pstore_sb != NULL;
290+
struct dentry *root;
291+
292+
mutex_lock(&pstore_sb_lock);
293+
/*
294+
* Having no backend is fine -- no records appear.
295+
* Not being mounted is fine -- nothing to do.
296+
*/
297+
if (!psinfo || !pstore_sb) {
298+
mutex_unlock(&pstore_sb_lock);
299+
return NULL;
300+
}
301+
302+
root = pstore_sb->s_root;
303+
inode_lock(d_inode(root));
304+
mutex_unlock(&pstore_sb_lock);
305+
306+
return root;
290307
}
291308

292309
/*
@@ -303,20 +320,18 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
303320
struct pstore_private *private, *pos;
304321
size_t size = record->size + record->ecc_notice_size;
305322

306-
WARN_ON(!inode_is_locked(d_inode(root)));
323+
if (WARN_ON(!inode_is_locked(d_inode(root))))
324+
return -EINVAL;
307325

326+
rc = -EEXIST;
327+
/* Skip records that are already present in the filesystem. */
308328
mutex_lock(&records_list_lock);
309329
list_for_each_entry(pos, &records_list, list) {
310330
if (pos->record->type == record->type &&
311331
pos->record->id == record->id &&
312-
pos->record->psi == record->psi) {
313-
rc = -EEXIST;
314-
break;
315-
}
332+
pos->record->psi == record->psi)
333+
goto fail;
316334
}
317-
mutex_unlock(&records_list_lock);
318-
if (rc)
319-
return rc;
320335

321336
rc = -ENOMEM;
322337
inode = pstore_get_inode(root->d_sb);
@@ -346,7 +361,6 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
346361

347362
d_add(dentry, inode);
348363

349-
mutex_lock(&records_list_lock);
350364
list_add(&private->list, &records_list);
351365
mutex_unlock(&records_list_lock);
352366

@@ -356,8 +370,8 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
356370
free_pstore_private(private);
357371
fail_inode:
358372
iput(inode);
359-
360373
fail:
374+
mutex_unlock(&records_list_lock);
361375
return rc;
362376
}
363377

@@ -369,25 +383,20 @@ int pstore_mkfile(struct dentry *root, struct pstore_record *record)
369383
*/
370384
void pstore_get_records(int quiet)
371385
{
372-
struct pstore_info *psi = psinfo;
373386
struct dentry *root;
374387

375-
if (!psi || !pstore_sb)
388+
root = psinfo_lock_root();
389+
if (!root)
376390
return;
377391

378-
root = pstore_sb->s_root;
379-
380-
inode_lock(d_inode(root));
381-
pstore_get_backend_records(psi, root, quiet);
392+
pstore_get_backend_records(psinfo, root, quiet);
382393
inode_unlock(d_inode(root));
383394
}
384395

385396
static int pstore_fill_super(struct super_block *sb, void *data, int silent)
386397
{
387398
struct inode *inode;
388399

389-
pstore_sb = sb;
390-
391400
sb->s_maxbytes = MAX_LFS_FILESIZE;
392401
sb->s_blocksize = PAGE_SIZE;
393402
sb->s_blocksize_bits = PAGE_SHIFT;
@@ -408,6 +417,10 @@ static int pstore_fill_super(struct super_block *sb, void *data, int silent)
408417
if (!sb->s_root)
409418
return -ENOMEM;
410419

420+
mutex_lock(&pstore_sb_lock);
421+
pstore_sb = sb;
422+
mutex_unlock(&pstore_sb_lock);
423+
411424
pstore_get_records(0);
412425

413426
return 0;
@@ -421,9 +434,17 @@ static struct dentry *pstore_mount(struct file_system_type *fs_type,
421434

422435
static void pstore_kill_sb(struct super_block *sb)
423436
{
437+
mutex_lock(&pstore_sb_lock);
438+
WARN_ON(pstore_sb != sb);
439+
424440
kill_litter_super(sb);
425441
pstore_sb = NULL;
442+
443+
mutex_lock(&records_list_lock);
426444
INIT_LIST_HEAD(&records_list);
445+
mutex_unlock(&records_list_lock);
446+
447+
mutex_unlock(&pstore_sb_lock);
427448
}
428449

429450
static struct file_system_type pstore_fs_type = {

fs/pstore/internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ extern void pstore_get_backend_records(struct pstore_info *psi,
3333
struct dentry *root, int quiet);
3434
extern int pstore_mkfile(struct dentry *root,
3535
struct pstore_record *record);
36-
extern bool pstore_is_mounted(void);
3736
extern void pstore_record_init(struct pstore_record *record,
3837
struct pstore_info *psi);
3938

fs/pstore/platform.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
460460
}
461461

462462
ret = psinfo->write(&record);
463-
if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
463+
if (ret == 0 && reason == KMSG_DUMP_OOPS)
464464
pstore_new_entry = 1;
465465

466466
total += record.size;
@@ -592,8 +592,7 @@ int pstore_register(struct pstore_info *psi)
592592
if (psi->flags & PSTORE_FLAGS_DMESG)
593593
allocate_buf_for_compression();
594594

595-
if (pstore_is_mounted())
596-
pstore_get_records(0);
595+
pstore_get_records(0);
597596

598597
if (psi->flags & PSTORE_FLAGS_DMESG)
599598
pstore_register_kmsg();

0 commit comments

Comments
 (0)