Go to file
Sebastian Andrzej Siewior e626cb02ee futex: Prevent the reuse of stale pi_state
Jiri Slaby reported a futex state inconsistency resulting in -EINVAL during
a lock operation for a PI futex. It requires that the a lock process is
interrupted by a timeout or signal:

  T1 Owns the futex in user space.

  T2 Tries to acquire the futex in kernel (futex_lock_pi()). Allocates a
     pi_state and attaches itself to it.

  T2 Times out and removes its rt_waiter from the rt_mutex. Drops the
     rtmutex lock and tries to acquire the hash bucket lock to remove
     the futex_q. The lock is contended and T2 schedules out.

  T1 Unlocks the futex (futex_unlock_pi()). Finds a futex_q but no
     rt_waiter. Unlocks the futex (do_uncontended) and makes it available
     to user space.

  T3 Acquires the futex in user space.

  T4 Tries to acquire the futex in kernel (futex_lock_pi()). Finds the
     existing futex_q of T2 and tries to attach itself to the existing
     pi_state.  This (attach_to_pi_state()) fails with -EINVAL because uval
     contains the TID of T3 but pi_state points to T1.

It's incorrect to unlock the futex and make it available for user space to
acquire as long as there is still an existing state attached to it in the
kernel.

T1 cannot hand over the futex to T2 because T2 already gave up and started
to clean up and is blocked on the hash bucket lock, so T2's futex_q with
the pi_state pointing to T1 is still queued.

T2 observes the futex_q, but ignores it as there is no waiter on the
corresponding rt_mutex and takes the uncontended path which allows the
subsequent caller of futex_lock_pi() (T4) to observe that stale state.

To prevent this the unlock path must dequeue all futex_q entries which
point to the same pi_state when there is no waiter on the rt mutex. This
requires obviously to make the dequeue conditional in the locking path to
prevent a double dequeue. With that it's guaranteed that user space cannot
observe an uncontended futex which has kernel state attached.

Fixes: fbeb558b0d ("futex/pi: Fix recursive rt_mutex waiter state")
Reported-by: Jiri Slaby <jirislaby@kernel.org>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Jiri Slaby <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20240118115451.0TkD_ZhB@linutronix.de
Closes: https://lore.kernel.org/all/4611bcf2-44d0-4c34-9b84-17406f881003@kernel.org
2024-01-19 12:58:17 +01:00
arch Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
block for-6.8/block-2024-01-08 2024-01-11 13:58:04 -08:00
certs This update includes the following changes: 2023-11-02 16:15:30 -10:00
crypto crypto: scomp - fix req->dst buffer overflow 2023-12-29 11:25:56 +08:00
Documentation Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
drivers Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
fs Generic: 2024-01-17 13:03:37 -08:00
include Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
init Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
io_uring Generic: 2024-01-17 13:03:37 -08:00
ipc shm: Slim down dependencies 2023-12-20 19:26:31 -05:00
kernel futex: Prevent the reuse of stale pi_state 2024-01-19 12:58:17 +01:00
lib 17 hotfixes. 10 address post-6.7 issues and the other 7 are cc:stable. 2024-01-17 09:31:36 -08:00
LICENSES LICENSES: Add the copyleft-next-0.3.1 license 2022-11-08 15:44:01 +01:00
mm Generic: 2024-01-17 13:03:37 -08:00
net for-6.8/io_uring-2024-01-08 2024-01-11 14:19:23 -08:00
rust Rust changes for v6.8 2024-01-11 13:05:41 -08:00
samples media updates for v6.8-rc1 2024-01-12 14:29:48 -08:00
scripts Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
security misc cleanups (the part that hadn't been picked by individual fs trees) 2024-01-11 20:23:50 -08:00
sound sound updates for 6.8-rc1 2024-01-12 11:55:28 -08:00
tools Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
usr usr/Kconfig: fix typos of "its" 2023-12-20 15:02:58 -08:00
virt Generic: 2024-01-17 13:03:37 -08:00
.clang-format clang-format: Update with v6.7-rc4's for_each macro list 2023-12-08 23:54:38 +01:00
.cocciconfig
.get_maintainer.ignore get_maintainer: add Alan to .get_maintainer.ignore 2022-08-20 15:17:44 -07:00
.gitattributes .gitattributes: set diff driver for Rust source code files 2023-05-31 17:48:25 +02:00
.gitignore kbuild: rpm-pkg: generate kernel.spec in rpmbuild/SPECS/ 2023-10-03 20:49:09 +09:00
.mailmap Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
.rustfmt.toml rust: add .rustfmt.toml 2022-09-28 09:02:20 +02:00
COPYING COPYING: state that all contributions really are covered by this file 2020-02-10 13:32:20 -08:00
CREDITS Another moderately busy cycle for documentation, including: 2024-01-11 19:46:52 -08:00
Kbuild Kbuild updates for v6.1 2022-10-10 12:00:45 -07:00
Kconfig kbuild: ensure full rebuild when the compiler is updated 2020-05-12 13:28:33 +09:00
MAINTAINERS Char/Misc and other Driver changes for 6.8-rc1 2024-01-17 16:47:17 -08:00
Makefile Quite a lot of kexec work this time around. Many singleton patches in 2024-01-09 11:46:20 -08:00
README Drop all 00-INDEX files from Documentation/ 2018-09-09 15:08:58 -06:00

Linux kernel
============

There are several guides for kernel developers and users. These guides can
be rendered in a number of formats, like HTML and PDF. Please read
Documentation/admin-guide/README.rst first.

In order to build the documentation, use ``make htmldocs`` or
``make pdfdocs``.  The formatted documentation can also be read online at:

    https://www.kernel.org/doc/html/latest/

There are various text files in the Documentation/ subdirectory,
several of them using the Restructured Text markup notation.

Please read the Documentation/process/changes.rst file, as it contains the
requirements for building and running the kernel, and information about
the problems which may result by upgrading your kernel.