Skip to content

Commit 24bb518

Browse files
Introduce memset_s in sqlite3 (#1286)
* Add memset_s to fix buffer overflow for memset function in sqlite3 * Use memset inside memset_s with buffer overflow checks * remove codeQL comment * Create separate header for memset_s * Update documentation for sqlite * Correct formatting --------- Co-authored-by: Lalit Kumar Bhasin <[email protected]>
1 parent 7700dc3 commit 24bb518

File tree

8 files changed

+80
-15
lines changed

8 files changed

+80
-15
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,13 @@ if (USE_ONEDS_BOUNDCHECK_METHODS)
199199
add_definitions(-DHAVE_ONEDS_BOUNDCHECK_METHODS)
200200
endif()
201201

202+
option(USE_ONEDS_SECURE_MEM_FUNCTIONS "Use secure memory functions for sqlite" OFF)
203+
if(USE_ONEDS_SECURE_MEM_FUNCTIONS)
204+
add_definitions(-DUSE_ONEDS_SECURE_MEM_FUNCTIONS)
205+
endif()
206+
202207
if(PAL_IMPLEMENTATION STREQUAL "WIN32")
203-
add_definitions(-DZLIB_WINAPI)
208+
add_definitions(-DZLIB_WINAPI)
204209
endif()
205210

206211
add_definitions(-DNOMINMAX)

docs/List-of-OSS-Components.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ SQLite is a C-language library that implements a small, fast, self-contained, hi
1616
SDK maintains its own snapshot of the mainline SQLite, which is used for Windows builds [here](../sqlite). Other platforms use platform-provided SQLite.
1717
It is the responsibility of product teams to ensure that a snapshot of zlib they use meets their product security and licensing requirements.
1818

19+
The SDK provides an option to use a secure version of the traditional `memset` function, which includes safety checks to prevent buffer overflows.
20+
1921
## [nlohmann/json](https://github.com/nlohmann/json)
2022

2123
JSON for Modern C++.

sqlite/memset_s.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef MEMSET_S_H
2+
#define MEMSET_S_H
3+
4+
#include <errno.h>
5+
#include <stddef.h>
6+
#include <string.h>
7+
8+
#ifndef SIZE_MAX
9+
#define SIZE_MAX ((size_t)-1)
10+
#endif
11+
12+
#ifndef RSIZE_MAX
13+
#define RSIZE_MAX (SIZE_MAX >> 1)
14+
#endif
15+
16+
typedef size_t rsize_t;
17+
typedef int errno_t;
18+
19+
/*
20+
** The memset_s implementation is added as a secure version of the traditional
21+
** memset function. It includes safety checks to prevent buffer overflows.
22+
*/
23+
static errno_t memset_s_impl(void* s, rsize_t smax, int c, rsize_t n)
24+
{
25+
if (!s || smax > RSIZE_MAX)
26+
{
27+
return EINVAL;
28+
}
29+
if (n > smax)
30+
{
31+
// Set memory up to the buffer size and return an error
32+
memset(s, c, smax);
33+
return EINVAL;
34+
}
35+
// Perform the memory set operation for the requested size
36+
memset(s, c, n);
37+
return 0;
38+
}
39+
40+
// Define the macro for conditional use of memset_s or memset
41+
#ifdef USE_ONEDS_SECURE_MEM_FUNCTIONS
42+
#define MEMSET_S(s, smax, c, n) memset_s_impl(s, smax, c, n)
43+
#else
44+
#define MEMSET_S(s, smax, c, n) memset(s, c, n)
45+
#endif
46+
47+
#endif // MEMSET_S_H

sqlite/sqlite.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@
586586
</ItemGroup>
587587
<ItemGroup>
588588
<ClInclude Include="msvc.h" />
589+
<ClInclude Include="memset_s.h" />
589590
<ClInclude Include="sqlite3.h" />
590591
</ItemGroup>
591592
<Import Project="$(SolutionDir)\build.props" Condition="Exists('$(SolutionDir)\build.props')" />

sqlite/sqlite.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@
2929
<ClInclude Include="msvc.h">
3030
<Filter>Header Files</Filter>
3131
</ClInclude>
32+
<ClInclude Include="memset_s.h">
33+
<Filter>Header Files</Filter>
34+
</ClInclude>
3235
</ItemGroup>
3336
</Project>

sqlite/sqlite3.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "msvc.h"
2+
#include "memset_s.h"
23
#ifdef NDEBUG
34
/* No debug */
45
#include "sqlite3_retail.c"

sqlite/sqlite3_debug.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49629,7 +49629,8 @@ static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
4962949629
assert( pPage!=0 );
4963049630
pPgHdr = (PgHdr*)pPage->pExtra;
4963149631
assert( pPgHdr->pPage==0 );
49632-
memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
49632+
MEMSET_S(&pPgHdr->pDirty, sizeof(PgHdr) - offsetof(PgHdr, pDirty), 0,
49633+
sizeof(PgHdr) - offsetof(PgHdr, pDirty));
4963349634
pPgHdr->pPage = pPage;
4963449635
pPgHdr->pData = pPage->pBuf;
4963549636
pPgHdr->pExtra = (void *)&pPgHdr[1];
@@ -78098,7 +78099,8 @@ SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
7809878099
Vdbe *p;
7809978100
p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
7810078101
if( p==0 ) return 0;
78101-
memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
78102+
MEMSET_S(&p->aOp, sizeof(Vdbe) - offsetof(Vdbe, aOp), 0,
78103+
sizeof(Vdbe) - offsetof(Vdbe, aOp));
7810278104
p->db = db;
7810378105
if( db->pVdbe ){
7810478106
db->pVdbe->pPrev = p;
@@ -101926,7 +101928,8 @@ SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
101926101928
pItem = &pList->a[pList->nExpr++];
101927101929
assert( offsetof(struct ExprList_item,zEName)==sizeof(pItem->pExpr) );
101928101930
assert( offsetof(struct ExprList_item,pExpr)==0 );
101929-
memset(&pItem->zEName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zEName));
101931+
MEMSET_S(&pItem->zEName, sizeof(*pItem) - offsetof(struct ExprList_item,zEName), 0,
101932+
sizeof(*pItem) - offsetof(struct ExprList_item,zEName));
101930101933
pItem->pExpr = pExpr;
101931101934
return pList;
101932101935

@@ -144787,8 +144790,8 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
144787144790
pTerm->wtFlags = wtFlags;
144788144791
pTerm->pWC = pWC;
144789144792
pTerm->iParent = -1;
144790-
memset(&pTerm->eOperator, 0,
144791-
sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
144793+
MEMSET_S(&pTerm->eOperator, sizeof(WhereTerm) - offsetof(WhereTerm,eOperator), 0,
144794+
sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
144792144795
return idx;
144793144796
}
144794144797

@@ -151130,8 +151133,8 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
151130151133
pWInfo->wctrlFlags = wctrlFlags;
151131151134
pWInfo->iLimit = iAuxArg;
151132151135
pWInfo->savedNQueryLoop = pParse->nQueryLoop;
151133-
memset(&pWInfo->nOBSat, 0,
151134-
offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
151136+
MEMSET_S(&pWInfo->nOBSat, offsetof(WhereInfo, sWC) - offsetof(WhereInfo, nOBSat),
151137+
0, offsetof(WhereInfo, sWC) - offsetof(WhereInfo, nOBSat));
151135151138
memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
151136151139
assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
151137151140
pMaskSet = &pWInfo->sMaskSet;

sqlite/sqlite3_retail.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49555,7 +49555,8 @@ static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
4955549555
assert( pPage!=0 );
4955649556
pPgHdr = (PgHdr*)pPage->pExtra;
4955749557
assert( pPgHdr->pPage==0 );
49558-
memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
49558+
MEMSET_S(&pPgHdr->pDirty, sizeof(PgHdr) - offsetof(PgHdr, pDirty), 0,
49559+
sizeof(PgHdr) - offsetof(PgHdr, pDirty));
4955949560
pPgHdr->pPage = pPage;
4956049561
pPgHdr->pData = pPage->pBuf;
4956149562
pPgHdr->pExtra = (void *)&pPgHdr[1];
@@ -78011,7 +78012,8 @@ SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
7801178012
Vdbe *p;
7801278013
p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
7801378014
if( p==0 ) return 0;
78014-
memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
78015+
MEMSET_S(&p->aOp, sizeof(Vdbe) - offsetof(Vdbe, aOp), 0,
78016+
sizeof(Vdbe) - offsetof(Vdbe, aOp));
7801578017
p->db = db;
7801678018
if( db->pVdbe ){
7801778019
db->pVdbe->pPrev = p;
@@ -101827,7 +101829,8 @@ SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
101827101829
pItem = &pList->a[pList->nExpr++];
101828101830
assert( offsetof(struct ExprList_item,zEName)==sizeof(pItem->pExpr) );
101829101831
assert( offsetof(struct ExprList_item,pExpr)==0 );
101830-
memset(&pItem->zEName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zEName));
101832+
MEMSET_S(&pItem->zEName, sizeof(*pItem) - offsetof(struct ExprList_item,zEName), 0,
101833+
sizeof(*pItem) - offsetof(struct ExprList_item,zEName));
101831101834
pItem->pExpr = pExpr;
101832101835
return pList;
101833101836

@@ -144659,8 +144662,8 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
144659144662
pTerm->wtFlags = wtFlags;
144660144663
pTerm->pWC = pWC;
144661144664
pTerm->iParent = -1;
144662-
memset(&pTerm->eOperator, 0,
144663-
sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
144665+
MEMSET_S(&pTerm->eOperator, sizeof(WhereTerm) - offsetof(WhereTerm,eOperator), 0,
144666+
sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
144664144667
return idx;
144665144668
}
144666144669

@@ -151001,8 +151004,8 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
151001151004
pWInfo->wctrlFlags = wctrlFlags;
151002151005
pWInfo->iLimit = iAuxArg;
151003151006
pWInfo->savedNQueryLoop = pParse->nQueryLoop;
151004-
memset(&pWInfo->nOBSat, 0,
151005-
offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
151007+
MEMSET_S(&pWInfo->nOBSat, offsetof(WhereInfo, sWC) - offsetof(WhereInfo, nOBSat),
151008+
0, offsetof(WhereInfo, sWC) - offsetof(WhereInfo, nOBSat));
151006151009
memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
151007151010
assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
151008151011
pMaskSet = &pWInfo->sMaskSet;

0 commit comments

Comments
 (0)