Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp944055pxb; Sun, 22 Aug 2021 00:58:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDDs5Sg5HdOVvrA9FPKLJkjJlpS1NoK2N0BWX0bFLIZzpjSSF1QEfyZkTcHd10WkP7h9Je X-Received: by 2002:a17:906:c342:: with SMTP id ci2mr30562534ejb.122.1629619115082; Sun, 22 Aug 2021 00:58:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629619115; cv=none; d=google.com; s=arc-20160816; b=IbSrje+lIRYJMROiJOLXUxNQBdd6LUZKn56caZXAKA8884E1fbcA/b9WnKa3drG+xh i6Hd38Vq+W3Wq7cREbOqt8Na+s0A0zlK0L+GxwTkvZM80jo3OYO2WgObbTTXUVWUD7/w v786ZpjrOcb6K5Cuv3nholYlh4R151Vp76e3Q7hZ8+O+e/gmf9xsFWG90vvt9pEOKNDm ilUteA1zoVbZHJfIPUOgnJtHkgmxM9nAmc8+GHbNavWqNYzPFOEkf6yVHQ32xAHZBXx6 yYRZIP+qf6Hlva3QowrNbDA9eiIMEs6bUCAZ3biV9eY/aYBsY1BMvYlSlGWdE4kc1vco WOuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QvdBkUpptZhXKvwrMm3zs3qjx3Jw+Diy7BkjH6c2x9o=; b=lTtNICbWcyKlEO1MkHB4DkY6Klk4xsae7rwJzjSqG8lnUBKP8Ol80gNsZyS7Si50xt 7wvdDP+Sb8TpJBLUzhVX5rre5Y2pC/X3xhVzsmw9g1NUVkybK3Ybb5tZEP7JUM5hXuw/ BLjYTIlwer4Y/cLW4cc5IDnrVAcjQD/trj65whr90B0Bf0eXXTvZclFi2uSc+nqX7CrG 7D1sFQm2Xdb4jHFlbSCTyMy3n1i0VcwZ/R5MqzkRMWpt3ZSSVNXVC0iD8kXKV9YXq+ER yKrWGkOxJeFkvFA9nX1fNc0xPLQyqibO/jHZZ3lQlgVIDfSh94m7vkL39MDRLMpvCq40 5SuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fg7ZhYLv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ne29si12618643ejc.233.2021.08.22.00.58.12; Sun, 22 Aug 2021 00:58:35 -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; dkim=pass header.i=@chromium.org header.s=google header.b=fg7ZhYLv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231147AbhHVH5j (ORCPT + 99 others); Sun, 22 Aug 2021 03:57:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231156AbhHVH5h (ORCPT ); Sun, 22 Aug 2021 03:57:37 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DF8AC061575 for ; Sun, 22 Aug 2021 00:56:56 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id om1-20020a17090b3a8100b0017941c44ce4so16639843pjb.3 for ; Sun, 22 Aug 2021 00:56:56 -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 :mime-version:content-transfer-encoding; bh=QvdBkUpptZhXKvwrMm3zs3qjx3Jw+Diy7BkjH6c2x9o=; b=fg7ZhYLvxUugb8iVfWfC92pGeyt+/r98pwZwtJjW7D5m9+btWV+mAQjVNa2DeXw4uZ TBCIVYhAM3WflYWa48L2s/VrNdwxu7CILWfDneyuRYST6Ezk3jJitCQAyBVZW7Ex0udr QdVPoFRKhE/kfvccRSPKXi6X4fAb/9Tcqr5Xk= 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:mime-version:content-transfer-encoding; bh=QvdBkUpptZhXKvwrMm3zs3qjx3Jw+Diy7BkjH6c2x9o=; b=tX4EyzRqxJpg9QP/UJ0z920Ieicq1ijXBzN3ZDDqV7w6n9NZAMNxH89SFPF0n/9TxZ zmMuCdM26tvPDs0MlofMDfw3BiqOiimIp0fxmh0N8cNvvnUwvn/8D6gVAbWfu76hbcvv l8ZgIbuyCRPbVxieopGNoeZcrpiifzLS3k4LC2os/MQcu4qN0Gz4ckkbPdYl53X8eUPo BratJk0Bs0F6dMq8Y3LG6FzEgHRpdJd0kzcc3+OELd+bXY2CGbt+9ZNSh8huSu2z2rH/ neogVs6/9Q5JEUWRkkEF2ZY8rqoudjYzIcSFBV866tym+nmKmS1V6r1VWPbs/skmdRb6 lxTA== X-Gm-Message-State: AOAM533XOFoN8Fe6JJzebWmMcKuW7bFuCpFyh9RslQ9DTg9pq7b7WiSy 8o9WyRp14Sa8r10C7Zuzxu3gTg== X-Received: by 2002:a17:902:d2cd:b0:130:a7b7:3c8f with SMTP id n13-20020a170902d2cd00b00130a7b73c8fmr10580004plc.46.1629619016141; Sun, 22 Aug 2021 00:56:56 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id y62sm11996200pfg.88.2021.08.22.00.56.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Aug 2021 00:56:55 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Rasmus Villemoes , Daniel Micay , Francis Laniel , Bart Van Assche , David Gow , linux-mm@kvack.org, clang-built-linux@googlegroups.com, linux-hardening@vger.kernel.org Subject: [PATCH for-next 22/25] string.h: Introduce memset_after() for wiping trailing members/padding Date: Sun, 22 Aug 2021 00:51:19 -0700 Message-Id: <20210822075122.864511-23-keescook@chromium.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210822075122.864511-1-keescook@chromium.org> References: <20210822075122.864511-1-keescook@chromium.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2741; h=from:subject; bh=E6gFtKPMaqY94NYGTcjgqzMn4Zg8efYnWFddeRIDocU=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBhIgH50IyFEulz6vf4MWlQ/Ezu82EvPp7pmwWKKNPw FQi+vpGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYSIB+QAKCRCJcvTf3G3AJh3rEA CrFF/z6kOuE4E1nPr3TROjLRJGQjTLaabDuXR/aIm4UQKeKkoTwGA2+A4SqjMTq/+P5Ys7g2X4LT/W Z2l1Vk9U0SrRQPJl7vZYRJ+9O4qUs3Kf8Ny2bIVgxeZT2N0blFHjxagjdD8vAzrY3b1LssVIU5HLiQ CkTZwHf1BnUGyXWqlHau1Vtb3xdCMueRk/f0+dyoPkbCq9ZiKc0XgtZ5MOlzVE66BQLezYe3VZxDUH aDeSYCo7PVgr9pV36o86mUKSUXJWTQ74tdMd1P2VvIhlNEI+vIzBXIjvq2l/IC+Ia2/oqyD/PBLJJk RoXa16m2Lr5ydwZuU+5qMNSo8Cz5OcHc0iQS5x/zK4L2Y6i84HtrS2Ja/RoBQ4SawOGvwkqQMDWUhM tML3SJfr/6qBoVbM7j7oTKiWGwFiZkpavW0iPpwrFqTjTonEi3vdUzO/ahwHLs5uU8gEPP+nPovcfo wZ9AAvOWdDZeSU8i0x0zCI2Hiu5fGcmVETpSSG/F1WqZuQHz8KRI4ndkMy5COuwjY5DADDoBVXuei2 GDhIxOMqkO2jtycMsYYhPX4LOTHskAA/ikwrRY9YVN753cwclPR1h9ULCg29KZIvJfiQ4oFMPNQ49S SyWtCljmqAu2E43Fu6xl6RJf6H3Wn1D9Ei2FNzhed+0om1vGjS5eu9TtBUHg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A common idiom in kernel code is to wipe the contents of a structure after a given member. This is especially useful in places where there is trailing padding. These open-coded cases are usually difficult to read and very sensitive to struct layout changes. Introduce a new helper, memset_after() that takes the target struct instance, the byte to write, and the member name after which the zeroing should start. Additionally adds memset_startat() for wiping trailing members _starting_ at a specific member instead of after a member, which is more readable in certain circumstances, but doesn't include any preceding padding. Signed-off-by: Kees Cook --- include/linux/string.h | 17 +++++++++++++++++ lib/test_memcpy.c | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 9473f81b9db2..d593de2635ba 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -271,6 +271,23 @@ static inline void memcpy_and_pad(void *dest, size_t dest_len, memcpy(dest, src, dest_len); } +/** + * memset_after - Set a value after a struct member to the end of a struct + * + * @obj: Address of target struct instance + * @v: Byte value to repeatedly write + * @member: after which struct member to start writing bytes + * + * This is good for clearing padding following the given member. + */ +#define memset_after(obj, v, member) \ +({ \ + u8 *__ptr = (u8 *)(obj); \ + typeof(v) __val = (v); \ + memset(__ptr + offsetofend(typeof(*(obj)), member), __val, \ + sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \ +}) + /** * str_has_prefix - Test if a string has a given prefix * @str: The string to test diff --git a/lib/test_memcpy.c b/lib/test_memcpy.c index ec546cec883e..3b485de8c885 100644 --- a/lib/test_memcpy.c +++ b/lib/test_memcpy.c @@ -215,6 +215,13 @@ static void memset_test(struct kunit *test) 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, }, }; + struct some_bytes after = { + .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x72, + 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, + 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, + 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, + }, + }; struct some_bytes dest = { }; int count, value; u8 *ptr; @@ -245,6 +252,12 @@ static void memset_test(struct kunit *test) ptr += 8; memset(ptr++, value++, count++); compare("argument side-effects", dest, three); + + /* Verify memset_after() */ + dest = control; + memset_after(&dest, 0x72, three); + compare("memset_after()", dest, after); + #undef TEST_OP } -- 2.30.2