From patchwork Wed Jun 17 19:53:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11610507 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1C34060D for ; Wed, 17 Jun 2020 19:54:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DDB4F21789 for ; Wed, 17 Jun 2020 19:54:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="fjNF7THt" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DDB4F21789 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E150A6B0005; Wed, 17 Jun 2020 15:53:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id DC68B6B0007; Wed, 17 Jun 2020 15:53:57 -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 C43186B000A; Wed, 17 Jun 2020 15:53:57 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0111.hostedemail.com [216.40.44.111]) by kanga.kvack.org (Postfix) with ESMTP id 97BF16B0005 for ; Wed, 17 Jun 2020 15:53:57 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5532155A15 for ; Wed, 17 Jun 2020 19:53:57 +0000 (UTC) X-FDA: 76939754514.01.wood77_610cf8a26e0a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id A6BD220001569C82 for ; Wed, 17 Jun 2020 19:53:56 +0000 (UTC) X-Spam-Summary: 2,0,0,1b463c783b8d7e1a,d41d8cd98f00b204,keescook@chromium.org,,RULES_HIT:41:69:355:379:541:800:960:965:966:973:988:989:1260:1311:1314:1345:1359:1437:1515:1534:1541:1711:1730:1747:1777:1792:2196:2197:2199:2200:2393:2559:2562:2739:3138:3139:3140:3141:3142:3352:3865:3866:3867:4250:4321:4385:4390:4395:5007:6261:6653:6742:7903:9010:10004:11026:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12895:12986:13069:13311:13357:13894:14181:14384:14394:14721:21080:21444:21451:21627:21990:30054:30070,0,RBL:209.85.215.195:@chromium.org:.lbl8.mailshell.net-62.2.0.100 66.100.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: wood77_610cf8a26e0a X-Filterd-Recvd-Size: 4892 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Wed, 17 Jun 2020 19:53:56 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id u128so1777480pgu.13 for ; Wed, 17 Jun 2020 12:53:56 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=dVavS5ePbCYrK2z1L2wDQV1nTQ1HDrNhoLc61sG4Q5M=; b=fjNF7THtjmalkLduwfjs6WAcXUJ8XrLQgh8AJX/PbmUoNRwB7NJXiEnIF4/WGEgshv 0Sc3PHTV8qK08Y1adWyY7RpUt61/9K8iqxsOWwrnN8OiquPeq+5Drr0hasoLXBo3LkL9 Qyi/sIH5I2ZUP4aCOZ0r4QvPuhCN7u5xakY94= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=dVavS5ePbCYrK2z1L2wDQV1nTQ1HDrNhoLc61sG4Q5M=; b=RuAjfw00b+uJjYMbex1cyeIkkZ0YgZWjxQutYf0YcqxBvcaag6fFnFTDsQJptuOYLx Fqo0TKYFmsImScHUekrD0BkMhZTvxhjtxOUm0W6o+sgNnirW6+XK8rShb9nbfbso26Da oL+EMiBV6FmDrbq0k4uqLr6ws5waiyIWaL30L8YGUtGcVTcth6nFrdD72johi3/O0DPo 79oVN/fCveOTsy4S0evqNq72cxdqSWXSg7OxyjwzE3zjoKMuXmtS13AEzbc5KXZ/LP4y 15Uffet88If0cxgQmk9rydu5c/tUjUk0z4yczbvBWhboSFBCwLYnWxg6xponfJ4adCJi xmcA== X-Gm-Message-State: AOAM532bbJTQDTdCf6h3e7RDlKw4aPBFq/Pv4KZ4DY6ZvmQfUVKzFx4U n3YiW2La3Fe34HNIrOent8uVZg== X-Google-Smtp-Source: ABdhPJwOg59bPWyGSiT3mjPppY6RygkG6jLQonyfdZ8GMgBRzj7URVeINwVlCe50tSbxuqAvoLP34w== X-Received: by 2002:a63:4562:: with SMTP id u34mr358332pgk.309.1592423635262; Wed, 17 Jun 2020 12:53:55 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id r4sm566043pgp.60.2020.06.17.12.53.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jun 2020 12:53:53 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Vlastimil Babka , Roman Gushchin , Christoph Lameter , Alexander Popov , Pekka Enberg , David Rientjes , Joonsoo Kim , vinmenon@codeaurora.org, Matthew Garrett , Jann Horn , Vijayanand Jitta , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] slab: Add naive detection of double free Date: Wed, 17 Jun 2020 12:53:49 -0700 Message-Id: <20200617195349.3471794-3-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200617195349.3471794-1-keescook@chromium.org> References: <20200617195349.3471794-1-keescook@chromium.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: A6BD220001569C82 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: Similar to commit ce6fa91b9363 ("mm/slub.c: add a naive detection of double free or corruption"), add a very cheap double-free check for SLAB under CONFIG_SLAB_FREELIST_HARDENED. With this added, the "SLAB_FREE_DOUBLE" LKDTM test passes under SLAB: lkdtm: Performing direct entry SLAB_FREE_DOUBLE lkdtm: Attempting double slab free ... ------------[ cut here ]------------ WARNING: CPU: 2 PID: 2193 at mm/slab.c:757 ___cache _free+0x325/0x390 Signed-off-by: Kees Cook --- mm/slab.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index 9350062ffc1a..c4e3a194b271 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -749,6 +749,16 @@ static void drain_alien_cache(struct kmem_cache *cachep, } } +/* &alien->lock must be held by alien callers. */ +static __always_inline void __free_one(struct array_cache *ac, void *objp) +{ + /* Avoid trivial double-free. */ + if (IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) && + WARN_ON_ONCE(ac->avail > 0 && ac->entry[ac->avail - 1] == objp)) + return; + ac->entry[ac->avail++] = objp; +} + static int __cache_free_alien(struct kmem_cache *cachep, void *objp, int node, int page_node) { @@ -767,7 +777,7 @@ static int __cache_free_alien(struct kmem_cache *cachep, void *objp, STATS_INC_ACOVERFLOW(cachep); __drain_alien_cache(cachep, ac, page_node, &list); } - ac->entry[ac->avail++] = objp; + __free_one(ac, objp); spin_unlock(&alien->lock); slabs_destroy(cachep, &list); } else { @@ -3466,7 +3476,7 @@ void ___cache_free(struct kmem_cache *cachep, void *objp, } } - ac->entry[ac->avail++] = objp; + __free_one(ac, objp); } /**