Skip to content

Commit 0ae71c7

Browse files
ratakees
authored andcommitted
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 <[email protected]> Signed-off-by: Sargun Dhillon <[email protected]> Acked-by: Tycho Andersen <[email protected]> Acked-by: Christian Brauner <[email protected]> Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ddc4739 commit 0ae71c7

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

Documentation/userspace-api/seccomp_filter.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ and ``ioctl(SECCOMP_IOCTL_NOTIF_SEND)`` a response, indicating what should be
259259
returned to userspace. The ``id`` member of ``struct seccomp_notif_resp`` should
260260
be the same ``id`` as in ``struct seccomp_notif``.
261261

262+
Userspace can also add file descriptors to the notifying process via
263+
``ioctl(SECCOMP_IOCTL_NOTIF_ADDFD)``. The ``id`` member of
264+
``struct seccomp_notif_addfd`` should be the same ``id`` as in
265+
``struct seccomp_notif``. The ``newfd_flags`` flag may be used to set flags
266+
like O_EXEC on the file descriptor in the notifying process. If the supervisor
267+
wants to inject the file descriptor with a specific number, the
268+
``SECCOMP_ADDFD_FLAG_SETFD`` flag can be used, and set the ``newfd`` member to
269+
the specific number to use. If that file descriptor is already open in the
270+
notifying process it will be replaced. The supervisor can also add an FD, and
271+
respond atomically by using the ``SECCOMP_ADDFD_FLAG_SEND`` flag and the return
272+
value will be the injected file descriptor number.
273+
262274
It is worth noting that ``struct seccomp_data`` contains the values of register
263275
arguments to the syscall, but does not contain pointers to memory. The task's
264276
memory is accessible to suitably privileged traces via ``ptrace()`` or

include/uapi/linux/seccomp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct seccomp_notif_resp {
115115

116116
/* valid flags for seccomp_notif_addfd */
117117
#define SECCOMP_ADDFD_FLAG_SETFD (1UL << 0) /* Specify remote fd */
118+
#define SECCOMP_ADDFD_FLAG_SEND (1UL << 1) /* Addfd and return it, atomically */
118119

119120
/**
120121
* struct seccomp_notif_addfd

kernel/seccomp.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct seccomp_knotif {
107107
* installing process should allocate the fd as normal.
108108
* @flags: The flags for the new file descriptor. At the moment, only O_CLOEXEC
109109
* is allowed.
110+
* @ioctl_flags: The flags used for the seccomp_addfd ioctl.
110111
* @ret: The return value of the installing process. It is set to the fd num
111112
* upon success (>= 0).
112113
* @completion: Indicates that the installing process has completed fd
@@ -118,6 +119,7 @@ struct seccomp_kaddfd {
118119
struct file *file;
119120
int fd;
120121
unsigned int flags;
122+
__u32 ioctl_flags;
121123

122124
union {
123125
bool setfd;
@@ -1065,18 +1067,37 @@ static u64 seccomp_next_notify_id(struct seccomp_filter *filter)
10651067
return filter->notif->next_id++;
10661068
}
10671069

1068-
static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd)
1070+
static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd, struct seccomp_knotif *n)
10691071
{
1072+
int fd;
1073+
10701074
/*
10711075
* Remove the notification, and reset the list pointers, indicating
10721076
* that it has been handled.
10731077
*/
10741078
list_del_init(&addfd->list);
10751079
if (!addfd->setfd)
1076-
addfd->ret = receive_fd(addfd->file, addfd->flags);
1080+
fd = receive_fd(addfd->file, addfd->flags);
10771081
else
1078-
addfd->ret = receive_fd_replace(addfd->fd, addfd->file,
1079-
addfd->flags);
1082+
fd = receive_fd_replace(addfd->fd, addfd->file, addfd->flags);
1083+
addfd->ret = fd;
1084+
1085+
if (addfd->ioctl_flags & SECCOMP_ADDFD_FLAG_SEND) {
1086+
/* If we fail reset and return an error to the notifier */
1087+
if (fd < 0) {
1088+
n->state = SECCOMP_NOTIFY_SENT;
1089+
} else {
1090+
/* Return the FD we just added */
1091+
n->flags = 0;
1092+
n->error = 0;
1093+
n->val = fd;
1094+
}
1095+
}
1096+
1097+
/*
1098+
* Mark the notification as completed. From this point, addfd mem
1099+
* might be invalidated and we can't safely read it anymore.
1100+
*/
10801101
complete(&addfd->completion);
10811102
}
10821103

@@ -1120,7 +1141,7 @@ static int seccomp_do_user_notification(int this_syscall,
11201141
struct seccomp_kaddfd, list);
11211142
/* Check if we were woken up by a addfd message */
11221143
if (addfd)
1123-
seccomp_handle_addfd(addfd);
1144+
seccomp_handle_addfd(addfd, &n);
11241145

11251146
} while (n.state != SECCOMP_NOTIFY_REPLIED);
11261147

@@ -1581,7 +1602,7 @@ static long seccomp_notify_addfd(struct seccomp_filter *filter,
15811602
if (addfd.newfd_flags & ~O_CLOEXEC)
15821603
return -EINVAL;
15831604

1584-
if (addfd.flags & ~SECCOMP_ADDFD_FLAG_SETFD)
1605+
if (addfd.flags & ~(SECCOMP_ADDFD_FLAG_SETFD | SECCOMP_ADDFD_FLAG_SEND))
15851606
return -EINVAL;
15861607

15871608
if (addfd.newfd && !(addfd.flags & SECCOMP_ADDFD_FLAG_SETFD))
@@ -1591,6 +1612,7 @@ static long seccomp_notify_addfd(struct seccomp_filter *filter,
15911612
if (!kaddfd.file)
15921613
return -EBADF;
15931614

1615+
kaddfd.ioctl_flags = addfd.flags;
15941616
kaddfd.flags = addfd.newfd_flags;
15951617
kaddfd.setfd = addfd.flags & SECCOMP_ADDFD_FLAG_SETFD;
15961618
kaddfd.fd = addfd.newfd;
@@ -1616,6 +1638,23 @@ static long seccomp_notify_addfd(struct seccomp_filter *filter,
16161638
goto out_unlock;
16171639
}
16181640

1641+
if (addfd.flags & SECCOMP_ADDFD_FLAG_SEND) {
1642+
/*
1643+
* Disallow queuing an atomic addfd + send reply while there are
1644+
* some addfd requests still to process.
1645+
*
1646+
* There is no clear reason to support it and allows us to keep
1647+
* the loop on the other side straight-forward.
1648+
*/
1649+
if (!list_empty(&knotif->addfd)) {
1650+
ret = -EBUSY;
1651+
goto out_unlock;
1652+
}
1653+
1654+
/* Allow exactly only one reply */
1655+
knotif->state = SECCOMP_NOTIFY_REPLIED;
1656+
}
1657+
16191658
list_add(&kaddfd.list, &knotif->addfd);
16201659
complete(&knotif->ready);
16211660
mutex_unlock(&filter->notify_lock);

0 commit comments

Comments
 (0)