Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OpenArena on glibc 2.37+ and replace all strncpy calls #659

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

zturtleman
Copy link

macOS (targeting newer SDKs) and glibc 2.37 changes broke overlapping dest and src in strncpy() used by OpenArena 0.8.8 QVMs. (Technically it was undefined behavior.)

Add Q_strncpy() by Eugene C. from Quake3e that allows overlapping dest and src for QVM strncpy syscalls.

I made everything use Q_strncpyz and it use the new Q_strncpy function. I made QVMs remap the Q_strncpy call to the strncpy syscall in case there is a better idea of how to handle it in the future. I tried to make "strncpy" behave the same in QVMs and DLLs.

Also fix a CGame network buffer overflow due to mishandling of strncpy. NUL terminator is great but it's better when it's in the destination buffer.

Fixes #639.

It can't overflow because buff and text have the same max length (1024
and MAX_TOKENLENGTH).
macOS (targeting newer SDKs) and glibc 2.37 changes broke overlapping
strncpy() used by OpenArena 0.8.8 QVMs. (Technically it was undefined
behavior.)

Add Q_strncpy() by Eugene C. from Quake3e that allows overlapping dest
and src for QVM strncpy syscalls.

I made Q_strncpyz() use it for consistency and made QVMs remap the
Q_strncpy() call to the strncpy syscall in case there is a better idea
of how to handle strncpy in the future. Try to handle strncpy the same
in QVMs and DLLs.
@zturtleman zturtleman marked this pull request as draft May 20, 2024 19:32
@zturtleman
Copy link
Author

  • I still need to test glibc 2.37+.
  • I should probably reword "allows overlapping dest and src" as it only works correctly if dest <= src.
  • Still debating just using an ifdef Q3_VM in Q_strncpyz to call strncpy instead of adding Q_strncpy in *_syscalls.asm.

mgerhardy added a commit to PadWorld-Entertainment/worldofpadman that referenced this pull request May 30, 2024
ioquake/ioq3#659

remapping strncpy to the new Q_strncpy is still missing
@ec-
Copy link

ec- commented Jun 7, 2024

I'm implemented both src >= dst and src < dst cases :

char *Q_strncpy( char *dest, char *src, int destsize )
{
	char *s = src, *start = dest;
	int src_len;

	while ( *s != '\0' )
		++s;
	src_len = (int)(s - src);
	
	if ( src_len > destsize ) {
		src_len = destsize;
	}
	destsize -= src_len;

	if ( dest > src && dest < src + src_len ) {
		int i;
#ifdef _DEBUG
		Com_Printf( S_COLOR_YELLOW "Q_strncpy: overlapped (dest > src) buffers\n" );
#endif
		for ( i = src_len - 1; i >= 0; --i ) {
			dest[i] = src[i]; // back overlapping
		}
		dest += src_len;
	} else {
#ifdef _DEBUG
		if ( src >= dest && src < dest + src_len ) {
			Com_Printf( S_COLOR_YELLOW "Q_strncpy: overlapped (src >= dst) buffers\n" );
#ifdef _MSC_VER
			// __debugbreak();
#endif 
		}
#endif
		while ( src_len > 0 ) {
			*dest++ = *src++;
			--src_len;
		}
	}

	while ( destsize > 0 ) {
		*dest++ = '\0';
		--destsize;
	}

	return start;
}

it is longer than any suggested/existing version but fully inline and do not have any references to external functions like memmove()

If you want to use it in QVM code then you should replace all post-increment assignments like *dest++ = '\0'; with *dest = '\0'; ++dest; because LCC generates suboptimal code for such cases

@zturtleman
Copy link
Author

Thank you. I don't compile it in QVMs, it's only called it through the strncpy syscall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants