Skip to content

Commit b498496

Browse files
authored
CDRIVER-6134 check SASL username length (#2156)
Check in callback. Reject too-large username.
1 parent 42cea44 commit b498496

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/libmongoc/src/mongoc/mongoc-cyrus.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,18 @@ _mongoc_cyrus_canon_user(sasl_conn_t *conn,
112112
BSON_UNUSED(sasl);
113113
BSON_UNUSED(flags);
114114
BSON_UNUSED(user_realm);
115-
BSON_UNUSED(out_max);
115+
116+
// `inlen` is a string length (excluding trailing NULL).
117+
// Cyrus-SASL passes an `out` buffer of size `out_max + 1`. Assume `out_max` is the max to be safe.
118+
if (inlen + 1 >= out_max) {
119+
MONGOC_ERROR("SASL username too large");
120+
return SASL_BUFOVER;
121+
}
116122

117123
TRACE("Canonicalizing %s (%" PRIu32 ")\n", in, inlen);
118-
strcpy(out, in);
124+
// Use memmove in case buffers overlap. From Cyrus-SASL: "output buffers and the input buffers may be the same"
125+
memmove(out, in, inlen);
126+
out[inlen] = '\0';
119127
*out_len = inlen;
120128
return SASL_OK;
121129
}

0 commit comments

Comments
 (0)