From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=HTML_MESSAGE, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: (qmail 20102 invoked from network); 17 Apr 2020 15:56:23 -0000 Received-SPF: pass (mother.openwall.net: domain of lists.openwall.com designates 195.42.179.200 as permitted sender) receiver=inbox.vuxu.org; client-ip=195.42.179.200 envelope-from= Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with UTF8ESMTPZ; 17 Apr 2020 15:56:23 -0000 Received: (qmail 1672 invoked by uid 550); 17 Apr 2020 15:56:18 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 1638 invoked from network); 17 Apr 2020 15:56:18 -0000 From: Pascal Cuoq To: "musl@lists.openwall.com" Thread-Topic: Invalid pointer subtractions in __shlim and __shgetc Thread-Index: AQHWFNC0VEu0t//oAEethmur3WJvWQ== Date: Fri, 17 Apr 2020 15:56:06 +0000 Message-ID: <1587138997905.95619@trust-in-soft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [93.6.34.187] Content-Type: multipart/alternative; boundary="_000_158713899790595619trustinsoftcom_" MIME-Version: 1.0 Subject: [musl] Invalid pointer subtractions in __shlim and __shgetc --_000_158713899790595619trustinsoftcom_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable ?Hello, both functions `__shlim` and `__shgetc` subtract the members named `buf` and `rpos` of the struct they manipulate. In `__shlim`, this happens in the statement `f->shcnt =3D f->buf - f->rpos;= `. And in `__shgetc`, in happens inside the `shcnt` macro: #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf)) In our tests, while running `testsuite` in `libc-testsuite`, both the `__shlim` and `__shgetc` functions are reached with `f->buf` non-null and `f->rpos` a null pointer. This can be made visible on execution platforms other than ours by adding a statement at the beginning of the functions: + if (f->buf && !f->rpos) dprintf (2, "XXX Problem in __shlim\n"); + if (f->buf && !f->rpos) dprintf (2, "XXX Problem in __shgetc\n"); Then if, running `libc-testsuite`, you see the following, it means that `f->buf` was non-null and `f->rpos` was null when these points were reached: $ ./testsuite fdopen test passed fcntl test passed fnmatch test passed XXX Problem in __shlim XXX Problem in __shgetc XXX Problem in __shlim XXX Problem in __shgetc XXX Problem in __shlim XXX Problem in __shgetc XXX Problem in __shlim XXX Problem in __shgetc XXX Problem in __shlim XXX Problem in __shgetc XXX Problem in __shlim XXX Problem in __shgetc fscanf test passed (...) This has been tested on the (tag: v1.2.0) branch of git://git.musl-libc.org= /musl These pointer subtractions are undefined behavior. This is slightly worse than computing `(char*)0-(char*)0`, which is undefined in C and defined in = C++, because compilers for both C and C++ are unlikely to exploit this one for optimization. Subtracting between a non-null pointer and a null pointer on the other hand is undefined behavior in both languages, and it is plausible that doing it may someday have unexpected consequences. I mention this because similar undefined behaviors that were extremely unlikely to cause harm have been fixed in musl in recent months, so that this looks like something you may want to fix too. Pascal --_000_158713899790595619trustinsoftcom_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
​Hello,

both functions `__shlim` and `__shgetc` subtract the members
named `buf` and `rpos` of the struct they manipulate.

In `__shlim`, this happens in the statement `f->shcnt =3D f->buf= - f->rpos;`.
And in `__shgetc`, in happens inside the `shcnt` macro:

#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
  
In our tests, while running `testsuite` in `libc-testsuite`,
both the `__shlim` and `__shgetc` functions are reached
with `f->buf` non-null and `f->rpos` a null pointer.

This can be made visible on execution platforms other than ours
by adding a statement at the beginning of the functions:

+      if (f->buf && !f->rpos) dprint= f (2, "XXX Problem in __shlim\n");
+      if (f->buf && !f->rpos) dprint= f (2, "XXX Problem in __shgetc\n");

Then if, running `libc-testsuite`, you see the following, it mean= s that 
`f->buf` was non-null and `f->r= pos` was null when these points were
reached:

$ ./testsuite
fdopen test passed
fcntl test passed
fnmatch test passed
XXX Problem in __shlim
XXX Problem in __shgetc
XXX Problem in __shlim
XXX Problem in __shgetc
XXX Problem in __shlim
XXX Problem in __shgetc
XXX Problem in __shlim
XXX Problem in __shgetc
XXX Problem in __shlim
XXX Problem in __shgetc
XXX Problem in __shlim
XXX Problem in __shgetc
fscanf test passed
(...)

This has been tested on the (tag: v1.2.0) branch of git://git.musl-lib= c.org/musl

These pointer subtractions are undefined behavior. This is slightly wo= rse
than computing `(char*)0-(char*)0`, which is undefined in C and define= d in C++,
because compilers for both C and C++ are unlikely to exploit t= his one
for optimization. Subtracting between a non-null pointer and a nu= ll pointer 
on the other hand is undefi= ned behavior in both languages, and it is
plausible that doing it may someday h= ave unexpected consequences.

I mention this because similar undefined behaviors that were extremely=
unlikely to cause harm have been fixed in musl in recent months,
so that this looks like something you may want to fix too.

Pascal



--_000_158713899790595619trustinsoftcom_--