Commit 41e84562 authored by Mateusz Guzik's avatar Mateusz Guzik Committed by Paul Moore

cred: add get_cred_many and put_cred_many

Some of the frequent consumers of get_cred and put_cred operate on 2
references on the same creds back-to-back.

Switch them to doing the work in one go instead.
Signed-off-by: default avatarMateusz Guzik <mjguzik@gmail.com>
[PM: removed changelog from commit description]
Signed-off-by: default avatarPaul Moore <paul@paul-moore.com>
parent 20a2aa47
...@@ -218,6 +218,20 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred) ...@@ -218,6 +218,20 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred)
cred->cap_inheritable)); cred->cap_inheritable));
} }
/**
* get_new_cred_many - Get references on a new set of credentials
* @cred: The new credentials to reference
* @nr: Number of references to acquire
*
* Get references on the specified set of new credentials. The caller must
* release all acquired references.
*/
static inline struct cred *get_new_cred_many(struct cred *cred, int nr)
{
atomic_add(nr, &cred->usage);
return cred;
}
/** /**
* get_new_cred - Get a reference on a new set of credentials * get_new_cred - Get a reference on a new set of credentials
* @cred: The new credentials to reference * @cred: The new credentials to reference
...@@ -227,16 +241,16 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred) ...@@ -227,16 +241,16 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred)
*/ */
static inline struct cred *get_new_cred(struct cred *cred) static inline struct cred *get_new_cred(struct cred *cred)
{ {
atomic_inc(&cred->usage); return get_new_cred_many(cred, 1);
return cred;
} }
/** /**
* get_cred - Get a reference on a set of credentials * get_cred_many - Get references on a set of credentials
* @cred: The credentials to reference * @cred: The credentials to reference
* @nr: Number of references to acquire
* *
* Get a reference on the specified set of credentials. The caller must * Get references on the specified set of credentials. The caller must release
* release the reference. If %NULL is passed, it is returned with no action. * all acquired reference. If %NULL is passed, it is returned with no action.
* *
* This is used to deal with a committed set of credentials. Although the * This is used to deal with a committed set of credentials. Although the
* pointer is const, this will temporarily discard the const and increment the * pointer is const, this will temporarily discard the const and increment the
...@@ -244,14 +258,28 @@ static inline struct cred *get_new_cred(struct cred *cred) ...@@ -244,14 +258,28 @@ static inline struct cred *get_new_cred(struct cred *cred)
* accidental alteration of a set of credentials that should be considered * accidental alteration of a set of credentials that should be considered
* immutable. * immutable.
*/ */
static inline const struct cred *get_cred(const struct cred *cred) static inline const struct cred *get_cred_many(const struct cred *cred, int nr)
{ {
struct cred *nonconst_cred = (struct cred *) cred; struct cred *nonconst_cred = (struct cred *) cred;
if (!cred) if (!cred)
return cred; return cred;
validate_creds(cred); validate_creds(cred);
nonconst_cred->non_rcu = 0; nonconst_cred->non_rcu = 0;
return get_new_cred(nonconst_cred); return get_new_cred_many(nonconst_cred, nr);
}
/*
* get_cred - Get a reference on a set of credentials
* @cred: The credentials to reference
*
* Get a reference on the specified set of credentials. The caller must
* release the reference. If %NULL is passed, it is returned with no action.
*
* This is used to deal with a committed set of credentials.
*/
static inline const struct cred *get_cred(const struct cred *cred)
{
return get_cred_many(cred, 1);
} }
static inline const struct cred *get_cred_rcu(const struct cred *cred) static inline const struct cred *get_cred_rcu(const struct cred *cred)
...@@ -269,6 +297,7 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred) ...@@ -269,6 +297,7 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred)
/** /**
* put_cred - Release a reference to a set of credentials * put_cred - Release a reference to a set of credentials
* @cred: The credentials to release * @cred: The credentials to release
* @nr: Number of references to release
* *
* Release a reference to a set of credentials, deleting them when the last ref * Release a reference to a set of credentials, deleting them when the last ref
* is released. If %NULL is passed, nothing is done. * is released. If %NULL is passed, nothing is done.
...@@ -277,17 +306,29 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred) ...@@ -277,17 +306,29 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred)
* on task_struct are attached by const pointers to prevent accidental * on task_struct are attached by const pointers to prevent accidental
* alteration of otherwise immutable credential sets. * alteration of otherwise immutable credential sets.
*/ */
static inline void put_cred(const struct cred *_cred) static inline void put_cred_many(const struct cred *_cred, int nr)
{ {
struct cred *cred = (struct cred *) _cred; struct cred *cred = (struct cred *) _cred;
if (cred) { if (cred) {
validate_creds(cred); validate_creds(cred);
if (atomic_dec_and_test(&(cred)->usage)) if (atomic_sub_and_test(nr, &cred->usage))
__put_cred(cred); __put_cred(cred);
} }
} }
/*
* put_cred - Release a reference to a set of credentials
* @cred: The credentials to release
*
* Release a reference to a set of credentials, deleting them when the last ref
* is released. If %NULL is passed, nothing is done.
*/
static inline void put_cred(const struct cred *cred)
{
put_cred_many(cred, 1);
}
/** /**
* current_cred - Access the current task's subjective credentials * current_cred - Access the current task's subjective credentials
* *
......
...@@ -162,23 +162,29 @@ EXPORT_SYMBOL(__put_cred); ...@@ -162,23 +162,29 @@ EXPORT_SYMBOL(__put_cred);
*/ */
void exit_creds(struct task_struct *tsk) void exit_creds(struct task_struct *tsk)
{ {
struct cred *cred; struct cred *real_cred, *cred;
kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred, kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
atomic_read(&tsk->cred->usage), atomic_read(&tsk->cred->usage),
read_cred_subscribers(tsk->cred)); read_cred_subscribers(tsk->cred));
cred = (struct cred *) tsk->real_cred; real_cred = (struct cred *) tsk->real_cred;
tsk->real_cred = NULL; tsk->real_cred = NULL;
validate_creds(cred);
alter_cred_subscribers(cred, -1);
put_cred(cred);
cred = (struct cred *) tsk->cred; cred = (struct cred *) tsk->cred;
tsk->cred = NULL; tsk->cred = NULL;
validate_creds(cred); validate_creds(cred);
alter_cred_subscribers(cred, -1); if (real_cred == cred) {
put_cred(cred); alter_cred_subscribers(cred, -2);
put_cred_many(cred, 2);
} else {
validate_creds(real_cred);
alter_cred_subscribers(real_cred, -1);
put_cred(real_cred);
alter_cred_subscribers(cred, -1);
put_cred(cred);
}
#ifdef CONFIG_KEYS_REQUEST_CACHE #ifdef CONFIG_KEYS_REQUEST_CACHE
key_put(tsk->cached_requested_key); key_put(tsk->cached_requested_key);
...@@ -355,8 +361,7 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags) ...@@ -355,8 +361,7 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
#endif #endif
clone_flags & CLONE_THREAD clone_flags & CLONE_THREAD
) { ) {
p->real_cred = get_cred(p->cred); p->real_cred = get_cred_many(p->cred, 2);
get_cred(p->cred);
alter_cred_subscribers(p->cred, 2); alter_cred_subscribers(p->cred, 2);
kdebug("share_creds(%p{%d,%d})", kdebug("share_creds(%p{%d,%d})",
p->cred, atomic_read(&p->cred->usage), p->cred, atomic_read(&p->cred->usage),
...@@ -520,8 +525,7 @@ int commit_creds(struct cred *new) ...@@ -520,8 +525,7 @@ int commit_creds(struct cred *new)
proc_id_connector(task, PROC_EVENT_GID); proc_id_connector(task, PROC_EVENT_GID);
/* release the old obj and subj refs both */ /* release the old obj and subj refs both */
put_cred(old); put_cred_many(old, 2);
put_cred(old);
return 0; return 0;
} }
EXPORT_SYMBOL(commit_creds); EXPORT_SYMBOL(commit_creds);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment