Skip to content

Commit 6d6f332

Browse files
Tetsuo HandaJames Morris
authored andcommitted
commoncap: don't alloc the credential unless needed in cap_task_prctl
In function cap_task_prctl(), we would allocate a credential unconditionally and then check if we support the requested function. If not we would release this credential with abort_creds() by using RCU method. But on some archs such as powerpc, the sys_prctl is heavily used to get/set the floating point exception mode. So the unnecessary allocating/releasing of credential not only introduce runtime overhead but also do cause OOM due to the RCU implementation. This patch removes abort_creds() from cap_task_prctl() by calling prepare_creds() only when we need to modify it. Reported-by: Kevin Hao <haokexin@gmail.com> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Reviewed-by: Paul Moore <paul@paul-moore.com> Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: James Morris <james.l.morris@oracle.com>
1 parent fd33c43 commit 6d6f332

1 file changed

Lines changed: 30 additions & 42 deletions

File tree

‎security/commoncap.c‎

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,20 @@ int cap_task_setnice(struct task_struct *p, int nice)
822822
* Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
823823
* the current task's bounding set. Returns 0 on success, -ve on error.
824824
*/
825-
static long cap_prctl_drop(struct cred *new, unsigned long cap)
825+
static int cap_prctl_drop(unsigned long cap)
826826
{
827+
struct cred *new;
828+
827829
if (!ns_capable(current_user_ns(), CAP_SETPCAP))
828830
return -EPERM;
829831
if (!cap_valid(cap))
830832
return -EINVAL;
831833

834+
new = prepare_creds();
835+
if (!new)
836+
return -ENOMEM;
832837
cap_lower(new->cap_bset, cap);
833-
return 0;
838+
return commit_creds(new);
834839
}
835840

836841
/**
@@ -848,26 +853,17 @@ static long cap_prctl_drop(struct cred *new, unsigned long cap)
848853
int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
849854
unsigned long arg4, unsigned long arg5)
850855
{
856+
const struct cred *old = current_cred();
851857
struct cred *new;
852-
long error = 0;
853-
854-
new = prepare_creds();
855-
if (!new)
856-
return -ENOMEM;
857858

858859
switch (option) {
859860
case PR_CAPBSET_READ:
860-
error = -EINVAL;
861861
if (!cap_valid(arg2))
862-
goto error;
863-
error = !!cap_raised(new->cap_bset, arg2);
864-
goto no_change;
862+
return -EINVAL;
863+
return !!cap_raised(old->cap_bset, arg2);
865864

866865
case PR_CAPBSET_DROP:
867-
error = cap_prctl_drop(new, arg2);
868-
if (error < 0)
869-
goto error;
870-
goto changed;
866+
return cap_prctl_drop(arg2);
871867

872868
/*
873869
* The next four prctl's remain to assist with transitioning a
@@ -889,10 +885,9 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
889885
* capability-based-privilege environment.
890886
*/
891887
case PR_SET_SECUREBITS:
892-
error = -EPERM;
893-
if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
894-
& (new->securebits ^ arg2)) /*[1]*/
895-
|| ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
888+
if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
889+
& (old->securebits ^ arg2)) /*[1]*/
890+
|| ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
896891
|| (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
897892
|| (cap_capable(current_cred(),
898893
current_cred()->user_ns, CAP_SETPCAP,
@@ -906,46 +901,39 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
906901
*/
907902
)
908903
/* cannot change a locked bit */
909-
goto error;
904+
return -EPERM;
905+
906+
new = prepare_creds();
907+
if (!new)
908+
return -ENOMEM;
910909
new->securebits = arg2;
911-
goto changed;
910+
return commit_creds(new);
912911

913912
case PR_GET_SECUREBITS:
914-
error = new->securebits;
915-
goto no_change;
913+
return old->securebits;
916914

917915
case PR_GET_KEEPCAPS:
918-
if (issecure(SECURE_KEEP_CAPS))
919-
error = 1;
920-
goto no_change;
916+
return !!issecure(SECURE_KEEP_CAPS);
921917

922918
case PR_SET_KEEPCAPS:
923-
error = -EINVAL;
924919
if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
925-
goto error;
926-
error = -EPERM;
920+
return -EINVAL;
927921
if (issecure(SECURE_KEEP_CAPS_LOCKED))
928-
goto error;
922+
return -EPERM;
923+
924+
new = prepare_creds();
925+
if (!new)
926+
return -ENOMEM;
929927
if (arg2)
930928
new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
931929
else
932930
new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
933-
goto changed;
931+
return commit_creds(new);
934932

935933
default:
936934
/* No functionality available - continue with default */
937-
error = -ENOSYS;
938-
goto error;
935+
return -ENOSYS;
939936
}
940-
941-
/* Functionality provided */
942-
changed:
943-
return commit_creds(new);
944-
945-
no_change:
946-
error:
947-
abort_creds(new);
948-
return error;
949937
}
950938

951939
/**

0 commit comments

Comments
 (0)