Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp188611rdb; Tue, 19 Dec 2023 13:23:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFAMX9IKxrvSGZJSQEsUQLl32xeQtEp54B1Nz29M1TR5t3rr6D9xjKreg5MFDCUr3DcC0Wi X-Received: by 2002:a05:6a00:1307:b0:6ce:2731:e86b with SMTP id j7-20020a056a00130700b006ce2731e86bmr24099505pfu.50.1703020995448; Tue, 19 Dec 2023 13:23:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703020995; cv=none; d=google.com; s=arc-20160816; b=wfLpSr05jQaZgqE9e21Nclrj5KKe0V6ffOlMP8W8aqATMYatneZjVIu3KE/bopk7S+ tX+Q8aIy/SxvkzLmXECekpD0fs4YzXmX51UUnOztxUOyOEvXIXOQSxAy88zLw8ocpuVm Uk6uWOiW0ADSZLgQijS4kE3SZcww5066Wb7NyyZqU4cDVVajD404/8TuHMn863SPteEO 2slmZR3YrwyP5O1Ug3y1R/UQWLwRBYDhxY7+ikdtM17sshxZNFAX6JiL5ulGdJTsl6uY cf3QPpuLcoJVz4aZuM6ilmFddhsL/6yv8JJovg1DL8xAnAdaTvPY7kWIKNK2GUNyNdCN 9PKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=8LGegP+G5PWWJJoK9xgDRxY4ivR1WBT3Si9rlrLcuhs=; fh=2SMY3Ygb4gA67Fm9yk5IPAJMxRWhdsxd8qEuh3AIfK8=; b=tV1eBiddhhqalJscNOxJuPfoU4uSY6EWizytFub9cmVDl/G3so7kNfjENxNSpGV9Ec OPBdpp1IxU1a/WCryy9O1p0NN8Sw4ILASrHjqy+bmmIjAPXHgdOl12Lk//5xDTY5Exsg 4qhWa2OdUsLmmhiP7fqksOrUQ9RESRaGjj2eXIcHK0FmzGvloCV76HaxoVYAZIsoBBId A8Ees2eO2/9GI3deX1YWZfK1N2MQzJBAWkzoAMfbJGi2MPp54a/nRW5j2RCx9xrcEFoL rlPQCaLjaTIJ8u+w9uvWfR0A7w1bPYQ6NEmUWC276jtqkgcutYn8iJZwExO6CmUo4jE+ 1ceA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="BLfKU/60"; spf=pass (google.com: domain of linux-kernel+bounces-5968-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5968-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id b10-20020a056a00114a00b006ce4de73f9bsi20074110pfm.117.2023.12.19.13.23.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 13:23:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5968-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="BLfKU/60"; spf=pass (google.com: domain of linux-kernel+bounces-5968-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5968-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 43F3FB24CB4 for ; Tue, 19 Dec 2023 21:22:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 59A2E3D0B3; Tue, 19 Dec 2023 21:22:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BLfKU/60" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ua1-f52.google.com (mail-ua1-f52.google.com [209.85.222.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 082853D0A5 for ; Tue, 19 Dec 2023 21:22:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ua1-f52.google.com with SMTP id a1e0cc1a2514c-7cbf6ce782dso488738241.0 for ; Tue, 19 Dec 2023 13:22:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1703020923; x=1703625723; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=8LGegP+G5PWWJJoK9xgDRxY4ivR1WBT3Si9rlrLcuhs=; b=BLfKU/60GmFLDOteu0Ir3XZ0xybACjQKaidD+D+jP1bqZNQUfCIk3r8uDRf4Dppvnt MtYrnma6B7L77EcdPiVc/fjJyXodvQ0+WCBFfkqlJgK7iR5BtbSy9YfRPZkQXEKMgiiV IWWed5SI/cH64/t4BPN6/y+/YjQ+gJllt24xPChwxMfMrfl5tkfLxcSQt90ebyD5z1aH 3NWivXCMUEUbONIrZzOwZsBYZ31GumyDjHpuSfMSiuKtQ1unaBcaFmjJTmal+FPHMTFv pVlWfsjSwLJIzEincycUbb5x0hQb8EjrurMJmtXmNvajEMjV+vYZ3SFbKM/c2D+vJapj dLmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703020923; x=1703625723; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8LGegP+G5PWWJJoK9xgDRxY4ivR1WBT3Si9rlrLcuhs=; b=qT3VhtmFZ4BsEo8zhEp4+bAk4hqjN4NW+ab9eJ7Q20bRtW0ki9Vzo7KIqN3e21fHqm NvOOsVh/DhLOZ+pfToOpYw5Aonu35C1TBjC913Bge39UxJpIzOzMFhU6Xqju2I8tcDc5 ztuAUraWRUEjI1BwrSlzHQ/uVcIvxgJITzIZIOLxEkg0JlOi0wa6ONmoNdVZezXv6A4I bWLGztoJZcP63g4bM01PGLSmZPCcgj6HjO/7RnClA6V74Fi4PsUBMHc8RuG0faiDwsdJ TO9dJsXyN5rvewHdGZx4xYVrMAbKtdpneGaoSv7ozG7rO3Q6kfy4kElofRJaV+deHiRa J4PA== X-Gm-Message-State: AOJu0Yw9jZtdUnO0NUKo9Dsy6kNOvb8tZ0bREU2c1MoLPlcoTTj9/72+ ZOzuM7WoJII09xY4skCFHFq2G20ZnCR2S0Ha7Z0rbw== X-Received: by 2002:a05:6102:559e:b0:466:9bec:ae53 with SMTP id dc30-20020a056102559e00b004669becae53mr2855956vsb.25.1703020922750; Tue, 19 Dec 2023 13:22:02 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <1606b960e2f746862d1f459515972f9695bf448a.1703020707.git.andreyknvl@google.com> In-Reply-To: <1606b960e2f746862d1f459515972f9695bf448a.1703020707.git.andreyknvl@google.com> From: Marco Elver Date: Tue, 19 Dec 2023 22:21:25 +0100 Message-ID: Subject: Re: [PATCH v3 mm 2/4] kasan: handle concurrent kasan_record_aux_stack calls To: andrey.konovalov@linux.dev Cc: Andrew Morton , Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Tetsuo Handa , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov , syzbot+186b55175d8360728234@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" On Tue, 19 Dec 2023 at 22:19, wrote: > > From: Andrey Konovalov > > kasan_record_aux_stack can be called concurrently on the same object. > This might lead to a race condition when rotating the saved aux stack > trace handles, which in turns leads to incorrect accounting of stack > depot handles and refcount underflows in the stack depot code. > > Fix by introducing a raw spinlock to protect the aux stack trace handles > in kasan_record_aux_stack. > > Reported-by: Tetsuo Handa > Reported-by: syzbot+186b55175d8360728234@syzkaller.appspotmail.com > Closes: https://lore.kernel.org/all/000000000000784b1c060b0074a2@google.com/ > Fixes: 773688a6cb24 ("kasan: use stack_depot_put for Generic mode") > Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver > --- > > Changes v2->v3: > - Use raw spinlock to avoid lockdep complaints on RT kernels. > > Changes v1->v2: > - Use per-object spinlock instead of a global one. > --- > mm/kasan/generic.c | 32 +++++++++++++++++++++++++++++--- > mm/kasan/kasan.h | 8 ++++++++ > 2 files changed, 37 insertions(+), 3 deletions(-) > > diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c > index 54e20b2bc3e1..55e6b5db2cae 100644 > --- a/mm/kasan/generic.c > +++ b/mm/kasan/generic.c > @@ -25,6 +25,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -471,8 +472,18 @@ void kasan_init_object_meta(struct kmem_cache *cache, const void *object) > struct kasan_free_meta *free_meta; > > alloc_meta = kasan_get_alloc_meta(cache, object); > - if (alloc_meta) > + if (alloc_meta) { > __memset(alloc_meta, 0, sizeof(*alloc_meta)); > + > + /* > + * Temporarily disable KASAN bug reporting to allow instrumented > + * raw_spin_lock_init to access aux_lock, which resides inside > + * of a redzone. > + */ > + kasan_disable_current(); > + raw_spin_lock_init(&alloc_meta->aux_lock); > + kasan_enable_current(); > + } > free_meta = kasan_get_free_meta(cache, object); > if (free_meta) > __memset(free_meta, 0, sizeof(*free_meta)); > @@ -502,6 +513,8 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) > struct kmem_cache *cache; > struct kasan_alloc_meta *alloc_meta; > void *object; > + depot_stack_handle_t new_handle, old_handle; > + unsigned long flags; > > if (is_kfence_address(addr) || !slab) > return; > @@ -512,9 +525,22 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) > if (!alloc_meta) > return; > > - stack_depot_put(alloc_meta->aux_stack[1]); > + new_handle = kasan_save_stack(0, depot_flags); > + > + /* > + * Temporarily disable KASAN bug reporting to allow instrumented > + * spinlock functions to access aux_lock, which resides inside of a > + * redzone. > + */ > + kasan_disable_current(); > + raw_spin_lock_irqsave(&alloc_meta->aux_lock, flags); > + old_handle = alloc_meta->aux_stack[1]; > alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; > - alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags); > + alloc_meta->aux_stack[0] = new_handle; > + raw_spin_unlock_irqrestore(&alloc_meta->aux_lock, flags); > + kasan_enable_current(); > + > + stack_depot_put(old_handle); > } > > void kasan_record_aux_stack(void *addr) > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index 5e298e3ac909..69e4f5e58e33 100644 > --- a/mm/kasan/kasan.h > +++ b/mm/kasan/kasan.h > @@ -6,6 +6,7 @@ > #include > #include > #include > +#include > #include > > #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) > @@ -249,6 +250,13 @@ struct kasan_global { > struct kasan_alloc_meta { > struct kasan_track alloc_track; > /* Free track is stored in kasan_free_meta. */ > + /* > + * aux_lock protects aux_stack from accesses from concurrent > + * kasan_record_aux_stack calls. It is a raw spinlock to avoid sleeping > + * on RT kernels, as kasan_record_aux_stack_noalloc can be called from > + * non-sleepable contexts. > + */ > + raw_spinlock_t aux_lock; > depot_stack_handle_t aux_stack[2]; > }; > > -- > 2.25.1 >