liveupdate: Return -ENOTTY for unknown ioctl numbers
Subsystems (4)
cover liveupdate: Return -ENOTTY for unknown ioctl numbers
luo_ioctl() and luo_session_ioctl() return -EINVAL for a command number outside their ops table, but ENOTTY for an in-table number whose full command does not match. That contradicts the errno contract in include/uapi/linux/liveupdate.h, and it means a program cannot tell an ioctl the kernel does not have from a known ioctl with a bad argument. It also makes isatty() on a LUO fd fail with EINVAL instead of ENOTTY.
Patch 1 returns -ENOIOCTLCMD for out-of-range numbers too. Patch 2 adds a selftest for it.
Chris Down (2):
liveupdate: Return -ENOTTY for unknown ioctl numbers selftests/liveupdate: Test ENOTTY for unknown ioctl numbers
kernel/liveupdate/luo_core.c | 2 +- kernel/liveupdate/luo_session.c | 2 +- .../testing/selftests/liveupdate/liveupdate.c | 35 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-)
base-commit: ec459d99ed222228b3d601818fd1e24822a450d6
Signature · 1 line
2.56.1
PATCH 1/2 liveupdate: Return -ENOTTY for unknown ioctl numbers
include/uapi/linux/liveupdate.h documents the errno contract for /dev/liveupdate and session fds: ENOTTY means "the IOCTL number itself is not supported at all", and EINVAL means "everything about the IOCTL was understood, but a field is not correct". luo_ioctl() and luo_session_ioctl() only keep that promise when the command number lands inside their ops table. If _IOC_NR(cmd) falls outside it, they return -EINVAL, but they return -ENOIOCTLCMD (which userspace sees as ENOTTY) when the number is in the table and the full command does not match.
This matters most for feature detection. Say a program built against a newer uAPI calls an ioctl the running kernel does not have yet, such as LIVEUPDATE_SESSION_GET_NAME on a kernel that predates it. It gets EINVAL, which is exactly what a known ioctl with a bad argument returns, so it cannot tell "unsupported" from "I passed something wrong". A kernel that returns ENOTTY for the same number is unambiguous.
It also shows up when something probes an fd it did not create, which session fds allow since they can be passed around (eg. via a service manager's fd store). Current glibc sends TCGETS2 from isatty() and tcgetattr(), and its number is outside both tables:
isatty(luo_fd) -> EINVAL
isatty(session_fd) -> EINVAL
ioctl(session_fd, TCGETS, buf) -> EINVAL
ioctl(session_fd, _IO('T', 0x40), buf) -> ENOTTYDocumentation/driver-api/ioctl.rst also says an unknown command should get -ENOTTY or -ENOIOCTLCMD and that returning -EINVAL there is wrong.
Let's return -ENOIOCTLCMD for out-of-range numbers too, as the in-range mismatch already does. This does change the errno for a released interface, but the selftests only expect EINVAL for known commands with bad arguments, and those still get EINVAL, E2BIG and so on from the later checks. systemd's fd_get_luo_session_name() already treats ENOTTY and EINVAL alike when it falls back for a missing GET_NAME, and its other LUO ioctls only issue commands the kernel knows.
--- kernel/liveupdate/luo_core.c | 2 +- kernel/liveupdate/luo_session.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
kernel/liveupdate/luo_core.c +1 -1
@@ -394,7 +394,7 @@ static long luo_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) nr =_IOC_NR (cmd);if (nr -LIVEUPDATE_CMD_BASE >=ARRAY_SIZE (luo_ioctl_ops)) -return -EINVAL ; +return -ENOIOCTLCMD ; ucmd.ubuffer = (void __user *)arg; err =get_user (ucmd.user_size, (u32 __user *)ucmd.ubuffer);
kernel/liveupdate/luo_session.c +1 -1
@@ -414,7 +414,7 @@ static long luo_session_ioctl(struct file *filep, unsigned int cmd, nr =_IOC_NR (cmd);if ((nr -LIVEUPDATE_CMD_SESSION_BASE ) >=ARRAY_SIZE (luo_session_ioctl_ops)) -return -EINVAL ; +return -ENOIOCTLCMD ; ucmd.ubuffer = (void __user *)arg; ret =get_user (ucmd.user_size, (u32 __user *)ucmd.ubuffer);
Signature · 1 line
2.56.1
PATCH 2/2 selftests/liveupdate: Test ENOTTY for unknown ioctl numbers
In reply to Chris DownCheck that command numbers outside the ioctl tables of /dev/liveupdate and of a session fd fail with ENOTTY, not EINVAL.
--- .../testing/selftests/liveupdate/liveupdate.c | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+)
tools/testing/selftests/liveupdate/liveupdate.c +35 -0
@@ -530,4 +530,39 @@ TEST_F(liveupdate_device, preserve_many_files)ASSERT_EQ (close (session_fd),0 ); } +/* +* Test Case: Unknown IOCTL Numbers +* +* Verifies that command numbers outside the ioctl tables of /dev/liveupdate +* and of a session fd fail with ENOTTY, not EINVAL, which is reserved for a +* known command with a bad argument. +*/ +TEST_F(liveupdate_device, unknown_ioctl_number) +{ +char buf[64 ] = {}; +int session_fd; + + self->fd1 =open (LIVEUPDATE_DEV ,O_RDWR ); +if (self->fd1 <0 && errno ==ENOENT ) +SKIP (return,"%s does not exist" ,LIVEUPDATE_DEV ); +ASSERT_GE (self->fd1,0 ); + + session_fd =luo_create_session (self->fd1,"unknown-ioctl-test" ); +ASSERT_GE (session_fd,0 ); + +EXPECT_LT (ioctl (self->fd1,_IO (LIVEUPDATE_IOCTL_TYPE ,0x3f ), buf),0 ); +EXPECT_EQ (errno,ENOTTY ); +EXPECT_LT (ioctl (self->fd1,_IO (LIVEUPDATE_IOCTL_TYPE ,0xff ), buf),0 ); +EXPECT_EQ (errno,ENOTTY ); + +EXPECT_LT (ioctl (session_fd,TCGETS , buf),0 ); +EXPECT_EQ (errno,ENOTTY ); +EXPECT_LT (ioctl (session_fd,_IO (LIVEUPDATE_IOCTL_TYPE ,0x3f ), buf),0 ); +EXPECT_EQ (errno,ENOTTY ); +EXPECT_LT (ioctl (session_fd,_IO (LIVEUPDATE_IOCTL_TYPE ,0xff ), buf),0 ); +EXPECT_EQ (errno,ENOTTY ); + +ASSERT_EQ (close (session_fd),0 ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.56.1
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Quoted 39 lines · Chris Down
include/uapi/linux/liveupdate.hdocuments the errno contract for/dev/liveupdateand session fds: ENOTTY means "the IOCTL number itself is not supported at all", and EINVAL means "everything about the IOCTL was understood, but a field is not correct".luo_ioctl()andluo_session_ioctl()only keep that promise when the command number lands inside their ops table. If_IOC_NR(cmd)falls outside it, they return-EINVAL, but they return-ENOIOCTLCMD(which userspace sees as ENOTTY) when the number is in the table and the full command does not match.This matters most for feature detection. Say a program built against a newer uAPI calls an ioctl the running kernel does not have yet, such as
LIVEUPDATE_SESSION_GET_NAMEon a kernel that predates it. It gets EINVAL, which is exactly what a known ioctl with a bad argument returns, so it cannot tell "unsupported" from "I passed something wrong". A kernel that returns ENOTTY for the same number is unambiguous.It also shows up when something probes an fd it did not create, which session fds allow since they can be passed around (eg. via a service manager's fd store). Current glibc sends TCGETS2 from
isatty()andtcgetattr(), and its number is outside both tables:isatty(luo_fd) -> EINVAL isatty(session_fd) -> EINVAL ioctl(session_fd, TCGETS, buf) -> EINVAL ioctl(session_fd, _IO('T', 0x40), buf) -> ENOTTY
Documentation/driver-api/ioctl.rstalso says an unknown command should get-ENOTTYor-ENOIOCTLCMDand that returning-EINVALthere is wrong.Let's return
-ENOIOCTLCMDfor out-of-range numbers too, as the in-range mismatch already does. This does change the errno for a released interface, but the selftests only expect EINVAL for known commands with bad arguments, and those still get EINVAL, E2BIG and so on from the later checks. systemd'sfd_get_luo_session_name()already treats ENOTTY and EINVAL alike when it falls back for a missingGET_NAME, and its other LUO ioctls only issue commands the kernel knows.
Quoted 3 lines
Fixes 81cd25d263a1 ("liveupdate: luo_core: add user interface")Fixes 16cec0d26521 ("liveupdate: luo_session: add ioctls for file preservation")Signed-off-by Chris Down <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerRe: [PATCH 2/2] selftests/liveupdate: Test ENOTTY for unknown ioctl numbers
In reply to Chris DownQuoted 30 lines · Chris Down
Check that command numbers outside the ioctl tables of
/dev/liveupdateand of a session fd fail with ENOTTY, not EINVAL.Signed-off-by Chris Down <[email protected]>--- .../testing/selftests/liveupdate/liveupdate.c | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+)tools/testing/selftests/liveupdate/liveupdate.c +14
-0@@ -530,4 +530,39 @@ TEST_F(liveupdate_device, preserve_many_files)ASSERT_EQ (close (session_fd),0 ); } +/* +* Test Case: Unknown IOCTL Numbers +* +* Verifies that command numbers outside the ioctl tables of /dev/liveupdate +* and of a session fd fail with ENOTTY, not EINVAL, which is reserved for a +* known command with a bad argument. +*/ +TEST_F(liveupdate_device, unknown_ioctl_number) +{ +char buf[64 ] = {}; +int session_fd; + + self->fd1 =open (LIVEUPDATE_DEV ,O_RDWR ); +if (self->fd1 <0 && errno ==ENOENT )
tools/testing/selftests/liveupdate/liveupdate.c TEST_F(liveupdate_device, preserve_many_files) · line 556
Quoted 10 lines
tools/testing/selftests/liveupdate/liveupdate.c +10
-0+SKIP (return,"%s does not exist" ,LIVEUPDATE_DEV ); +ASSERT_GE (self->fd1,0 ); + + session_fd =luo_create_session (self->fd1,"unknown-ioctl-test" ); +ASSERT_GE (session_fd,0 ); + +EXPECT_LT (ioctl (self->fd1,_IO (LIVEUPDATE_IOCTL_TYPE ,0x3f ), buf),0 ); +EXPECT_EQ (errno,ENOTTY ); +EXPECT_LT (ioctl (self->fd1,_IO (LIVEUPDATE_IOCTL_TYPE ,0xff ), buf),0 ); +EXPECT_EQ (errno,ENOTTY );
Why 0x3f and 0xff? I assume you use this because LIVEUPDATE_CMD_SESSION_BASE is 0x40, so you want to test for a command before and after the base. This makes sense for a session FD, but for /dev/liveupdate, LIVEUPDATE_CMD_BASE is 0x00, so both 0x3f and 0xff would test for the same condition, right?
Also, please give these numbers a name. Otherwise we will forget why these were used in the first place.
Quoted 12 lines
tools/testing/selftests/liveupdate/liveupdate.c +11
-0+ +EXPECT_LT (ioctl (session_fd,TCGETS , buf),0 ); +EXPECT_EQ (errno,ENOTTY ); +EXPECT_LT (ioctl (session_fd,_IO (LIVEUPDATE_IOCTL_TYPE ,0x3f ), buf),0 ); +EXPECT_EQ (errno,ENOTTY ); +EXPECT_LT (ioctl (session_fd,_IO (LIVEUPDATE_IOCTL_TYPE ,0xff ), buf),0 ); +EXPECT_EQ (errno,ENOTTY ); + +ASSERT_EQ (close (session_fd),0 ); +} +TEST_HARNESS_MAIN
Signature · 2 lines
Regards, Pratyush Yadav