From patchwork Tue Jul 9 18:44:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuxuan Luo X-Patchwork-Id: 1958572 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WJVJs2xDqz1xrJ for ; Wed, 10 Jul 2024 04:44:28 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sRFp1-0003ZD-SG; Tue, 09 Jul 2024 18:44:15 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sRFp0-0003YV-7N for kernel-team@lists.ubuntu.com; Tue, 09 Jul 2024 18:44:14 +0000 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id AA1043F0DE for ; Tue, 9 Jul 2024 18:44:13 +0000 (UTC) Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6b07ef34bfcso851926d6.1 for ; Tue, 09 Jul 2024 11:44:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720550652; x=1721155452; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kAHM9Gsju/RT2ykpZwrF7tlQSzDevgutxvTbZpCfsx4=; b=XwfS1wvt+2sYVxaAG8aSCEx8qcSt9ySKQkTH/IRVhoAPRPqw+weNJ3jTo3t6qbmw9I 6rnOVtjd+/gFQVmGjFqKcjiDaaHHCfTFKoQEapdtlj5ILpJmHdJJlEAQ8XIQUf3g9BvI SsdVXccbf34uNwaotbs12OSSHW5ryxPCQnPX50QpBLFOwXKVRTWQfab8snEc+wHkKKeV bOrHI6F8WwvfpAh1v451WusseyWGumJJNDVGQe2ASLYmW2cUJvORhQxpsb58Rgd1smgv on5GluJT1XFJFeGckCq4596brJVeE697gJM8oFycwJaYsb3jA+ifGZqEx2YocvyCQZL7 yD0g== X-Gm-Message-State: AOJu0YwW4g6/34LsDL+eHOIAE9uvSCtytK3xryXd9m6l0lOi7j2f/NIa g3r0c68RTwdlPfYrNswrt242CX93IzNNOW15awIWsnxiyByMydMoPa1AbE+FfjqzHRs1KZh68Ba xVViQSVfWFZD4hAZJtDgE9Xbsr3aBDS0N+XyZYqiD/hXJjdiwGIFyR3MtttUcDp+708Nh6W/uAW Rnj/C6FrIhHw== X-Received: by 2002:a05:6214:1ccb:b0:6b5:2f57:1a63 with SMTP id 6a1803df08f44-6b61d9d3b96mr45322946d6.21.1720550651629; Tue, 09 Jul 2024 11:44:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5Opc6LXHEpuDVWRjxC9InjaZJFLScJiBSmDYk4//Y6iEiceuAzfMRialRMhC2mS5iMRkoEw== X-Received: by 2002:a05:6214:1ccb:b0:6b5:2f57:1a63 with SMTP id 6a1803df08f44-6b61d9d3b96mr45322686d6.21.1720550651334; Tue, 09 Jul 2024 11:44:11 -0700 (PDT) Received: from cache-ubuntu.hsd1.nj.comcast.net ([2001:67c:1562:8007::aac:4795]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b61ba8c9f1sm10960206d6.123.2024.07.09.11.44.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 11:44:10 -0700 (PDT) From: Yuxuan Luo To: kernel-team@lists.ubuntu.com Subject: [PATCH 1/1] Bluetooth: Fix atomicity violation in {min, max}_key_size_set Date: Tue, 9 Jul 2024 14:44:08 -0400 Message-Id: <20240709184408.2020719-2-yuxuan.luo@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240709184408.2020719-1-yuxuan.luo@canonical.com> References: <20240709184408.2020719-1-yuxuan.luo@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Gui-Dong Han <2045gemini@gmail.com> In min_key_size_set(): if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE) return -EINVAL; hci_dev_lock(hdev); hdev->le_min_key_size = val; hci_dev_unlock(hdev); In max_key_size_set(): if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size) return -EINVAL; hci_dev_lock(hdev); hdev->le_max_key_size = val; hci_dev_unlock(hdev); The atomicity violation occurs due to concurrent execution of set_min and set_max funcs.Consider a scenario where setmin writes a new, valid 'min' value, and concurrently, setmax writes a value that is greater than the old 'min' but smaller than the new 'min'. In this case, setmax might check against the old 'min' value (before acquiring the lock) but write its value after the 'min' has been updated by setmin. This leads to a situation where the 'max' value ends up being smaller than the 'min' value, which is an inconsistency. This possible bug is found by an experimental static analysis tool developed by our team, BassCheck[1]. This tool analyzes the locking APIs to extract function pairs that can be concurrently executed, and then analyzes the instructions in the paired functions to identify possible concurrency bugs including data races and atomicity violations. The above possible bug is reported when our tool analyzes the source code of Linux 5.17. To resolve this issue, it is suggested to encompass the validity checks within the locked sections in both set_min and set_max funcs. The modification ensures that the validation of 'val' against the current min/max values is atomic, thus maintaining the integrity of the settings. With this patch applied, our tool no longer reports the bug, with the kernel configuration allyesconfig for x86_64. Due to the lack of associated hardware, we cannot test the patch in runtime testing, and just verify it according to the code logic. [1] https://sites.google.com/view/basscheck/ Fixes: 18f81241b74f ("Bluetooth: Move {min,max}_key_size debugfs ...") Cc: stable@vger.kernel.org Signed-off-by: Gui-Dong Han <2045gemini@gmail.com> Signed-off-by: Luiz Augusto von Dentz (backported from commit da9065caa594d19b26e1a030fd0cc27bd365d685) [yuxuan.luo: Although the break commit is not included in the tree, we believe that the race condition has long existed since the introduction of le_min_key_size_write() and le_max_key_size_write() which were later renamed to {min,max}_key_size_set(). Therefore, I manually applied the fix made for hci_debugfs.c to smp.c since 18f81241b74f ("Bluetooth: Move {min,max}_key_size debugfs into hci_debugfs_create_le") is yet to be introduced. ] CVE-2024-24860 Signed-off-by: Yuxuan Luo --- net/bluetooth/smp.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index 4d5ba9778885d..2b0163abd2cfb 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -3398,11 +3398,15 @@ static ssize_t le_min_key_size_write(struct file *file, sscanf(buf, "%hhu", &key_size); + hci_dev_lock(hdev); if (key_size > hdev->le_max_key_size || - key_size < SMP_MIN_ENC_KEY_SIZE) + key_size < SMP_MIN_ENC_KEY_SIZE) { + hci_dev_unlock(hdev); return -EINVAL; + } hdev->le_min_key_size = key_size; + hci_dev_unlock(hdev); return count; } @@ -3442,11 +3446,15 @@ static ssize_t le_max_key_size_write(struct file *file, sscanf(buf, "%hhu", &key_size); + hci_dev_lock(hdev); if (key_size > SMP_MAX_ENC_KEY_SIZE || - key_size < hdev->le_min_key_size) + key_size < hdev->le_min_key_size) { + hci_dev_unlock(hdev); return -EINVAL; + } hdev->le_max_key_size = key_size; + hci_dev_unlock(hdev); return count; }