From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755337Ab0CVQ5s (ORCPT ); Mon, 22 Mar 2010 12:57:48 -0400 Received: from cantor.suse.de ([195.135.220.2]:60581 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755283Ab0CVQ5r (ORCPT ); Mon, 22 Mar 2010 12:57:47 -0400 Date: Mon, 22 Mar 2010 17:57:41 +0100 Message-ID: From: Takashi Iwai To: Dan Carpenter Cc: Joe Perches , Mauro Carvalho Chehab , Srinivasa Deevi , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: [patch v2] cx231xx: card->driver "Conexant cx231xx Audio" too long In-Reply-To: <20100322165430.GU21571@bicker> References: <20100319114957.GQ5331@bicker> <20100322153909.GC23411@bicker> <1269272627.22616.35.camel@Joe-Laptop.home> <20100322165430.GU21571@bicker> User-Agent: Wanderlust/2.15.6 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.7 Emacs/23.1 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At Mon, 22 Mar 2010 19:54:30 +0300, Dan Carpenter wrote: > > On Mon, Mar 22, 2010 at 05:04:55PM +0100, Takashi Iwai wrote: > > At Mon, 22 Mar 2010 08:43:47 -0700, > > Joe Perches wrote: > > > > > > On Mon, 2010-03-22 at 18:39 +0300, Dan Carpenter wrote: > > > > card->driver is 15 characters and a NULL, the original code could > > > > cause a buffer overflow. > > > > > > > In version 2, I used a better name that Takashi Iwai suggested. > > > > > > Perhaps it's better to use strncpy as well. > > > > strlcpy() would be safer :) > > > > But, in such a case, we want rather that the error is notified at > > build time. > > > > Maybe a macro like below would be helpful to catch such bugs? > > > > #define COPY_STRING(buf, src) \ > > do { \ > > if (__builtin_constant_p(src)) \ > > BUILD_BUG_ON(strlen(src) >= sizeof(buf)); \ > > strcpy(buf, src); \ > > } while (0) > > > > and used like: > > > > struct foo { > > char foo[5]; > > } x; > > > > COPY_STRING(x.foo, "OK"); // OK > > COPY_STRING(x.foo, "1234567890"); // NG > > > > I can do the same thing with Smatch. The smatch check can also find > bugs like this: > > buf = kmalloc(10, GFP_KERNEL); > strcpy(buf, "1234567890"); > > I used smatch to find this bug and 5 others on my allmodconfig w/ staging. > I also found 19 other places that use strcpy() to copy from a large buffer > into a smaller buffer. Ah, nice. > Your idea is nice, but I think anyone who deliberately uses the new > macro is not going to have the bug in the first place. ;) Yeah, in theory, such a code should be never committed because it can be caught at build time ;) Takashi