Skip to content

Commit 034faeb

Browse files
committed
KEYS: Fix keyring quota misaccounting on key replacement and unlink
If a key is displaced from a keyring by a matching one, then four more bytes of quota are allocated to the keyring - despite the fact that the keyring does not change in size. Further, when a key is unlinked from a keyring, the four bytes of quota allocated the link isn't recovered and returned to the user's pool. The first can be tested by repeating: keyctl add big_key a fred @s cat /proc/key-users (Don't put it in a shell loop otherwise the garbage collector won't have time to clear the displaced keys, thus affecting the result). This was causing the kerberos keyring to run out of room fairly quickly. The second can be tested by: cat /proc/key-users a=`keyctl add user a a @s` cat /proc/key-users keyctl unlink $a sleep 1 # Give RCU a chance to delete the key cat /proc/key-users assuming no system activity that otherwise adds/removes keys, the amount of key data allocated should go up (say 40/20000 -> 47/20000) and then return to the original value at the end. Reported-by: Stephen Gallagher <sgallagh@redhat.com> Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 74792b0 commit 034faeb

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

‎security/keys/keyring.c‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,12 +1063,6 @@ int __key_link_begin(struct key *keyring,
10631063
if (index_key->type == &key_type_keyring)
10641064
down_write(&keyring_serialise_link_sem);
10651065

1066-
/* check that we aren't going to overrun the user's quota */
1067-
ret = key_payload_reserve(keyring,
1068-
keyring->datalen + KEYQUOTA_LINK_BYTES);
1069-
if (ret < 0)
1070-
goto error_sem;
1071-
10721066
/* Create an edit script that will insert/replace the key in the
10731067
* keyring tree.
10741068
*/
@@ -1078,17 +1072,25 @@ int __key_link_begin(struct key *keyring,
10781072
NULL);
10791073
if (IS_ERR(edit)) {
10801074
ret = PTR_ERR(edit);
1081-
goto error_quota;
1075+
goto error_sem;
1076+
}
1077+
1078+
/* If we're not replacing a link in-place then we're going to need some
1079+
* extra quota.
1080+
*/
1081+
if (!edit->dead_leaf) {
1082+
ret = key_payload_reserve(keyring,
1083+
keyring->datalen + KEYQUOTA_LINK_BYTES);
1084+
if (ret < 0)
1085+
goto error_cancel;
10821086
}
10831087

10841088
*_edit = edit;
10851089
kleave(" = 0");
10861090
return 0;
10871091

1088-
error_quota:
1089-
/* undo the quota changes */
1090-
key_payload_reserve(keyring,
1091-
keyring->datalen - KEYQUOTA_LINK_BYTES);
1092+
error_cancel:
1093+
assoc_array_cancel_edit(edit);
10921094
error_sem:
10931095
if (index_key->type == &key_type_keyring)
10941096
up_write(&keyring_serialise_link_sem);
@@ -1146,7 +1148,7 @@ void __key_link_end(struct key *keyring,
11461148
if (index_key->type == &key_type_keyring)
11471149
up_write(&keyring_serialise_link_sem);
11481150

1149-
if (edit) {
1151+
if (edit && !edit->dead_leaf) {
11501152
key_payload_reserve(keyring,
11511153
keyring->datalen - KEYQUOTA_LINK_BYTES);
11521154
assoc_array_cancel_edit(edit);
@@ -1243,6 +1245,7 @@ int key_unlink(struct key *keyring, struct key *key)
12431245
goto error;
12441246

12451247
assoc_array_apply_edit(edit);
1248+
key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES);
12461249
ret = 0;
12471250

12481251
error:

0 commit comments

Comments
 (0)