Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2655136pxa; Mon, 17 Aug 2020 15:39:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqtdRmC3Ir+SWhvq0DEYJSFHgLS/0jDOLnklDBtVq5Y7WGpKx3NbSLCsulLipiIu3HDGiz X-Received: by 2002:a17:906:248b:: with SMTP id e11mr16871243ejb.280.1597703942659; Mon, 17 Aug 2020 15:39:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597703942; cv=none; d=google.com; s=arc-20160816; b=ilq5Ng5Ne7qaflu32jxG+WLyf8ioZyrQ2JHmFMhIkPW9u4oGqq94JE+wT8y3AWMCWD FKCxu+ubkjOcTFkaZ/w/n1mFnrcBsaS0iGcB8AN55vXfpJVE1WhUvEb6hjxF1btye5ck ac/jSj36cJKq0ctI0SH6IqFJWwFAZ8bMGRCli1P2IPeKgTghDDS1VQJtx0MTVWNhpVBb t7Gll3MBcrf9ftYymKQQJO1Hf4e50A0fQopitfyFoo2O9urbDi7u8I2dqBvLI23QTlrl wMe4l13JLoBYhtu7QDq9WOkM9UfHO1v+CkOW/XR9r3lfuFDILNoQoHuBpEPhYf4EOJkC fJ9Q== 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:autocrypt:from:references:cc:to:subject:reply-to; bh=zioXayb3aZ1+dwOWJvBfiHPq2HNW4QALEO2XKd3ld70=; b=UUJndMiwkTC3SkqLXUXBdkYH7tPXOD8Z2YIDws8bJbFcC5yHJD7+53z8R/pPoVjy+2 2ej1x595GlUc2QkiRY2wj5iDPQtjQ7zeRAswZSXJ/AKSCOTec/kKna2k0ZNta3UyVmhi mQl0BsiRIYNRWefjFTk6HLWfu0bje7/Ll6GDLhjhVag6V8WzXTzic0l0q2cP0ym4jXec aawVC910YtmWfdxO6dwTSSjqrR0YYWD+xYZN8j6ZdZAh6TVfdgKIT4VRtrmK1AfkIFqe P0MhPJx0z3v1QK6OWSyC4MTy+pSkCajK2bKg+9tL8Rz9ju74PPwtmvYoCDmt9NXq+2hl SeJw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b13si12823817ejq.223.2020.08.17.15.38.39; Mon, 17 Aug 2020 15:39:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731686AbgHQRyd (ORCPT + 99 others); Mon, 17 Aug 2020 13:54:33 -0400 Received: from mail-ej1-f65.google.com ([209.85.218.65]:40918 "EHLO mail-ej1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731672AbgHQRyN (ORCPT ); Mon, 17 Aug 2020 13:54:13 -0400 Received: by mail-ej1-f65.google.com with SMTP id o18so18785732eje.7 for ; Mon, 17 Aug 2020 10:54:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=zioXayb3aZ1+dwOWJvBfiHPq2HNW4QALEO2XKd3ld70=; b=Hzo4i6dGsRt1tBL/EFvUCLkHY0hUYS2fscqzRw6nwV+jCsp7MIwvHPM9nVP0Z1h5Ax u1lxRwqXzIxrNqvBd7R/98IAvkgZMhK5oRCGwQ8GhYCYrVhv/aDHukuQ9PzdwAyMIq6V d/XNHv2bUyabHryD9yOj3oFMnTQjmxTYN6sN81nxlx1VMIh+Y/+qHQc/skD0iAGtxVX8 Q8bssHx+7M/pwsJ2zvkcix/ta5GaWwBGgIojtw1qyrD1yS3stBEf1e1EO9nMrZATMoeh 6bwm7KyvrwvcdqTyv+T/sfrcqG6JUVl5tVHV0u9ATi7/bCEWVoLjDg80ykvK8/vxwYu8 YS5Q== X-Gm-Message-State: AOAM531/DoaehCPyaooZ4VYCrfWyXevXsDFHtjDQUwJJqeuM8c5ajtCv fEKDKNknTnSsAQN2UWCEelrBr8nIrP/0fw== X-Received: by 2002:a17:906:528d:: with SMTP id c13mr16654442ejm.61.1597686850290; Mon, 17 Aug 2020 10:54:10 -0700 (PDT) Received: from [10.9.0.18] ([185.248.161.177]) by smtp.gmail.com with ESMTPSA id m12sm10332353eda.51.2020.08.17.10.54.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 17 Aug 2020 10:54:09 -0700 (PDT) Reply-To: alex.popov@linux.com Subject: Re: [PATCH RFC 2/2] lkdtm: Add heap spraying test To: Kees Cook Cc: Jann Horn , Will Deacon , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Masahiro Yamada , Masami Hiramatsu , Steven Rostedt , Peter Zijlstra , Krzysztof Kozlowski , Patrick Bellasi , David Howells , Eric Biederman , Johannes Weiner , Laura Abbott , Arnd Bergmann , Greg Kroah-Hartman , kasan-dev@googlegroups.com, linux-mm@kvack.org, kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org, notify@kernel.org, Andrey Konovalov References: <20200813151922.1093791-1-alex.popov@linux.com> <20200813151922.1093791-3-alex.popov@linux.com> <202008150952.E81C4A52F@keescook> From: Alexander Popov Autocrypt: addr=alex.popov@linux.com; prefer-encrypt=mutual; keydata= mQINBFX15q4BEADZartsIW3sQ9R+9TOuC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K4QhMEUCHmoOIaS9j0VTU4fR9ZCpVT/ksc2LPMhg3YqNTrnb1v RVNUZvh78zQeCXC2VamSl9DMcwARAQABiQI8BBgBCAAmAhsMFiEEuXYksCQBUzRtW9ZOjg6d 67x+j6oFAl16ErcFCQtGkwkACgkQjg6d67x+j6q7zA/+IsjSKSJypgOImN9LYjeb++7wDjXp qvEpq56oAn21CvtbGus3OcC0hrRtyZ/rC5Qc+S5SPaMRFUaK8S3j1vYC0wZJ99rrmQbcbYMh C2o0k4pSejaINmgyCajVOhUhln4IuwvZke1CLfXe1i3ZtlaIUrxfXqfYpeijfM/JSmliPxwW BRnQRcgS85xpC1pBUMrraxajaVPwu7hCTke03v6bu8zSZlgA1rd9E6KHu2VNS46VzUPjbR77 kO7u6H5PgQPKcuJwQQ+d3qa+5ZeKmoVkc2SuHVrCd1yKtAMmKBoJtSku1evXPwyBzqHFOInk mLMtrWuUhj+wtcnOWxaP+n4ODgUwc/uvyuamo0L2Gp3V5ItdIUDO/7ZpZ/3JxvERF3Yc1md8 5kfflpLzpxyl2fKaRdvxr48ZLv9XLUQ4qNuADDmJArq/+foORAX4BBFWvqZQKe8a9ZMAvGSh uoGUVg4Ks0uC4IeG7iNtd+csmBj5dNf91C7zV4bsKt0JjiJ9a4D85dtCOPmOeNuusK7xaDZc gzBW8J8RW+nUJcTpudX4TC2SGeAOyxnM5O4XJ8yZyDUY334seDRJWtS4wRHxpfYcHKTewR96 IsP1USE+9ndu6lrMXQ3aFsd1n1m1pfa/y8hiqsSYHy7JQ9Iuo9DxysOj22UNOmOE+OYPK48D j3lCqPk= Message-ID: <37ec713d-10c8-0222-f624-27815b96da7a@linux.com> Date: Mon, 17 Aug 2020 20:54:04 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <202008150952.E81C4A52F@keescook> Content-Type: text/plain; charset=utf-8 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 15.08.2020 19:59, Kees Cook wrote: > On Thu, Aug 13, 2020 at 06:19:22PM +0300, Alexander Popov wrote: >> Add a simple test for CONFIG_SLAB_QUARANTINE. >> >> It performs heap spraying that aims to reallocate the recently freed heap >> object. This technique is used for exploiting use-after-free >> vulnerabilities in the kernel code. >> >> This test shows that CONFIG_SLAB_QUARANTINE breaks heap spraying >> exploitation technique. > > Yay tests! Yes :) I'm going to improve it to demonstrate the quarantine security properties. >> Signed-off-by: Alexander Popov >> --- >> drivers/misc/lkdtm/core.c | 1 + >> drivers/misc/lkdtm/heap.c | 40 ++++++++++++++++++++++++++++++++++++++ >> drivers/misc/lkdtm/lkdtm.h | 1 + >> 3 files changed, 42 insertions(+) >> >> diff --git a/drivers/misc/lkdtm/core.c b/drivers/misc/lkdtm/core.c >> index a5e344df9166..78b7669c35eb 100644 >> --- a/drivers/misc/lkdtm/core.c >> +++ b/drivers/misc/lkdtm/core.c >> @@ -126,6 +126,7 @@ static const struct crashtype crashtypes[] = { >> CRASHTYPE(SLAB_FREE_DOUBLE), >> CRASHTYPE(SLAB_FREE_CROSS), >> CRASHTYPE(SLAB_FREE_PAGE), >> + CRASHTYPE(HEAP_SPRAY), >> CRASHTYPE(SOFTLOCKUP), >> CRASHTYPE(HARDLOCKUP), >> CRASHTYPE(SPINLOCKUP), >> diff --git a/drivers/misc/lkdtm/heap.c b/drivers/misc/lkdtm/heap.c >> index 1323bc16f113..a72a241e314a 100644 >> --- a/drivers/misc/lkdtm/heap.c >> +++ b/drivers/misc/lkdtm/heap.c >> @@ -205,6 +205,46 @@ static void ctor_a(void *region) >> static void ctor_b(void *region) >> { } >> >> +#define HEAP_SPRAY_SIZE 128 >> + >> +void lkdtm_HEAP_SPRAY(void) >> +{ >> + int *addr; >> + int *spray_addrs[HEAP_SPRAY_SIZE] = { 0 }; > > (the 0 isn't needed -- and it was left there, it should be NULL) It is used in tear-down below. I'll change it to { NULL }. >> + unsigned long i = 0; >> + >> + addr = kmem_cache_alloc(a_cache, GFP_KERNEL); > > I would prefer this test add its own cache (e.g. spray_cache), to avoid > misbehaviors between tests. (e.g. the a and b caches already run the > risk of getting corrupted weirdly.) Ok, I'll do that. >> + if (!addr) { >> + pr_info("Unable to allocate memory in lkdtm-heap-a cache\n"); >> + return; >> + } >> + >> + *addr = 0x31337; >> + kmem_cache_free(a_cache, addr); >> + >> + pr_info("Performing heap spraying...\n"); >> + for (i = 0; i < HEAP_SPRAY_SIZE; i++) { >> + spray_addrs[i] = kmem_cache_alloc(a_cache, GFP_KERNEL); >> + *spray_addrs[i] = 0x31337; >> + pr_info("attempt %lu: spray alloc addr %p vs freed addr %p\n", >> + i, spray_addrs[i], addr); > > That's 128 lines spewed into dmesg... I would leave this out. Ok. >> + if (spray_addrs[i] == addr) { >> + pr_info("freed addr is reallocated!\n"); >> + break; >> + } >> + } >> + >> + if (i < HEAP_SPRAY_SIZE) >> + pr_info("FAIL! Heap spraying succeed :(\n"); > > I'd move this into the "if (spray_addrs[i] == addr)" test instead of the > pr_info() that is there. > >> + else >> + pr_info("OK! Heap spraying hasn't succeed :)\n"); > > And then make this an "if (i == HEAP_SPRAY_SIZE)" test Do you mean that I need to avoid the additional line in the test output, printing only the final result? >> + >> + for (i = 0; i < HEAP_SPRAY_SIZE; i++) { >> + if (spray_addrs[i]) >> + kmem_cache_free(a_cache, spray_addrs[i]); >> + } >> +} >> + >> void __init lkdtm_heap_init(void) >> { >> double_free_cache = kmem_cache_create("lkdtm-heap-double_free", >> diff --git a/drivers/misc/lkdtm/lkdtm.h b/drivers/misc/lkdtm/lkdtm.h >> index 8878538b2c13..dfafb4ae6f3a 100644 >> --- a/drivers/misc/lkdtm/lkdtm.h >> +++ b/drivers/misc/lkdtm/lkdtm.h >> @@ -45,6 +45,7 @@ void lkdtm_READ_BUDDY_AFTER_FREE(void); >> void lkdtm_SLAB_FREE_DOUBLE(void); >> void lkdtm_SLAB_FREE_CROSS(void); >> void lkdtm_SLAB_FREE_PAGE(void); >> +void lkdtm_HEAP_SPRAY(void); >> >> /* lkdtm_perms.c */ >> void __init lkdtm_perms_init(void); >> -- >> 2.26.2 >> > > I assume enabling the quarantine defense also ends up being seen in the > SLAB_FREE_DOUBLE LKDTM test too, yes? I'll experiment with that. Thank you! Best regards, Alexander