Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp3099908ybc; Thu, 21 Nov 2019 03:21:40 -0800 (PST) X-Google-Smtp-Source: APXvYqxnAxMrxRv7acHVxnEdwmSat65QLw5dnK1I+IFgvdoVfVUsInZcNTEzwKOoLxK/z4OAIlK0 X-Received: by 2002:a17:906:278a:: with SMTP id j10mr13274741ejc.125.1574335300262; Thu, 21 Nov 2019 03:21:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574335300; cv=none; d=google.com; s=arc-20160816; b=l0HqKtUgs93nZh4rQHdmC2AxKh9BVBIf0k3XRwGRsr0IEBD9tLj2TTdq5XQ6UMEydy xi++wfHoDhq9Q8s9tG7fnvSbYhYj6yg2+u65wYweuS7ejeKrfIy6w2Gz9B3gvrWWuzN+ 3oFLI+MJWo38FQQlMLSsUDhG++iH2wJaHWMR2tZROBn8PxFrZv4EbiJIjxF0B+LPjpEJ 6se8XC3HSxYvXkRuOCh1OO3x9OzgNfmKgB442mMV260UgZedY7ITmTbCQvK3ojlmDk5r qMcW4taHspaeFHTZDJu2bHSroCp0cMBZDX/3cQ0/Bp3TYry0/oUP+S4x4dYVQQHEsLj4 oBeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=gzvSlr5O34GEctGYyyD8HjVkp9oKmEMBIwx3ahcgEQI=; b=InpaP2r2d/jaDZO/76rgrSMMJC3W6xpD3AR7jaj+7K1Fvu7/ggby297rY3Ju5j/gIy LEUT2dGgMvnOdjHWZA9xUht1tmTPGERly160vpjqZViaZ6KpueocsoWMS8rXjuUAvrZQ KzXEZvy6pt0X5RGnK8s5cX2A/tZ2+2kWBvov/mrhkbRTkmA5F+oPE4M0gaiG7uENT9Ib XKvgnK2CM1LROTdMZqNjCYAA6vRnYlARIEH5hq49/ai85MgrkqYZmZIXSJeRUFIn+PPK 9vmndDyiwNFlDDoX0ZxabdxwUoXq3FGbac9VEcKQh6DkJSukW0V6Ysyp/+igIU1AGMMe I4rw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gx24si1586834ejb.429.2019.11.21.03.21.16; Thu, 21 Nov 2019 03:21:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727016AbfKULTW (ORCPT + 99 others); Thu, 21 Nov 2019 06:19:22 -0500 Received: from mx2.suse.de ([195.135.220.15]:60160 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726165AbfKULTW (ORCPT ); Thu, 21 Nov 2019 06:19:22 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 93A30B049; Thu, 21 Nov 2019 11:19:19 +0000 (UTC) Received: by unicorn.suse.cz (Postfix, from userid 1000) id 9A4A9E03A4; Thu, 21 Nov 2019 12:19:17 +0100 (CET) Date: Thu, 21 Nov 2019 12:19:17 +0100 From: Michal Kubecek To: "Enrico Weigelt, metux IT consult" Cc: Joe Perches , zhanglin , davem@davemloft.net, cocci , Andrew Morton , Thomas Gleixner , Linus Torvalds , jakub.kicinski@netronome.com, ast@kernel.org, jiang.xuexin@zte.com.cn, f.fainelli@gmail.com, daniel@iogearbox.net, john.fastabend@gmail.com, lirongqing@baidu.com, maxime.chevallier@bootlin.com, vivien.didelot@gmail.com, dan.carpenter@oracle.com, wang.yi59@zte.com.cn, hawk@kernel.org, arnd@arndb.de, jiri@mellanox.com, xue.zhihong@zte.com.cn, natechancellor@gmail.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linyunsheng@huawei.com, pablo@netfilter.org, bpf@vger.kernel.org Subject: Re: [Cocci] [PATCH] net: Zeroing the structure ethtool_wolinfo in ethtool_get_wol() Message-ID: <20191121111917.GE29650@unicorn.suse.cz> References: <1572076456-12463-1-git-send-email-zhang.lin16@zte.com.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 21, 2019 at 11:23:34AM +0100, Enrico Weigelt, metux IT consult wrote: > On 26.10.19 21:40, Joe Perches wrote: > > On Sat, 2019-10-26 at 15:54 +0800, zhanglin wrote: > >> memset() the structure ethtool_wolinfo that has padded bytes > >> but the padded bytes have not been zeroed out. > > [] > >> diff --git a/net/core/ethtool.c b/net/core/ethtool.c > > [] > >> @@ -1471,11 +1471,13 @@ static int ethtool_reset(struct net_device *dev, char __user *useraddr) > >> > >> static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) > >> { > >> - struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; > >> + struct ethtool_wolinfo wol; > >> > >> if (!dev->ethtool_ops->get_wol) > >> return -EOPNOTSUPP; > >> > >> + memset(&wol, 0, sizeof(struct ethtool_wolinfo)); > >> + wol.cmd = ETHTOOL_GWOL; > >> dev->ethtool_ops->get_wol(dev, &wol); > >> > >> if (copy_to_user(useraddr, &wol, sizeof(wol))) > > > > It seems likely there are more of these. > > > > Is there any way for coccinelle to find them? > > Just curios: is static struct initialization (on stack) something that > should be avoided ? I've been under the impression that static > initialization allows thinner code and gives the compiler better chance > for optimizations. Not in general. The (potential) problem here is that the structure has padding and it is as a whole (i.e. including the padding) copied to userspace. While I'm not aware of a compiler that wouldn't actually initialize the whole data block including the padding in this case, the C standard provides no guarantee about that so that to be sure we cannot leak leftover kernel data to userspace, we need to explicitly initialize the whole block. If the structure is not going to be copied to userspace (or otherwise exposed), using the initializer is fully sufficient and looks cleaner. Michal Kubecek