-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added context struct #43
Conversation
interpreter to be exist at the same time.
} | ||
|
||
|
||
/* Include files from command line */ | ||
|
||
for(i=0; i<argc; i++) { | ||
include(argv[i]); | ||
include(ctx, argv[i]); |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user input (a command-line argument)
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 17 days ago
To fix the problem, we need to validate the user input before using it to construct a file path. Specifically, we should ensure that the file path provided by the user does not contain any path separators or ".." sequences, which could lead to path traversal vulnerabilities. We can achieve this by adding a validation function that checks for these invalid sequences and rejects the input if any are found.
-
Copy modified lines R271-R274
@@ -270,2 +270,6 @@ | ||
for(i=0; i<argc; i++) { | ||
if (strstr(argv[i], "..") || strchr(argv[i], '/') || strchr(argv[i], '\\')) { | ||
fprintf(stderr, "Invalid file path: %s\n", argv[i]); | ||
continue; | ||
} | ||
include(ctx, argv[i]); |
if(code <= PRIM_COUNT) { | ||
do_prim((zf_prim)code, input); | ||
if(code < PRIM_COUNT) { | ||
do_prim(ctx, (zf_prim)code, input); |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user input (string read by fgets)
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 17 days ago
To fix the problem, we need to validate the user input before it is used to construct a file path. Specifically, we should ensure that the input does not contain any path separators or ".." sequences that could lead to path traversal attacks. This can be done by adding a validation function that checks for these invalid sequences and rejects the input if any are found.
- Add a validation function to check for invalid sequences in the user input.
- Use this validation function in the
include
function insrc/linux/main.c
before callingfopen
. - Ensure that the
input
parameter in therun
function insrc/zforth/zforth.c
is validated before it is used.
-
Copy modified lines R453-R458
@@ -452,2 +452,8 @@ | ||
{ | ||
// Validate the input | ||
if (input && (strstr(input, "..") || strchr(input, '/') || strchr(input, '\\'))) { | ||
fprintf(stderr, "Invalid input.\n"); | ||
return; | ||
} | ||
|
||
while(ctx->ip != 0) { |
-
Copy modified lines R63-R68
@@ -62,2 +62,8 @@ | ||
|
||
// Validate the filename | ||
if (strstr(fname, "..") || strchr(fname, '/') || strchr(fname, '\\')) { | ||
fprintf(stderr, "Invalid filename.\n"); | ||
return; | ||
} | ||
|
||
FILE *f = fopen(fname, "rb"); |
* definition as a literal. At run time, the value will be pushed | ||
* on the stack. */ | ||
if(COMPILING(ctx)) dict_add_lit(ctx, zf_pop(ctx)); | ||
/* FIXME: else abort "!compiling"? */ |
Check notice
Code scanning / CodeQL
FIXME comment Note
break; | ||
|
||
case PRIM_EXIT: | ||
ip = zf_popr(); | ||
/* Return from word */ | ||
ctx->ip = zf_popr(ctx); |
Check warning
Code scanning / CodeQL
Lossy function result cast Warning
zf_push(peek(addr, &d1, len)); | ||
/* Get length of cell; consumes size encoding and address */ | ||
size = zf_pop(ctx); | ||
addr = zf_pop(ctx); |
Check warning
Code scanning / CodeQL
Lossy function result cast Warning
zf_push(d1); | ||
/* Peek at memory; consumes size encoding and address */ | ||
size = zf_pop(ctx); | ||
addr = zf_pop(ctx); |
Check warning
Code scanning / CodeQL
Lossy function result cast Warning
d1 = zf_pop(); | ||
/* Poke memory; consumes size encoding, address, and value */ | ||
size = zf_pop(ctx); | ||
addr = zf_pop(ctx); |
Check warning
Code scanning / CodeQL
Lossy function result cast Warning
addr = zf_pop(); | ||
zf_push(zf_pick(addr)); | ||
/* Pick n-th element from stack */ | ||
addr = zf_pop(ctx); |
Check warning
Code scanning / CodeQL
Lossy function result cast Warning
addr = zf_pop(); | ||
zf_push(zf_pickr(addr)); | ||
/* Pick n-th element from return stack */ | ||
addr = zf_pop(ctx); |
Check warning
Code scanning / CodeQL
Lossy function result cast Warning
break; | ||
|
||
case PRIM_EQUAL: | ||
zf_push(zf_pop() == zf_pop()); | ||
/* Push true if top two elements on stack are equal, else false */ | ||
zf_push(ctx, zf_pop(ctx) == zf_pop(ctx) ? ZF_TRUE : ZF_FALSE); |
Check notice
Code scanning / CodeQL
Equality test on floating-point values Note
This change allows a program to create multiple instances of the zforth interpreter and virtual machine: moves all the globals from
zforth.c
to a new structzf_ctx
and adds thezf_ctx *ctx
argument to all zforth functions.This does result in a bit larger binary size.