Bug #81015
openmgr: MgrCap::parse retains grammar-parsed grants after profile-evaluation failure
0%
Description
MgrCap::parse (src/mgr/MgrCap.cc) has two failure paths, but only one clears the partially-populated grants list. When the Boost.Spirit grammar parse succeeds but a subsequent profile expansion fails (an unrecognized profile, or a profile that rejects an argument key), parse returns false without calling grants.clear(). This violates the invariant the code documents for itself -- "Make sure no grants are kept after parsing failed!" -- leaving grammar-parsed grants live on the MgrCap object after a failed capability parse. Because MgrCap is an authorization object, retaining grants after a parse failure is a security-relevant correctness bug.
Code (src/mgr/MgrCap.cc, HEAD 6d92b5650ea):
bool MgrCap::parse(const std::string& str, std::ostream *err) {
...
bool r = qi::parse(iter, end, exp, *this);
if (r && iter == end) {
text = str;
std::stringstream profile_err;
for (auto& g : grants) {
g.parse_network();
if (!g.profile.empty()) {
g.expand_profile(&profile_err);
}
}
if (!profile_err.str().empty()) {
if (err != nullptr) {
*err << "mgr capability parse failed during profile evaluation: "
<< profile_err.str();
}
return false; // <-- returns WITHOUT grants.clear()
}
return true;
}
// Make sure no grants are kept after parsing failed!
grants.clear(); // <-- only reached on grammar-parse failure
...
return false;
}
Failure scenario¶
Parsing a capability string whose grammar is valid but whose profile is not -- e.g. "profile does-not-exist" or "profile rbd badkey=x" -- leaves the grammar-parsed grant(s) in this->grants even though parse() returns false. A caller that ignores the return value, or reuses the object, sees a populated and unvalidated grant list.
Suggested fix¶
Call grants.clear() before returning false on the profile-evaluation failure path, or restructure so both failure paths share the clear.
Unit test (illustrative)¶
TEST(MgrCap, ProfileEvalFailureClearsGrants) {
MgrCap cap;
// grammar-valid, profile-invalid
ASSERT_FALSE(cap.parse("profile does-not-exist", nullptr));
// no grants must remain after a failed parse
std::ostringstream oss;
oss << cap;
EXPECT_EQ("", oss.str());
}
SZ Updated by Steven Zuraski 2 months ago
- Pull request ID set to 72346
SZ Updated by Steven Zuraski 2 months ago
- Status changed from New to Fix Under Review