Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH 0/1] tcmu: Fix inode use-after-free in global data block pool
@ 2026-11-27  2:33 Tao Zou
  2026-11-27  2:33 ` [PATCH 1/1] " Tao Zou
  0 siblings, 1 reply; 3+ messages in thread
From: Tao Zou @ 2026-11-27  2:33 UTC (permalink / raw)
  To: Bodo Stroesser, Martin K . Petersen; +Cc: linux-scsi, target-devel, Tao Zou

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 -----

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27  2:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  2:33 [PATCH 0/1] tcmu: Fix inode use-after-free in global data block pool Tao Zou
2026-11-27  2:33 ` [PATCH 1/1] " Tao Zou
2026-11-27  2:45   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox