Skip to content

Commit 6c2f82c

Browse files
committed
fix(pipeline): repair Ensemble routing settings lookup, XML bounds
Maintainer fixups pushed to the PR branch, each with a test that fails without it. - The TargetConfigNames/TargetConfigName path emitted nothing. Route nodes are keyed __route__ensemble__<prod>.<item> (as emit_async_call and emit_handles both do), but the settings loop looked the source item up by its bare <prod>.<item> qn. cbm_gbuf_find_by_qn is an exact hashtable lookup (graph_buffer.c:769), so item_node was always NULL and every declarative routing edge was dropped by `continue`. An incomplete rename from the EnsembleItem version. Proof: the new settings test reports async_calls == 0 against the previous code. - Heap-buffer-overflow on malformed XML. With an <Item that has no closing </Item>, item_end became the buffer's NUL terminator and `p = item_end + 7` then pointed 7 bytes past the allocation, which the loop dereferenced before strstr scanned unbounded heap. Now the scan stops at an unterminated item. Proof: ASan reports heap-buffer-overflow at pass_ensemble_routing.c:544 against the previous code. - extract_xml_attr scanned forward from the tag with no bound, so a tag omitting an attribute silently inherited it from a LATER tag, mispairing ClassName/Enabled across items. The search is now bounded at the tag's closing '>'. Proof: an item with no ClassName produced a second, bogus Route node against the previous code. - read_file ignored the fread result, leaving an uninitialised tail on a short read for the string scans to walk. Terminate at the byte count actually read. Three tests added: a production whose ONLY routing information is a TargetConfigNames setting (so any ASYNC_CALLS edge must come from that path), an unterminated <Item> in an otherwise well-formed file, and an item missing ClassName ahead of a well-formed one. Full local suite: 7576 passed, 0 failed, 8 skipped (139 suites). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent ee12615 commit 6c2f82c

2 files changed

Lines changed: 203 additions & 6 deletions

File tree

‎src/pipeline/pass_ensemble_routing.c‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,18 @@ typedef struct {
7474
static void extract_xml_attr(const char *xml, int offset, const char *attr, char *out, int outsz) {
7575
char needle[CBM_SZ_64];
7676
snprintf(needle, sizeof(needle), "%s=\"", attr);
77-
const char *p = strstr(xml + offset, needle);
7877
out[0] = '\0';
79-
if (!p)
78+
const char *tag = xml + offset;
79+
/* An attribute belongs to THIS tag only. Without bounding the search at the
80+
* tag's closing '>', a tag that omits the attribute silently inherits it
81+
* from a later tag, mispairing ClassName/Enabled across production items. */
82+
const char *tag_end = strchr(tag, '>');
83+
const char *p = strstr(tag, needle);
84+
if (!p || (tag_end && p >= tag_end))
8085
return;
8186
p += strlen(needle);
8287
const char *e = strchr(p, '"');
83-
if (!e)
88+
if (!e || (tag_end && e >= tag_end))
8489
return;
8590
int len = (int)(e - p);
8691
if (len >= outsz)
@@ -128,6 +133,7 @@ static ens_prod_def_t *parse_production_xml(const char *xml, const char *class_q
128133
}
129134

130135
const char *item_end = strstr(item_start, "</Item>");
136+
const bool item_terminated = item_end != NULL;
131137
if (!item_end)
132138
item_end = item_start + strlen(item_start);
133139

@@ -165,6 +171,10 @@ static ens_prod_def_t *parse_production_xml(const char *xml, const char *class_q
165171
"settings truncated at MAX_SETTINGS cap");
166172
}
167173
def->n_items++;
174+
/* item_end is the NUL terminator when the item is unterminated, so
175+
* advancing by strlen("</Item>") would read past the allocation. */
176+
if (!item_terminated)
177+
break;
168178
p = item_end + 7;
169179
}
170180
/* Warn when items were truncated at the cap. */
@@ -191,8 +201,8 @@ static char *read_file(const char *full_path) {
191201
fclose(f);
192202
return NULL;
193203
}
194-
fread(buf, 1, (size_t)sz, f);
195-
buf[sz] = '\0';
204+
size_t got = fread(buf, 1, (size_t)sz, f);
205+
buf[got] = '\0';
196206
fclose(f);
197207
return buf;
198208
}
@@ -586,7 +596,12 @@ void cbm_pipeline_pass_ensemble_routing(cbm_pipeline_ctx_t *ctx) {
586596
continue;
587597
char item_qn[CBM_SZ_512];
588598
snprintf(item_qn, sizeof(item_qn), "%s.%s", def->production_class, item->item_name);
589-
const cbm_gbuf_node_t *item_node = cbm_gbuf_find_by_qn(ctx->gbuf, item_qn);
599+
/* Route nodes are keyed by the prefixed qn (see emit_async_call);
600+
* cbm_gbuf_find_by_qn is an exact hashtable lookup, so the bare
601+
* item qn never matches and every settings edge was dropped. */
602+
char item_route_qn[CBM_SZ_512];
603+
snprintf(item_route_qn, sizeof(item_route_qn), "__route__ensemble__%s", item_qn);
604+
const cbm_gbuf_node_t *item_node = cbm_gbuf_find_by_qn(ctx->gbuf, item_route_qn);
590605
if (!item_node)
591606
continue;
592607
if (strcmp(setting->setting_name, "TargetConfigNames") == 0) {

‎tests/test_pipeline.c‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12150,6 +12150,185 @@ TEST(pipeline_ensemble_routing_edges) {
1215012150
PASS();
1215112151
}
1215212152

12153+
TEST(pipeline_ensemble_routing_attr_does_not_leak_across_items) {
12154+
char tmpdir[256];
12155+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_ens_attr_XXXXXX");
12156+
if (!cbm_mkdtemp(tmpdir))
12157+
FAIL("failed to create temp dir");
12158+
12159+
char path[512];
12160+
12161+
/* The first item declares no ClassName. Attribute lookup used to scan
12162+
* forward from the tag with no bound, so it inherited ClassName from the
12163+
* SECOND item and emitted a Route for an item that does not name a class,
12164+
* pairing it with the wrong implementation. Only the well-formed item may
12165+
* produce a Route. */
12166+
snprintf(path, sizeof(path), "%s/AttrProduction.cls", tmpdir);
12167+
FILE *f = fopen(path, "w");
12168+
if (!f) {
12169+
th_rmtree(tmpdir);
12170+
FAIL("fopen production");
12171+
}
12172+
fprintf(f, "Class MyApp.AttrProduction Extends Ens.Production\n"
12173+
"{\n"
12174+
"XData ProductionDefinition\n"
12175+
"{\n"
12176+
"<Production Name=\"MyApp.AttrProduction\">\n"
12177+
" <Item Name=\"NoClass\" Enabled=\"true\">\n"
12178+
" </Item>\n"
12179+
" <Item Name=\"Real\" ClassName=\"MyApp.Real\" Enabled=\"true\">\n"
12180+
" </Item>\n"
12181+
"</Production>\n"
12182+
"}\n"
12183+
"}\n");
12184+
fclose(f);
12185+
12186+
char db_path[512];
12187+
snprintf(db_path, sizeof(db_path), "%s/ens.db", tmpdir);
12188+
12189+
cbm_pipeline_t *p = cbm_pipeline_new(tmpdir, db_path, CBM_MODE_FULL);
12190+
ASSERT_NOT_NULL(p);
12191+
ASSERT_EQ(cbm_pipeline_run(p), 0);
12192+
12193+
cbm_store_t *s = cbm_store_open_path(db_path);
12194+
ASSERT_NOT_NULL(s);
12195+
const char *project = cbm_pipeline_project_name(p);
12196+
cbm_node_t *routes = NULL;
12197+
int route_count = 0;
12198+
cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count);
12199+
int ens_route_count = 0;
12200+
for (int i = 0; i < route_count; i++) {
12201+
if (routes[i].qualified_name &&
12202+
strstr(routes[i].qualified_name, "__route__ensemble__") != NULL)
12203+
ens_route_count++;
12204+
}
12205+
ASSERT_EQ(ens_route_count, 1);
12206+
cbm_store_free_nodes(routes, route_count);
12207+
12208+
cbm_store_close(s);
12209+
cbm_pipeline_free(p);
12210+
th_rmtree(tmpdir);
12211+
PASS();
12212+
}
12213+
12214+
TEST(pipeline_ensemble_routing_settings_targets) {
12215+
char tmpdir[256];
12216+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_ens_set_XXXXXX");
12217+
if (!cbm_mkdtemp(tmpdir))
12218+
FAIL("failed to create temp dir");
12219+
12220+
char path[512];
12221+
12222+
/* A production whose ONLY routing information is declarative: a
12223+
* TargetConfigNames setting on the router item. No class file defines
12224+
* SendRequestSync anywhere, so every ASYNC_CALLS edge this run produces
12225+
* must have come from the settings path. */
12226+
snprintf(path, sizeof(path), "%s/RouterProduction.cls", tmpdir);
12227+
FILE *f = fopen(path, "w");
12228+
if (!f) {
12229+
th_rmtree(tmpdir);
12230+
FAIL("fopen production");
12231+
}
12232+
fprintf(f, "Class MyApp.RouterProduction Extends Ens.Production\n"
12233+
"{\n"
12234+
"XData ProductionDefinition\n"
12235+
"{\n"
12236+
"<Production Name=\"MyApp.RouterProduction\">\n"
12237+
" <Item Name=\"Router\" ClassName=\"MyApp.Router\" Enabled=\"true\">\n"
12238+
" <Setting Target=\"Host\" Name=\"TargetConfigNames\">OpA,OpB</Setting>\n"
12239+
" </Item>\n"
12240+
" <Item Name=\"OpA\" ClassName=\"MyApp.OpA\" Enabled=\"true\">\n"
12241+
" </Item>\n"
12242+
" <Item Name=\"OpB\" ClassName=\"MyApp.OpB\" Enabled=\"true\">\n"
12243+
" </Item>\n"
12244+
"</Production>\n"
12245+
"}\n"
12246+
"}\n");
12247+
fclose(f);
12248+
12249+
char db_path[512];
12250+
snprintf(db_path, sizeof(db_path), "%s/ens.db", tmpdir);
12251+
12252+
cbm_pipeline_t *p = cbm_pipeline_new(tmpdir, db_path, CBM_MODE_FULL);
12253+
ASSERT_NOT_NULL(p);
12254+
ASSERT_EQ(cbm_pipeline_run(p), 0);
12255+
12256+
cbm_store_t *s = cbm_store_open_path(db_path);
12257+
ASSERT_NOT_NULL(s);
12258+
const char *project = cbm_pipeline_project_name(p);
12259+
12260+
/* Router -> OpA and Router -> OpB, both derived from TargetConfigNames.
12261+
* Before the Route-qn fix this was 0: the settings loop looked the source
12262+
* item up by its bare qn, which cbm_gbuf_find_by_qn (an exact hashtable
12263+
* lookup) never matches, so every edge was skipped by `continue`. */
12264+
int async_calls = cbm_store_count_edges_by_type(s, project, "ASYNC_CALLS");
12265+
ASSERT_EQ(async_calls, 2);
12266+
12267+
cbm_store_close(s);
12268+
cbm_pipeline_free(p);
12269+
th_rmtree(tmpdir);
12270+
PASS();
12271+
}
12272+
12273+
TEST(pipeline_ensemble_routing_unterminated_item_is_safe) {
12274+
char tmpdir[256];
12275+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_ens_bad_XXXXXX");
12276+
if (!cbm_mkdtemp(tmpdir))
12277+
FAIL("failed to create temp dir");
12278+
12279+
char path[512];
12280+
12281+
/* Truncated XData: an <Item that never closes. The parser used to set
12282+
* item_end to the buffer's NUL terminator and then advance by
12283+
* strlen("</Item>"), reading 7 bytes past the allocation and scanning
12284+
* unbounded heap from there. Under ASan this aborts the run. */
12285+
snprintf(path, sizeof(path), "%s/BadProduction.cls", tmpdir);
12286+
FILE *f = fopen(path, "w");
12287+
if (!f) {
12288+
th_rmtree(tmpdir);
12289+
FAIL("fopen production");
12290+
}
12291+
fprintf(f, "Class MyApp.BadProduction Extends Ens.Production\n"
12292+
"{\n"
12293+
"XData ProductionDefinition\n"
12294+
"{\n"
12295+
"<Production Name=\"MyApp.BadProduction\">\n"
12296+
" <Item Name=\"Orphan\" ClassName=\"MyApp.Orphan\" Enabled=\"true\">\n"
12297+
"</Production>\n"
12298+
"}\n"
12299+
"}\n");
12300+
fclose(f);
12301+
12302+
char db_path[512];
12303+
snprintf(db_path, sizeof(db_path), "%s/ens.db", tmpdir);
12304+
12305+
cbm_pipeline_t *p = cbm_pipeline_new(tmpdir, db_path, CBM_MODE_FULL);
12306+
ASSERT_NOT_NULL(p);
12307+
ASSERT_EQ(cbm_pipeline_run(p), 0);
12308+
12309+
/* The well-formed prefix is still honoured: the item is parsed and its
12310+
* Route node emitted. Malformed input degrades, it does not disappear. */
12311+
cbm_store_t *s = cbm_store_open_path(db_path);
12312+
ASSERT_NOT_NULL(s);
12313+
const char *project = cbm_pipeline_project_name(p);
12314+
cbm_node_t *routes = NULL;
12315+
int route_count = 0;
12316+
cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count);
12317+
int ens_route_count = 0;
12318+
for (int i = 0; i < route_count; i++) {
12319+
if (routes[i].qualified_name &&
12320+
strstr(routes[i].qualified_name, "__route__ensemble__") != NULL)
12321+
ens_route_count++;
12322+
}
12323+
ASSERT_EQ(ens_route_count, 1);
12324+
cbm_store_free_nodes(routes, route_count);
12325+
12326+
cbm_store_close(s);
12327+
cbm_pipeline_free(p);
12328+
th_rmtree(tmpdir);
12329+
PASS();
12330+
}
12331+
1215312332
TEST(pipeline_ensemble_routing_method_scoping) {
1215412333
/* Verifies scan_source_for_send_targets scopes to the calling method body.
1215512334
* Two methods in the same class each route to a different operation; each
@@ -12592,6 +12771,9 @@ SUITE(pipeline) {
1259212771
/* Ensemble routing pass */
1259312772
RUN_TEST(pipeline_ensemble_routing_edges);
1259412773
RUN_TEST(pipeline_ensemble_routing_method_scoping);
12774+
RUN_TEST(pipeline_ensemble_routing_attr_does_not_leak_across_items);
12775+
RUN_TEST(pipeline_ensemble_routing_settings_targets);
12776+
RUN_TEST(pipeline_ensemble_routing_unterminated_item_is_safe);
1259512777
}
1259612778

1259712779
/* Focused semantic-manifest and publication contracts. Kept separate from the

0 commit comments

Comments
 (0)