Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1397440ybk; Thu, 14 May 2020 08:02:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxi+xTvKNDACIMWqN8CuFCGU4s+Y0y5p4FHj0dRI7VHE4HLljRwRpRn67WDFMhmyhylo3J4 X-Received: by 2002:a05:6402:21cc:: with SMTP id bi12mr4358969edb.294.1589468578805; Thu, 14 May 2020 08:02:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589468578; cv=none; d=google.com; s=arc-20160816; b=0cRcGJCTvgU92ZKxOIABkpJ7kFcVRPM8Z5whf8BrusaWllLCCR3U6GqWXt/64K+3yQ lIggATzXxoj4xYQ8n0VrQjwbK/96gaqOd+8KgE036C9zRSH43k2SWKVsk6SB+oEmVNVf CLT3Hy1SswRx8F7SxKGyaM08pSZNW6j9asjjsvWsNx/PCZ3LOyy2E2VAQId3puOE84LS Ydz4WRN4MgM7Hq8IxYqYiKazUTBUw3h67ExhNSPonLidJo2VaA28PI3DzSzgN06cC+Lt AC7dIsNr/s3VltSPmZdiwU5+h3cLLXB6Y92rPmjrdesPWJH//637pikMY3VBuN7QoKDq uJgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:mime-version:user-agent :message-id:in-reply-to:date:references:cc:to:from; bh=LKxx5CRmh0wSbVNTCLuzJTTBSq0yB6z4Pyck95zYF1k=; b=cgfzUB+8KDEN23MBuYI6wvEWl2pXVzkP3CGvI6GmtS5G0hOSLJjNiiG+AQpPAmLTKr uYFuC9Ny97/G7tIlNxQkQ8t8X/Nbw1fx5GFxNZaMHCoxDsl3le/5SY2W+kW0VC7zU58D VWc2NuvmIUqERD4o+/GkK4GfUCdYK4miG22UIPcMrU++WhBnf5ibcR1QRquH+svwiWTB LsIFiHVWJX9sLpF2kkpHfKiirbB7D09zguuv30eai3/xCtRdEeeco7Oz/SF9keGTXWUz T/XNGQ1xB97WM0hlEnN5C7idflFaJtPQ7GxgR2Fli/PtGWrQb220sFre6UfE4GzAtFn7 na0g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gr5si1877060ejb.509.2020.05.14.08.02.33; Thu, 14 May 2020 08:02:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728050AbgENPA3 (ORCPT + 99 others); Thu, 14 May 2020 11:00:29 -0400 Received: from out02.mta.xmission.com ([166.70.13.232]:60264 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727991AbgENPA1 (ORCPT ); Thu, 14 May 2020 11:00:27 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out02.mta.xmission.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jZFLM-0008Eb-OC; Thu, 14 May 2020 09:00:16 -0600 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95] helo=x220.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.87) (envelope-from ) id 1jZFLI-0002l2-0S; Thu, 14 May 2020 09:00:16 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: Kees Cook Cc: Linus Torvalds , Tetsuo Handa , Linux Kernel Mailing List , Oleg Nesterov , Jann Horn , Greg Ungerer , Rob Landley , Bernd Edlinger , linux-fsdevel , Al Viro , Alexey Dobriyan , Andrew Morton , Casey Schaufler , LSM List , James Morris , "Serge E. Hallyn" , Andy Lutomirski References: <87eerszyim.fsf_-_@x220.int.ebiederm.org> <87sgg6v8we.fsf@x220.int.ebiederm.org> <202005111428.B094E3B76A@keescook> <874kslq9jm.fsf@x220.int.ebiederm.org> <202005121218.ED0B728DA@keescook> <87lflwq4hu.fsf@x220.int.ebiederm.org> <202005121606.5575978B@keescook> <202005121625.20B35A3@keescook> <202005121649.4ED677068@keescook> Date: Thu, 14 May 2020 09:56:36 -0500 In-Reply-To: <202005121649.4ED677068@keescook> (Kees Cook's message of "Tue, 12 May 2020 16:51:29 -0700") Message-ID: <87sgg2ftuj.fsf@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1jZFLI-0002l2-0S;;;mid=<87sgg2ftuj.fsf@x220.int.ebiederm.org>;;;hst=in02.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX184UonXunbqRpJTjGc7cdgy0PX4+YlZfjw= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on sa01.xmission.com X-Spam-Level: ** X-Spam-Status: No, score=2.0 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,T_TM2_M_HEADER_IN_MSG,T_TooManySym_01,XMNoVowels, XMSubLong autolearn=disabled version=3.4.2 X-Spam-Virus: No X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.4995] * 1.5 XMNoVowels Alpha-numberic number with no vowels * 0.7 XMSubLong Long Subject * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa01 0; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: ; sa01 0; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: **;Kees Cook X-Spam-Relay-Country: X-Spam-Timing: total 4318 ms - load_scoreonly_sql: 0.03 (0.0%), signal_user_changed: 3.6 (0.1%), b_tie_ro: 2.5 (0.1%), parse: 0.72 (0.0%), extract_message_metadata: 10 (0.2%), get_uri_detail_list: 1.37 (0.0%), tests_pri_-1000: 4.5 (0.1%), tests_pri_-950: 1.02 (0.0%), tests_pri_-900: 0.87 (0.0%), tests_pri_-90: 262 (6.1%), check_bayes: 254 (5.9%), b_tokenize: 7 (0.2%), b_tok_get_all: 9 (0.2%), b_comp_prob: 2.1 (0.0%), b_tok_touch_all: 233 (5.4%), b_finish: 0.71 (0.0%), tests_pri_0: 296 (6.8%), check_dkim_signature: 0.39 (0.0%), check_dkim_adsp: 3.1 (0.1%), poll_dns_idle: 3729 (86.4%), tests_pri_10: 1.79 (0.0%), tests_pri_500: 3736 (86.5%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH 3/5] exec: Remove recursion from search_binary_handler X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Kees Cook writes: > On Tue, May 12, 2020 at 04:47:14PM -0700, Kees Cook wrote: >> And now I wonder if qemu actually uses the resulting AT_EXECFD ... > > It does, though I'm not sure if this is to support crossing mount points, > dropping privileges, or something else, since it does fall back to just > trying to open the file. > > execfd = qemu_getauxval(AT_EXECFD); > if (execfd == 0) { > execfd = open(filename, O_RDONLY); > if (execfd < 0) { > printf("Error while loading %s: %s\n", filename, strerror(errno)); > _exit(EXIT_FAILURE); > } > } My hunch is that the fallback exists from a time when the kernel did not implement AT_EXECFD, or so that qemu can run on kernels that don't implement AT_EXECFD. It doesn't really matter unless the executable is suid, or otherwise changes privileges. I looked into this a bit to remind myself why exec works the way it works, with changing privileges. The classic attack is pointing a symlink at a #! script that is suid or otherwise changes privileges. The kernel will open the script and set the privileges, read the interpreter from the first line, and proceed to exec the interpreter. The interpreter will then open the script using the pathname supplied by the kernel. The name of the symlink. Before the interpreter reopens the script the attack would replace the symlink with a script that does something else, but gets to run with the privileges of the script. Defending against that time of check vs time of use attack is why bprm_fill_uid, and cap_bprm_set_creds use the credentials derived from the interpreter instead of the credentials derived from the script. The other defense is to replace the pathname of the executable that the intepreter will open with /dev/fd/N. All of this predates Linux entirely. I do remember this was fixed at some point in Linux but I don't remember the details. I can just read the solution that was picked in the code. All of this makes me wonder how are the LSMs protected against this attack. Let's see the following LSMS implement brpm_set_creds: tomoyo - Abuses bprm_set_creds to call tomoyo_load_policy [ safe ] smack - Requires CAP_MAC_ADMIN to smack setxattrs [ vulnerable? ] Uses those xattrs in smack_bprm_set_creds apparmor - Everything is based on names so the symlink [ safe? ] attack won't work as it has the wrong name. As long as the trusted names can't be renamed apparmor appears good. selinux - Appears to let anyone set selinux xattrs [ safe? ] Requires permission for a sid transfer As the attack appears not to allow anything that would not be allowed anyway it looks like selinux is safe. LSM folks, especially Casey am I reading this correctly? Did I correctly infer how your LSMs deal with the time of check to time of use attack on the script name? Eric