Re: [PATCH] Keys: Remove redundant initialization of cred

From: David Howells
Date: Tue May 11 2021 - 06:50:26 EST


Yang Li <yang.lee@xxxxxxxxxxxxxxxxx> wrote:

> - const struct cred *cred = current_cred();
> + const struct cred *cred;

Good catch, but it's probably the wrong fix.

In that function, there is:

const struct cred *cred = current_cred();
...
cred = get_current_cred();
keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred, ...);
put_cred(cred);
...
sprintf(uid_str, "%d", from_kuid(&init_user_ns, cred->fsuid));
...

So we get the creds again, but with a ref, and then drop after calling
keyring_alloc()... and then access cred again, which is dodgy - but we get
away with it because cred is still pinned by our task_struct.

I think what is actually needed is to remove the get_current_cred() and the
put_cred() calls, in which case, you want this:

Fixes: d84f4f992cbd ("CRED: Inaugurate COW credentials")

David