Skip to content

Commit 2fbb0ef

Browse files
Darwin Huangmibrunin
authored andcommitted
[Backport] Security bugs 1175522 and 1181276
Manual cherry-pick of patch originally reviewed on https://chromium-review.googlesource.com/c/chromium/deps/sqlite/+/2730249: Fix a couple of memory-sanitizer complaints that could be triggered by a corrupt database. Cherry-picking from https://www.sqlite.org/src/info/39c8686cabe6c437 FossilOrigin-Name: 9c8686cabe6c437ba4860aade49a701c4f5772b97d9fbe6cb9a394e85b9c092 Bug: 1181276, 1175522 Change-Id: Icc7e115ec54789fab59c03071dccf97987d5ac7f Reviewed-by: Allan Sandfeld Jensen <[email protected]>
1 parent 1fec1be commit 2fbb0ef

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

chromium/third_party/sqlite/amalgamation/sqlite3.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50422,6 +50422,7 @@ static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
5042250422
p->page.pExtra = &p[1];
5042350423
p->isBulkLocal = 0;
5042450424
p->isAnchor = 0;
50425+
p->pLruPrev = 0; /* Initializing this saves a valgrind error */
5042550426
}
5042650427
(*pCache->pnPurgeable)++;
5042750428
return p;
@@ -72324,7 +72325,9 @@ static int balance_nonroot(
7232472325
}
7232572326
pgno = get4byte(pRight);
7232672327
while( 1 ){
72327-
rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
72328+
if( rc==SQLITE_OK ){
72329+
rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
72330+
}
7232872331
if( rc ){
7232972332
memset(apOld, 0, (i+1)*sizeof(MemPage*));
7233072333
goto balance_cleanup;
@@ -72363,12 +72366,10 @@ static int balance_nonroot(
7236372366
if( pBt->btsFlags & BTS_FAST_SECURE ){
7236472367
int iOff;
7236572368

72369+
/* If the following if() condition is not true, the db is corrupted.
72370+
** The call to dropCell() below will detect this. */
7236672371
iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
72367-
if( (iOff+szNew[i])>(int)pBt->usableSize ){
72368-
rc = SQLITE_CORRUPT_BKPT;
72369-
memset(apOld, 0, (i+1)*sizeof(MemPage*));
72370-
goto balance_cleanup;
72371-
}else{
72372+
if( (iOff+szNew[i])<=(int)pBt->usableSize ){
7237272373
memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
7237372374
apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
7237472375
}
@@ -231234,7 +231235,7 @@ SQLITE_API int sqlite3_stmt_init(
231234231235
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
231235231236

231236231237
/************** End of stmt.c ************************************************/
231237-
#if __LINE__!=231237
231238+
#if __LINE__!=231238
231238231239
#undef SQLITE_SOURCE_ID
231239231240
#define SQLITE_SOURCE_ID "2020-12-01 16:14:00 b7738010bc8ef02ba84820368e557306390a33c38adaa5c7703154bae3edalt2"
231240231241
#endif

chromium/third_party/sqlite/src/src/btree.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7417,7 +7417,9 @@ static int balance_nonroot(
74177417
}
74187418
pgno = get4byte(pRight);
74197419
while( 1 ){
7420-
rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
7420+
if( rc==SQLITE_OK ){
7421+
rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
7422+
}
74217423
if( rc ){
74227424
memset(apOld, 0, (i+1)*sizeof(MemPage*));
74237425
goto balance_cleanup;
@@ -7450,12 +7452,10 @@ static int balance_nonroot(
74507452
if( pBt->btsFlags & BTS_FAST_SECURE ){
74517453
int iOff;
74527454

7455+
/* If the following if() condition is not true, the db is corrupted.
7456+
** The call to dropCell() below will detect this. */
74537457
iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
7454-
if( (iOff+szNew[i])>(int)pBt->usableSize ){
7455-
rc = SQLITE_CORRUPT_BKPT;
7456-
memset(apOld, 0, (i+1)*sizeof(MemPage*));
7457-
goto balance_cleanup;
7458-
}else{
7458+
if( (iOff+szNew[i])<=(int)pBt->usableSize ){
74597459
memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
74607460
apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
74617461
}

chromium/third_party/sqlite/src/src/pcache1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
446446
p->page.pExtra = &p[1];
447447
p->isBulkLocal = 0;
448448
p->isAnchor = 0;
449+
p->pLruPrev = 0; /* Initializing this saves a valgrind error */
449450
}
450451
(*pCache->pnPurgeable)++;
451452
return p;

0 commit comments

Comments
 (0)