Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp569302ybh; Sun, 12 Jul 2020 16:06:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywLgcxNsCdM/A/fZ00PDJMte16g49i0y/k5D7IVIsyZSL011zSPiQK+49xHsmP3uEF0Plo X-Received: by 2002:a17:907:94ca:: with SMTP id dn10mr69438892ejc.348.1594595196719; Sun, 12 Jul 2020 16:06:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594595196; cv=none; d=google.com; s=arc-20160816; b=p8/nlC5D/l18EwYMyRm/IfxAuC43JtRaviS0XmMw8JOJerdbP+iH3g+C8Dvp8F2CJg J12CYDsns+FnBZ5zHPVPiqI324GxDlzuRnDk5lFKMFPUfJVelWLngbKQqh4IsBMijCrQ fUdszinnQrEZUD/2877Qea3rjLfaFkOpZPYyJEACh//SWbpx2DDaoLZ5kBWIvUvaFw9+ h/U8sW32ACvNECGkqHOZNjRloNhfHnyxe3XZMLah3QXvrbqgP9Z4MGa+Q+6knJThIK6e t9uP3HzD57LHYU72kjvluuZoXYScNoT5MmvH77F7MgQt1Kv06VNTFaMZhFkzb7Moxm8E aklA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=yo0Mnk4z2ZKKeclS9nj/V3zBsAaD+8xk/39wn8faHH8=; b=B723E9sukVrEpQONsb+CTvwKjgMys34AZQa/inxfcAxGVerSjfDFryGTAojSSjyj4v aKmFP6B7Ywf9x7I1bIbw4XihJk1zFiq4loy9nxg8TztxrlXtDoMfE7r+mZyO3TCY/Een CcXF03ATwYWVWeUD8Ze8if0e7M78y0zI3XlAw/zJb6oBQUt6bzcvr16KxVQrL+5T0SWY KKeVsz0Qp0jS1fJOe1nGjYduevcK5Zr8+RP2lVdZ+UHYE3IjvXuk68ICkTGqKSG2snMN /zrnO9+6/p3TZyp4L5S106XMA347hnxbyQmJWvjei7EadP+kVyx8LwYKHbb/kl+pf/uF m0TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=JgbUYhmW; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cm12si8225942edb.333.2020.07.12.16.05.46; Sun, 12 Jul 2020 16:06:36 -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; dkim=pass header.i=@linux-foundation.org header.s=google header.b=JgbUYhmW; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728329AbgGLXEs (ORCPT + 99 others); Sun, 12 Jul 2020 19:04:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727906AbgGLXEr (ORCPT ); Sun, 12 Jul 2020 19:04:47 -0400 Received: from mail-ej1-x642.google.com (mail-ej1-x642.google.com [IPv6:2a00:1450:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CF62C061794 for ; Sun, 12 Jul 2020 16:04:47 -0700 (PDT) Received: by mail-ej1-x642.google.com with SMTP id dp18so13280439ejc.8 for ; Sun, 12 Jul 2020 16:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yo0Mnk4z2ZKKeclS9nj/V3zBsAaD+8xk/39wn8faHH8=; b=JgbUYhmWzr+3//UqU1Ua64whjD+TQo14a7RmhB8c633RX/QOiKcCQbsyY/hFuqiEZ5 BvLqBjYYHepAIfQNocZKo+mB3oyrODeGszIw3KMFLfUKLJ4N1S+uGMefleAq+uA7OUg5 DLKYfe4ZuClUyfP06Vewwx33e/+3F2kX7EtpE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yo0Mnk4z2ZKKeclS9nj/V3zBsAaD+8xk/39wn8faHH8=; b=ee1ZJYydwWugLL9BB3kJA6Y0ANfu2UkH4pAsoIo7a+mgqt4a1QYs9D3fjXtYk+IG6i AlNOAA+f4bGOYk3A2nr1OpQAkROgy6tquZsRgWSrIp0MHfTHBm+B3MUZ4vp9GKFOPPWq JXwW7wyFOm37hkb1OlwAoTFJyYBd+n1kq1T5mfSfsdGLf0jE2JfxdQ6F7ZhD2YDOEGBw DKvtA+DKtLTmSZ+jlTk8C5EMTkB5HHmpcd17kW1NNhHVR8sax9DLRdJtgbNIJ+9HzZe7 g8MNPPNqN4yZ7iy1plaMLXvmz5ycOKq/me0ptGEK/9LulAAYBOaL/Rv2YzDgpnEU/LrV cm/g== X-Gm-Message-State: AOAM530+ummqK3lL/lPJlT4RwNeGFcBeV8jLCUUvrBHw+Zqs+hoBMLu2 a/L/xwwWXnPw2ivORfF4GAGolFHQZag= X-Received: by 2002:a17:906:6b0c:: with SMTP id q12mr70965968ejr.525.1594595085507; Sun, 12 Jul 2020 16:04:45 -0700 (PDT) Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com. [209.85.208.54]) by smtp.gmail.com with ESMTPSA id u8sm8337336ejm.65.2020.07.12.16.04.45 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 12 Jul 2020 16:04:45 -0700 (PDT) Received: by mail-ed1-f54.google.com with SMTP id dg28so10358562edb.3 for ; Sun, 12 Jul 2020 16:04:45 -0700 (PDT) X-Received: by 2002:a2e:9c92:: with SMTP id x18mr40507440lji.70.1594594702310; Sun, 12 Jul 2020 15:58:22 -0700 (PDT) MIME-Version: 1.0 References: <20200712215041.GA3644504@google.com> In-Reply-To: <20200712215041.GA3644504@google.com> From: Linus Torvalds Date: Sun, 12 Jul 2020 15:58:06 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: WARNING: at mm/mremap.c:211 move_page_tables in i386 To: Joel Fernandes Cc: Naresh Kamboju , linux- stable , open list , linux-mm , Arnd Bergmann , Andrew Morton , Roman Gushchin , Michal Hocko , lkft-triage@lists.linaro.org, Chris Down , Michel Lespinasse , Fan Yang , Brian Geffon , Anshuman Khandual , Will Deacon , Catalin Marinas , pugaowei@gmail.com, Jerome Glisse , Greg Kroah-Hartman , Mel Gorman , Hugh Dickins , Al Viro , Tejun Heo , Sasha Levin 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 On Sun, Jul 12, 2020 at 2:50 PM Joel Fernandes wrote: > > I reproduced Naresh's issue on a 32-bit x86 machine and the below patch fixes it. > The issue is solely within execve() itself and the way it allocates/copies the > temporary stack. > > It is actually indeed an overlapping case because the length of the > stack is big enough to cause overlap. The VMA grows quite a bit because of > all the page faults that happen due to the copy of the args/env. Then during > the move of overlapped region, it finds that a PMD is already allocated. Oh, ok, I think I see. So the issue is that while move_normal_pmd() _itself_ will be clearing the old pmd entries when it copies them, the 4kB copies that happened _before_ this time will not have cleared the old pmd that they were in. So we've had a deeper stack, and we've already copied some of it one page at a time, and we're done with those 4kB entries, but now we hit a 2MB-aligned case and want to move that down. But when it does that, it hits the (by now hopefully empty) pmd that used to contain the 4kB entries we copied earlier. So we've cleared the page table, but we've simply never called pgtable_clear() here, because the page table got cleared in move_ptes() when it did that pte = ptep_get_and_clear(mm, old_addr, old_pte); on the previous old pte entries. That makes sense to me. Does that match what you see? Because when I said it wasn't overlapped, I was really talking about that one single pmd move itself not being overlapped. > The below patch fixes it and is not warning anymore in 30 minutes of testing > so far. So I'm not hugely happy with the patch, I have to admit. Why? Because: > + /* Ensure the temporary stack is shifted by atleast its size */ > + if (stack_shift < (vma->vm_end - vma->vm_start)) > + stack_shift = (vma->vm_end - vma->vm_start); This basically overrides the random stack shift done by arch_align_stack(). Yeah, yeah, arch_align_stack() is kind of misnamed. It _used_ to do what the name implies it does, which on x86 is to just give the minimum ABI-specified 16-byte stack alignment. But these days, what it really does is say "align the stack pointer, but also shift it down by a random amount within 8kB so that the start of the stack isn't some repeatable thing that an attacker can trivially control with the right argv/envp size.." I don't think it works right anyway because we then PAGE_ALIGN() it, but whatever. But it looks like what you're doing means that now the size of the stack will override that shift, and that means that now the randomization is entirely undone. No? Plus you do it after we've also checked that we haven't grown the stack down to below mmap_min_addr(). But maybe I misread that patch. But I do feel like you figured out why the bug happened, now we're just discussing whether the patch is the right thing to do. Maybe saying "doing the pmd copies for the initial stack isn't important, so let's just note this as a special case and get rid of the WARN_ON()" might be an alternative solution. The reason I worried was that I felt like we didn't understand why the WARN_ON() happened. Now that I do, I think we could just say "ok, don't warn, we know that this can happen, and it's harmless". Anybody else have any opinions? Linus