Fossil SCM

Improve defenses against hostile command-line arguments in commands that call out to other programs (like ssh) using popen.

drh 2026-11-15 23:34 UTC trunk merge
Commit d4ef18966762ae6229302b5d707e05adfada5e9d1a452895967a5aed02ef13a4
+2 -2
--- src/blob.c
+++ src/blob.c
@@ -1690,21 +1690,21 @@
16901690
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */
16911691
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 7x */
16921692
#else
16931693
/* Unix
16941694
** Prohibit: all control characters, including tab, \r and \n
1695
-** Escape: (space) ! " # $ % & ' ( ) * ; < > ? [ \ ] ^ ` { | }
1695
+** Escape: (space) ! " # $ % & ' ( ) * ; < > ? [ \ ] ^ ` { | } ~
16961696
*/
16971697
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
16981698
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x */
16991699
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 1x */
17001700
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 2x */
17011701
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, /* 3x */
17021702
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
17031703
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 5x */
17041704
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */
1705
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 7x */
1705
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, /* 7x */
17061706
#endif
17071707
/* all bytes 0x80 through 0xbf are unescaped, being secondary
17081708
** bytes to UTF8 characters. Bytes 0xc0 through 0xff are the
17091709
** first byte of a UTF8 character and do get escaped */
17101710
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 8x */
17111711
--- src/blob.c
+++ src/blob.c
@@ -1690,21 +1690,21 @@
1690 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */
1691 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 7x */
1692 #else
1693 /* Unix
1694 ** Prohibit: all control characters, including tab, \r and \n
1695 ** Escape: (space) ! " # $ % & ' ( ) * ; < > ? [ \ ] ^ ` { | }
1696 */
1697 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
1698 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x */
1699 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 1x */
1700 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 2x */
1701 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, /* 3x */
1702 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
1703 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 5x */
1704 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */
1705 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 7x */
1706 #endif
1707 /* all bytes 0x80 through 0xbf are unescaped, being secondary
1708 ** bytes to UTF8 characters. Bytes 0xc0 through 0xff are the
1709 ** first byte of a UTF8 character and do get escaped */
1710 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 8x */
1711
--- src/blob.c
+++ src/blob.c
@@ -1690,21 +1690,21 @@
1690 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */
1691 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 7x */
1692 #else
1693 /* Unix
1694 ** Prohibit: all control characters, including tab, \r and \n
1695 ** Escape: (space) ! " # $ % & ' ( ) * ; < > ? [ \ ] ^ ` { | } ~
1696 */
1697 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
1698 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x */
1699 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 1x */
1700 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 2x */
1701 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, /* 3x */
1702 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
1703 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 5x */
1704 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */
1705 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, /* 7x */
1706 #endif
1707 /* all bytes 0x80 through 0xbf are unescaped, being secondary
1708 ** bytes to UTF8 characters. Bytes 0xc0 through 0xff are the
1709 ** first byte of a UTF8 character and do get escaped */
1710 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 8x */
1711
--- src/http_transport.c
+++ src/http_transport.c
@@ -74,19 +74,32 @@
7474
if( resetFlag ){
7575
transport.nSent = 0;
7676
transport.nRcvd = 0;
7777
}
7878
}
79
+
80
+/*
81
+** Whatever follows the host name on an ssh command line is joined into
82
+** one string by ssh and parsed again by the login shell on the remote
83
+** system. Quoting for the local shell does not protect an argument
84
+** from that second parse. Return true if zArg holds none of the
85
+** characters that a shell treats specially, so that the remote shell
86
+** takes it as a single word, unchanged.
87
+*/
88
+int ssh_remote_arg_is_safe(const char *zArg){
89
+ return strpbrk(zArg, " !\"#$&'()*;<>?[]^`{|}")==0;
90
+}
7991
8092
/*
8193
** Check zFossil to see if it is a reasonable "fossil" command to
8294
** run on the server. Do not allow an attacker to substitute something
83
-** like "/bin/rm".
95
+** like "/bin/rm", or to add to the command.
8496
*/
8597
static int is_safe_fossil_command(const char *zFossil){
8698
static const char *const azSafe[] = { "*/fossil", "*/fossil.exe", "*/echo" };
8799
int i;
100
+ if( !ssh_remote_arg_is_safe(zFossil) ) return 0;
88101
for(i=0; i<(int)(sizeof(azSafe)/sizeof(azSafe[0])); i++){
89102
if( sqlite3_strglob(azSafe[i], zFossil)==0 ) return 1;
90103
if( strcmp(azSafe[i]+2, zFossil)==0 ) return 1;
91104
}
92105
return 0;
@@ -138,21 +151,30 @@
138151
blob_append_escaped_arg(&zCmd, pUrlData->name, 0);
139152
}
140153
if( (pUrlData->flags & URL_SSH_EXE)!=0
141154
&& !is_safe_fossil_command(pUrlData->fossil)
142155
){
143
- fossil_fatal("the ssh:// URL is asking to run an unsafe command [%s] on "
144
- "the server.", pUrlData->fossil);
156
+ socket_set_errmsg("the ssh:// URL is asking to run an unsafe command "
157
+ "[%s] on the server.", pUrlData->fossil);
158
+ blob_reset(&zCmd);
159
+ return 1;
145160
}
146161
if( (pUrlData->flags & URL_SSH_EXE)==0
147162
&& (pUrlData->flags & URL_SSH_PATH)!=0
148163
){
149164
ssh_add_path_argument(&zCmd);
150165
}
151166
blob_append_escaped_arg(&zCmd, pUrlData->fossil, 1);
152167
blob_append(&zCmd, " test-http", 10);
153168
if( pUrlData->path && pUrlData->path[0] ){
169
+ if( !ssh_remote_arg_is_safe(pUrlData->path) ){
170
+ socket_set_errmsg("the repository path \"%s\" in the ssh:// URL "
171
+ "contains a character that is special to the shell "
172
+ "on the remote system", pUrlData->path);
173
+ blob_reset(&zCmd);
174
+ return 1;
175
+ }
154176
blob_append_escaped_arg(&zCmd, pUrlData->path, 1);
155177
}else{
156178
fossil_fatal("ssh:// URI does not specify a path to the repository");
157179
}
158180
if( g.fSshTrace || g.fHttpTrace ){
159181
--- src/http_transport.c
+++ src/http_transport.c
@@ -74,19 +74,32 @@
74 if( resetFlag ){
75 transport.nSent = 0;
76 transport.nRcvd = 0;
77 }
78 }
 
 
 
 
 
 
 
 
 
 
 
 
79
80 /*
81 ** Check zFossil to see if it is a reasonable "fossil" command to
82 ** run on the server. Do not allow an attacker to substitute something
83 ** like "/bin/rm".
84 */
85 static int is_safe_fossil_command(const char *zFossil){
86 static const char *const azSafe[] = { "*/fossil", "*/fossil.exe", "*/echo" };
87 int i;
 
88 for(i=0; i<(int)(sizeof(azSafe)/sizeof(azSafe[0])); i++){
89 if( sqlite3_strglob(azSafe[i], zFossil)==0 ) return 1;
90 if( strcmp(azSafe[i]+2, zFossil)==0 ) return 1;
91 }
92 return 0;
@@ -138,21 +151,30 @@
138 blob_append_escaped_arg(&zCmd, pUrlData->name, 0);
139 }
140 if( (pUrlData->flags & URL_SSH_EXE)!=0
141 && !is_safe_fossil_command(pUrlData->fossil)
142 ){
143 fossil_fatal("the ssh:// URL is asking to run an unsafe command [%s] on "
144 "the server.", pUrlData->fossil);
 
 
145 }
146 if( (pUrlData->flags & URL_SSH_EXE)==0
147 && (pUrlData->flags & URL_SSH_PATH)!=0
148 ){
149 ssh_add_path_argument(&zCmd);
150 }
151 blob_append_escaped_arg(&zCmd, pUrlData->fossil, 1);
152 blob_append(&zCmd, " test-http", 10);
153 if( pUrlData->path && pUrlData->path[0] ){
 
 
 
 
 
 
 
154 blob_append_escaped_arg(&zCmd, pUrlData->path, 1);
155 }else{
156 fossil_fatal("ssh:// URI does not specify a path to the repository");
157 }
158 if( g.fSshTrace || g.fHttpTrace ){
159
--- src/http_transport.c
+++ src/http_transport.c
@@ -74,19 +74,32 @@
74 if( resetFlag ){
75 transport.nSent = 0;
76 transport.nRcvd = 0;
77 }
78 }
79
80 /*
81 ** Whatever follows the host name on an ssh command line is joined into
82 ** one string by ssh and parsed again by the login shell on the remote
83 ** system. Quoting for the local shell does not protect an argument
84 ** from that second parse. Return true if zArg holds none of the
85 ** characters that a shell treats specially, so that the remote shell
86 ** takes it as a single word, unchanged.
87 */
88 int ssh_remote_arg_is_safe(const char *zArg){
89 return strpbrk(zArg, " !\"#$&'()*;<>?[]^`{|}")==0;
90 }
91
92 /*
93 ** Check zFossil to see if it is a reasonable "fossil" command to
94 ** run on the server. Do not allow an attacker to substitute something
95 ** like "/bin/rm", or to add to the command.
96 */
97 static int is_safe_fossil_command(const char *zFossil){
98 static const char *const azSafe[] = { "*/fossil", "*/fossil.exe", "*/echo" };
99 int i;
100 if( !ssh_remote_arg_is_safe(zFossil) ) return 0;
101 for(i=0; i<(int)(sizeof(azSafe)/sizeof(azSafe[0])); i++){
102 if( sqlite3_strglob(azSafe[i], zFossil)==0 ) return 1;
103 if( strcmp(azSafe[i]+2, zFossil)==0 ) return 1;
104 }
105 return 0;
@@ -138,21 +151,30 @@
151 blob_append_escaped_arg(&zCmd, pUrlData->name, 0);
152 }
153 if( (pUrlData->flags & URL_SSH_EXE)!=0
154 && !is_safe_fossil_command(pUrlData->fossil)
155 ){
156 socket_set_errmsg("the ssh:// URL is asking to run an unsafe command "
157 "[%s] on the server.", pUrlData->fossil);
158 blob_reset(&zCmd);
159 return 1;
160 }
161 if( (pUrlData->flags & URL_SSH_EXE)==0
162 && (pUrlData->flags & URL_SSH_PATH)!=0
163 ){
164 ssh_add_path_argument(&zCmd);
165 }
166 blob_append_escaped_arg(&zCmd, pUrlData->fossil, 1);
167 blob_append(&zCmd, " test-http", 10);
168 if( pUrlData->path && pUrlData->path[0] ){
169 if( !ssh_remote_arg_is_safe(pUrlData->path) ){
170 socket_set_errmsg("the repository path \"%s\" in the ssh:// URL "
171 "contains a character that is special to the shell "
172 "on the remote system", pUrlData->path);
173 blob_reset(&zCmd);
174 return 1;
175 }
176 blob_append_escaped_arg(&zCmd, pUrlData->path, 1);
177 }else{
178 fossil_fatal("ssh:// URI does not specify a path to the repository");
179 }
180 if( g.fSshTrace || g.fHttpTrace ){
181
+5 -1
--- src/main.c
+++ src/main.c
@@ -3563,16 +3563,20 @@
35633563
int bRunning = 0; /* True when fossil starts up on the remote */
35643564
int isRetry; /* True if on the second attempt */
35653565
const char *zUrl = g.argv[2];
35663566
char zLine[1000];
35673567
3568
+ if( !ssh_remote_arg_is_safe(zUrl) ){
3569
+ fossil_fatal("the repository name \"%s\" contains a character that "
3570
+ "is special to the shell on the remote system", zUrl);
3571
+ }
35683572
blob_init(&ssh, 0, 0);
35693573
for(isRetry=0; isRetry<2 && !bRunning; isRetry++){
35703574
blob_reset(&ssh);
35713575
transport_ssh_command(&ssh);
35723576
blob_appendf(&ssh,
3573
- " -t -L 127.0.0.1:%d:127.0.0.1:%d %!$",
3577
+ " -t -L 127.0.0.1:%d:127.0.0.1:%d -- %!$",
35743578
iPort, iPort, zRemote
35753579
);
35763580
if( zFossilCmd==0 ){
35773581
if( ssh_needs_path_argument(zRemote,-1) ^ isRetry ){
35783582
ssh_add_path_argument(&ssh);
35793583
--- src/main.c
+++ src/main.c
@@ -3563,16 +3563,20 @@
3563 int bRunning = 0; /* True when fossil starts up on the remote */
3564 int isRetry; /* True if on the second attempt */
3565 const char *zUrl = g.argv[2];
3566 char zLine[1000];
3567
 
 
 
 
3568 blob_init(&ssh, 0, 0);
3569 for(isRetry=0; isRetry<2 && !bRunning; isRetry++){
3570 blob_reset(&ssh);
3571 transport_ssh_command(&ssh);
3572 blob_appendf(&ssh,
3573 " -t -L 127.0.0.1:%d:127.0.0.1:%d %!$",
3574 iPort, iPort, zRemote
3575 );
3576 if( zFossilCmd==0 ){
3577 if( ssh_needs_path_argument(zRemote,-1) ^ isRetry ){
3578 ssh_add_path_argument(&ssh);
3579
--- src/main.c
+++ src/main.c
@@ -3563,16 +3563,20 @@
3563 int bRunning = 0; /* True when fossil starts up on the remote */
3564 int isRetry; /* True if on the second attempt */
3565 const char *zUrl = g.argv[2];
3566 char zLine[1000];
3567
3568 if( !ssh_remote_arg_is_safe(zUrl) ){
3569 fossil_fatal("the repository name \"%s\" contains a character that "
3570 "is special to the shell on the remote system", zUrl);
3571 }
3572 blob_init(&ssh, 0, 0);
3573 for(isRetry=0; isRetry<2 && !bRunning; isRetry++){
3574 blob_reset(&ssh);
3575 transport_ssh_command(&ssh);
3576 blob_appendf(&ssh,
3577 " -t -L 127.0.0.1:%d:127.0.0.1:%d -- %!$",
3578 iPort, iPort, zRemote
3579 );
3580 if( zFossilCmd==0 ){
3581 if( ssh_needs_path_argument(zRemote,-1) ^ isRetry ){
3582 ssh_add_path_argument(&ssh);
3583
+12
--- src/merge3.c
+++ src/merge3.c
@@ -1195,10 +1195,22 @@
11951195
zOrig = file_newname(zV1, "original", 1);
11961196
blob_write_to_file(s.pV1, zOrig);
11971197
zOther = file_newname(zV1, "merge", 1);
11981198
blob_write_to_file(s.pV2, zOther);
11991199
if( rc>0 ){
1200
+#if !defined(_WIN32)
1201
+ if( zGMerge && zGMerge[0] && strpbrk(zPivot, "\"$`")!=0 ){
1202
+ /* The file names are put into the gmerge-command as they are,
1203
+ ** and the examples for that setting put double quotes around
1204
+ ** them. A name that holds one of these characters would not
1205
+ ** reach the command as one argument. zPivot stands for all
1206
+ ** four names, which differ only in their suffix. */
1207
+ fossil_warning("not running the gmerge-command on \"%s\" because "
1208
+ "the name holds a character that is special to "
1209
+ "the shell", zV1);
1210
+ }else
1211
+#endif
12001212
if( zGMerge && zGMerge[0] ){
12011213
char *zOut; /* Temporary output file */
12021214
char *zCmd; /* Command to invoke */
12031215
const char *azSubst[8]; /* Strings to be substituted */
12041216
zOut = file_newname(zV1, "output", 1);
12051217
--- src/merge3.c
+++ src/merge3.c
@@ -1195,10 +1195,22 @@
1195 zOrig = file_newname(zV1, "original", 1);
1196 blob_write_to_file(s.pV1, zOrig);
1197 zOther = file_newname(zV1, "merge", 1);
1198 blob_write_to_file(s.pV2, zOther);
1199 if( rc>0 ){
 
 
 
 
 
 
 
 
 
 
 
 
1200 if( zGMerge && zGMerge[0] ){
1201 char *zOut; /* Temporary output file */
1202 char *zCmd; /* Command to invoke */
1203 const char *azSubst[8]; /* Strings to be substituted */
1204 zOut = file_newname(zV1, "output", 1);
1205
--- src/merge3.c
+++ src/merge3.c
@@ -1195,10 +1195,22 @@
1195 zOrig = file_newname(zV1, "original", 1);
1196 blob_write_to_file(s.pV1, zOrig);
1197 zOther = file_newname(zV1, "merge", 1);
1198 blob_write_to_file(s.pV2, zOther);
1199 if( rc>0 ){
1200 #if !defined(_WIN32)
1201 if( zGMerge && zGMerge[0] && strpbrk(zPivot, "\"$`")!=0 ){
1202 /* The file names are put into the gmerge-command as they are,
1203 ** and the examples for that setting put double quotes around
1204 ** them. A name that holds one of these characters would not
1205 ** reach the command as one argument. zPivot stands for all
1206 ** four names, which differ only in their suffix. */
1207 fossil_warning("not running the gmerge-command on \"%s\" because "
1208 "the name holds a character that is special to "
1209 "the shell", zV1);
1210 }else
1211 #endif
1212 if( zGMerge && zGMerge[0] ){
1213 char *zOut; /* Temporary output file */
1214 char *zCmd; /* Command to invoke */
1215 const char *azSubst[8]; /* Strings to be substituted */
1216 zOut = file_newname(zV1, "output", 1);
1217
+1 -1
--- src/patch.c
+++ src/patch.c
@@ -756,11 +756,11 @@
756756
blob_appendf(&cmd, " patch %s%s %$ -", zRemoteCmd, zForce, zDir);
757757
}else{
758758
Blob remote;
759759
*(char*)(zDir-1) = 0;
760760
transport_ssh_command(&cmd);
761
- blob_appendf(&cmd, " -T");
761
+ blob_appendf(&cmd, " -T --"); /* End of switches */
762762
blob_append_escaped_arg(&cmd, zRemote, 0);
763763
blob_init(&remote, 0, 0);
764764
if( zFossilCmd==0 ){
765765
if( ssh_needs_path_argument(zRemote,-1) ^ isRetry ){
766766
ssh_add_path_argument(&cmd);
767767
--- src/patch.c
+++ src/patch.c
@@ -756,11 +756,11 @@
756 blob_appendf(&cmd, " patch %s%s %$ -", zRemoteCmd, zForce, zDir);
757 }else{
758 Blob remote;
759 *(char*)(zDir-1) = 0;
760 transport_ssh_command(&cmd);
761 blob_appendf(&cmd, " -T");
762 blob_append_escaped_arg(&cmd, zRemote, 0);
763 blob_init(&remote, 0, 0);
764 if( zFossilCmd==0 ){
765 if( ssh_needs_path_argument(zRemote,-1) ^ isRetry ){
766 ssh_add_path_argument(&cmd);
767
--- src/patch.c
+++ src/patch.c
@@ -756,11 +756,11 @@
756 blob_appendf(&cmd, " patch %s%s %$ -", zRemoteCmd, zForce, zDir);
757 }else{
758 Blob remote;
759 *(char*)(zDir-1) = 0;
760 transport_ssh_command(&cmd);
761 blob_appendf(&cmd, " -T --"); /* End of switches */
762 blob_append_escaped_arg(&cmd, zRemote, 0);
763 blob_init(&remote, 0, 0);
764 if( zFossilCmd==0 ){
765 if( ssh_needs_path_argument(zRemote,-1) ^ isRetry ){
766 ssh_add_path_argument(&cmd);
767

Keyboard Shortcuts

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