Skip to content

Commit 47061cf

Browse files
committed
Fix misuses of set_error() and set_exception() callbacks that could sometimes lead to injection of formatting strings through query parameters
Fixes MapServer/MapServer-private#3
1 parent 882c009 commit 47061cf

File tree

10 files changed

+24
-16
lines changed

10 files changed

+24
-16
lines changed

include/mapcache.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ struct mapcache_extent_i {
148148
int maxy;
149149
};
150150

151-
151+
#ifdef __GNUC__
152+
/** Tag a function to have printf() formatting */
153+
#define MAPCACHE_PRINT_FUNC_FORMAT(format_idx, arg_idx) \
154+
__attribute__((__format__(__printf__, format_idx, arg_idx)))
155+
#else
156+
#define MAPCACHE_PRINT_FUNC_FORMAT(format_idx, arg_idx)
157+
#endif
152158

153159
mapcache_image *mapcache_error_image(mapcache_context *ctx, int width, int height, char *msg);
154160

@@ -164,9 +170,9 @@ struct mapcache_context {
164170
* \param code the error code
165171
* \param message human readable message of what happened
166172
*/
167-
void (*set_error)(mapcache_context *ctx, int code, char *message, ...);
173+
void (*set_error)(mapcache_context *ctx, int code, char *message, ...) MAPCACHE_PRINT_FUNC_FORMAT(3, 4);
168174

169-
void (*set_exception)(mapcache_context *ctx, char *key, char *message, ...);
175+
void (*set_exception)(mapcache_context *ctx, char *key, char *message, ...) MAPCACHE_PRINT_FUNC_FORMAT(3, 4);
170176

171177
/**
172178
* \brief query context to know if an error has occurred

lib/cache_fallback.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static int _mapcache_cache_fallback_tile_get(mapcache_context *ctx, mapcache_cac
8686
}
8787
}
8888
/* all backends failed, return primary error message */
89-
ctx->set_error(ctx,first_error,first_error_message);
89+
ctx->set_error(ctx,first_error,"%s",first_error_message);
9090
return MAPCACHE_FAILURE;
9191
} else {
9292
/* success or notfound */
@@ -113,7 +113,7 @@ static void _mapcache_cache_fallback_tile_set(mapcache_context *ctx, mapcache_ca
113113
}
114114
}
115115
if(first_error) {
116-
ctx->set_error(ctx,first_error,first_error_message);
116+
ctx->set_error(ctx,first_error,"%s",first_error_message);
117117
}
118118
}
119119

@@ -136,7 +136,7 @@ static void _mapcache_cache_fallback_tile_multi_set(mapcache_context *ctx, mapca
136136
}
137137
}
138138
if(first_error) {
139-
ctx->set_error(ctx,first_error,first_error_message);
139+
ctx->set_error(ctx,first_error,"%s",first_error_message);
140140
}
141141
}
142142

lib/core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ void mapcache_prefetch_tiles(mapcache_context *ctx, mapcache_tile **tiles, int n
151151
if(GC_HAS_ERROR(thread_tiles[i].ctx)) {
152152
/* transfer error message from child thread to main context */
153153
ctx->set_error(ctx,thread_tiles[i].ctx->get_error(thread_tiles[i].ctx),
154+
"%s",
154155
thread_tiles[i].ctx->get_error_message(thread_tiles[i].ctx));
155156
}
156157
}
@@ -597,7 +598,8 @@ mapcache_http_response *mapcache_core_get_featureinfo(mapcache_context *ctx,
597598
apr_table_set(response->headers,"Content-Type",fi->format);
598599
return response;
599600
} else {
600-
ctx->set_error(ctx,404, "tileset %s does not support feature info requests");
601+
ctx->set_error(ctx,404, "tileset %s does not support feature info requests",
602+
tileset->name);
601603
return NULL;
602604
}
603605
}

lib/service_kml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ void _mapcache_service_kml_parse_request(mapcache_context *ctx, mapcache_service
272272
}
273273
endptr++;
274274
if(strcmp(endptr,"kml")) {
275-
ctx->set_error(ctx,404, "received kml request with invalid extension %s", pathinfo, endptr);
275+
ctx->set_error(ctx,404, "received kml request %s with invalid extension %s", pathinfo, endptr);
276276
return;
277277
}
278278
break;

lib/service_mapguide.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void _mapcache_service_mg_parse_request(mapcache_context *ctx, mapcache_service
217217
*request = (mapcache_request*)req;
218218
return;
219219
} else {
220-
ctx->set_error(ctx,404, "received request with wrong number of arguments", pathinfo);
220+
ctx->set_error(ctx,404, "received request %s with wrong number of arguments", pathinfo);
221221
return;
222222
}
223223
}

lib/service_tms.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ void _mapcache_service_tms_parse_request(mapcache_context *ctx, mapcache_service
461461
*request = (mapcache_request*)req;
462462
return;
463463
} else {
464-
ctx->set_error(ctx,404, "received request with wrong number of arguments", pathinfo);
464+
ctx->set_error(ctx,404, "received request %s with wrong number of arguments", pathinfo);
465465
return;
466466
}
467467
}

lib/service_wms.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ void _mapcache_service_wms_parse_request(mapcache_context *ctx, mapcache_service
958958
* or there was an error parsing the request and no rules to proxy it elsewhere
959959
*/
960960
if(errcode != 200) {
961-
ctx->set_error(ctx,errcode,errmsg);
961+
ctx->set_error(ctx,errcode,"%s",errmsg);
962962
return;
963963
}
964964
#ifdef DEBUG

lib/service_wmts.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ void _mapcache_service_wmts_parse_request(mapcache_context *ctx, mapcache_servic
905905
req->tiles[0] = mapcache_tileset_tile_create(ctx->pool, tileset, grid_link);
906906
if(!req->tiles[0]) {
907907
ctx->set_error(ctx, 500, "failed to allocate tile");
908-
if(kvp) ctx->set_exception(ctx,"NoApplicableCode","");
908+
if(kvp) ctx->set_exception(ctx,"NoApplicableCode","%s","");
909909
return;
910910
}
911911

@@ -953,7 +953,7 @@ void _mapcache_service_wmts_parse_request(mapcache_context *ctx, mapcache_servic
953953

954954
if(!tileset->source || !tileset->source->info_formats) {
955955
ctx->set_error(ctx,400,"tileset %s does not support featureinfo requests", tileset->name);
956-
if(kvp) ctx->set_exception(ctx,"OperationNotSupported","");
956+
if(kvp) ctx->set_exception(ctx,"OperationNotSupported","%s","");
957957
return;
958958
}
959959
req_fi = (mapcache_request_get_feature_info*)apr_pcalloc(

lib/source_fallback.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void _mapcache_source_fallback_render_map(mapcache_context *ctx, mapcache_source
7272
}
7373
}
7474
/* all backends failed, return primary error message */
75-
ctx->set_error(ctx,first_error,first_error_message);
75+
ctx->set_error(ctx,first_error,"%s",first_error_message);
7676
return;
7777
}
7878
}
@@ -106,7 +106,7 @@ void _mapcache_source_fallback_query(mapcache_context *ctx, mapcache_source *pso
106106
}
107107
}
108108
/* all backends failed, return primary error message */
109-
ctx->set_error(ctx,first_error,first_error_message);
109+
ctx->set_error(ctx,first_error,"%s",first_error_message);
110110
return;
111111
}
112112
ctx->set_error(ctx,500,"fallback source does not support queries");

lib/tileset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ void mapcache_tileset_tile_set_get_with_subdimensions(mapcache_context *ctx, map
964964
if(GC_HAS_ERROR(thread_subtiles[i].ctx)) {
965965
/* transfer error message from child thread to main context */
966966
ctx->set_error(ctx,thread_subtiles[i].ctx->get_error(thread_subtiles[i].ctx),
967-
thread_subtiles[i].ctx->get_error_message(thread_subtiles[i].ctx));
967+
"%s",thread_subtiles[i].ctx->get_error_message(thread_subtiles[i].ctx));
968968
}
969969
}
970970
}

0 commit comments

Comments
 (0)