[PATCH v2 1/2] blktrace: require CAP_SYS_ADMIN for the blktrace interface

From: Tao Cui

Date: Wed Nov 11 2026 - 09:52:56 EST


From: Tao Cui <cuitao@xxxxxxxxxx>

BLKTRACESETUP allocates user-sized relay buffers and sets up a trace
that outlives the creating fd. Every command of the interface,
BLKTRACESETUP, BLKTRACESETUP2, BLKTRACESTART, BLKTRACESTOP,
BLKTRACETEARDOWN and, on x86_64 with CONFIG_COMPAT, the 32-bit
variant can be issued by anyone who can open the block device node,
and the sysfs trace attribute group reaches the same tracing state
without any check. A container that is handed the node can hold
trace state and relay memory on a shared queue; the relay files
themselves stay root-only (0400).

Gate blk_trace_ioctl() and sysfs_blk_trace_attr_store() with
CAP_SYS_ADMIN. The ioctls return -EPERM, the sysfs store -EACCES
like the md store callbacks it resembles. Unprivileged callers now
get one answer that depends only on their credentials, instead of
values that revealed whether a trace existed and was running.

Traces survive the creating fd by design, so a creator that drops
its privileges can no longer tear down its own trace. Such traces
wait for a privileged teardown or for the queue to go away;
blk_trace_remove() from sg_device_destroy() and
blk_trace_shutdown() from blk_unregister_queue() bound that wait for
unplugged devices.
Document the sysfs attributes in Documentation/ABI, where the group
has been missing since it was added.

Signed-off-by: Tao Cui <cuitao@xxxxxxxxxx>
---
Documentation/ABI/stable/sysfs-block | 18 ++++++++++++++++++
kernel/trace/blktrace.c | 6 ++++++
2 files changed, 24 insertions(+)

diff --git a/Documentation/ABI/stable/sysfs-block b/Documentation/ABI/stable/sysfs-block
index 900b3fc4c72d..177c4b95a47d 100644
--- a/Documentation/ABI/stable/sysfs-block
+++ b/Documentation/ABI/stable/sysfs-block
@@ -939,3 +939,21 @@ Description:
== ==============================================

For more details refer Documentation/admin-guide/iostats.rst
+
+
+What: /sys/block/<disk>/trace/{enable,act_mask,pid,start_lba,end_lba}
+ /sys/block/<disk>/<partition>/trace/{enable,act_mask,pid,start_lba,end_lba}
+Date: January 2009
+Contact: Steven Rostedt <rostedt@xxxxxxxxxxx>, Masami Hiramatsu <mhiramat@xxxxxxxxxx>
+Description:
+ Configure blktrace for this device's request queue.
+ "enable" creates (1) or removes (0) the per-queue trace
+ bookkeeping; "act_mask" selects the traced action
+ categories, either as a number or as a list of category
+ names; "pid" restricts tracing to one process;
+ "start_lba" and "end_lba" bound the traced sector range.
+ Reading an attribute shows the current setting and needs
+ no privilege; writing any attribute requires
+ CAP_SYS_ADMIN. Enabling from sysfs does not set up
+ relay buffers; full tracing is armed through the
+ BLKTRACESETUP ioctl.
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 8cd2520b4c99..f56d57a924bf 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -927,6 +927,9 @@ int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
int ret, start = 0;
char b[BDEVNAME_SIZE];

+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
switch (cmd) {
case BLKTRACESETUP2:
snprintf(b, sizeof(b), "%pg", bdev);
@@ -2079,6 +2082,9 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
u64 value;
ssize_t ret = -EINVAL;

+ if (!capable(CAP_SYS_ADMIN))
+ return -EACCES;
+
if (count == 0)
goto out;

--
2.53.0