Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp278904pxb; Wed, 25 Aug 2021 03:06:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUl1cUDYqbGzRAar7pfjBI4zxr3DmcsB2jqwu9h0kLSp9q5DBhM+IQ0spCPhqPQ4KvEGLS X-Received: by 2002:a17:906:7095:: with SMTP id b21mr45585239ejk.131.1629885994304; Wed, 25 Aug 2021 03:06:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629885994; cv=none; d=google.com; s=arc-20160816; b=pXQL/oPaLz8mRkRhL0lOaXjOAMk6bTk6PpYisdAzUOSaPy7BSBByxao+7pEUaIwSfS ETQSCZBRlF/YPhGoFbee24Irq4XT38qzsM4EmvhgfkFcC9nNzVTw1T1An3XUJpFbabjd z6eFoqzlMX83wSEnAu1A5Ixztmj1TSVYiLppan7c1iG/g20Z+Bmp+sRaKkrds0XeY9wZ yr1mCvvynCopuWj+o/i8Ty3wgjwPlnojnKEP+zYAjJqbKST17pMvF0rOgUUmD1Fnf6Kp EXgEF8QJDIyjT3fe3mjTtKBgAX3cXJ6SjyesG26EpmfHTz72mSCiFRwsYixzD3PNfVyA DHrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date; bh=9oJ6CJBroI89A+Yc8quEo4xqgpEr0O1UTP9oPtzRdEM=; b=gNJlcywDB+mKPmYHui3XbjQJPaQGZmXQos7v8UgjeTeISJgRMtHY5KW1PKsLlNziKJ CYHDf5bAaJxhOrFRv5X2sgHz7ed+HUM8YwBn9LynoxLHXf0U2T1kxiE4U9bzncoSzQO7 G10mb5ieLO7pISs9sstpEX0ARg3MT4Ze8VpMnNXU76bjsTiQAvA5WmOFdBhlzBzh1CHu g5Gqd3phlxJuLp3layTLEBiPekRVM4klXf4L4DIArqMF2HxdyMDxkbrYYeO681JsiiJO 5lIuenfZhceZfU1GlhY1PLJOC22yei5L2chMPexUpqSCy4ogdBeVZep+Xvmemo6OP201 L89Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h4si5922252edq.526.2021.08.25.03.06.11; Wed, 25 Aug 2021 03:06:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239811AbhHYKCa (ORCPT + 99 others); Wed, 25 Aug 2021 06:02:30 -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().