Skip to contentSkip to conversation

tcmu: Fix inode use-after-free in global data block pool

PATCH New Tao Zou 2 participants 3 messages Nov 27

Subsystems (3)
lore
Share & tools
lore

Tao Zou

author
#0 lore raw

cover tcmu: Fix inode use-after-free in global data block pool

Hi,

TCMU's global data block pool can dereference udev->inode after the VFS has reclaimed the inode. tcmu_open() stores the pointer without taking a reference, while delayed unmap work can still reach the configured device through root_udev after the last UIO close.

I reproduced this on mainline using only user-space configuration and I/O. On the unfixed 40212b29a6f718d4e7fb3275cede181cb8e137cf baseline, the reproducer panics in down_read() through unmap_mapping_pages() and find_free_blocks(). An earlier mainline build at 4dce97c6c265544342e716e62fd24bbb232590c5 produced the same target panic in 10/10 independent cold-boot runs.

Patch 1/1 holds an inode reference until the final tcmu_dev reference is released. It replaces the pointer under cmdr_lock and drops the previous reference after a reopen. The reference intentionally remains held across tcmu_release(), since delayed unmap work still needs the inode after the last UIO close.

With the patch applied, the unchanged reproducer survived its full trigger sequence in 2/2 independent boots. The script's exit code 42 is its explicit survival marker. Additional regression testing completed 20 tcmu-runner restarts and 80 direct fio runs across four devices, followed by successful destruction of all four devices. No BUG, Oops, WARNING, general protection fault, or KASAN report was found after the regression run.

Using tcmu-runner 1.6.2 built from commit 1bdb239fa183c84d2ace6b52aa33476968a0cef7, the portable reproducer also triggered the target panic once each on Debian 13 (6.12.118+deb13-amd64), Ubuntu 26.04.1 (7.0.0-44-generic), and CentOS Stream 10 (6.12.0-284.el10.x86_64). Ubuntu was tested again after moving from 2 vCPU and 4 GiB to 4 vCPU and 8 GiB, without changing the reproducer. These are single-run portability checks, separate from the mainline A/B results.

The reproducer deliberately grooms shmem_inode_cache slab placement to make the lifetime bug deterministic in the documented test environment; it is not intended to estimate trigger probability under normal production loads.

WARNING: The reproducer is expected to panic a vulnerable kernel and must only be run as root on a dedicated, disposable test VM with no data or other workloads. Enable kdump before running if crash evidence is required.

The complete reproducer is included below as plain text. It requires targetcli, tcmu-runner with its file handler, tcm_loop, fio, python3 and udev. Test with init_on_free=1 panic_on_oops=1 on the kernel command line.

Thanks,
Tao Zou

----- BEGIN TCMU REPRODUCER: tcmu-inode-lifetime-reproducer.sh ----- #!/bin/bash
# WARNING: This script intentionally panics vulnerable kernels. Run it only

# as root on a dedicated, disposable VM with no data or other workloads.
# It requires targetcli, tcmu-runner with its file handler, tcm_loop, fio,
# python3 and udev. Test with init_on_free=1 panic_on_oops=1 on the kernel
# command line. Enable kdump before running if crash evidence is required.
set -euxo pipefail

DEVICE_COUNT=4
WWN=naa.5001405a11b22c40 IMAGE_PREFIX=/root/tcmu-userspace-page-zero PARAM=/sys/module/target_core_user/parameters/global_max_data_area_mb GUARD_DIR=/mnt/tcmu-page-zero-guards
MAX_GUARDS=4096
RUN_ID="$(date +%s%N)-$$" RUN_DIR="/root/tcmu-userspace-runs/$RUN_ID"
mkdir -p "$RUN_DIR"
exec > >(tee "$RUN_DIR/reproduce.log") 2>&1

cat /etc/os-release
uname -a
[[ " $(cat /proc/cmdline) " == *" init_on_free=1 "* ]] dmesg > "$RUN_DIR/dmesg-boot.txt"
grep -q 'heap free:on' "$RUN_DIR/dmesg-boot.txt"
[[ "$(cat /sys/kernel/slab/shmem_inode_cache/poison)" == 0 ]] [[ "$(cat /proc/sys/kernel/panic_on_oops)" == 1 ]]
! grep -q '^spray ' /proc/modules
printf 'RUN_ID=%s\n' "$RUN_ID" | tee /root/tcmu-userspace-current-run cat /proc/cmdline
cat /proc/sys/kernel/tainted
date -Ins
date +%s > "$RUN_DIR/start.epoch"
if command -v kdumpctl >/dev/null 2>&1; then

	kdumpctl status || true

fi
taskset -pc 0 $$

targetcli /loopback delete "$WWN" 2>/dev/null || true for ((i = 0; i < DEVICE_COUNT; i++)); do

	targetcli /backstores/user:file delete "tcmu_page_zero_repro_${i}" 2>/dev/null || true
	rm -f "${IMAGE_PREFIX}-${i}.img"

done
udevadm settle

python3 - "$RUN_DIR/dev-paths-before.json" <<'PY'
import json
import os
import sys

paths = []
for root in ('/dev', '/run/udev'):
for dirpath, dirnames, filenames in os.walk(root):
paths.extend(os.path.join(dirpath, name) for name in dirnames + filenames)
with open(sys.argv[1], 'w') as output:
json.dump(sorted(paths), output)
PY

mkdir -p "$GUARD_DIR"
if mountpoint -q "$GUARD_DIR"; then
umount "$GUARD_DIR"
fi
mount -t tmpfs -o size=32M tmpfs "$GUARD_DIR"
mkdir "$GUARD_DIR/pre" "$GUARD_DIR/post"
printf '512\n' > "$PARAM"
systemctl start tcmu-runner
systemctl is-active tcmu-runner
taskset -apc 0 "$(systemctl show -p MainPID --value tcmu-runner)" taskset -apc 0 "$(systemctl show -p MainPID --value systemd-udevd)" if pgrep -x kdevtmpfs >/dev/null; then

	taskset -pc 0 "$(pgrep -x kdevtmpfs)"

fi

slab_count() {
	set -- $(grep '^shmem_inode_cache ' /proc/slabinfo)
	printf '%s\n' "${15}"
}

PRE_GUARD_COUNT=0
FRESH_SLABS=''

shopt -s nullglob
before_sd_paths=(/sys/class/block/sd*)
declare -A before_sd=()
for path in "${before_sd_paths[@]}"; do

	before_sd["${path##*/}"]=1

done

for ((i = 0; i < DEVICE_COUNT; i++)); do

	fresh=''
	for ((guard = 0; guard < MAX_GUARDS; guard++)); do
		old_count="$(slab_count)"
		: > "$GUARD_DIR/pre/g$PRE_GUARD_COUNT"
		PRE_GUARD_COUNT=$((PRE_GUARD_COUNT + 1))
		new_count="$(slab_count)"
		if ((new_count > old_count)); then
			fresh="$new_count"
			break
		fi
	done
	[[ -n "$fresh" ]]
	FRESH_SLABS="${FRESH_SLABS:+$FRESH_SLABS,}$fresh"
	image="${IMAGE_PREFIX}-${i}.img"
	name="tcmu_page_zero_repro_${i}"
	truncate -s 64M "$image"
	targetcli /backstores/user:file create name="$name" cfgstring="$image" size=64M
	udevadm settle

done
udevadm settle
uio_paths=(/dev/uio[0-9]*)
(( ${#uio_paths[@]} == DEVICE_COUNT ))
: > "$RUN_DIR/uio-stat.txt"
for UIO in "${uio_paths[@]}"; do

	[[ -c "$UIO" ]]
	stat --printf='path=%n dev=%d ino=%i mode=%f rdev=%r\n' "$UIO" | tee -a "$RUN_DIR/uio-stat.txt"

done
targetcli ls > "$RUN_DIR/targetcli-before-io.txt"

targetcli /loopback create "$WWN"
for ((i = 0; i < DEVICE_COUNT; i++)); do

	targetcli "/loopback/$WWN/luns" create "/backstores/user:file/tcmu_page_zero_repro_${i}"
done
for scan in /sys/class/scsi_host/host*/scan; do
	printf -- '- - -\n' > "$scan"

done
udevadm settle
new_sd_paths=()
for path in /sys/class/block/sd*; do

	if [[ -z "${before_sd[${path##*/}]+present}" ]]; then
		new_sd_paths+=("$path")
	fi

done
(( ${#new_sd_paths[@]} == DEVICE_COUNT ))
printf 'DEVICE_COUNT=%s\n' "$DEVICE_COUNT" > "$RUN_DIR/devices.env" printf 'UIO_PATHS=%s\n' "${uio_paths[*]}" >> "$RUN_DIR/devices.env" for ((i = 0; i < DEVICE_COUNT; i++)); do

	SD_DEVICE="/dev/${new_sd_paths[$i]##*/}"
	[[ -b "$SD_DEVICE" ]]
	printf 'SD_DEVICE_%s=%s\n' "$i" "$SD_DEVICE" | tee -a "$RUN_DIR/devices.env"
	properties="$RUN_DIR/scsi-device-${i}.properties"
	udevadm info --query=property --name="$SD_DEVICE" | tee "$properties"
	grep -q '^ID_VENDOR=LIO-ORG$' "$properties"
	grep -q '^DEVPATH=/devices/tcm_loop_' "$properties"
	printf 'running\n' > "/sys/block/${SD_DEVICE##*/}/device/state"
	timeout 45s fio --name="tcmu-userspace-page-zero-${i}" --filename="$SD_DEVICE" --direct=1 --ioengine=libaio --rw=write --bs=1M --size=64M --iodepth=32 --group_reporting > "$RUN_DIR/fio-${i}.log" 2>&1
	cat "$RUN_DIR/fio-${i}.log"

done
POST_GUARD_COUNT=0
SEALED_SLABS=''
for ((i = 0; i < MAX_GUARDS; i++)); do

	old_count="$(slab_count)"
	: > "$GUARD_DIR/post/g$i"
	POST_GUARD_COUNT=$((POST_GUARD_COUNT + 1))
	new_count="$(slab_count)"
	if ((new_count > old_count)); then
		SEALED_SLABS="$new_count"
		break
	fi
done
[[ -n "$SEALED_SLABS" ]]
printf 'PRE_GUARD_COUNT=%s\nPOST_GUARD_COUNT=%s\nFRESH_SLABS=%s\nSEALED_SLABS=%s\n' \
	"$PRE_GUARD_COUNT" "$POST_GUARD_COUNT" "$FRESH_SLABS" "$SEALED_SLABS" | tee "$RUN_DIR/guards.env"

systemctl stop tcmu-runner
systemctl is-active tcmu-runner || true
: > "$RUN_DIR/uio-holders.txt"
for UIO in "${uio_paths[@]}"; do

	if fuser "$UIO" >> "$RUN_DIR/uio-holders.txt" 2>&1; then
		cat "$RUN_DIR/uio-holders.txt"
		exit 1
	fi

done
systemctl stop systemd-udevd-kernel.socket systemd-udevd-control.socket systemd-udevd systemctl is-active systemd-udevd || true
dmesg > "$RUN_DIR/dmesg-before-trigger.txt"
printf 'armed_epoch=%s\n' "$(date +%s)" | tee "$RUN_DIR/armed.env"

python3 - "$RUN_DIR/dev-paths-before.json" "$RUN_DIR/created-paths-removed.txt" <<'PY' import json
import os
import stat
import sys

with open(sys.argv[1]) as source:
before = set(json.load(source))
paths = []
for root in ('/dev', '/run/udev'):
for dirpath, dirnames, filenames in os.walk(root):
paths.extend(os.path.join(dirpath, name) for name in dirnames + filenames)
created = sorted(set(paths) - before)
with open(sys.argv[2], 'w') as output:
for path in created:
output.write(path + '\n')
for path in created:
try:
if not stat.S_ISDIR(os.lstat(path).st_mode):
os.unlink(path)
except FileNotFoundError:
pass
for path in sorted(created, key=len, reverse=True):
try:
if stat.S_ISDIR(os.lstat(path).st_mode):
os.rmdir(path)
except FileNotFoundError:
pass
PY
rm -f /run/rtslib_backstore.lock
python3 - "$GUARD_DIR" <<'PY'
import os
import sys

for group in ('pre', 'post'):
root = os.path.join(sys.argv[1], group)
for name in os.listdir(root):
os.unlink(os.path.join(root, name))
os.rmdir(root)
PY
umount "$GUARD_DIR"
printf '2\n' > /proc/sys/vm/drop_caches
grep '^shmem_inode_cache ' /proc/slabinfo > "$RUN_DIR/slabinfo-before-shrink.txt" printf '1\n' > /sys/kernel/slab/shmem_inode_cache/shrink
printf '1\n' > "$PARAM"
grep '^shmem_inode_cache ' /proc/slabinfo > "$RUN_DIR/slabinfo-after-shrink.txt" cat "$RUN_DIR/slabinfo-before-shrink.txt" "$RUN_DIR/slabinfo-after-shrink.txt"

sleep 15
cat "$PARAM"
dmesg > "$RUN_DIR/dmesg-after-trigger.txt" printf 'survived=1\n' | tee "$RUN_DIR/survived.env" exit 42
----- END TCMU REPRODUCER -----

author
#1 lore raw

PATCH 1/1 tcmu: Fix inode use-after-free in global data block pool

The global data block pool can use udev->inode from delayed unmap work after userspace closes the UIO device. tcmu_open() only stores the raw pointer, so VFS can reclaim the inode while the device remains on root_udev. A later find_free_blocks() then dereferences the stale pointer in unmap_mapping_range().

Take an inode reference in tcmu_open() and hold it until the final device reference is released. Replace the pointer under cmdr_lock to synchronize with find_free_blocks(), and drop the previous reference after a reopen. Keep the reference across tcmu_release() because delayed unmap work still needs the inode after the last UIO close.

Fixes b6df4b79a551 ("tcmu: Add global data block pool support")
---
 drivers/target/target_core_user.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/target/target_core_user.c +11 -0
index 80da90a..1bd53cd 100644
@@ -1741,6 +1741,8 @@ static void tcmu_dev_kref_release(struct kref *kref)
 	bitmap_free(udev->data_bitmap);
 	mutex_unlock(&udev->cmdr_lock);
 
+	iput(udev->inode);
+
 	pr_debug("dev_kref_release\n");
 
 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
@@ -1954,12 +1956,21 @@ static int tcmu_mmap_prepare(struct uio_info *info, struct vm_area_desc *desc)
 static int tcmu_open(struct uio_info *info, struct inode *inode)
 {
 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
+	struct inode *old_inode;
 
 	/* O_EXCL not supported for char devs, so fake it? */
 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
 		return -EBUSY;
 
+	/* find_free_blocks() may use the inode after the last UIO close. */
+	ihold(inode);
+
+	mutex_lock(&udev->cmdr_lock);
+	old_inode = udev->inode;
 	udev->inode = inode;
+	mutex_unlock(&udev->cmdr_lock);
+
+	iput(old_inode);
 
 	pr_debug("open\n");
 
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] tcmu: pinning udev->inode past tcmu_release() breaks superblock unmount --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] tcmu: pinning udev->inode past tcmu_release() breaks superblock unmount
--

commit b2536748e9ead622de5208515ec23331a1f19191
Author: Tao Zou <[email protected]>

tcmu: Fix inode use-after-free in global data block pool

This commit takes an inode reference in tcmu_open() under cmdr_lock and holds it until tcmu_dev_kref_release() or a subsequent reopen so find_free_blocks() does not dereference a reclaimed inode after the UIO device is closed.

Quoted 24 lines
drivers/target/target_core_user.c +4 -0
index 80da90accac56..1bd53cdd1d964 100644
@@ -1741,6 +1741,8 @@ static void tcmu_dev_kref_release(struct kref *kref)
 	bitmap_free(udev->data_bitmap);
 	mutex_unlock(&udev->cmdr_lock);
 
+	iput(udev->inode);
+
 	pr_debug("dev_kref_release\n");
 
 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
@@ -1954,12 +1956,21 @@ static int tcmu_mmap_prepare(struct uio_info *info, struct vm_area_desc *desc)
 static int tcmu_open(struct uio_info *info, struct inode *inode)
 {
 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
+	struct inode *old_inode;
 
 	/* O_EXCL not supported for char devs, so fake it? */
 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
 		return -EBUSY;
 
+	/* find_free_blocks() may use the inode after the last UIO close. */

drivers/target/target_core_user.c static int tcmu_mmap_prepare(struct uio_info *info, struct vm_area_desc *desc) · line 1975

Quoted 10 lines
drivers/target/target_core_user.c +7 -0
+	ihold(inode);
+
+	mutex_lock(&udev->cmdr_lock);
+	old_inode = udev->inode;
 	udev->inode = inode;
+	mutex_unlock(&udev->cmdr_lock);
+
+	iput(old_inode);
 
 	pr_debug("open\n");

[Severity: High]
Can holding udev->inode past tcmu_release() leave a busy inode when the filesystem hosting the character device node is unmounted?

After the last file descriptor and all VMAs are closed, __fput() invokes tcmu_release() and drops the file's dentry and vfsmount references. Because ihold() does not pin the mount or sb->s_active, unmounting that filesystem while the TCMU device is still configured reaches generic_shutdown_super() with udev->inode still on sb->s_inodes, triggering CHECK_DATA_CORRUPTION ("VFS: Busy inodes after unmount") and poisoning inode->i_op, inode->i_sb, and inode->i_mapping with VFS_PTR_POISON. Any later find_free_blocks() call or final iput() then dereferences VFS_PTR_POISON.

Since __fput() only calls tcmu_release() after all VMAs mapping the file have been unmapped, could tcmu_release() instead clear udev->inode = NULL under udev->cmdr_lock and find_free_blocks() skip unmap_mapping_range() when udev->inode is NULL?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1