From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 534F1C433E1 for ; Tue, 19 May 2020 22:58:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3721620674 for ; Tue, 19 May 2020 22:58:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726318AbgESW6k (ORCPT ); Tue, 19 May 2020 18:58:40 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:37712 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726064AbgESW6k (ORCPT ); Tue, 19 May 2020 18:58:40 -0400 Received: from static-50-53-45-43.bvtn.or.frontiernet.net ([50.53.45.43] helo=[192.168.192.153]) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jbBBy-00019f-Ib; Tue, 19 May 2020 22:58:34 +0000 Subject: Re: [PATCH 0/4] Relocate execve() sanity checks To: Kees Cook , "Eric W. Biederman" Cc: Al Viro , Andrew Morton , Tetsuo Handa , Eric Biggers , Dmitry Vyukov , linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org References: <20200518055457.12302-1-keescook@chromium.org> <87a724t153.fsf@x220.int.ebiederm.org> <202005190918.D2BD83F7C@keescook> <87o8qjstyw.fsf@x220.int.ebiederm.org> <202005191052.0A6B1D5843@keescook> <87sgfvrckr.fsf@x220.int.ebiederm.org> <202005191342.97EE972E3@keescook> From: John Johansen Autocrypt: addr=john.johansen@canonical.com; prefer-encrypt=mutual; keydata= LS0tLS1CRUdJTiBQR1AgUFVCTElDIEtFWSBCTE9DSy0tLS0tCgptUUlOQkU1bXJQb0JFQURB azE5UHNnVmdCS2tJbW1SMmlzUFE2bzdLSmhUVEtqSmR3VmJrV1NuTm4rbzZVcDVrCm5LUDFm NDlFQlFsY2VXZzF5cC9Od2JSOGFkK2VTRU8vdW1hL0srUHFXdkJwdEtDOVNXRDk3Rkc0dUI0 L2Nhb20KTEVVOTdzTFFNdG52R1dkeHJ4VlJHTTRhbnpXWU1neno1VFptSWlWVFo0M091NVZw YVMxVnoxWlN4UDNoL3hLTgpaci9UY1c1V1FhaTh1M1BXVm5ia2poU1pQSHYxQmdoTjY5cXhF UG9tckpCbTFnbXR4M1ppVm1GWGx1d1RtVGdKCk9rcEZvbDduYkowaWxuWUhyQTdTWDNDdFIx dXBlVXBNYS9XSWFuVk85NldkVGpISElhNDNmYmhtUXViZTR0eFMKM0ZjUUxPSlZxUXN4NmxF OUI3cUFwcG05aFExMHFQV3dkZlB5LyswVzZBV3ROdTVBU2lHVkNJbld6bDJIQnFZZAovWmxs OTN6VXErTklvQ244c0RBTTlpSCt3dGFHRGNKeXdJR0luK2VkS050SzcyQU1nQ2hUZy9qMVpv V0g2WmVXClBqdVVmdWJWelp0bzFGTW9HSi9TRjRNbWRRRzFpUU50ZjRzRlpiRWdYdXk5Y0dp MmJvbUYwenZ5QkpTQU5weGwKS05CRFlLek42S3owOUhVQWtqbEZNTmdvbUwvY2pxZ0FCdEF4 NTlMK2RWSVpmYUYyODFwSWNVWnp3dmg1K0pvRwplT1c1dUJTTWJFN0wzOG5zem9veWtJSjVY ckFjaGtKeE5mejdrK0ZuUWVLRWtOekVkMkxXYzNRRjRCUVpZUlQ2ClBISGdhM1JneWtXNSsx d1RNcUpJTGRtdGFQYlhyRj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tLS1FTkQgUEdQIFBVQkxJQyBL RVkgQkxPQ0stLS0tLQo= Organization: Canonical Message-ID: <4ec7af27-1614-534d-f659-b9dfe26e2f38@canonical.com> Date: Tue, 19 May 2020 15:58:31 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <202005191342.97EE972E3@keescook> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: On 5/19/20 2:17 PM, Kees Cook wrote: > On Tue, May 19, 2020 at 01:42:28PM -0500, Eric W. Biederman wrote: >> Kees Cook writes: >> >>> On Tue, May 19, 2020 at 12:41:27PM -0500, Eric W. Biederman wrote: >>>> Kees Cook writes: >>>>> and given the LSM hooks, I think the noexec check is too late as well. >>>>> (This is especially true for the coming O_MAYEXEC series, which will >>>>> absolutely need those tests earlier as well[1] -- the permission checking >>>>> is then in the correct place: during open, not exec.) I think the only >>>>> question is about leaving the redundant checks in fs/exec.c, which I >>>>> think are a cheap way to retain a sense of robustness. >>>> >>>> The trouble is when someone passes through changes one of the permission >>>> checks for whatever reason (misses that they are duplicated in another >>>> location) and things then fail in some very unexpected way. >>> >>> Do you think this series should drop the "late" checks in fs/exec.c? >>> Honestly, the largest motivation for me to move the checks earlier as >>> I've done is so that other things besides execve() can use FMODE_EXEC >>> during open() and receive the same sanity-checking as execve() (i.e the >>> O_MAYEXEC series -- the details are still under discussion but this >>> cleanup will be needed regardless). >> >> I think this series should drop the "late" checks in fs/exec.c It feels >> less error prone, and it feels like that would transform this into >> something Linus would be eager to merge because series becomes a cleanup >> that reduces line count. > > Yeah, that was my initial sense too. I just started to get nervous about > removing the long-standing exec sanity checks. ;) > >> I haven't been inside of open recently enough to remember if the >> location you are putting the check fundamentally makes sense. But the >> O_MAYEXEC bits make a pretty strong case that something of the sort >> needs to happen. > > Right. I *think* it's correct place for now, based on my understanding > of the call graph (which is why I included it in the commit logs). > >> I took a quick look but I can not see clearly where path_noexec >> and the regular file tests should go. >> >> I do see that you have code duplication with faccessat which suggests >> that you haven't put the checks in the right place. > > Yeah, I have notes on the similar call sites (which I concluded, perhaps > wrongly) to ignore: > > do_faccessat() > user_path_at(dfd, filename, lookup_flags, &path); > if (acc_mode & MAY_EXEC .... path_noexec() > inode_permission(inode, mode | MAY_ACCESS); > > This appears to be strictly advisory, and the path_noexec() test is > there to, perhaps, avoid surprises when doing access() then fexecve()? > I would note, however, that that path-based LSMs appear to have no hook > in this call graph at all. I was expecting a call like: > > security_file_permission(..., mode | MAY_ACCESS) > > but I couldn't find one (or anything like it), so only > inode_permission() is being tested (which means also the existing > execve() late tests are missed, and the newly added S_ISREG() test from > do_dentry_open() is missed). > > sadly correct, its something that we intend to fix but haven't gotten to it > prctl_set_mm_exe_file() > err = -EACCESS; > if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path)) > goto exit; > err = inode_permission(inode, MAY_EXEC); > > This is similar (no path-based LSM hooks present, only inode_permission() > used for permission checking), but it is at least gated by CAP_SYS_ADMIN. > dito here > > And this bring me to a related question from my review: does > dentry_open() intentionally bypass security_inode_permission()? I.e. it > calls vfs_open() not do_open(): > > openat2(dfd, char * filename, open_how) > build_open_flags(open_how, open_flags) > do_filp_open(dfd, filename, open_flags) > path_openat(nameidata, open_flags, flags) > file = alloc_empty_file(open_flags, current_cred()); > do_open(nameidata, file, open_flags) > may_open(path, acc_mode, open_flag) > inode_permission(inode, MAY_OPEN | acc_mode) > security_inode_permission(inode, acc_mode) > vfs_open(path, file) > do_dentry_open(file, path->dentry->d_inode, open) > if (unlikely(f->f_flags & FMODE_EXEC && !S_ISREG(inode->i_mode))) ... > security_file_open(f) > /* path-based LSMs check for open here > * and use FMODE_* flags to determine how a file > * is being opened. */ > open() > > vs > > dentry_open(path, flags, cred) > f = alloc_empty_file(flags, cred); > vfs_open(path, f); > > I would expect dentry_open() to mostly duplicate a bunch of > path_openat(), but it lacks the may_open() call, etc. > > I really got the feeling that there was some new conceptual split needed > inside do_open() where the nameidata details have been finished, after > we've gained the "file" information, but before we've lost the "path" > information. For example, may_open(path, ...) has no sense of "file", > though it does do the inode_permission() call. > yes that would be nice, sadly the path hooks are really a bolted on after thought > Note also that may_open() is used in do_tmpfile() too, and has a comment > implying it needs to be checking only a subset of the path details. So > I'm not sure how to split things up. > /me neither anymore > So, that's why I put the new checks just before the may_open() call in > do_open(): it's the most central, positions itself correctly for dealing > with O_MAYEXEC, and doesn't appear to make any existing paths worse. > >> I am wondering if we need something distinct to request the type of the >> file being opened versus execute permissions. > > Well, this is why I wanted to centralize it -- the knowledge of how a > file is going to be used needs to be tested both by the core VFS > (S_ISREG, path_noexec) and the LSMs. Things were inconsistent before. > yep >> All I know is being careful and putting the tests in a good logical >> place makes the code more maintainable, whereas not being careful >> results in all kinds of sharp corners that might be exploitable. >> So I think it is worth digging in and figuring out where those checks >> should live. Especially so that code like faccessat does not need >> to duplicate them. > > I think this is the right place with respect to execve(), though I think > there are other cases that could use to be improved (or at least made > more consistent). > > -Kees >