[SERVER-1275] DBClientCursor may call strlen on uninitialized bytes Created: 21/Jun/10  Updated: 12/Jul/16  Resolved: 21/Jun/10

Status: Closed
Project: Core Server
Component/s: Internal Client
Affects Version/s: 1.5.3
Fix Version/s: 1.5.4

Type: Bug Priority: Major - P3
Reporter: Andrew Morrow (Inactive) Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

See http://github.com/mongodb/mongo/blob/master/client/dbclientcursor.cpp#L125

If qr->nReturned is zero, then qr->data() returns a pointer to uninitialized bytes, which is then passed to DBConnector::checkResponse at the above line. But the signature of DBConnector::checkResponse requires that this call convert its first argument to a temporary std::string: that requires a call to strlen on the provided pointer. In the case under discussion, that leads to a call to strlen on an uninitialized buffer.

Either line 125 should be changed to correctly handle the case where nReturned is zero, and not attempt to construct a temporary std::string in that case, or DBConnector::checkResponse should be changed to take a const char* first argument, and then correctly handle the case where its second argument is zero.

This shows up in valgrind like this:

==15773== Conditional jump or move depends on uninitialised value(s)
==15773== at 0x4C2AA39: __GI_strlen (mc_replace_strmem.c:284)
==15773== by 0x8978A3F: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==15773== by 0x9F834ED: mongo::DBClientCursor::dataReceived() (dbclientcursor.cpp:125)
==15773== by 0x9F82C8E: mongo::DBClientCursor::init() (dbclientcursor.cpp:54)
==15773== by 0x9F78057: mongo::DBClientBase::query(std::string const&, mongo::Query, int, int, mongo::BSONObj const*, int, int) (dbclient.cpp:530)
==15773== by 0x9F7D181: mongo::DBClientConnection::query(std::string const&, mongo::Query, int, int, mongo::BSONObj const*, int, int) (dbclient.h:704)
==15773== by 0x9F7728D: mongo::DBClientInterface::findOne(std::string const&, mongo::Query, mongo::BSONObj const*, int) (dbclient.cpp:450)
...
==15773== Uninitialised value was created by a heap allocation
==15773== at 0x4C2940D: malloc (vg_replace_malloc.c:236)
==15773== by 0x9F3C03C: mongo::ourmalloc(unsigned long) (allocator.h:23)
==15773== by 0x9F57642: mongo::MessagingPort::recv(mongo::Message&) (message.cpp:391)
==15773== by 0x9F57863: mongo::MessagingPort::call(mongo::Message&, mongo::Message&) (message.cpp:426)
==15773== by 0x9F7A2AC: mongo::DBClientConnection::call(mongo::Message&, mongo::Message&, bool) (dbclient.cpp:789)
==15773== by 0x9F82C4B: mongo::DBClientCursor::init() (dbclientcursor.cpp:50)
==15773== by 0x9F78057: mongo::DBClientBase::query(std::string const&, mongo::Query, int, int, mongo::BSONObj const*, int, int) (dbclient.cpp:530)
==15773== by 0x9F7D181: mongo::DBClientConnection::query(std::string const&, mongo::Query, int, int, mongo::BSONObj const*, int, int) (dbclient.h:704)
==15773== by 0x9F7728D: mongo::DBClientInterface::findOne(std::string const&, mongo::Query, mongo::BSONObj const*, int) (dbclient.cpp:450)
...



 Comments   
Comment by Andrew Morrow (Inactive) [ 21/Jun/10 ]

Thanks Eliot -

I noticed that in the checkin above that you fixed the next issue I was about to report: that checkResponse was a shadowed virtual function. This actually breaks the codebase where we are trying to integrate the mongo C++ client, which builds with the GCC flag -Woverloaded-virtual to catch exactly this sort of issue. Unfortunately, we can't currently compile the mongo headers because of these shadowed virtuals, and we are reluctant to dial down our warnings.

Here are some other shadowed functions exposed in the client headers:

~/mongo.temp.install/include/mongo/client/dbclient.h:293: error: 'virtual bool mongo::DBClientWithCommands::auth(const std::string&, const std::string&, const std::string&, std::string&, bool)' was hidden
~/mongo.temp.install/include/mongo/client/dbclient.h:785: error: by 'bool mongo::DBClientPaired::auth(const std::string&, const std::string&, const std::string&, std::string&)'
In file included from ~/mongo.temp.install/include/mongo/client/../db/db.h:22,
from ~/mongo.temp.install/include/mongo/client/../db/curop.h:24,
from ~/mongo.temp.install/include/mongo/client/../db/instance.h:23,
from ~/mongo.temp.install/include/mongo/client/../db/dbmessage.h:244,
from ~/mongo.temp.install/include/mongo/client/parallel.h:26,
from ./header_check.cc:22:
~/mongo.temp.install/include/mongo/client/dbclient.h: At global scope:
~/mongo.temp.install/include/mongo/client/dbclient.h:593: error: 'virtual std::auto_ptr<mongo::DBClientCursor> mongo::DBClientBase::query(const std::string&, mongo::Query, int, int, const mongo::BSONObj*, int, int)' was hidden
~/mongo.temp.install/include/mongo/client/../db/instance.h:114: error: by 'virtual std::auto_ptr<mongo::DBClientCursor> mongo::DBDirectClient::query(const std::string&, mongo::Query, int, int, const mongo::BSONObj*, int)'

These sorts of overloads are frequently buggy: maybe you should add -Woverloaded-virtual to your warning flags as well?

Comment by auto [ 21/Jun/10 ]

Author:

{'login': 'erh', 'name': 'Eliot Horowitz', 'email': 'eliot@10gen.com'}

Message: fix bad result handling SERVER-1275
http://github.com/mongodb/mongo/commit/38f4332418be56f559de8cb71734490b1149ee03

Generated at Thu Feb 08 02:56:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.