Skip to content

Commit 5ebcbe3

Browse files
committed
ucounts: Move get_ucounts from cred_alloc_blank to key_change_session_keyring
Setting cred->ucounts in cred_alloc_blank does not make sense. The uid and user_ns are deliberately not set in cred_alloc_blank but instead the setting is delayed until key_change_session_keyring. So move dealing with ucounts into key_change_session_keyring as well. Unfortunately that movement of get_ucounts adds a new failure mode to key_change_session_keyring. I do not see anything stopping the parent process from calling setuid and changing the relevant part of it's cred while keyctl_session_to_parent is running making it fundamentally necessary to call get_ucounts in key_change_session_keyring. Which means that the new failure mode cannot be avoided. A failure of key_change_session_keyring results in a single threaded parent keeping it's existing credentials. Which results in the parent process not being able to access the session keyring and whichever keys are in the new keyring. Further get_ucounts is only expected to fail if the number of bits in the refernece count for the structure is too few. Since the code has no other way to report the failure of get_ucounts and because such failures are not expected to be common add a WARN_ONCE to report this problem to userspace. Between the WARN_ONCE and the parent process not having access to the keys in the new session keyring I expect any failure of get_ucounts will be noticed and reported and we can find another way to handle this condition. (Possibly by just making ucounts->count an atomic_long_t). Cc: [email protected] Fixes: 905ae01 ("Add a reference to ucounts for each cred") Link: https://lkml.kernel.org/r/7k0ias0uf.fsf_-_@disp2133 Tested-by: Yu Zhao <[email protected]> Reviewed-by: Alexey Gladkov <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent 34dc2fd commit 5ebcbe3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

kernel/cred.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,6 @@ struct cred *cred_alloc_blank(void)
225225
#ifdef CONFIG_DEBUG_CREDENTIALS
226226
new->magic = CRED_MAGIC;
227227
#endif
228-
new->ucounts = get_ucounts(&init_ucounts);
229-
230228
if (security_cred_alloc_blank(new, GFP_KERNEL_ACCOUNT) < 0)
231229
goto error;
232230

security/keys/process_keys.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,13 @@ void key_change_session_keyring(struct callback_head *twork)
918918
return;
919919
}
920920

921+
/* If get_ucounts fails more bits are needed in the refcount */
922+
if (unlikely(!get_ucounts(old->ucounts))) {
923+
WARN_ONCE(1, "In %s get_ucounts failed\n", __func__);
924+
put_cred(new);
925+
return;
926+
}
927+
921928
new-> uid = old-> uid;
922929
new-> euid = old-> euid;
923930
new-> suid = old-> suid;
@@ -927,6 +934,7 @@ void key_change_session_keyring(struct callback_head *twork)
927934
new-> sgid = old-> sgid;
928935
new->fsgid = old->fsgid;
929936
new->user = get_uid(old->user);
937+
new->ucounts = old->ucounts;
930938
new->user_ns = get_user_ns(old->user_ns);
931939
new->group_info = get_group_info(old->group_info);
932940

0 commit comments

Comments
 (0)