Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5144654pxv; Wed, 28 Jul 2021 04:20:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzaJCjYFmC+u4Cad142Mt+4CG7VAnksjhHSt0jed+N0jQ3Rq/klM7ndhyXsmpTlhWTv6sdf X-Received: by 2002:a05:6638:418f:: with SMTP id az15mr26527069jab.8.1627471242271; Wed, 28 Jul 2021 04:20:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627471242; cv=none; d=google.com; s=arc-20160816; b=yEXc4jnPMso/26aIsD0PZvnlhwhPb4foCVdcFSnBlNUI9HXamWTI3e9rNGej6Sqkxv FNYk1Sh3SkWOURxWx6mvkdpB/NBIKOMQjXErrgjFzvzS1kSvZsOGZJG+9QSXM4j4p3S8 DrJSzmUnbje2HlJd7PLvuZJvhN/n6JGFfDm/BFrtcnHuauDCGnyNOxmnxYEyAciTo0QN KkTT/UyY2wbLK2Zn2Ks9IM18pPe3r8lbaoSxEBnEs9OwN37WJouqz+lz7VyqvohuUyZt 76jKLb5EGShwxo2HGjvgEQ6vp/asUXc7b0ceRfdRp220WZxfxGLGkABJ5k6hMFVkQ9ns xmMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=WRpAt4Qf30VQcI04z59j1Xhna4kVm3ZAFQjWCiFVzII=; b=sE6ErAPiJjh9wotlN4hHUMplNreHnFcU5D1Ei0+eAypOhUrBafyBjTb5L2l83ADaHB gHRB2PEnEjgsfIwejNAUMnnCLr9nD3dHalC/qbdGSkrdmMQxGruZIYebjzPeUbL9J2b3 shW5l+Vn5fy0KEKAX5ZG9Zy/Un5NHabKN3OZ859XWdQYcsqFcHu9G+Kv5fddRHQ58Gof EYxxgN8+XOZp6QAd0+jpqPvZPkkuO4Y+2XTlhkz0b7U9U4RjlSXEEYhqol2lA8peQODE K2vIr4890pxKEhrtL5n1dlB3JHrBvX4nLf+P2vXSLp8B/xlz33qfUp1vvqboMPSu7xwO e4eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=ZITf0JJk; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-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 o7si6123096jao.123.2021.07.28.04.20.25; Wed, 28 Jul 2021 04:20:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=ZITf0JJk; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234169AbhG1LUG (ORCPT + 99 others); Wed, 28 Jul 2021 07:20:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232165AbhG1LUE (ORCPT ); Wed, 28 Jul 2021 07:20:04 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DF23C061760 for ; Wed, 28 Jul 2021 04:20:02 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id d18so3229971lfb.6 for ; Wed, 28 Jul 2021 04:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=WRpAt4Qf30VQcI04z59j1Xhna4kVm3ZAFQjWCiFVzII=; b=ZITf0JJkY6siA6zuTWx/S+3I3pJ92j2lsEWHCAUrQgybgq2nu7vMyrV5yFzPnr0ASF zQnwOwgLaX1f2Ag8AdE3Zoi+yryD69qxehZj3Axp0G1SI8NHWpwdtRD/AWfB8hXTTU+w 7JIntZBgXB7V3Bx+KAGMmd0eo1ehu7YXfBEGo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=WRpAt4Qf30VQcI04z59j1Xhna4kVm3ZAFQjWCiFVzII=; b=B7GiRPRkcVWiQUnERoLSwObCWTB/4UaftGUTXvAq/lOrcOuqxIqRXuebTPlnY4Uqet iGVt5CMJ4wwDnOwY+LGoN+zmv0E/+XBKotmWoRdLpuSU/2Gh+zdS69slk31EY6GZiDp1 s6Ltj2bU9gv0OtZ3XyEdzEWDWSaIsDWa4ESYhJuxXjf08tk8C8cE92c5+zsbv4abE8zf IU3tahRDusDzs1OQnBf2qixpPmQKEaAwwyCbGGsdFnwme/n00OiqeaNzXSAbEvHp8J/g mhJBc5xaagpOWk/JHl31+y0heDte9zh0vR+aXTHJKMd249UgEg0qw8R+KnK1qXuggjih tx+A== X-Gm-Message-State: AOAM532sT/APxH99ph/AmYSK8wgjPy0jaYq3/s2dhwsIGDTD2UZGt5aA 2Pq1JX+02wrn6GSvVzgje1Cc9Q== X-Received: by 2002:a19:dc5e:: with SMTP id f30mr20474709lfj.318.1627471200888; Wed, 28 Jul 2021 04:20:00 -0700 (PDT) Received: from [172.16.11.1] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id z4sm564853lfe.274.2021.07.28.04.19.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Jul 2021 04:20:00 -0700 (PDT) Subject: Re: [PATCH 34/64] fortify: Detect struct member overflows in memcpy() at compile-time To: Kees Cook , linux-hardening@vger.kernel.org Cc: "Gustavo A. R. Silva" , Keith Packard , Greg Kroah-Hartman , Andrew Morton , linux-kernel@vger.kernel.org, linux-wireless@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-staging@lists.linux.dev, linux-block@vger.kernel.org, linux-kbuild@vger.kernel.org, clang-built-linux@googlegroups.com References: <20210727205855.411487-1-keescook@chromium.org> <20210727205855.411487-35-keescook@chromium.org> From: Rasmus Villemoes Message-ID: Date: Wed, 28 Jul 2021 13:19:59 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210727205855.411487-35-keescook@chromium.org> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org On 27/07/2021 22.58, Kees Cook wrote: > At its core, FORTIFY_SOURCE uses the compiler's __builtin_object_size() > internal[0] to determine the available size at a target address based on > the compile-time known structure layout details. It operates in two > modes: outer bounds (0) and inner bounds (1). In mode 0, the size of the > enclosing structure is used. In mode 1, the size of the specific field > is used. For example: > > struct object { > u16 scalar1; /* 2 bytes */ > char array[6]; /* 6 bytes */ > u64 scalar2; /* 8 bytes */ > u32 scalar3; /* 4 bytes */ > } instance; > > > __builtin_object_size(instance.array, 0) == 18, since the remaining size > of the enclosing structure starting from "array" is 18 bytes (6 + 8 + 4). I think the compiler would usually end up making that struct size 24, with 4 bytes of trailing padding (at least when alignof(u64) is 8). In that case, does __builtin_object_size(instance.array, 0) actually evaluate to 18, or to 22? A quick test on x86-64 suggests the latter, so the memcpy(, , 20) would not be a violation. Perhaps it's better to base the example on something which doesn't have potential trailing padding - so either add another 4 byte member, or also make scalar2 u32. Rasmus