Re: [PATCH] KEYS: prevent KEYCTL_READ on negative key

From: Eric Biggers
Date: Thu Sep 21 2017 - 19:34:25 EST

On Tue, Sep 19, 2017 at 05:09:02PM +0100, David Howells wrote:
> Eric Biggers <ebiggers3@xxxxxxxxx> wrote:
> > + if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
> > + ret = -ENOKEY;
> > + goto error2;
> > + }
> It might be better to do this check in key_validate(). Also, it should
> perhaps take the error from key->reject_error.
> David

Putting the check in key_validate() would make lookups with KEY_LOOKUP_PARTIAL
stop returning negative keys, which would break keyctl_describe(),
keyctl_chown(), keyctl_setperm(), keyctl_set_timeout(), keyctl_get_security() on
negative keys. I presume those are supposed to work?

Another solution would be to remove the special case from lookup_user_key()
where it can return a negative/revoked/invalidated/expired key if
KEY_LOOKUP_PARTIAL is not specified and the 'perm' mask is 0. The only callers
it would affect are the case in question here which is clearly a bug, and the
root-only exceptions for keyctl_invalidate() and keyctl_clear(). And I suspect
the latter two are unintentional as well. (Is root *supposed* to be able to
invalidate a negative/revoked/invalidated/expired key, or clear a
revoked/invalidated/expired keyring?)