From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Morton Date: Mon, 27 Sep 2010 23:12:03 +0000 Subject: Re: [patch] tlb_uv: handle large snprintf() returns Message-Id: <20100927161203.732833b0.akpm@linux-foundation.org> List-Id: References: <20100816105502.GC645@bicker> In-Reply-To: <20100816105502.GC645@bicker> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Dan Carpenter Cc: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Cliff Wickman , Jack Steiner , Robin Holt , linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org On Mon, 16 Aug 2010 12:55:02 +0200 Dan Carpenter wrote: > snprintf() returns the number of bytes that *would* have been copied if > the buffer was large enough, so it can be larger than the size of the > buffer. In this case it's ok, but let's put a cap on it anyway so it's > easier to audit. > > Signed-off-by: Dan Carpenter > > diff --git a/arch/x86/kernel/tlb_uv.c b/arch/x86/kernel/tlb_uv.c > index 312ef02..5e88b3a 100644 > --- a/arch/x86/kernel/tlb_uv.c > +++ b/arch/x86/kernel/tlb_uv.c > @@ -1012,6 +1012,9 @@ static ssize_t tunables_read(struct file *file, char __user *userbuf, > timeoutsb4reset, ipi_reset_limit, complete_threshold, > congested_response_us, congested_reps, congested_period); > > + if (ret > 300) > + ret = 300; > + > return simple_read_from_buffer(userbuf, count, ppos, buf, ret); > } That 300-byte local array is yuk. Duplicating the "300" later in the function (instead of using sizeof) is also yuk. The code can be deyukked by using sprintf_to_user(), only we don't have one. But we do have kasprintf(). From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760284Ab0I0XMb (ORCPT ); Mon, 27 Sep 2010 19:12:31 -0400 Received: from smtp1.linux-foundation.org ([140.211.169.13]:60330 "EHLO smtp1.linux-foundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756506Ab0I0XM3 (ORCPT ); Mon, 27 Sep 2010 19:12:29 -0400 Date: Mon, 27 Sep 2010 16:12:03 -0700 From: Andrew Morton To: Dan Carpenter Cc: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Cliff Wickman , Jack Steiner , Robin Holt , linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: [patch] tlb_uv: handle large snprintf() returns Message-Id: <20100927161203.732833b0.akpm@linux-foundation.org> In-Reply-To: <20100816105502.GC645@bicker> References: <20100816105502.GC645@bicker> X-Mailer: Sylpheed 2.4.8 (GTK+ 2.12.9; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 16 Aug 2010 12:55:02 +0200 Dan Carpenter wrote: > snprintf() returns the number of bytes that *would* have been copied if > the buffer was large enough, so it can be larger than the size of the > buffer. In this case it's ok, but let's put a cap on it anyway so it's > easier to audit. > > Signed-off-by: Dan Carpenter > > diff --git a/arch/x86/kernel/tlb_uv.c b/arch/x86/kernel/tlb_uv.c > index 312ef02..5e88b3a 100644 > --- a/arch/x86/kernel/tlb_uv.c > +++ b/arch/x86/kernel/tlb_uv.c > @@ -1012,6 +1012,9 @@ static ssize_t tunables_read(struct file *file, char __user *userbuf, > timeoutsb4reset, ipi_reset_limit, complete_threshold, > congested_response_us, congested_reps, congested_period); > > + if (ret > 300) > + ret = 300; > + > return simple_read_from_buffer(userbuf, count, ppos, buf, ret); > } That 300-byte local array is yuk. Duplicating the "300" later in the function (instead of using sizeof) is also yuk. The code can be deyukked by using sprintf_to_user(), only we don't have one. But we do have kasprintf().