Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp754787ybi; Fri, 21 Jun 2019 07:38:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqxBqGDq94a0DZSYRMa25CfdiQuLofoSpf1HOhGCPIkrvnVBvI7kzSNhgR65yBRrePk7oc44 X-Received: by 2002:a17:902:e2:: with SMTP id a89mr132776858pla.210.1561127907056; Fri, 21 Jun 2019 07:38:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561127907; cv=none; d=google.com; s=arc-20160816; b=E0dJxzAI2evzak4txtDcXZ1+H7A1iSjX9rc1uS7B0b8Rt8tz8IcHvzJKpVknpmuAaH 0Gz9dAaCdzZCCzMa1+rS//GAqMcnNZNOsN2Nm38LzWGd6AwSgShkJnGI/HJ4ArgTuSwv NxoxlWEK2dn0HFNNW5uI4vPLnmSh/FhlKOFV+ojIS0vOAVm+CTBZYMHPZ+GI8PyHK7QE q0WOgBIX3Yl1n86NM23csdgnLe+PjxR/RnnkMBuSk8bkHBZ0ooIal6nMUblZ4KjEhh0c AX28cHNyKUZnI31ozMrJ8t+SwrTTuAgdg9455c6qIQ1Sqc5Nf+uO/Nc/LEYGVT1Fk7XA oRBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=CRsZKa850d7hy2Fg7kcjRq2q649+iIRBsH7xdd7VMHk=; b=UFNUk0djLlMA0AfK5qeoH5my+Mk2xXIsYFEY6EmIMHnZkJrzYowc+ue/QlcH0WjH2h VSBi05erQ2O/nBLKK6igP7ZZeB5Cgd7EtRJ002enljq8nlBY71ylbjyTCpceNiaoHVzm WTt8UsFOEEPLRsO+n3ZZuH0TiYyvzdFeBMT+AcukD0DkWFzSzFYmADN2tznRoSbm6xz9 X2LYHfFLpvI3+JEgT2V7C15Q8c+XrdC+FaqAdLKYafyU6WdGNL4CHVLykEiVNkiPaVXF Be6RjT1F4MoSdhxV7rb1/whYblcMYNLgN4+BnFJvesaQ1ssY43zlsU4zI+uiOR65Hjai 8qww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=krUEz3jU; 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 h5si2747226plr.134.2019.06.21.07.38.11; Fri, 21 Jun 2019 07:38:27 -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=@google.com header.s=20161025 header.b=krUEz3jU; 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 S1726503AbfFUOhY (ORCPT + 99 others); Fri, 21 Jun 2019 10:37:24 -0400 Received: from mail-vk1-f196.google.com ([209.85.221.196]:43160 "EHLO mail-vk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725985AbfFUOhY (ORCPT ); Fri, 21 Jun 2019 10:37:24 -0400 Received: by mail-vk1-f196.google.com with SMTP id m193so1329235vke.10 for ; Fri, 21 Jun 2019 07:37:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=CRsZKa850d7hy2Fg7kcjRq2q649+iIRBsH7xdd7VMHk=; b=krUEz3jU5PDEs/c+y0vBtzgq6AFBae0v8SSlcMMS/hmJNHLzMQL83zvltWuywWMrmp il/scSnj+2Ivb9k/0MUKDwG5H8G94payV/kNpVWZGUwF9HI0qsHAoTTCtNgPSjT1amoW t/xIUvgHCrBLBdHz2IcXtQQkX/ioF7maLpTQHiJYn6nhdefKPoApx1v464zVabU1GZJ1 aImXO+fQ1VTBBSq2STNMAC5PLpwVD39s9jZYcZoEF0CrHSrTXQBU3JsAm7J6CO9KyyrJ O+cq0pdgRfdAvOH7q4sn0X6D7qxonOPLIOAtlI2cQlnDKMtjM8V4LLd65XTNAnaOqus0 Q7JQ== 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:content-transfer-encoding; bh=CRsZKa850d7hy2Fg7kcjRq2q649+iIRBsH7xdd7VMHk=; b=j9GCvvFZgnBD1CmNfgYpq7g5SPjV8ynjwABTVtKy0U76PPyXWsqHpjMmGDP60Zds+T 05yem7FKiIQdJsjcJeelYFNlctETOE8Mejy6cd+T9Vlfj4vJ8YgwWr/7ChFQ1lX8hOgi fcY+91Db7zRo0LVVQYF/QXJAARZVHBvqIci5sqqHzMV+NzHVK3qQ/cp/1XFJiYGkbiGE Fcpu/4V3IaKKKUJcwBfrQD7UfIuHDtc1kvAZe+OZRWOIE3Vj4b/NuclSx4vkvNGqlsk6 5m4ijlzDDXFJDZNo48bT4kB/bk0I7pwxHayIz6TcwnOEqJ8nNnDACOSyPZChjCKLe4Wc orOQ== X-Gm-Message-State: APjAAAW/cRs/tRD54BUeesWEVlmgFDHOSPNR0YnUtRSowmYUo41kNPCl h+FzSSq2OK3b+73LYrnQNNfI28+pnLaYvkNdK02RNg== X-Received: by 2002:a1f:dcc5:: with SMTP id t188mr9877753vkg.29.1561127842845; Fri, 21 Jun 2019 07:37:22 -0700 (PDT) MIME-Version: 1.0 References: <1561063566-16335-1-git-send-email-cai@lca.pw> <201906201801.9CFC9225@keescook> <1561119983.5154.33.camel@lca.pw> In-Reply-To: <1561119983.5154.33.camel@lca.pw> From: Alexander Potapenko Date: Fri, 21 Jun 2019 16:37:10 +0200 Message-ID: Subject: Re: [PATCH -next v2] mm/page_alloc: fix a false memory corruption To: Qian Cai Cc: Kees Cook , Andrew Morton , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 21, 2019 at 2:26 PM Qian Cai wrote: > > On Fri, 2019-06-21 at 12:39 +0200, Alexander Potapenko wrote: > > On Fri, Jun 21, 2019 at 3:01 AM Kees Cook wrote= : > > > > > > On Thu, Jun 20, 2019 at 04:46:06PM -0400, Qian Cai wrote: > > > > The linux-next commit "mm: security: introduce init_on_alloc=3D1 an= d > > > > init_on_free=3D1 boot options" [1] introduced a false positive when > > > > init_on_free=3D1 and page_poison=3Don, due to the page_poison expec= ts the > > > > pattern 0xaa when allocating pages which were overwritten by > > > > init_on_free=3D1 with 0. > > > > > > > > Fix it by switching the order between kernel_init_free_pages() and > > > > kernel_poison_pages() in free_pages_prepare(). > > > > > > Cool; this seems like the right approach. Alexander, what do you thin= k? > > > > Can using init_on_free together with page_poison bring any value at all= ? > > Isn't it better to decide at boot time which of the two features we're > > going to enable? > > I think the typical use case is people are using init_on_free=3D1, and th= en decide > to debug something by enabling page_poison=3Don. Definitely, don't want > init_on_free=3D1 to disable page_poison as the later has additional check= ing in > the allocation time to make sure that poison pattern set in the free time= is > still there. In addition to information lifetime reduction the idea of init_on_free is to ensure the newly allocated objects have predictable contents. Therefore it's handy (although not strictly necessary) to keep them zero-initialized regardless of other boot-time flags. Right now free_pages_prezeroed() relies on that, though this can be changed= . On the other hand, since page_poison already initializes freed memory, we can probably make want_init_on_free() return false in that case to avoid extra initialization. Side note: if we make it possible to switch betwen 0x00 and 0xAA in init_on_free mode, we can merge it with page_poison, performing the initialization depending on a boot-time flag and doing heavyweight checks under a separate config. > > > > > Reviewed-by: Kees Cook > > > > > > -Kees > > > > > > > > > > > [1] https://patchwork.kernel.org/patch/10999465/ > > > > > > > > Signed-off-by: Qian Cai > > > > --- > > > > > > > > v2: After further debugging, the issue after switching order is lik= ely a > > > > separate issue as clear_page() should not cause issues with fut= ure > > > > accesses. > > > > > > > > mm/page_alloc.c | 3 ++- > > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > > > > index 54dacf35d200..32bbd30c5f85 100644 > > > > --- a/mm/page_alloc.c > > > > +++ b/mm/page_alloc.c > > > > @@ -1172,9 +1172,10 @@ static __always_inline bool > > > > free_pages_prepare(struct page *page, > > > > PAGE_SIZE << order); > > > > } > > > > arch_free_page(page, order); > > > > - kernel_poison_pages(page, 1 << order, 0); > > > > if (want_init_on_free()) > > > > kernel_init_free_pages(page, 1 << order); > > > > + > > > > + kernel_poison_pages(page, 1 << order, 0); > > > > if (debug_pagealloc_enabled()) > > > > kernel_map_pages(page, 1 << order, 0); > > > > > > > > -- > > > > 1.8.3.1 > > > > > > > > > > -- > > > Kees Cook > > > > > > --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg