Skip to content

Commit ad56eda

Browse files
jankaratytso
authored andcommitted
jbd2: fix use after free in jbd2_journal_dirty_metadata()
jbd2_journal_dirty_metadata() didn't get a reference to journal_head it was working with. This is OK in most of the cases since the journal head should be attached to a transaction but in rare occasions when we are journalling data, __ext4_journalled_writepage() can race with jbd2_journal_invalidatepage() stripping buffers from a page and thus journal head can be freed under hands of jbd2_journal_dirty_metadata(). Fix the problem by getting own journal head reference in jbd2_journal_dirty_metadata() (and also in jbd2_journal_set_triggers() which can possibly have the same issue). Reported-by: Zheng Liu <gnehzuil.liu@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Cc: stable@vger.kernel.org
1 parent 386ad67 commit ad56eda

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

‎fs/jbd2/transaction.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,9 +1065,12 @@ int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
10651065
void jbd2_journal_set_triggers(struct buffer_head *bh,
10661066
struct jbd2_buffer_trigger_type *type)
10671067
{
1068-
struct journal_head *jh = bh2jh(bh);
1068+
struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
10691069

1070+
if (WARN_ON(!jh))
1071+
return;
10701072
jh->b_triggers = type;
1073+
jbd2_journal_put_journal_head(jh);
10711074
}
10721075

10731076
void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
@@ -1119,17 +1122,18 @@ int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
11191122
{
11201123
transaction_t *transaction = handle->h_transaction;
11211124
journal_t *journal = transaction->t_journal;
1122-
struct journal_head *jh = bh2jh(bh);
1125+
struct journal_head *jh;
11231126
int ret = 0;
11241127

1125-
jbd_debug(5, "journal_head %p\n", jh);
1126-
JBUFFER_TRACE(jh, "entry");
11271128
if (is_handle_aborted(handle))
11281129
goto out;
1129-
if (!buffer_jbd(bh)) {
1130+
jh = jbd2_journal_grab_journal_head(bh);
1131+
if (!jh) {
11301132
ret = -EUCLEAN;
11311133
goto out;
11321134
}
1135+
jbd_debug(5, "journal_head %p\n", jh);
1136+
JBUFFER_TRACE(jh, "entry");
11331137

11341138
jbd_lock_bh_state(bh);
11351139

@@ -1220,6 +1224,7 @@ int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
12201224
spin_unlock(&journal->j_list_lock);
12211225
out_unlock_bh:
12221226
jbd_unlock_bh_state(bh);
1227+
jbd2_journal_put_journal_head(jh);
12231228
out:
12241229
JBUFFER_TRACE(jh, "exit");
12251230
WARN_ON(ret); /* All errors are bugs, so dump the stack */

0 commit comments

Comments
 (0)