Fossil SCM

Further improvements to the defenses against being invoked without file descriptor 2 being open.

drh 2015-02-07 21:14 trunk
Commit 6dbaa67420c7c732b47a77fb59b0eab079203677
1 file changed +13 -4
+13 -4
--- src/main.c
+++ src/main.c
@@ -689,14 +689,23 @@
689689
g.argv = zNewArgv;
690690
}
691691
zCmdName = g.argv[1];
692692
}
693693
#ifndef _WIN32
694
- /* Make sure open() will not return file descriptor 2. */
695
- { int nTry = 0;
696
- while( !is_valid_fd(2) && nTry++ < 2 && open("/dev/null",O_WRONLY)>=0 ){}
697
- if( !is_valid_fd(2) ){
694
+ /* There is a bug in stunnel4 in which it sometimes starts up client
695
+ ** processes without first opening file descriptor 2 (standard error).
696
+ ** If this happens, and a subsequent open() of a database returns file
697
+ ** descriptor 2, and then an assert() fires and writes on fd 2, that
698
+ ** can corrupt the data file. To avoid this problem, make sure open()
699
+ ** will never return file descriptor 2 or less. */
700
+ if( !is_valid_fd(2) ){
701
+ int nTry = 0;
702
+ int fd = 0;
703
+ while( !is_valid_fd(2)
704
+ && (nTry++)<2
705
+ && (fd = open("/dev/null",O_WRONLY))>=0 && fd<2 ){}
706
+ if( fd<2 && !is_valid_fd(2) ){
698707
g.cgiOutput = 1;
699708
g.httpOut = stdout;
700709
g.fullHttpReply = !g.isHTTP;
701710
fossil_fatal("file descriptor 2 is not open");
702711
}
703712
--- src/main.c
+++ src/main.c
@@ -689,14 +689,23 @@
689 g.argv = zNewArgv;
690 }
691 zCmdName = g.argv[1];
692 }
693 #ifndef _WIN32
694 /* Make sure open() will not return file descriptor 2. */
695 { int nTry = 0;
696 while( !is_valid_fd(2) && nTry++ < 2 && open("/dev/null",O_WRONLY)>=0 ){}
697 if( !is_valid_fd(2) ){
 
 
 
 
 
 
 
 
 
698 g.cgiOutput = 1;
699 g.httpOut = stdout;
700 g.fullHttpReply = !g.isHTTP;
701 fossil_fatal("file descriptor 2 is not open");
702 }
703
--- src/main.c
+++ src/main.c
@@ -689,14 +689,23 @@
689 g.argv = zNewArgv;
690 }
691 zCmdName = g.argv[1];
692 }
693 #ifndef _WIN32
694 /* There is a bug in stunnel4 in which it sometimes starts up client
695 ** processes without first opening file descriptor 2 (standard error).
696 ** If this happens, and a subsequent open() of a database returns file
697 ** descriptor 2, and then an assert() fires and writes on fd 2, that
698 ** can corrupt the data file. To avoid this problem, make sure open()
699 ** will never return file descriptor 2 or less. */
700 if( !is_valid_fd(2) ){
701 int nTry = 0;
702 int fd = 0;
703 while( !is_valid_fd(2)
704 && (nTry++)<2
705 && (fd = open("/dev/null",O_WRONLY))>=0 && fd<2 ){}
706 if( fd<2 && !is_valid_fd(2) ){
707 g.cgiOutput = 1;
708 g.httpOut = stdout;
709 g.fullHttpReply = !g.isHTTP;
710 fossil_fatal("file descriptor 2 is not open");
711 }
712

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button