-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fortify source against memory attacks (using ..._s-functions portably…
…, or __builtin___..._chk-functions)
- Loading branch information
Showing
10 changed files
with
252 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright (c) 2008-2024 Jan W. Krieger (<[email protected]>), German Cancer Research Center (DKFZ) & IWR, University of Heidelberg | ||
This software is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License (LGPL) as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include "tinytiff_ctools_internal.h" | ||
#include <string.h> | ||
#include <assert.h> | ||
|
||
#ifdef __has_builtin | ||
|
||
# if defined(TINYTIFF_HASOBJSIZE) && __has_builtin (__builtin___memcpy_chk) | ||
# define tinytiff_builtin_memcpy_chk(dest, src, n) __builtin___memcpy_chk(dest, src, n, tinytiff_builtin_object_size0(dest)) | ||
# endif | ||
|
||
# if defined(TINYTIFF_HASOBJSIZE) && __has_builtin (__builtin___memset_chk) | ||
# define tinytiff_builtin_memset_chk(dest, ch, n) __builtin___memset_chk(dest, ch, n, tinytiff_builtin_object_size0(dest)) | ||
# endif | ||
|
||
#endif | ||
|
||
void TinyTIFF_memcpy_s(void *dest, unsigned long destsz, const void *src, unsigned long count) { | ||
if (count<=0) return; | ||
#ifdef HAVE_MEMCPY_S | ||
memcpy_s(dest, destsz, src, count); | ||
#else | ||
# if defined(tinytiff_builtin_memcpy_chk) | ||
tinytiff_builtin_memcpy_chk(dest, src, count); | ||
# else | ||
assert((dest!=NULL) && "TinyTIFF_memcpy_s: dest==NULL"); | ||
assert((src!=NULL) && "TinyTIFF_memcpy_s: src==NULL"); | ||
#if defined(TINYTIFF_HASOBJSIZE) | ||
assert((tinytiff_builtin_object_size(dest,1)>=destsz) && "TinyTIFF_memcpy_s: dest too small (destsz incorrect)"); | ||
assert((tinytiff_builtin_object_size(dest,1)>=count) && "TinyTIFF_memcpy_s: dest too small (count larger than available memory)"); | ||
assert((tinytiff_builtin_object_size(src,1)>=count) && "TinyTIFF_memcpy_s: src too small"); | ||
#endif | ||
memcpy(dest, src, count); | ||
# endif | ||
#endif | ||
} | ||
|
||
void TinyTIFF_memset_s(void *dest, unsigned long destsz, char ch, unsigned long count) | ||
{ | ||
#ifdef HAVE_MEMSET_S | ||
memset_s(dest, destsz, ch, count); | ||
#else | ||
# if defined(tinytiff_builtin_memset_chk) | ||
tinytiff_builtin_memset_chk(dest, ch, count); | ||
# else | ||
memset(dest, ch, count); | ||
# endif | ||
#endif | ||
|
||
} | ||
|
||
unsigned long TinyTIFF_strlen_s(const char *str, unsigned long strsz) | ||
{ | ||
#ifdef HAVE_STRLEN_S | ||
return strlen_s(str, strsz); | ||
#else | ||
if (str==NULL) return 0; | ||
#if defined(TINYTIFF_HASOBJSIZE) | ||
assert((tinytiff_builtin_object_size1(str)>=strsz) && "TinyTIFF_strlen_s: str too small (strsz incorrect)"); | ||
#endif | ||
return strlen(str); | ||
#endif | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright (c) 2008-2024 Jan W. Krieger (<[email protected]>), German Cancer Research Center (DKFZ) & IWR, University of Heidelberg | ||
This software is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License (LGPL) as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
|
||
#ifndef TINYTIFF_CTOOLS_INTERNAL_H | ||
#define TINYTIFF_CTOOLS_INTERNAL_H | ||
|
||
#if defined(__has_builtin) | ||
# if __has_builtin(__builtin_object_size) | ||
# define tinytiff_builtin_object_size(ptr) __builtin_object_size(ptr, 2) | ||
# define tinytiff_builtin_object_size0(dest) __builtin_object_size (dest, 0) | ||
# define tinytiff_builtin_object_size1(dest) __builtin_object_size (dest, 1) | ||
# define TINYTIFF_HASOBJSIZE | ||
# endif | ||
#endif | ||
|
||
#ifdef HAVE_STRCPY_S | ||
#define TINYTIFF_SET_LAST_ERROR(tiff, message) strcpy_s(tiff->lastError, TIFF_LAST_ERROR_SIZE, message); | ||
#define TINYTIFF_STRCPY_S(dest, dsize, src) strcpy_s(dest, dsize, src); | ||
#else | ||
# if defined(TINYTIFF_HASOBJSIZE) && __has_builtin(__builtin___strcpy_chk) | ||
# define TINYTIFF_SET_LAST_ERROR(tiff, message) __builtin___strcpy_chk(tiff->lastError, message, tinytiff_builtin_object_size0(tiff->lastError)) | ||
# define TINYTIFF_STRCPY_S(dest, dsize, src) __builtin___strcpy_chk(dest, src, tinytiff_builtin_object_size0(dest)) | ||
# else | ||
# define TINYTIFF_SET_LAST_ERROR(tiff, message) strcpy(tiff->lastError, message); | ||
# define TINYTIFF_STRCPY_S(dest, dsize, src) strcpy(dest, src); | ||
# endif | ||
#endif | ||
|
||
|
||
#ifdef HAVE_STRCAT_S | ||
#define TINYTIFF_STRCAT_S(dest, dsize, src) strcat_s(dest, dsize, src); | ||
#else | ||
# if defined(TINYTIFF_HASOBJSIZE) && __has_builtin(__builtin___strcat_chk) | ||
# define TINYTIFF_STRCAT_S(dest, dsize, src) __builtin___strcat_chk(dest, src, tinytiff_builtin_object_size0(dest)) | ||
# else | ||
# define TINYTIFF_STRCAT_S(dest, dsize, src) strcat(dest, src); | ||
# endif | ||
#endif | ||
|
||
|
||
#ifdef HAVE_SPRINTF_S | ||
# define TINYTIFF_SPRINTF_S(str, ssize, message, ...) sprintf_s(str, ssize, message, __VA_ARGS__); | ||
# define TINYTIFF_SPRINTF_LAST_ERROR(tiff, message, ...) sprintf_s(tiff->lastError, TIFF_LAST_ERROR_SIZE, message, __VA_ARGS__); | ||
#else | ||
# if defined(TINYTIFF_HASOBJSIZE) && __has_builtin(__builtin___sprintf_chk) | ||
# define TINYTIFF_SPRINTF_LAST_ERROR(tiff, message, ...) __builtin___sprintf_chk(tiff->lastError, 0, tinytiff_builtin_object_size0(tiff->lastError), message, __VA_ARGS__) | ||
# define TINYTIFF_SPRINTF_S(str, ssize, message, ...) __builtin___sprintf_chk(str, 0, tinytiff_builtin_object_size0(str), message, __VA_ARGS__) | ||
# else | ||
# define TINYTIFF_SPRINTF_S(str, ssize, message, ...) sprintf(str, message, __VA_ARGS__); | ||
# define TINYTIFF_SPRINTF_LAST_ERROR(tiff, message, ...) sprintf(tiff->lastError, message, __VA_ARGS__); | ||
# endif | ||
#endif | ||
|
||
|
||
/** \brief hardened memcpy(), that calls memcpy_s() or __builtin___memcpy_chk() if available | ||
* \internal | ||
* \ingroup tinytiffreader_internal | ||
*/ | ||
void TinyTIFF_memcpy_s( void * dest, unsigned long destsz, const void * src, unsigned long count ); | ||
|
||
/** \brief hardened memset(), that calls memset_s() or __builtin___memset_chk() if available | ||
* \internal | ||
* \ingroup tinytiffreader_internal | ||
*/ | ||
void TinyTIFF_memset_s( void * dest, unsigned long destsz, char ch, unsigned long count ); | ||
|
||
/** \brief hardened strlen(), that calls strlen_s() or __builtin___strlen_chk() if available | ||
* \internal | ||
* \ingroup tinytiffreader_internal | ||
*/ | ||
unsigned long TinyTIFF_strlen_s( const char * str, unsigned long strsz); | ||
|
||
#endif // TINYTIFF_CTOOLS_INTERNAL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.