Skip to contentSkip to conversation

poll: rust: allow poll_table ptrs to be null

PATCH Applied Alice Ryhl 3 participants 8 messages Jun 20, 2025 – Jun 23, 2025

Subsystems (3)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

PATCH 1/1 poll: rust: allow poll_table ptrs to be null

It's possible for a poll_table to be null. This can happen if an end-user just wants to know if a resource has events right now without registering a waiter for when events become available. Furthermore, these null pointers should be handled transparently by the API, so we should not change from_ptr to return an Option. Thus, change PollTable to wrap a raw pointer rather than use a reference so that you can pass null.

Comments mentioning struct poll_table are changed to just poll_table since poll_table is a typedef. (It's a typedef because it's supposed to be opaque.)

---
This issue was discovered from a syzkaller report on Rust Binder.

Intended for Christian Brauner's tree.

---
 rust/helpers/helpers.c   |  1 +
 rust/helpers/poll.c      | 10 ++++++++
 rust/kernel/sync/poll.rs | 65 +++++++++++++++++-------------------------------
 3 files changed, 34 insertions(+), 42 deletions(-)
rust/helpers/helpers.c +1 -0
index 0f1b5d11598591bc62bb6439747211af164b76d6..ff65073fe3a5220e7792306fc9ae74c416935a52 100644
@@ -30,6 +30,7 @@
 #include "platform.c"
 #include "pci.c"
 #include "pid_namespace.c"
+#include "poll.c"
 #include "rbtree.c"
 #include "rcu.c"
 #include "refcount.c"
rust/helpers/poll.c new file +10 -0
new file mode 100644
index 0000000000000000000000000000000000000000..7e5b1751c2d526f2dd467fcd61dcf49294d3c20d
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/export.h>
+#include <linux/poll.h>
+
+void rust_helper_poll_wait(struct file *filp, wait_queue_head_t *wait_address,
+			   poll_table *p)
+{
+	poll_wait(filp, wait_address, p);
+}
rust/kernel/sync/poll.rs +23 -42

90 lines not on this page · raw message

---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20250620-poll-table-null-bf9a6a6c569e

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
reviewer
#1 lore raw

rust/kernel/sync/poll.rs macro_rules! new_poll_condvar { · line 51

Quoted 9 lines · Alice Ryhl
     ///
     /// # Safety
     ///
rust/kernel/sync/poll.rs +0 -6
-    /// The caller must ensure that for the duration of `'a`, the pointer will point at a valid poll
-    /// table (as defined in the type invariants).
-    ///
-    /// The caller must also ensure that the `poll_table` is only accessed via the returned
-    /// reference for the duration of `'a`.
-    pub unsafe fn from_ptr<'a>(ptr: *mut bindings::poll_table) -> &'a mut PollTable {

Returning Option<&'a mut PollTable> is not an option? I'd like to avoid wrapping raw pointers...

---
Cheers,
Benno

Quoted 4 lines
rust/kernel/sync/poll.rs +0 -4
-        // SAFETY: The safety requirements guarantee the validity of the dereference, while the
-        // `PollTable` type being transparent makes the cast ok.
-        unsafe { &mut *ptr.cast() }
-    }
author maintainer
#2 lore raw
Quoted 12 lines · Benno Lossin

rust/kernel/sync/poll.rs macro_rules! new_poll_condvar { · line 51

Quoted 9 lines · Alice Ryhl
     ///
     /// # Safety
     ///
rust/kernel/sync/poll.rs +0 -6
-    /// The caller must ensure that for the duration of `'a`, the pointer will point at a valid poll
-    /// table (as defined in the type invariants).
-    ///
-    /// The caller must also ensure that the `poll_table` is only accessed via the returned
-    /// reference for the duration of `'a`.
-    pub unsafe fn from_ptr<'a>(ptr: *mut bindings::poll_table) -> &'a mut PollTable {
Quoted 2 lines

Returning Option<&'a mut PollTable> is not an option? I'd like to avoid wrapping raw pointers...

You're going to make people handle the Option by early-returning if you do that, but that's wrong. You're supposed to treat null and non-null the same.

Alice

reviewer
#3 lore raw
Quoted 13 lines · Alice Ryhl
Quoted 12 lines · Benno Lossin
Quoted 9 lines · Alice Ryhl
     ///
     /// # Safety
     ///
rust/kernel/sync/poll.rs +0 -6
-    /// The caller must ensure that for the duration of `'a`, the pointer will point at a valid poll
-    /// table (as defined in the type invariants).
-    ///
-    /// The caller must also ensure that the `poll_table` is only accessed via the returned
-    /// reference for the duration of `'a`.
-    pub unsafe fn from_ptr<'a>(ptr: *mut bindings::poll_table) -> &'a mut PollTable {
Quoted 6 lines
Quoted 2 lines

Returning Option<&'a mut PollTable> is not an option? I'd like to avoid wrapping raw pointers...

You're going to make people handle the Option by early-returning if you do that, but that's wrong. You're supposed to treat null and non-null the same.

Ah right...

An PollTableInner type that wraps the bindings::poll_table opaquely sounds like too much work, so let's go with your approach.

---
Cheers,
Benno

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Alice Ryhl
Quoted 16 lines · Alice Ryhl

It's possible for a poll_table to be null. This can happen if an end-user just wants to know if a resource has events right now without registering a waiter for when events become available. Furthermore, these null pointers should be handled transparently by the API, so we should not change from_ptr to return an Option. Thus, change PollTable to wrap a raw pointer rather than use a reference so that you can pass null.

Comments mentioning struct poll_table are changed to just poll_table since poll_table is a typedef. (It's a typedef because it's supposed to be opaque.)

---
This issue was discovered from a syzkaller report on Rust Binder.

Quoted 6 lines

Intended for Christian Brauner's tree.

---
 rust/helpers/helpers.c   |  1 +
 rust/helpers/poll.c      | 10 ++++++++
 rust/kernel/sync/poll.rs | 65 +++++++++++++++++-------------------------------
 3 files changed, 34 insertions(+), 42 deletions(-)

Looks good, one safety comment concern below, with that fixed:

Quoted 6 lines
     /// Register this [`PollTable`] with the provided [`PollCondVar`], so that it can be notified
     /// using the condition variable.
rust/kernel/sync/poll.rs +0 -4
-    pub fn register_wait(&mut self, file: &File, cv: &PollCondVar) {
-        if let Some(qproc) = self.get_qproc() {
-            // SAFETY: The pointers to `file` and `self` need to be valid for the duration of this
-            // call to `qproc`, which they are because they are references.

rust/kernel/sync/poll.rs macro_rules! new_poll_condvar { · line 53

Quoted 10 lines
rust/kernel/sync/poll.rs +2 -8
-            //
-            // The `cv.wait_queue_head` pointer must be valid until an rcu grace period after the
-            // waiter is removed. The `PollCondVar` is pinned, so before `cv.wait_queue_head` can
-            // be destroyed, the destructor must run. That destructor first removes all waiters,
-            // and then waits for an rcu grace period. Therefore, `cv.wait_queue_head` is valid for
-            // long enough.
-            unsafe { qproc(file.as_ptr() as _, cv.wait_queue_head.get(), self.0.get()) };
-        }
+    pub fn register_wait(&self, file: &File, cv: &PollCondVar) {
+        // SAFETY: The pointers `self.table` and `file` are valid for the duration of this call.

self.table might be null, which I think we agreed to is not "valid".

rust/kernel/sync/poll.rs macro_rules! new_poll_condvar { · line 57

Quoted 4 lines
rust/kernel/sync/poll.rs +4 -0
+        // The `cv.wait_queue_head` pointer must be valid until an rcu grace period after the
+        // waiter is removed. The `PollCondVar` is pinned, so before `cv.wait_queue_head` can be
+        // destroyed, the destructor must run. That destructor first removes all waiters, and then
+        // waits for an rcu grace period. Therefore, `cv.wait_queue_head` is valid for long enough.

Could you use bullet points for the different requirements?

---
Cheers,
Benno

Quoted 10 lines

+ unsafe { bindings::poll_wait(file.as_ptr(), cv.wait_queue_head.get(), self.table) }

     }

}

---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20250620-poll-table-null-bf9a6a6c569e

Best regards,

maintainer Applied
#5 lore raw
In reply to Alice Ryhl
Quoted 9 lines · Alice Ryhl

It's possible for a poll_table to be null. This can happen if an end-user just wants to know if a resource has events right now without registering a waiter for when events become available. Furthermore, these null pointers should be handled transparently by the API, so we should not change from_ptr to return an Option. Thus, change PollTable to wrap a raw pointer rather than use a reference so that you can pass null.

[...]

Applied to the vfs-6.17.rust branch of the vfs/vfs.git tree. Patches in the vfs-6.17.rust branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.17.rust

[1/1] poll: rust: allow poll_table ptrs to be null

      https://git.kernel.org/vfs/vfs/c/6efbf978891b
reviewer
#6 lore raw
Quoted 20 lines · Christian Brauner
Quoted 9 lines · Alice Ryhl

It's possible for a poll_table to be null. This can happen if an end-user just wants to know if a resource has events right now without registering a waiter for when events become available. Furthermore, these null pointers should be handled transparently by the API, so we should not change from_ptr to return an Option. Thus, change PollTable to wrap a raw pointer rather than use a reference so that you can pass null.

[...]

Applied to the vfs-6.17.rust branch of the vfs/vfs.git tree. Patches in the vfs-6.17.rust branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Quoted 8 lines

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.17.rust

[1/1] poll: rust: allow poll_table ptrs to be null

      https://git.kernel.org/vfs/vfs/c/6efbf978891b

You took my RB without changing the safety comment that I had a concern about. Can we fix that before you send the pull request?

---
Cheers,
Benno

author maintainer
#7 lore raw
In reply to Christian Brauner
Quoted 21 lines · Christian Brauner
Quoted 9 lines · Alice Ryhl

It's possible for a poll_table to be null. This can happen if an end-user just wants to know if a resource has events right now without registering a waiter for when events become available. Furthermore, these null pointers should be handled transparently by the API, so we should not change from_ptr to return an Option. Thus, change PollTable to wrap a raw pointer rather than use a reference so that you can pass null.

[...]

Applied to the vfs-6.17.rust branch of the vfs/vfs.git tree. Patches in the vfs-6.17.rust branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Quoted 8 lines

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.17.rust

[1/1] poll: rust: allow poll_table ptrs to be null

      https://git.kernel.org/vfs/vfs/c/6efbf978891b

I just sent a v2, would you be able to pick that up instead? Thanks!