Merge tag 'drm-intel-fixes-2021-06-03' of git://anongit.freedesktop.org/drm/drm-intel...
[linux-2.6-microblaze.git] / kernel / seccomp.c
index 1e63db4..9f58049 100644 (file)
@@ -119,8 +119,11 @@ struct seccomp_kaddfd {
        int fd;
        unsigned int flags;
 
-       /* To only be set on reply */
-       int ret;
+       union {
+               bool setfd;
+               /* To only be set on reply */
+               int ret;
+       };
        struct completion completion;
        struct list_head list;
 };
@@ -1069,7 +1072,11 @@ static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd)
         * that it has been handled.
         */
        list_del_init(&addfd->list);
-       addfd->ret = receive_fd_replace(addfd->fd, addfd->file, addfd->flags);
+       if (!addfd->setfd)
+               addfd->ret = receive_fd(addfd->file, addfd->flags);
+       else
+               addfd->ret = receive_fd_replace(addfd->fd, addfd->file,
+                                               addfd->flags);
        complete(&addfd->completion);
 }
 
@@ -1098,28 +1105,30 @@ static int seccomp_do_user_notification(int this_syscall,
 
        up(&match->notif->request);
        wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
-       mutex_unlock(&match->notify_lock);
 
        /*
         * This is where we wait for a reply from userspace.
         */
-wait:
-       err = wait_for_completion_interruptible(&n.ready);
-       mutex_lock(&match->notify_lock);
-       if (err == 0) {
-               /* Check if we were woken up by a addfd message */
+       do {
+               mutex_unlock(&match->notify_lock);
+               err = wait_for_completion_interruptible(&n.ready);
+               mutex_lock(&match->notify_lock);
+               if (err != 0)
+                       goto interrupted;
+
                addfd = list_first_entry_or_null(&n.addfd,
                                                 struct seccomp_kaddfd, list);
-               if (addfd && n.state != SECCOMP_NOTIFY_REPLIED) {
+               /* Check if we were woken up by a addfd message */
+               if (addfd)
                        seccomp_handle_addfd(addfd);
-                       mutex_unlock(&match->notify_lock);
-                       goto wait;
-               }
-               ret = n.val;
-               err = n.error;
-               flags = n.flags;
-       }
 
+       }  while (n.state != SECCOMP_NOTIFY_REPLIED);
+
+       ret = n.val;
+       err = n.error;
+       flags = n.flags;
+
+interrupted:
        /* If there were any pending addfd calls, clear them out */
        list_for_each_entry_safe(addfd, tmp, &n.addfd, list) {
                /* The process went away before we got a chance to handle it */
@@ -1583,8 +1592,8 @@ static long seccomp_notify_addfd(struct seccomp_filter *filter,
                return -EBADF;
 
        kaddfd.flags = addfd.newfd_flags;
-       kaddfd.fd = (addfd.flags & SECCOMP_ADDFD_FLAG_SETFD) ?
-                   addfd.newfd : -1;
+       kaddfd.setfd = addfd.flags & SECCOMP_ADDFD_FLAG_SETFD;
+       kaddfd.fd = addfd.newfd;
        init_completion(&kaddfd.completion);
 
        ret = mutex_lock_interruptible(&filter->notify_lock);