From patchwork Tue Oct 29 22:47:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11218711 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D3A1D1515 for ; Tue, 29 Oct 2019 22:48:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AF98E2087E for ; Tue, 29 Oct 2019 22:48:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="JCRbAozj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726667AbfJ2WsI (ORCPT ); Tue, 29 Oct 2019 18:48:08 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:46307 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726366AbfJ2WsH (ORCPT ); Tue, 29 Oct 2019 18:48:07 -0400 Received: by mail-pg1-f194.google.com with SMTP id f19so79823pgn.13 for ; Tue, 29 Oct 2019 15:48:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qKUfU/s6VNCMjI6UpTZnmPGQRCLUuGC/0Ur+XQYjYn4=; b=JCRbAozjQ5VTk70EYdt3VQp5FkX2TuQhI8+q0O4swFbEKS/ln8ZpNnfrYQNiTJub2f VQLa3gkCiTL02XzRdUu4wS9OJ+52SaR9sC98PuxTQVCYDfX+vDvExtqftZCxF5xCObaF hABVTjF8M6/pRCAh5vpu7idIZ5tib6qfY2wrc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qKUfU/s6VNCMjI6UpTZnmPGQRCLUuGC/0Ur+XQYjYn4=; b=fVaRHYLZP54Nm/IAg9EujO7PKVJ7tw6GaIBOGdz+8SUM6ZrqY0Qh/AshB+wJCO2qAc nf0Fc6OinsXowFx2nO3jnFYmejpuJ2r6UbAPGW3RDI1CCgAzQA9oJfHnTzfzOGyv8I9e lo6EuialiZr4TXQ/TYYdHGe59c2EBOHo5TzX+B7pZqbULRe54imKCtuRJM5be57Nn7M4 KkwncLSK7T7PVly2esfcqbzoKHJ0MSPDo8aV7Txq4uHC7Gpch+dPkbObqeTck3ZKGVfQ Gu1ZX5AICjA9FnCE0Vk5bEetoHG3kOl0w4B7oUnu3X75gSimlpm7a2azKgUQ4HhpzwKh TquQ== X-Gm-Message-State: APjAAAXKHGsfSgIi3QN1KM+XLOCP3uxWk43Rv/6FE7ZGusHTqTvWKnQM ksNN59E9M7vmeAuzN74RXmPV8A== X-Google-Smtp-Source: APXvYqx2jf6YGVEJMofLmnQuj1bd8RRLe/mmzmKbe1+WcVdSDVf/LY61lDqqHnkFYpwNpoarVLLQbw== X-Received: by 2002:a62:cecf:: with SMTP id y198mr10781942pfg.86.1572389286825; Tue, 29 Oct 2019 15:48:06 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id b23sm143117pju.16.2019.10.29.15.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2019 15:48:05 -0700 (PDT) From: Kees Cook To: Linus Torvalds Cc: Kees Cook , Pankaj Bharadiya , Alexey Dobriyan , "David S. Miller" , Randy Dunlap , Andrew Morton , netdev@vger.kernel.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/3] MIPS: OCTEON: Replace SIZEOF_FIELD() macro Date: Tue, 29 Oct 2019 15:47:54 -0700 Message-Id: <20191029224756.28618-2-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191029224756.28618-1-keescook@chromium.org> References: <20191029224756.28618-1-keescook@chromium.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Pankaj Bharadiya In preparation for switching to a standard sizeof_member() macro to find the size of a member of a struct, remove the custom SIZEOF_FIELD() macro and use the more common FIELD_SIZEOF() instead. Later patches will globally replace FIELD_SIZEOF() and sizeof_field() with the more accurate sizeof_member(). Signed-off-by: Pankaj Bharadiya Link: https://lore.kernel.org/r/20190924105839.110713-4-pankaj.laxminarayan.bharadiya@intel.com Co-developed-by: Kees Cook Signed-off-by: Kees Cook --- arch/mips/cavium-octeon/executive/cvmx-bootmem.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c index ba8f82a29a81..44b506a14666 100644 --- a/arch/mips/cavium-octeon/executive/cvmx-bootmem.c +++ b/arch/mips/cavium-octeon/executive/cvmx-bootmem.c @@ -44,13 +44,6 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; /* See header file for descriptions of functions */ -/** - * This macro returns the size of a member of a structure. - * Logically it is the same as "sizeof(s::field)" in C++, but - * C lacks the "::" operator. - */ -#define SIZEOF_FIELD(s, field) sizeof(((s *)NULL)->field) - /** * This macro returns a member of the * cvmx_bootmem_named_block_desc_t structure. These members can't @@ -65,7 +58,7 @@ static struct cvmx_bootmem_desc *cvmx_bootmem_desc; #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field) \ __cvmx_bootmem_desc_get(addr, \ offsetof(struct cvmx_bootmem_named_block_desc, field), \ - SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc, field)) + FIELD_SIZEOF(struct cvmx_bootmem_named_block_desc, field)) /** * This function is the implementation of the get macros defined From patchwork Tue Oct 29 22:47:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11218713 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F3F8C1390 for ; Tue, 29 Oct 2019 22:48:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D114E2173E for ; Tue, 29 Oct 2019 22:48:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="CkwdYGQt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726423AbfJ2WsH (ORCPT ); Tue, 29 Oct 2019 18:48:07 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:45740 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726297AbfJ2WsH (ORCPT ); Tue, 29 Oct 2019 18:48:07 -0400 Received: by mail-pl1-f195.google.com with SMTP id y24so8666plr.12 for ; Tue, 29 Oct 2019 15:48:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0G08DVDmHHpYBvqrNiRz2P/EWIDmTXItkvaTB27EiyI=; b=CkwdYGQthHbjeB7YJk2k7oOQyekpKWhBz8LE56+eFTTP9mqZeL9MuGgiv2GrcLSaBg rE2sCuLvAVGnNCs4vowlSz50Su8Mv9l48u1zbUbe4ZDaypaV5WsadZZAOOcYYKfOmOIf EmLxn+hz5Gpaz6QM1cM8GcO4kjJgDDwECBfv8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0G08DVDmHHpYBvqrNiRz2P/EWIDmTXItkvaTB27EiyI=; b=gcE3S+EfADxzdW3kgr5rBSAP7Z5QRyXwK0pBptqUKpovGvKic3eWETZNtoz08PNvHJ /xTlZWkN8KoRG6cVogXSha9UEm9WbvSxBEjebtSfvgW5IFTqckPdicIAKOJjwrFeyBGB uu0dBpbpaTt/NfVw6nlcRGKQNQJxaCNARukWtUA1NPVnTaoheR8vNmxQ7l6v9vzfkwIL skUetqI5cWocH8mTv9ml6VQZylJ545bt5WbJVMbnHzpuU1Qhbk2Hp2cnAbHAn7YxxA9B OL+PrONFY3Xl7jKLj9/3oYhahWziBiGepBYwsSksglhTVFEhZOaAZsN4jkkZN2PeFO0P fo2g== X-Gm-Message-State: APjAAAXYjDs4HdWEWbDjjcud2kOgcEYOb0eXMht6x8CS/lFS3Aqpz0nK O2fvxEIzPAu2G6PLDiQo6Ul90w== X-Google-Smtp-Source: APXvYqy6v7l6MhB+p5gWacT10BGV47R2lNIZI3D56QBZanDs4YzchO4lyFefyJjvfINrgcE9a5Si7A== X-Received: by 2002:a17:902:b091:: with SMTP id p17mr1086640plr.13.1572389286296; Tue, 29 Oct 2019 15:48:06 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id e7sm254020pgr.25.2019.10.29.15.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2019 15:48:05 -0700 (PDT) From: Kees Cook To: Linus Torvalds Cc: Kees Cook , Pankaj Bharadiya , Alexey Dobriyan , "David S. Miller" , Randy Dunlap , Andrew Morton , netdev@vger.kernel.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/3] linux/stddef.h: Add sizeof_member() macro Date: Tue, 29 Oct 2019 15:47:55 -0700 Message-Id: <20191029224756.28618-3-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191029224756.28618-1-keescook@chromium.org> References: <20191029224756.28618-1-keescook@chromium.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Pankaj Bharadiya At present we have 2 different macros to calculate the size of a member of a struct: FIELD_SIZEOF() and sizeof_field(). As a prerequisite to bringing uniformity to the entire kernel source tree, add sizeof_member() macro as it is both more pleasant (not upper case) and more correct (sizeof()-family cannot operate on bit fields; this is meant to operate on struct members), as discussed[1]. Future patches will replace all occurrences of above macros with sizeof_member(). [1] https://www.openwall.com/lists/kernel-hardening/2019/07/02/2 Signed-off-by: Pankaj Bharadiya Link: https://lore.kernel.org/r/20190924105839.110713-2-pankaj.laxminarayan.bharadiya@intel.com Co-developed-by: Kees Cook Signed-off-by: Kees Cook --- include/linux/stddef.h | 13 ++++++++++++- tools/testing/selftests/bpf/bpf_util.h | 6 +++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/linux/stddef.h b/include/linux/stddef.h index 998a4ba28eba..ecadb736c853 100644 --- a/include/linux/stddef.h +++ b/include/linux/stddef.h @@ -27,6 +27,17 @@ enum { */ #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) +/** + * sizeof_member(TYPE, MEMBER) - get the size of a struct's member + * + * @TYPE: the target struct + * @MEMBER: the target struct's member + * + * Return: the size of @MEMBER in the struct definition without having a + * declared instance of @TYPE. + */ +#define sizeof_member(TYPE, MEMBER) (sizeof(((TYPE *)0)->MEMBER)) + /** * offsetofend(TYPE, MEMBER) * @@ -34,6 +45,6 @@ enum { * @MEMBER: The member within the structure to get the end offset of */ #define offsetofend(TYPE, MEMBER) \ - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) + (offsetof(TYPE, MEMBER) + sizeof_member(TYPE, MEMBER)) #endif diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h index ec219f84e041..6b4b3e24ba9f 100644 --- a/tools/testing/selftests/bpf/bpf_util.h +++ b/tools/testing/selftests/bpf/bpf_util.h @@ -31,13 +31,13 @@ static inline unsigned int bpf_num_possible_cpus(void) # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #endif -#ifndef sizeof_field -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) +#ifndef sizeof_member +#define sizeof_member(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) #endif #ifndef offsetofend #define offsetofend(TYPE, MEMBER) \ - (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) + (offsetof(TYPE, MEMBER) + sizeof_member(TYPE, MEMBER)) #endif #endif /* __BPF_UTIL__ */