Skip to contentSkip to conversation

Fix two issue in jbd2_fc_wait_bufs

PATCH Applied Ye Bin 3 participants 6 messages Sep 14, 2022 – Sep 30, 2022 Journalling layer for block devices (JBD2)

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

cover Fix two issue in jbd2_fc_wait_bufs

Ye Bin (2):
jbd2: fix potential buffer head reference count leak
jbd2: fix potential use-after-free in jbd2_fc_wait_bufs

 fs/jbd2/journal.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/2 jbd2: fix potential buffer head reference count leak

As in 'jbd2_fc_wait_bufs' if buffer isn't uptodate, will return -EIO without update 'journal->j_fc_off'. But 'jbd2_fc_release_bufs' will release buffer head from ‘j_fc_off - 1’ if 'bh' is NULL will terminal release which will lead to buffer head buffer head reference count leak.
To solve above issue, update 'journal->j_fc_off' before return -EIO.

---
 fs/jbd2/journal.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/jbd2/journal.c +7 -1
index 140b070471c0..1c833d8cb0fe 100644
@@ -925,8 +925,14 @@ int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
 		wait_on_buffer(bh);
 		put_bh(bh);
 		journal->j_fc_wbuf[i] = NULL;
-		if (unlikely(!buffer_uptodate(bh)))
+		/*
+		 * Update j_fc_off so jbd2_fc_release_bufs can release remain
+		 * buffer head.
+		 */
+		if (unlikely(!buffer_uptodate(bh))) {
+			journal->j_fc_off = i;
 			return -EIO;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH -next 1/2] jbd2: fix potential buffer head reference count leak

Quoted 7 lines · Ye Bin

As in 'jbd2_fc_wait_bufs' if buffer isn't uptodate, will return -EIO without update 'journal->j_fc_off'. But 'jbd2_fc_release_bufs' will release buffer head from ‘j_fc_off - 1’ if 'bh' is NULL will terminal release which will lead to buffer head buffer head reference count leak.
To solve above issue, update 'journal->j_fc_off' before return -EIO.

Looks good. Feel free to add:

Honza

Quoted 27 lines
---
 fs/jbd2/journal.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/jbd2/journal.c +7 -1
index 140b070471c0..1c833d8cb0fe 100644
@@ -925,8 +925,14 @@ int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
 		wait_on_buffer(bh);
 		put_bh(bh);
 		journal->j_fc_wbuf[i] = NULL;
-		if (unlikely(!buffer_uptodate(bh)))
+		/*
+		 * Update j_fc_off so jbd2_fc_release_bufs can release remain
+		 * buffer head.
+		 */
+		if (unlikely(!buffer_uptodate(bh))) {
+			journal->j_fc_off = i;
 			return -EIO;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#3 lore raw

PATCH 2/2 jbd2: fix potential use-after-free in jbd2_fc_wait_bufs

In reply to Ye Bin

In 'jbd2_fc_wait_bufs' use 'bh' after put buffer head reference count which may lead to use-after-free.
So judge buffer if uptodate before put buffer head reference count.

---
 fs/jbd2/journal.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/jbd2/journal.c +3 -3
index 1c833d8cb0fe..4d49f629d06b 100644
@@ -923,16 +923,16 @@ int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
 	for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) {
 		bh = journal->j_fc_wbuf[i];
 		wait_on_buffer(bh);
-		put_bh(bh);
-		journal->j_fc_wbuf[i] = NULL;
 		/*
 		 * Update j_fc_off so jbd2_fc_release_bufs can release remain
 		 * buffer head.
 		 */
 		if (unlikely(!buffer_uptodate(bh))) {
-			journal->j_fc_off = i;
+			journal->j_fc_off = i + 1;
 			return -EIO;
 		}
+		put_bh(bh);
+		journal->j_fc_wbuf[i] = NULL;
 	}
 
 	return 0;
Signature · 1 line
2.31.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH -next 2/2] jbd2: fix potential use-after-free in jbd2_fc_wait_bufs

Quoted 5 lines · Ye Bin

In 'jbd2_fc_wait_bufs' use 'bh' after put buffer head reference count which may lead to use-after-free.
So judge buffer if uptodate before put buffer head reference count.

Looks good. Feel free to add:

Honza

Quoted 31 lines
---
 fs/jbd2/journal.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/jbd2/journal.c +3 -3
index 1c833d8cb0fe..4d49f629d06b 100644
@@ -923,16 +923,16 @@ int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
 	for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) {
 		bh = journal->j_fc_wbuf[i];
 		wait_on_buffer(bh);
-		put_bh(bh);
-		journal->j_fc_wbuf[i] = NULL;
 		/*
 		 * Update j_fc_off so jbd2_fc_release_bufs can release remain
 		 * buffer head.
 		 */
 		if (unlikely(!buffer_uptodate(bh))) {
-			journal->j_fc_off = i;
+			journal->j_fc_off = i + 1;
 			return -EIO;
 		}
+		put_bh(bh);
+		journal->j_fc_wbuf[i] = NULL;
 	}
 
 	return 0;
Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#5 lore raw
In reply to Ye Bin
Quoted 8 lines · Ye Bin

Ye Bin (2):
jbd2: fix potential buffer head reference count leak
jbd2: fix potential use-after-free in jbd2_fc_wait_bufs

fs/jbd2/journal.c | 10 ++++++++--

 1 file changed, 8 insertions(+), 2 deletions(-)

[...]

Applied, thanks!

[1/2] jbd2: fix potential buffer head reference count leak

      commit: 53dee029ec341f5c985b880de34f64de60072688
[2/2] jbd2: fix potential use-after-free in jbd2_fc_wait_bufs
      commit: 6e16a2d9ff8f3ea4a53e10ae4607feb446cf5c90

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>