KEYS: reaching the keys quotas correctly
authorYang Xu <xuyang2018.jy@cn.fujitsu.com>
Fri, 28 Feb 2020 04:41:51 +0000 (12:41 +0800)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Sun, 15 Mar 2020 18:59:50 +0000 (20:59 +0200)
Currently, when we add a new user key, the calltrace as below:

add_key()
  key_create_or_update()
    key_alloc()
    __key_instantiate_and_link
      generic_key_instantiate
        key_payload_reserve
          ......

Since commit a08bf91ce28e ("KEYS: allow reaching the keys quotas exactly"),
we can reach max bytes/keys in key_alloc, but we forget to remove this
limit when we reserver space for payload in key_payload_reserve. So we
can only reach max keys but not max bytes when having delta between plen
and type->def_datalen. Remove this limit when instantiating the key, so we
can keep consistent with key_alloc.

Also, fix the similar problem in keyctl_chown_key().

Fixes: 0b77f5bfb45c ("keys: make the keyring quotas controllable through /proc/sys")
Fixes: a08bf91ce28e ("KEYS: allow reaching the keys quotas exactly")
Cc: stable@vger.kernel.org # 5.0.x
Cc: Eric Biggers <ebiggers@google.com>
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Reviewed-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
security/keys/key.c
security/keys/keyctl.c

index 718bf72174203779177d453183845f1e537cc689..e959b3c96b4870a808899ac7e3b1a15c0d4cd325 100644 (file)
@@ -382,7 +382,7 @@ int key_payload_reserve(struct key *key, size_t datalen)
                spin_lock(&key->user->lock);
 
                if (delta > 0 &&
-                   (key->user->qnbytes + delta >= maxbytes ||
+                   (key->user->qnbytes + delta > maxbytes ||
                     key->user->qnbytes + delta < key->user->qnbytes)) {
                        ret = -EDQUOT;
                }
index 9b898c9695583da389c4129544081c334f679e26..d1a3dea58deeebe0ef86341c4c3ba4b862894536 100644 (file)
@@ -937,8 +937,8 @@ long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
                                key_quota_root_maxbytes : key_quota_maxbytes;
 
                        spin_lock(&newowner->lock);
-                       if (newowner->qnkeys + 1 >= maxkeys ||
-                           newowner->qnbytes + key->quotalen >= maxbytes ||
+                       if (newowner->qnkeys + 1 > maxkeys ||
+                           newowner->qnbytes + key->quotalen > maxbytes ||
                            newowner->qnbytes + key->quotalen <
                            newowner->qnbytes)
                                goto quota_overrun;