Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp6884165ybe; Wed, 18 Sep 2019 10:35:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqzMnmD2OdEAnzoizC2OcbP1xGKuCyvP0aBI0tq4eWxV+KFao3jUF3Jsn1JffqpTvdkVHTuY X-Received: by 2002:a05:6402:1a4c:: with SMTP id bf12mr11756546edb.277.1568828124111; Wed, 18 Sep 2019 10:35:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568828124; cv=none; d=google.com; s=arc-20160816; b=UWQZo6AFwGsNxuQMmJgPVMhESjMWYNpZit5rWY0BNUJ3BHyo61GHQstPWGPm6eqb9Y 8m6o4BfaARl4x8niKIP+szbSHJxyX6xaDFENtaXUrVmuZw4bJv4xZeE+mmS3WJyOqcaC am3EHi/hR9qrvOM4IEpyUbPwl6eSkuOkdtXma2TPWTKgW2TRnHUXyUTOH8fMHoUeRLJn tfDKuOHdnGU/1aelaEq1UV4hF5xCy+qxxe/1XL1hUNnqz4OGYwpJjcX+tqEv6UuKAdkT tQosVSnyUYla+lGSI/YER0tMilAsBCyRh2Njnv/5IGOpiGBJI0aSbQ39of1cuL9Bo/7X 7KHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=izemo0Z6cE0j5OZM+tXjxCgJOy9LkdmZpvEY3TVF51M=; b=T/siYiF/VgBxjzEDbdLct5U/4aaVECFNN5NShmvWVYtYQUnxpUiP8puD7UzEIEFkuY sdHWZerxJ+L6ZzySvfzkXzuwTPtyNi5WkXtnPLF81J//9vytQHfFLGhhlquQWL0mCSfY ysR0/lt4X/8dCHhoR9h7DXOiP5dXBGfTcO6pYxGG9AuRNYRAtMYkfVqGIreOA3gwMPhF qhpWES5BjiK7Uq0j/Q3+aMmNltJQPaswcrqsr2YDuNU2pSyeyibQXfFNMpVlchgjhBqo d87sooRb3vqJc509tDb065T4gLChpMlNCrzFv+hwWSAA+3sYnPNv8t08M7XhEncC3e5Z e3Zg== ARC-Authentication-Results: i=1; mx.google.com; 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 k1si3722995ede.255.2019.09.18.10.35.00; Wed, 18 Sep 2019 10:35:24 -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; 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 S1731757AbfIRPqi (ORCPT + 99 others); Wed, 18 Sep 2019 11:46:38 -0400 Received: from mx1.mailbox.org ([80.241.60.212]:18286 "EHLO mx1.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726676AbfIRPqg (ORCPT ); Wed, 18 Sep 2019 11:46:36 -0400 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id A4F6050D4B; Wed, 18 Sep 2019 17:46:30 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id ZgCJrYNr5TyO; Wed, 18 Sep 2019 17:46:22 +0200 (CEST) Date: Wed, 18 Sep 2019 17:46:15 +0200 From: Aleksa Sarai To: Jann Horn Cc: Al Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , David Howells , Shuah Khan , Shuah Khan , Ingo Molnar , Peter Zijlstra , Christian Brauner , Andy Lutomirski , Eric Biederman , Andrew Morton , Alexei Starovoitov , Kees Cook , Tycho Andersen , David Drysdale , Chanho Min , Oleg Nesterov , Rasmus Villemoes , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Aleksa Sarai , Linus Torvalds , Linux Containers , linux-alpha@vger.kernel.org, Linux API , linux-arch , linux-arm-kernel@lists.infradead.org, linux-fsdevel , linux-ia64@vger.kernel.org, kernel list , "open list:KERNEL SELFTEST FRAMEWORK" , linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390 , linux-sh@vger.kernel.org, linux-xtensa@linux-xtensa.org, sparclinux@vger.kernel.org Subject: Re: [PATCH v12 05/12] namei: obey trailing magic-link DAC permissions Message-ID: <20190918154615.suruy5v5xjftfwyl@yavin.microfocus.com> References: <20190904201933.10736-1-cyphar@cyphar.com> <20190904201933.10736-6-cyphar@cyphar.com> <20190918135100.sdxdmdluq6wlwryv@yavin.microfocus.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="blplt4ksioniygek" Content-Disposition: inline In-Reply-To: <20190918135100.sdxdmdluq6wlwryv@yavin.microfocus.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --blplt4ksioniygek Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-09-18, Aleksa Sarai wrote: > On 2019-09-17, Jann Horn wrote: > > On Wed, Sep 4, 2019 at 10:21 PM Aleksa Sarai wrote: > > > The ability for userspace to "re-open" file descriptors through > > > /proc/self/fd has been a very useful tool for all sorts of usecases > > > (container runtimes are one common example). However, the current > > > interface for doing this has resulted in some pretty subtle security > > > holes. Userspace can re-open a file descriptor with more permissions > > > than the original, which can result in cases such as /proc/$pid/exe > > > being re-opened O_RDWR at a later date even though (by definition) > > > /proc/$pid/exe cannot be opened for writing. When combined with O_PATH > > > the results can get even more confusing. > > [...] > > > Instead we have to restrict it in such a way that it doesn't break > > > (good) users but does block potential attackers. The solution applied= in > > > this patch is to restrict *re-opening* (not resolution through) > > > magic-links by requiring that mode of the link be obeyed. Normal > > > symlinks have modes of a+rwx but magic-links have other modes. These > > > magic-link modes were historically ignored during path resolution, but > > > they've now been re-purposed for more useful ends. > >=20 > > Thanks for dealing with this issue! > >=20 > > [...] > > > diff --git a/fs/namei.c b/fs/namei.c > > > index 209c51a5226c..54d57dad0f91 100644 > > > --- a/fs/namei.c > > > +++ b/fs/namei.c > > > @@ -872,7 +872,7 @@ void nd_jump_link(struct path *path) > > > > > > nd->path =3D *path; > > > nd->inode =3D nd->path.dentry->d_inode; > > > - nd->flags |=3D LOOKUP_JUMPED; > > > + nd->flags |=3D LOOKUP_JUMPED | LOOKUP_MAGICLINK_JUMPED; > > > } > > [...] > > > +static int trailing_magiclink(struct nameidata *nd, int acc_mode, > > > + fmode_t *opath_mask) > > > +{ > > > + struct inode *inode =3D nd->link_inode; > > > + fmode_t upgrade_mask =3D 0; > > > + > > > + /* Was the trailing_symlink() a magic-link? */ > > > + if (!(nd->flags & LOOKUP_MAGICLINK_JUMPED)) > > > + return 0; > > > + > > > + /* > > > + * Figure out the upgrade-mask of the link_inode. Since these= aren't > > > + * strictly POSIX semantics we don't do an acl_permission_che= ck() here, > > > + * so we only care that at least one bit is set for each upgr= ade-mode. > > > + */ > > > + if (inode->i_mode & S_IRUGO) > > > + upgrade_mask |=3D FMODE_PATH_READ; > > > + if (inode->i_mode & S_IWUGO) > > > + upgrade_mask |=3D FMODE_PATH_WRITE; > > > + /* Restrict the O_PATH upgrade-mask of the caller. */ > > > + if (opath_mask) > > > + *opath_mask &=3D upgrade_mask; > > > + return may_open_magiclink(upgrade_mask, acc_mode); > > > } > >=20 > > This looks racy because entries in the file descriptor table can be > > switched out as long as task->files->file_lock isn't held. Unless I'm > > missing something, something like the following (untested) would > > bypass this restriction: >=20 > You're absolutely right -- good catch! >=20 > > Perhaps you could change nd_jump_link() to "void nd_jump_link(struct > > path *path, umode_t link_mode)", and let proc_pid_get_link() pass the > > link_mode through from an out-argument of .proc_get_link()? Then > > proc_fd_link() could grab the proper mode in a race-free manner. And > > nd_jump_link() could stash the mode in the nameidata. >=20 > This indeed does appear to be the simplest solution -- I'm currently > testing a variation of the patch you proposed (with a few extra bits to > deal with nd_jump_link and proc_get_link being used elsewhere). >=20 > I'll include this change (assuming it fixes the flaw you found) in the > v13 series I'll send around next week. Thanks, Jann! In case you're interested -- I've also included a selftest based on this attack in my series (though it uses CLONE_FILES so that we could also test O_EMPTYPATH, which wasn't affected because it didn't go through procfs and thus couldn't hit the "outdated inode->i_mode" problem). The attack script succeeds around 20% of the time on the original patchset, and with the updated patchset it doesn't succeed in several hundred thousand attempts (which I've repeated a few times). --=20 Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH --blplt4ksioniygek Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQSxZm6dtfE8gxLLfYqdlLljIbnQEgUCXYJRRAAKCRCdlLljIbnQ Ep3WAP0cvG8YTD9aS1zuiIbFfMQLKt1nuxBciHwn7LaCHk9Z0QEAtNdPaxztVO/p utsBd24Q6vZYzx6vj8OnW5nGpjaLpQA= =rL8c -----END PGP SIGNATURE----- --blplt4ksioniygek--