Commit graph

44 commits

Author SHA1 Message Date
Maciej Wieczor-Retman
4d7f4e8158 selftests/pidfd: Fix ksft print formats
Compiling pidfd selftest after adding a __printf() attribute to
ksft_print_msg() and ksft_test_result_pass() exposes -Wformat warnings
in error_report(), test_pidfd_poll_exec_thread(),
child_poll_exec_test(), test_pidfd_poll_leader_exit_thread(),
child_poll_leader_exit_test().

The ksft_test_result_pass() in error_report() expects a string but
doesn't provide any argument after the format string. All the other
calls to ksft_print_msg() in the functions mentioned above have format
strings that don't match with other passed arguments.

Fix format specifiers so they match the passed variables.

Add a missing variable to ksft_test_result_pass() inside
error_report() so it matches other cases in the switch statement.

Fixes: 2def297ec7 ("pidfd: add tests for NSpid info in fdinfo")

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2023-10-13 14:08:21 -06:00
Ziqi Zhao
bcda4c863e selftest: pidfd: Omit long and repeating outputs
An output message:

> # # waitpid WEXITSTATUS=0

will be printed for 30,000+ times in the `pidfd_test` selftest, which
does not seem ideal. This patch removes the print logic in the
`wait_for_pid` function, so each call to this function does not output
a line by default. Any existing call sites where the extra line might
be beneficial have been modified to include extra print statements
outside of the function calls.

Signed-off-by: Ziqi Zhao <astrajoan@yahoo.com>
Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2023-06-12 16:39:11 -06:00
Mathieu Desnoyers
3f7d717687 selftests: pidfd: Fix incorrect kernel headers search path
Use $(KHDR_INCLUDES) as lookup path for kernel headers. This prevents
building against kernel headers from the build environment in scenarios
where kernel headers are installed into a specific output directory
(O=...).

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: linux-kselftest@vger.kernel.org
Cc: Ingo Molnar <mingo@redhat.com>
Cc: <stable@vger.kernel.org> # 5.18+
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2023-02-13 09:09:45 -07:00
Zhao Gongyi
89c1017aac selftests/pidfd_test: Remove the erroneous ','
Remove the erroneous ',', otherwise it might result in wrong output
and report:
 ...
 Bail out! (errno %d)
  test: Unexpected epoll_wait result (c=4208480, events=2)
 ...

Fixes: 740378dc78 ("pidfd: add polling selftests")
Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-11-02 03:09:57 -06:00
Li Zhijian
3d98244130 selftests: pidfd: Fix compling warnings
Fix warnings and enable Wall.

pidfd_wait.c: In function ‘wait_nonblock’:
pidfd_wait.c:150:13: warning: unused variable ‘status’ [-Wunused-variable]
  150 |  int pidfd, status = 0;
      |             ^~~~~~
...
pidfd_test.c: In function ‘child_poll_exec_test’:
pidfd_test.c:438:1: warning: no return statement in function returning non-void [-Wreturn-type]
  438 | }
      | ^

Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
v2: fix mistake assignment to pidfd
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-10-30 02:23:58 -06:00
Li Zhijian
88e1f16ba5 ksefltests: pidfd: Fix wait_states: Test terminated by timeout
0Day/LKP observed that the kselftest blocks forever since one of the
pidfd_wait doesn't terminate in 1 of 30 runs. After digging into
the source, we found that it blocks at:
ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WCONTINUED, NULL), 0);

wait_states has below testing flow:
  CHILD                 PARENT
  ---------------+--------------
1 STOP itself
2                   WAIT for CHILD STOPPED
3                   SIGNAL CHILD to CONT
4 CONT
5 STOP itself
5'                  WAIT for CHILD CONT
6                   WAIT for CHILD STOPPED

The problem is that the kernel cannot ensure the order of 5 and 5', once
5 goes first, the test will fail.

we can reproduce it by:
$ while true; do make run_tests -C pidfd; done

Introduce a blocking read in child process to make sure the parent can
check its WCONTINUED.

CC: Philip Li <philip.li@intel.com>
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
Reviewed-by: Christian Brauner (Microsoft) <brauner@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-10-30 02:23:41 -06:00
Axel Rasmussen
187816d077 selftests: fix an unused variable warning in pidfd selftest
I fixed a few warnings like this in commit e2aa5e650b
("selftests: fixup build warnings in pidfd / clone3 tests"), but I
missed this one by mistake. Since this variable is unused, remove it.

Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-04-04 13:32:53 -06:00
Axel Rasmussen
e2aa5e650b selftests: fixup build warnings in pidfd / clone3 tests
These are some trivial fixups, which were needed to build the tests with
clang and -Werror. The following issues are fixed:

- Remove various unused variables.
- In child_poll_leader_exit_test, clang isn't smart enough to realize
  syscall(SYS_exit, 0) won't return, so it complains we never return
  from a non-void function. Add an extra exit(0) to appease it.
- In test_pidfd_poll_leader_exit, ret may be branched on despite being
  uninitialized, if we have !use_waitpid. Initialize it to zero to get
  the right behavior in that case.

Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Acked-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-02-04 13:07:07 -07:00
Axel Rasmussen
4cbd93c3c1 pidfd: fix test failure due to stack overflow on some arches
When running the pidfd_fdinfo_test on arm64, it fails for me. After some
digging, the reason is that the child exits due to SIGBUS, because it
overflows the 1024 byte stack we've reserved for it.

To fix the issue, increase the stack size to 8192 bytes (this number is
somewhat arbitrary, and was arrived at through experimentation -- I kept
doubling until the failure no longer occurred).

Also, let's make the issue easier to debug. wait_for_pid() returns an
ambiguous value: it may return -1 in all of these cases:

1. waitpid() itself returned -1
2. waitpid() returned success, but we found !WIFEXITED(status).
3. The child process exited, but it did so with a -1 exit code.

There's no way for the caller to tell the difference. So, at least log
which occurred, so the test runner can debug things.

While debugging this, I found that we had !WIFEXITED(), because the
child exited due to a signal. This seems like a reasonably common case,
so also print out whether or not we have WIFSIGNALED(), and the
associated WTERMSIG() (if any). This lets us see the SIGBUS I'm fixing
clearly when it occurs.

Finally, I'm suspicious of allocating the child's stack on our stack.
man clone(2) suggests that the correct way to do this is with mmap(),
and in particular by setting MAP_STACK. So, switch to doing it that way
instead.

Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Acked-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-02-04 13:06:39 -07:00
Tommi Rantala
7b9621d459 selftests: pidfd: drop needless linux/kcmp.h inclusion in pidfd_setns_test.c
kcmp is not used in pidfd_setns_test.c, so do not include <linux/kcmp.h>

Signed-off-by: Tommi Rantala <tommi.t.rantala@nokia.com>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-10-27 17:04:01 -06:00
Tommi Rantala
90da74af34 selftests: pidfd: add CONFIG_CHECKPOINT_RESTORE=y to config
kcmp syscall is used in pidfd_getfd_test.c, so add
CONFIG_CHECKPOINT_RESTORE=y to config to ensure kcmp is available.

Signed-off-by: Tommi Rantala <tommi.t.rantala@nokia.com>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-10-27 17:02:03 -06:00
Tommi Rantala
b5ec9fe5be selftests: pidfd: skip test on kcmp() ENOSYS
Skip test if kcmp() is not available, for example if kernel is compiled
without CONFIG_CHECKPOINT_RESTORE=y.

Signed-off-by: Tommi Rantala <tommi.t.rantala@nokia.com>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-10-27 17:01:45 -06:00
Tommi Rantala
0b18fed98b selftests: pidfd: use ksft_test_result_skip() when skipping test
There's planned tests != run tests in pidfd_test when some test is
skipped:

  $ ./pidfd_test
  TAP version 13
  1..8
  [...]
  # pidfd_send_signal signal recycled pid test: Skipping test
  # Planned tests != run tests (8 != 7)
  # Totals: pass:7 fail:0 xfail:0 xpass:0 skip:0 error:0

Fix by using ksft_test_result_skip():

  $ ./pidfd_test
  TAP version 13
  1..8
  [...]
  ok 8 # SKIP pidfd_send_signal signal recycled pid test: Unsharing pid namespace not permitted
  # Totals: pass:7 fail:0 xfail:0 xpass:0 skip:1 error:0

Signed-off-by: Tommi Rantala <tommi.t.rantala@nokia.com>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-10-27 17:01:24 -06:00
Tommi Rantala
1948172fdb selftests: pidfd: fix compilation errors due to wait.h
Drop unneeded <linux/wait.h> header inclusion to fix pidfd compilation
errors seen in Fedora 32:

In file included from pidfd_open_test.c:9:
../../../../usr/include/linux/wait.h:17:16: error: expected identifier before numeric constant
   17 | #define P_ALL  0
      |                ^

Signed-off-by: Tommi Rantala <tommi.t.rantala@nokia.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-10-27 16:51:36 -06:00
Linus Torvalds
4da9af0014 threads-v5.10
-----BEGIN PGP SIGNATURE-----
 
 iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCX4a4sAAKCRCRxhvAZXjc
 ohdRAP9fclgrRkTl3o4cgaK0PUMt8BZ5QCg/SPQrVT58AQlfSwEAsNtWAeo6U2z1
 FLGuCoPBEW1Zghkj1lMbIhj5zyVaEQ8=
 =Z7Q0
 -----END PGP SIGNATURE-----

Merge tag 'threads-v5.10' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux

Pull pidfd updates from Christian Brauner:
 "This introduces a new extension to the pidfd_open() syscall. Users can
  now raise the new PIDFD_NONBLOCK flag to support non-blocking pidfd
  file descriptors. This has been requested for uses in async process
  management libraries such as async-pidfd in Rust.

  Ever since the introduction of pidfds and more advanced async io
  various programming languages such as Rust have grown support for
  async event libraries. These libraries are created to help build
  epoll-based event loops around file descriptors. A common pattern is
  to automatically make all file descriptors they manage to O_NONBLOCK.

  For such libraries the EAGAIN error code is treated specially. When a
  function is called that returns EAGAIN the function isn't called again
  until the event loop indicates the the file descriptor is ready.
  Supporting EAGAIN when waiting on pidfds makes such libraries just
  work with little effort.

  This introduces a new flag PIDFD_NONBLOCK that is equivalent to
  O_NONBLOCK. This follows the same patterns we have for other (anon
  inode) file descriptors such as EFD_NONBLOCK, IN_NONBLOCK,
  SFD_NONBLOCK, TFD_NONBLOCK and the same for close-on-exec flags.

  Passing a non-blocking pidfd to waitid() currently has no effect, i.e.
  is not supported. There are users which would like to use waitid() on
  pidfds that are O_NONBLOCK and mix it with pidfds that are blocking
  and both pass them to waitid().

  The expected behavior is to have waitid() return -EAGAIN for
  non-blocking pidfds and to block for blocking pidfds without needing
  to perform any additional checks for flags set on the pidfd before
  passing it to waitid(). Non-blocking pidfds will return EAGAIN from
  waitid() when no child process is ready yet. Returning -EAGAIN for
  non-blocking pidfds makes it easier for event loops that handle EAGAIN
  specially.

  It also makes the API more consistent and uniform. In essence,
  waitid() is treated like a read on a non-blocking pidfd or a recvmsg()
  on a non-blocking socket.

  With the addition of support for non-blocking pidfds we support the
  same functionality that sockets do. For sockets() recvmsg() supports
  MSG_DONTWAIT for pidfds waitid() supports WNOHANG. Both flags are
  per-call options. In contrast non-blocking pidfds and non-blocking
  sockets are a setting on an open file description affecting all
  threads in the calling process as well as other processes that hold
  file descriptors referring to the same open file description. Both
  behaviors, per call and per open file description, have genuine
  use-cases.

  The interaction with the WNOHANG flag is documented as follows:

   - If a non-blocking pidfd is passed and WNOHANG is not raised we
     simply raise the WNOHANG flag internally. When do_wait() returns
     indicating that there are eligible child processes but none have
     exited yet we set EAGAIN. If no child process exists we continue
     returning ECHILD.

   - If a non-blocking pidfd is passed and WNOHANG is raised waitid()
     will continue returning 0, i.e. it will not set EAGAIN. This ensure
     backwards compatibility with applications passing WNOHANG
     explicitly with pidfds"

* tag 'threads-v5.10' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux:
  tests: remove O_NONBLOCK before waiting for WSTOPPED
  tests: add waitid() tests for non-blocking pidfds
  tests: port pidfd_wait to kselftest harness
  pidfd: support PIDFD_NONBLOCK in pidfd_open()
  exit: support non-blocking pidfds
2020-10-14 14:39:20 -07:00
Christian Brauner
01361b665a
tests: remove O_NONBLOCK before waiting for WSTOPPED
Naresh reported that selftests: pidfd: pidfd_wait hangs on linux next kernel on
x86_64, i386 and arm64 Juno-r2
These devices are using NFS mounted rootfs.
I have tested pidfd testcases independently and all test PASS.

The Hang or exit from test run noticed when run by run_kselftest.sh

pidfd_wait.c:208:wait_nonblock:Expected sys_waitid(P_PIDFD, pidfd,
&info, WSTOPPED, NULL) (-1) == 0 (0)
wait_nonblock: Test terminated by assertion

metadata:
  git branch: master
  git repo: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
  git commit: e64997027d5f171148687e58b78c8b3c869a6158
  git describe: next-20200922
  make_kernelversion: 5.9.0-rc6
  kernel-config:
http://snapshots.linaro.org/openembedded/lkft/lkft/sumo/intel-core2-32/lkft/linux-next/865/config

The reason for this is a simple race in the selftests, that I overlooked and
which is more likely to hit when there's a lot of processes running on the
system. Basically the child process hasn't SIGSTOPed itself yet but the parent
is already calling waitid() on a O_NONBLOCK pidfd. Since it doesn't find a
WSTOPPED process it returns -EAGAIN correctly.

The fix for this is to move the line where we're removing the O_NONBLOCK
property from the fd before the waitid() WSTOPPED call so we hang until the
child becomes stopped.

Fixes: cd89597bbe ("tests: add waitid() tests for non-blocking pidfds")
Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org>
Tested-by: Naresh Kamboju <naresh.kamboju@linaro.org>
Link: https://lkft.validation.linaro.org/scheduler/job/1813223
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2020-10-09 11:56:51 +02:00
Kees Cook
e953aeaa91 selftests/clone3: Avoid OS-defined clone_args
As the UAPI headers start to appear in distros, we need to avoid
outdated versions of struct clone_args to be able to test modern
features, named "struct __clone_args". Additionally update the struct
size macro names to match UAPI names.

Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/lkml/20200921075432.u4gis3s2o5qrsb5g@wittgenstein/
Signed-off-by: Kees Cook <keescook@chromium.org>
2020-10-08 13:17:25 -07:00
Christian Brauner
cd89597bbe
tests: add waitid() tests for non-blocking pidfds
Verify that the PIDFD_NONBLOCK flag works with pidfd_open() and that
waitid() with a non-blocking pidfd returns EAGAIN:

	TAP version 13
	1..3
	# Starting 3 tests from 1 test cases.
	#  RUN           global.wait_simple ...
	#            OK  global.wait_simple
	ok 1 global.wait_simple
	#  RUN           global.wait_states ...
	#            OK  global.wait_states
	ok 2 global.wait_states
	#  RUN           global.wait_nonblock ...
	#            OK  global.wait_nonblock
	ok 3 global.wait_nonblock
	# PASSED: 3 / 3 tests passed.
	# Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: linux-kselftest@vger.kernel.org
Link: https://lore.kernel.org/r/20200902102130.147672-5-christian.brauner@ubuntu.com
2020-09-04 13:48:57 +02:00
Christian Brauner
09d1de1a8e
tests: port pidfd_wait to kselftest harness
All of the new pidfd selftests already use the new kselftest harness
infrastructure. It makes for clearer output, makes the code easier to
understand, and makes adding new tests way simpler.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: linux-kselftest@vger.kernel.org
Link: https://lore.kernel.org/r/20200902102130.147672-4-christian.brauner@ubuntu.com
2020-09-04 12:34:50 +02:00
Linus Torvalds
4834ce9d8e linux-kselftest-5.9-rc1
This Kselftest update for Linux 5.9-rc1 consists of
 
 - TAP output reporting related fixes from Paolo Bonzini and Kees Cook.
   These fixes make it skip reporting consistent with TAP format.
 - Cleanup fixes to framework run_tests from Yauheni Kaliuta
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEPZKym/RZuOCGeA/kCwJExA0NQxwFAl8p1akACgkQCwJExA0N
 QxwqKg/9HzbnpWeb736HAjeA3v0LFuPte8TELSerjKqfas+g9xQxhf+ReHaZXz9i
 KnhBPYyOb57DjnT7Mi7c5qGYzLKCvBF30OR0M1P5lRWBX3SC0VvkgdHzLpksIsHx
 1wIht3gClvdOnHeHWWRG374iCbw86po8Xa5V9KOJOofbEKjctYjiShnd3OXXNLdJ
 h1/Ro+LffdsqO7VWoJruSuBplCXAVIr8IpUnOhtw/JTGlK7csNHBdHb7KTBm+zKU
 i0+f6H5uxRM3BA793OHen9D6kHAVLzhtPc7O0O1IhNRKnDgzY/UIS0qSGpxzD6KG
 +ZZ4FpvyfN2EPqgGegjdTNhQjPrjXpPos46FTNOM/qiQNYvCuvUFjRCAzVuN9cqU
 QzXdNPUOI7YLpOYpqLNqeefTXhZUxCWPF33oHPhU28W59qYpqQDRRDoOu7l9e7KQ
 DMwIKCaSw5qCB7S6x5LqOiQcc4/3xIbJVjO/CQU7G4cuREkAHqUvc2rfwCHcn60e
 rt2h/v2rnHzie4kTEukWKPuyHL64CL0jYTdHtFb/PbavnfbEPe3t6GiGu2DJN680
 mem6+9Q12KTtY4VY6enBE4YHVlxQPksbp/o5G91evNuFul1ZsAFgWdR5t+iiMrCU
 d6u2m5H6dMShZYqTw98MU/PKAh31sQCNZqhruhZMgUOJEV7lP/s=
 =4uja
 -----END PGP SIGNATURE-----

Merge tag 'linux-kselftest-5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull kselftest updates form Shuah Khan:

 - TAP output reporting related fixes from Paolo Bonzini and Kees Cook.

   These fixes make it skip reporting consistent with TAP format.

 - Cleanup fixes to framework run_tests from Yauheni Kaliuta

* tag 'linux-kselftest-5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: (23 commits)
  selftests/harness: Limit step counter reporting
  selftests/seccomp: Check ENOSYS under tracing
  selftests/seccomp: Refactor to use fixture variants
  selftests/harness: Clean up kern-doc for fixtures
  selftests: kmod: Add module address visibility test
  Replace HTTP links with HTTPS ones: KMOD KERNEL MODULE LOADER - USERMODE HELPER
  selftests: fix condition in run_tests
  selftests: do not use .ONESHELL
  selftests: pidfd: skip test if unshare fails with EPERM
  selftests: pidfd: do not use ksft_exit_skip after ksft_set_plan
  selftests/harness: Report skip reason
  selftests/harness: Display signed values correctly
  selftests/harness: Refactor XFAIL into SKIP
  selftests/harness: Switch to TAP output
  selftests: Add header documentation and helpers
  selftests/binderfs: Fix harness API usage
  selftests: Remove unneeded selftest API headers
  selftests/clone3: Reorder reporting output
  selftests: sync_test: do not use ksft_exit_skip after ksft_set_plan
  selftests: sigaltstack: do not use ksft_exit_skip after ksft_set_plan
  ...
2020-08-05 10:28:25 -07:00
Christian Brauner
55d9ad97e4
tests: add CLONE_NEWTIME setns tests
Now that pidfds support CLONE_NEWTIME as well enable testing them in the
setns() testuite.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Dmitry Safonov <dima@arista.com>
Cc: Andrei Vagin <avagin@gmail.com>
Link: https://lore.kernel.org/r/20200706154912.3248030-5-christian.brauner@ubuntu.com
2020-07-08 11:14:22 +02:00
Paolo Bonzini
05790fd7f8 selftests: pidfd: skip test if unshare fails with EPERM
Similar to how ENOSYS causes a skip if pidfd_send_signal is not present,
we can do the same for unshare if it fails with EPERM.  This way, running
the test without privileges causes four tests to skip but no early bail out.

Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-07-07 13:28:58 -06:00
Paolo Bonzini
bb91c0ca7b selftests: pidfd: do not use ksft_exit_skip after ksft_set_plan
Calling ksft_exit_skip after ksft_set_plan results in executing fewer tests
than planned.  Use ksft_test_result_skip instead.

The plan passed to ksft_set_plan was wrong, too, so fix it while at it.

Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-07-07 13:28:44 -06:00
Kees Cook
ce79097a8f selftests: Remove unneeded selftest API headers
Remove unused includes of the kselftest.h header.

Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-07-06 15:59:07 -06:00
Christian Brauner
86f56395fe
tests: test for setns() EINVAL regression
Verify that setns() reports EINVAL when an fd is passed that refers to an
open file but the file is not a file descriptor useable to interact with
namespaces.

Cc: Jan Stancek <jstancek@redhat.com>
Cc: Cyril Hrubis <chrubis@suse.cz>
Link: https://lore.kernel.org/lkml/20200615085836.GR12456@shao2-debian
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2020-06-17 00:48:54 +02:00
Christian Brauner
2b40c5db73
selftests/pidfd: add pidfd setns tests
This is basically a test-suite for setns() and as of now contains:
- test that we can't pass garbage flags
- test that we can't attach to the namespaces of  task that has already exited
- test that we can incrementally setns into all namespaces of a target task
  using a pidfd
- test that we can setns atomically into all namespaces of a target task
- test that we can't cross setns into a user namespace outside of our user
  namespace hierarchy
- test that we can't setns into namespaces owned by user namespaces over which
  we are not privileged

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20200505140432.181565-4-christian.brauner@ubuntu.com
2020-05-13 11:41:22 +02:00
Linus Torvalds
1e396a5d17 threads-v5.7
-----BEGIN PGP SIGNATURE-----
 
 iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCXoSOqgAKCRCRxhvAZXjc
 omfTAQDlbSZkzNfEWcxi+WE9tg+Y5tHR7hjdsK99Mb3vhAh5GwD+LT0EOI3WKoNY
 MQUp2RaR7YwWL/pktd9N1EX8qa5IPg4=
 =zvBX
 -----END PGP SIGNATURE-----

Merge tag 'threads-v5.7' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux

Pull thread updates from Christian Brauner:
 "The main change for this cycle was the extension for clone3() to
  support spawning processes directly into cgroups via CLONE_INTO_CGROUP
  (commit ef2c41cf38: "clone3: allow spawning processes
  into cgroups").

  But since I had to touch kernel/cgroup/ quite a bit I had Tejun route
  that through his tree this time around to make it easier for him to
  handle other changes.

  So here is just the unexciting leftovers: a regression test for the
  ENOMEM regression we fixed in commit b26ebfe12f ("pid: Fix error
  return value in some cases") verifying that we report ENOMEM when
  trying to create a new process in a pid namespace whose init
  process/subreaper has already exited"

* tag 'threads-v5.7' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux:
  selftests: add pid namespace ENOMEM regression test
2020-04-04 10:08:18 -07:00
Christian Brauner
6952a4f646
selftests: add pid namespace ENOMEM regression test
We recently regressed (cf. [1] and its corresponding fix in [2]) returning
ENOMEM when trying to create a process in a pid namespace whose init
process/child subreaper has already died. This has caused confusion at
least once before that (cf. [3]). Let's add a simple regression test to
catch this in the future.

[1]: 49cb2fc42c ("fork: extend clone3() to support setting a PID")
[2]: b26ebfe12f ("pid: Fix error return value in some cases")
[3]: 35f71bc0a0 ("fork: report pid reservation failure properly")
Cc: Corey Minyard <cminyard@mvista.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Adrian Reber <areber@redhat.com>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: Andrei Vagin <avagin@gmail.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2020-03-25 13:50:34 +01:00
Masahiro Yamada
d198b34f38 .gitignore: add SPDX License Identifier
Add SPDX License Identifier to all .gitignore files.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-03-25 11:50:48 +01:00
Christophe Leroy
186e28a18a
selftests: pidfd: Add pidfd_fdinfo_test in .gitignore
The commit identified below added pidfd_fdinfo_test
but failed to add it to .gitignore

Fixes: 2def297ec7 ("pidfd: add tests for NSpid info in fdinfo")
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/966567c7dbaa26a06730d796354f8a086c0ee288.1582847778.git.christophe.leroy@c-s.fr
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2020-02-28 13:35:05 +01:00
Sargun Dhillon
873dfd7881
test: Add test for pidfd getfd
The following tests:
  * Fetch FD, and then compare via kcmp
  * Make sure getfd can be blocked by blocking ptrace_may_access
  * Making sure fetching bad FDs fails
  * Make sure trying to set flags to non-zero results in an EINVAL

Signed-off-by: Sargun Dhillon <sargun@sargun.me>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20200107175927.4558-5-sargun@sargun.me
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2020-01-13 21:49:54 +01:00
Christian Brauner
67fc700016
test: verify fdinfo for pidfd of reaped process
Test that the fdinfo field of a pidfd referring to a dead process
correctly shows Pid: -1 and NSpid: -1.

Cc: Christian Kellner <christian@kellner.me>
Cc: linux-kselftest@vger.kernel.org
Reviewed-by: Christian Kellner <christian@kellner.me>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20191017101832.5985-2-christian.brauner@ubuntu.com
2019-10-17 15:36:54 +02:00
Christian Kellner
2def297ec7 pidfd: add tests for NSpid info in fdinfo
Add a test that checks that if pid namespaces are configured the fdinfo
file of a pidfd contains an NSpid: entry containing the process id in
the current and additionally all nested namespaces. In the case that
a pidfd is from a pid namespace not in the same namespace hierarchy as
the process accessing the fdinfo file, ensure the 'NSpid' shows 0 for
that pidfd, analogous to the 'Pid' entry.

Signed-off-by: Christian Kellner <christian@kellner.me>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20191014162034.2185-2-ckellner@redhat.com
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2019-10-15 12:17:11 +02:00
Shuah Khan
3969e76909
selftests: pidfd: Fix undefined reference to pthread_create()
Fix build failure:

undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

Fix CFLAGS to include pthread correctly.

Fixes: 740378dc78 ("pidfd: add polling selftests")
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Reviewed-by: Christian Brauner <christian.brauner@ubuntu.com>
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20190924195237.30519-1-skhan@linuxfoundation.org
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2019-09-30 22:32:55 +02:00
Linus Torvalds
c17112a5c4 core-process-v5.4
-----BEGIN PGP SIGNATURE-----
 
 iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCXXe8mQAKCRCRxhvAZXjc
 ou7oAQCszihkNfpjORSSSOqenMDrxxDW++A7TIOLuq7UyZQl8QD+LM1wvT/xypfJ
 ORD9XX8+Wrv07AQn85fZBEFXGrnengk=
 =o+VL
 -----END PGP SIGNATURE-----

Merge tag 'core-process-v5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux

Pull pidfd/waitid updates from Christian Brauner:
 "This contains two features and various tests.

  First, it adds support for waiting on process through pidfds by adding
  the P_PIDFD type to the waitid() syscall. This completes the basic
  functionality of the pidfd api (cf. [1]). In the meantime we also have
  a new adition to the userspace projects that make use of the pidfd
  api. The qt project was nice enough to send a mail pointing out that
  they have a pr up to switch to the pidfd api (cf. [2]).

  Second, this tag contains an extension to the waitid() syscall to make
  it possible to wait on the current process group in a race free manner
  (even though the actual problem is very unlikely) by specifing 0
  together with the P_PGID type. This extension traces back to a
  discussion on the glibc development mailing list.

  There are also a range of tests for the features above. Additionally,
  the test-suite which detected the pidfd-polling race we fixed in [3]
  is included in this tag"

[1] https://lwn.net/Articles/794707/
[2] https://codereview.qt-project.org/c/qt/qtbase/+/108456
[3] commit b191d6491b ("pidfd: fix a poll race when setting exit_state")

* tag 'core-process-v5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux:
  waitid: Add support for waiting for the current process group
  tests: add pidfd poll tests
  tests: move common definitions and functions into pidfd.h
  pidfd: add pidfd_wait tests
  pidfd: add P_PIDFD to waitid()
2019-09-16 09:28:19 -07:00
Suren Baghdasaryan
aed5a8df3d tests: add pidfd poll tests
This adds testing for polling on pidfd of a process being killed. Test runs
10000 iterations by default to stress test pidfd polling functionality.
It accepts an optional command-line parameter to override the number or
iterations to run.
Specifically, it tests for:
- pidfd_open on a child process succeeds
- pidfd_send_signal on a child process succeeds
- polling on pidfd succeeds and returns exactly one event
- returned event is POLLIN
- event is received within 3 secs of the process being killed

10000 iterations was chosen because of the race condition being tested
which is not consistently reproducible but usually is revealed after less
than 2000 iterations.
Reveals race fixed by commit b191d6491b ("pidfd: fix a poll race when setting exit_state")

Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Link: https://lore.kernel.org/r/20190726162226.252750-2-surenb@google.com
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2019-08-06 19:52:41 +02:00
Suren Baghdasaryan
2ec2f99abd
tests: move common definitions and functions into pidfd.h
Move definitions and functions used across different pidfd tests into
pidfd.h header.

Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Christian Brauner <christian@brauner.io>
Link: https://lore.kernel.org/r/20190726162226.252750-1-surenb@google.com
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2019-08-06 19:39:45 +02:00
Christian Brauner
e63f308570
pidfd: add pidfd_wait tests
Add tests for pidfd_wait() and CLONE_WAIT_PID:
- test that waitid(P_PIDFD) fails on /proc/<pid>
- test that waitid(P_PIDFD) fails on /dev/null
- test that waitid(P_PIDFD) can wait on a pidfd
- test that waitid(P_PIDFD) can wait on a pidfd and return siginfo_t
- test that waitid(P_PIDFD) works with WEXITED
- test that waitid(P_PIDFD) works with WSTOPPED
- test that waitid(P_PIDFD) works with WUNTRACED
- test that waitid(P_PIDFD) works with WCONTINUED
- test that waitid(P_PIDFD) works with WNOWAIT
- test that waitid(P_PIDFD)works with WNOHANG

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: David Howells <dhowells@redhat.com>
Cc: Jann Horn <jannh@google.com>
Cc: Andy Lutomirsky <luto@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Link: https://lore.kernel.org/r/20190727222229.6516-3-christian@brauner.io
2019-08-06 19:39:30 +02:00
Christian Brauner
3884ae44f4
pidfd: remove obsolete comments from test
Since the introduction of CLONE_PIDFD pidfd_send_signal() is independent
of CONFIG_PROC_FS.

Signed-off-by: Christian Brauner <christian@brauner.io>
2019-07-29 17:17:27 +02:00
Christian Brauner
172bb24a4f
tests: add pidfd_open() tests
This adds testing for the new pidfd_open() syscalls. Specifically, we test:
- that no invalid flags can be passed to pidfd_open()
- that no invalid pid can be passed to pidfd_open()
- that a pidfd can be retrieved with pidfd_open()
- that the retrieved pidfd references the correct pid

Signed-off-by: Christian Brauner <christian@brauner.io>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Jann Horn <jannh@google.com>
Cc: David Howells <dhowells@redhat.com>
Cc: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
Cc: Andy Lutomirsky <luto@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: linux-api@vger.kernel.org
2019-06-28 12:17:55 +02:00
Joel Fernandes (Google)
740378dc78
pidfd: add polling selftests
Other than verifying pidfd based polling, the tests make sure that
wait semantics are preserved with the pidfd poll. Notably the 2 cases:
1. If a thread group leader exits while threads still there, then no
   pidfd poll notifcation should happen.
2. If a non-thread group leader does an execve, then the thread group
   leader is signaled to exit and is replaced with the execing thread
   as the new leader, however the parent is not notified in this case.

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Signed-off-by: Christian Brauner <christian@brauner.io>
2019-06-28 12:17:55 +02:00
Kelsey Skunberg
4d0b5f4d75 selftests: pidfd: Create .gitignore to include pidfd_test
Create ../selftests/pidfd/.gitignore which holds the following file name
created after compiling:

	- pidfd_test

Signed-off-by: Kelsey Skunberg <skunberg.kelsey@gmail.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-05-14 17:36:49 -06:00
Kees Cook
5821ba9695 selftests: Add test plan API to kselftest.h and adjust callers
The test plan for TAP needs to be declared immediately after the header.
This adds the test plan API to kselftest.h and updates all callers to
declare their expected test counts.

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-04-25 13:15:46 -06:00
Christian Brauner
575a0ae974
selftests: add tests for pidfd_send_signal()
As suggested by Andrew Morton in [1] add selftests for the new
sys_pidfd_send_signal() syscall:

/* test_pidfd_send_signal_syscall_support */
Test whether the pidfd_send_signal() syscall is supported and the tests can
be run or need to be skipped.

/* test_pidfd_send_signal_simple_success */
Test whether sending a signal via a pidfd works.

/* test_pidfd_send_signal_exited_fail */
Verify that sending a signal to an already exited process fails with ESRCH.

/* test_pidfd_send_signal_recycled_pid_fail */
Verify that a recycled pid cannot be signaled via a pidfd referring to an
already exited process that had the same pid (cf. [2], [3]).

[1]: https://lore.kernel.org/lkml/20181228152012.dbf0508c2508138efc5f2bbe@linux-foundation.org/
[2]: https://lore.kernel.org/lkml/20181230210245.GA30252@mail.hallyn.com/
[3]: https://lore.kernel.org/lkml/20181230232711.7aayb7vnhogbv4co@brauner.io/

Cc: Arnd Bergmann <arnd@arndb.de>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Jann Horn <jannh@google.com>
Cc: Andy Lutomirsky <luto@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Christian Brauner <christian@brauner.io>
Reviewed-by: Tycho Andersen <tycho@tycho.ws>
Acked-by: Serge Hallyn <serge@hallyn.com>
2019-03-05 17:04:33 +01:00