CryptoPkg: Fix BaseCryptLib CrtWrapper strncpy and strcat

Following https://bugzilla.tianocore.org/show_bug.cgi?id=2817 this
bug could also apply to strncpy and strcat.

For strncpy use count+1 if smaller than MAX_STRING_SIZE. This still
restricts the destination size to MAX_STRING_SIZE as before but allows
a strncpy when the source is close after destination without triggering
the InternalSafeStringNoAsciiStrOverlap check in AsciiStrnCpyS.

For strcat use the destination string length + the size of the source
string including the terminator as destination size if smaller than
MAX_STRING_SIZE.

Also move both functions to CrtWrapper.c as they do not return the
correct return value. AsciiStrnCpyS and AsciiStrCatS return
RETURN_VALUE instead of a char * to the destination buffer.

Signed-off-by: Sebastian Witt <sebastian.witt@siemens.com>
This commit is contained in:
Sebastian Witt 2024-06-04 13:38:26 +02:00 committed by mergify[bot]
parent df8c61e4c0
commit 948f234170
2 changed files with 64 additions and 14 deletions

View File

@ -271,7 +271,46 @@ strcpy (
const char *strSource
)
{
AsciiStrCpyS (strDest, AsciiStrnSizeS (strSource, MAX_STRING_SIZE), strSource);
AsciiStrCpyS (strDest, AsciiStrnSizeS (strSource, MAX_STRING_SIZE - 1), strSource);
return strDest;
}
char *
strncpy (
char *strDest,
const char *strSource,
size_t count
)
{
UINTN DestMax = MAX_STRING_SIZE;
if (count < MAX_STRING_SIZE) {
DestMax = count + 1;
} else {
count = MAX_STRING_SIZE-1;
}
AsciiStrnCpyS (strDest, DestMax, strSource, (UINTN)count);
return strDest;
}
char *
strcat (
char *strDest,
const char *strSource
)
{
UINTN DestMax;
DestMax = AsciiStrnLenS (strDest, MAX_STRING_SIZE) + AsciiStrnSizeS (strSource, MAX_STRING_SIZE);
if (DestMax > MAX_STRING_SIZE) {
DestMax = MAX_STRING_SIZE;
}
AsciiStrCatS (strDest, DestMax, strSource);
return strDest;
}

View File

@ -403,22 +403,33 @@ strcpy (
const char *strSource
);
char *
strncpy (
char *strDest,
const char *strSource,
size_t count
);
char *
strcat (
char *strDest,
const char *strSource
);
//
// Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
//
#define memcpy(dest, source, count) CopyMem(dest,source,(UINTN)(count))
#define memset(dest, ch, count) SetMem(dest,(UINTN)(count),(UINT8)(ch))
#define memchr(buf, ch, count) ScanMem8(buf,(UINTN)(count),(UINT8)ch)
#define memcmp(buf1, buf2, count) (int)(CompareMem(buf1,buf2,(UINTN)(count)))
#define memmove(dest, source, count) CopyMem(dest,source,(UINTN)(count))
#define strlen(str) (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))
#define strncpy(strDest, strSource, count) AsciiStrnCpyS(strDest,MAX_STRING_SIZE,strSource,(UINTN)count)
#define strcat(strDest, strSource) AsciiStrCatS(strDest,MAX_STRING_SIZE,strSource)
#define strncmp(string1, string2, count) (int)(AsciiStrnCmp(string1,string2,(UINTN)(count)))
#define strcasecmp(str1, str2) (int)AsciiStriCmp(str1,str2)
#define strstr(s1, s2) AsciiStrStr(s1,s2)
#define sprintf(buf, ...) AsciiSPrint(buf,MAX_STRING_SIZE,__VA_ARGS__)
#define localtime(timer) NULL
#define memcpy(dest, source, count) CopyMem(dest,source,(UINTN)(count))
#define memset(dest, ch, count) SetMem(dest,(UINTN)(count),(UINT8)(ch))
#define memchr(buf, ch, count) ScanMem8(buf,(UINTN)(count),(UINT8)ch)
#define memcmp(buf1, buf2, count) (int)(CompareMem(buf1,buf2,(UINTN)(count)))
#define memmove(dest, source, count) CopyMem(dest,source,(UINTN)(count))
#define strlen(str) (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))
#define strncmp(string1, string2, count) (int)(AsciiStrnCmp(string1,string2,(UINTN)(count)))
#define strcasecmp(str1, str2) (int)AsciiStriCmp(str1,str2)
#define strstr(s1, s2) AsciiStrStr(s1,s2)
#define sprintf(buf, ...) AsciiSPrint(buf,MAX_STRING_SIZE,__VA_ARGS__)
#define localtime(timer) NULL
#define assert(expression)
#define offsetof(type, member) OFFSET_OF(type,member)
#define atoi(nptr) AsciiStrDecimalToUintn(nptr)