From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-it0-x233.google.com ([IPv6:2607:f8b0:4001:c0b::233]:37309 "EHLO mail-it0-x233.google.com" rhost-flags-OK-OK-OK-OK) by eddie.linux-mips.org with ESMTP id S23993869AbdDGDWAsB0G4 (ORCPT ); Fri, 7 Apr 2017 05:22:00 +0200 Received: by mail-it0-x233.google.com with SMTP id a140so36847149ita.0 for ; Thu, 06 Apr 2017 20:22:00 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87mvbtzztb.fsf@intel.com> References: <20170405214711.GA5711@beast> <87mvbtzztb.fsf@intel.com> From: Kees Cook Date: Thu, 6 Apr 2017 20:21:54 -0700 Message-ID: Subject: Re: [PATCH] format-security: move static strings to const Content-Type: text/plain; charset=UTF-8 Return-Path: Sender: linux-mips-bounce@linux-mips.org Errors-to: linux-mips-bounce@linux-mips.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-subscribe: List-owner: List-post: List-archive: To: Jani Nikula Cc: Andrew Morton , Tony Lindgren , Russell King , "Maciej W. Rozycki" , Ralf Baechle , Arnd Bergmann , Greg Kroah-Hartman , "Rafael J. Wysocki" , Viresh Kumar , Daniel Vetter , Sean Paul , David Airlie , Yisen Zhuang , Salil Mehta , Thomas Bogendoerfer , Jes Sorensen , Jiri Slaby , Patrice Chotard , "David S. Miller" , James Hogan , Paul Burton , Matt Redfearn , Paolo Bonzini , Ingo Molnar , Rasmus Villemoes , Mugunthan V N , Felipe Balbi , Jarod Wilson , Florian Westphal , Antonio Quartulli , Dmitry Torokhov , Kejian Yan , Daode Huang , Philippe Reynes , Colin Ian King , Eric Dumazet , Christian Gromm , Andrey Shvetsov , Jason Litzinger , WANG Cong , "linux-arm-kernel@lists.infradead.org" , linux-omap@vger.kernel.org, Linux MIPS Mailing List , Linux PM list , Maling list - DRI developers , Network Development , devel@driverdev.osuosl.org, linux-serial@vger.kernel.org, linux-decnet-user@lists.sourceforge.net, LKML Message-ID: <20170407032154.9cOg8CVSSe-Ten9lYdDkVYWh61X6zsuKFHklGcjM6Ew@z> On Thu, Apr 6, 2017 at 1:48 AM, Jani Nikula wrote: > On Thu, 06 Apr 2017, Kees Cook wrote: >> While examining output from trial builds with -Wformat-security enabled, >> many strings were found that should be defined as "const", or as a char >> array instead of char pointer. This makes some static analysis easier, >> by producing fewer false positives. >> >> As these are all trivial changes, it seemed best to put them all in >> a single patch rather than chopping them up per maintainer. > >> diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c >> index f6d4d9700734..1ff9d5912b83 100644 >> --- a/drivers/gpu/drm/drm_fb_helper.c >> +++ b/drivers/gpu/drm/drm_fb_helper.c >> @@ -2331,7 +2331,7 @@ EXPORT_SYMBOL(drm_fb_helper_hotplug_event); >> int __init drm_fb_helper_modinit(void) >> { >> #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) >> - const char *name = "fbcon"; >> + const char name[] = "fbcon"; > > I'd always write the former out of habit. Why should I start using the > latter? What makes it better? For me, it's mainly two reasons: sizeof() and -Wformat-security behavior. The compiler treats "sizeof" differently. E.g. "sizeof(var)" shows the allocation size for the array, and pointer size for the char pointer. When doing things like snprintf(buf, sizeof(buf), ...) will do the right thing, etc. (This is a poor example for a _const_ string, but the point is that some calculations still work better with the array over the pointer.) The other situation (which is why I noted this to change them) is that gcc's handling of them is different when faced with -Wformat-security since it doesn't like to believe that const char pointers are actually const for the purposes of being a format string. > What keeps the kernel from accumulating tons more of the former? Right now, nothing. The good news is that they're relatively rare, and I notice them when they're added (since I have a -Wformat-security tree). We could add a warning to checkpatch for suggesting const char var[] over const char *var, perhaps? > Here's an interesting comparison of the generated code. I'm a bit > surprised by what gcc does, I would have expected no difference, like > clang. https://godbolt.org/g/OdqUvN Here's your example with sizeof() added, if you're curious... https://godbolt.org/g/U1zIZK > The other changes adding const in this patch are, of course, good. Thanks! -Kees -- Kees Cook Pixel Security