From: Tao Cui <cui.tao@linux.dev>
To: axboe@kernel.dk, rostedt@goodmis.org, mhiramat@kernel.org,
dgilbert@interlog.com, James.Bottomley@HansenPartnership.com,
mkp@kernel.org
Cc: linux-block@vger.kernel.org, linux-scsi@vger.kernel.org,
linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
mathieu.desnoyers@efficios.com, cui.tao@linux.dev,
Tao Cui <cuitao@kylinos.cn>
Subject: [PATCH v2 2/2] scsi: sg: Require CAP_SYS_ADMIN for BLKTRACE passthrough ioctls
Date: Wed, 11 Nov 2026 22:50:34 +0800 [thread overview]
Message-ID: <20261111145034.248717-3-cui.tao@linux.dev> (raw)
In-Reply-To: <20261111145034.248717-1-cui.tao@linux.dev>
From: Tao Cui <cuitao@kylinos.cn>
sg_ioctl_common() calls blk_trace_setup(), blk_trace_startstop()
and blk_trace_remove() directly instead of going through
blk_trace_ioctl(), so gating the block side does not close the sg
passthrough: an unprivileged process with access to an /dev/sg node
could still allocate relay buffers and control tracing state on the
underlying request queue.
Require CAP_SYS_ADMIN for the four passthrough commands. The check
sits after the command log line so the new rejections stay visible
in the ioctl trace, and it is qualified with
IS_ENABLED(CONFIG_BLK_DEV_IO_TRACE) so kernels without blktrace
keep returning -ENOTTY for these commands instead of a new -EPERM.
BLKTRACE ioctls through sg now get the same answer they would get
through the block device node: -EPERM for unprivileged callers,
regardless of the trace or queue state behind the node.
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
drivers/scsi/sg.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 6551f1284d44..f863979604ef 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -911,6 +911,13 @@ static int put_compat_request_table(struct compat_sg_req_info __user *o,
}
#endif
+/* keep in sync with the commands blk_trace_ioctl() gates */
+static bool sg_is_blktrace_cmd(unsigned int cmd)
+{
+ return cmd == BLKTRACESETUP || cmd == BLKTRACESTART ||
+ cmd == BLKTRACESTOP || cmd == BLKTRACETEARDOWN;
+}
+
static long
sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,
unsigned int cmd_in, void __user *p)
@@ -922,6 +929,9 @@ sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
"sg_ioctl: cmd=0x%x\n", (int) cmd_in));
+ if (IS_ENABLED(CONFIG_BLK_DEV_IO_TRACE) &&
+ sg_is_blktrace_cmd(cmd_in) && !capable(CAP_SYS_ADMIN))
+ return -EPERM;
read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
switch (cmd_in) {
--
2.53.0
prev parent reply other threads:[~2026-11-11 14:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 14:50 [PATCH v2 0/2] blktrace: gate the blktrace interface behind CAP_SYS_ADMIN Tao Cui
2026-11-11 14:50 ` [PATCH v2 1/2] blktrace: require CAP_SYS_ADMIN for the blktrace interface Tao Cui
2026-11-11 14:50 ` Tao Cui [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261111145034.248717-3-cui.tao@linux.dev \
--to=cui.tao@linux.dev \
--cc=James.Bottomley@HansenPartnership.com \
--cc=axboe@kernel.dk \
--cc=cuitao@kylinos.cn \
--cc=dgilbert@interlog.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=mkp@kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®