mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pascal Cuoq <cuoq@trust-in-soft.com>
To: "musl@lists.openwall.com" <musl@lists.openwall.com>
Subject: musl's putenv makes assumptions about memcmp
Date: Mon, 21 Aug 2017 05:50:28 +0000	[thread overview]
Message-ID: <A1F6D5AF-081B-4BE2-A744-577CBBA49E5C@trust-in-soft.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1953 bytes --]

Hello,

Quoting myself from a previous report:

when I started testing parts of musl with TIS Interpreter, I made sure to use TIS Interpreter versions of low-level functions such as memcpy and memset, while testing higher-level functions. Musl's functions can provide guarantees beyond the standard, and it is fair game to rely on these guarantees elsewhere in musl since musl's versions of these functions are called, but I thought it would be interesting to know that musl provides additional guarantees and relies on them.

An interesting new example just turned up.

According to the succinct description of memcmp in the C standard, and to the interpretation of Glibc's developers, memcmp should only be called with fully valid buffers, even when a sequential comparison of the two buffers would not make any out-of-bounds access. Please see https://trust-in-soft.com/memcmp-requires-pointers-to-fully-valid-buffers/ for details.

musl's implementation of putenv uses memcmp exactly like the blog post says it shouldn't:

https://git.musl-libc.org/cgit/musl/tree/src/env/putenv.c?id=80bf5952551c002cf12d96deb145629765272db0

This doesn't matter now, because the implementation of memcmp used is musl's, which does sequentially compare characters and does stop as early as it knows what to return: https://git.musl-libc.org/cgit/musl/tree/src/string/memcmp.c?id=80bf5952551c002cf12d96deb145629765272db0

However this behavior of musl's memcmp should be documented and preserved in future versions. Otherwise introducing a memcmp optimization in musl similar to Glibc's would silently break putenv, making it segfault in rare and perhaps hard to reproduce circumstances.

Alternately, it is possible to make putenv rely only on the standard behavior of the functions it calls. It seems to me that strncmp can be used here. The difference in execution speed between memcmp and strncmp should not matter in this context.

Pascal


[-- Attachment #2: Type: text/html, Size: 3166 bytes --]

             reply	other threads:[~2017-08-21  5:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-21  5:50 Pascal Cuoq [this message]
2017-08-21  8:02 ` Alexander Monakov
2017-08-21 12:38   ` Pascal Cuoq

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A1F6D5AF-081B-4BE2-A744-577CBBA49E5C@trust-in-soft.com \
    --to=cuoq@trust-in-soft.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).