Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit 5c02db4

Browse files
author
Hui Yu
committed
Apply security patch to sqlite 3.22.
This patch fixes Magellan SQLite Security Vulnerability as in b/121156452 Download from https://www.sqlite.org/src/info/c255889bd95bd543 Get the Tarball or ZIP Archive, unzip it on linux machine then run: mkdir bld; cd bld; ../configure; make sqlite3.c Copy sqlite3.c, sqlite3.h, sqlite3ext.h, shell.c from bld directory to dist/orig. dist contains a copy of dist/orig, but with the Android.patch file applied. Test: select sqlite_version() - returns 3.22.0 Test: atest cts/SQLiteDatabaseTest all passed. Test: atest cts/SQLSecurityTest Change-Id: Iad8c3d1d74d8591a75a4c033c8bf9d3956d2fe4f
1 parent fb001b3 commit 5c02db4

File tree

6 files changed

+211
-96
lines changed

6 files changed

+211
-96
lines changed

README.version

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
URL: https://www.sqlite.org/2018/sqlite-autoconf-3220000.tar.gz
2-
Version: 3.22.0 0c55d179733b46d8
1+
URL: https://www.sqlite.org/src/info/c255889bd95bd543
2+
Version: 3.22.0 c255889bd95bd5430dc7ced3317011ae2abb483d6c9af883af3dc7d6c2c2f234
3+
This patch fixes Magellan SQLite Security Vulnerability as in b/121156452
34
BugComponent: 24950

dist/Android.patch

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
--- orig/shell.c 2018-02-18 23:53:25.407627650 -0800
2-
+++ shell.c 2018-02-18 23:53:25.467627374 -0800
1+
--- orig/shell.c 2018-12-19 15:21:18.344932246 -0800
2+
+++ shell.c 2018-12-19 15:24:25.751492011 -0800
33
@@ -87,6 +87,12 @@
44
#endif
55
#include <ctype.h>
@@ -37,8 +37,8 @@
3737
if( p->openMode==SHELL_OPEN_ZIPFILE ){
3838
char *zSql = sqlite3_mprintf(
3939
"CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
40-
--- orig/sqlite3.c 2018-02-18 23:53:25.459627411 -0800
41-
+++ sqlite3.c 2018-08-22 15:28:04.016188846 -0700
40+
--- orig/sqlite3.c 2018-12-19 15:21:25.032880851 -0800
41+
+++ sqlite3.c 2018-12-19 15:24:25.751492011 -0800
4242
@@ -30672,6 +30672,10 @@
4343
# include <sys/mount.h>
4444
#endif

dist/orig/sqlite3.c

Lines changed: 101 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ extern "C" {
11491149
*/
11501150
#define SQLITE_VERSION "3.22.0"
11511151
#define SQLITE_VERSION_NUMBER 3022000
1152-
#define SQLITE_SOURCE_ID "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
1152+
#define SQLITE_SOURCE_ID "2018-12-19 01:30:22 c255889bd95bd5430dc7ced3317011ae2abb483d6c9af883af3dc7d6c2c2f234"
11531153

11541154
/*
11551155
** CAPI3REF: Run-Time Library Version Numbers
@@ -150618,7 +150618,7 @@ static int fts3ScanInteriorNode(
150618150618
const char *zCsr = zNode; /* Cursor to iterate through node */
150619150619
const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
150620150620
char *zBuffer = 0; /* Buffer to load terms into */
150621-
int nAlloc = 0; /* Size of allocated buffer */
150621+
i64 nAlloc = 0; /* Size of allocated buffer */
150622150622
int isFirstTerm = 1; /* True when processing first term on page */
150623150623
sqlite3_int64 iChild; /* Block id of child node to descend to */
150624150624

@@ -150656,14 +150656,14 @@ static int fts3ScanInteriorNode(
150656150656
zCsr += fts3GetVarint32(zCsr, &nSuffix);
150657150657

150658150658
assert( nPrefix>=0 && nSuffix>=0 );
150659-
if( &zCsr[nSuffix]>zEnd ){
150659+
if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr ){
150660150660
rc = FTS_CORRUPT_VTAB;
150661150661
goto finish_scan;
150662150662
}
150663-
if( nPrefix+nSuffix>nAlloc ){
150663+
if( (i64)nPrefix+nSuffix>nAlloc ){
150664150664
char *zNew;
150665-
nAlloc = (nPrefix+nSuffix) * 2;
150666-
zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
150665+
nAlloc = ((i64)nPrefix+nSuffix) * 2;
150666+
zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
150667150667
if( !zNew ){
150668150668
rc = SQLITE_NOMEM;
150669150669
goto finish_scan;
@@ -160262,15 +160262,19 @@ static int fts3SegReaderNext(
160262160262
** safe (no risk of overread) even if the node data is corrupted. */
160263160263
pNext += fts3GetVarint32(pNext, &nPrefix);
160264160264
pNext += fts3GetVarint32(pNext, &nSuffix);
160265-
if( nPrefix<0 || nSuffix<=0
160266-
|| &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
160265+
if( nSuffix<=0
160266+
|| (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
160267+
|| nPrefix>pReader->nTermAlloc
160267160268
){
160268160269
return FTS_CORRUPT_VTAB;
160269160270
}
160270160271

160271-
if( nPrefix+nSuffix>pReader->nTermAlloc ){
160272-
int nNew = (nPrefix+nSuffix)*2;
160273-
char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
160272+
/* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
160273+
** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
160274+
** overflow - hence the (i64) casts. */
160275+
if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
160276+
i64 nNew = ((i64)nPrefix+nSuffix)*2;
160277+
char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
160274160278
if( !zNew ){
160275160279
return SQLITE_NOMEM;
160276160280
}
@@ -160292,7 +160296,7 @@ static int fts3SegReaderNext(
160292160296
** b-tree node. And that the final byte of the doclist is 0x00. If either
160293160297
** of these statements is untrue, then the data structure is corrupt.
160294160298
*/
160295-
if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
160299+
if( (&pReader->aNode[pReader->nNode] - pReader->aDoclist)<pReader->nDoclist
160296160300
|| (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
160297160301
){
160298160302
return FTS_CORRUPT_VTAB;
@@ -162615,21 +162619,26 @@ static int nodeReaderNext(NodeReader *p){
162615162619
}
162616162620
p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
162617162621

162622+
if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){
162623+
return SQLITE_CORRUPT_VTAB;
162624+
}
162618162625
blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
162619162626
if( rc==SQLITE_OK ){
162620162627
memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
162621162628
p->term.n = nPrefix+nSuffix;
162622162629
p->iOff += nSuffix;
162623162630
if( p->iChild==0 ){
162624162631
p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
162632+
if( (p->nNode-p->iOff)<p->nDoclist ){
162633+
return SQLITE_CORRUPT_VTAB;
162634+
}
162625162635
p->aDoclist = &p->aNode[p->iOff];
162626162636
p->iOff += p->nDoclist;
162627162637
}
162628162638
}
162629162639
}
162630162640

162631162641
assert( p->iOff<=p->nNode );
162632-
162633162642
return rc;
162634162643
}
162635162644

@@ -173110,7 +173119,8 @@ struct rbu_vfs {
173110173119
sqlite3_vfs *pRealVfs; /* Underlying VFS */
173111173120
sqlite3_mutex *mutex; /* Mutex to protect pMain */
173112173121
sqlite3rbu *pRbu; /* Owner RBU object */
173113-
rbu_file *pMain; /* Linked list of main db files */
173122+
rbu_file *pMain; /* List of main db files */
173123+
rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
173114173124
};
173115173125

173116173126
/*
@@ -173139,6 +173149,7 @@ struct rbu_file {
173139173149
const char *zWal; /* Wal filename for this main db file */
173140173150
rbu_file *pWalFd; /* Wal file descriptor for this main db */
173141173151
rbu_file *pMainNext; /* Next MAIN_DB file */
173152+
rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
173142173153
};
173143173154

173144173155
/*
@@ -174517,7 +174528,7 @@ static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
174517174528
int iCid = sqlite3_column_int(pXInfo, 1);
174518174529
int bDesc = sqlite3_column_int(pXInfo, 3);
174519174530
const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
174520-
zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
174531+
zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
174521174532
iCid, pIter->azTblType[iCid], zCollate
174522174533
);
174523174534
zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
@@ -174578,7 +174589,7 @@ static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
174578174589
** "PRIMARY KEY" to the imposter table column declaration. */
174579174590
zPk = "PRIMARY KEY ";
174580174591
}
174581-
zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
174592+
zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
174582174593
zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
174583174594
(pIter->abNotNull[iCol] ? " NOT NULL" : "")
174584174595
);
@@ -176727,6 +176738,69 @@ static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
176727176738
return SQLITE_OK;
176728176739
}
176729176740

176741+
/*
176742+
** Add an item to the main-db lists, if it is not already present.
176743+
**
176744+
** There are two main-db lists. One for all file descriptors, and one
176745+
** for all file descriptors with rbu_file.pDb!=0. If the argument has
176746+
** rbu_file.pDb!=0, then it is assumed to already be present on the
176747+
** main list and is only added to the pDb!=0 list.
176748+
*/
176749+
static void rbuMainlistAdd(rbu_file *p){
176750+
rbu_vfs *pRbuVfs = p->pRbuVfs;
176751+
rbu_file *pIter;
176752+
assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
176753+
sqlite3_mutex_enter(pRbuVfs->mutex);
176754+
if( p->pRbu==0 ){
176755+
for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
176756+
p->pMainNext = pRbuVfs->pMain;
176757+
pRbuVfs->pMain = p;
176758+
}else{
176759+
for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
176760+
if( pIter==0 ){
176761+
p->pMainRbuNext = pRbuVfs->pMainRbu;
176762+
pRbuVfs->pMainRbu = p;
176763+
}
176764+
}
176765+
sqlite3_mutex_leave(pRbuVfs->mutex);
176766+
}
176767+
176768+
/*
176769+
** Remove an item from the main-db lists.
176770+
*/
176771+
static void rbuMainlistRemove(rbu_file *p){
176772+
rbu_file **pp;
176773+
sqlite3_mutex_enter(p->pRbuVfs->mutex);
176774+
for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
176775+
if( *pp ) *pp = p->pMainNext;
176776+
p->pMainNext = 0;
176777+
for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
176778+
if( *pp ) *pp = p->pMainRbuNext;
176779+
p->pMainRbuNext = 0;
176780+
sqlite3_mutex_leave(p->pRbuVfs->mutex);
176781+
}
176782+
176783+
/*
176784+
** Given that zWal points to a buffer containing a wal file name passed to
176785+
** either the xOpen() or xAccess() VFS method, search the main-db list for
176786+
** a file-handle opened by the same database connection on the corresponding
176787+
** database file.
176788+
**
176789+
** If parameter bRbu is true, only search for file-descriptors with
176790+
** rbu_file.pDb!=0.
176791+
*/
176792+
static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
176793+
rbu_file *pDb;
176794+
sqlite3_mutex_enter(pRbuVfs->mutex);
176795+
if( bRbu ){
176796+
for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
176797+
}else{
176798+
for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
176799+
}
176800+
sqlite3_mutex_leave(pRbuVfs->mutex);
176801+
return pDb;
176802+
}
176803+
176730176804
/*
176731176805
** Close an rbu file.
176732176806
*/
@@ -176744,17 +176818,14 @@ static int rbuVfsClose(sqlite3_file *pFile){
176744176818
sqlite3_free(p->zDel);
176745176819

176746176820
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
176747-
rbu_file **pp;
176748-
sqlite3_mutex_enter(p->pRbuVfs->mutex);
176749-
for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
176750-
*pp = p->pMainNext;
176751-
sqlite3_mutex_leave(p->pRbuVfs->mutex);
176821+
rbuMainlistRemove(p);
176752176822
rbuUnlockShm(p);
176753176823
p->pReal->pMethods->xShmUnmap(p->pReal, 0);
176754176824
}
176755176825
else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
176756176826
rbuUpdateTempSize(p, 0);
176757176827
}
176828+
assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
176758176829

176759176830
/* Close the underlying file handle */
176760176831
rc = p->pReal->pMethods->xClose(p->pReal);
@@ -177013,6 +177084,9 @@ static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
177013177084
}else if( rc==SQLITE_NOTFOUND ){
177014177085
pRbu->pTargetFd = p;
177015177086
p->pRbu = pRbu;
177087+
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
177088+
rbuMainlistAdd(p);
177089+
}
177016177090
if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
177017177091
rc = SQLITE_OK;
177018177092
}
@@ -177174,20 +177248,6 @@ static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
177174177248
return rc;
177175177249
}
177176177250

177177-
/*
177178-
** Given that zWal points to a buffer containing a wal file name passed to
177179-
** either the xOpen() or xAccess() VFS method, return a pointer to the
177180-
** file-handle opened by the same database connection on the corresponding
177181-
** database file.
177182-
*/
177183-
static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
177184-
rbu_file *pDb;
177185-
sqlite3_mutex_enter(pRbuVfs->mutex);
177186-
for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
177187-
sqlite3_mutex_leave(pRbuVfs->mutex);
177188-
return pDb;
177189-
}
177190-
177191177251
/*
177192177252
** A main database named zName has just been opened. The following
177193177253
** function returns a pointer to a buffer owned by SQLite that contains
@@ -177266,7 +177326,7 @@ static int rbuVfsOpen(
177266177326
pFd->zWal = rbuMainToWal(zName, flags);
177267177327
}
177268177328
else if( flags & SQLITE_OPEN_WAL ){
177269-
rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
177329+
rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
177270177330
if( pDb ){
177271177331
if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
177272177332
/* This call is to open a *-wal file. Intead, open the *-oal. This
@@ -177318,10 +177378,7 @@ static int rbuVfsOpen(
177318177378
** mutex protected linked list of all such files. */
177319177379
pFile->pMethods = &rbuvfs_io_methods;
177320177380
if( flags & SQLITE_OPEN_MAIN_DB ){
177321-
sqlite3_mutex_enter(pRbuVfs->mutex);
177322-
pFd->pMainNext = pRbuVfs->pMain;
177323-
pRbuVfs->pMain = pFd;
177324-
sqlite3_mutex_leave(pRbuVfs->mutex);
177381+
rbuMainlistAdd(pFd);
177325177382
}
177326177383
}else{
177327177384
sqlite3_free(pFd->zDel);
@@ -177369,7 +177426,7 @@ static int rbuVfsAccess(
177369177426
** file opened instead.
177370177427
*/
177371177428
if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
177372-
rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
177429+
rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
177373177430
if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
177374177431
if( *pResOut ){
177375177432
rc = SQLITE_CANTOPEN;
@@ -203333,7 +203390,7 @@ static void fts5SourceIdFunc(
203333203390
){
203334203391
assert( nArg==0 );
203335203392
UNUSED_PARAM2(nArg, apUnused);
203336-
sqlite3_result_text(pCtx, "fts5: 2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d", -1, SQLITE_TRANSIENT);
203393+
sqlite3_result_text(pCtx, "fts5: 2018-12-19 01:30:22 c255889bd95bd5430dc7ced3317011ae2abb483d6c9af883af3dc7d6c2c2f234", -1, SQLITE_TRANSIENT);
203337203394
}
203338203395

203339203396
static int fts5Init(sqlite3 *db){
@@ -207601,9 +207658,9 @@ SQLITE_API int sqlite3_stmt_init(
207601207658
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207602207659

207603207660
/************** End of stmt.c ************************************************/
207604-
#if __LINE__!=207604
207661+
#if __LINE__!=207661
207605207662
#undef SQLITE_SOURCE_ID
207606-
#define SQLITE_SOURCE_ID "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2alt2"
207663+
#define SQLITE_SOURCE_ID "2018-12-19 01:30:22 c255889bd95bd5430dc7ced3317011ae2abb483d6c9af883af3dc7d6c2c2alt2"
207607207664
#endif
207608207665
/* Return the source-id for this library */
207609207666
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }

dist/orig/sqlite3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ extern "C" {
125125
*/
126126
#define SQLITE_VERSION "3.22.0"
127127
#define SQLITE_VERSION_NUMBER 3022000
128-
#define SQLITE_SOURCE_ID "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
128+
#define SQLITE_SOURCE_ID "2018-12-19 01:30:22 c255889bd95bd5430dc7ced3317011ae2abb483d6c9af883af3dc7d6c2c2f234"
129129

130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers

0 commit comments

Comments
 (0)