Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1241288

Re: [PATCH] string: Improve the generic strlcpy() implementation

Path csiph.com!eternal-september.org!feeder.eternal-september.org!aioe.org!bofh.it!news.nic.it!robomod
From Linus Torvalds <torvalds@linux-foundation.org>
Newsgroups linux.kernel
Subject Re: [PATCH] string: Improve the generic strlcpy() implementation
Date Wed, 07 Oct 2015 11:30:02 +0200
Message-ID <qgTcS-DX-37@gated-at.bofh.it> (permalink)
References <q7g13-2Rm-7@gated-at.bofh.it> <qfTRD-4Np-7@gated-at.bofh.it> <qgc7U-5TG-9@gated-at.bofh.it> <qgmqC-3Uq-19@gated-at.bofh.it> <qgvtU-iV-5@gated-at.bofh.it> <qgIAO-2nd-3@gated-at.bofh.it> <qgRb3-6e5-3@gated-at.bofh.it> <qgSTw-gt-1@gated-at.bofh.it>
X-Original-To Rasmus Villemoes <linux@rasmusvillemoes.dk>
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=SPzd6tcpJpmDuNuuLoyuptYekvKS659VO/FEymFDik4=; b=tP8tdRbwxGHBLiLnLxdXSkdYNCVWKKuQ9568uchuixJZugSpKjxOVnmohay1TDbHSy Qr8DFwg7LfBvdkILl8tJFkGK1PpkFjpsEEZjF6k+RC5InGXNTlIVkwYZlaPLPvx/ARvN KeEs+ytJpVwLJUCq0Hd/pkKQPZtrQc9YfCl6wxct2t+RVwNoAUHjcXitwgkAvu/dW9YR ZkB2jb8VbBL9J6vGAKfCwkGSwL8vy326Omm/F7FT9+QlgDMT/7YJxzrpxzmSd/Uf/VRs RLug+V8IjWpwwRTtKpln9/YHWdTwTHiO+6IkZd9GZoH85oL0e/L/6Ojl7sqf+b4OFsbb L6OA==
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=SPzd6tcpJpmDuNuuLoyuptYekvKS659VO/FEymFDik4=; b=Vgr5OxxbcVzBKMc5w6V3KyQ8jMPW3FVyunc+p/57LmMUwel2PPGPZ8czVBD0CkbNrQ /O4rr9+AQNVjQYf0OCflB7G5JN5XCBmcme2cefi4eOUjkRfzsitgcC0uVv2ORKJcwh4M SHEFW2CjhrJ5Vh7w2Zeon/D4sU7gn+edMZUiw=
MIME-Version 1.0
X-Received by 10.50.114.5 with SMTP id jc5mr444486igb.25.1444209773342; Wed, 07 Oct 2015 02:22:53 -0700 (PDT)
X-Google-Sender-Auth J0M7RSbrKguoBAkhP4oJppCFtCU
Content-Type text/plain; charset=UTF-8
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 75
Organization linux.* mail to news gateway
X-Original-Cc Ingo Molnar <mingo@kernel.org>, Chris Metcalf <cmetcalf@ezchip.com>, open list <linux-kernel@vger.kernel.org>, Peter Zijlstra <a.p.zijlstra@chello.nl>, Thomas Gleixner <tglx@linutronix.de>, "H. Peter Anvin" <hpa@zytor.com>, Borislav Petkov <bp@alien8.de>
X-Original-Date Wed, 7 Oct 2015 10:22:53 +0100
X-Original-Message-ID <CA+55aFx6mSs_7qcove4cnbptRsYWa5De8pM2mD8S-=RGuWQN1A@mail.gmail.com>
X-Original-References <55F1DD53.1070102@ezchip.com> <CA+55aFwHCPnPf_xs6GJu37UBvg_BSiFPH2uQps7qNNFV8Ej-SA@mail.gmail.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>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1241288

Show key headers only | View raw


On Wed, Oct 7, 2015 at 10:04 AM, Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
>
> Well, if truncation has happened the return value is different
> (larger). But assuming the output buffer is large enough, the 'compute
> strlen, then do copying, potentially copying a nul byte which wasn't
> there moments before' is pretty obvious:
[snip]

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.

With strlcpy(), the situation is different at least in the sense that
we *can* write a source-modification-safe version. Of course, the end
result will still be undefined, but at least the resulting string
length in the destination can be made to not disagree violently with
the return value.

Do we care? Probably not. If you do strlcpy() on strings that change
without using locking, it's either a serialization bug, or you really
don't care very deeply about the end result anyway (ie it's something
like the "current->comm" issue). But just from a quality of
implementation standpoint, I think it would be good to just do the
RightThing(tm) anyway.

That's particularly true since we should be able to do it trivially by
just implementing strlcpy() using strscpy() plus the overflow fixup.
But let's wait with that until people are happy about the state of
strscpy. There's absolutely no rush, and in fact the one thing I
absolutely wanted to avoid was to have the introduction of strscpy()
resulting in pointless churn elsewhere, so let's do the *opposite* of
rushing into this, and just say :"ok, some day we should do this, just
in case"

> Here's a few pseudo-datapoints. About half the strlcpy instances (just
> from lazy grepping) has a string literal as src, and those only have
> about 1/8th chance of being aligned.

Hmm. I think gcc actually tends to align string literals - at least on
architectures where unaligned accesses tend to be more expensive and
we do the whole SLOW_UNALIGNEd handling etc.

I don't think gcc does it on x86, but on x86 we don't much care.
Somebody on ppc or ARM might want to check.

                 Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: [PATCH] string: Improve the generic strlcpy() implementation Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-06 00:30 +0200
  Re: [PATCH] string: Improve the generic strlcpy() implementation Ingo Molnar <mingo@kernel.org> - 2015-10-06 10:00 +0200
  Re: [PATCH] string: Improve the generic strlcpy() implementation Ingo Molnar <mingo@kernel.org> - 2015-10-06 10:10 +0200
    Re: [PATCH] string: Improve the generic strlcpy() implementation Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-07 00:10 +0200
      Re: [PATCH] string: Improve the generic strlcpy() implementation Ingo Molnar <mingo@kernel.org> - 2015-10-07 09:20 +0200
        Re: [PATCH] string: Improve the generic strlcpy() implementation Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-07 11:10 +0200
          Re: [PATCH] string: Improve the generic strlcpy() implementation Linus Torvalds <torvalds@linux-foundation.org> - 2015-10-07 11:30 +0200
            Re: [PATCH] string: Improve the generic strlcpy() implementation Ingo Molnar <mingo@kernel.org> - 2015-10-08 10:50 +0200
              Re: [PATCH] string: Improve the generic strlcpy() implementation Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-09 10:20 +0200
                [RFC 0/3] eliminate potential race in string() (was: [PATCH] string: Improve the generic strlcpy() implementation) Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-09 11:20 +0200
                [RFC 1/3] lib/vsprintf.c: pull out padding code from dentry_name() Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-09 11:20 +0200
                [RFC 3/3] lib/vsprintf.c: eliminate potential race in string() Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-09 11:20 +0200
                [RFC 2/3] lib/vsprintf.c: move string() below widen_string() Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-10-09 11:20 +0200
                Re: [RFC 0/3] eliminate potential race in string() (was: [PATCH]  string: Improve the generic strlcpy() implementation) Ingo Molnar <mingo@kernel.org> - 2015-10-10 09:50 +0200

csiph-web