2022-07-24 04:09:21

by Siddh Raman Pant

[permalink] [raw]
Subject: [PATCH v2] kernel/watch_queue: Make pipe NULL while clearing watch_queue

If not done, a reference to a freed pipe remains in the watch_queue,
as this function is called before freeing a pipe in free_pipe_info()
(see line 834 of fs/pipe.c).

This causes a UAF when post_one_notification tries to access the pipe on
a key update, which is reported by syzbot.

Bug report: https://syzkaller.appspot.com/bug?id=1870dd7791ba05f2ea7f47f7cbdde701173973fc
Reported-and-tested-by: [email protected]

Signed-off-by: Siddh Raman Pant <[email protected]>
---
Changes since v1:
- Removed the superfluous ifdef guard.

kernel/watch_queue.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c
index bb9962b33f95..0357e5c6cf18 100644
--- a/kernel/watch_queue.c
+++ b/kernel/watch_queue.c
@@ -637,8 +637,15 @@ void watch_queue_clear(struct watch_queue *wqueue)
spin_lock_bh(&wqueue->lock);
}

- spin_unlock_bh(&wqueue->lock);
rcu_read_unlock();
+
+ /* Clearing the watch queue, so we should clean the associated pipe. */
+ if (wqueue->pipe) {
+ wqueue->pipe->watch_queue = NULL;
+ wqueue->pipe = NULL;
+ }
+
+ spin_unlock_bh(&wqueue->lock);
}

/**
--
2.35.1



2022-07-24 06:40:45

by Siddh Raman Pant

[permalink] [raw]
Subject: Re: [PATCH v2] kernel/watch_queue: Make pipe NULL while clearing watch_queue

On Sun, 24 Jul 2022 10:54:26 +0530 Hillf Danton <[email protected]> wrote:
> According to sysbot's report [1], what is proposed fails to fix the
> reported uaf in the scenario below because of no wqueue->lock acquired
> in the post path. Despite of defunct checked, it is checked after taking
> pipe's wait lock - a bit too late.
>
> Hillf
>
> post_one_notification watch_queue_clear
> === ===
> pipe = wqueue->pipe;
> if (!pipe)
> return false;
> spin_lock_bh(&wqueue->lock);
> wqueue->pipe = NULL;
> spin_lock_bh(&wqueue->lock);
>
> pipe is freed
>
> spin_lock_irq(&pipe->rd_wait.lock); // uaf reported
>
> if (wqueue->defunct)
> goto out;
>
> [1] https://lore.kernel.org/lkml/[email protected]/
>

Before post_one_notification(), in __post_watch_notification(), on line 228
wqueue->lock is acquired (using lock_wqueue()).

The comment on post_one_notification() also tells that it should be called
with wqueue->lock held. So we do acquire the lock in the post path.

The pipe is freed after the execution of watch_queue_clear() in
free_pipe_info().

A side note: Your emails don't seem to pop up on lore's LKML archive. Maybe
check if the email isn't being blocked?

Thanks,
Siddh

2022-07-24 08:06:43

by Siddh Raman Pant

[permalink] [raw]
Subject: Re: [PATCH v2] kernel/watch_queue: Make pipe NULL while clearing watch_queue

On Sun, 24 Jul 2022 12:49:58 +0530 Hillf Danton <[email protected]> wrote:
> Given defunct serialized, still need to clear wqueue->pipe in the clear
> path as proposed?

I am not sure what you mean by this...

If you mean freeing the pipe, it is done by free_pipe_info(), which is
the caller of watch_queue_clear().

If you mean making wqueue->pipe NULL, it is being done so in the patch.

Thanks,
Siddh

2022-07-27 14:55:34

by David Howells

[permalink] [raw]
Subject: Re: [PATCH v2] kernel/watch_queue: Make pipe NULL while clearing watch_queue

Hillf Danton <[email protected]> wrote:

> Yes, you are right. I missed 353f7988dd84 ("watchqueue: make sure to serialize
> 'wqueue->defunct' properly"). Sorry for my noise.
>
> Given defunct serialized, still need to clear wqueue->pipe in the clear
> path as proposed?

In fact, with the locking, is wqueue->defunct even still needed, I wonder?

David