Received: by 10.223.176.5 with SMTP id f5csp1392943wra; Wed, 7 Feb 2018 18:58:14 -0800 (PST) X-Google-Smtp-Source: AH8x225BjyoG+qxV+lsl1XnzMACG785MHb6BaZOXO5p6cgg6/XDg8R4DcE/iJOVfJuLlho3yfGgh X-Received: by 2002:a17:902:102:: with SMTP id 2-v6mr8142732plb.178.1518058694619; Wed, 07 Feb 2018 18:58:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518058694; cv=none; d=google.com; s=arc-20160816; b=PnblcmVlI+MMfoNPKIA34jKBnvpzy3UeZrfD7SQa9RmGADubXvo1IW6GuFhJohYDrj kbp7lKcszvonNMDD88mpYFjS8JtNqd8H7ZIjAh3qq9Kx/ixVJ1vjmV/zf6SNUQ6oHxta EqNBRQbbHcQmKr9Z17H26WfGVQU0U/z+shVkIBinalCemx3n3BZm5YG4muHVwuVfLVIY WR1bblcEVvlXgB1WhFPYj9+zwN3TU2j8pr1wi+jdAOXtDDXIJ4XQaaGhYc+wO9eqL2de 0uy3Py+4v25GE6Xekmhw213sdVlHUMmkIF6S8yLG0skVYyPFbe2Dm8FF5u+hc37r8e3y nGdw== 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 :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=6c2rQEJFCoLaIES6eYxOEpFofJg5C2VycJjlycPKFMk=; b=AyYGD5wXxlfpCu1JzmPz/KZ6ytrqoIdpVZz5mcAl+guXu7X5gTG03JhFpjj58KfZpN OWSCyxeTOQRgOiYZI0j/E3dYONt8rKmEmMav1lHhtokDlwywMkNUE54dqU3fS98D82JQ MuOIz5+/SSkMgFIelJW4KDChcCMdCu/rnH+lR/2XHhsJcoD7MFBXhhz/DDEpbvWvhh3Z e+n0M8WsloVrfN9FJ5GNCTRgjBPKe4amJwG+Mba+tSNHdGr76h/FuNGRqCqPD75Ay0Xu B9pIMU6qZEC1AtoIEKqlV4a9/OMB25FZScnSxyjeg3zu0vsPHN8ef6Fh88WchP457zbe j2Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=BpOgVJDS; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w13si671717pgp.606.2018.02.07.18.57.59; Wed, 07 Feb 2018 18:58:14 -0800 (PST) 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=@google.com header.s=20161025 header.b=BpOgVJDS; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751424AbeBHC4t (ORCPT + 99 others); Wed, 7 Feb 2018 21:56:49 -0500 Received: from mail-ot0-f170.google.com ([74.125.82.170]:33662 "EHLO mail-ot0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750986AbeBHC4s (ORCPT ); Wed, 7 Feb 2018 21:56:48 -0500 Received: by mail-ot0-f170.google.com with SMTP id q9so2864922oti.0 for ; Wed, 07 Feb 2018 18:56:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=6c2rQEJFCoLaIES6eYxOEpFofJg5C2VycJjlycPKFMk=; b=BpOgVJDSIUlowiXmgYEkR5Fjn5Zf9osiTN7we/oMxE837k802YTncdb0rtfu3pL+LN rgolOT+qlGVVcsKtE9OfB1IB71u0ii6zDTxBhmMtaj9LD+m2cDl8Tv3TIJuFB4XJTlCc 2F6cI/CN9Nd4yMVNcoE3PZsm+r/jNbeexkD2HN/F9bfRFVFTIyTa3wfbSNsKm+qle68B 7zvv5hhWZO6P7o2J2KSq9MiO22CGHH1g5XNveQOm6ddYHJLtY8L+WiLNA9Yuhk9xv+Gk tPe4EoRCm3MZCa+U5r6s5NVVIx8Hfz/70Ye5FARSZ3LfTecw+aaQTqeXjANxQClEgOXp hN0w== 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=6c2rQEJFCoLaIES6eYxOEpFofJg5C2VycJjlycPKFMk=; b=kphNP91k0dh8pM6rpL0oXUdOohj1n8pg+21Ev2u96CSh1YMPLNCKQLAgAU5FtEPnyF fbxusfZqYJsIHz65XTCFLWYImPcSfjBlrJF51I3zl2nTO48cQ9Z9Vmh3eZ2RbiVOWowr S+boq/783yljZl3u8CP7Hso02Y5SSv94TrDnDa5sSCcV3vzLw9Gk311ItEQbLtbAeP3T pjbN3bsLD1fT1B7t2JVXWKTEz26it6WNYcVzljVENBPIZgMk1Pay/NojpJje8uz2L7ph p065kC791ydPnhRCPPaGOB4Wg3yLw4vIXvcz3gG3VmvYtTulDOzwYferm0mWTWyGcNXI jvBg== X-Gm-Message-State: APf1xPCy+Db68emmJNQCyldGbqhC6ChILTNS2U3PbakRJBrYmHJ0Nrp6 1rDX+TpKiURFMQvg/PCnw24qTHvRBKwwv8XR+d8PHTfcv2E= X-Received: by 10.157.24.28 with SMTP id b28mr3990719ote.356.1518058607287; Wed, 07 Feb 2018 18:56:47 -0800 (PST) MIME-Version: 1.0 Received: by 10.74.183.132 with HTTP; Wed, 7 Feb 2018 18:56:26 -0800 (PST) In-Reply-To: <20180208021112.GB14918@bombadil.infradead.org> References: <20180208021112.GB14918@bombadil.infradead.org> From: Jann Horn Date: Thu, 8 Feb 2018 03:56:26 +0100 Message-ID: Subject: Re: [RFC] Warn the user when they could overflow mapcount To: Matthew Wilcox Cc: linux-mm@kvack.org, Kernel Hardening , kernel list , "Kirill A. Shutemov" 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 Thu, Feb 8, 2018 at 3:11 AM, Matthew Wilcox wrote: > Kirill and I were talking about trying to overflow page->_mapcount > the other day and realised that the default settings of pid_max and > max_map_count prevent it [1]. But there isn't even documentation to > warn a sysadmin that they've just opened themselves up to the possibility > that they've opened their system up to a sufficiently-determined attacker. > > I'm not sufficiently wise in the ways of the MM to understand exactly > what goes wrong if we do wrap mapcount. Kirill says: > > rmap depends on mapcount to decide when the page is not longer mapped. > If it sees page_mapcount() == 0 due to 32-bit wrap we are screwed; > data corruption, etc. How much memory would you need to trigger this? You need one vm_area_struct per increment, and those are 200 bytes? So at least 800GiB of memory for the vm_area_structs, and maybe more for other data structures? I wouldn't be too surprised if there are more 32-bit overflows that start being realistic once you put something on the order of terabytes of memory into one machine, given that refcount_t is 32 bits wide - for example, the i_count. See https://bugs.chromium.org/p/project-zero/issues/detail?id=809 for an example where, given a sufficiently high RLIMIT_MEMLOCK, it was possible to overflow a 32-bit refcounter on a system with just ~32GiB of free memory (minimum required to store 2^32 64-bit pointers). On systems with RAM on the order of terabytes, it's probably a good idea to turn on refcount hardening to make issues like that non-exploitable for now. > That seems pretty bad. So here's a patch which adds documentation to the > two sysctls that a sysadmin could use to shoot themselves in the foot, > and adds a warning if they change either of them to a dangerous value. I have negative feelings about this patch, mostly because AFAICS: - It documents an issue instead of fixing it. - It likely only addresses a small part of the actual problem. > It's possible to get into a dangerous situation without triggering this > warning (already have the file mapped a lot of times, then lower pid_max, > then raise max_map_count, then map the file a lot more times), but it's > unlikely to happen. > > Comments? > > [1] map_count counts the number of times that a page is mapped to > userspace; max_map_count restricts the number of times a process can > map a page and pid_max restricts the number of processes that can exist. > So map_count can never be larger than pid_max * max_map_count. [...] > +int sysctl_pid_max(struct ctl_table *table, int write, > + void __user *buffer, size_t *lenp, loff_t *ppos) > +{ > + struct ctl_table t; > + int ret; > + > + t = *table; > + t.data = &pid_max; > + t.extra1 = &pid_max_min; > + t.extra2 = &pid_max_max; > + > + ret = proc_douintvec_minmax(&t, write, buffer, lenp, ppos); > + if (ret || !write) > + return ret; > + > + if ((INT_MAX / max_map_count) > pid_max) > + pr_warn("pid_max is dangerously large\n"); This in reordered is "if (pid_max * max_map_count < INT_MAX) pr_warn(...);", no? That doesn't make sense to me. Same thing again further down. [...] > diff --git a/mm/madvise.c b/mm/madvise.c > index 4d3c922ea1a1..5b66a4a48192 100644 > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -147,7 +147,7 @@ static long madvise_behavior(struct vm_area_struct *vma, > *prev = vma; > > if (start != vma->vm_start) { > - if (unlikely(mm->map_count >= sysctl_max_map_count)) { > + if (unlikely(mm->map_count >= max_map_count)) { Why the renaming?