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 B6607C432BE for ; Wed, 25 Aug 2021 10:01:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2CC1661373 for ; Wed, 25 Aug 2021 10:01:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2CC1661373 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=gentwo.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id AB1FB900002; Wed, 25 Aug 2021 06:01:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A611B6B0071; Wed, 25 Aug 2021 06:01:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9505D900002; Wed, 25 Aug 2021 06:01:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0227.hostedemail.com [216.40.44.227]) by kanga.kvack.org (Postfix) with ESMTP id 749CB6B006C for ; Wed, 25 Aug 2021 06:01:46 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 1085625F2C for ; Wed, 25 Aug 2021 10:01:46 +0000 (UTC) X-FDA: 78513161412.36.E42214C Received: from gentwo.de (vmi485042.contaboserver.net [161.97.139.209]) by imf04.hostedemail.com (Postfix) with ESMTP id 6404650000BC for ; Wed, 25 Aug 2021 10:01:45 +0000 (UTC) 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 Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of cl@gentwo.de designates 161.97.139.209 as permitted sender) smtp.mailfrom=cl@gentwo.de; dmarc=none X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 6404650000BC X-Stat-Signature: wt84jan7r4yjyfhqtqayn8apxy3g8gp9 X-HE-Tag: 1629885705-854414 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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().