Skip to content

Commit a0da4c6

Browse files
authored
Merge pull request uniconproject#429 from Jafaral/wildcard-fix
runtime: fix buffer overflow with wildcard shell expansion in open()
2 parents ad32bca + 8002711 commit a0da4c6

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/runtime/fsys.r

+15-5
Original file line numberDiff line numberDiff line change
@@ -1101,21 +1101,31 @@ Deliberate Syntax Error
11011101
else if (stat(fnamestr, &st) < 0) {
11021102
/* stat reported an error; file does not exist */
11031103
1104-
if (strchr(fnamestr, '*') || strchr(fnamestr, '?')) {
1105-
char tempbuf[1024];
1104+
if ((strlen(fnamestr) < MaxPath) && (strchr(fnamestr, '*') || strchr(fnamestr, '?'))) {
1105+
/* account for (2 * strlen(fnamestr)) + 128 for the shell script */
1106+
char tempbuf[MaxPath*2 + 128];
11061107
#if UNIX
11071108
/*
1108-
* attempted to open a wildcard. used to use ls(1) output.
1109+
* attempt to open a wildcard. used to use ls(1) output.
11091110
* Now using shell for-loop and echo in order to avoid bad
11101111
* answers when no match is found.
11111112
*/
1112-
sprintf(tempbuf, "for i in %s; do if [ \"$i\" != \"%s\" ]; then echo \"$i\"; fi; done", fnamestr, fnamestr);
1113+
int rt;
1114+
rt = snprintf(tempbuf, sizeof(tempbuf),
1115+
"for i in %s; do if [ \"$i\" != \"%s\" ]; then echo \"$i\"; fi; done",
1116+
fnamestr, fnamestr);
1117+
1118+
if (rt < 0 || rt > sizeof(tempbuf)) {
1119+
set_errortext(218);
1120+
fail;
1121+
}
1122+
11131123
status |= Fs_Pipe;
11141124
f = popen(tempbuf, "r");
11151125
#endif /* UNIX */
11161126
#if NT
11171127
/*
1118-
* attempted to open a wildcard, do file completion
1128+
* attempt to open a wildcard, do file completion
11191129
*/
11201130
strcpy(tempbuf, fnamestr);
11211131
if (*tempbuf) {

0 commit comments

Comments
 (0)