-
Notifications
You must be signed in to change notification settings - Fork 1.3k
input validation and removal of unsafe numeric parsing #628
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
Open
metsw24-max
wants to merge
1
commit into
apache:trunk
Choose a base branch
from
metsw24-max:input-validation-balancer-manager
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−22
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,9 @@ | |
| #include "apr_escape.h" | ||
| #include "mod_watchdog.h" | ||
|
|
||
| #include <errno.h> | ||
| #include <stdlib.h> | ||
|
|
||
| static const char *balancer_mutex_type = "proxy-balancer-shm"; | ||
| ap_slotmem_provider_t *storage = NULL; | ||
|
|
||
|
|
@@ -40,6 +43,54 @@ static APR_OPTIONAL_FN_TYPE(hc_show_exprs) *hc_show_exprs_f = NULL; | |
| static APR_OPTIONAL_FN_TYPE(hc_select_exprs) *hc_select_exprs_f = NULL; | ||
| static APR_OPTIONAL_FN_TYPE(hc_valid_expr) *hc_valid_expr_f = NULL; | ||
|
|
||
| static int balancer_parse_int(const char *val, int min, int max, int *result) | ||
| { | ||
| apr_int64_t parsed; | ||
| char *end = NULL; | ||
|
|
||
| if (!val || !*val) { | ||
| return 0; | ||
| } | ||
|
|
||
| errno = 0; | ||
| parsed = apr_strtoi64(val, &end, 10); | ||
| if (errno == ERANGE || end == val || *end != '\0') { | ||
| return 0; | ||
| } | ||
|
|
||
| if (parsed < min || parsed > max) { | ||
| return 0; | ||
| } | ||
|
|
||
| *result = (int)parsed; | ||
| return 1; | ||
| } | ||
|
|
||
| static int balancer_parse_lbfactor(const char *val, int *result) | ||
| { | ||
| char *end = NULL; | ||
| double parsed; | ||
| double scaled; | ||
|
|
||
| if (!val || !*val) { | ||
| return 0; | ||
| } | ||
|
|
||
| errno = 0; | ||
| parsed = strtod(val, &end); | ||
| if (errno == ERANGE || end == val || *end != '\0') { | ||
| return 0; | ||
| } | ||
|
|
||
| scaled = parsed * 100.0; | ||
| if (scaled < 100.0 || scaled > 10000.0) { | ||
| return 0; | ||
| } | ||
|
|
||
| *result = (int)scaled; | ||
| return 1; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| * Register our mutex type before the config is read so we | ||
|
|
@@ -1114,9 +1165,7 @@ static int balancer_process_balancer_worker(request_rec *r, proxy_server_conf *c | |
|
|
||
| if ((val = apr_table_get(params, "w_lf"))) { | ||
| int ival; | ||
| double fval = atof(val); | ||
| ival = fval * 100.0; | ||
| if (ival >= 100 && ival <= 10000) { | ||
| if (balancer_parse_lbfactor(val, &ival)) { | ||
| wsel->s->lbfactor = ival; | ||
| if (bsel) | ||
| recalc_factors(bsel); | ||
|
|
@@ -1139,29 +1188,50 @@ static int balancer_process_balancer_worker(request_rec *r, proxy_server_conf *c | |
| * on that # character, since the character == the flag | ||
| */ | ||
| if ((val = apr_table_get(params, "w_status_I"))) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_IGNORE_ERRORS_FLAG, atoi(val), wsel); | ||
| int ival; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. coding style nit, declaring |
||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_IGNORE_ERRORS_FLAG, ival, wsel); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_status_N"))) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_DRAIN_FLAG, atoi(val), wsel); | ||
| int ival; | ||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_DRAIN_FLAG, ival, wsel); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_status_D"))) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_DISABLED_FLAG, atoi(val), wsel); | ||
| int ival; | ||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_DISABLED_FLAG, ival, wsel); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_status_H"))) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_HOT_STANDBY_FLAG, atoi(val), wsel); | ||
| int ival; | ||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_HOT_STANDBY_FLAG, ival, wsel); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_status_R"))) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_HOT_SPARE_FLAG, atoi(val), wsel); | ||
| int ival; | ||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_HOT_SPARE_FLAG, ival, wsel); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_status_S"))) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_STOPPED_FLAG, atoi(val), wsel); | ||
| int ival; | ||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_STOPPED_FLAG, ival, wsel); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_status_C"))) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_HC_FAIL_FLAG, atoi(val), wsel); | ||
| int ival; | ||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| ap_proxy_set_wstatus(PROXY_WORKER_HC_FAIL_FLAG, ival, wsel); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_ls"))) { | ||
| int ival = atoi(val); | ||
| if (ival >= 0 && ival <= 99) { | ||
| int ival; | ||
| if (balancer_parse_int(val, 0, 99, &ival)) { | ||
| wsel->s->lbset = ival; | ||
| } | ||
| } | ||
|
|
@@ -1174,14 +1244,14 @@ static int balancer_process_balancer_worker(request_rec *r, proxy_server_conf *c | |
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_hp"))) { | ||
| int ival = atoi(val); | ||
| if (ival >= 1) { | ||
| int ival; | ||
| if (balancer_parse_int(val, 1, APR_INT32_MAX, &ival)) { | ||
| wsel->s->passes = ival; | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "w_hf"))) { | ||
| int ival = atoi(val); | ||
| if (ival >= 1) { | ||
| int ival; | ||
| if (balancer_parse_int(val, 1, APR_INT32_MAX, &ival)) { | ||
| wsel->s->fails = ival; | ||
| } | ||
| } | ||
|
|
@@ -1234,21 +1304,20 @@ static int balancer_process_balancer_worker(request_rec *r, proxy_server_conf *c | |
| } | ||
| } | ||
| if ((val = apr_table_get(params, "b_tmo"))) { | ||
| ival = atoi(val); | ||
| if (ival >= 0 && ival <= 7200) { /* 2 hrs enuff? */ | ||
| if (balancer_parse_int(val, 0, 7200, &ival)) { /* 2 hrs enuff? */ | ||
| bsel->s->timeout = apr_time_from_sec(ival); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "b_max"))) { | ||
| ival = atoi(val); | ||
| if (ival >= 0 && ival <= 99) { | ||
| if (balancer_parse_int(val, 0, 99, &ival)) { | ||
| bsel->s->max_attempts = ival; | ||
| bsel->s->max_attempts_set = 1; | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "b_sforce"))) { | ||
| ival = atoi(val); | ||
| bsel->s->sticky_force = (ival != 0); | ||
| if (balancer_parse_int(val, 0, 1, &ival)) { | ||
| bsel->s->sticky_force = (ival != 0); | ||
| } | ||
| } | ||
| if ((val = apr_table_get(params, "b_ss")) && *val) { | ||
| if (strlen(val) < (sizeof(bsel->s->sticky_path)-1)) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<errno.h>should be inherited via APR headers already, are these really needed?