Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2575370yba; Mon, 6 May 2019 08:13:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqwBMX6PwC8rDr/7b2I+BKf8UfzyDeqG+vDeEu6i7SIPNvBsXXNoi/nrwD1DRg5AFrMFgbHZ X-Received: by 2002:a17:902:b782:: with SMTP id e2mr10529724pls.228.1557155591258; Mon, 06 May 2019 08:13:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557155591; cv=none; d=google.com; s=arc-20160816; b=VA03uJ7RcRF1/vSoruCpCZTks4yIwbJZL3h6FyQRzR2EKXz9jE2JKKlgVZV0BXdX8d 9J5ZjvMR48CPKage4MkjT7S1f9KyBYiNtfsL5outtoqdvVz1WHkUDsk/Ae+zw/UQ2M80 jmte8zLLviHs6utnYb6XCs+G43UybuKW5kgbxQUtvU2Bckww+9GUMfwrKRh+oeVQfDzA jE+lP00eptxlfApeErgbXqanIpbNxf6k5Uf00BYaTRMRWnLeKMwtScHDdONzi93iMh1Q ZAO6+v1w+2XdXlxjRtlTIca/BKk2AShNJ2ctfEKPn/UltcxP7zM5kaj8aGWYFjhFrH2Q n2bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KlJbgUV6xBKeGsdGQBbEKPIVJFJdvCEwsMF7NMh+xxs=; b=ktaMLDVuH7wn/C8KVMW0MjeOa3dhrYEv+nIdOqPgWqIjjU1Sef495SJcazzYC17p+M TYwTwyW6ET5KyedGsuEGwOB2Mr5JE7jTB/oX7iZvtmbict9cfVJ+6oGTIJUEbEtj8Q/m QpbaCv+fJLlXpAqs8QjQHKlxGkJzoxYuFty1EXEUCWXJexl5qEmJJpuQFkmPQofvN7rb Wbc2wVe1g2ZHtdFMJV1+ZPYWWJWV4JKKk55UkoLrEN2rq+6HCvx/SRgpbJcfMqoW4fye wjakJcu0tSPif0GAaS7bPu7ATSFpYWSiMBZx66CJH9fmf9qOOPioK0QNZbo9vWby6YN6 OH0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OE3Egd00; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 142si15848651pfu.129.2019.05.06.08.12.54; Mon, 06 May 2019 08:13:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OE3Egd00; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726560AbfEFPKs (ORCPT + 99 others); Mon, 6 May 2019 11:10:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:53706 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726520AbfEFOeJ (ORCPT ); Mon, 6 May 2019 10:34:09 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2110E20C01; Mon, 6 May 2019 14:34:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557153248; bh=C1jL5xd+iDU1mafVM9Qi8hHy+7kHs/6vpAvJf3AD2h0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OE3Egd00i5AwAOsIKHohaMaWt67kJVi8mvCmNGCRQ492PB3e6i/bHeAWqe1LGyJYf WYqR8y1tiYjAdyLX8lPu2qNYtfOlRAYSLt8RVHPZSfyz4NeA1qK4P/R5ZkiNpD7AU3 csAQ1+ZfiNaMak7mPpbJRs12mgXmKLtSftYrwvqk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+b562969adb2e04af3442@syzkaller.appspotmail.com, Tycho Andersen , Kees Cook , James Morris Subject: [PATCH 5.0 002/122] seccomp: Make NEW_LISTENER and TSYNC flags exclusive Date: Mon, 6 May 2019 16:31:00 +0200 Message-Id: <20190506143054.928504763@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190506143054.670334917@linuxfoundation.org> References: <20190506143054.670334917@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tycho Andersen commit 7a0df7fbc14505e2e2be19ed08654a09e1ed5bf6 upstream. As the comment notes, the return codes for TSYNC and NEW_LISTENER conflict, because they both return positive values, one in the case of success and one in the case of error. So, let's disallow both of these flags together. While this is technically a userspace break, all the users I know of are still waiting on me to land this feature in libseccomp, so I think it'll be safe. Also, at present my use case doesn't require TSYNC at all, so this isn't a big deal to disallow. If someone wanted to support this, a path forward would be to add a new flag like TSYNC_AND_LISTENER_YES_I_UNDERSTAND_THAT_TSYNC_WILL_JUST_RETURN_EAGAIN, but the use cases are so different I don't see it really happening. Finally, it's worth noting that this does actually fix a UAF issue: at the end of seccomp_set_mode_filter(), we have: if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) { if (ret < 0) { listener_f->private_data = NULL; fput(listener_f); put_unused_fd(listener); } else { fd_install(listener, listener_f); ret = listener; } } out_free: seccomp_filter_free(prepared); But if ret > 0 because TSYNC raced, we'll install the listener fd and then free the filter out from underneath it, causing a UAF when the task closes it or dies. This patch also switches the condition to be simply if (ret), so that if someone does add the flag mentioned above, they won't have to remember to fix this too. Reported-by: syzbot+b562969adb2e04af3442@syzkaller.appspotmail.com Fixes: 6a21cc50f0c7 ("seccomp: add a return code to trap to userspace") CC: stable@vger.kernel.org # v5.0+ Signed-off-by: Tycho Andersen Signed-off-by: Kees Cook Acked-by: James Morris Signed-off-by: Greg Kroah-Hartman --- kernel/seccomp.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -500,7 +500,10 @@ out: * * Caller must be holding current->sighand->siglock lock. * - * Returns 0 on success, -ve on error. + * Returns 0 on success, -ve on error, or + * - in TSYNC mode: the pid of a thread which was either not in the correct + * seccomp mode or did not have an ancestral seccomp filter + * - in NEW_LISTENER mode: the fd of the new listener */ static long seccomp_attach_filter(unsigned int flags, struct seccomp_filter *filter) @@ -1256,6 +1259,16 @@ static long seccomp_set_mode_filter(unsi if (flags & ~SECCOMP_FILTER_FLAG_MASK) return -EINVAL; + /* + * In the successful case, NEW_LISTENER returns the new listener fd. + * But in the failure case, TSYNC returns the thread that died. If you + * combine these two flags, there's no way to tell whether something + * succeeded or failed. So, let's disallow this combination. + */ + if ((flags & SECCOMP_FILTER_FLAG_TSYNC) && + (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER)) + return -EINVAL; + /* Prepare the new filter before holding any locks. */ prepared = seccomp_prepare_user_filter(filter); if (IS_ERR(prepared)) @@ -1302,7 +1315,7 @@ out: mutex_unlock(¤t->signal->cred_guard_mutex); out_put_fd: if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) { - if (ret < 0) { + if (ret) { listener_f->private_data = NULL; fput(listener_f); put_unused_fd(listener);