namspace: format NW_HPATH debug output safely
All checks were successful
Source release / source-package (push) Successful in 48s

Stop logging NW_HPATH path component buffers with %s in MAPDEBUG output.

The namespace path buffer is not a NUL-terminated C string. It contains a list
of length-prefixed components, so printing it directly can over-read into
following packet or process memory and pollute unrelated client/test output.

Add small debug formatters for complete NW_HPATH component lists and individual
components, and use them in build_base/add_hpath_to_nwpath debug messages.

No protocol behavior change.
This commit is contained in:
Mario Fetka
2026-05-29 14:41:19 +02:00
parent e0fcd4809e
commit 519ee500a0

View File

@@ -508,6 +508,77 @@ static void rmdir_from_structures(char *unname, DIR_BASE_ENTRY *dbe)
}
static char *debug_hpath_components(NW_HPATH *nwp, uint8 *pp_pathes)
/*
* Format NW_HPATH path components for debug output.
*
* The on-wire path buffer is not a NUL-terminated C string; it is a list of
* length-prefixed components. Logging it with %s can over-read into following
* packet memory and pollute unrelated client/test output files.
*/
{
char buf[768];
int pos = 0;
int k;
if (!nwp || !pp_pathes || !nwp->components)
return(gettmpstr("", 0, 0));
for (k = 0; k < nwp->components && pos < (int)sizeof(buf) - 1; k++) {
int len = (int)*(pp_pathes++);
int i;
if (k && pos < (int)sizeof(buf) - 1)
buf[pos++] = '/';
if (!len) {
if (pos < (int)sizeof(buf) - 1) buf[pos++] = '.';
if (pos < (int)sizeof(buf) - 1) buf[pos++] = '.';
continue;
}
for (i = 0; i < len && pos < (int)sizeof(buf) - 1; i++) {
unsigned char c = pp_pathes[i];
if (c < 32 || c == 127 || c == '`')
c = '?';
buf[pos++] = (char)c;
}
pp_pathes += len;
}
if (k < nwp->components && pos < (int)sizeof(buf) - 1) {
buf[pos++] = '/';
if (pos < (int)sizeof(buf) - 1) buf[pos++] = '.';
if (pos < (int)sizeof(buf) - 1) buf[pos++] = '.';
if (pos < (int)sizeof(buf) - 1) buf[pos++] = '.';
}
buf[pos] = '\0';
return(gettmpstr(buf, pos, 0));
}
static char *debug_hpath_component(uint8 *path, int len)
/* Format one length-prefixed NW_HPATH component for debug output. */
{
char buf[260];
int i;
int pos = 0;
if (!path || len <= 0)
return(gettmpstr("", 0, 0));
for (i = 0; i < len && pos < (int)sizeof(buf) - 1; i++) {
unsigned char c = path[i];
if (c < 32 || c == 127 || c == '`')
c = '?';
buf[pos++] = (char)c;
}
buf[pos] = '\0';
return(gettmpstr(buf, pos, 0));
}
static int get_comp_pathes_size(NW_HPATH *nwp, uint8 *pp_pathes)
/* returns size of path components in bytes */
{
@@ -545,7 +616,7 @@ static int add_hpath_to_nwpath(N_NW_PATH *nwpath,
pp_pathes+=len;
XDPRINTF((5, 0, "component %2d, len=%3d, path='%s'",
k, len, gettmpstr(p, len, 0) ));
k, len, debug_hpath_component(p, len) ));
if (!len) { /* this means '..' ! */
if (pp > nwpath->path) {