From patchwork Fri Jun 1 00:42:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10442209 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 15A2B603D7 for ; Fri, 1 Jun 2018 00:43:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1073A28E43 for ; Fri, 1 Jun 2018 00:43:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 04D1A28E58; Fri, 1 Jun 2018 00:43:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7B2B928E43 for ; Fri, 1 Jun 2018 00:43:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C76C16B0010; Thu, 31 May 2018 20:43:47 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BDC396B0266; Thu, 31 May 2018 20:43:47 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A4FA46B0269; Thu, 31 May 2018 20:43:47 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg0-f69.google.com (mail-pg0-f69.google.com [74.125.83.69]) by kanga.kvack.org (Postfix) with ESMTP id 6421F6B0010 for ; Thu, 31 May 2018 20:43:47 -0400 (EDT) Received: by mail-pg0-f69.google.com with SMTP id k13-v6so5720580pgr.11 for ; Thu, 31 May 2018 17:43:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=nOqvyg4ESECqEu7xshwH3mIkIyxetEA/GGDZ/hrFeaI=; b=GyXdUfwE07E7apSKxiEVdljrccPlBUi7KGBm964/x4AKlCY48U0aiA7QJuILsGqUiO 6WJgH9m2sO/BIzN5lZUzLk79X0r6Hw1u04hixaa1QPdUfA6tZyDl0SoIM7ICqjryiHGe SaIdYfwJUtuOgfHmhxz8NnOHcrW0bgBaXOSf8GjR3+sEhMJFVXgOWEFcuOrpNQCkyNDg FtMXggoaEc2mMUa4e+DTK6gIvQH/6azeL4HW5Uv+GF6uMNF0pxEYfNDf8VGZSe9FQyZL CZtb+lNfnN9hzeyy0hFC+luSrOzkT6z+grV045ijxzlxJBRLuMsJcyVaCObohdOZ3cA/ 4NoA== X-Gm-Message-State: ALKqPwfbDV8kGwhNTgkVg+fSaxITdhMZiAIDVKTNyeEy+aXgf/K4tXPJ rxl/hXkNky9rZgdaQTCed5zeYCWlly0sxYo02UFpCVgxq/wjjBlY46OVnwvdgB5WASYq3NLyhvV EM0IFCLNpObMnHMlIdbxeLHu2DBGpt+Oi5QFTweRTQbs98xlhN1nDgTyTrrfO3ZxAe1NQQLZwFw +31ELPQXP3FOFGRs81VkhRfT6KSn8g1U6X/a5fLYdkw16X5LbKH+WCkILo/8hIMB7i64k6nH1hG jLkZ4FwcT8BI8+tpAKd3TWKgS82iH9JyaR81JyPiA2qUscOnrFoNGkTYoRKr6Z8cmzzNJ7+7S1m vWCXVs8DTFklLevPjv37lCrwi5Wy/QdYdA+2p3/b/63OdX+8SbrPCXbhi5kj9m7gbMsw6IcVtpA s X-Received: by 2002:a17:902:4081:: with SMTP id c1-v6mr8937884pld.60.1527813827086; Thu, 31 May 2018 17:43:47 -0700 (PDT) X-Received: by 2002:a17:902:4081:: with SMTP id c1-v6mr8937849pld.60.1527813826255; Thu, 31 May 2018 17:43:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527813826; cv=none; d=google.com; s=arc-20160816; b=iCVmUnyHmyzI8STZ4tiZDmrnLzXBBv4pLAyB+XENGsB7Up6ZvmEYVqS51cBXO9RoVQ fSI9/ArN22oPlSgsqVqXh8V5/z8EiSFvXb0awStzuSNC/0KMRYmpFVv4yGI32zpXiMWX sHqmoKKIwigLVjSN+1N9wDWZaQsPlKcTOUABcSz0pTz55vxznep9AnIUwkXNgYr9DOjt ccMBjaWQheXwRdHnEtuUGVCCxS4iqVLuyocL7ci8GePZzCw5Z8iw+KbzCXYPrz25Yq6n /TKMYdIFdnWKO8F2A/tBD8ipRkDnTZL806zUJZU/pUjAZ+g0CAA7RLA7ezx5pHp04W/U 7aNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=nOqvyg4ESECqEu7xshwH3mIkIyxetEA/GGDZ/hrFeaI=; b=aiavgsD1hToxeOke6ViahTtidWYA2sM+NId2FEyLllwTv1OFQlkRkPO+aofWXEVH5/ KVMoJqg6st9+bW6TMWFuXCBOAerSBcB2MMhctd0hMIz6sOJvg2ebWsERVx/6ge3jsWg8 uVhQdZ69n5Tn/NgVb0iDoVC84uq/v6hXRTQRHUohcSE0XmS548jBg/DmHV6KgQEhgO8u vYda9iOD/uDt8ymEGnsiaR6eh7/AzjwsqFr9nw7lUhqeXRUjAISdGN3YHDBuKsqi2vad BJGV1v7o/vA/OBdM30wRNZ50QCH6wc5eeEvVAZJfJzNMZPJO0QTX2sPTdREpaR0P532J Ot5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fbOqIZfk; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id z16-v6sor7158799pge.189.2018.05.31.17.43.46 for (Google Transport Security); Thu, 31 May 2018 17:43:46 -0700 (PDT) Received-SPF: pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fbOqIZfk; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nOqvyg4ESECqEu7xshwH3mIkIyxetEA/GGDZ/hrFeaI=; b=fbOqIZfkXmAzKYHRQ7aRixekkLKMMufz5HIloAZ0E/YgBD9N5ofnelt7zQQWQVO80R C7rgGyDnHmrLo5HeakjctvDdwtjRDTiX13uYD6SE3YWzk3vVRYJmogeh1vDt0QSWrX4G X0KRl5pqlTKQDNVE0QngqK2C7wVurpdTMH0yE= X-Google-Smtp-Source: ADUXVKLVzREeVj3MNs/RVmGHND2YrfJtczub7n9FJIf0r0l2c2ucFbAkbKPlIAy1YAYBGarJQYluSw== X-Received: by 2002:a65:644f:: with SMTP id s15-v6mr7124729pgv.228.1527813825932; Thu, 31 May 2018 17:43:45 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id v16-v6sm113148642pfk.164.2018.05.31.17.43.40 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 May 2018 17:43:41 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Linus Torvalds , Rasmus Villemoes , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening Subject: [PATCH v3 05/16] lib: overflow: Add memory allocation overflow tests Date: Thu, 31 May 2018 17:42:22 -0700 Message-Id: <20180601004233.37822-6-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180601004233.37822-1-keescook@chromium.org> References: <20180601004233.37822-1-keescook@chromium.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Make sure that the memory allocators are behaving as expected in the face of overflows. Signed-off-by: Kees Cook --- lib/test_overflow.c | 109 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/lib/test_overflow.c b/lib/test_overflow.c index 482d71c880fa..3f4457ea7d7c 100644 --- a/lib/test_overflow.c +++ b/lib/test_overflow.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #define DEFINE_TEST_ARRAY(t) \ static const struct test_ ## t { \ @@ -294,11 +297,117 @@ static int __init test_overflow_calculation(void) return err; } +/* + * Deal with the various forms of allocator arguments. See comments above + * the DEFINE_TEST_ALLOC() instances for mapping of the "bits". + */ +#define alloc010(alloc, arg, sz) alloc(sz, GFP_KERNEL) +#define alloc011(alloc, arg, sz) alloc(sz, GFP_KERNEL, NUMA_NO_NODE) +#define alloc000(alloc, arg, sz) alloc(sz) +#define alloc001(alloc, arg, sz) alloc(sz, NUMA_NO_NODE) +#define alloc110(alloc, arg, sz) alloc(arg, sz, GFP_KERNEL) +#define free0(free, arg, ptr) free(ptr) +#define free1(free, arg, ptr) free(arg, ptr) + +/* Wrap around to 8K */ +#define TEST_SIZE (9 << PAGE_SHIFT) + +#define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\ +static int __init test_ ## func (void *arg) \ +{ \ + volatile size_t a = TEST_SIZE; \ + volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1; \ + void *ptr; \ + \ + /* Tiny allocation test. */ \ + ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\ + if (!ptr) { \ + pr_warn(#func " failed regular allocation?!\n"); \ + return 1; \ + } \ + free ## want_arg (free_func, arg, ptr); \ + \ + /* Wrapped allocation test. */ \ + ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \ + a * b); \ + if (!ptr) { \ + pr_warn(#func " unexpectedly failed bad wrapping?!\n"); \ + return 1; \ + } \ + free ## want_arg (free_func, arg, ptr); \ + \ + /* Saturated allocation test. */ \ + ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \ + array_size(a, b)); \ + if (ptr) { \ + pr_warn(#func " missed saturation!\n"); \ + free ## want_arg (free_func, arg, ptr); \ + return 1; \ + } \ + pr_info(#func " detected saturation\n"); \ + return 0; \ +} + +/* + * Allocator uses a trailing node argument --------+ (e.g. kmalloc_node()) + * Allocator uses the gfp_t argument -----------+ | (e.g. kmalloc()) + * Allocator uses a special leading argument + | | (e.g. devm_kmalloc()) + * | | | + */ +DEFINE_TEST_ALLOC(kmalloc, kfree, 0, 1, 0); +DEFINE_TEST_ALLOC(kmalloc_node, kfree, 0, 1, 1); +DEFINE_TEST_ALLOC(kzalloc, kfree, 0, 1, 0); +DEFINE_TEST_ALLOC(kzalloc_node, kfree, 0, 1, 1); +DEFINE_TEST_ALLOC(vmalloc, vfree, 0, 0, 0); +DEFINE_TEST_ALLOC(vmalloc_node, vfree, 0, 0, 1); +DEFINE_TEST_ALLOC(vzalloc, vfree, 0, 0, 0); +DEFINE_TEST_ALLOC(vzalloc_node, vfree, 0, 0, 1); +DEFINE_TEST_ALLOC(kvmalloc, kvfree, 0, 1, 0); +DEFINE_TEST_ALLOC(kvmalloc_node, kvfree, 0, 1, 1); +DEFINE_TEST_ALLOC(kvzalloc, kvfree, 0, 1, 0); +DEFINE_TEST_ALLOC(kvzalloc_node, kvfree, 0, 1, 1); +DEFINE_TEST_ALLOC(devm_kmalloc, devm_kfree, 1, 1, 0); +DEFINE_TEST_ALLOC(devm_kzalloc, devm_kfree, 1, 1, 0); + +static int __init test_overflow_allocation(void) +{ + const char device_name[] = "overflow-test"; + struct device *dev; + int err = 0; + + /* Create dummy device for devm_kmalloc()-family tests. */ + dev = root_device_register(device_name); + if (!dev) { + pr_warn("Cannot register test device\n"); + return 1; + } + + err |= test_kmalloc(NULL); + err |= test_kmalloc_node(NULL); + err |= test_kzalloc(NULL); + err |= test_kzalloc_node(NULL); + err |= test_kvmalloc(NULL); + err |= test_kvmalloc_node(NULL); + err |= test_kvzalloc(NULL); + err |= test_kvzalloc_node(NULL); + err |= test_vmalloc(NULL); + err |= test_vmalloc_node(NULL); + err |= test_vzalloc(NULL); + err |= test_vzalloc_node(NULL); + err |= test_devm_kmalloc(dev); + err |= test_devm_kzalloc(dev); + + device_unregister(dev); + + return err; +} + static int __init test_module_init(void) { int err = 0; err |= test_overflow_calculation(); + err |= test_overflow_allocation(); if (err) { pr_warn("FAIL!\n");