mbox series

[V2,0/1] selinux-testsuite: Add filesystem tests

Message ID 20200109150709.360345-1-richard_c_haines@btinternet.com (mailing list archive)
Headers show
Series selinux-testsuite: Add filesystem tests | expand

Message

Richard Haines Jan. 9, 2020, 3:07 p.m. UTC
These tests should cover all the areas in selinux/hooks.c that touch
the 'filesystem' class. Each hooks.c function is listed in the 'test'
script as there are some permissions that are checked in multiple places.

Tested on Fedora 31 and Rawhide (5.5 for the new watch perm).

V2 Changes:
1) If udisks(8) daemon is running, stop then restart after tests. The tests
   run faster and stops the annoying habit of adding mounts to the 'files'
   app on the desktop. Supports /usr/bin/systemctl or /usr/sbin/service
   More importantly it stops interferance with the '*context=' tests as it
   can cause intermittent failures. Tested by running 'test' in a continuous
   loop with udisks enabled, and then again disabled.
   Loop 200 times, with udisks failed between 1 to 70 iterations, without
   udisks, no failures.
2) Add "#define QFMT_VFS_V0 2" to quotas_test.c to fix a RHEL/CentOS 7 and
   below build issue.
3) Build new file context based on the original in
   create_file_change_context.c
4) Use "runcon `id -Z` quotacheck ..." to resolve RHEL-6 test run issue.
5) Fix free() contexts in create_file_change_context.c and
   check_mount_context.c

To test fanotify fs watch perm on 5.5+:
1) Build the testsuite policy first:
make -C policy load

2) Add the following CIL statements to watch.cil and install:
semodule -i watch.cil

(common filesystem (watch))
(classcommon filesystem filesystem)
(allow test_filesystem_t self(filesystem (watch)))
; Until 'fs_watch_all_fs(test_filesystem_t)' in Policy use:
(allow test_filesystem_t fs_t (filesystem (watch)))
; Required if notify policy enabled
;(allow test_filesystem_t self (dir (watch_sb)))

3) Edit /usr/share/selinux/devel/include/support/all_perms.spt
   and insert the 'watch' permission at:

define(`all_filesystem_perms',`{ mount remount ..... watch }')

Richard Haines (1):
  selinux-testsuite: Add filesystem tests

 defconfig                                     |   6 +
 policy/Makefile                               |   4 +
 policy/test_filesystem.te                     | 324 +++++++
 tests/Makefile                                |   7 +
 tests/filesystem/.gitignore                   |  11 +
 tests/filesystem/Makefile                     |  16 +
 tests/filesystem/check_file_context.c         |  75 ++
 tests/filesystem/check_mount_context.c        | 125 +++
 tests/filesystem/create_file.c                |  79 ++
 tests/filesystem/create_file_change_context.c | 146 +++
 tests/filesystem/fanotify_fs.c                |  79 ++
 tests/filesystem/fs_relabel.c                 |  72 ++
 tests/filesystem/grim_reaper.c                |  63 ++
 tests/filesystem/mount.c                      | 130 +++
 tests/filesystem/quotas_test.c                | 143 +++
 tests/filesystem/statfs_test.c                |  65 ++
 tests/filesystem/test                         | 882 ++++++++++++++++++
 tests/filesystem/umount.c                     |  84 ++
 18 files changed, 2311 insertions(+)
 create mode 100644 policy/test_filesystem.te
 create mode 100644 tests/filesystem/.gitignore
 create mode 100644 tests/filesystem/Makefile
 create mode 100644 tests/filesystem/check_file_context.c
 create mode 100644 tests/filesystem/check_mount_context.c
 create mode 100644 tests/filesystem/create_file.c
 create mode 100644 tests/filesystem/create_file_change_context.c
 create mode 100644 tests/filesystem/fanotify_fs.c
 create mode 100644 tests/filesystem/fs_relabel.c
 create mode 100644 tests/filesystem/grim_reaper.c
 create mode 100644 tests/filesystem/mount.c
 create mode 100644 tests/filesystem/quotas_test.c
 create mode 100644 tests/filesystem/statfs_test.c
 create mode 100755 tests/filesystem/test
 create mode 100644 tests/filesystem/umount.c

Comments

Stephen Smalley Jan. 9, 2020, 6:04 p.m. UTC | #1
On 1/9/20 10:07 AM, Richard Haines wrote:
> These tests should cover all the areas in selinux/hooks.c that touch
> the 'filesystem' class. Each hooks.c function is listed in the 'test'
> script as there are some permissions that are checked in multiple places.
> 
> Tested on Fedora 31 and Rawhide (5.5 for the new watch perm).
> 
> V2 Changes:
> 1) If udisks(8) daemon is running, stop then restart after tests. The tests
>     run faster and stops the annoying habit of adding mounts to the 'files'
>     app on the desktop. Supports /usr/bin/systemctl or /usr/sbin/service
>     More importantly it stops interferance with the '*context=' tests as it
>     can cause intermittent failures. Tested by running 'test' in a continuous
>     loop with udisks enabled, and then again disabled.
>     Loop 200 times, with udisks failed between 1 to 70 iterations, without
>     udisks, no failures.

Wondering why udisks is causing failures - that seems like another bug.
Richard Haines Jan. 9, 2020, 8:36 p.m. UTC | #2
On Thu, 2020-01-09 at 13:04 -0500, Stephen Smalley wrote:
> On 1/9/20 10:07 AM, Richard Haines wrote:
> > These tests should cover all the areas in selinux/hooks.c that
> > touch
> > the 'filesystem' class. Each hooks.c function is listed in the
> > 'test'
> > script as there are some permissions that are checked in multiple
> > places.
> > 
> > Tested on Fedora 31 and Rawhide (5.5 for the new watch perm).
> > 
> > V2 Changes:
> > 1) If udisks(8) daemon is running, stop then restart after tests.
> > The tests
> >     run faster and stops the annoying habit of adding mounts to the
> > 'files'
> >     app on the desktop. Supports /usr/bin/systemctl or
> > /usr/sbin/service
> >     More importantly it stops interferance with the '*context='
> > tests as it
> >     can cause intermittent failures. Tested by running 'test' in a
> > continuous
> >     loop with udisks enabled, and then again disabled.
> >     Loop 200 times, with udisks failed between 1 to 70 iterations,
> > without
> >     udisks, no failures.
> 
> Wondering why udisks is causing failures - that seems like another
> bug.

With udisk2 enabled, 99% of the time the 'rootcontext=' test fails (the
1% is 'defcontext='). However if I run this test on its own, it does
not fail. If I add the 'context=' test before and run, the
'rootcontext=' will fail at some point.

If I add a short delay as shown in the 'context=' sequence, the fault
does not occur:
-- Start --
system("losetup -d $dev 2>/dev/null");
system("sleep 0.01");
get_loop_dev();
attach_dev();

# Mount again with no xttr support
$context2_opts =
"context=system_u:object_r:test_filesystem_context_t:s0";
-- End --

It could be udisk2 has a timing problem as the losetup(8) man page '-d' 
entry reads:
Note that since Linux v3.7 kernel uses "lazy  device destruction". The
detach operation does not return EBUSY error anymore if device is
actively used by system, but it  is  marked by autoclear flag and
destroyed later.

But then again it could be something else !!!!
Ondrej Mosnacek Jan. 9, 2020, 9:01 p.m. UTC | #3
On Thu, Jan 9, 2020 at 9:36 PM Richard Haines
<richard_c_haines@btinternet.com> wrote:
> On Thu, 2020-01-09 at 13:04 -0500, Stephen Smalley wrote:
> > On 1/9/20 10:07 AM, Richard Haines wrote:
> > > These tests should cover all the areas in selinux/hooks.c that
> > > touch
> > > the 'filesystem' class. Each hooks.c function is listed in the
> > > 'test'
> > > script as there are some permissions that are checked in multiple
> > > places.
> > >
> > > Tested on Fedora 31 and Rawhide (5.5 for the new watch perm).
> > >
> > > V2 Changes:
> > > 1) If udisks(8) daemon is running, stop then restart after tests.
> > > The tests
> > >     run faster and stops the annoying habit of adding mounts to the
> > > 'files'
> > >     app on the desktop. Supports /usr/bin/systemctl or
> > > /usr/sbin/service
> > >     More importantly it stops interferance with the '*context='
> > > tests as it
> > >     can cause intermittent failures. Tested by running 'test' in a
> > > continuous
> > >     loop with udisks enabled, and then again disabled.
> > >     Loop 200 times, with udisks failed between 1 to 70 iterations,
> > > without
> > >     udisks, no failures.
> >
> > Wondering why udisks is causing failures - that seems like another
> > bug.
>
> With udisk2 enabled, 99% of the time the 'rootcontext=' test fails (the
> 1% is 'defcontext='). However if I run this test on its own, it does
> not fail. If I add the 'context=' test before and run, the
> 'rootcontext=' will fail at some point.
>
> If I add a short delay as shown in the 'context=' sequence, the fault
> does not occur:
> -- Start --
> system("losetup -d $dev 2>/dev/null");
> system("sleep 0.01");
> get_loop_dev();
> attach_dev();

Can you try putting `udevadm settle` instead of the sleep there? I
remember having some issues with udev race conditions a long time ago
and I think that helped. (But I'm not sure at all if that's the right
fix...)

>
> # Mount again with no xttr support
> $context2_opts =
> "context=system_u:object_r:test_filesystem_context_t:s0";
> -- End --
>
> It could be udisk2 has a timing problem as the losetup(8) man page '-d'
> entry reads:
> Note that since Linux v3.7 kernel uses "lazy  device destruction". The
> detach operation does not return EBUSY error anymore if device is
> actively used by system, but it  is  marked by autoclear flag and
> destroyed later.
>
> But then again it could be something else !!!!
>
>
Richard Haines Jan. 10, 2020, 2:28 p.m. UTC | #4
On Thu, 2020-01-09 at 22:01 +0100, Ondrej Mosnacek wrote:
> On Thu, Jan 9, 2020 at 9:36 PM Richard Haines
> <richard_c_haines@btinternet.com> wrote:
> > On Thu, 2020-01-09 at 13:04 -0500, Stephen Smalley wrote:
> > > On 1/9/20 10:07 AM, Richard Haines wrote:
> > > > These tests should cover all the areas in selinux/hooks.c that
> > > > touch
> > > > the 'filesystem' class. Each hooks.c function is listed in the
> > > > 'test'
> > > > script as there are some permissions that are checked in
> > > > multiple
> > > > places.
> > > > 
> > > > Tested on Fedora 31 and Rawhide (5.5 for the new watch perm).
> > > > 
> > > > V2 Changes:
> > > > 1) If udisks(8) daemon is running, stop then restart after
> > > > tests.
> > > > The tests
> > > >     run faster and stops the annoying habit of adding mounts to
> > > > the
> > > > 'files'
> > > >     app on the desktop. Supports /usr/bin/systemctl or
> > > > /usr/sbin/service
> > > >     More importantly it stops interferance with the '*context='
> > > > tests as it
> > > >     can cause intermittent failures. Tested by running 'test'
> > > > in a
> > > > continuous
> > > >     loop with udisks enabled, and then again disabled.
> > > >     Loop 200 times, with udisks failed between 1 to 70
> > > > iterations,
> > > > without
> > > >     udisks, no failures.
> > > 
> > > Wondering why udisks is causing failures - that seems like
> > > another
> > > bug.
> > 
> > With udisk2 enabled, 99% of the time the 'rootcontext=' test fails
> > (the
> > 1% is 'defcontext='). However if I run this test on its own, it
> > does
> > not fail. If I add the 'context=' test before and run, the
> > 'rootcontext=' will fail at some point.
> > 
> > If I add a short delay as shown in the 'context=' sequence, the
> > fault
> > does not occur:
> > -- Start --
> > system("losetup -d $dev 2>/dev/null");
> > system("sleep 0.01");
> > get_loop_dev();
> > attach_dev();
> 
> Can you try putting `udevadm settle` instead of the sleep there? I
> remember having some issues with udev race conditions a long time ago
> and I think that helped. (But I'm not sure at all if that's the right
> fix...)

Thanks - I had to add this to a couple of places and worked okay for
200 interations on Rawhide.

On the next patch version would you like the
udisks2_stop()/udisks2_restart() functions removed, left as is or set
as an option for the test when run locally ??

> 
> > # Mount again with no xttr support
> > $context2_opts =
> > "context=system_u:object_r:test_filesystem_context_t:s0";
> > -- End --
> > 
> > It could be udisk2 has a timing problem as the losetup(8) man page
> > '-d'
> > entry reads:
> > Note that since Linux v3.7 kernel uses "lazy  device destruction".
> > The
> > detach operation does not return EBUSY error anymore if device is
> > actively used by system, but it  is  marked by autoclear flag and
> > destroyed later.
> > 
> > But then again it could be something else !!!!
> > 
> >