Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3653577pxb; Mon, 24 Jan 2022 14:31:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJwpMie38v2HFjTJenJtEmu9yopfVJTKj1+q3xIBY2uNesbK/II8q1eZD5dWayX+Oq6GUNDK X-Received: by 2002:a17:902:bd94:b0:149:c926:7c26 with SMTP id q20-20020a170902bd9400b00149c9267c26mr16772357pls.64.1643063487214; Mon, 24 Jan 2022 14:31:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643063487; cv=none; d=google.com; s=arc-20160816; b=VdF/4BM0ws7mdcL/4ak6kgTw0EV0ABSnU79gzf70W1RPj6H//dwLZGUu69Pk00Zcw4 AJceILDd3p51e1UOAZ+YwrdME7F0Yq7V9AY2YS/GMAyzZZTXrYpihCQiJGVtkgYYKDcV jiPDCgLvLhF0wRMT+qVwlXKVFn3MgbzJoANBsroH7JJTJww4vDteIBqUzX0PqvVHVV04 HFqZR2js51JXfE7QPnpVru7oRQ2vmePT+BfWDayDB1954qag8otX7gFPHTOXac6eOojK 2674PXiS9rekFR1PtEURz72iOF2CQh9NneGtsnysN6XeTMHoR/QWI2YlfC20vrRuomHA jMgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3SZ78Ok83NVER9qUu6iwf40u6DUq4x5Dfp2Rnc1A73c=; b=I5dTjL+/zSTOzIA4ecNIZ2nUawLMTztg44DVdUM1nY2qjs37WtPwj9E4jdUFFY7y1f TUx7K71cgi8a3BA9rXc4tVfRFnAGxQ/IjfhrQud3EatVTXUoN/6SdAZm6gfqDlZk7c/M mcLhAPHz1ACJjfnluXGCS+53d95McXSDgQ0bgb+eWPZm1VRUjXOq4lJrkIjU56TQeKDU DPuR4BkyFaHhXFpjHM3vOzRo8ngZlAIZ+MlsKhjP5vTG3LKlGB9l/brQ7U1tYwYf5aBN W9qsw3RaxEi9ShkQ5aS9fxoXiXbXUXxkbKHkbaYRP96T1bwaWwXISfEH9leWgLp2nc0H qaaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qkv81Vm2; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w2si14124605plz.170.2022.01.24.14.31.15; Mon, 24 Jan 2022 14:31:27 -0800 (PST) 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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qkv81Vm2; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1587411AbiAXW2Q (ORCPT + 99 others); Mon, 24 Jan 2022 17:28:16 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:53848 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1455023AbiAXVei (ORCPT ); Mon, 24 Jan 2022 16:34:38 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 76D396131F; Mon, 24 Jan 2022 21:34:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56670C340E4; Mon, 24 Jan 2022 21:34:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643060076; bh=SmVEn4GoyNBFLoGjar7lW6qHLF92Ww9k0BA4JayBMZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qkv81Vm24chKsgTqfEHX5mcodchLieY+C+w2iw/461Psz0bZsrlFbs4OQwwQikg2K 28Xbym+QZzdGwNd8QX3HpzYEG6bMFp4wmUyJg/ln3ZN4PkgeIqMWAKENktmm57sVTq 5FthXt6wO0AJMMRNEqMN9SloA6DcF1B6jsegkBYQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrey Konovalov , Marco Elver , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.16 0830/1039] kasan: fix quarantine conflicting with init_on_free Date: Mon, 24 Jan 2022 19:43:39 +0100 Message-Id: <20220124184153.183232888@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrey Konovalov [ Upstream commit 26dca996ea7b1ac7008b6b6063fc88b849e3ac3e ] KASAN's quarantine might save its metadata inside freed objects. As this happens after the memory is zeroed by the slab allocator when init_on_free is enabled, the memory coming out of quarantine is not properly zeroed. This causes lib/test_meminit.c tests to fail with Generic KASAN. Zero the metadata when the object is removed from quarantine. Link: https://lkml.kernel.org/r/2805da5df4b57138fdacd671f5d227d58950ba54.1640037083.git.andreyknvl@google.com Fixes: 6471384af2a6 ("mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options") Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver Cc: Alexander Potapenko Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Andrey Ryabinin Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/kasan/quarantine.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c index d8ccff4c1275e..47ed4fc33a29e 100644 --- a/mm/kasan/quarantine.c +++ b/mm/kasan/quarantine.c @@ -132,11 +132,22 @@ static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache) static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) { void *object = qlink_to_object(qlink, cache); + struct kasan_free_meta *meta = kasan_get_free_meta(cache, object); unsigned long flags; if (IS_ENABLED(CONFIG_SLAB)) local_irq_save(flags); + /* + * If init_on_free is enabled and KASAN's free metadata is stored in + * the object, zero the metadata. Otherwise, the object's memory will + * not be properly zeroed, as KASAN saves the metadata after the slab + * allocator zeroes the object. + */ + if (slab_want_init_on_free(cache) && + cache->kasan_info.free_meta_offset == 0) + memzero_explicit(meta, sizeof(*meta)); + /* * As the object now gets freed from the quarantine, assume that its * free track is no longer valid. -- 2.34.1