Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp114796lqg; Thu, 29 Feb 2024 22:59:22 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVLcq9QRH10a8vEpA2hdPYFvGwuPGPXkNwyFFoA07fURo3PvfnJKDk1nZLwGq8ueY3wEdUJOjFX/EgowayU512JYNtFXf2OLFy4UOSJyA== X-Google-Smtp-Source: AGHT+IFctssEbeuB7JFMceAspeL/m63Lep2D9SUKC1xkbDn0x+ESzHi5SZ1qAfJBv71Lihi73o5A X-Received: by 2002:a05:6808:d4c:b0:3c1:ad98:95f1 with SMTP id w12-20020a0568080d4c00b003c1ad9895f1mr991903oik.39.1709276362098; Thu, 29 Feb 2024 22:59:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709276362; cv=pass; d=google.com; s=arc-20160816; b=ifbqK1csF+PA6ln/klxIbnUoYLsztuKcou+vKbUx2sPlZwcLLLVzOiAXmLhCMu4wT3 iN3Dz9CeOApR22tMIhOtPB4AaoEj1L40M99xwbiy8YKpKZPSn8JLAmfjndPTgWCPyVFE Uf95VDZDSN4XSkUI8BdKYMOPnSez8pgfTWRCnbOsM1+aaZCBhok5DehQQrL5stZ1fbfp M7WqAgmDBUYhIVdRzngejDkrWEvaUIv/oSGsYDTyJaCnWgRZjFv59YWn68WYv2BTQH49 VdAZIQ3E+dXb78NOF3pEBaGzi15ZSqnhaBuyrtD8n99ZpbBlxJjG/9dK5SIwSTGDL5Pw MRyw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=s5q2oZTrcUjndkt3GXq94X+4YrZUjF/w2ByGXujuE0Y=; fh=8X2/GYlMf2mVy3IQd//Z+H9x0Lmp8H3dLLINe/CR1is=; b=IGQUtokJhC7OHk1jVtsQskdrABfnzRvw6xBmebeqiRDMzKuWPUI+s54emVzpqUGgO9 DmnV/i2YYML4qIjYjXCvdLloZB3zxwmkBx5zwwBvMHIzYjZbBsnpS3Xer4L0HD7N9zb4 BMV6eLRzDVLfVSEK0bPwjQnzCdhaqTz9eNap92WIXrOOTLTt6EEUC7q53g8Yejyepkzk N15uYS3xsu7JTNKsu0l5EASNWF9B2EFQNFYz3XlzmML4c+8uvlO4Y7khUeh8vQftottG W9DqV+igsimGy83dgnxDC0CzMTOrQXdYQYpF+SkTmloqTAwb6HIVOhPRJaBX/P5nmUF3 BmYA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VcS3hDub; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-87981-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87981-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id bx14-20020a056a02050e00b005dc528a9d0bsi3290563pgb.241.2024.02.29.22.59.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 22:59:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87981-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VcS3hDub; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-87981-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87981-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id BE76A288D48 for ; Fri, 1 Mar 2024 06:59:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9A34369315; Fri, 1 Mar 2024 06:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VcS3hDub" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3DA18464; Fri, 1 Mar 2024 06:59:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709276352; cv=none; b=YYiWMho4ncPBrKmR7qBDE+LiG/Q4AFohxtav2IWSVEsQo2t7Fh98b/+9kGQybx68kBGxP+l8qUiB2Fap1Ys8D9+Yjbk9cobOtNPzIXpBlTqZJ2gf0wGwrs3YDcKq8gcbNLkgiFraovhHueyVq8dmzMORz/oYg4aVC2FsAfcO6fY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709276352; c=relaxed/simple; bh=jL1paCHEOa9QLVRu5Ik8YUZsHarWWAgotaMOmfcOHv0=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tFPzhGCzLnLm8p/2zRqmYJfhdVUsTsWB1lmO+JlseHzD4a0H2HVy6oZd4XZED+MTNduIuzhKrz6+KHHmYTUOLU/eECkhGEJVJplS++CAYt4M2h+APejmTpCh2C/hCOt8ED3UHVjCkMRUcmw8zkQtMaXtVm1zvLoahROo7iLG8bI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VcS3hDub; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95CD6C433C7; Fri, 1 Mar 2024 06:59:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709276352; bh=jL1paCHEOa9QLVRu5Ik8YUZsHarWWAgotaMOmfcOHv0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=VcS3hDub6/Yc/PAbRjoUwgS8EtjeYp8Fw7QbenSaHDgjcV2ngKGKtCWzx4IS1E0pn ZTm3hvXBHI0TI4h/yNEVKEBnwC1v3YDu8QO0BY4tsRzKs0w42F73XhNf4Zb9aC3A8v LQPnh8BaZqjOfUN7N+Jml5NF5iAxW2YOLAHy0wb4MQIXcSaDkljxuKmuYPPgdh5D+i 2rY6QXASkRSPICmAcCph2Ha7GQp2DreG8P2pQqXlU2OOEOgueZPUDUmMIcTCIYGjzN guFkKnFmP2ths4UFOjhbHqbFaSvcO6AOTNH+gROcGZB+4yGVi2W3uysUiR8UclLZ/7 cU0nJSpFlf1CQ== Date: Thu, 29 Feb 2024 22:59:10 -0800 From: Jakub Kicinski To: Kees Cook Cc: "David S. Miller" , Eric Dumazet , Paolo Abeni , Andy Shevchenko , "Gustavo A. R. Silva" , netdev@vger.kernel.org, linux-hardening@vger.kernel.org, Simon Horman , Jiri Pirko , Daniel Borkmann , Coco Li , Amritha Nambiar , linux-kernel@vger.kernel.org Subject: Re: [PATCH] netdev: Use flexible array for trailing private bytes Message-ID: <20240229225910.79e224cf@kernel.org> In-Reply-To: <20240229213018.work.556-kees@kernel.org> References: <20240229213018.work.556-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 29 Feb 2024 13:30:22 -0800 Kees Cook wrote: > Introduce a new struct net_device_priv that contains struct net_device > but also accounts for the commonly trailing bytes through the "size" and > "data" members. I'm a bit unclear on the benefit. Perhaps I'm unaccustomed to "safe C". > As many dummy struct net_device instances exist still, > it is non-trivial to but this flexible array inside struct net_device put Non-trivial, meaning what's the challenge? We also do somewhat silly things with netdev lifetime, because we can't assume netdev gets freed by netdev_free(). Cleaning up the "embedders" would be beneficial for multiple reasons. > itself. But we can add a sanity check in netdev_priv() to catch any > attempts to access the private data of a dummy struct. > > Adjust allocation logic to use the new full structure. > > Signed-off-by: Kees Cook > diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h > index 118c40258d07..b476809d0bae 100644 > --- a/include/linux/netdevice.h > +++ b/include/linux/netdevice.h > @@ -1815,6 +1815,8 @@ enum netdev_stat_type { > NETDEV_PCPU_STAT_DSTATS, /* struct pcpu_dstats */ > }; > > +#define NETDEV_ALIGN 32 Unless someone knows what this is for it should go. Align priv to cacheline size. > /** > * struct net_device - The DEVICE structure. > * > @@ -2665,7 +2673,14 @@ void dev_net_set(struct net_device *dev, struct net *net) > */ > static inline void *netdev_priv(const struct net_device *dev) > { > - return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN); > + struct net_device_priv *priv; > + > + /* Dummy struct net_device have no trailing data. */ > + if (WARN_ON_ONCE(dev->reg_state == NETREG_DUMMY)) > + return NULL; This is a static inline with roughly 11,000 call sites, according to a quick grep. Aren't WARN_ONCE() in static inlines creating a "once" object in every compilation unit where they get used? > + priv = container_of(dev, struct net_device_priv, dev); > + return (u8 *)priv->data; > }