Skip to content

Commit 7fbd220

Browse files
authored
FIX: Memory Leak and Security Risk with Static Token Buffer (#264)
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below (e.g. AB#37452) For external contributors: Insert Github Issue number below (e.g. #149) Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#37606](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/37606) <!-- External contributors: GitHub Issue --> > GitHub Issue: #<ISSUE_NUMBER> ------------------------------------------------------------------- ### Summary <!-- Insert your summary of changes below. Minimum 10 characters required. --> This pull request improves the handling of sensitive data when setting SQL connection attributes in `connection.cpp`. The main change is replacing a static buffer with a stack-allocated buffer to better control memory and securely erase sensitive data after use. Sensitive data handling: * Replaced the static vector of buffers with a stack-allocated `std::string` (`buffer`) to temporarily hold sensitive data when setting SQL attributes. This avoids unnecessary retention of sensitive data in memory. * Added logic to zero out the contents of `buffer` after it is used, ensuring sensitive data does not remain in memory. <!-- ### PR Title Guide > For feature requests FEAT: (short-description) > For non-feature requests like test case updates, config updates , dependency updates etc CHORE: (short-description) > For Fix requests FIX: (short-description) > For doc update requests DOC: (short-description) > For Formatting, indentation, or styling update STYLE: (short-description) > For Refactor, without any feature changes REFACTOR: (short-description) > For release related changes, without any feature changes RELEASE: #<RELEASE_VERSION> (short-description) ### Contribution Guidelines External contributors: - Create a GitHub issue first: https://github.com/microsoft/mssql-python/issues/new - Link the GitHub issue in the "GitHub Issue" section above - Follow the PR title format and provide a meaningful summary mssql-python maintainers: - Create an ADO Work Item following internal processes - Link the ADO Work Item in the "ADO Work Item" section above - Follow the PR title format and provide a meaningful summary -->
1 parent d844cfe commit 7fbd220

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

mssql_python/pybind/connection/connection.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ SQLRETURN Connection::setAttribute(SQLINTEGER attribute, py::object value) {
173173
LOG("Setting SQL attribute");
174174
SQLPOINTER ptr = nullptr;
175175
SQLINTEGER length = 0;
176+
std::string buffer; // to hold sensitive data temporarily
176177

177178
if (py::isinstance<py::int_>(value)) {
178179
int intValue = value.cast<int>();
179180
ptr = reinterpret_cast<SQLPOINTER>(static_cast<uintptr_t>(intValue));
180181
length = SQL_IS_INTEGER;
181182
} else if (py::isinstance<py::bytes>(value) || py::isinstance<py::bytearray>(value)) {
182-
static std::vector<std::string> buffers;
183-
buffers.emplace_back(value.cast<std::string>());
184-
ptr = const_cast<char*>(buffers.back().c_str());
185-
length = static_cast<SQLINTEGER>(buffers.back().size());
183+
buffer = value.cast<std::string>(); // stack buffer
184+
ptr = buffer.data();
185+
length = static_cast<SQLINTEGER>(buffer.size());
186186
} else {
187187
LOG("Unsupported attribute value type");
188188
return SQL_ERROR;
@@ -195,6 +195,11 @@ SQLRETURN Connection::setAttribute(SQLINTEGER attribute, py::object value) {
195195
else {
196196
LOG("Set attribute successfully");
197197
}
198+
199+
// Zero out sensitive data if used
200+
if (!buffer.empty()) {
201+
std::fill(buffer.begin(), buffer.end(), static_cast<char>(0));
202+
}
198203
return ret;
199204
}
200205

0 commit comments

Comments
 (0)