Skip to content

Conversation

MathiasVP
Copy link
Contributor

@MathiasVP MathiasVP commented Jul 11, 2023

This PR fixes both the barriers in cpp/invalid-pointer-deref. That is, the first commit fixes the barrier InvalidPointerToDereference.qll, and the fifth commit fixes the barrier in AllocationToInvalidPointer.qll.

Both barriers should now come with (very informal) correctness proofs.

@github-actions github-actions bot added the C++ label Jul 11, 2023
@MathiasVP MathiasVP marked this pull request as ready for review July 12, 2023 08:20
@MathiasVP MathiasVP requested a review from a team as a code owner July 12, 2023 08:20
geoffw0
geoffw0 previously approved these changes Jul 13, 2023
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QL changes seem reasonable (though I won't pretend to have a deep understanding of all of them). Test results speak for themselves.

On DCA we lose 6 results, which appear to be false positives as well (as best I can tell).

@MathiasVP MathiasVP marked this pull request as draft July 25, 2023 08:17
@MathiasVP MathiasVP force-pushed the fix-barriers-in-invalid-pointer-deref branch from 7d0a2cb to c2e2525 Compare July 25, 2023 08:18
@MathiasVP MathiasVP force-pushed the fix-barriers-in-invalid-pointer-deref branch from c2e2525 to cd19876 Compare July 25, 2023 10:27
@MathiasVP MathiasVP force-pushed the fix-barriers-in-invalid-pointer-deref branch from cd19876 to af07efe Compare August 5, 2023 19:59
@MathiasVP
Copy link
Contributor Author

DCA looks good. We lose the same 6 results as we did on the very first DCA run (which both Geoffrey and I confirmed to be FPs) 🎉

@MathiasVP MathiasVP marked this pull request as ready for review August 7, 2023 09:53
@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Aug 7, 2023
@MathiasVP
Copy link
Contributor Author

@jketema ce9b018 moves the check out of the predicate as we discussed offline. I hope that makes things more clear. It certainly makes the comments easier to write (since we now have more names for things 😂).

@MathiasVP MathiasVP force-pushed the fix-barriers-in-invalid-pointer-deref branch from 14c1143 to 6d949cb Compare August 10, 2023 12:19
@MathiasVP MathiasVP force-pushed the fix-barriers-in-invalid-pointer-deref branch from 58de7c3 to 2164069 Compare August 11, 2023 12:43
@jketema
Copy link
Contributor

jketema commented Aug 11, 2023

I believe I understand the barrier changes to AllocationToInvalidPointer.qll, and they make sense to me.

…InvalidPointerToDereference.qll

Co-authored-by: Jeroen Ketema <93738568+jketema@users.noreply.github.com>
@jketema
Copy link
Contributor

jketema commented Aug 14, 2023

Did we do a DCA run after we fixed the off-by-one issue?

@MathiasVP
Copy link
Contributor Author

Did we do a DCA run after we fixed the off-by-one issue?

I haven't done one yet, but I suppose I might as well start one now, yeah 👍

Copy link
Contributor

@jketema jketema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM provided DCA comes back ok.

@MathiasVP
Copy link
Contributor Author

MathiasVP commented Aug 14, 2023

The DCA results are in: We still lose the 6 FPs as before, but we now gain a couple of new results that I'm investigating. So far (will update as I go along):

Wireshark

One new result related to a complex loop that depends on the input being a multiple of 4 that looks like:

for (i=ICQ5_CL_SESSIONID; i < size; i+=4 ) {
    k = key+table_v5[i&0xff];
    if ( i != 0x16 ) {
        bfr[i] ^= (guchar)(k & 0xff);
        bfr[i+1] ^= (guchar)((k & 0xff00)>>8); // <--- We now raise an alert here
    }
    if ( i != 0x12 ) {
        bfr[i+2] ^= (guchar)((k & 0xff0000)>>16);
        bfr[i+3] ^= (guchar)((k & 0xff000000)>>24);
    }
}

Vim

A very strange loop that changes the number of iterations, and does different things depending on the current value that's incremented 😭:

for (pos.col = 0; pos.col < len; pos.col += width) {
		if (vterm_screen_get_cell(screen, pos, &cell) == 0) {
			width = 1;
			CLEAR_POINTER(p + pos.col);
			if (ga_grow(&ga, 1) == OK)
					ga.ga_len += utf_char2bytes(' ',
								(char_u *)ga.ga_data + ga.ga_len);
		}
		else width = cell.width;

	cell2cellattr(&cell, &p[pos.col]);
	if (width == 2)
			// second cell of double-width character has the
			// same attributes.
			p[pos.col + 1] = p[pos.col]; // <--- We now raise an alert here
	/* ... */
}

Nelson

This alert LGTM, but it may be a FP because allocateArrayOf may allocate nbElements + 1 number of elements (in which case the code is fine). I haven't verified this yet, though. In any case, I'm happy with this result as it does look like something the current query is expected to find (i.e., while there's a barrier that's making k a safe index, the same thing cannot be said about k + 1). So I'm happy with this result.

double* ptrComplex = (double*)ArrayOf::allocateArrayOf(NLS_DCOMPLEX, nbElements, stringVector(), false);
auto* strElements = (ArrayOf*)A.getDataPointer();
indexType q = 0;
for (indexType k = 0; k < nbElements; k = k + 1) {
    ArrayOf element = strElements[k];
    if (element.getDataClass() == NLS_CHAR) {
        std::wstring str = element.getContentAsWideString();
        bool wasConverted = false;
        doublecomplex asComplex = stringToDoubleComplex(str, wasConverted);
        if (wasConverted) {
            ptrComplex[q] = asComplex.real();
            ptrComplex[q + 1] = asComplex.imag();
        } else {
            ptrComplex[q] = std::nan("NaN");
            ptrComplex[q + 1] = 0;
        }
    } else {
        ptrComplex[k] = std::nan("NaN");
        ptrComplex[k + 1] = 0; // <--- We now raise an alert here
    }
}

@MathiasVP MathiasVP merged commit 9359bea into github:main Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants