linux/samples
Christian Brauner 43c6afee48
samples: show race-free pidfd metadata access
This is a sample program showing userspace how to get race-free access
to process metadata from a pidfd.  It is rather easy to do and userspace
can actually simply reuse code that currently parses a process's status
file in procfs.
The program can easily be extended into a generic helper suitable for
inclusion in a libc to make it even easier for userspace to gain metadata
access.

Since this came up in a discussion because this API is going to be used
in various service managers: A lot of programs will have a whitelist
seccomp filter that returns <some-errno> for all new syscalls.  This
means that programs might get confused if CLONE_PIDFD works but the
later pidfd_send_signal() syscall doesn't.  Hence, here's a ahead of
time check that pidfd_send_signal() is supported:

bool pidfd_send_signal_supported()
{
        int procfd = open("/proc/self", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
        if (procfd < 0)
                return false;

        /*
         * A process is always allowed to signal itself so
         * pidfd_send_signal() should never fail this test. If it does
         * it must mean it is not available, blocked by an LSM, seccomp,
         * or other.
         */
        return pidfd_send_signal(procfd, 0, NULL, 0) == 0;
}

Signed-off-by: Christian Brauner <christian@brauner.io>
Co-developed-by: Jann Horn <jannh@google.com>
Signed-off-by: Jann Horn <jannh@google.com>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
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: Aleksa Sarai <cyphar@cyphar.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
2019-05-07 14:31:04 +02:00
..
auxdisplay auxdisplay: Replace licenses with SPDX identifiers 2018-04-12 19:02:45 +02:00
binderfs samples: add binderfs sample program 2019-01-14 17:22:22 -07:00
bpf bpf: hbm: fix spelling mistake "deault" -> "default" 2019-03-07 10:35:00 +01:00
configfs configfs: make config_item_type const 2017-10-19 16:15:15 +02:00
connector kbuild: announce removal of SUBDIRS if used 2018-12-01 22:21:56 +09:00
hidraw HID: samples/hidraw: fix typo in printed message 2018-11-21 00:31:58 +01:00
hw_breakpoint perf: Add context field to perf_event 2011-07-01 11:06:38 +02:00
kdb
kfifo kfifo: clean up example to not use page_link 2017-07-12 16:26:01 -07:00
kobject kobject: Remove redundant license text 2017-12-07 18:36:43 +01:00
kprobes arch: remove tile port 2018-03-16 10:56:03 +01:00
livepatch Merge branch 'for-5.0/upstream-fixes' into for-linus 2019-03-05 15:55:59 +01:00
mei samples: mei: use /dev/mei0 instead of /dev/mei 2019-01-30 15:24:45 +01:00
mic/mpssd samples/mic/mpssd/mpssd.h: remove duplicate header 2019-03-07 18:32:03 -08:00
pidfd samples: show race-free pidfd metadata access 2019-05-07 14:31:04 +02:00
pktgen Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2017-11-04 09:26:51 +09:00
qmi samples: Introduce Qualcomm QMI sample client 2018-02-12 16:57:22 -08:00
rpmsg rpmsg: Allow callback to return errors 2016-09-08 22:15:25 -07:00
seccomp samples/seccomp: Fix 32-bit build 2019-01-08 07:45:01 +01:00
statx kbuild: remove all dummy assignments to obj- 2017-11-18 11:46:06 +09:00
timers License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
trace_events Merge branch 'for-linus' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/jikos/trivial 2017-11-15 10:14:11 -08:00
trace_printk tracing: Add trace_printk sample code 2016-06-20 09:54:21 -04:00
uhid kbuild: remove all dummy assignments to obj- 2017-11-18 11:46:06 +09:00
v4l media: v4l2-pci-skeleton.c: fix outdated irq code 2019-01-25 18:47:06 -02:00
vfio-mdev samples/vfio-mdev/mtty: expand minor range when registering chrdev region 2019-02-12 13:21:18 -07:00
watchdog License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
Kconfig samples: add binderfs sample program 2019-01-14 17:22:22 -07:00
Makefile samples: show race-free pidfd metadata access 2019-05-07 14:31:04 +02:00