From: Jan Kara Subject: Re: [PATCH] audit: Fix check of return value of strnlen_user() Date: Thu, 4 Jun 2015 23:32:04 +0200 Message-ID: <20150604213204.GA24919@quack.suse.cz> References: <1433257709-1530-1-git-send-email-jack@suse.cz> <1616713.xG362InQ3V@sifl> <20150604073637.GA5923@quack.suse.cz> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t54LWAq8026391 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Thu, 4 Jun 2015 17:32:11 -0400 Received: from mx2.suse.de (cantor2.suse.de [195.135.220.15]) by mx1.redhat.com (Postfix) with ESMTPS id 09D862D44E8 for ; Thu, 4 Jun 2015 21:32:10 +0000 (UTC) Content-Disposition: inline In-Reply-To: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-audit-bounces@redhat.com Errors-To: linux-audit-bounces@redhat.com To: Paul Moore Cc: linux-audit@redhat.com, Jan Kara List-Id: linux-audit@redhat.com On Thu 04-06-15 09:18:49, Paul Moore wrote: > On Thu, Jun 4, 2015 at 3:36 AM, Jan Kara wrote: > > On Wed 03-06-15 14:56:18, Paul Moore wrote: > >> On Tuesday, June 02, 2015 05:08:29 PM Jan Kara wrote: > >> > strnlen_user() returns 0 when it hits fault, not -1. Fix the test in > >> > audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless > >> > there's a kernel bug so it's mostly a cosmetic fix. > >> > > >> > CC: Paul Moore > >> > Signed-off-by: Jan Kara > >> > --- > >> > kernel/auditsc.c | 2 +- > >> > 1 file changed, 1 insertion(+), 1 deletion(-) > >> > > >> > diff --git a/kernel/auditsc.c b/kernel/auditsc.c > >> > index 9fb9d1cb83ce..bb947ceeee4d 100644 > >> > --- a/kernel/auditsc.c > >> > +++ b/kernel/auditsc.c > >> > @@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct > >> > audit_context *context, * for strings that are too long, we should not have > >> > created > >> > * any. > >> > */ > >> > - if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) { > >> > + if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) { > >> > >> While we're at it, should we make it just "len > MAX_ARG_STRLEN" as well? > >> Reading the comments in include/uapi/linux/binfmts.h as well as > >> valid_arg_len() that seems to be the correct logic. > > > > Umm, but audit_log_single_execve_arg() does decrement 1 from > > strnlen_user() result before doing the comparison. So the current test > > seems to match the one in valid_arg_len() exactly... > > For reference (taken from fs/exec.c in Linus' tree just now): > > static bool valid_arg_len(struct linux_binprm *bprm, long len) > { > return len <= MAX_ARG_STRLEN; > } > > The valid_arg_len() returns true when the length is less than or equal > to MAX_ARG_STRLEN, implying that lengths greater than MAX_ARG_STRLEN > are invalid. The existing test in audit_log_single_execve_arg() > treats lengths greater than (MAX_ARG_STRLEN-1) as invalid. But the 'len' passed to valid_arg_len() is the return value of strnlen_user() and that one returns lenght *including* the terminating '\0'. So in fact valid_arg_len() tests whether the argument length is <= (MAX_ARG_STRLEN-1). Hmm? Honza -- Jan Kara SUSE Labs, CR