From owner-svn-src-all@freebsd.org Fri May 25 18:57:32 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E3E5AEF1264; Fri, 25 May 2018 18:57:31 +0000 (UTC) (envelope-from araujobsdport@gmail.com) Received: from mail-wm0-x242.google.com (mail-wm0-x242.google.com [IPv6:2a00:1450:400c:c09::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 51CD16E96C; Fri, 25 May 2018 18:57:31 +0000 (UTC) (envelope-from araujobsdport@gmail.com) Received: by mail-wm0-x242.google.com with SMTP id f6-v6so16860420wmc.4; Fri, 25 May 2018 11:57:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to:cc; bh=mBRrx5x4BhoSjZAq9IXJHcvmxk8OiBsWl461K6MCdA4=; b=c9FkEr6CSwq+OwM2YU/7peRscqB1UXxRUebOwIvIT+dhFxXues8/Kn7AWEkkNjvpox gpwA8lYb+fp8PW94MivPidwflXo9MJ2wWeYR4on5v/+JM66yqjX/IKNVme+gAX36uvov Qk2BtZqDkdWncCVhgI53QC8Wx+9z71kKgXzDi99AlU3YqctdtcktdVZBoYboNqbIYwT5 H7eQQZrJBFnrHevl3SRHCM5rRN+Y6fGgED4UHvJNroFvcMqxMJ6l9eJT/W/39lnAg5aK udZFI/qz9S+a+QHajqY1X4LgDyV8cAtbvg++EnMsVuA/dULY6vFMM3452bqqOOk8j/lC PCyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:reply-to:in-reply-to:references :from:date:message-id:subject:to:cc; bh=mBRrx5x4BhoSjZAq9IXJHcvmxk8OiBsWl461K6MCdA4=; b=nda1zlpj3FAXulgEx4REu4AXmbRwmoBsj21qWEmoJNncsRHLATtknXnIfNHadKRq50 P2ab5QXuaVXcTHdsaYH0oIF907KijOIhM2pqZg+RX1DfQNpcnDpA+6xjYuMtuTI1AHNB UwxgNjsJjlFO9ifq1UE0qxX48drAA5JTmZ9hrRX3gWFVki601NLl7/hqzkcRNxz0P3id DyJoMCl5ay3RBE4E6cK0pG1cbSrROsMNyU5cOupyojt9MlR7fuHg3y3XnYQKFSSf5fVr UPscL8P6emxps5d7pyxLfEiz6yixzUecTEOzY628qvv8ARFN1Krc2bw73GP6LdgoXiya xzEA== X-Gm-Message-State: ALKqPwe03bL4mFiARjHfReA1W4AmBXEmXk9JC82h/vgE5zP23jmr+Nmc rGO9Ejdenlvuyw6l5sMzL7MPFi18jACcdOcUgnE= X-Google-Smtp-Source: AB8JxZpJsg93kX84W7bcAnHe0p7QNbjoVWuj1OMHE3rU1NADQs3lNQ0eJ3MBzhAw7bPk96X5gFLF0Ijq+axYu7RqftY= X-Received: by 2002:a2e:9cc7:: with SMTP id g7-v6mr2473632ljj.141.1527274650131; Fri, 25 May 2018 11:57:30 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a19:1fc9:0:0:0:0:0 with HTTP; Fri, 25 May 2018 11:57:29 -0700 (PDT) Reply-To: araujo@freebsd.org In-Reply-To: <20180525183403.i3bt2npfc3fq2cgf@mutt-hbsd> References: <201805250207.w4P275Pf060725@repo.freebsd.org> <20180525151134.GB99063@spindle.one-eyed-alien.net> <20180525174424.GD99063@spindle.one-eyed-alien.net> <20180525182139.GE99063@spindle.one-eyed-alien.net> <20180525183403.i3bt2npfc3fq2cgf@mutt-hbsd> From: Marcelo Araujo Date: Sat, 26 May 2018 02:57:29 +0800 Message-ID: Subject: Re: svn commit: r334199 - head/usr.sbin/bhyve To: Shawn Webb Cc: Brooks Davis , svn-src-head@freebsd.org, Eitan Adler , svn-src-all@freebsd.org, src-committers Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.26 X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 May 2018 18:57:32 -0000 Thanks Shawn, I think there are plenty of places to fix this case! Thanks for the extra work :D. 2018-05-26 2:34 GMT+08:00 Shawn Webb : > On Sat, May 26, 2018 at 02:26:33AM +0800, Marcelo Araujo wrote: > > 2018-05-26 2:21 GMT+08:00 Brooks Davis : > > > > > On Sat, May 26, 2018 at 01:56:28AM +0800, Marcelo Araujo wrote: > > > > 2018-05-26 1:44 GMT+08:00 Brooks Davis : > > > > > > > > > On Sat, May 26, 2018 at 01:21:33AM +0800, Marcelo Araujo wrote: > > > > > > On Sat, May 26, 2018, 1:11 AM Eitan Adler > > > wrote: > > > > > > > > > > > > > On 25 May 2018 at 08:23, Marcelo Araujo < > araujobsdport@gmail.com> > > > > > wrote: > > > > > > > > > > > > > > > > > > > > > > > > On Fri, May 25, 2018, 11:11 PM Brooks Davis < > brooks@freebsd.org> > > > > > wrote: > > > > > > > >> > > > > > > > >> On Fri, May 25, 2018 at 02:07:05AM +0000, Marcelo Araujo > wrote: > > > > > > > >> > Author: araujo > > > > > > > >> > Date: Fri May 25 02:07:05 2018 > > > > > > > >> > New Revision: 334199 > > > > > > > >> > URL: https://svnweb.freebsd.org/changeset/base/334199 > > > > > > > >> > > > > > > > > >> > Log: > > > > > > > >> > Fix a memory leak on topology_parse(). > > > > > > > >> > > > > > > > > >> > strdup(3) allocates memory for a copy of the string, > does > > > the > > > > > copy > > > > > > > and > > > > > > > >> > returns a pointer to it. If there is no sufficient > memory > > > NULL > > > > > is > > > > > > > >> > returned > > > > > > > >> > and the global errno is set to ENOMEM. > > > > > > > >> > We do a sanity check to see if it was possible to > allocate > > > > > enough > > > > > > > >> > memory. > > > > > > > >> > > > > > > > > >> > Also as we allocate memory, we need to free this memory > > > used. > > > > > Or it > > > > > > > >> > will > > > > > > > >> > going out of scope leaks the storage it points to. > > > > > > > >> > > > > > > > > >> > Reviewed by: rgrimes > > > > > > > >> > MFC after: 3 weeks. > > > > > > > >> > X-MFC: r332298 > > > > > > > >> > Sponsored by: iXsystems Inc. > > > > > > > >> > Differential Revision: > https://reviews.freebsd.org/ > > > D15550 > > > > > > > >> > > > > > > > > >> > Modified: > > > > > > > >> > head/usr.sbin/bhyve/bhyverun.c > > > > > > > >> > > > > > > > > >> > Modified: head/usr.sbin/bhyve/bhyverun.c > > > > > > > >> > > > > > > > > >> > > > > > > > > ============================================================ > > > > > ================== > > > > > > > >> > --- head/usr.sbin/bhyve/bhyverun.c Fri May 25 01:38:59 > 2018 > > > > > > > >> > (r334198) > > > > > > > >> > +++ head/usr.sbin/bhyve/bhyverun.c Fri May 25 02:07:05 > 2018 > > > > > > > >> > (r334199) > > > > > > > >> > @@ -193,6 +193,7 @@ topology_parse(const char *opt) > > > > > > > >> > c = 1, n = 1, s = 1, t = 1; > > > > > > > >> > ns = false, scts = false; > > > > > > > >> > str = strdup(opt); > > > > > > > >> > + assert(str != NULL); > > > > > > > >> > > > > > > > >> Using assert seems like an odd choice when you've already > added > > > a > > > > > > > >> failure path and the strsep will crash immediately if > assert is > > > > > elided. > > > > > > > > > > > > > > > > > > > > > > > > Just to make a better point, I had the same discussion about > > > > > assert(3) in > > > > > > > > another review, we don't do NDEBUG even for RELEASE. > > > > > > > > > > > > > > IMHO we only use assert for asserting things ought to never be > > > false > > > > > > > except in buggy code. Using assert for handling is poor > practice. > > > > > > > > > > > > > > > > > > > Again, in this case we are using it all over the place and we > must > > > > > replace > > > > > > it. Also we should document it in somewhere perhaps in the > assert(3) > > > > > > otherwise myself and others will keep using it. If you use find, > not > > > only > > > > > > myself is using it to check strdup! So what is the suggestion to > > > handle > > > > > > assert(3)? Deprecated it? > > > > > > > > > > Code that uses assert() in place of error handling is wrong and > should > > > > > be fixed. assert(condition) means that condition must never happen > > > > > and if it does a bug has occurred (or the programmers assumptions > are > > > > > wrong). In this case failure would not be due to a bug, but do to > > > > > resource exhaustion which is expected to be handled. > > > > > > > > > > > > > I agree with you! We have plenty of place that use strdup(3) without > > > check > > > > the errno ENOMEN return; so do you think would be better bypass a > errno > > > > ENOMEN without check it and have a crash, or better abort(3) using > > > > assert(3) in case we have no memory available to allocated the memory > > > for a > > > > copy of a string? > > > > > > The correct code here would be one of: > > > > > > str = strdup(opt); > > > if (str == NULL) > > > goto out; > > > > > > > No, it is not the correct code! If we go out and free(str) we have > nothing > > to free, because we even didn't allocated memory for str. > > Hey Marcelo, > > I've authored this commit, which fixes the issues Brooks brought up > (and with which I agree): > > https://github.com/HardenedBSD/hardenedBSD/commit/ > 9c05b8def2c33e3889430cc2f54be0402a257366 > > Thanks, > > -- > Shawn Webb > Cofounder and Security Engineer > HardenedBSD > > Tor-ified Signal: +1 443-546-8752 > Tor+XMPP+OTR: lattera@is.a.hacker.sx > GPG Key ID: 0x6A84658F52456EEE > GPG Key Fingerprint: 2ABA B6BD EF6A F486 BE89 3D9E 6A84 658F 5245 6EEE > -- -- Marcelo Araujo (__)araujo@FreeBSD.org \\\'',)http://www.FreeBSD.org \/ \ ^ Power To Server. .\. /_)