Received: by 10.192.165.148 with SMTP id m20csp1186681imm; Wed, 2 May 2018 16:02:52 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrRkj8tdW5InBESxvaVb06QAVLggE/I519eNLrghZpypPxyDkLSvCRe7F6iwogugb2S5gGm X-Received: by 2002:a63:3c0c:: with SMTP id j12-v6mr18121110pga.203.1525302172062; Wed, 02 May 2018 16:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525302172; cv=none; d=google.com; s=arc-20160816; b=yZh8nLTW9nCBoDg9+tmoNxWDNKRWRd+i6WCrRF0DwZuEhzHAwwc6HAR1xIQxsNajRb taSqB510wTB6NVjj0YGzWM6Six0nMD/H2ySKPxEuUslIWifRCIKuJDqko1enN6R2jrNy 4hBPZ+qiYulQLBDdKldDhH3osuQgo8TUBViLZtMrI63zVQl8DXVbc4+v0ixoboMglBRt iu7Iii0/zmkF53pGzSgSQp2TJXNxQsGrwFE7xT+5jjYpFDY1R6sd4Rj1jqiOVVNud1P6 DGIzF0WxdLJ52oQRXrQ65npy5M7MshqP0gA8SaPmFNE9sAOGJrDmYsLZokps1msF+Co1 kFug== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=lhJJvNIjDLAo2Od0d61KaSRARTumrADZ/MehhoqVYBk=; b=NF2C7zAgL1gBDf35EAmUNiLTA+Zs3K8Giim/GFpEak2ioasDU/WBPnVEfzGWoCW9Nz Y29Hsc+DPLwglQLtye0x03QhKE/+1uicn4BF+Lyv2Clnzn8/kNYcBpsbKwrAnj9atuJw VN01fIyz0H4CC2WkHDwnQmTORgNlqG+cIN9rVlcC0PV5O0vncdsXE0a5QV4hGfkkuzPn iSddeCMGSboID9+uYBV3el/7xJZIgYHsiOZFPSMT/CDIelhkLJOPGV6QH8CYzS9eYMIo Shl7AyjFq4Zh9f/wMTrzu+iPsjnO/S73EYxjfzj49zCUYlcPoqC7oDA3ojwUViyOSlCE CIAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=FkcatyqE; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 69-v6si6100812pgc.64.2018.05.02.16.02.36; Wed, 02 May 2018 16:02:52 -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=@gmail.com header.s=20161025 header.b=FkcatyqE; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751645AbeEBXC0 (ORCPT + 99 others); Wed, 2 May 2018 19:02:26 -0400 Received: from mail-lf0-f44.google.com ([209.85.215.44]:33447 "EHLO mail-lf0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751148AbeEBXCX (ORCPT ); Wed, 2 May 2018 19:02:23 -0400 Received: by mail-lf0-f44.google.com with SMTP id m18-v6so23245048lfb.0; Wed, 02 May 2018 16:02:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=lhJJvNIjDLAo2Od0d61KaSRARTumrADZ/MehhoqVYBk=; b=FkcatyqEGjIL4ZJPG9Xl1lxd1N/b0plvunDB1zZQEOxEyMVoGyBIeHK/osdlNDt4+R sEpdajFGuO6ozHDbjRUUASkM9qqg0CYfXTgOys1Djj6nx1rBDRWJaDZg137TS4XvLKLD jpReenJHsPaOiCUZlsO9cxHceX19X5P5w1zgWpByiZAQvXS0wyiMC8TgNHFIQEOuZB6o dBkYXyUpOlCwW2XDWmQ/ynbrYaS05hKI4yiQ1F8KeDInUORQUhGVU63VJuW9iNfHsuoc De+fRCQMWwCGptEwW6ZZbLFtCHMl5MnxD9BKG/5V+8629wzMfAF88L53hwvrt7JVPALY D0WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=lhJJvNIjDLAo2Od0d61KaSRARTumrADZ/MehhoqVYBk=; b=U/BzPb3+x+1iqttuEqvJMSOmS3brC6fkHEgvdEWS1SYirzLtsXEJ0cYQ4MLFPPQZ9z ZHO4CikzxDKhg3PbYwUrJdIiJmAWh11kS6Onhjzu/NsS1hDAYUp5pX7p7KoSiOiHFxEt 3NortoHHwB5L2xU0HDrFWPm2hKrONODmDU0SNVLHhaymMa0733Ibd4UKtrmcPTTNA3g+ VXyQbUhhYIBabti1/PvAfGrJeKLXVVBK/dOo+hjYzPongFlTnUtYo7ZhD3kc8fK7D4Xx X9VZTu3Vzt3Ac2YFFdjeg3QzbnO2xpx08fVocPZAyxlJLRGB/BC1oUqb5Hzo1/W9JVE6 QZsQ== X-Gm-Message-State: ALQs6tBqDn/uktd3mIoC09xMgmv8zjRX0r2AG7c/Xo6ydt4UXshRWgNc du184TqPgMgKhps851DUJE0= X-Received: by 2002:a19:e444:: with SMTP id b65-v6mr12523849lfh.61.1525302141148; Wed, 02 May 2018 16:02:21 -0700 (PDT) Received: from [192.168.10.160] (91-159-62-250.elisa-laajakaista.fi. [91.159.62.250]) by smtp.gmail.com with ESMTPSA id c26-v6sm2605499lfh.25.2018.05.02.16.02.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 May 2018 16:02:20 -0700 (PDT) Subject: Re: [PATCH 0/3 v2] linux-next: mm: Track genalloc allocations To: Andrew Morton Cc: mhocko@kernel.org, keescook@chromium.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com, linux-security-module@vger.kernel.org, willy@infradead.org, labbott@redhat.com, linux-kernel@vger.kernel.org, igor.stoppa@huawei.com References: <20180502010522.28767-1-igor.stoppa@huawei.com> <20180502145044.373c268eeaaa9022b99f9191@linux-foundation.org> From: Igor Stoppa Message-ID: Date: Thu, 3 May 2018 03:02:19 +0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180502145044.373c268eeaaa9022b99f9191@linux-foundation.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/05/18 01:50, Andrew Morton wrote: > On Wed, 2 May 2018 05:05:19 +0400 Igor Stoppa wrote: > >> This patchset was created as part of an older version of pmalloc, however >> it has value per-se, as it hardens the memory management for the generic >> allocator genalloc. >> >> Genalloc does not currently track the size of the allocations it hands >> out. >> >> Either by mistake, or due to an attack, it is possible that more memory >> than what was initially allocated is freed, leaving behind dangling >> pointers, ready for an use-after-free attack. >> >> With this patch, genalloc becomes capable of tracking the size of each >> allocation it has handed out, when it's time to free it. >> >> It can either verify that the size received is correct, when free is >> invoked, or it can decide autonomously how much memory to free, if the >> value received for the size parameter is 0. >> >> These patches are proposed for beign merged into linux-next, to verify >> that they do not introduce regressions, by comparing the value received >> from the callers of the free function with the internal tracking. >> >> For this reason, the patchset does not contain the removal of the size >> parameter from users of the free() function. >> >> Later on, the "size" parameter can be dropped, and each caller can be >> adjusted accordingly. >> >> However, I do not have access to most of the HW required for confirming >> that all of its users are not negatively affected. >> This is where I believe having the patches in linux-next would help to >> coordinate with the maintaiers of the code that uses gen_alloc. >> >> Since there were comments about the (lack-of) efficiency introduced by >> this patchset, I have added some more explanations and calculations to the >> description of the first patch, the one adding the bitmap. >> My conclusion is that this patch should not cause any major perfomance >> problem. >> >> Regarding the possibility of completely changing genalloc into some other >> type of allocator, I think it should not be a showstopper for this >> patchset, which aims to plug a security hole in genalloc, without >> introducing any major regression. >> >> The security flaw is clear and present, while the benefit of introducing a >> new allocator is not clear, at least for the current users of genalloc. >> >> And anyway the users of genalloc should be fixed to not pass any size >> parameter, which can be done after this patch is merged. >> >> A newer, more efficient allocator will still benefit from not receiving a >> spurious parameter (size), when freeing memory. >> >> ... >> >> Documentation/core-api/genalloc.rst | 4 + >> include/linux/genalloc.h | 112 +++--- >> lib/Kconfig.debug | 23 ++ >> lib/Makefile | 1 + >> lib/genalloc.c | 742 ++++++++++++++++++++++++++---------- >> lib/test_genalloc.c | 419 ++++++++++++++++++++ > > That's a big patch, True, but I am afraid I do not see how to split it further without braking bisection. and I'm having trouble believing that it's > justified? We're trying to reduce the harm in bugs (none of which are > known to exist) in a small number of drivers to avoid exploits, none of > which are known to exist and which may not even be possible. Should I create one, to justify the patch? Maybe, what we are really discussing if security should be reactive or preventive. And what amount of extra complexity is acceptable, without a current, present threat that has already materialized. My personal take is, if I see something that I think I could exploit, most likely those who do write exploits for a (really well paid) living can do much more harm than I can even think of. > Or something like that. Perhaps all this is taking defensiveness a bit > too far? My main goal was to remove the "size" parameter from the free() call, without introducing noticeable performance regression. Is that a reasonable endeavor? After all, we have IOMMUs also for preventing similar types of attack. The current users of genalloc are primarily: * SRAM memory managers, which are attractive because they are used for example to store system wide state inbetween transitions to off, when some components (like the MMU) might not be even active. * DMA page allocators, another nice side channel, where a DMA controller could be used to completely side-step the type of protection enforced by the MMU > And a bitmap is a pretty crappy way of managing memory anyway, surely? I did not put it there :-P It also depends what one needs it for and if it's good enough. Or if something better is justified. > If this code is indeed performance-sensitive then perhaps a > reimplementation with some standard textbook allocator(?) is warranted? But, is it really performance sensitive? I might be wrong, but I think this change that I am proposing is not really affecting performance. I did get a question/comment about performance implications. I have explained why I think my patches are not adding any real performance problem, in the comment of the patch that does the actual change to the bitmap, providing numbers that I think represent the current real use cases. I was hoping in a reply to that. And a review of the code, also from performance perspective. If I am making some wrong assumption or some mistake, I'll be the first one to acknowledge it, once it is pointed out, however I have not received specific comments about *why* this patch is either bad or wrong, besides "bitmaps are crappy". From my POV, providing a better allocator would be nice, but I do not have time for it, right now. And I am not even sure if it would make any real difference, with the current users of genalloc. A new allocator would be a great thing for intensive allocation-release patterns, with lots of fragmentation. The users of genalloc do not do that. If they did, I suspect someone else would have already come up with a patch to replace genalloc. If a new allocator is being considered for the kernel, what I found to be possibly the best available at the moment is jemalloc [jemalloc.net] It might even be better than other allocators currently in use in the kernel. But it would really need its own project, imho. It shouldn't be done as side activity of kernel hardening. Coming back to genalloc, what I think *can* be said about it, is that: - it's risky because it blindly relies on freeing what its callers asks. - its current users probably wouldn't benefit from a better allocator - hardening the API, provided that there is no performance regression, is a separate activity from rewriting the implementation Maybe genalloc should be renamed to low_frequency_alloc :-P -- igor