Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4237547yba; Tue, 23 Apr 2019 18:05:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqyKVzT9j3XMgGDpwRl+2dvvG0wS1zomvpxiuuoRbl/F+9kgSPR9M6pybIb4tPv4QghjqPQ9 X-Received: by 2002:a17:902:6809:: with SMTP id h9mr20402581plk.274.1556067941189; Tue, 23 Apr 2019 18:05:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556067941; cv=none; d=google.com; s=arc-20160816; b=LNEV8iRm/lq7YKg37raQ5kUxhnAWdc7O3Aoh9Wps+QfV+9GTz7kd9zr8CeeSq4RPUO +mKKQuO+bq0KHoFrNeZbfFEOBEcCCsHcO/GcQVgHbopp6lg0d0XpyebFWa7bGw+V2IL6 QiULLH53g5HwWivf2NZgma+DFCagIlhNVy7SaAUBnPcicxeqRHu3OV+uLRBl6Emdx2KK E46ws0SlKfa77wDHumGaAdR7qh/ylaHPw4SdUovcudvzZHJWnHEjJ6S0kCGIR0pvc9ul zGJlrfU7vyLKTgJ4bzqltXF1l9qoyqmOh0bVqcavQNQKWVM5Dj45Q0UUpHsfnZmeeRWJ XFmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=mgGp/GRBCs0fDORPm8J9/AXYWRoFaNJ2A1fhxlIRTu8=; b=09v7qAEwREVTvL13VruqxZXfk6ozFkWe8nqMeuuG0hXrPuPDlhoH9V3LT7vIQbEGM+ IyuRbW4E2fWHWIzhM5j0aBmfbIcgt14BZETSPHZ1F1yHHhciFGjWwDEbDJa7jGKmRR4i 9112YK9shG2JDHMPci5qGbZgY2FzA+fCKZjWOCcMcitlDlT5chFXpVV8KU1iQ6SN9X9I bpN1hM0ImNHkzMpJsCoH43kB4/6q6ddYNYsxUZRGFWq6eV45wJrcfnpBsRTWsnRuS0Vn Ug4YbVguTbF5VHVnJPAcgjSfxhVzPtr79QZO/cXw+bo5J6cSJUcqIVUKMa9W1UiPDCIP 6opA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=M2+pi3lS; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k11si16379834pga.257.2019.04.23.18.05.24; Tue, 23 Apr 2019 18:05:41 -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=@chromium.org header.s=google header.b=M2+pi3lS; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729009AbfDXBCZ (ORCPT + 99 others); Tue, 23 Apr 2019 21:02:25 -0400 Received: from mail-ua1-f66.google.com ([209.85.222.66]:37095 "EHLO mail-ua1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726844AbfDXBCZ (ORCPT ); Tue, 23 Apr 2019 21:02:25 -0400 Received: by mail-ua1-f66.google.com with SMTP id l17so5464925uar.4 for ; Tue, 23 Apr 2019 18:02:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=mgGp/GRBCs0fDORPm8J9/AXYWRoFaNJ2A1fhxlIRTu8=; b=M2+pi3lSxbTZmmC6p50m4pCOE5uptyL8RaaWF3gFDZ4tmq6RSCHrfDyW+SZvTlz9rG tfGy7oWH3hpKPp/TJh4q1fhRNqJvMDWjazlt6t26yEnv2pL4Ym39TAX5OaITuECz4Sbg U2O92RaZnQFtNFB0VzDryJG3BTuR0dOL58H84= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=mgGp/GRBCs0fDORPm8J9/AXYWRoFaNJ2A1fhxlIRTu8=; b=Q785Aa2IkAxxO/PoBV1f4/KdXsfeP90kfXhjeQh71YjiPnSsZhzJILMVM1O7+gqDis Rp/wHR4ZMwFbGM/OtYa0G0FAhanUU5JBY2zjoMsPKwYJDlwxasaB4svEgrM2/rJmBLmB UCb8wvkAuL7PQ/ZCSfzSUJ6Gh/bglurEW9EWaqXYHhdt4o8DD3lCGu+g/CdSYlle/zOV NdX9xaqlbyBjHVJoI2QdbexPdAzkYPNOGHzhet5lK7FZdE5R41WJzF4ZXCmEk5etOW/B 2Fe7Wq2/UvY/Re83Hznd8w7tOjcbncdHvyPuwFG8bn8fAVrfdL18jp6lgv68GrGgO6+T Uxhg== X-Gm-Message-State: APjAAAWBYqSX2juWuqiEVIrtsDVhD600UgTwL+QLWhgt58JTNsD9BY/R ylb6RPrewhJZ6wjqMbPxFBC1Cj3vkeU= X-Received: by 2002:ab0:6983:: with SMTP id t3mr877660uaq.142.1556067742955; Tue, 23 Apr 2019 18:02:22 -0700 (PDT) Received: from mail-vk1-f172.google.com (mail-vk1-f172.google.com. [209.85.221.172]) by smtp.gmail.com with ESMTPSA id m23sm26699286vsl.24.2019.04.23.18.02.21 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Tue, 23 Apr 2019 18:02:22 -0700 (PDT) Received: by mail-vk1-f172.google.com with SMTP id 204so2318681vkv.2 for ; Tue, 23 Apr 2019 18:02:21 -0700 (PDT) X-Received: by 2002:a1f:7d8e:: with SMTP id y136mr9345556vkc.40.1556067741281; Tue, 23 Apr 2019 18:02:21 -0700 (PDT) MIME-Version: 1.0 References: <20190306201413.14153-1-tycho@tycho.ws> <20190306201413.14153-2-tycho@tycho.ws> <20190423233422.GK3758@cisco> In-Reply-To: <20190423233422.GK3758@cisco> From: Kees Cook Date: Tue, 23 Apr 2019 18:02:08 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/2] seccomp: disallow NEW_LISTENER and TSYNC flags To: Tycho Andersen Cc: James Morris , LKML , "# 3.4.x" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 23, 2019 at 4:34 PM Tycho Andersen wrote: > > On Tue, Apr 23, 2019 at 04:31:45PM -0700, Kees Cook wrote: > > On Tue, Apr 23, 2019 at 3:09 PM Kees Cook wrote: > > > > > > On Wed, Mar 6, 2019 at 12:14 PM Tycho Andersen wrote: > > > > > > > > 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. > > > > > > > > Signed-off-by: Tycho Andersen > > > > Fixes: 6a21cc50f0c7 ("seccomp: add a return code to trap to userspace") > > > > CC: stable@vger.kernel.org # v5.0+ > > > > > > Thanks! Sorry I missed this. James, can you take this for Linus's > > > fixes for v5.1? (Or should I send a pull request to you?) > > > > > > Acked-by: Kees Cook > > > > > > Let's also add: > > > > > > Reported-by: syzbot+b562969adb2e04af3442@syzkaller.appspotmail.com > > > > > > > --- > > > > kernel/seccomp.c | 17 +++++++++++++++-- > > > > 1 file changed, 15 insertions(+), 2 deletions(-) > > > > > > > > diff --git a/kernel/seccomp.c b/kernel/seccomp.c > > > > index d0d355ded2f4..79bada51091b 100644 > > > > --- a/kernel/seccomp.c > > > > +++ b/kernel/seccomp.c > > > > @@ -500,7 +500,10 @@ seccomp_prepare_user_filter(const char __user *user_filter) > > > > * > > > > * 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(unsigned int flags, > > > > 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 > > > > + * succeded or failed. So, let's disallow this combination. > > > > > > also a tiny typo: succeeded > > > > > > > + */ > > > > + if ((flags & SECCOMP_FILTER_FLAG_TSYNC) && > > > > + (flags && SECCOMP_FILTER_FLAG_NEW_LISTENER)) > > > > also a typo: && should be & > > Oh, yes. Do you want me to send another version? Nah, I fixed it up. :) -- Kees Cook