Project

General

Profile

Actions

Bug #81015

open
SZ SZ

mgr: MgrCap::parse retains grammar-parsed grants after profile-evaluation failure

Bug #81015: mgr: MgrCap::parse retains grammar-parsed grants after profile-evaluation failure

Added by Steven Zuraski 2 months ago. Updated 2 months ago.

Status:
Fix Under Review
Priority:
Normal
Category:
ceph-mgr
Target version:
-
Due date:
% Done:

0%

Source:
Backport:
Regression:
No
Severity:
3 - minor
Reviewed:
Affected Versions:
ceph-qa-suite:
Pull request ID:
Tags (freeform):
Merge Commit:
Fixed In:
Released In:
Upkeep Timestamp:

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 Actions #1

  • Pull request ID set to 72346

SZ Updated by Steven Zuraski 2 months ago Actions #2

  • Status changed from New to Fix Under Review
Actions

Also available in: PDF Atom