From patchwork Wed Jun 23 01:38:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Chancellor X-Patchwork-Id: 12338817 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DAE5C2B9F4 for ; Wed, 23 Jun 2021 01:38:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 38BBF61375 for ; Wed, 23 Jun 2021 01:38:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229794AbhFWBlL (ORCPT ); Tue, 22 Jun 2021 21:41:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:36306 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229751AbhFWBlL (ORCPT ); Tue, 22 Jun 2021 21:41:11 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2F2CA610A0; Wed, 23 Jun 2021 01:38:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624412334; bh=tYMcg0A6FJf1k3FBz4ypXl/o/qa0iWWfHJFijI/1IVw=; h=From:To:Cc:Subject:Date:From; b=uIr9F5Lyaoq60Bqi+omXqpIj5pbfw2wbaVSYCUmslV6JcQnd6y+BfiDUG/EgT9bHi pc/P+mbrKTmojx+UUZgptzALVoTrs2AE14tyf7shwPFvU9/l17ozqB4a+lUKWRzXhB VqNOoOVzvxT1icBNst3mWou4IvfGGMrjleWqEIc2SeQdOkgyhdDIPC6dzWZdwb7hQg LgdVvR3XKPWlkVKL1dA2g01PC6/AYopHBQoaiwp50TCGQ151FhHNuADgaDXwXFiVYY FK/UloIOC0X4XVMRCppak2ieyvadK5Y2ahOmfuv3wSouEYa5L+plULA0IHaiI+G1pG YZadOlnW3ecYQ== From: Nathan Chancellor To: "Rafael J. Wysocki" , Len Brown Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Nick Desaulniers , Sami Tolvanen , Kees Cook , clang-built-linux@googlegroups.com, Nathan Chancellor Subject: [PATCH 1/2] ACPI: bgrt: Fix CFI violation Date: Tue, 22 Jun 2021 18:38:01 -0700 Message-Id: <20210623013802.1904951-1-nathan@kernel.org> X-Mailer: git-send-email 2.32.0.93.g670b81a890 MIME-Version: 1.0 X-Patchwork-Bot: notify Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org clang's Control Flow Integrity requires that every indirect call has a valid target, which is based on the type of the function pointer. The *_show() functions in this file are written as if they will be called from dev_attr_show(); however, they will be called from sysfs_kf_seq_show() because the files were created by sysfs_create_group() and the sysfs ops are based on kobj_sysfs_ops because of kobject_add_and_create(). Because the *_show() functions do not match the type of the show() member in struct kobj_attribute, there is a CFI violation. $ cat /sys/firmware/acpi/bgrt/{status,type,version,{x,y}offset}} 1 0 1 522 307 $ dmesg | grep "CFI failure" [ 267.761825] CFI failure (target: type_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762246] CFI failure (target: xoffset_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762584] CFI failure (target: status_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.762973] CFI failure (target: yoffset_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): [ 267.763330] CFI failure (target: version_show.d5e1ad21498a5fd14edbc5c320906598.cfi_jt+0x0/0x8): Convert these functions to the type of the show() member in struct kobj_attribute so that there is no more CFI violation. Because these functions are all so similar, combine them into a macro. Fixes: d1ff4b1cdbab ("ACPI: Add support for exposing BGRT data") Link: https://github.com/ClangBuiltLinux/linux/issues/1406 Signed-off-by: Nathan Chancellor Reviewed-by: Kees Cook --- drivers/acpi/bgrt.c | 57 ++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 39 deletions(-) base-commit: a51c80057a887e0f24bd8303b0791a130ff04121 diff --git a/drivers/acpi/bgrt.c b/drivers/acpi/bgrt.c index 19bb7f870204..e0d14017706e 100644 --- a/drivers/acpi/bgrt.c +++ b/drivers/acpi/bgrt.c @@ -15,40 +15,19 @@ static void *bgrt_image; static struct kobject *bgrt_kobj; -static ssize_t version_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab.version); -} -static DEVICE_ATTR_RO(version); - -static ssize_t status_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab.status); -} -static DEVICE_ATTR_RO(status); - -static ssize_t type_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab.image_type); -} -static DEVICE_ATTR_RO(type); - -static ssize_t xoffset_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab.image_offset_x); -} -static DEVICE_ATTR_RO(xoffset); - -static ssize_t yoffset_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab.image_offset_y); -} -static DEVICE_ATTR_RO(yoffset); +#define BGRT_SHOW(_name, _member) \ + static ssize_t _name##_show(struct kobject *kobj, \ + struct kobj_attribute *attr, char *buf) \ + { \ + return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab._member); \ + } \ + struct kobj_attribute bgrt_attr_##_name = __ATTR_RO(_name) + +BGRT_SHOW(version, version); +BGRT_SHOW(status, status); +BGRT_SHOW(type, image_type); +BGRT_SHOW(xoffset, image_offset_x); +BGRT_SHOW(yoffset, image_offset_y); static ssize_t image_read(struct file *file, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t count) @@ -60,11 +39,11 @@ static ssize_t image_read(struct file *file, struct kobject *kobj, static BIN_ATTR_RO(image, 0); /* size gets filled in later */ static struct attribute *bgrt_attributes[] = { - &dev_attr_version.attr, - &dev_attr_status.attr, - &dev_attr_type.attr, - &dev_attr_xoffset.attr, - &dev_attr_yoffset.attr, + &bgrt_attr_version.attr, + &bgrt_attr_status.attr, + &bgrt_attr_type.attr, + &bgrt_attr_xoffset.attr, + &bgrt_attr_yoffset.attr, NULL, }; From patchwork Wed Jun 23 01:38:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Chancellor X-Patchwork-Id: 12338819 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEC7AC48BE5 for ; Wed, 23 Jun 2021 01:38:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AE66F61375 for ; Wed, 23 Jun 2021 01:38:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230157AbhFWBlP (ORCPT ); Tue, 22 Jun 2021 21:41:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:36420 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229751AbhFWBlO (ORCPT ); Tue, 22 Jun 2021 21:41:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4A7D76137D; Wed, 23 Jun 2021 01:38:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624412338; bh=GPF0WU0fX1LOCzKrpyTOeYxkBOMEHDtAKOkeYmjfo8E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A1YI5HnYTyeZXNeF9zX3FAqL/vQJN7eiCcEDBH9KGqUjJxO+WbJpvD7fT8vesUzCX xbKr9ms5/f/ydCqnGR+Yv7XwGyiOymt61xUvbWkqzDYBhz3vs5eA+Rcg64rlptTJ1z 422vq7MHqXonOZMZXS2K1ijVwAtJt9N0zwaHMvCZXsemu9xZR3+fFti35SczbsBVL7 tKU2knxojUf9YNZwRGiQXjPQ7QPsavjeBqT92vjU3Wrz63OghvZFsGst2+LnXBb+OO 1vTc9kwT9E/YMajGshwOSNw0s8ED5V8TMJ8Tb3/GT9eC15DnBFzfc34BAEVcIRUxoB WhgKdoIaNMN2A== From: Nathan Chancellor To: "Rafael J. Wysocki" , Len Brown Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Nick Desaulniers , Sami Tolvanen , Kees Cook , clang-built-linux@googlegroups.com, Nathan Chancellor Subject: [PATCH 2/2] ACPI: bgrt: Use sysfs_emit Date: Tue, 22 Jun 2021 18:38:02 -0700 Message-Id: <20210623013802.1904951-2-nathan@kernel.org> X-Mailer: git-send-email 2.32.0.93.g670b81a890 In-Reply-To: <20210623013802.1904951-1-nathan@kernel.org> References: <20210623013802.1904951-1-nathan@kernel.org> MIME-Version: 1.0 X-Patchwork-Bot: notify Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org sysfs_emit is preferred to snprintf for emitting values after commit 2efc459d06f1 ("sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs output"). Signed-off-by: Nathan Chancellor Reviewed-by: Kees Cook --- drivers/acpi/bgrt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/acpi/bgrt.c b/drivers/acpi/bgrt.c index e0d14017706e..02d208732f9a 100644 --- a/drivers/acpi/bgrt.c +++ b/drivers/acpi/bgrt.c @@ -19,7 +19,7 @@ static struct kobject *bgrt_kobj; static ssize_t _name##_show(struct kobject *kobj, \ struct kobj_attribute *attr, char *buf) \ { \ - return snprintf(buf, PAGE_SIZE, "%d\n", bgrt_tab._member); \ + return sysfs_emit(buf, "%d\n", bgrt_tab._member); \ } \ struct kobj_attribute bgrt_attr_##_name = __ATTR_RO(_name)