From owner-svn-src-head@freebsd.org Thu Feb 21 18:23:25 2019 Return-Path: Delivered-To: svn-src-head@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 DC2D514EB1A2; Thu, 21 Feb 2019 18:23:24 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 7900186DD5; Thu, 21 Feb 2019 18:23:24 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from John-Baldwins-MacBook-Pro-3.local (ralph.baldwin.cx [66.234.199.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: jhb) by smtp.freebsd.org (Postfix) with ESMTPSA id 84F8FCBCD; Thu, 21 Feb 2019 18:23:23 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Subject: Re: svn commit: r344389 - head/usr.sbin/newsyslog To: Warner Losh , Garrett Cooper Cc: Bruce Evans , David Bright , src-committers , svn-src-all@freebsd.org, svn-src-head@freebsd.org References: <201902202205.x1KM5iZX036319@repo.freebsd.org> <20190221121712.Y989@besplex.bde.org> <3CD59489-0595-4D09-B5C9-C3F25D23BB8D@gmail.com> From: John Baldwin Openpgp: preference=signencrypt Autocrypt: addr=jhb@FreeBSD.org; keydata= mQGiBETQ+XcRBADMFybiq69u+fJRy/0wzqTNS8jFfWaBTs5/OfcV7wWezVmf9sgwn8TW0Dk0 c9MBl0pz+H01dA2ZSGZ5fXlmFIsee1WEzqeJzpiwd/pejPgSzXB9ijbLHZ2/E0jhGBcVy5Yo /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Z16 4yrZgIXFdKj2t8F+x613/SJW1lIr9/bDp4U9tw0V1g3l2dFtD3p3ZrQ3hpoDtoK70ioIAjjH aIXIAcm3FGZFXy503DOA0KaTWwvOVdYCFLm3zWuSOmrX/GsEc7ovasOWwjPn878qVjbUKWwx Q4QkF4OhUV9zPtf9tDSAZ3x7QSwoKbCoRCZ/xbyTUPyQ1VvNy/mYrBcYlzHodsaqUDjHuW+I SQQYEQIACQUCRND5ggIbDAAKCRBy3lIGd+N/BCO8AJ9j1dWVQWxw/YdTbEyrRKOY8YZNwwCf afMAg8QvmOWnHx3wl8WslCaXaE8= Message-ID: Date: Thu, 21 Feb 2019 10:22:55 -0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 7900186DD5 X-Spamd-Bar: ------ Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-6.88 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.88)[-0.883,0]; REPLY(-4.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Feb 2019 18:23:25 -0000 On 2/20/19 9:20 PM, Warner Losh wrote: > On Wed, Feb 20, 2019, 9:59 PM Enji Cooper >> >>> On Feb 20, 2019, at 5:17 PM, Bruce Evans wrote: >>> >>> On Wed, 20 Feb 2019, David Bright wrote: >>> >>>> Log: >>>> Complete fix for CID 1007454, CID 1007453: Resource leak in newsyslog >>>> >>>> The result of a strdup() was stored in a global variable and not freed >>>> before program exit. This is a follow-up to r343906. That change >>> >>> This was an especially large bug in Coverity. Understanding that exit(3) >>> exits is about the first thing to understand for a checker. >>> >>> Now it is also a style bug in the source code. >>> >>>> attempted to plug these resource leaks but managed to miss a code path >>>> on which the leak still occurs. Plug the leak on that path, too. >>> >>>> Modified: head/usr.sbin/newsyslog/newsyslog.c >>>> >> ============================================================================== >>>> --- head/usr.sbin/newsyslog/newsyslog.c Wed Feb 20 21:24:56 2019 >> (r344388) >>>> +++ head/usr.sbin/newsyslog/newsyslog.c Wed Feb 20 22:05:44 2019 >> (r344389) >>>> @@ -793,6 +793,9 @@ usage(void) >>>> fprintf(stderr, >>>> "usage: newsyslog [-CFNPnrsv] [-a directory] [-d directory] >> [-f config_file]\n" >>>> " [-S pidfile] [-t timefmt] [[-R tagname] file >> ...]\n"); >>>> + /* Free global dynamically-allocated storage. */ >>>> + free(timefnamefmt); >>>> + free(requestor); >>>> exit(1); >>>> } >>> >>> There was no leak here. exit(3) frees storage much more finally than >>> free(3). >>> >>> It is especially obvious that there is no leak here, since the exit() is >>> 1-2 lines later than the frees. >>> >>> In theory, exit() might fail because it tries to allocate 100 MB more >>> storage but wouldn't fail if 100 bytes are freed here (applications can >>> easily do this foot shooting by allocating without freeing in atexit() >>> destructors). In practice, even allocation failures "can't happen", >>> except in programs that use setrlimit followed but foot shooting to test >>> the limits. setrlimit is now broken for this purpose, since it doesn't >>> limit allocations done using mmap() instead of break(), and malloc() now >>> uses mmap(). >>> >>> If coverity understood this and wanted to spam you with warnings, then it >>> would not warn about this, but would warn about more important things >> like >>> failure to fflush() or fclose() or check for or handle errors for all >>> open streams before calling exit(). Also, if all callers of usage() are >>> not understood, for failures to switch stderr to unbuffered mode before >>> using it in usage(). >>> >>> The error reporting is even harder to do if stderr is not available. >>> Windowing systems and even curses need to do lots more cleanup _before_ >>> exit() and it may be difficult to clean up enough to print error messages >>> using the windowing system. >> >> I agree with Bruce. Items like these should be ignored in the Coverity UI >> as false positives with reasoning, like “global variables; freed on exit”. >> >> As others have noted in past mailing threads, freeing variables on exit >> can cause applications to hang for a period of time, while the memory is >> being reclaimed. I think it’s best to ignore these kinds of allocations on >> exit to avoid introducing unnecessary complexity in the program, as they’re >> benign issues. >> > > > It's been a long running debate since 92 or so when purify came out and > this problem started to be found. In the last 25 years the question hasn't > been settled. I tend to think it's a waste of time, though I get that > issues like this create a lot of false positives. I'm +1 on Bruce's point on this. I find it similar to the recent spate of adding pointless '__dead2' annotations to usage functions that unconditionally call exit() (and thus are already inferred as __dead2 by any compiler written in this millenium) -- John Baldwin