Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp800513imm; Mon, 21 May 2018 14:43:39 -0700 (PDT) X-Google-Smtp-Source: AB8JxZphxCLq+Ma81B5cnix1oKtXGmrn+tIUJwDRlG8VQzOv7MXo2RIeYpcyFkwvIpx23hwyJMzK X-Received: by 2002:a17:902:6b47:: with SMTP id g7-v6mr22060867plt.251.1526939019005; Mon, 21 May 2018 14:43:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526939018; cv=none; d=google.com; s=arc-20160816; b=VBcXlz7FN2CIRdKpmRgKdzy6rsHlYzOmSTZ2FGtyK9DcaneQiSkmyVlBQ4YjB9T7VG gyvXPYcUd+1aoS32+0s2bRZdfWKblNo4VeioktFBy+H43NOT1ON6vTkIsYAYyym2pdB8 MYX2bO5XRuNow+ttOrca+ToWcIknPjDbU47r7ol8i/AQZBpg7zBM+oroi1lo2ci0nygi j2yXYFLLFc1SaXbSFhkPCDD4IOCZJnZLI6sf725rsrMcHKed0bztpNQtTGTqbm5qW9cX AIxmltgCEIntQAMcKfRci0D0HsWm+fyESmUqIE3Bx1qlp9TK9qUMA56cf4C0Zj/QlOdy epRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=7zAVnflMppk3faQV0qSoTFiI3SVYf0ekb+jwaIafOFU=; b=hZiIZScN41BFI57dIvecPr6e2jYUU78QpumoLvVQuDMqRUsWcoHsQUXiRA405adWzH xUpWbbzV4NILtqwT9mF0irrN0F/3VjEUgOX3sNiGioOsrLbbh5XXNFs3P0JTBS84pB+f mz5IkmciXZ7wYh7DLXmES6frJF9IMQ5AVZcSToZNtVXfarIMUd3LYCuvXFw7PLdMkOrq WC8nLktvCZN+WNyoAAMhiqlDTOhhgGUOWvh3Qn/kfTOcDaJRw+n0tcvpjxx2RAVAg7sg u+fxLoH/IkHRysJpnOAOYjuyEcXHbchsZXEgcyyxk47K7B3BggxChma5QUAS1PEHgEkX rUag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FC2E/Bgf; 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 u6-v6si15450320plm.99.2018.05.21.14.43.24; Mon, 21 May 2018 14:43:38 -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=@kernel.org header.s=default header.b=FC2E/Bgf; 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 S1754646AbeEUVnR (ORCPT + 99 others); Mon, 21 May 2018 17:43:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:39170 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932248AbeEUVYP (ORCPT ); Mon, 21 May 2018 17:24:15 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A30E82075C; Mon, 21 May 2018 21:24:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526937855; bh=wIfinqddn9l8hmk5i/Aid1osLpng3OONd1cBMSy0mz8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FC2E/BgfQEGxIIXoaNihi9po6tXUKnVOOvAH5tlasAo0Hz02NV04fh0cAD0xHidmC 1qxeVT+TlE1MijuqKgOM8rt/2wECKdYY7F8mepd2W+WsMzSo6z9zrQqWN/jcSZOpyN 79szwjERK8rVZT5IUVvMwJNdRHMweMr44HOfUFdI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexey Izbyshev , Dmitry Safonov , Thomas Gleixner , Cyrill Gorcunov , Borislav Petkov , Alexander Monakov , Dmitry Safonov <0x7f454c46@gmail.com>, linux-mm@kvack.org, Andy Lutomirski , "H. Peter Anvin" , "Kirill A. Shutemov" Subject: [PATCH 4.16 050/110] x86/mm: Drop TS_COMPAT on 64-bit exec() syscall Date: Mon, 21 May 2018 23:11:47 +0200 Message-Id: <20180521210509.418263210@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180521210503.823249477@linuxfoundation.org> References: <20180521210503.823249477@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 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 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dmitry Safonov commit acf46020012ccbca1172e9c7aeab399c950d9212 upstream. The x86 mmap() code selects the mmap base for an allocation depending on the bitness of the syscall. For 64bit sycalls it select mm->mmap_base and for 32bit mm->mmap_compat_base. exec() calls mmap() which in turn uses in_compat_syscall() to check whether the mapping is for a 32bit or a 64bit task. The decision is made on the following criteria: ia32 child->thread.status & TS_COMPAT x32 child->pt_regs.orig_ax & __X32_SYSCALL_BIT ia64 !ia32 && !x32 __set_personality_x32() was dropping TS_COMPAT flag, but set_personality_64bit() has kept compat syscall flag making in_compat_syscall() return true during the first exec() syscall. Which in result has user-visible effects, mentioned by Alexey: 1) It breaks ASAN $ gcc -fsanitize=address wrap.c -o wrap-asan $ ./wrap32 ./wrap-asan true ==1217==Shadow memory range interleaves with an existing memory mapping. ASan cannot proceed correctly. ABORTING. ==1217==ASan shadow was supposed to be located in the [0x00007fff7000-0x10007fff7fff] range. ==1217==Process memory map follows: 0x000000400000-0x000000401000 /home/izbyshev/test/gcc/asan-exec-from-32bit/wrap-asan 0x000000600000-0x000000601000 /home/izbyshev/test/gcc/asan-exec-from-32bit/wrap-asan 0x000000601000-0x000000602000 /home/izbyshev/test/gcc/asan-exec-from-32bit/wrap-asan 0x0000f7dbd000-0x0000f7de2000 /lib64/ld-2.27.so 0x0000f7fe2000-0x0000f7fe3000 /lib64/ld-2.27.so 0x0000f7fe3000-0x0000f7fe4000 /lib64/ld-2.27.so 0x0000f7fe4000-0x0000f7fe5000 0x7fed9abff000-0x7fed9af54000 0x7fed9af54000-0x7fed9af6b000 /lib64/libgcc_s.so.1 [snip] 2) It doesn't seem to be great for security if an attacker always knows that ld.so is going to be mapped into the first 4GB in this case (the same thing happens for PIEs as well). The testcase: $ cat wrap.c int main(int argc, char *argv[]) { execvp(argv[1], &argv[1]); return 127; } $ gcc wrap.c -o wrap $ LD_SHOW_AUXV=1 ./wrap ./wrap true |& grep AT_BASE AT_BASE: 0x7f63b8309000 AT_BASE: 0x7faec143c000 AT_BASE: 0x7fbdb25fa000 $ gcc -m32 wrap.c -o wrap32 $ LD_SHOW_AUXV=1 ./wrap32 ./wrap true |& grep AT_BASE AT_BASE: 0xf7eff000 AT_BASE: 0xf7cee000 AT_BASE: 0x7f8b9774e000 Fixes: 1b028f784e8c ("x86/mm: Introduce mmap_compat_base() for 32-bit mmap()") Fixes: ada26481dfe6 ("x86/mm: Make in_compat_syscall() work during exec") Reported-by: Alexey Izbyshev Bisected-by: Alexander Monakov Investigated-by: Andy Lutomirski Signed-off-by: Dmitry Safonov Signed-off-by: Thomas Gleixner Reviewed-by: Cyrill Gorcunov Cc: Borislav Petkov Cc: Alexander Monakov Cc: Dmitry Safonov <0x7f454c46@gmail.com> Cc: stable@vger.kernel.org Cc: linux-mm@kvack.org Cc: Andy Lutomirski Cc: "H. Peter Anvin" Cc: Cyrill Gorcunov Cc: "Kirill A. Shutemov" Link: https://lkml.kernel.org/r/20180517233510.24996-1-dima@arista.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/process_64.c | 1 + 1 file changed, 1 insertion(+) --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -528,6 +528,7 @@ void set_personality_64bit(void) clear_thread_flag(TIF_X32); /* Pretend that this comes from a 64bit execve */ task_pt_regs(current)->orig_ax = __NR_execve; + current_thread_info()->status &= ~TS_COMPAT; /* Ensure the corresponding mm is not marked. */ if (current->mm)