Fossil SCM
In the ssh transport and in "fossil ui HOST:REPO", refuse a repository name that holds a character special to a shell, and refuse such a character in the fossil= parameter of an ssh:// URL. The name was quoted for the local shell only, and the login shell on the remote system parsed it again. An ssh:// URL that is refused is reported as a failed sync and no longer as a fatal error.
Commit
e6c67c8b596813f04101c207b793d7bdd6a427e6ba5bf734f0c9d1a6b2652198
Parent
2f5b53565d98f1b…
2 files changed
+25
-3
+4
+25
-3
| --- src/http_transport.c | ||
| +++ src/http_transport.c | ||
| @@ -74,19 +74,32 @@ | ||
| 74 | 74 | if( resetFlag ){ |
| 75 | 75 | transport.nSent = 0; |
| 76 | 76 | transport.nRcvd = 0; |
| 77 | 77 | } |
| 78 | 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 | +} | |
| 79 | 91 | |
| 80 | 92 | /* |
| 81 | 93 | ** Check zFossil to see if it is a reasonable "fossil" command to |
| 82 | 94 | ** 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. | |
| 84 | 96 | */ |
| 85 | 97 | static int is_safe_fossil_command(const char *zFossil){ |
| 86 | 98 | static const char *const azSafe[] = { "*/fossil", "*/fossil.exe", "*/echo" }; |
| 87 | 99 | int i; |
| 100 | + if( !ssh_remote_arg_is_safe(zFossil) ) return 0; | |
| 88 | 101 | for(i=0; i<(int)(sizeof(azSafe)/sizeof(azSafe[0])); i++){ |
| 89 | 102 | if( sqlite3_strglob(azSafe[i], zFossil)==0 ) return 1; |
| 90 | 103 | if( strcmp(azSafe[i]+2, zFossil)==0 ) return 1; |
| 91 | 104 | } |
| 92 | 105 | return 0; |
| @@ -138,21 +151,30 @@ | ||
| 138 | 151 | blob_append_escaped_arg(&zCmd, pUrlData->name, 0); |
| 139 | 152 | } |
| 140 | 153 | if( (pUrlData->flags & URL_SSH_EXE)!=0 |
| 141 | 154 | && !is_safe_fossil_command(pUrlData->fossil) |
| 142 | 155 | ){ |
| 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; | |
| 145 | 160 | } |
| 146 | 161 | if( (pUrlData->flags & URL_SSH_EXE)==0 |
| 147 | 162 | && (pUrlData->flags & URL_SSH_PATH)!=0 |
| 148 | 163 | ){ |
| 149 | 164 | ssh_add_path_argument(&zCmd); |
| 150 | 165 | } |
| 151 | 166 | blob_append_escaped_arg(&zCmd, pUrlData->fossil, 1); |
| 152 | 167 | blob_append(&zCmd, " test-http", 10); |
| 153 | 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 | + } | |
| 154 | 176 | blob_append_escaped_arg(&zCmd, pUrlData->path, 1); |
| 155 | 177 | }else{ |
| 156 | 178 | fossil_fatal("ssh:// URI does not specify a path to the repository"); |
| 157 | 179 | } |
| 158 | 180 | if( g.fSshTrace || g.fHttpTrace ){ |
| 159 | 181 |
| --- 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 |
+4
| --- src/main.c | ||
| +++ src/main.c | ||
| @@ -3563,10 +3563,14 @@ | ||
| 3563 | 3563 | int bRunning = 0; /* True when fossil starts up on the remote */ |
| 3564 | 3564 | int isRetry; /* True if on the second attempt */ |
| 3565 | 3565 | const char *zUrl = g.argv[2]; |
| 3566 | 3566 | char zLine[1000]; |
| 3567 | 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 | + } | |
| 3568 | 3572 | blob_init(&ssh, 0, 0); |
| 3569 | 3573 | for(isRetry=0; isRetry<2 && !bRunning; isRetry++){ |
| 3570 | 3574 | blob_reset(&ssh); |
| 3571 | 3575 | transport_ssh_command(&ssh); |
| 3572 | 3576 | blob_appendf(&ssh, |
| 3573 | 3577 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -3563,10 +3563,14 @@ | |
| 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 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -3563,10 +3563,14 @@ | |
| 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 |