Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp484710pxb; Thu, 21 Oct 2021 03:38:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyG2lMlletq9F/Zs72oWFUzoybcyJaVzoU5Eo9SDJtiaXyvpGmcK7z+Y0kls2ztqB52ZIcT X-Received: by 2002:a17:902:d88c:b0:13f:f584:f630 with SMTP id b12-20020a170902d88c00b0013ff584f630mr3299588plz.58.1634812702374; Thu, 21 Oct 2021 03:38:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634812702; cv=none; d=google.com; s=arc-20160816; b=FMpXIq5HPHh+5GZC3+VR0jbU+2prwgA2LnyVWZaW6SAJdpk/J0yk4MWjuj16zzF1Ut 6AEN/o/pnokSKMomcmmRtUdbaVPcup1aUEHB2JQi0yBfoFhKL1dvWg/czBJ8IIC/236B hDKPaoO5Y+1S5M2/joBGBBJ+TLQJoTDAeqiEeHyUtCb1sV8qzspXM6OgZBYWevVjdiO7 6QXg1z3YZLIEFEBt38PwV4QMZ/JjHE6cI+AbRmRNiPvZQY+byGSEsK6NK6afTlvxhBCq N/iUdQJw3fvoGGUNnj6wQHbZwPSnJ/KvOEUn/8iPEYSHDbWKRD7CiZNuYfkwXqCXhcGt n9JQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=ZIQ1evG3JcfP14fnAmThR7DabXSKbcX4zhT17hg7Jpc=; b=tjnxIVz6RI5opGa4/bbz9s8PFS8uHx6MJWkj/41zAyJxnweclcd1WTOnlq/hxO4Yby ZpRpGm/D1a/hlcX9g84ZkvFzgD9UXnmL049k+NtB1HNAbxDyVo1C0Tv765BsiN2jWiOG RrUIaMnTODwDdZYr8MAty3K7V6KwLhXvO80Ei6y4urRPtKj1vtMO7ekTwXjr2eLfw/+L L3VwoUJFmxVIUjG0JwcLZMOQGtSTZP0oiXLR4kORwlmlUktUI1CRDAXYExfr7BQ/D3KZ obeQtYidAx+7qhj6EI9DFLcNfQl4CSIZxCVZ/9NuNH3DuCK+v0pbeGHSzOqcU3M/5rfB 4jnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ECp2xJ4e; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f16si6575591pgb.585.2021.10.21.03.38.09; Thu, 21 Oct 2021 03:38:22 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=ECp2xJ4e; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230316AbhJUKiG (ORCPT + 99 others); Thu, 21 Oct 2021 06:38:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230267AbhJUKiF (ORCPT ); Thu, 21 Oct 2021 06:38:05 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC0B2C06161C; Thu, 21 Oct 2021 03:35:49 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id kk10so208575pjb.1; Thu, 21 Oct 2021 03:35:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZIQ1evG3JcfP14fnAmThR7DabXSKbcX4zhT17hg7Jpc=; b=ECp2xJ4eWy9H8KkGyneV/LJsYm9Ap+21RMBdPYbgJ5SWCNzEGpAaxuushG7hsgN+Ii ZWPpAP1PN+ar8FwjHl3fDfZPIixXOr6l2zN8ySsBicCUv5X4JG/nyiTv1JSIY1qWxEky NKCLxqfjaBKzz684Ymfo6lYqXQz5r8/AOD0aKR5/yvQ17qanPfHPUCdeEO8uKKeLOJOb 4v1vtMOwIz4S7ffNCPb4nJJgWZKPBBf7dUsvZzSk+Dd5IOaBhbU76wiEZ88X1FTDhkNC bBBWKkFoYeW/zwTwUDtge5K0KmOhZzKIRHun+xTGYyPhC1+nOsUX6IzCxS3MMRVOj/Lv UmNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZIQ1evG3JcfP14fnAmThR7DabXSKbcX4zhT17hg7Jpc=; b=BkCd+NC4LsNlF3VxaAqaSJPokfNZQ7Qprl5+c4y4klWYLrhHbHN7DXbbMHsK1mFtTO LTreYQFu/X1EHQWBducuo0T7VyiNzteMnT2yMKE1+XKzLi0h1jdnHmKXOsAjM/mKGqlq hq38C66Y9qBqswTaetG8p7uhujfBdfbPZSjsSnaKJ1Q/AhBJ8CfI0gOvf9/xzAIqd2Mu AqTvOquS1nfzMHqM+p9YaXx21FzZN+Gi2mglDkOJe/1AcIU3tP1I7P4Z4ToNUa1sh5oP IgASiPz/eflAhJqFmACGDIta06IZ91dQugzHy1gAX3aDG/VzwsWmbSsdFn95lptE4qRv pzvQ== X-Gm-Message-State: AOAM530FKubg9QTNj3+m+uTfsLrtUwzUs0GGNQJ2wBVdOk/81HUew8Yt 2KU/6bgyAUFxz7v1OIAOsok= X-Received: by 2002:a17:902:d48a:b0:13f:4411:8ae9 with SMTP id c10-20020a170902d48a00b0013f44118ae9mr4407316plg.86.1634812549442; Thu, 21 Oct 2021 03:35:49 -0700 (PDT) Received: from localhost.localdomain ([156.146.35.76]) by smtp.gmail.com with ESMTPSA id z1sm5872156pfh.150.2021.10.21.03.35.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 03:35:48 -0700 (PDT) From: William Breathitt Gray To: gregkh@linuxfoundation.org Cc: david@lechnology.com, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, William Breathitt Gray Subject: [PATCH] counter: Fix use-after-free race condition for events_queue_size write Date: Thu, 21 Oct 2021 19:35:40 +0900 Message-Id: <20211021103540.955639-1-vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A race condition is possible when writing to events_queue_size where the events kfifo is freed during the execution of a kfifo_in(), resulting in a use-after-free. This patch prevents such a scenario by protecting the events queue in operation with a spinlock and locking before performing the events queue size adjustment. The existing events_lock mutex is renamed to events_out_lock to reflect that it only protects events queue out operations. Because the events queue in operations can occur in an interrupt context, a new events_in_lock spinlock is introduced and utilized. Fixes: feff17a550c7 ("counter: Implement events_queue_size sysfs attribute") Cc: David Lechner Signed-off-by: William Breathitt Gray --- drivers/counter/counter-chrdev.c | 10 ++++++---- drivers/counter/counter-sysfs.c | 7 +++++++ include/linux/counter.h | 6 ++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/counter/counter-chrdev.c b/drivers/counter/counter-chrdev.c index b747dc81c..afa41c517 100644 --- a/drivers/counter/counter-chrdev.c +++ b/drivers/counter/counter-chrdev.c @@ -82,10 +82,10 @@ static ssize_t counter_chrdev_read(struct file *filp, char __user *buf, return -ENODEV; } - if (mutex_lock_interruptible(&counter->events_lock)) + if (mutex_lock_interruptible(&counter->events_out_lock)) return -ERESTARTSYS; err = kfifo_to_user(&counter->events, buf, len, &copied); - mutex_unlock(&counter->events_lock); + mutex_unlock(&counter->events_out_lock); if (err < 0) return err; } while (!copied); @@ -437,7 +437,8 @@ int counter_chrdev_add(struct counter_device *const counter) spin_lock_init(&counter->events_list_lock); mutex_init(&counter->n_events_list_lock); init_waitqueue_head(&counter->events_wait); - mutex_init(&counter->events_lock); + spin_lock_init(&counter->events_in_lock); + mutex_init(&counter->events_out_lock); /* Initialize character device */ cdev_init(&counter->chrdev, &counter_fops); @@ -560,7 +561,8 @@ void counter_push_event(struct counter_device *const counter, const u8 event, ev.watch.component = comp_node->component; ev.status = -counter_get_data(counter, comp_node, &ev.value); - copied += kfifo_in(&counter->events, &ev, 1); + copied += kfifo_in_spinlocked_noirqsave(&counter->events, &ev, + 1, &counter->events_in_lock); } exit_early: diff --git a/drivers/counter/counter-sysfs.c b/drivers/counter/counter-sysfs.c index 8c2d7c29e..03564c3af 100644 --- a/drivers/counter/counter-sysfs.c +++ b/drivers/counter/counter-sysfs.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -797,6 +799,7 @@ static int counter_events_queue_size_write(struct counter_device *counter, { DECLARE_KFIFO_PTR(events, struct counter_event); int err; + unsigned long flags; /* Allocate new events queue */ err = kfifo_alloc(&events, val, GFP_KERNEL); @@ -804,8 +807,12 @@ static int counter_events_queue_size_write(struct counter_device *counter, return err; /* Swap in new events queue */ + mutex_lock(&counter->events_out_lock); + spin_lock_irqsave(&counter->events_in_lock, flags); kfifo_free(&counter->events); counter->events.kfifo = events.kfifo; + spin_unlock_irqrestore(&counter->events_in_lock, flags); + mutex_unlock(&counter->events_out_lock); return 0; } diff --git a/include/linux/counter.h b/include/linux/counter.h index 0fd99e255..b7d0a00a6 100644 --- a/include/linux/counter.h +++ b/include/linux/counter.h @@ -296,7 +296,8 @@ struct counter_ops { * @n_events_list_lock: lock to protect Counter next events list operations * @events: queue of detected Counter events * @events_wait: wait queue to allow blocking reads of Counter events - * @events_lock: lock to protect Counter events queue read operations + * @events_in_lock: lock to protect Counter events queue in operations + * @events_out_lock: lock to protect Counter events queue out operations * @ops_exist_lock: lock to prevent use during removal */ struct counter_device { @@ -323,7 +324,8 @@ struct counter_device { struct mutex n_events_list_lock; DECLARE_KFIFO_PTR(events, struct counter_event); wait_queue_head_t events_wait; - struct mutex events_lock; + spinlock_t events_in_lock; + struct mutex events_out_lock; struct mutex ops_exist_lock; }; base-commit: 8135cc5b270b3f224615bdee8bd7d66afee87991 -- 2.33.1