From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B3E9C433EF for ; Mon, 20 Dec 2021 17:16:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A178D6B0074; Mon, 20 Dec 2021 12:16:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A1CB6B0075; Mon, 20 Dec 2021 12:16:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 841226B0078; Mon, 20 Dec 2021 12:16:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0007.hostedemail.com [216.40.44.7]) by kanga.kvack.org (Postfix) with ESMTP id 710AF6B0074 for ; Mon, 20 Dec 2021 12:16:08 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 1AB37180BA555 for ; Mon, 20 Dec 2021 17:16:08 +0000 (UTC) X-FDA: 78938825616.25.2A22B57 Received: from mail-io1-f48.google.com (mail-io1-f48.google.com [209.85.166.48]) by imf25.hostedemail.com (Postfix) with ESMTP id 01698A0037 for ; Mon, 20 Dec 2021 17:15:59 +0000 (UTC) Received: by mail-io1-f48.google.com with SMTP id b187so14192844iof.11 for ; Mon, 20 Dec 2021 09:16:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=v5//0K87lk7Jc0WpZqUISoaFtZRXpKHfv2jyt4Plt/w=; b=Za2uc0ZjTJ3p7WVdGb4nQkLZieOH7j/VDhp98Z9W+IYUofMP1w/JsWzlLKdizHQnGm gUirx1Y05APskSBSeXCdqF5VSvcenrw31YK6F4yr5VBtvfZTezZHObxyLbAdF0EsCPVi WrYh4uFoQr7T9bgwA928tLKaHMp2wOCGdDY4da8TJ6EM2tliWSSTPv/Nl1v2MTID7JJh u7hwIzAxpmfcSAK2+lmGInyVDf9COj71UBT30VYGVJ2ZbJmwg3cbLCmDhQMIRODY4JZH WvKNsLsNAwof/cm38B7oepvXWXJNxl+2rDIG4/RONshtwQ+v8iR8cJYejQ4cbBAU4Lw3 Bwtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=v5//0K87lk7Jc0WpZqUISoaFtZRXpKHfv2jyt4Plt/w=; b=c9B6ny+tGit0q0lO1sWsn2a2nSKSVAbJLZPgQm8uUDsGHWxpy5EiOd1FGdxegP2Po5 QqqyvBK8oGBzdO2XB2bdAXaclnhDR4138oJHLMLmO3Tb+JEDUwpPBN7s07Q4b6BSpuM1 5J/+jVZZcnJPBHdB/mHwQ3ATinQZ/m7FTjiMKWURLZHBwtA1fkqEogyS3l/tOMYZYOzu S4ZYoa4WFBH0xvypR4u220OhnY14x3/vB4pQ9zh41ObaXMCMba7NvHMHtwT5MtMcCt3k Dt7cyPlP7dBnMXHtp3lfAF41wXb2fZR6hCF9I889Rm3dcCEl+PrsZVolP6QuAdAXS7rZ 7OJg== X-Gm-Message-State: AOAM5300aFApOHllelU0oO75grz1D7UxyRpzqlStIBCu5kPVbSu4Vy+y DN6Z0DWHoWz9QV4SwhXfBY/6CKmH8BDFOTjrAJY= X-Google-Smtp-Source: ABdhPJzYaK1o82lVW6qc2Ewtyf3k01M4kblzuMz0WaRPqu4xRADToqCEQ3NmL5KgB9B9vJw2gUnJ9Z7VCUm+dC7xsv0= X-Received: by 2002:a02:864b:: with SMTP id e69mr11110608jai.9.1640020566762; Mon, 20 Dec 2021 09:16:06 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Andrey Konovalov Date: Mon, 20 Dec 2021 18:15:56 +0100 Message-ID: Subject: Re: [PATCH] kasan: fix quarantine conflicting with init_on_free To: Marco Elver Cc: andrey.konovalov@linux.dev, Alexander Potapenko , Andrew Morton , Dmitry Vyukov , Andrey Ryabinin , kasan-dev , Linux Memory Management List , LKML , Andrey Konovalov Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 01698A0037 X-Stat-Signature: qucxffqm43gt6sttcpxd1we85emhm8cd Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Za2uc0Zj; spf=pass (imf25.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.166.48 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1640020559-364225 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: On Mon, Dec 20, 2021 at 6:07 PM Marco Elver wrote: > > On Mon, 20 Dec 2021 at 17:37, wrote: > > > > From: Andrey Konovalov > > > > 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. > > > > Fixes: 6471384af2a6 ("mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options") > > Signed-off-by: Andrey Konovalov > > --- > > mm/kasan/quarantine.c | 11 +++++++++++ > > 1 file changed, 11 insertions(+) > > > > diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c > > index 587da8995f2d..2e50869fd8e2 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) > > + memset(meta, 0, sizeof(*meta)); > > memzero_explicit() > > although in this case it probably doesn't matter much, because AFAIK > memzero_explicit() only exists to prevent the compiler from eliding > the zeroing. Up to you. I've thought about using memzero_explicit(), but the rest of init_on_alloc/free code uses memset(0) so I decided to use it as well. If we decide to switch to memzero_explicit(), it makes sense to do it everywhere. Thanks!