Page MenuHomeFreeBSD

dtrace: Fix /"string" == NULL/ comparisons using an uninitialized value.
ClosedPublic

Authored by bdrewery on Dec 18 2020, 6:10 PM.

Details

Summary

A test of this is funcs/tst.strtok.d which has this filter:

BEGIN
/(this->field = strtok(this->str, ",")) == NULL/
{
        exit(1);
}

The test will randomly fail with exit status of 1 indicating that this->field
was NULL even though printing it out shows it is not.

This is compiled to the DTrace instruction set:

// Pushed arguments not shown here
// call strtok() and set result into %r1
07: 2f001f01    call DIF_SUBR(31), %r1          ! strtok
// set thread local scalar this->field from %r1
08: 39050101    stls %r1, DT_VAR(1281)          ! DT_VAR(1281) = "field"
// Prepare for the == comparison
// Set right side of %r2 to NULL
09: 25000102    setx DT_INTEGER[1], %r2         ! 0x0
// string compare %r1 (strtok result) to %r2
10: 27010200    scmp %r1, %r2

In this case only %r1 is loaded with a string limit set to lim1. %r2 being
NULL does not get loaded and does not set lim2. Then we call dtrace_strncmp()
with MIN(lim1, lim2) resulting in passing 0 and comparing neither side.
dtrace_strncmp() handles this case fine and it already has been while
being lucky with what lim2 was [un]initialized as.

Specifically in SCMP we are wanting to see cc_r = !0 in this case.

Sponsored by: Dell EMC

Diff Detail

Repository
R10 FreeBSD src repository
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

sys/modules/dtrace/dtrace/Makefile
64

This is spread all over the CDDL codebase (and ZFS). I am just limiting the scope of the change here but do have interest to remove -Wno-unused as well in all other cases.

markj added inline comments.
sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c
6382

The reason for using 1 is basically that dtrace_strncmp() treats null the same as the empty string, and lim is supposed to include the nul terminator. IMO it would be clearer to state that explicitly.

sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c
6382

Sure I can do that. The value doesn't matter much as long as it is >0.

This revision was not accepted when it landed; it landed in state Needs Review.Jan 8 2021, 10:39 PM
This revision was automatically updated to reflect the committed changes.