Received: by 10.223.176.46 with SMTP id f43csp1061397wra; Wed, 24 Jan 2018 10:00:11 -0800 (PST) X-Google-Smtp-Source: AH8x224zo9SVENWqc6SjiPqmtr7RhMDboxZ5f2p/oxe3M+mKC7Pf3TjGSSRKSIqxeSkN62jG5HIO X-Received: by 10.101.80.69 with SMTP id k5mr432836pgo.431.1516816811764; Wed, 24 Jan 2018 10:00:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516816811; cv=none; d=google.com; s=arc-20160816; b=tDpnhGKxfIkTyOMKTDFz3yBv06PZ2Phj8S83+kaJLaBhcfBBF6e31J5no4ZM2RR4j5 fnhgziEOMNhjcojk27ymk40PYLQURzzBbkPIGaBqYiC5tekk/N/F9hv0Vc8k/uyyBlJp Vd9i5LuvfSB8VaGdrRZMKUcfa9PbDKUA27QqUDoqPxtDVUctt8aSw4gX3zIKg6ZUxtxw e//+o9RtUdoVOM2q4gRP8UWTMVPF9zccwR3Tqz4DjUFw9an/goqgVXH0WDZHFIVJ9/Cy ryJHGLKivLVCbpPdTwXrnFUHZqx39c8coDYSI+IM3n7s1K820IAV+GIxm1EiD1uzvUZQ z4SA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=Yza1Wv+XZgauIKgVsHM6DEK96ZCCFQQHCnJ3McsNZnU=; b=Ir5CK7kIi7cBN3nmRW//r1/O+n+Q0izCq7DW/o2QUoeRLxfiMCox7yb/ZQx8WYFzLG N8ch92zwdI90nBixiZh5/044XXTbbIxgYSITk3rVnezBt+58YyGWvg13rclgjfTQ96Ua Vh4kBtkPHZAoqtYrOqzdu3WdpGRIO2XuJRnxl6zcP6kRGtdvAVF6vJZC6dC9Gj5iOYBQ +pqi0e2uZzmIS7518qi6/0q5DKV90Jve/Eu7Ja04QITABtx5N68ChzUHV+ZEGuFwrvMC T8i6Ep5I1z1YFawyzg5FpXS2ulgJB5pNyz5gRU42k9JyPXn/fIznMJEheTdVkKc1eDPx OEVQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3-v6si532876plq.540.2018.01.24.09.59.57; Wed, 24 Jan 2018 10:00:11 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965095AbeAXR7W (ORCPT + 99 others); Wed, 24 Jan 2018 12:59:22 -0500 Received: from lhrrgout.huawei.com ([194.213.3.17]:22732 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S964850AbeAXR7V (ORCPT ); Wed, 24 Jan 2018 12:59:21 -0500 Received: from LHREML712-CAH.china.huawei.com (unknown [172.18.7.107]) by Forcepoint Email with ESMTP id E13816F2C0DD2; Wed, 24 Jan 2018 17:59:17 +0000 (GMT) Received: from localhost.localdomain (10.122.225.51) by smtpsuk.huawei.com (10.201.108.35) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 24 Jan 2018 17:59:09 +0000 From: Igor Stoppa To: , , , , , CC: , , , , , Igor Stoppa Subject: [PATCH 5/6] Documentation for Pmalloc Date: Wed, 24 Jan 2018 19:56:30 +0200 Message-ID: <20180124175631.22925-6-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20180124175631.22925-1-igor.stoppa@huawei.com> References: <20180124175631.22925-1-igor.stoppa@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.122.225.51] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Detailed documentation about the protectable memory allocator. Signed-off-by: Igor Stoppa --- Documentation/core-api/pmalloc.txt | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Documentation/core-api/pmalloc.txt diff --git a/Documentation/core-api/pmalloc.txt b/Documentation/core-api/pmalloc.txt new file mode 100644 index 0000000..9c39672 --- /dev/null +++ b/Documentation/core-api/pmalloc.txt @@ -0,0 +1,104 @@ +============================ +Protectable memory allocator +============================ + +Introduction +------------ + +When trying to perform an attack toward a system, the attacker typically +wants to alter the execution flow, in a way that allows actions which +would otherwise be forbidden. + +In recent years there has been lots of effort in preventing the execution +of arbitrary code, so the attacker is progressively pushed to look for +alternatives. + +If code changes are either detected or even prevented, what is left is to +alter kernel data. + +As countermeasure, constant data is collected in a section which is then +marked as readonly. +To expand on this, also statically allocated variables which are tagged +as __ro_after_init will receive a similar treatment. +The difference from constant data is that such variables can be still +altered freely during the kernel init phase. + +However, such solution does not address those variables which could be +treated essentially as read-only, but whose size is not known at compile +time or cannot be fully initialized during the init phase. + + +Design +------ + +pmalloc builds on top of genalloc, using the same concept of memory pools +A pool is a handle to a group of chunks of memory of various sizes. +When created, a pool is empty. It will be populated by allocating chunks +of memory, either when the first memory allocation request is received, or +when a pre-allocation is performed. + +Either way, one or more memory pages will be obtaiend from vmalloc and +registered in the pool as chunk. Subsequent requests will be satisfied by +either using any available free space from the current chunks, or by +allocating more vmalloc pages, should the current free space not suffice. + +This is the key point of pmalloc: it groups data that must be protected +into a set of pages. The protection is performed through the mmu, which +is a prerequisite and has a minimum granularity of one page. + +If the relevant variables were not grouped, there would be a problem of +allowing writes to other variables that might happen to share the same +page, but require further alterations over time. + +A pool is a group of pages that are write protected at the same time. +Ideally, they have some high level correlation (ex: they belong to the +same module), which justifies write protecting them all together. + +To keep it to a minimum, locking is left to the user of the API, in +those cases where it's not strictly needed. +Ideally, no further locking is required, since each module can have own +pool (or pools), which should, for example, avoid the need for cross +module or cross thread synchronization about write protecting a pool. + +The overhead of creating an additional pool is minimal: a handful of bytes +from kmalloc space for the metadata and then what is left unused from the +page(s) registered as chunks. + +Compared to plain use of vmalloc, genalloc has the advantage of tightly +packing the allocations, reducing the number of pages used and therefore +the pressure on the TLB. The slight overhead in execution time of the +allocation should be mostly irrelevant, because pmalloc memory is not +meant to be allocated/freed in tight loops. Rather it ought to be taken +in use, initialized and write protected. Possibly destroyed. + +Considering that not much data is supposed to be dynamically allocated +and then marked as read-only, it shouldn't be an issue that the address +range for pmalloc is limited, on 32-bit systemd. + +Regarding SMP systems, the allocations are expected to happen mostly +during an initial transient, after which there should be no more need to +perform cross-processor synchronizations of page tables. + + +Use +--- + +The typical sequence, when using pmalloc, is: + +1. create a pool +2. [optional] pre-allocate some memory in the pool +3. issue one or more allocation requests to the pool +4. initialize the memory obtained + - iterate over points 3 & 4 as needed - +5. write protect the pool +6. use in read-only mode the handlers obtained throguh the allocations +7. [optional] destroy the pool + + +In a scenario where, for example due to some error, part or all of the +allocations performed at point 3 must be reverted, it is possible to free +them, as long as point 5 has not been executed, and the pool is still +modifiable. Such freed memory can be re-used. +Performing a free operation on a write-protected pool will, instead, +simply release the corresponding memory from the accounting, but it will +be still impossible to alter its content. -- 2.9.3