Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Fix world-readable permissions due to sqlite race condition
Browse files Browse the repository at this point in the history
Existing code uses umask() to temporarily modify the file permissions for
open(). A race condition can occur where a second thread reads in the
temporary value, saves it, and then restores the file to the temporary value
resulting in world-readable permissions. Backporting a known fix:
http://www.sqlite.org/src/info/6c4c2b7dba

Bug: 15288755
Change-Id: I89779f3a5ba0bec181d6614b29b1e26ea4f4f049
  • Loading branch information
jeffvanderstoep committed Sep 10, 2014
1 parent e6db643 commit d4f30d0
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions dist/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -25426,11 +25426,7 @@ static struct unix_syscall {
aSyscall[13].pCurrent)
#endif

#if SQLITE_ENABLE_LOCKING_STYLE
{ "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
#else
{ "fchmod", (sqlite3_syscall_ptr)0, 0 },
#endif
#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)

#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
Expand All @@ -25455,9 +25451,6 @@ static struct unix_syscall {
{ "fchown", (sqlite3_syscall_ptr)fchown, 0 },
#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)

{ "umask", (sqlite3_syscall_ptr)umask, 0 },
#define osUmask ((mode_t(*)(mode_t))aSyscall[21].pCurrent)

}; /* End of the overrideable system calls */

/*
Expand Down Expand Up @@ -25561,20 +25554,20 @@ static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
** recover the hot journals.
*/
static int robust_open(const char *z, int f, mode_t m){
int rc;
mode_t m2;
mode_t origM = 0;
if( m==0 ){
m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
}else{
m2 = m;
origM = osUmask(0);
}
do{ rc = osOpen(z,f,m2); }while( rc<0 && errno==EINTR );
if( m ){
osUmask(origM);
int fd;
mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
do{
fd = osOpen(z,f,m2);
}while( fd<0 && errno==EINTR );
if( fd>=0 ){
if( m!=0 ){
struct stat statbuf;
if( osFstat(fd, &statbuf)==0 && (statbuf.st_mode&0777)!=m ){
osFchmod(fd, m);
}
}
}
return rc;
return fd;
}

/*
Expand Down

0 comments on commit d4f30d0

Please sign in to comment.