Files
rockchip-kernel/include/uapi/linux
Rodrigo Campos 0ae71c7720 seccomp: Support atomic "addfd + send reply"
Alban Crequy reported a race condition userspace faces when we want to
add some fds and make the syscall return them[1] using seccomp notify.

The problem is that currently two different ioctl() calls are needed by
the process handling the syscalls (agent) for another userspace process
(target): SECCOMP_IOCTL_NOTIF_ADDFD to allocate the fd and
SECCOMP_IOCTL_NOTIF_SEND to return that value. Therefore, it is possible
for the agent to do the first ioctl to add a file descriptor but the
target is interrupted (EINTR) before the agent does the second ioctl()
call.

This patch adds a flag to the ADDFD ioctl() so it adds the fd and
returns that value atomically to the target program, as suggested by
Kees Cook[2]. This is done by simply allowing
seccomp_do_user_notification() to add the fd and return it in this case.
Therefore, in this case the target wakes up from the wait in
seccomp_do_user_notification() either to interrupt the syscall or to add
the fd and return it.

This "allocate an fd and return" functionality is useful for syscalls
that return a file descriptor only, like connect(2). Other syscalls that
return a file descriptor but not as return value (or return more than
one fd), like socketpair(), pipe(), recvmsg with SCM_RIGHTs, will not
work with this flag.

This effectively combines SECCOMP_IOCTL_NOTIF_ADDFD and
SECCOMP_IOCTL_NOTIF_SEND into an atomic opteration. The notification's
return value, nor error can be set by the user. Upon successful invocation
of the SECCOMP_IOCTL_NOTIF_ADDFD ioctl with the SECCOMP_ADDFD_FLAG_SEND
flag, the notifying process's errno will be 0, and the return value will
be the file descriptor number that was installed.

[1]: https://lore.kernel.org/lkml/CADZs7q4sw71iNHmV8EOOXhUKJMORPzF7thraxZYddTZsxta-KQ@mail.gmail.com/
[2]: https://lore.kernel.org/lkml/202012011322.26DCBC64F2@keescook/

Signed-off-by: Rodrigo Campos <rodrigo@kinvolk.io>
Signed-off-by: Sargun Dhillon <sargun@sargun.me>
Acked-by: Tycho Andersen <tycho@tycho.pizza>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20210517193908.3113-4-sargun@sargun.me
2021-06-28 12:49:52 -07:00
..
2021-01-25 18:44:44 +01:00
2020-05-28 11:22:14 +02:00
2020-12-28 14:21:31 +00:00
2019-12-18 18:07:31 +01:00
2021-02-09 10:58:19 +01:00
2020-08-12 10:58:00 -07:00
2020-10-09 12:47:02 -06:00
2021-04-19 15:27:36 -07:00
2021-03-04 17:58:15 -08:00
2020-12-08 15:54:16 +01:00
2019-10-09 22:31:14 -04:00
2019-09-25 17:51:39 -07:00
2020-04-27 16:29:41 +05:30
2020-07-19 19:20:22 -07:00
2020-06-24 21:34:11 +02:00
2020-05-14 16:44:25 +02:00
2021-02-08 13:01:24 +01:00
2020-07-13 15:32:56 -07:00
2020-04-20 12:43:24 -07:00
2020-07-24 17:12:41 -07:00
2020-08-27 07:55:59 -07:00
2021-02-26 09:41:03 -08:00
2021-03-23 23:40:55 -04:00
2020-11-26 14:19:10 +01:00
2020-05-21 08:20:35 -06:00
2020-03-29 22:30:57 -07:00
2021-03-10 09:34:06 +01:00
2020-08-18 15:44:44 +02:00
2021-01-24 14:43:45 +01:00
2021-04-02 14:21:50 -07:00
2019-10-02 20:32:27 -06:00
2020-03-29 22:30:57 -07:00
2021-01-16 23:19:26 +01:00
2021-03-10 09:34:06 +01:00
2019-12-18 18:07:31 +01:00
2021-01-07 16:17:32 +01:00
2019-12-09 09:59:07 +01:00
2020-03-29 23:29:08 +02:00
2020-10-23 11:55:28 -04:00
2020-10-23 11:55:28 -04:00
2020-07-13 15:32:56 -07:00
2020-06-24 07:51:01 +02:00