From mboxrd@z Thu Jan 1 00:00:00 1970 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=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no 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 BA42EC4320A for ; Wed, 25 Aug 2021 10:01:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9300E61101 for ; Wed, 25 Aug 2021 10:01:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239813AbhHYKCb (ORCPT ); Wed, 25 Aug 2021 06:02:31 -0400 Received: from vmi485042.contaboserver.net ([161.97.139.209]:34628 "EHLO gentwo.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239219AbhHYKC3 (ORCPT ); Wed, 25 Aug 2021 06:02:29 -0400 Received: by gentwo.de (Postfix, from userid 1001) id E4281B004CA; Wed, 25 Aug 2021 12:01:42 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by gentwo.de (Postfix) with ESMTP id E2A99B0021C; Wed, 25 Aug 2021 12:01:42 +0200 (CEST) Date: Wed, 25 Aug 2021 12:01:42 +0200 (CEST) From: Christoph Lameter To: Daniel Micay cc: Christoph Hellwig , Kees Cook , kernel list , Andrew Morton , Miguel Ojeda , Nathan Chancellor , Nick Desaulniers , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Dennis Zhou , Tejun Heo , Masahiro Yamada , Michal Marek , clang-built-linux@googlegroups.com, Linux-MM , linux-kbuild , linux-hardening@vger.kernel.org Subject: Re: [PATCH 0/5] Add __alloc_size() for better bounds checking In-Reply-To: Message-ID: References: <20210818050841.2226600-1-keescook@chromium.org> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 19 Aug 2021, Daniel Micay wrote: > For example, it will know that kmalloc(n) returns either NULL or an > allocation of size n. A simple sample program with calloc in > userspace: > > #include > #include > > int main(void) { > char *p = calloc(64, 1); > if (!p) { > return 1; > } > printf("%zu\n", __builtin_object_size(p, 1)); > return 0; > } > > It will also detect an out-of-bounds access via the allocation with > -fsanitize=object-size including with a runtime value as the index. > > It's not as useful as it should be yet because __builtin_object_size > must return a compile-time constant. Clang has a new > __builtin_dynamic_object_size that's allowed to return a value that's > not a compile-time constant so it can work for kmalloc(n) where n is a > runtime value. It might not be quite ready for use yet but it should > be able to make it a lot more useful. GCC also seems open to adding it > too. The other complication with kmalloc etc is that the slab allocators may decided to allocate more bytes than needed because it does not support that particular allocation size. Some functions check the allocated true size and make use of that. See ksize().