[RFC PATCH 4/4] apparmor: differentiate between subjective and objective task credentials

John Johansen john.johansen at canonical.com
Sun Feb 21 12:57:26 UTC 2021


On 2/19/21 3:29 PM, Paul Moore wrote:
> With the split of the security_task_getsecid() into subjective and
> objective variants it's time to update AppArmor to ensure it is
> using the correct task creds.
> 
> Signed-off-by: Paul Moore <paul at paul-moore.com>

This has a couple problems, that I will work on addressing


> ---
>  security/apparmor/domain.c       |    2 +-
>  security/apparmor/include/cred.h |   19 ++++++++++++++++---
>  security/apparmor/include/task.h |    3 ++-
>  security/apparmor/lsm.c          |   23 +++++++++++++++--------
>  security/apparmor/task.c         |   23 ++++++++++++++++++++---
>  5 files changed, 54 insertions(+), 16 deletions(-)
> 
> diff --git a/security/apparmor/domain.c b/security/apparmor/domain.c
> index f919ebd042fd2..9ed00b8dcdf0c 100644
> --- a/security/apparmor/domain.c
> +++ b/security/apparmor/domain.c
> @@ -67,7 +67,7 @@ static int may_change_ptraced_domain(struct aa_label *to_label,
>  	tracer = ptrace_parent(current);
>  	if (tracer)
>  		/* released below */
> -		tracerl = aa_get_task_label(tracer);
> +		tracerl = aa_get_task_label_subj(tracer);
>  
>  	/* not ptraced */
>  	if (!tracer || unconfined(tracerl))
> diff --git a/security/apparmor/include/cred.h b/security/apparmor/include/cred.h
> index 0b9ae4804ef73..43c21ef5568ab 100644
> --- a/security/apparmor/include/cred.h
> +++ b/security/apparmor/include/cred.h
> @@ -64,14 +64,27 @@ static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
>  }
>  
>  /**
> - * __aa_task_raw_label - retrieve another task's label
> + * __aa_task_raw_label_subj - retrieve another task's subjective label
>   * @task: task to query  (NOT NULL)
>   *
> - * Returns: @task's label without incrementing its ref count
> + * Returns: @task's subjective label without incrementing its ref count
>   *
>   * If @task != current needs to be called in RCU safe critical section
>   */
> -static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
> +static inline struct aa_label *__aa_task_raw_label_subj(struct task_struct *task)
> +{
> +	return aa_cred_raw_label(rcu_dereference(task->cred));
> +}
> +
> +/**
> + * __aa_task_raw_label_obj - retrieve another task's objective label
> + * @task: task to query  (NOT NULL)
> + *
> + * Returns: @task's objective label without incrementing its ref count
> + *
> + * If @task != current needs to be called in RCU safe critical section
> + */
> +static inline struct aa_label *__aa_task_raw_label_obj(struct task_struct *task)
>  {
>  	return aa_cred_raw_label(__task_cred(task));
>  }
> diff --git a/security/apparmor/include/task.h b/security/apparmor/include/task.h
> index f13d12373b25e..27a2961558555 100644
> --- a/security/apparmor/include/task.h
> +++ b/security/apparmor/include/task.h
> @@ -33,7 +33,8 @@ int aa_replace_current_label(struct aa_label *label);
>  int aa_set_current_onexec(struct aa_label *label, bool stack);
>  int aa_set_current_hat(struct aa_label *label, u64 token);
>  int aa_restore_previous_label(u64 cookie);
> -struct aa_label *aa_get_task_label(struct task_struct *task);
> +struct aa_label *aa_get_task_label_subj(struct task_struct *task);
> +struct aa_label *aa_get_task_label_obj(struct task_struct *task);
>  
>  /**
>   * aa_free_task_ctx - free a task_ctx
> diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
> index 15e37b9132679..38430851675b9 100644
> --- a/security/apparmor/lsm.c
> +++ b/security/apparmor/lsm.c
> @@ -119,7 +119,7 @@ static int apparmor_ptrace_access_check(struct task_struct *child,
>  	int error;
>  
>  	tracer = __begin_current_label_crit_section();
> -	tracee = aa_get_task_label(child);
> +	tracee = aa_get_task_label_obj(child);
>  	error = aa_may_ptrace(tracer, tracee,
>  			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
>  						  : AA_PTRACE_TRACE);
> @@ -135,7 +135,7 @@ static int apparmor_ptrace_traceme(struct task_struct *parent)
>  	int error;
>  
>  	tracee = __begin_current_label_crit_section();
> -	tracer = aa_get_task_label(parent);
> +	tracer = aa_get_task_label_subj(parent);
>  	error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
>  	aa_put_label(tracer);
>  	__end_current_label_crit_section(tracee);
> @@ -719,9 +719,16 @@ static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
>  	return;
>  }
>  
> -static void apparmor_task_getsecid(struct task_struct *p, u32 *secid)
> +static void apparmor_task_getsecid_subj(struct task_struct *p, u32 *secid)
>  {
> -	struct aa_label *label = aa_get_task_label(p);
> +	struct aa_label *label = aa_get_task_label_subj(p);
> +	*secid = label->secid;
> +	aa_put_label(label);
> +}
> +
> +static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
> +{
> +	struct aa_label *label = aa_get_task_label_obj(p);
>  	*secid = label->secid;
>  	aa_put_label(label);
>  }
> @@ -750,7 +757,7 @@ static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo
>  		 * Dealing with USB IO specific behavior
>  		 */
>  		cl = aa_get_newest_cred_label(cred);
> -		tl = aa_get_task_label(target);
> +		tl = aa_get_task_label_obj(target);
>  		error = aa_may_signal(cl, tl, sig);
>  		aa_put_label(cl);
>  		aa_put_label(tl);
> @@ -758,7 +765,7 @@ static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo
>  	}
>  
>  	cl = __begin_current_label_crit_section();
> -	tl = aa_get_task_label(target);
> +	tl = aa_get_task_label_obj(target);
>  	error = aa_may_signal(cl, tl, sig);
>  	aa_put_label(tl);
>  	__end_current_label_crit_section(cl);
> @@ -1243,8 +1250,8 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
>  
>  	LSM_HOOK_INIT(task_free, apparmor_task_free),
>  	LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
> -	LSM_HOOK_INIT(task_getsecid_subj, apparmor_task_getsecid),
> -	LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid),
> +	LSM_HOOK_INIT(task_getsecid_subj, apparmor_task_getsecid_subj),
> +	LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
>  	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
>  	LSM_HOOK_INIT(task_kill, apparmor_task_kill),
>  
> diff --git a/security/apparmor/task.c b/security/apparmor/task.c
> index d17130ee6795d..c03c8e3928055 100644
> --- a/security/apparmor/task.c
> +++ b/security/apparmor/task.c
> @@ -16,17 +16,34 @@
>  #include "include/task.h"
>  
>  /**
> - * aa_get_task_label - Get another task's label
> + * aa_get_task_label_subj - Get another task's subjective label
>   * @task: task to query  (NOT NULL)
>   *
>   * Returns: counted reference to @task's label
>   */
> -struct aa_label *aa_get_task_label(struct task_struct *task)
> +struct aa_label *aa_get_task_label_subj(struct task_struct *task)
>  {
>  	struct aa_label *p;
>  
>  	rcu_read_lock();
> -	p = aa_get_newest_label(__aa_task_raw_label(task));
> +	p = aa_get_newest_label(__aa_task_raw_label_subj(task));
> +	rcu_read_unlock();
> +
> +	return p;
> +}
> +
> +/**
> + * aa_get_task_label_obj - Get another task's objective label
> + * @task: task to query  (NOT NULL)
> + *
> + * Returns: counted reference to @task's label
> + */
> +struct aa_label *aa_get_task_label_obj(struct task_struct *task)
> +{
> +	struct aa_label *p;
> +
> +	rcu_read_lock();
> +	p = aa_get_newest_label(__aa_task_raw_label_obj(task));
>  	rcu_read_unlock();
>  
>  	return p;
> 



More information about the Linux-security-module-archive mailing list