From owner-freebsd-hackers@freebsd.org Sat Mar 3 11:21:31 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 87145F325D8 for ; Sat, 3 Mar 2018 11:21:31 +0000 (UTC) (envelope-from benl@google.com) Received: from mail-it0-x232.google.com (mail-it0-x232.google.com [IPv6:2607:f8b0:4001:c0b::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 1BDD17C26C for ; Sat, 3 Mar 2018 11:21:31 +0000 (UTC) (envelope-from benl@google.com) Received: by mail-it0-x232.google.com with SMTP id w63so4459320ita.3 for ; Sat, 03 Mar 2018 03:21:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=+nw0QLwXyx2oWj8ZRbnBmccX+ahAV9qBipJELGpnd0Y=; b=sUutoUbZvamX9Z3lRAzSeciAbcWhZ6H8ARY0KbNlyzMubdTkS+WNBEmGn8eJIY6gDE i0Jn1nrnouzu0OipPHs+tUpN4MeaVpMLdKEtKaZBq9qLSIQu+d9noeE95QaWkE1/1FNo naWZc8WnKf08SQwPXYdIexhPYZQ3YhSMCyJW679P6rP/6vKnvjrIaEn/stBgn98bNe1O V/HMaSae8ATo0HnG8KfriIlzL7q7z8ieKCehoPYbW8h/6+W5W6daH1HhiEWn+p8e6MPT cZGMH+wH3BY9lrpbM038g+2q17CiKZ6TJrpDtDnhOFqp5fxg2h889j1WaPBotXdlmkIg OTvg== X-Gm-Message-State: AElRT7EStN+l3w5H585POQi/zyFDgK/9fYmuvwdIp23rjenbLExOF5SS HH+8IMon52PVYjzKaGs6nhg/9abltkfDQNNj0kDHrURP X-Google-Smtp-Source: AG47ELsFf/RAmyz8H27hKl/fj6T6sKP2k0P4gOBPzgPt3lEYiEBiifu/ifR5E/zCmBvP2k0pXQKXM3stDmHQrhEjQKQ= X-Received: by 10.36.48.135 with SMTP id q129mr6300422itq.23.1520076090129; Sat, 03 Mar 2018 03:21:30 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.167.204 with HTTP; Sat, 3 Mar 2018 03:21:29 -0800 (PST) In-Reply-To: <20180302183514.GA99279@x-wing> References: <20180302183514.GA99279@x-wing> From: Ben Laurie Date: Sat, 3 Mar 2018 11:21:29 +0000 Message-ID: Subject: Re: [capsicum] unlinkfd To: Mariusz Zaborski Cc: "" , freebsd-hackers@freebsd.org X-Mailman-Approved-At: Sat, 03 Mar 2018 13:42:07 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.25 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 03 Mar 2018 11:21:31 -0000 On 2 March 2018 at 18:35, Mariusz Zaborski wrote: > Hello, > > Today I would like to propose a new syscall called unlinkfd(2) which came > up > during a discussion with Ed Maste. > > Currently in UNIX we can=E2=80=99t remove files safely. If we will try to= do so we > always end up in a race condition. For example when we open a file, and > check > it with fstat, etc. then we want to unlink(2) it=E2=80=A6 but the file we= are > trying to > unlink could be a different one than the one we were fstating just a > moment ago. > > Another reason of implementing unlinkfd(2) came to us when we were trying > to sandbox some applications like: uudecode/b64decode or bspatch. It > occured > to us that we don=E2=80=99t have a good way of removing single files. Of = course we > can > try to determine in which directory we are in, and then open this > directory and > remove a single file. > > It looks even more bizarre if we would think about a program which > operates on > multiple files. If we would analyze a situation with two totally differen= t > directories like `/tmp` and `/home/oshogbo` we would end up with pre > opening > a root directory or keeping as many directories as we are working on open= . > All of that effort only to remove two files. This make it totally > impractical! > > I think that opening directories also presents some wider attack vector > because > we are keeping a single descriptor to a directory only to remove one file= . > Unfortunately this means that an attacker can remove all files in that > directory. > > I proposed this as well on the last Capsicum call. There was a suggestion > that > instead of doing a single syscall maybe we should have a Casper service > that > will allow us to remove files. Another idea was that we should perhaps > redesign > programs to create some subdirs work on the subdirs and then remove all > files in > this subdir. I don=E2=80=99t feel that creating a Casper service is a goo= d idea > because > we still have exactly the same issue of race condition. In my opinion > creating > subdirs is also a problem for us. > > First we would need to redesign some of our tools and I think we should > simplyfiy capsicumizition of the process instead of making it harder. > > Secondly we can create a temporary subdirectory but what will remove it? > We are going back to having a fd to directory in which we just created a > subdir. > Another way would be to have Casper service which would remove a director= y > but > with the risk of RC. > > In conclusion, I think we need syscall like unlinkfd(2), which turn out > taht it > is easy to implement. The only downside of this implementation is that we > not > only need to provide a fd but also a path file. This is because inodes no= r > vnodes don=E2=80=99t contain filenames. We are comparing vnodes of the fd= and the > given > path, if they are exactly the same we remove a file. In the syscall we ar= e > using > a fd so there is no Ambient Authority because we are proving that we > already > have access to that file. That seems incorrect. You are proving you have access to the inode, not the directory entry. So, for example, I could create a link to a file I wanted to remove, that I don't have permission to remove, then use this call to unlink it. > Thanks to that the syscall can be safely used with > Caspsicum. I have already discussed this with some people and they said > `Hey I already had that idea a while ago=E2=80=A6` so let=E2=80=99s do so= mething with that > idea! > If you are intereted in patch you can find it here: > https://reviews.freebsd.org/D14567 > > Thanks, > -- > Mariusz Zaborski > oshogbo//vx | http://oshogbo.vexillium.org > FreeBSD commiter | https://freebsd.org > Software developer | http://wheelsystems.com > If it's not broken, let's fix it till it is!!1 >