From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754860AbbJHIsp (ORCPT ); Thu, 8 Oct 2015 04:48:45 -0400 Received: from mail-wi0-f180.google.com ([209.85.212.180]:33297 "EHLO mail-wi0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753960AbbJHIsl (ORCPT ); Thu, 8 Oct 2015 04:48:41 -0400 Date: Thu, 8 Oct 2015 10:48:37 +0200 From: Ingo Molnar To: Linus Torvalds Cc: Rasmus Villemoes , Chris Metcalf , open list , Peter Zijlstra , Thomas Gleixner , "H. Peter Anvin" , Borislav Petkov Subject: Re: [PATCH] string: Improve the generic strlcpy() implementation Message-ID: <20151008084837.GA4729@gmail.com> References: <55F1DD53.1070102@ezchip.com> <20151005112700.GA1096@gmail.com> <87h9m57xwa.fsf@rasmusvillemoes.dk> <20151006080346.GB11523@gmail.com> <8737xnbqtn.fsf@rasmusvillemoes.dk> <20151007071821.GD7837@gmail.com> <87y4ffuk0r.fsf@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * Linus Torvalds wrote: > So I really refuse to worry about the snprintf() family of functions wrt this > race. I don't think it was hugely important for strlcpy() either - more of a > "quality of implementation" issue rather than anything fundamental - but for > snprintf and friends it's an almost unavoidable issue because of how snprintf > works. > > Saying that 'strlcpy()' and 'snprintf("%s")' are equivalent is true only in the > loosest sense. Yes, they return the same return value. Yes, the result string > should be the same. But the two are completely different despite that. > > snprintf() has to handle all the *other* cases than just "%s", including > right-justification, string precision handling, etc etc. It is effectively > impossible to do without doing "strlen()" on the source of the string > beforehand. As a result, snprintf() is fundamentally always going to be racy wrt > the string changing during the call. > > So the simple end result is that we shouldn't worry about it, and if you are > doing snprintf() on a changing string, you should just be aware of it. We *do* > actually do that, for things like "current->comm" that really can change while > being printed out. We just don't care deeply, and have in fact been removing > locks in this area, because the end result is still guaranteed to be > NUL-terminated etc. > > Can we get odd truncated printouts in the (very very very unlikely) case that > the string is being changed? Yes. We just don't care. I do agree mostly, but I think we should still try to achieve the following two properties, if possible sanely+cheaply+cleanly: - the printed string should not contain spurious \0 bytes even if the %s source 'races'. [I think this is true currently.] - the return code should correctly represent what snprintf did to the target string. [This might not be the case currently. But I'm not sure!] Because that's a real concern I think: snprintf() return is used frequently to iterate over buffers, and it should correctly and reliably represent what it did, regardless of what the source buffer does - because snprintf obviously knows what it did to the output buffer, it has full, race-free control over it. Whether left-alignment and other formatting details were calculated correctly, etc. is a secondary concern and cannot be guaranteed, but we should at least guarantee that we generated a single string, that we did nothing else, and that we correctly returned its length. Agreed? Thanks, Ingo