Received: by 10.192.165.148 with SMTP id m20csp85244imm; Tue, 1 May 2018 18:06:12 -0700 (PDT) X-Google-Smtp-Source: AB8JxZov6LiuviEup3ZyxjFCkRsdVE2GXlRCK7EA3GkXQLZc4bFfSocrzfVn7OqeXXR14KlSgszg X-Received: by 2002:a65:4542:: with SMTP id x2-v6mr14894088pgr.24.1525223171978; Tue, 01 May 2018 18:06:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525223171; cv=none; d=google.com; s=arc-20160816; b=lB+SfvRVEIMxmS2hU1gH6NfKxsVRycoOSd3KIY2TjFRJRG0lTTLyUBl8CjlEbY8vWJ NGUs/nRu8UZAt399dS+jjkhtTIRFAgwK2EQzMlb+FyOg9NAFQuL15waKdiLi8dDl6hGd kwLxGS5dpB8bVJ3pGQTEsBT+TAUQ3rloRSXAbEjq8zH8V3hwdOHpd7kO2sI6Lc0UOH+a vD7/1u3Ck5SCvP0lpz8ee0enWwl6KZmT7IVT7u+5NaIJFRYsdpsITj3O+wVUVGtyhYYB pvmiyqwi5of20KT58ZOg8BK8iuqnLh+YZDxbo0jZKxp3Lo0j28KjV5yBwCdeJY5Fmg9R vSaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=L+JDF9iTaljm+FXparRmo0KN96D2OqblEqICBrv6xUM=; b=i/tIqPfxAcVRCMOhiJk++dvgGPWgqkt1lcZHzXiLMmbFPbLqIr2FDAkYlfwVwTFDPE A9Qexk7wzLJkaJJrqqFphPkZGAT9R07+QsgVXvB21sF3da/jIGUMCIzTCBoPJuP/92Ok LmrotKGXxau4o9pBz/7ypGmmMV3x1xEJrDpeFyMOnh+J02KdZ4Ck/ek20pY2y8wMh4UH arXwYBwlrbyis0WZBFmpBpapseH+1BHmmQmf1zNLmgWwInXhmn+UlFoxRNgYJIqr+7B/ 6NL+st+mTm0pDfeZemyHemhp96+tVloR5asfE1w3QKX/Gjazh4JFc6/lTRbo7kOrP6Py sGVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EHRlsBjr; 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 k14-v6si8866634pgs.418.2018.05.01.18.05.57; Tue, 01 May 2018 18:06:11 -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=EHRlsBjr; 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 S1751262AbeEBBFi (ORCPT + 99 others); Tue, 1 May 2018 21:05:38 -0400 Received: from mail-lf0-f48.google.com ([209.85.215.48]:41525 "EHLO mail-lf0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750962AbeEBBFf (ORCPT ); Tue, 1 May 2018 21:05:35 -0400 Received: by mail-lf0-f48.google.com with SMTP id o123-v6so18404964lfe.8; Tue, 01 May 2018 18:05:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=L+JDF9iTaljm+FXparRmo0KN96D2OqblEqICBrv6xUM=; b=EHRlsBjrrsTvFJwRMSZuEDftfiPGZVBcXtB6fYSzjOucAQ4tt8SmhErhMBLiPmgVFH VSCS8uWM0s1/YwSgRn7X7VU/ZC/OiaCwhEIUxbmWMn1Qk6OwN8BaATezW8mqVv9Nf/xv j0XkPQ8+09AGvFpeqWnlj7Me/sOoME41uVB8NiFe/5t3hZGJlwy+CqXiMshrxukabCBx RpoogEX766QnOx1etQVp+aVl8jJ/695uGl/GtFylhRea1A/RqOL777u0vGZyKgMGMaqK gQgvbpeEs3afqc3ERmkPR7FUh2t5mfvq+lJ2Ea+DWcpF64XpQ8bbtlMDTtrpEDUIlkCu Zfyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=L+JDF9iTaljm+FXparRmo0KN96D2OqblEqICBrv6xUM=; b=nI0kUNtLTxBs4XyH8iRVcbM+htK+tXNkz4i2j2tT+qLucp/OlmHFjMJKTtrHvtStEH u1NSs0jSsTMIIJ4wWLhgzQ8Zxj+G5kTBGRpKJRwD4J+sYeLdqw3cLyYuwlL2eY7K3jKw eZ1jrU9Cwk+67/KgmvrSo+MYtKQqAZBZZOZ+uQ4Y4LDPMiZMZohiSeojPop7uIbpmQx2 D9pziQXXqdetZ6RNmp0uNFHy8lzruRO4Ruc5tKBsOiJIoCVH87BvxKb4Biqy7WN7atv1 0jtqyZw+wgHRJ4oUkvOIqRLNvAgYTP8unD2rmna8p78J0WHcwkKT8rTWkaJKt70Dnct2 jUBQ== X-Gm-Message-State: ALQs6tAydPdTGSXoAuXGLseJ/l/djPbPIgJFvJTE3q8OUKE5+mFJQkcK L1w04qjtqoSoMRrc2moZBME= X-Received: by 2002:a19:d7df:: with SMTP id q92-v6mr2269839lfi.71.1525223133501; Tue, 01 May 2018 18:05:33 -0700 (PDT) Received: from Mort.event.rightround.com (91-156-179-11.elisa-laajakaista.fi. [91.156.179.11]) by smtp.gmail.com with ESMTPSA id 17-v6sm2127729ljr.17.2018.05.01.18.05.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 May 2018 18:05:32 -0700 (PDT) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: mhocko@kernel.org, akpm@linux-foundation.org, keescook@chromium.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com, linux-security-module@vger.kernel.org Cc: willy@infradead.org, labbott@redhat.com, linux-kernel@vger.kernel.org, igor.stoppa@huawei.com Subject: [PATCH 0/3 v2] linux-next: mm: Track genalloc allocations Date: Wed, 2 May 2018 05:05:19 +0400 Message-Id: <20180502010522.28767-1-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.14.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Changes since v1: [http://www.openwall.com/lists/kernel-hardening/2018/04/29/1] * make the tester code a kernel module * turn selftest BUG() error exit paths into WARN() * add analysis of impact on current users of genalloc Igor Stoppa (3): genalloc: track beginning of allocations Add label and license to genalloc.rst genalloc: selftest 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 ++++++++++++++++++++ 6 files changed, 1046 insertions(+), 255 deletions(-) create mode 100644 lib/test_genalloc.c -- 2.14.1