Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2795730yba; Mon, 6 May 2019 11:39:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwH5MpvbvIJTMecNDzCNdmehaI3VLpIs/gB8MWm+ScSIAORFa6ik2wAA85LWyqUjJ4yF6OB X-Received: by 2002:a17:902:7783:: with SMTP id o3mr34236630pll.159.1557167954718; Mon, 06 May 2019 11:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557167954; cv=none; d=google.com; s=arc-20160816; b=cfy8D3r9o+7ym3E4Y7sulgAdzwxI2pyOc0m8cJORZBx8UeiA2wTw+hGkApnkVVz2rv n/8ogFEa4iG+wtV5qzPjL0/tE6NGnc/34e0QnoYHXYSIk8FpTJDqOBSpCSFkn0jnhO0E TmRiBU5C9B3e9kb790prxvVWzX9Fssp1Kwg9b5Y8YR2uogg/knfgHSF6RzO/pgngdSTW jIOMZM3rO4aw0+VIXbaJq2zOwmIST1l7e8yWHd4hH/RugevB8ZrgImzdGMmelbjvUkm6 g76yHqL6P7EBSY3bxhBk3qcfZ9xniAEMACOqSyNrRn0RNK0NkVw629XkUab63jCy/3pb 7tFQ== 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=DJEsU4nDrYVuZtMVVFf4WrJq7TRs9Dl+cfY2LjHeYik=; b=HuamHW+ofSJEmrtYEcIkTbml+1yDt2S2GabVwPfdYFqM78ILo47yqFZaqwqh/fwFiU V9xBk1ccNNM7XlYsH8ePVykk77Qm3wgRWMky7+uDlEvsty8RQHOtwYXXnQS4fq+AHbzW i7XOPHjAlvv79Vsq/OXkWV09WiOG7MfiUp7WZ39hJFYW+3CfaC2kyFTiec3bH6XVBBrm ZwcjLwUaW9KZboBq8VPxgkVTf1nvu9nbKsxp63oradgg6pkHEYEZP6BRtHrzzzPSIc4i y40qS/IQpG656XGpccPo+Y6mWEs7RwdAVhkArNMD8CIVUPnCdtcWgpB75CkHp2BRcPlQ b+1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MBoBEQKf; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bh3si15656812plb.284.2019.05.06.11.38.57; Mon, 06 May 2019 11:39:14 -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=@google.com header.s=20161025 header.b=MBoBEQKf; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726560AbfEFSiF (ORCPT + 99 others); Mon, 6 May 2019 14:38:05 -0400 Received: from mail-oi1-f195.google.com ([209.85.167.195]:39339 "EHLO mail-oi1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726370AbfEFSiE (ORCPT ); Mon, 6 May 2019 14:38:04 -0400 Received: by mail-oi1-f195.google.com with SMTP id x16so4487403oic.6 for ; Mon, 06 May 2019 11:38:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=DJEsU4nDrYVuZtMVVFf4WrJq7TRs9Dl+cfY2LjHeYik=; b=MBoBEQKfRSbHx9GOc308vRlaTuHtNlVgKaleqLz6VWU9jPubTioCuQS5bAsqlJPl70 kLLf/a2s74wO9d7xMzl6NvPOTPL95DMI5Kd8roODkNjxkLqb+OJj7T2K9+fA3DD5YiFy Csn5W9HSmnaJHJQKPXJBxM2D9lac7YK6NYqM6oWR3WwnOdIdF2bxlYFtB6WOB8Re40Ge 1pbNACljeg5BON3hEiNjZvJYjZgCF6OX3MuKJy0mXq/rO3FU0ptaX4FNkdijc4oSUknn hJFQ0aAo3YIZ2MKlhIfRpUSY02KuEtPwlFSu2EjYDy7zdM0tmerfOmBXnlYcrQB+zWBH YTHQ== 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=DJEsU4nDrYVuZtMVVFf4WrJq7TRs9Dl+cfY2LjHeYik=; b=XUX1/im/B7l4Ua6J3PyxEVd1gHiT1YHdRII4/7lB+GjwSlcrUo03G/+r7ktQRXf7EN J8+FxT+M3o6wSoHgyrLVySZZgyOO08Oei2kWRU0m7xbzLFcPe9MA9huOj9wnMrqAuzqI pRNBo/arkFMiqkxMbFbo13dqnJxDvqB2r1y/seHIdrv+4XC1pOdS4ZlyYcFAm13fsuZR gP5YukZvBiKk3yOqzJhAMh6SxdUvfOsR7D5pmSQjVysiMvgG1tr9F6f7w2bqDvZ4T35G K/W/GuqX/hq5PSha3wiTW8tiP+WXNx9WI4d9M630g4whl39zANsTbLq71KKMce3q9qpD l6Ww== X-Gm-Message-State: APjAAAWjybn55+45RrrBU4y7Fpd1Pb4TxCkd0k8bAK2rF38IEH4SRwzP xV0Ke0w50JC+rtXYGBGxQWx3fTcPGOTXetN4vZ8sxQ== X-Received: by 2002:aca:5412:: with SMTP id i18mr2217737oib.157.1557167883599; Mon, 06 May 2019 11:38:03 -0700 (PDT) MIME-Version: 1.0 References: <20190506165439.9155-1-cyphar@cyphar.com> <20190506165439.9155-6-cyphar@cyphar.com> In-Reply-To: <20190506165439.9155-6-cyphar@cyphar.com> From: Jann Horn Date: Mon, 6 May 2019 20:37:37 +0200 Message-ID: Subject: Re: [PATCH v6 5/6] binfmt_*: scope path resolution of interpreters To: Aleksa Sarai , Andy Lutomirski Cc: Al Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , David Howells , Eric Biederman , Andrew Morton , Alexei Starovoitov , Kees Cook , Christian Brauner , Tycho Andersen , David Drysdale , Chanho Min , Oleg Nesterov , Aleksa Sarai , Linus Torvalds , containers@lists.linux-foundation.org, linux-fsdevel , Linux API , kernel list , linux-arch 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 Mon, May 6, 2019 at 6:56 PM Aleksa Sarai wrote: > The need to be able to scope path resolution of interpreters became > clear with one of the possible vectors used in CVE-2019-5736 (which > most major container runtimes were vulnerable to). > > Naively, it might seem that openat(2) -- which supports path scoping -- > can be combined with execveat(AT_EMPTY_PATH) to trivially scope the > binary being executed. Unfortunately, a "bad binary" (usually a symlink) > could be written as a #!-style script with the symlink target as the > interpreter -- which would be completely missed by just scoping the > openat(2). An example of this being exploitable is CVE-2019-5736. > > In order to get around this, we need to pass down to each binfmt_* > implementation the scoping flags requested in execveat(2). In order to > maintain backwards-compatibility we only pass the scoping AT_* flags. > > To avoid breaking userspace (in the exceptionally rare cases where you > have #!-scripts with a relative path being execveat(2)-ed with dfd != > AT_FDCWD), we only pass dfd down to binfmt_* if any of our new flags are > set in execveat(2). This seems extremely dangerous. I like the overall series, but not this patch. > @@ -1762,6 +1774,12 @@ static int __do_execve_file(int fd, struct filename *filename, > > sched_exec(); > > + bprm->flags = flags & (AT_XDEV | AT_NO_MAGICLINKS | AT_NO_SYMLINKS | > + AT_THIS_ROOT); [...] > +#define AT_THIS_ROOT 0x100000 /* - Scope ".." resolution to dirfd (like chroot(2)). */ So now what happens if there is a setuid root ELF binary with program interpreter "/lib64/ld-linux-x86-64.so.2" (like /bin/su), and an unprivileged user runs it with execveat(..., AT_THIS_ROOT)? Is that going to let the unprivileged user decide which interpreter the setuid-root process should use? From a high-level perspective, opening the interpreter should be controlled by the program that is being loaded, not by the program that invoked it. In my opinion, CVE-2019-5736 points out two different problems: The big problem: The __ptrace_may_access() logic has a special-case short-circuit for "introspection" that you can't opt out of; this makes it possible to open things in procfs that are related to the current process even if the credentials of the process wouldn't permit accessing another process like it. I think the proper fix to deal with this would be to add a prctl() flag for "set whether introspection is allowed for this process", and if userspace has manually un-set that flag, any introspection special-case logic would be skipped. An additional problem: /proc/*/exe can be used to open a file for writing; I think it may have been Andy Lutomirski who pointed out some time ago that it would be nice if you couldn't use /proc/*/fd/* to re-open files with more privileges, which is sort of the same thing.