pipe: Fix a potential UAF and delete a duplicate line of code
cover pipe: Fix a potential UAF and delete a duplicate line of code
I found these when I was learning the code. I briefly tested it, and no new problems were introduced.
Zhen Lei (2):
pipe: fix potential use-after-free in pipe_read()
pipe: delete a duplicate line of code in pipe_write()
fs/pipe.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Signature · 1 line
2.26.0.106.g9fadedd
PATCH 1/2 pipe: fix potential use-after-free in pipe_read()
Accessing buf->flags after pipe_buf_release(pipe, buf) is unsafe, because the 'buf' memory maybe freed.
--- fs/pipe.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/pipe.c +3 -1
@@ -319,10 +319,12 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to) }if (!buf->len) { +unsigned int __maybe_unused flags = buf->flags; +pipe_buf_release (pipe, buf);spin_lock_irq (&pipe->rd_wait.lock);#ifdef CONFIG_WATCH_QUEUE -if (buf->flags &PIPE_BUF_FLAG_LOSS ) +if (flags &PIPE_BUF_FLAG_LOSS ) pipe->note_loss = true;#endif tail++;
Signature · 1 line
2.26.0.106.g9fadedd
Re: [PATCH 1/2] pipe: fix potential use-after-free in pipe_read()
fs/pipe.c pipe_read(struct kiocb *iocb, struct iov_iter *to) · line 322
Quoted 2 lines · Zhen Lei
fs/pipe.c +1
-0if (!buf->len) { +unsigned int __maybe_unused flags = buf->flags;
Why __maybe_unused?
Re: [PATCH 1/2] pipe: fix potential use-after-free in pipe_read()
Quoted 5 lines · Matthew Wilcox
fs/pipe.c pipe_read(struct kiocb *iocb, struct iov_iter *to) · line 322
Quoted 2 lines · Zhen Lei
fs/pipe.c +1
-0if (!buf->len) { +unsigned int __maybe_unused flags = buf->flags;Why
__maybe_unused?
It's used only if "#ifdef CONFIG_WATCH_QUEUE". Otherwise, a warning will be reported.
Quoted 2 lines
.
Re: [PATCH 1/2] pipe: fix potential use-after-free in pipe_read()
Quoted 10 lines · Leizhen (ThunderTown)
Quoted 5 lines · Matthew Wilcox
fs/pipe.c pipe_read(struct kiocb *iocb, struct iov_iter *to) · line 322
Quoted 2 lines · Zhen Lei
fs/pipe.c +1
-0if (!buf->len) { +unsigned int __maybe_unused flags = buf->flags;Why
__maybe_unused?It's used only if "#ifdef
CONFIG_WATCH_QUEUE". Otherwise, a warning will be reported.
Better to turn the #ifdef into if (IS_ENABLED())
Re: [PATCH 1/2] pipe: fix potential use-after-free in pipe_read()
Quoted 5 lines · Matthew Wilcox
Quoted 4 lines · Leizhen (ThunderTown)
Quoted 1 line · Matthew Wilcox
On Mon, Nov 15, 2021 at 11:57:20AM +0800, Zhen Lei wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
fs/pipe.c pipe_read(struct kiocb *iocb, struct iov_iter *to) · line 322
Quoted 2 lines
fs/pipe.c +1
-0if (!buf->len) { +unsigned int __maybe_unused flags = buf->flags;Why
__maybe_unused?It's used only if "#ifdef
CONFIG_WATCH_QUEUE". Otherwise, a warning will be reported.Better to turn the #ifdef into if (
IS_ENABLED())
Hi, Matthew:
Thank you for your advice. IS_ENABLED() is a good idea, but when I tried it, I found that
the macro 'PIPE_BUF_FLAG_LOSS' and the structure member 'note_loss' were also separated by "ifdef CONFIG_WATCH_QUEUE", so this method is not suitable here.
#ifdef CONFIG_WATCH_QUEUE
#define PIPE_BUF_FLAG_LOSS 0x40 /* Message loss happened after this buffer */
#endif
@@ -62,9 +60,7 @@ struct pipe_inode_info {
unsigned int tail;
unsigned int max_usage;
unsigned int ring_size;
#ifdef CONFIG_WATCH_QUEUE
bool note_loss;#endif
Quoted 2 lines
.
PATCH 2/2 pipe: delete a duplicate line of code in pipe_write()
In reply to Zhen LeiThe buf->offset has been assigned to 0 before the copy operation, and it seems odd to readjust its value after the copy is complete. Delete the second 'buf->offset = 0'.
--- fs/pipe.c | 1 - 1 file changed, 1 deletion(-)
fs/pipe.c +0 -1
@@ -536,7 +536,6 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)break ; } ret += copied; - buf->offset =0 ; buf->len = copied;if (!iov_iter_count (from))
Signature · 1 line
2.26.0.106.g9fadedd