swapon04: Report TCONF when SELinux denies loop device I/O
PATCH 1/1 swapon04: Report TCONF when SELinux denies loop device I/O
Kernels without commit f2fed441c69b ("loop: stop using vfs_iter_{read,write} for buffered I/O") access the loop backing file through vfs_iter_{read,write}(), which run the LSM file permission checks with the credentials of the loop worker kernel thread.
swapon04 backs the loop device with a file on the freshly formatted test filesystem. That file has no SELinux label, and strict policies such as Android's do not allow the kernel domain to access unlabeled files. Every I/O on the loop device then fails with EIO and the test breaks on the first mkswap call:
tst_cmd.c:114: TBROK: 'mkswap' exited with a non-zero code 1
avc: denied { read } for comm="kworker/u8:0" dev="loop52" ino=12
scontext=u:r:kernel:s0 tcontext=u:object_r:unlabeled:s0 tclass=file permissive=0
This is a policy restriction rather than a swap bug, and it does not depend on how LTP is built: running the system's losetup and mkswap on such a file fails the same way. The test passes on the same 6.1 kernel once SELinux is switched to permissive mode, and on a 6.18 kernel in enforcing mode.
A kernel version check would not help, since that commit has been backported to v6.12.25, and older kernels pass the test when the policy does not get in the way. Instead, read the first sector of the loop device in setup() and report TCONF if it fails with EIO while SELinux is enforcing. On the 6.1 kernel above, ext2, ext3 and ext4 are now skipped, while tmpfs, whose files the kernel domain is allowed to access, still passes.
--- testcases/kernel/syscalls/swapon/swapon04.c | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+)
testcases/kernel/syscalls/swapon/swapon04.c +29 -0
@@ -84,6 +84,33 @@ static struct tcase { }, }; +/* +* Kernels without commit f2fed441c69b ("loop: stop using +* vfs_iter_{read,write} for buffered I/O") access the backing file through +* vfs_iter_{read,write}(), which run LSM permission checks with the +* credentials of the loop worker kernel thread. Strict SELinux policies, +* such as Android's, do not allow the kernel to access the unlabeled files +* on the freshly formatted test filesystem, so any I/O on the loop device +* fails with EIO. +*/ +static void check_loop_io (void ) +{ +char buf[512 ]; +int fd; + + fd =SAFE_OPEN (loop_dev,O_RDONLY ); +TEST (read (fd, buf,sizeof (buf))); +SAFE_CLOSE (fd); + +if (TST_RET == (long )sizeof (buf)) +return ; + +if (TST_RET ==-1 &&TST_ERR ==EIO &&tst_selinux_enforcing ()) +tst_brk (TCONF |TTERRNO ,"I/O on %s is likely denied by SELinux" , loop_dev); + +tst_brk (TBROK |TTERRNO ,"read(%s) returned %ld" , loop_dev,TST_RET ); +} +static void setup (void ) {char discard_path[PATH_MAX ];
@@ -114,6 +141,8 @@ static void setup(void)tst_brk (TBROK ,"Failed to attach %s to %s" , loop_dev,BACKING_FILE ); loop_attached =1 ; +check_loop_io (); +snprintf (discard_path,sizeof (discard_path),"/sys/block/loop%d/queue/discard_max_bytes" , loop_dev_id);if (FILE_SCANF (discard_path,"%lu" , &discard_max_bytes) !=0 || discard_max_bytes ==0 ) {
Signature · 5 lines
2.56.0.385.gd3acb90ef8-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wake,
Quoted 1 line · Wake Liu via ltp
swapon04: Report TCONF when SELinux denies loop device I/O
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 13 lines
testcases/kernel/syscalls/swapon/swapon04.c +13
-0+/* +* Kernels without commit f2fed441c69b ("loop: stop using +* vfs_iter_{read,write} for buffered I/O") access the backing file through +* vfs_iter_{read,write}(), which run LSM permission checks with the +* credentials of the loop worker kernel thread. Strict SELinux policies, +* such as Android's, do not allow the kernel to access the unlabeled files +* on the freshly formatted test filesystem, so any I/O on the loop device +* fails with EIO. +*/ +static void check_loop_io (void ) +{ +char buf[512 ]; +int fd;
testcases/kernel/syscalls/swapon/swapon04.c static struct tcase { · line 109
Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon04.c +10
-0+ + fd =SAFE_OPEN (loop_dev,O_RDONLY ); +TEST (read (fd, buf,sizeof (buf))); +SAFE_CLOSE (fd); + +if (TST_RET == (long )sizeof (buf)) +return ; + +if (TST_RET ==-1 &&TST_ERR ==EIO &&tst_selinux_enforcing ()) +tst_brk (TCONF |TTERRNO ,"I/O on %s is likely denied by SELinux" , loop_dev);
If nothing else we whould check for the selinux as a first thing in the function.
if (!tst_selinux_enforcing()) return; fd = SAFE_OPEN(loop_dev, ...); ...
And the check should be skipped on 6.15 and newer, where this problem does not happen.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/1 swapon04: Report TCONF when SELinux denies loop device I/O
In reply to Wake Liu via ltpKernels without commit f2fed441c69b ("loop: stop using vfs_iter_{read,write} for buffered I/O") access the loop backing file through vfs_iter_{read,write}(), which run the LSM file permission checks with the credentials of the loop worker kernel thread.
swapon04 backs the loop device with a file on the freshly formatted test filesystem. That file has no SELinux label, and strict policies such as Android's do not allow the kernel domain to access unlabeled files. Every I/O on the loop device then fails with EIO and the test breaks on the first mkswap call:
tst_cmd.c:114: TBROK: 'mkswap' exited with a non-zero code 1
avc: denied { read } for comm="kworker/u8:0" dev="loop52" ino=12
scontext=u:r:kernel:s0 tcontext=u:object_r:unlabeled:s0 tclass=file permissive=0
This is a policy restriction rather than a swap bug, and it does not depend on how LTP is built: running the system's losetup and mkswap on such a file fails the same way. The test passes on the same 6.1 kernel once SELinux is switched to permissive mode, and on a 6.18 kernel in enforcing mode.
The commit was merged in v6.15, so v6.15 and newer are not affected. On older kernels the version alone does not tell whether the test can
run: the commit has been backported to stable kernels such as v6.12.25 and v6.14.4, and kernels without it still pass the test unless the policy gets in the way.
So on kernels older than v6.15 with SELinux in enforcing mode, read the first sector of the loop device in setup() and report TCONF if it fails with EIO. On the 6.1 kernel above, ext2, ext3 and ext4 are now skipped, while tmpfs, whose files the kernel domain is allowed to access, still passes.
---
Changes in v2:
- Return early on v6.15 and newer and when SELinux is not enforcing, so the loop device is only probed where the denial can happen (Cyril).
- Mention v6.15 and the stable backports in the comment and the commit message.
v1: https://lore.kernel.org/ltp/[email protected]/
testcases/kernel/syscalls/swapon/swapon04.c | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+)
testcases/kernel/syscalls/swapon/swapon04.c +35 -0
@@ -84,6 +84,39 @@ static struct tcase { }, }; +/* +* Kernels without commit f2fed441c69b ("loop: stop using +* vfs_iter_{read,write} for buffered I/O"), merged in v6.15, access the +* backing file through vfs_iter_{read,write}(), which run LSM permission +* checks with the credentials of the loop worker kernel thread. Strict +* SELinux policies, such as Android's, do not allow the kernel to access +* the unlabeled files on the freshly formatted test filesystem, so any I/O +* on the loop device fails with EIO. +* +* The commit has been backported to some stable kernels (e.g. v6.12.25), +* so on older kernels the loop device has to be probed. +*/ +static void check_loop_io (void ) +{ +char buf[512 ]; +int fd; + +if (tst_kvercmp (6 ,15 ,0 ) >=0 || !tst_selinux_enforcing ()) +return ; + + fd =SAFE_OPEN (loop_dev,O_RDONLY ); +TEST (read (fd, buf,sizeof (buf))); +SAFE_CLOSE (fd); + +if (TST_RET == (long )sizeof (buf)) +return ; + +if (TST_RET ==-1 &&TST_ERR ==EIO ) +tst_brk (TCONF |TTERRNO ,"I/O on %s is likely denied by SELinux" , loop_dev); + +tst_brk (TBROK |TTERRNO ,"read(%s) returned %ld" , loop_dev,TST_RET ); +} +static void setup (void ) {char discard_path[PATH_MAX ];
@@ -114,6 +147,8 @@ static void setup(void)tst_brk (TBROK ,"Failed to attach %s to %s" , loop_dev,BACKING_FILE ); loop_attached =1 ; +check_loop_io (); +snprintf (discard_path,sizeof (discard_path),"/sys/block/loop%d/queue/discard_max_bytes" , loop_dev_id);if (FILE_SCANF (discard_path,"%lu" , &discard_max_bytes) !=0 || discard_max_bytes ==0 ) {
Signature · 5 lines
2.56.0.176.g3575e65682-goog -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wake,
Quoted 1 line · Wake Liu
swapon04: Report TCONF when SELinux denies loop device I/O
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi,
Regards,
Avinesh
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wake,
unfortunately I don't see other options available but skipping the test "guessing" that SELinux enforcement is producing an EIO error.
But did you check if other tests are having the same issue?
Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Andrea,
Quoted 1 line · Andrea Cervesato
But did you check if other tests are having the same issue?
Yes. Only loop devices backed by a file on the freshly formatted test filesystem are affected, since those files have no SELinux label, and swapon04 is the only test that does that.
The other loop users (ioctl_loop*, ioctl09, uevent01, sys_block_loop01, isofs.sh and quota_remount_test01.sh) keep their images in TMPDIR, like the test device itself, and Android's policy allows the kernel domain to access files there. Swap files on the test filesystem are fine too, since swap I/O does not go through vfs_iter_{read,write}().
Thanks,
Wake
Quoted 13 lines · Andrea Cervesato
Hi Wake,
unfortunately I don't see other options available but skipping the test "guessing" that SELinux enforcement is producing an EIO error.
But did you check if other tests are having the same issue?
Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 5 lines
Best Regards, Wake Liu -- Mailing list info: https://lists.linux.it/listinfo/ltp