From patchwork Thu Oct 10 23:23:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11184581 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 2FCD6112B for ; Thu, 10 Oct 2019 23:24:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0F6FF21A4A for ; Thu, 10 Oct 2019 23:24:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="D51UL9rR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727202AbfJJXYB (ORCPT ); Thu, 10 Oct 2019 19:24:01 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46846 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727133AbfJJXYA (ORCPT ); Thu, 10 Oct 2019 19:24:00 -0400 Received: by mail-pg1-f196.google.com with SMTP id b8so4617873pgm.13 for ; Thu, 10 Oct 2019 16:23:59 -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=D51UL9rR9U7OMRC5kU/1LTr0ek5QCAdhAcQxhNohfHmGY1xfQuGmsaX43izG0+lrE0 ylCyES33CwISf98T+j7OteRdsjztl0mPIJJyaZTX072VBYlsYPO47quZ4flLQdBfNjVz zjCmXjZDOX7lIGxFeth+TOG2oD33SESzGVRI8= 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=bNg7/qymQVT2hSmn5QvYIXXUkTqc3yjDmqcGY7OriUKwN8VSePwUreIy1UJMYPKUia s1F9g3LAp79atQdY05TDEoOTh6kjaLz2DhNx2aYwpR5veYeoqOK3MXMZuFu31LhqFoPw INaI+pYx7VkGqLvq4bq90LKJd9c1/qhmlBdQxAmFKUB/hGOj5n0fqT5enZWx6bEiY3kQ ssSwUM12K9JOXs7kzOyIFTbaXDupB0QX7qQNbEXFSJsY7R+qbcalqyRnseITnZWLEC9q AfBbB+wF6Iohcceq4kbxU6Whws+ruLzcqaGhhMh4ihawTWg8c/6zvklOhYN+2EMk2P8l bY+A== X-Gm-Message-State: APjAAAVgMmPG6JzbLnaD6gIQP9jr/5eQsF6yAz1fTk0cVcS7judFEoVA Tp8fhFzfDcYnreIcmtXoZ0KnJQ== X-Google-Smtp-Source: APXvYqzKvAiLNIuxwJuj4wKQhF4ru4G8rWSNTC6EJdJa6fnz989nt0l/p9guk494hejHp/ITyXIR/w== X-Received: by 2002:a63:8b:: with SMTP id 133mr3725412pga.183.1570749838835; Thu, 10 Oct 2019 16:23:58 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p1sm10658112pfb.112.2019.10.10.16.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 16:23:57 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Pankaj Bharadiya , Alexey Dobriyan , Linus Torvalds , "David S. Miller" , Randy Dunlap , Andrew Morton , netdev@vger.kernel.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v2 1/4] MIPS: OCTEON: Replace SIZEOF_FIELD() macro Date: Thu, 10 Oct 2019 16:23:42 -0700 Message-Id: <20191010232345.26594-2-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191010232345.26594-1-keescook@chromium.org> References: <20191010232345.26594-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 Thu Oct 10 23:23:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11184583 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 C286D1668 for ; Thu, 10 Oct 2019 23:24:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A2E7121A4C for ; Thu, 10 Oct 2019 23:24:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="L+26j3w/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727528AbfJJXYZ (ORCPT ); Thu, 10 Oct 2019 19:24:25 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:42397 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727164AbfJJXYB (ORCPT ); Thu, 10 Oct 2019 19:24:01 -0400 Received: by mail-pf1-f193.google.com with SMTP id q12so4881715pff.9 for ; Thu, 10 Oct 2019 16:24:01 -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=L+26j3w/f0Pvqirk5VQGVIaNnOzQ2IdJK1izuYoKNo3SNs6N6HAOgE0M/aYhGT3nnY YJiZD1qfB6cRN1s5hFnMMcbVtr1ThMS0G3uUnMNPgxf+aQ5Yioc6LCYHRTtv6d5KMIg/ Ss3qpng+qg6mwj+zSd5BFQKD6vfSiz3COgl80= 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=jPIRINpwAdhH3RZuQmUTErO4Rj47nRb3lFZQ8I4kikPp71RZ2IAuuM8p+jHpQLPn9Z T5fUZiLxZ6xAJsQ+dqOe12DC3d6ZW0nLG0Cjb+hgMUyLelfiOAz2/quZ77WLBuZ9C+2W LuQTk+MwEmH9mJ1VMmG6H1eJtI/rTokcVAAe6EsRVQwu9Z2mqgd4x2OGmMcxPHdoopJR fKCMrB1HmwGGQ25VXe4UuiPu5xQ5TqnDRWWwwBOwrsTmYt6BCIpfWSPST30TqRm4B+2b VERL4uiOvyli2wU6RIpZeE4IUKxbJyFsSb0dGpuswayDCL3h6mMrqy7Axdj+0V5Nb7lS 0CqQ== X-Gm-Message-State: APjAAAWZjh/GEO+NjYd8hTJlZIUcbxSaaz64DNjxgDQXsqaKngPvn2+F KQ+RdS2tnpXUttyHORRu0RjSKg== X-Google-Smtp-Source: APXvYqxjCP9hXmIHY8zl6IMdUQdpDHJPmIqU3YoKsTJXuzror2pooZA8+yn8qs+v6tKBGdrepjliWw== X-Received: by 2002:a63:cc4a:: with SMTP id q10mr13860587pgi.221.1570749840928; Thu, 10 Oct 2019 16:24:00 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 184sm5994385pgf.33.2019.10.10.16.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 16:23:58 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Pankaj Bharadiya , Alexey Dobriyan , Linus Torvalds , "David S. Miller" , Randy Dunlap , Andrew Morton , netdev@vger.kernel.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v2 2/4] linux/stddef.h: Add sizeof_member() macro Date: Thu, 10 Oct 2019 16:23:43 -0700 Message-Id: <20191010232345.26594-3-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191010232345.26594-1-keescook@chromium.org> References: <20191010232345.26594-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__ */ From patchwork Thu Oct 10 23:23:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 11184579 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 A57BD1668 for ; Thu, 10 Oct 2019 23:24:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8574421D56 for ; Thu, 10 Oct 2019 23:24:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="X2Pgj+AX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727408AbfJJXYC (ORCPT ); Thu, 10 Oct 2019 19:24:02 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:41200 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727188AbfJJXYB (ORCPT ); Thu, 10 Oct 2019 19:24:01 -0400 Received: by mail-pl1-f195.google.com with SMTP id t10so3544048plr.8 for ; Thu, 10 Oct 2019 16:23:59 -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=yMqmNRh9/WRVuOhxmgeAss8KGdVZEWgP1gvN19lsOqw=; b=X2Pgj+AXTV2Ft841aCpOcTDGSCdqRW7jKxi/A0JNfzkCvRnBRTlgiBPSQlnSB+smXf KM/0N/YQd1W8z5kju+9JLAHVI1pfeWN/BEzNRrEERIuWwD3BkBcqvXop0Shui4yPMLqZ D7f/vZRRKSScbxm9PdqjdsYfiYl9R3gEC2Gak= 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=yMqmNRh9/WRVuOhxmgeAss8KGdVZEWgP1gvN19lsOqw=; b=AdGnHa0+iNBc+LrfGSb5HyCHzG9ze/0s8O0rBrd6pZ4PxFQIKtARXKFyKc1zrJ6WrV rpNCchvwm1euDJqprUpgUyxCNRmovld3GBiIWWVtHTZ6CXNt0os0qiiQibtUui5cWnb0 5vJ92lsvybx3+kKCDSwn1YvCx45+9jKi6dY8K/wBiXcNpLFlQtmVwL7TnOASjJxNn8si MsJvFL34vIFBAODHGs6DWOzgQwNXb5xjoN3Yz7ZS8L/BtN8BGvaw/SoO3uRcRUN4rCh9 2sw0WIFbclkzUH/Eboq2aPAjH6U99ArERWEV/vysi2VeVYOKgLXy9HiNp8JxmsF62QE6 UJmg== X-Gm-Message-State: APjAAAWLNC0OjLzx1OLd79t9mAeLzEHF8e3VUOxtzLAfDETdFCCUTr32 ly+7C5Vu6EjWXV2LY25Y9OFITw== X-Google-Smtp-Source: APXvYqzr5TEKsgP0lOS7AdzCh27u/YbVsT+Ij4ZMEVcIou1HxRIVeL6D9p3wjllIloBGYMby8gU1TQ== X-Received: by 2002:a17:902:7b96:: with SMTP id w22mr12067901pll.40.1570749839357; Thu, 10 Oct 2019 16:23:59 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id m2sm6645331pff.154.2019.10.10.16.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 16:23:58 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Pankaj Bharadiya , Alexey Dobriyan , Linus Torvalds , "David S. Miller" , Randy Dunlap , Andrew Morton , netdev@vger.kernel.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v2 4/4] include: Remove FIELD_SIZEOF() and sizeof_field() macros Date: Thu, 10 Oct 2019 16:23:45 -0700 Message-Id: <20191010232345.26594-5-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191010232345.26594-1-keescook@chromium.org> References: <20191010232345.26594-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 The common function used to find struct member size is sizeof_member(). Remove the now unused FIELD_SIZEOF() and sizeof_field() macros. Signed-off-by: Pankaj Bharadiya Link: https://lore.kernel.org/r/20190924105839.110713-5-pankaj.laxminarayan.bharadiya@intel.com Co-developed-by: Kees Cook Signed-off-by: Kees Cook --- include/linux/kernel.h | 9 --------- include/linux/stddef.h | 8 -------- 2 files changed, 17 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index d83d403dac2e..d67020250d75 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -79,15 +79,6 @@ */ #define round_down(x, y) ((x) & ~__round_mask(x, y)) -/** - * FIELD_SIZEOF - get the size of a struct's field - * @t: the target struct - * @f: the target struct's field - * Return: the size of @f in the struct definition without having a - * declared instance of @t. - */ -#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) - #define typeof_member(T, m) typeof(((T*)0)->m) #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP diff --git a/include/linux/stddef.h b/include/linux/stddef.h index ecadb736c853..74b5e644d50a 100644 --- a/include/linux/stddef.h +++ b/include/linux/stddef.h @@ -19,14 +19,6 @@ enum { #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) #endif -/** - * sizeof_field(TYPE, MEMBER) - * - * @TYPE: The structure containing the field of interest - * @MEMBER: The field to return the size of - */ -#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) - /** * sizeof_member(TYPE, MEMBER) - get the size of a struct's member *