diff options
author | Robert Haas <rhaas@postgresql.org> | 2022-08-22 11:35:17 -0400 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2022-08-22 11:35:17 -0400 |
commit | ce6b672e4455820a0348214be0da1a024c3f619f (patch) | |
tree | 97d6a9dd5d89d4b3e7d4c1b4a0866f78e2ec2f11 /src/backend/utils/adt/acl.c | |
parent | 36f729e2bc3539e777cda698dc441b8ccec42142 (diff) | |
download | postgresql-ce6b672e4455820a0348214be0da1a024c3f619f.tar.gz postgresql-ce6b672e4455820a0348214be0da1a024c3f619f.zip |
Make role grant system more consistent with other privileges.
Previously, membership of role A in role B could be recorded in the
catalog tables only once. This meant that a new grant of role A to
role B would overwrite the previous grant. For other object types, a
new grant of permission on an object - in this case role A - exists
along side the existing grant provided that the grantor is different.
Either grant can be revoked independently of the other, and
permissions remain so long as at least one grant remains. Make role
grants work similarly.
Previously, when granting membership in a role, the superuser could
specify any role whatsoever as the grantor, but for other object types,
the grantor of record must be either the owner of the object, or a
role that currently has privileges to perform a similar GRANT.
Implement the same scheme for role grants, treating the bootstrap
superuser as the role owner since roles do not have owners. This means
that attempting to revoke a grant, or admin option on a grant, can now
fail if there are dependent privileges, and that CASCADE can be used
to revoke these. It also means that you can't grant ADMIN OPTION on
a role back to a user who granted it directly or indirectly to you,
similar to how you can't give WITH GRANT OPTION on a privilege back
to a role which granted it directly or indirectly to you.
Previously, only the superuser could specify GRANTED BY with a user
other than the current user. Relax that rule to allow the grantor
to be any role whose privileges the current user posseses. This
doesn't improve compatibility with what we do for other object types,
where support for GRANTED BY is entirely vestigial, but it makes this
feature more usable and seems to make sense to change at the same time
we're changing related behaviors.
Along the way, fix "ALTER GROUP group_name ADD USER user_name" to
require the same privileges as "GRANT group_name TO user_name".
Previously, CREATEROLE privileges were sufficient for either, but
only the former form was permissible with ADMIN OPTION on the role.
Now, either CREATEROLE or ADMIN OPTION on the role suffices for
either spelling.
Patch by me, reviewed by Stephen Frost.
Discussion: http://postgr.es/m/CA+TgmoaFr-RZeQ+WoQ5nKPv97oT9+aDgK_a5+qWHSgbDsMp1Vg@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt/acl.c')
-rw-r--r-- | src/backend/utils/adt/acl.c | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c index 6fa58dd8eb0..3e045da31fc 100644 --- a/src/backend/utils/adt/acl.c +++ b/src/backend/utils/adt/acl.c @@ -4791,9 +4791,7 @@ has_rolinherit(Oid roleid) * Get a list of roles that the specified roleid is a member of * * Type ROLERECURSE_PRIVS recurses only through roles that have rolinherit - * set, while ROLERECURSE_MEMBERS recurses through all roles. This sets - * *is_admin==true if and only if role "roleid" has an ADMIN OPTION membership - * in role "admin_of". + * set, while ROLERECURSE_MEMBERS recurses through all roles. * * Since indirect membership testing is relatively expensive, we cache * a list of memberships. Hence, the result is only guaranteed good until @@ -4801,10 +4799,15 @@ has_rolinherit(Oid roleid) * * For the benefit of select_best_grantor, the result is defined to be * in breadth-first order, ie, closer relationships earlier. + * + * If admin_of is not InvalidOid, this function sets *admin_role, either + * to the OID of the first role in the result list that directly possesses + * ADMIN OPTION on the role corresponding to admin_of, or to InvalidOid if + * there is no such role. */ static List * roles_is_member_of(Oid roleid, enum RoleRecurseType type, - Oid admin_of, bool *is_admin) + Oid admin_of, Oid *admin_role) { Oid dba; List *roles_list; @@ -4812,7 +4815,9 @@ roles_is_member_of(Oid roleid, enum RoleRecurseType type, List *new_cached_roles; MemoryContext oldctx; - Assert(OidIsValid(admin_of) == PointerIsValid(is_admin)); + Assert(OidIsValid(admin_of) == PointerIsValid(admin_role)); + if (admin_role != NULL) + *admin_role = InvalidOid; /* If cache is valid and ADMIN OPTION not sought, just return the list */ if (cached_role[type] == roleid && !OidIsValid(admin_of) && @@ -4873,8 +4878,8 @@ roles_is_member_of(Oid roleid, enum RoleRecurseType type, */ if (otherid == admin_of && ((Form_pg_auth_members) GETSTRUCT(tup))->admin_option && - OidIsValid(admin_of)) - *is_admin = true; + OidIsValid(admin_of) && !OidIsValid(*admin_role)) + *admin_role = memberid; /* * Even though there shouldn't be any loops in the membership @@ -5014,7 +5019,7 @@ is_member_of_role_nosuper(Oid member, Oid role) bool is_admin_of_role(Oid member, Oid role) { - bool result = false; + Oid admin_role; if (superuser_arg(member)) return true; @@ -5023,8 +5028,30 @@ is_admin_of_role(Oid member, Oid role) if (member == role) return false; - (void) roles_is_member_of(member, ROLERECURSE_MEMBERS, role, &result); - return result; + (void) roles_is_member_of(member, ROLERECURSE_MEMBERS, role, &admin_role); + return OidIsValid(admin_role); +} + +/* + * Find a role whose privileges "member" inherits which has ADMIN OPTION + * on "role", ignoring super-userness. + * + * There might be more than one such role; prefer one which involves fewer + * hops. That is, if member has ADMIN OPTION, prefer that over all other + * options; if not, prefer a role from which member inherits more directly + * over more indirect inheritance. + */ +Oid +select_best_admin(Oid member, Oid role) +{ + Oid admin_role; + + /* By policy, a role cannot have WITH ADMIN OPTION on itself. */ + if (member == role) + return InvalidOid; + + (void) roles_is_member_of(member, ROLERECURSE_PRIVS, role, &admin_role); + return admin_role; } |