Message ID | 20221101223321.1326815-5-keescook@chromium.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3247603wru; Tue, 1 Nov 2022 15:35:24 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4LJxGfh87MUcgxliuQrFgcFllrNcRfUf6cq5mAlys24vRWUy06eK33M5UerNu5uDL/HIUD X-Received: by 2002:a17:907:6d23:b0:78d:8f0f:1266 with SMTP id sa35-20020a1709076d2300b0078d8f0f1266mr20719941ejc.762.1667342124600; Tue, 01 Nov 2022 15:35:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667342124; cv=none; d=google.com; s=arc-20160816; b=gZhL9hihL2RIl/wGKRj+yWB53FpmbvFzcBiPH+JbDFnjh5O2UiHp5FYJm5GhGeQQiC VrOWsO50BeDMJcIlw4EScfEdeXPh1NQ0FAhlKqIo2axhEeRQXLlZfBty8A2zWl6nFVAU z6/E/s2eA/fQsjj40jB26UR0kqbpP6nPA75YF1kjm6i8DjEtWWQaIAkXmzjm6BaNerjz ktMRtW5VQHTFO4VD+V/UMn5S/RePwI9HjeXSLugyiV27Zy2SplQiTkpI8jGH1OlZ6hSi riG3WAxi4UswSpPc6eD8Ix+ifP2AqjDDH8skJYY8AGd4hMjcUannl42NBSK0WjJ2tE/P nO8w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gzM6J7UGnorFDlDysY0MD6P7BjaP+6EbrbAqldiRrKE=; b=Uk7SjJfi7MoKd4zB7Dex0rY6HDRioHt4DdNTpOznAgQ1w3zXwCEwCZLkYFgK619kWV Jk6GGhaa5Nj2ZEtpOe/k9/vmvSap32syg024ryq9rpwNI80rqFsx3OKlqRqZzNEZLPXM XPaMbQabx2hehi6mgk4LA0/mtQ8DEx6NA/kEMx4VvedWnxgowyqR/QUHIGeu3FCYMAQq 7S4l4zagLYWvyjs1AfBrHpsIsSjCkO0VAFmKnjXcG+/7uA52ZI2IcoASdsP+ak5XrQX0 jvZgBFJyeMW7ffvSWcIZT7haqUFJEM0bM83OlCk940a3hJrqYI7sBHU0TVeYp4wh2ciD sPXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OKuDa6YS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t12-20020a056402240c00b0046153dd47f0si12021372eda.441.2022.11.01.15.35.00; Tue, 01 Nov 2022 15:35:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OKuDa6YS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231149AbiKAWeS (ORCPT <rfc822;kartikey406@gmail.com> + 99 others); Tue, 1 Nov 2022 18:34:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230388AbiKAWd2 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 1 Nov 2022 18:33:28 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D2AC2192 for <linux-kernel@vger.kernel.org>; Tue, 1 Nov 2022 15:33:25 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id 130so14775170pfu.8 for <linux-kernel@vger.kernel.org>; Tue, 01 Nov 2022 15:33:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gzM6J7UGnorFDlDysY0MD6P7BjaP+6EbrbAqldiRrKE=; b=OKuDa6YSbANP/NsNkIWjApa9dbOPZfAN/KtOhEl7y//1+lLuJ6c0t6cE9j/ifht6Vy 10XhU7TJYo8HMJE/VscMRVZgUI2flZNTZY+0daayeJydtHb94FxLwQJGgZ0qCpAcBHPl 7eIkUxKRPJyW+uxeOKrY9C5oPis/Bie6gRUZA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gzM6J7UGnorFDlDysY0MD6P7BjaP+6EbrbAqldiRrKE=; b=aDfK9/yZTMH2AB8kyDYCYVkBXxC7ji49JXL8MK5npJqmZMEWw2w4MjMpUJg7q+c/ZG ER8Cs+esxnwckAgeTvTdUGioAbhH7p3bArimJ3Vj2DLGpUcQJMN+zdGKuas67lV8jgF+ 5+jzEMc/GlfsXW+UfGwX0Z98B2TcUioZrX2+5k2n/XagJa61TTL2QL2m01pj/twsdt7V p54UdV6zcxUoTSAj9ZJM6dfnueNbgYKzwa4FHOi2JbY8MFWul5yy8stxe65Pwse9HWC0 18JWa2cki9atAGihpTBDtt0lWjXBUZ5Gaep+89+BoMqWYI7INS/0xssNcxmPWWTUjvo3 85KA== X-Gm-Message-State: ACrzQf2u1/zncORSGazEAmVJALxIlkDLok77VS6o9NX5KpWWaSTAeEIX TkcB9+ycSNnVJZIjAnGcALMgSA== X-Received: by 2002:a63:950d:0:b0:46e:d655:b377 with SMTP id p13-20020a63950d000000b0046ed655b377mr19259111pgd.191.1667342004776; Tue, 01 Nov 2022 15:33:24 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id s7-20020a170902988700b001811a197797sm6778934plp.194.2022.11.01.15.33.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:24 -0700 (PDT) From: Kees Cook <keescook@chromium.org> To: Vlastimil Babka <vbabka@suse.cz> Cc: Kees Cook <keescook@chromium.org>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Rasmus Villemoes <rasmus.villemoes@prevas.dk>, Thomas Gleixner <tglx@linutronix.de>, Jason Gunthorpe <jgg@ziepe.ca>, Nishanth Menon <nm@ti.com>, Michael Kelley <mikelley@microsoft.com>, Dan Williams <dan.j.williams@intel.com>, Won Chung <wonchung@google.com>, David Gow <davidgow@google.com>, Christoph Lameter <cl@linux.com>, Pekka Enberg <penberg@kernel.org>, David Rientjes <rientjes@google.com>, Joonsoo Kim <iamjoonsoo.kim@lge.com>, Roman Gushchin <roman.gushchin@linux.dev>, Hyeonggon Yoo <42.hyeyoo@gmail.com>, Guenter Roeck <linux@roeck-us.net>, Andy Shevchenko <andriy.shevchenko@intel.com>, Paolo Abeni <pabeni@redhat.com>, Geert Uytterhoeven <geert@linux-m68k.org>, Nathan Chancellor <nathan@kernel.org>, Nick Desaulniers <ndesaulniers@google.com>, Tom Rix <trix@redhat.com>, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 5/6] driver core: Add __alloc_size hint to devm allocators Date: Tue, 1 Nov 2022 15:33:13 -0700 Message-Id: <20221101223321.1326815-5-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2391; h=from:subject; bh=aUB9QFgSGPW6FN3jHA0HE05KQ7SqHyl2yIuilOndYc8=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6qr8iT9TeVIzVavaub9kYVPpos80os4w71z521 I5fSEqiJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqgAKCRCJcvTf3G3AJsI+EA CCoCqTQVECoE72alYcrcqBPY9Z20kQHjcfMq2clHkeoyFdKB7/Ud8kY1NoGi9q0m7Xda+lbswp0Jlt WBofqN/HFcFfrXp3FK8cKt+E6z83q0MQ0tPoIJ8VrbX4/wOJvdwTMD01BGiRQ0CbVNKJ7r2fSpRHS8 6wR/YVw5UWOYYryexrUjU3p7shnbVQ78CHVuSyCWWBAbvDwjqN1kQqmGB/xhqF30AtrLCJZeaXlMHI 6OlrqKUn+Izi77UJ7rrqoS6x931kwNHbO1+ZBj/lU9O5yZ7ff8PbhX3q5SJhLRKIVdaADnYGIqt296 LRbxLOfFTiDPd8DdumdGCzmXLZaUhS5Ef1GlGQJ9Ofz+SqZK6WjgYaw/RpelAbMhqf3Pa7NAUSRQ/T 0UZSTeVpU8AwIRrAhLh5s2zUneuHm2F3vZUdRjgBPdXjFW3Q8FsuKiDApeD5UShVJ5L64NL7K2I83F thVgiY7h7bGlpRRORua5rQEQLSEg4vsYiKvvUMbRLIuiacpWqT9b0zk2fLyBRESTF5/gChxZkF3d2S w2P/b+Zmj4VAqX324oLiSUkdASYfidRxN3jN1y+7w1rLl9h/VDvCFRZbbLicPvvMiNK9V6KClVIOZs yZWijWPTj3w0TMU+Cpgdq8ksSrwHICryAxiyTeUEHR3ni/aE+UAIMm7je+Vw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748334935601828173?= X-GMAIL-MSGID: =?utf-8?q?1748334935601828173?= |
Series |
slab: Provide full coverage for __alloc_size attribute
|
|
Commit Message
Kees Cook
Nov. 1, 2022, 10:33 p.m. UTC
Mark the devm_*alloc()-family of allocations with appropriate __alloc_size()/__realloc_size() hints so the compiler can attempt to reason about buffer lengths from allocations. Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Nishanth Menon <nm@ti.com> Cc: Michael Kelley <mikelley@microsoft.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Won Chung <wonchung@google.com> Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20221029074734.gonna.276-kees@kernel.org --- This is already in -next, but I'm including it here again to avoid any confusion about this series landing (or being tested) via another tree. --- include/linux/device.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
Comments
Hi, Kees This change causes "Kernel panic - not syncing: BRK handler: Fatal exception" for the android-mainline based hikey960 build, with this commit reverted, there is no problem for the build to boot to the homescreen. Not sure if you have any idea about it and give some suggestions. Here is part of the kernel panic log: [ 9.479878][ T122] ueventd: Loading module /vendor/lib/modules/spi-pl022.ko with args '' [ 9.480276][ T115] apexd-bootstrap: Pre-allocated loop device 29 [ 9.480517][ T123] ueventd: LoadWithAliases was unable to load of:Nhi3660_i2sT(null)Chisilicon,hi3660-i2s-1.0 [ 9.480632][ T121] Unexpected kernel BRK exception at EL1 [ 9.480637][ T121] Internal error: BRK handler: 00000000f2000001 [#1] PREEMPT SMP [ 9.480644][ T121] Modules linked in: cpufreq_dt(E+) hisi_thermal(E+) phy_hi3660_usb3(E) btqca(E) hi6421_pmic_core(E) btbcm(E) spi_pl022(E) hi3660_mailbox(E) i2c_designware_platform(E) mali_kbase(OE) dw_mmc_k3(E) bluetooth(E) dw_mmc_pltfm(E) dw_mmc(E) kirin_drm(E) rfkill(E) kirin_dsi(E) i2c_designware_core(E) k3dma(E) drm_dma_helper(E) cma_heap(E) system_heap(E) [ 9.480688][ T121] CPU: 4 PID: 121 Comm: ueventd Tainted: G OE 6.2.0-rc6-mainline-14196-g1d9f94ec75b9 #1 [ 9.480694][ T121] Hardware name: HiKey960 (DT) [ 9.480697][ T121] pstate: 20400005 (nzCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) [ 9.480703][ T121] pc : hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] [ 9.480722][ T121] lr : hi3660_thermal_probe+0x38/0x74 [hisi_thermal] [ 9.480733][ T121] sp : ffffffc00aa13700 [ 9.480735][ T121] x29: ffffffc00aa13700 x28: 0000007ff8ae8531 x27: 00000000000008c0 [ 9.480743][ T121] x26: ffffffc00aa2a300 x25: ffffffc00aa2ab40 x24: 000000000000001d [ 9.480749][ T121] x23: ffffffc00a29d000 x22: 0000000000000000 x21: ffffff8001fa4a80 [ 9.480755][ T121] x20: 0000000000000001 x19: ffffff8001fa4a80 x18: ffffffc00a8810b0 [ 9.480761][ T121] x17: 000000007ab542f2 x16: 000000007ab542f2 x15: ffffffc00aa01000 [ 9.480767][ T121] x14: ffffffc00966f250 x13: ffffffc0b58f9000 x12: ffffffc00a055f10 [ 9.480771][ T123] ueventd: LoadWithAliases was unable to load cpu:type:aarch64:feature:,0000,0001,0002,0003,0004,0005,0006,0007,000B [ 9.480773][ T121] [ 9.480774][ T121] x11: 0000000000000000 x10: 0000000000000001 x9 : 0000000100000000 [ 9.480780][ T123] ueventd: [ 9.480780][ T121] x8 : ffffffc0044154cb x7 : 0000000000000000 x6 : 000000000000003f [ 9.480786][ T121] x5 : 0000000000000020 x4 : ffffffc0098db323 x3 : ffffff801aeb62c0 [ 9.480792][ T121] x2 : ffffff801aeb62c0 x1 : 0000000000000000 x0 : ffffff8001fa4c80 [ 9.480798][ T121] Call trace: [ 9.480801][ T121] hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] [ 9.480813][ T121] hisi_thermal_probe+0xbc/0x284 [hisi_thermal] [ 9.480823][ T121] platform_probe+0xcc/0xf8 [ 9.480836][ T121] really_probe+0x19c/0x390 [ 9.480842][ T121] __driver_probe_device+0xc0/0xf0 [ 9.480848][ T121] driver_probe_device+0x4c/0x228 [ 9.480853][ T121] __driver_attach+0x110/0x1e0 [ 9.480858][ T121] bus_for_each_dev+0xa0/0xf4 [ 9.480864][ T121] driver_attach+0x2c/0x40 [ 9.480868][ T121] bus_add_driver+0x118/0x208 [ 9.480873][ T121] driver_register+0x80/0x124 [ 9.480878][ T121] __platform_driver_register+0x2c/0x40 [ 9.480884][ T121] init_module+0x28/0xfe4 [hisi_thermal] [ 9.480895][ T121] do_one_initcall+0xe4/0x334 [ 9.480902][ T121] do_init_module+0x50/0x1f0 [ 9.480909][ T121] load_module+0x1034/0x1204 [ 9.480914][ T121] __arm64_sys_finit_module+0xc8/0x11c [ 9.480919][ T121] invoke_syscall+0x60/0x130 [ 9.480926][ T121] el0_svc_common+0xbc/0x100 [ 9.480931][ T121] do_el0_svc+0x38/0xc4 [ 9.480937][ T121] el0_svc+0x34/0xc4 [ 9.480945][ T121] el0t_64_sync_handler+0x8c/0xfc [ 9.480950][ T121] el0t_64_sync+0x1a4/0x1a8 [ 9.480957][ T121] Code: 91132d08 b9001814 f9000013 f9000808 (d4200020) [ 9.480960][ T121] ---[ end trace 0000000000000000 ]--- [ 9.482201][ T72] dwmmc_k3 ff37f000.dwmmc1: IDMAC supports 64-bit address mode. [ 9.482225][ T72] dwmmc_k3 ff37f000.dwmmc1: Using internal DMA controller. [ 9.482232][ T72] dwmmc_k3 ff37f000.dwmmc1: Version ID is 270a [ 9.482261][ T72] dwmmc_k3 ff37f000.dwmmc1: DW MMC controller at irq 72,32 bit host data width,128 deep fifo [ 9.482406][ T117] cpu cpu0: EM: created perf domain [ 9.482677][ T118] ueventd: Loaded kernel module /vendor/lib/modules/btqca.ko [ 9.482745][ T118] ueventd: Loading module /vendor/lib/modules/hci_uart.ko with args '' [ 9.483117][ T117] cpu cpu4: EM: created perf domain [ 9.483767][ T117] ueventd: Loaded kernel module /vendor/lib/modules/cpufreq-dt.ko [ 9.484265][ T72] dwmmc_k3 ff37f000.dwmmc1: fifo-depth property not found, using value of FIFOTH register as default [ 9.484326][ T117] ueventd: LoadWithAliases was unable to load cpu:type:aarch64:feature:,0000,0001,0002,0003,0004,0005,0006,0007,000B [ 9.484335][ T117] ueventd: [ 9.486508][ T72] dwmmc_k3 ff37f000.dwmmc1: IDMAC supports 64-bit address mode. [ 9.486564][ T72] dwmmc_k3 ff37f000.dwmmc1: Using internal DMA controller. [ 9.486572][ T72] dwmmc_k3 ff37f000.dwmmc1: Version ID is 270a [ 9.486620][ T72] dwmmc_k3 ff37f000.dwmmc1: DW MMC controller at irq 72,32 bit host data width,64 deep fifo [ 9.488281][ T121] Kernel panic - not syncing: BRK handler: Fatal exception for the full serial console log, please check here: http://ix.io/4mLg Thanks, Yongqin Liu On Wed, 2 Nov 2022 at 06:34, Kees Cook <keescook@chromium.org> wrote: > > Mark the devm_*alloc()-family of allocations with appropriate > __alloc_size()/__realloc_size() hints so the compiler can attempt to > reason about buffer lengths from allocations. > > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> > Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk> > Cc: Thomas Gleixner <tglx@linutronix.de> > Cc: Jason Gunthorpe <jgg@ziepe.ca> > Cc: Nishanth Menon <nm@ti.com> > Cc: Michael Kelley <mikelley@microsoft.com> > Cc: Dan Williams <dan.j.williams@intel.com> > Cc: Won Chung <wonchung@google.com> > Signed-off-by: Kees Cook <keescook@chromium.org> > Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> > Link: https://lore.kernel.org/r/20221029074734.gonna.276-kees@kernel.org > --- > This is already in -next, but I'm including it here again to avoid any > confusion about this series landing (or being tested) via another tree. > --- > include/linux/device.h | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/include/linux/device.h b/include/linux/device.h > index 424b55df0272..5e4cd857e74f 100644 > --- a/include/linux/device.h > +++ b/include/linux/device.h > @@ -197,9 +197,9 @@ void devres_remove_group(struct device *dev, void *id); > int devres_release_group(struct device *dev, void *id); > > /* managed devm_k.alloc/kfree for device drivers */ > -void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc; > +void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_size(2); > void *devm_krealloc(struct device *dev, void *ptr, size_t size, > - gfp_t gfp) __must_check; > + gfp_t gfp) __must_check __realloc_size(3); > __printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp, > const char *fmt, va_list ap) __malloc; > __printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp, > @@ -226,7 +226,8 @@ static inline void *devm_kcalloc(struct device *dev, > void devm_kfree(struct device *dev, const void *p); > char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; > const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp); > -void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp); > +void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) > + __realloc_size(3); > > unsigned long devm_get_free_pages(struct device *dev, > gfp_t gfp_mask, unsigned int order); > -- > 2.34.1 >
On Tue, Jan 31, 2023 at 11:36 PM Yongqin Liu <yongqin.liu@linaro.org> wrote: > > Hi, Kees > > This change causes "Kernel panic - not syncing: BRK handler: Fatal exception" > for the android-mainline based hikey960 build, with this commit reverted, > there is no problem for the build to boot to the homescreen. > Not sure if you have any idea about it and give some suggestions. > > Here is part of the kernel panic log: > > [ 9.479878][ T122] ueventd: Loading module > /vendor/lib/modules/spi-pl022.ko with args '' > [ 9.480276][ T115] apexd-bootstrap: Pre-allocated loop device 29 > [ 9.480517][ T123] ueventd: LoadWithAliases was unable to load > of:Nhi3660_i2sT(null)Chisilicon,hi3660-i2s-1.0 > [ 9.480632][ T121] Unexpected kernel BRK exception at EL1 > [ 9.480637][ T121] Internal error: BRK handler: > 00000000f2000001 [#1] PREEMPT SMP > [ 9.480644][ T121] Modules linked in: cpufreq_dt(E+) > hisi_thermal(E+) phy_hi3660_usb3(E) btqca(E) hi6421_pmic_core(E) > btbcm(E) spi_pl022(E) hi3660_mailbox(E) i2c_designware_platform(E) > mali_kbase(OE) dw_mmc_k3(E) bluetooth(E) dw_mmc_pltfm(E) dw_mmc(E) > kirin_drm(E) rfkill(E) kirin_dsi(E) i2c_designware_core(E) k3dma(E) > drm_dma_helper(E) cma_heap(E) system_heap(E) > [ 9.480688][ T121] CPU: 4 PID: 121 Comm: ueventd Tainted: G > OE 6.2.0-rc6-mainline-14196-g1d9f94ec75b9 #1 > [ 9.480694][ T121] Hardware name: HiKey960 (DT) > [ 9.480697][ T121] pstate: 20400005 (nzCv daif +PAN -UAO -TCO > -DIT -SSBS BTYPE=--) > [ 9.480703][ T121] pc : hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] > [ 9.480722][ T121] lr : hi3660_thermal_probe+0x38/0x74 [hisi_thermal] > [ 9.480733][ T121] sp : ffffffc00aa13700 > [ 9.480735][ T121] x29: ffffffc00aa13700 x28: 0000007ff8ae8531 > x27: 00000000000008c0 > [ 9.480743][ T121] x26: ffffffc00aa2a300 x25: ffffffc00aa2ab40 > x24: 000000000000001d > [ 9.480749][ T121] x23: ffffffc00a29d000 x22: 0000000000000000 > x21: ffffff8001fa4a80 > [ 9.480755][ T121] x20: 0000000000000001 x19: ffffff8001fa4a80 > x18: ffffffc00a8810b0 > [ 9.480761][ T121] x17: 000000007ab542f2 x16: 000000007ab542f2 > x15: ffffffc00aa01000 > [ 9.480767][ T121] x14: ffffffc00966f250 x13: ffffffc0b58f9000 > x12: ffffffc00a055f10 > [ 9.480771][ T123] ueventd: LoadWithAliases was unable to load > cpu:type:aarch64:feature:,0000,0001,0002,0003,0004,0005,0006,0007,000B > [ 9.480773][ T121] > [ 9.480774][ T121] x11: 0000000000000000 x10: 0000000000000001 > x9 : 0000000100000000 > [ 9.480780][ T123] ueventd: > [ 9.480780][ T121] x8 : ffffffc0044154cb x7 : 0000000000000000 > x6 : 000000000000003f > [ 9.480786][ T121] x5 : 0000000000000020 x4 : ffffffc0098db323 > x3 : ffffff801aeb62c0 > [ 9.480792][ T121] x2 : ffffff801aeb62c0 x1 : 0000000000000000 > x0 : ffffff8001fa4c80 > [ 9.480798][ T121] Call trace: > [ 9.480801][ T121] hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] > [ 9.480813][ T121] hisi_thermal_probe+0xbc/0x284 [hisi_thermal] Taking a look here, it looks pretty obvious: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/thermal/hisi_thermal.c#n414 data->nr_sensors = 1; data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) * data->nr_sensors, GFP_KERNEL); Here as nr_sensors=1, we allocate only one structure for the array. But then below that, we modify two entries, writing past the valid array, and corrupting data when writing the second sensor values. data->sensor[0].id = HI3660_BIG_SENSOR; data->sensor[0].irq_name = "tsensor_a73"; data->sensor[0].data = data; data->sensor[1].id = HI3660_LITTLE_SENSOR; data->sensor[1].irq_name = "tsensor_a53"; data->sensor[1].data = data; I suspect nr_sensors needs to be set to 2. Nice work, Kees! thanks -john
On Wed, Feb 1, 2023 at 12:11 AM John Stultz <jstultz@google.com> wrote: > On Tue, Jan 31, 2023 at 11:36 PM Yongqin Liu <yongqin.liu@linaro.org> wrote: > > > > Hi, Kees > > > > This change causes "Kernel panic - not syncing: BRK handler: Fatal exception" > > for the android-mainline based hikey960 build, with this commit reverted, > > there is no problem for the build to boot to the homescreen. > > Not sure if you have any idea about it and give some suggestions. > > > > Here is part of the kernel panic log: > > > > [ 9.479878][ T122] ueventd: Loading module > > /vendor/lib/modules/spi-pl022.ko with args '' > > [ 9.480276][ T115] apexd-bootstrap: Pre-allocated loop device 29 > > [ 9.480517][ T123] ueventd: LoadWithAliases was unable to load > > of:Nhi3660_i2sT(null)Chisilicon,hi3660-i2s-1.0 > > [ 9.480632][ T121] Unexpected kernel BRK exception at EL1 > > [ 9.480637][ T121] Internal error: BRK handler: > > 00000000f2000001 [#1] PREEMPT SMP > > [ 9.480644][ T121] Modules linked in: cpufreq_dt(E+) > > hisi_thermal(E+) phy_hi3660_usb3(E) btqca(E) hi6421_pmic_core(E) > > btbcm(E) spi_pl022(E) hi3660_mailbox(E) i2c_designware_platform(E) > > mali_kbase(OE) dw_mmc_k3(E) bluetooth(E) dw_mmc_pltfm(E) dw_mmc(E) > > kirin_drm(E) rfkill(E) kirin_dsi(E) i2c_designware_core(E) k3dma(E) > > drm_dma_helper(E) cma_heap(E) system_heap(E) > > [ 9.480688][ T121] CPU: 4 PID: 121 Comm: ueventd Tainted: G > > OE 6.2.0-rc6-mainline-14196-g1d9f94ec75b9 #1 > > [ 9.480694][ T121] Hardware name: HiKey960 (DT) > > [ 9.480697][ T121] pstate: 20400005 (nzCv daif +PAN -UAO -TCO > > -DIT -SSBS BTYPE=--) > > [ 9.480703][ T121] pc : hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] > > [ 9.480722][ T121] lr : hi3660_thermal_probe+0x38/0x74 [hisi_thermal] > > [ 9.480733][ T121] sp : ffffffc00aa13700 > > [ 9.480735][ T121] x29: ffffffc00aa13700 x28: 0000007ff8ae8531 > > x27: 00000000000008c0 > > [ 9.480743][ T121] x26: ffffffc00aa2a300 x25: ffffffc00aa2ab40 > > x24: 000000000000001d > > [ 9.480749][ T121] x23: ffffffc00a29d000 x22: 0000000000000000 > > x21: ffffff8001fa4a80 > > [ 9.480755][ T121] x20: 0000000000000001 x19: ffffff8001fa4a80 > > x18: ffffffc00a8810b0 > > [ 9.480761][ T121] x17: 000000007ab542f2 x16: 000000007ab542f2 > > x15: ffffffc00aa01000 > > [ 9.480767][ T121] x14: ffffffc00966f250 x13: ffffffc0b58f9000 > > x12: ffffffc00a055f10 > > [ 9.480771][ T123] ueventd: LoadWithAliases was unable to load > > cpu:type:aarch64:feature:,0000,0001,0002,0003,0004,0005,0006,0007,000B > > [ 9.480773][ T121] > > [ 9.480774][ T121] x11: 0000000000000000 x10: 0000000000000001 > > x9 : 0000000100000000 > > [ 9.480780][ T123] ueventd: > > [ 9.480780][ T121] x8 : ffffffc0044154cb x7 : 0000000000000000 > > x6 : 000000000000003f > > [ 9.480786][ T121] x5 : 0000000000000020 x4 : ffffffc0098db323 > > x3 : ffffff801aeb62c0 > > [ 9.480792][ T121] x2 : ffffff801aeb62c0 x1 : 0000000000000000 > > x0 : ffffff8001fa4c80 > > [ 9.480798][ T121] Call trace: > > [ 9.480801][ T121] hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] > > [ 9.480813][ T121] hisi_thermal_probe+0xbc/0x284 [hisi_thermal] > > > Taking a look here, it looks pretty obvious: > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/thermal/hisi_thermal.c#n414 > > data->nr_sensors = 1; > data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) * > data->nr_sensors, GFP_KERNEL); > > Here as nr_sensors=1, we allocate only one structure for the array. > But then below that, we modify two entries, writing past the valid > array, and corrupting data when writing the second sensor values. > > data->sensor[0].id = HI3660_BIG_SENSOR; > data->sensor[0].irq_name = "tsensor_a73"; > data->sensor[0].data = data; > > data->sensor[1].id = HI3660_LITTLE_SENSOR; > data->sensor[1].irq_name = "tsensor_a53"; > data->sensor[1].data = data; > > I suspect nr_sensors needs to be set to 2. Looks like the bug was introduced here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7d3a2a2bbadb4bf5856ed394ba09b8fbb7a80460 But that change seems to imply the dual zones weren't fully supported at the time. I'm not sure if that's changed in the meantime, so removing the second sensor writes may potentially be a better fix. thanks -john
On Wed, Feb 01, 2023 at 12:11:41AM -0800, John Stultz wrote: > On Tue, Jan 31, 2023 at 11:36 PM Yongqin Liu <yongqin.liu@linaro.org> wrote: ... > data->nr_sensors = 1; > data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) * > data->nr_sensors, GFP_KERNEL); Side note: This should use devm_kcalloc().
On Wed, Feb 01, 2023 at 12:11:41AM -0800, John Stultz wrote: > On Tue, Jan 31, 2023 at 11:36 PM Yongqin Liu <yongqin.liu@linaro.org> wrote: > > > > Hi, Kees > > > > This change causes "Kernel panic - not syncing: BRK handler: Fatal exception" > > for the android-mainline based hikey960 build, with this commit reverted, > > there is no problem for the build to boot to the homescreen. > > Not sure if you have any idea about it and give some suggestions. > > > > Here is part of the kernel panic log: > > > > [ 9.479878][ T122] ueventd: Loading module > > /vendor/lib/modules/spi-pl022.ko with args '' > > [ 9.480276][ T115] apexd-bootstrap: Pre-allocated loop device 29 > > [ 9.480517][ T123] ueventd: LoadWithAliases was unable to load > > of:Nhi3660_i2sT(null)Chisilicon,hi3660-i2s-1.0 > > [ 9.480632][ T121] Unexpected kernel BRK exception at EL1 > > [ 9.480637][ T121] Internal error: BRK handler: > > 00000000f2000001 [#1] PREEMPT SMP > > [ 9.480644][ T121] Modules linked in: cpufreq_dt(E+) > > hisi_thermal(E+) phy_hi3660_usb3(E) btqca(E) hi6421_pmic_core(E) > > btbcm(E) spi_pl022(E) hi3660_mailbox(E) i2c_designware_platform(E) > > mali_kbase(OE) dw_mmc_k3(E) bluetooth(E) dw_mmc_pltfm(E) dw_mmc(E) > > kirin_drm(E) rfkill(E) kirin_dsi(E) i2c_designware_core(E) k3dma(E) > > drm_dma_helper(E) cma_heap(E) system_heap(E) > > [ 9.480688][ T121] CPU: 4 PID: 121 Comm: ueventd Tainted: G > > OE 6.2.0-rc6-mainline-14196-g1d9f94ec75b9 #1 > > [ 9.480694][ T121] Hardware name: HiKey960 (DT) > > [ 9.480697][ T121] pstate: 20400005 (nzCv daif +PAN -UAO -TCO > > -DIT -SSBS BTYPE=--) > > [ 9.480703][ T121] pc : hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] > > [ 9.480722][ T121] lr : hi3660_thermal_probe+0x38/0x74 [hisi_thermal] > > [ 9.480733][ T121] sp : ffffffc00aa13700 > > [ 9.480735][ T121] x29: ffffffc00aa13700 x28: 0000007ff8ae8531 > > x27: 00000000000008c0 > > [ 9.480743][ T121] x26: ffffffc00aa2a300 x25: ffffffc00aa2ab40 > > x24: 000000000000001d > > [ 9.480749][ T121] x23: ffffffc00a29d000 x22: 0000000000000000 > > x21: ffffff8001fa4a80 > > [ 9.480755][ T121] x20: 0000000000000001 x19: ffffff8001fa4a80 > > x18: ffffffc00a8810b0 > > [ 9.480761][ T121] x17: 000000007ab542f2 x16: 000000007ab542f2 > > x15: ffffffc00aa01000 > > [ 9.480767][ T121] x14: ffffffc00966f250 x13: ffffffc0b58f9000 > > x12: ffffffc00a055f10 > > [ 9.480771][ T123] ueventd: LoadWithAliases was unable to load > > cpu:type:aarch64:feature:,0000,0001,0002,0003,0004,0005,0006,0007,000B > > [ 9.480773][ T121] > > [ 9.480774][ T121] x11: 0000000000000000 x10: 0000000000000001 > > x9 : 0000000100000000 > > [ 9.480780][ T123] ueventd: > > [ 9.480780][ T121] x8 : ffffffc0044154cb x7 : 0000000000000000 > > x6 : 000000000000003f > > [ 9.480786][ T121] x5 : 0000000000000020 x4 : ffffffc0098db323 > > x3 : ffffff801aeb62c0 > > [ 9.480792][ T121] x2 : ffffff801aeb62c0 x1 : 0000000000000000 > > x0 : ffffff8001fa4c80 > > [ 9.480798][ T121] Call trace: > > [ 9.480801][ T121] hi3660_thermal_probe+0x6c/0x74 [hisi_thermal] > > [ 9.480813][ T121] hisi_thermal_probe+0xbc/0x284 [hisi_thermal] > > > Taking a look here, it looks pretty obvious: > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/thermal/hisi_thermal.c#n414 > > data->nr_sensors = 1; > data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) * > data->nr_sensors, GFP_KERNEL); > > Here as nr_sensors=1, we allocate only one structure for the array. > But then below that, we modify two entries, writing past the valid > array, and corrupting data when writing the second sensor values. > > data->sensor[0].id = HI3660_BIG_SENSOR; > data->sensor[0].irq_name = "tsensor_a73"; > data->sensor[0].data = data; > > data->sensor[1].id = HI3660_LITTLE_SENSOR; > data->sensor[1].irq_name = "tsensor_a53"; > data->sensor[1].data = data; > > I suspect nr_sensors needs to be set to 2. > > Nice work, Kees! Yay for compilers! :) Was a patch sent to fix this driver?
On Thu, Feb 2, 2023 at 9:18 AM Kees Cook <keescook@chromium.org> wrote: > On Wed, Feb 01, 2023 at 12:11:41AM -0800, John Stultz wrote: > > On Tue, Jan 31, 2023 at 11:36 PM Yongqin Liu <yongqin.liu@linaro.org> wrote: > > > This change causes "Kernel panic - not syncing: BRK handler: Fatal exception" > > > for the android-mainline based hikey960 build, with this commit reverted, > > > there is no problem for the build to boot to the homescreen. > > > Not sure if you have any idea about it and give some suggestions. > > > > > > Here is part of the kernel panic log: ... > > Here as nr_sensors=1, we allocate only one structure for the array. > > But then below that, we modify two entries, writing past the valid > > array, and corrupting data when writing the second sensor values. > > > > data->sensor[0].id = HI3660_BIG_SENSOR; > > data->sensor[0].irq_name = "tsensor_a73"; > > data->sensor[0].data = data; > > > > data->sensor[1].id = HI3660_LITTLE_SENSOR; > > data->sensor[1].irq_name = "tsensor_a53"; > > data->sensor[1].data = data; > > > > I suspect nr_sensors needs to be set to 2. > > > > Nice work, Kees! > > Yay for compilers! :) Well, I know it's not trivial to make the compilers catch these things, so yay for you and others putting in all the effort on this as well. That said, making sense of the error message isn't completely trivial either. I've been seeing a few cases recently of some of the new compiler tooling (I pinged you earlier on a CFI one) causing errors that developers aren't really sure how to address. I know sometimes it's not easy to surface the errors with context to what was wrong, but at the risk of intense bike shedding, is there some way to provide something like "Likely array bounds error" instead of just "BRK handler: Fatal exception"? > Was a patch sent to fix this driver? I think YongQin is looking into it (either setting the nr_sensors value to 2 or dropping the second sensor access). thanks -john
On Thu, Feb 02, 2023 at 10:56:29AM -0800, John Stultz wrote: > That said, making sense of the error message isn't completely trivial > either. I've been seeing a few cases recently of some of the new > compiler tooling (I pinged you earlier on a CFI one) causing errors > that developers aren't really sure how to address. I know sometimes > it's not easy to surface the errors with context to what was wrong, > but at the risk of intense bike shedding, is there some way to provide > something like "Likely array bounds error" instead of just "BRK > handler: Fatal exception"? Yeah, this is a result of the size trade-off that resulted in config CONFIG_UBSAN_TRAP -- there ends up being no message about what went wrong. I'd really like to have cleaner handling of this -- perhaps what was done for KCFI could be applied to UBSAN as well, though this is an area I don't know well myself. (i.e. encoding "this was a UBSAN trap" in the trap itself.) Sami or Ard, is this something that could be improved for arm64?
On Thu, 2 Feb 2023 at 20:10, Kees Cook <keescook@chromium.org> wrote: > > On Thu, Feb 02, 2023 at 10:56:29AM -0800, John Stultz wrote: > > That said, making sense of the error message isn't completely trivial > > either. I've been seeing a few cases recently of some of the new > > compiler tooling (I pinged you earlier on a CFI one) causing errors > > that developers aren't really sure how to address. I know sometimes > > it's not easy to surface the errors with context to what was wrong, > > but at the risk of intense bike shedding, is there some way to provide > > something like "Likely array bounds error" instead of just "BRK > > handler: Fatal exception"? > > Yeah, this is a result of the size trade-off that resulted in config > CONFIG_UBSAN_TRAP -- there ends up being no message about what went > wrong. I'd really like to have cleaner handling of this -- perhaps what > was done for KCFI could be applied to UBSAN as well, though this is an > area I don't know well myself. (i.e. encoding "this was a UBSAN trap" > in the trap itself.) > > Sami or Ard, is this something that could be improved for arm64? > -ENOCONTEXT, so I am going to assume this is about runtime instrumentation that needs some kind of 'panic' function which it will invoke if some condition is met that should never occur? We already use brk with different immediate values in the opcode, so the arch layer already has what we need. Is this a limitation in the compiler, perhaps, where it always emits the same brk opcode?
On Thu, Feb 2, 2023 at 11:20 AM Ard Biesheuvel <ardb@kernel.org> wrote: > > On Thu, 2 Feb 2023 at 20:10, Kees Cook <keescook@chromium.org> wrote: > > > > On Thu, Feb 02, 2023 at 10:56:29AM -0800, John Stultz wrote: > > > That said, making sense of the error message isn't completely trivial > > > either. I've been seeing a few cases recently of some of the new > > > compiler tooling (I pinged you earlier on a CFI one) causing errors > > > that developers aren't really sure how to address. I know sometimes > > > it's not easy to surface the errors with context to what was wrong, > > > but at the risk of intense bike shedding, is there some way to provide > > > something like "Likely array bounds error" instead of just "BRK > > > handler: Fatal exception"? > > > > Yeah, this is a result of the size trade-off that resulted in config > > CONFIG_UBSAN_TRAP -- there ends up being no message about what went > > wrong. I'd really like to have cleaner handling of this -- perhaps what > > was done for KCFI could be applied to UBSAN as well, though this is an > > area I don't know well myself. (i.e. encoding "this was a UBSAN trap" > > in the trap itself.) > > > > Sami or Ard, is this something that could be improved for arm64? > > > > -ENOCONTEXT, so I am going to assume this is about runtime > instrumentation that needs some kind of 'panic' function which it will > invoke if some condition is met that should never occur? > > We already use brk with different immediate values in the opcode, so > the arch layer already has what we need. Is this a limitation in the > compiler, perhaps, where it always emits the same brk opcode? Yeah, we'd need to update both the compiler to produce the encoding, and the kernel to recognize the encoding and do something special.
On Thu, Feb 2, 2023 at 11:31 AM Nick Desaulniers <ndesaulniers@google.com> wrote: > > On Thu, Feb 2, 2023 at 11:20 AM Ard Biesheuvel <ardb@kernel.org> wrote: > > > > On Thu, 2 Feb 2023 at 20:10, Kees Cook <keescook@chromium.org> wrote: > > > > > > On Thu, Feb 02, 2023 at 10:56:29AM -0800, John Stultz wrote: > > > > That said, making sense of the error message isn't completely trivial > > > > either. I've been seeing a few cases recently of some of the new > > > > compiler tooling (I pinged you earlier on a CFI one) causing errors > > > > that developers aren't really sure how to address. I know sometimes > > > > it's not easy to surface the errors with context to what was wrong, > > > > but at the risk of intense bike shedding, is there some way to provide > > > > something like "Likely array bounds error" instead of just "BRK > > > > handler: Fatal exception"? > > > > > > Yeah, this is a result of the size trade-off that resulted in config > > > CONFIG_UBSAN_TRAP -- there ends up being no message about what went > > > wrong. I'd really like to have cleaner handling of this -- perhaps what > > > was done for KCFI could be applied to UBSAN as well, though this is an > > > area I don't know well myself. (i.e. encoding "this was a UBSAN trap" > > > in the trap itself.) > > > > > > Sami or Ard, is this something that could be improved for arm64? > > > > > > > -ENOCONTEXT, so I am going to assume this is about runtime > > instrumentation that needs some kind of 'panic' function which it will > > invoke if some condition is met that should never occur? > > > > We already use brk with different immediate values in the opcode, so > > the arch layer already has what we need. Is this a limitation in the > > compiler, perhaps, where it always emits the same brk opcode? > > Yeah, we'd need to update both the compiler to produce the encoding, > and the kernel to recognize the encoding and do something special. A quick look at Clang's source code suggests that Intrinsic::ubsantrap already accepts the handler ID (from the SanitizerHandler enum) as an argument and the arm64 LLVM back-end appears to encode the value as an immediate for the brk instruction. I didn't confirm that this actually works, but perhaps we just need to teach the kernel about the possible values? Sami
On Thu, Feb 02, 2023 at 11:49:42AM -0800, Sami Tolvanen wrote: > On Thu, Feb 2, 2023 at 11:31 AM Nick Desaulniers > <ndesaulniers@google.com> wrote: > > > > On Thu, Feb 2, 2023 at 11:20 AM Ard Biesheuvel <ardb@kernel.org> wrote: > > > > > > On Thu, 2 Feb 2023 at 20:10, Kees Cook <keescook@chromium.org> wrote: > > > > > > > > On Thu, Feb 02, 2023 at 10:56:29AM -0800, John Stultz wrote: > > > > > That said, making sense of the error message isn't completely trivial > > > > > either. I've been seeing a few cases recently of some of the new > > > > > compiler tooling (I pinged you earlier on a CFI one) causing errors > > > > > that developers aren't really sure how to address. I know sometimes > > > > > it's not easy to surface the errors with context to what was wrong, > > > > > but at the risk of intense bike shedding, is there some way to provide > > > > > something like "Likely array bounds error" instead of just "BRK > > > > > handler: Fatal exception"? > > > > > > > > Yeah, this is a result of the size trade-off that resulted in config > > > > CONFIG_UBSAN_TRAP -- there ends up being no message about what went > > > > wrong. I'd really like to have cleaner handling of this -- perhaps what > > > > was done for KCFI could be applied to UBSAN as well, though this is an > > > > area I don't know well myself. (i.e. encoding "this was a UBSAN trap" > > > > in the trap itself.) > > > > > > > > Sami or Ard, is this something that could be improved for arm64? > > > > > > > > > > -ENOCONTEXT, so I am going to assume this is about runtime > > > instrumentation that needs some kind of 'panic' function which it will > > > invoke if some condition is met that should never occur? > > > > > > We already use brk with different immediate values in the opcode, so > > > the arch layer already has what we need. Is this a limitation in the > > > compiler, perhaps, where it always emits the same brk opcode? > > > > Yeah, we'd need to update both the compiler to produce the encoding, > > and the kernel to recognize the encoding and do something special. > > A quick look at Clang's source code suggests that Intrinsic::ubsantrap > already accepts the handler ID (from the SanitizerHandler enum) as an > argument and the arm64 LLVM back-end appears to encode the value as an > immediate for the brk instruction. I didn't confirm that this actually > works, but perhaps we just need to teach the kernel about the possible > values? Oh excellent. Yeah, if that's all that's needed here that would be great. What are the values?
On Thu, Feb 2, 2023 at 11:53 AM Kees Cook <keescook@chromium.org> wrote: > > On Thu, Feb 02, 2023 at 11:49:42AM -0800, Sami Tolvanen wrote: > > A quick look at Clang's source code suggests that Intrinsic::ubsantrap > > already accepts the handler ID (from the SanitizerHandler enum) as an > > argument and the arm64 LLVM back-end appears to encode the value as an > > immediate for the brk instruction. I didn't confirm that this actually > > works, but perhaps we just need to teach the kernel about the possible > > values? > > Oh excellent. Yeah, if that's all that's needed here that would be > great. What are the values? The arm64 brk immediate encoding seems to be "ubsantrap arg | 'U' << 8": https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/AArch64/AArch64InstrInfo.td#L7571 The argument values come from the SanitizerHandler enum, which is populated from this list: https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenFunction.h#L113 Therefore, according to the tests, for ubsantrap(12) we'll get brk #0x550c, for example: https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/AArch64/ubsantrap.ll Sami
On Thu, Feb 02, 2023 at 12:11:47PM -0800, Sami Tolvanen wrote: > On Thu, Feb 2, 2023 at 11:53 AM Kees Cook <keescook@chromium.org> wrote: > > > > On Thu, Feb 02, 2023 at 11:49:42AM -0800, Sami Tolvanen wrote: > > > A quick look at Clang's source code suggests that Intrinsic::ubsantrap > > > already accepts the handler ID (from the SanitizerHandler enum) as an > > > argument and the arm64 LLVM back-end appears to encode the value as an > > > immediate for the brk instruction. I didn't confirm that this actually > > > works, but perhaps we just need to teach the kernel about the possible > > > values? > > > > Oh excellent. Yeah, if that's all that's needed here that would be > > great. What are the values? > > The arm64 brk immediate encoding seems to be "ubsantrap arg | 'U' << 8": > > https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/AArch64/AArch64InstrInfo.td#L7571 > > The argument values come from the SanitizerHandler enum, which is > populated from this list: > > https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenFunction.h#L113 > > Therefore, according to the tests, for ubsantrap(12) we'll get brk > #0x550c, for example: > > https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/AArch64/ubsantrap.ll So the absolute minimal handler would look like this: diff --git a/arch/arm64/include/asm/brk-imm.h b/arch/arm64/include/asm/brk-imm.h index 6e000113e508..3f0f0d03268b 100644 --- a/arch/arm64/include/asm/brk-imm.h +++ b/arch/arm64/include/asm/brk-imm.h @@ -28,6 +28,8 @@ #define BUG_BRK_IMM 0x800 #define KASAN_BRK_IMM 0x900 #define KASAN_BRK_MASK 0x0ff +#define UBSAN_BRK_IMM 0x5500 +#define UBSAN_BRK_MASK 0x00ff #define CFI_BRK_IMM_TARGET GENMASK(4, 0) #define CFI_BRK_IMM_TYPE GENMASK(9, 5) diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index 4c0caa589e12..36b917d8fa5f 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -1074,6 +1074,18 @@ static struct break_hook kasan_break_hook = { }; #endif +#ifdef CONFIG_UBSAN_TRAP +static int ubsan_handler(struct pt_regs *regs, unsigned long esr) +{ + die("Oops - UBSAN", regs, esr); +} + +static struct break_hook ubsan_break_hook = { + .fn = ubsan_handler, + .imm = UBSAN_BRK_IMM, + .mask = UBSAN_BRK_MASK, +}; +#endif #define esr_comment(esr) ((esr) & ESR_ELx_BRK64_ISS_COMMENT_MASK) @@ -1091,6 +1103,10 @@ int __init early_brk64(unsigned long addr, unsigned long esr, #ifdef CONFIG_KASAN_SW_TAGS if ((esr_comment(esr) & ~KASAN_BRK_MASK) == KASAN_BRK_IMM) return kasan_handler(regs, esr) != DBG_HOOK_HANDLED; +#endif +#ifdef CONFIG_UBSAN_TRAP + if ((esr_comment(esr) & ~UBSAN_BRK_MASK) == UBSAN_BRK_IMM) + return ubsan_handler(regs, esr) != DBG_HOOK_HANDLED; #endif return bug_handler(regs, esr) != DBG_HOOK_HANDLED; } @@ -1104,6 +1120,9 @@ void __init trap_init(void) register_kernel_break_hook(&fault_break_hook); #ifdef CONFIG_KASAN_SW_TAGS register_kernel_break_hook(&kasan_break_hook); +#endif +#ifdef CONFIG_UBSAN_TRAP + register_kernel_break_hook(&ubsan_break_hook); #endif debug_traps_init(); } But we could expand ubsan_handler() to extract the SanitizerHandler enum value and report which UBSAN check was hit...
diff --git a/include/linux/device.h b/include/linux/device.h index 424b55df0272..5e4cd857e74f 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -197,9 +197,9 @@ void devres_remove_group(struct device *dev, void *id); int devres_release_group(struct device *dev, void *id); /* managed devm_k.alloc/kfree for device drivers */ -void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc; +void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_size(2); void *devm_krealloc(struct device *dev, void *ptr, size_t size, - gfp_t gfp) __must_check; + gfp_t gfp) __must_check __realloc_size(3); __printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap) __malloc; __printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp, @@ -226,7 +226,8 @@ static inline void *devm_kcalloc(struct device *dev, void devm_kfree(struct device *dev, const void *p); char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp); -void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp); +void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) + __realloc_size(3); unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order);