From patchwork Thu Jan 11 02:02:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10156467 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E849D605F5 for ; Thu, 11 Jan 2018 02:04:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D80F42856D for ; Thu, 11 Jan 2018 02:04:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CC188286D6; Thu, 11 Jan 2018 02:04:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 0B919286D4 for ; Thu, 11 Jan 2018 02:04:36 +0000 (UTC) Received: (qmail 30115 invoked by uid 550); 11 Jan 2018 02:03:44 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 30025 invoked from network); 11 Jan 2018 02:03:40 -0000 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=J6BPWJkJw0NIYe8aj5nt3aFUx1WO1Q0e1QihEl5WFgQ=; b=O5vpHoSy7SY6F6bbuRi9EWHiE3Hins4ofhcLZKTbUmG8ihai6l6uQcySXTaDLbH8xx BxLJclkym60w8Y5Q6pArqhC6CsWK7CCDWmF/lGG2my4xf7q5SWrrhy3jDRKbJD5c5RA7 Cd/adwsvVTHi/N7m9df+1X1oy0RuWre2Nt8pg= 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=J6BPWJkJw0NIYe8aj5nt3aFUx1WO1Q0e1QihEl5WFgQ=; b=h0qSvF4Y6DP/e2gOOZqoEWYaALJSuUvlWW9NSPKQoNnN7uyWMLpuiD7xt/LQk9Op1E hkQhWtP9FYGhdlPsBzB+svXjr7oUDWhC6mzKT2fVaTvZmw7GmjD/pl8BITVCegw4pubS D2O6XG+r4lQumf8entjK3ah4+p3bnKVqSrttmXknwUmO/W4EASIPH+FdIornVKy0oW9p t3AW7yogUEh9iOMDjtypoPoiueyFOF3pz2dTOzI/2gOdKRB5+dCGJ7RVD4pfbtfP+aI9 cBpvk44iI11X+YqhCLIoVAt5m423YDOnslkGP6M1doRTn97H5Hw7dGbDTjsQNxhRg65n t35A== X-Gm-Message-State: AKwxytdIv1BDZHcx3ZRmpXWZZmXE1LzVnZXbzLdCe72R41NKO7u+Xtnm f4T09FWsddf/sbtoSJWpu4E4gQ== X-Google-Smtp-Source: ACJfBosDuHcVjbj7hZuY+Pq3wDObgENoz+sfhgpkMj9oSTsvfIQm41ghImKcHpEUgaAIuj1t38ySrg== X-Received: by 10.159.246.139 with SMTP id c11mr8515002pls.446.1515636205241; Wed, 10 Jan 2018 18:03:25 -0800 (PST) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Linus Torvalds , David Windsor , Alexander Viro , Andrew Morton , Andy Lutomirski , Christoph Hellwig , Christoph Lameter , "David S. Miller" , Laura Abbott , Mark Rutland , "Martin K. Petersen" , Paolo Bonzini , Christian Borntraeger , Christoffer Dall , Dave Kleikamp , Jan Kara , Luis de Bethencourt , Marc Zyngier , Rik van Riel , Matthew Garrett , linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Date: Wed, 10 Jan 2018 18:02:37 -0800 Message-Id: <1515636190-24061-6-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515636190-24061-1-git-send-email-keescook@chromium.org> References: <1515636190-24061-1-git-send-email-keescook@chromium.org> Subject: [kernel-hardening] [PATCH 05/38] stddef.h: Introduce sizeof_field() X-Virus-Scanned: ClamAV using ClamSMTP The size of fields within a structure is needed in a few places in the kernel already, and will be needed for the usercopy whitelisting when declaring whitelist regions within structures. This creates a dedicated macro and redefines offsetofend() to use it. Existing usage, ignoring the 1200+ lustre assert uses: $ git grep -E 'sizeof\(\(\((struct )?[a-zA-Z_]+ \*\)0\)->' | \ grep -v staging/lustre | wc -l 65 Signed-off-by: Kees Cook --- include/linux/stddef.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/include/linux/stddef.h b/include/linux/stddef.h index 2181719fd907..998a4ba28eba 100644 --- a/include/linux/stddef.h +++ b/include/linux/stddef.h @@ -20,12 +20,20 @@ enum { #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)) + +/** * offsetofend(TYPE, MEMBER) * * @TYPE: The type of the structure * @MEMBER: The member within the structure to get the end offset of */ #define offsetofend(TYPE, MEMBER) \ - (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER)) + (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) #endif