Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2651208pxj; Mon, 31 May 2021 07:25:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYEZ9FiGIvQ0EfeohZqsslsr3Et70Hl7SicJ1L4ny7sxNk/evcCT3Zh2z7ms3iZgMqK1Hh X-Received: by 2002:a92:2c02:: with SMTP id t2mr17451547ile.233.1622471102540; Mon, 31 May 2021 07:25:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622471102; cv=none; d=google.com; s=arc-20160816; b=f4MPv2VhZEmuvGyaQWI5Z1o/qODGZgXSd8gizlQeM016W9UpPOVVkEIIuwarg3Hb/v 40elrJ7yyhSJ3gN+pnqd5CrX1WNYu8fvXSLn+FJo6BUGe+lqM+axczpanoFWRVt31XeF udO8lgZ9ZY4YbDOafXc05KLAKctkZBLXnHaUosaOGLtlpM95CjfhdjUitp7ZqWWDystZ ivkApgPnGMfbC+4vwC5KCpxuwY6KkoK6xfHqKDwLOn4jEWz0nxWYKmtINONoXv/oT7TJ mEf1McJKliLNDNuloecmtTVGz0gM9ugDSdDtWtyy3TUQFO4x7BcG8WCXwQruiEdSCem1 0guQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=DbPmHrK82jwyeF36U2t3iv9Y329LPlZAy1JVAjkgC6A=; b=xdYEERFw3VT1w+dWR8yPaaFle43f+pP3/oXydfBZUKsOCyYWdPylS7fP7LbWYGnZOr i/zv+g9jLGTF4GfhsBuTjGK5DF87qW3f18clgJqdKoDIIleWaBdwnrreKdESQDWj46cR ZW/MEh0r6A64I7XIvB0L4ZHaiRzoYznA/IoPzooBUW99xJ5JyVgGh0H1Wx5vonThs2jb hd+BM2YXbpfXUoYFv2xomvHZdLk8RNuOdyMKc+CnozICH4e3z7wAXXeihET6wEBga87U XlHmDHzGAvtZ8z9gO032OPYK1f4UVazTDrjNoFQ5PWU6LWoFjVi87YSrxLcdd1INHpnB 2lHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Sd/jz7YC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p31si16130158jac.95.2021.05.31.07.24.49; Mon, 31 May 2021 07:25:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Sd/jz7YC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231814AbhEaOYh (ORCPT + 99 others); Mon, 31 May 2021 10:24:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:56074 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231799AbhEaNur (ORCPT ); Mon, 31 May 2021 09:50:47 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BBA8061434; Mon, 31 May 2021 13:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622467925; bh=8sBlBF3aZ7IQ3aX/sNN68lKWcwkvZUQSjfXUGH8riqo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Sd/jz7YCxyqOQG/USjUKJZQZB+DQ83me+C+ro8+ldIBbYbSv/hAzpUUkid6ZLB54K TmnEQEkfFNdRefP0I7qkFnpTAXsspH1Ws/ie8UGC1N6030i9uq+AjXDVduisXQOb55 deKEe4c97A5BqPcUR1s62MNGadA1fEvX2SbzSjw8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sargun Dhillon , Tycho Andersen , Christian Brauner , Kees Cook , Rodrigo Campos Subject: [PATCH 5.10 052/252] seccomp: Refactor notification handler to prepare for new semantics Date: Mon, 31 May 2021 15:11:57 +0200 Message-Id: <20210531130659.748784740@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130657.971257589@linuxfoundation.org> References: <20210531130657.971257589@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sargun Dhillon commit ddc473916955f7710d1eb17c1273d91c8622a9fe upstream. This refactors the user notification code to have a do / while loop around the completion condition. This has a small change in semantic, in that previously we ignored addfd calls upon wakeup if the notification had been responded to, but instead with the new change we check for an outstanding addfd calls prior to returning to userspace. Rodrigo Campos also identified a bug that can result in addfd causing an early return, when the supervisor didn't actually handle the syscall [1]. [1]: https://lore.kernel.org/lkml/20210413160151.3301-1-rodrigo@kinvolk.io/ Fixes: 7cf97b125455 ("seccomp: Introduce addfd ioctl to seccomp user notifier") Signed-off-by: Sargun Dhillon Acked-by: Tycho Andersen Acked-by: Christian Brauner Signed-off-by: Kees Cook Tested-by: Rodrigo Campos Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20210517193908.3113-3-sargun@sargun.me Signed-off-by: Greg Kroah-Hartman --- kernel/seccomp.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -864,28 +864,30 @@ static int seccomp_do_user_notification( 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 */