/*1* AppArmor security module2*3* This file contains AppArmor ipc mediation4*5* Copyright (C) 1998-2008 Novell/SUSE6* Copyright 2009-2010 Canonical Ltd.7*8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License as10* published by the Free Software Foundation, version 2 of the11* License.12*/1314#include <linux/gfp.h>15#include <linux/ptrace.h>1617#include "include/audit.h"18#include "include/capability.h"19#include "include/context.h"20#include "include/policy.h"2122/* call back to audit ptrace fields */23static void audit_cb(struct audit_buffer *ab, void *va)24{25struct common_audit_data *sa = va;26audit_log_format(ab, " target=");27audit_log_untrustedstring(ab, sa->aad.target);28}2930/**31* aa_audit_ptrace - do auditing for ptrace32* @profile: profile being enforced (NOT NULL)33* @target: profile being traced (NOT NULL)34* @error: error condition35*36* Returns: %0 or error code37*/38static int aa_audit_ptrace(struct aa_profile *profile,39struct aa_profile *target, int error)40{41struct common_audit_data sa;42COMMON_AUDIT_DATA_INIT(&sa, NONE);43sa.aad.op = OP_PTRACE;44sa.aad.target = target;45sa.aad.error = error;4647return aa_audit(AUDIT_APPARMOR_AUTO, profile, GFP_ATOMIC, &sa,48audit_cb);49}5051/**52* aa_may_ptrace - test if tracer task can trace the tracee53* @tracer_task: task who will do the tracing (NOT NULL)54* @tracer: profile of the task doing the tracing (NOT NULL)55* @tracee: task to be traced56* @mode: whether PTRACE_MODE_READ || PTRACE_MODE_ATTACH57*58* Returns: %0 else error code if permission denied or error59*/60int aa_may_ptrace(struct task_struct *tracer_task, struct aa_profile *tracer,61struct aa_profile *tracee, unsigned int mode)62{63/* TODO: currently only based on capability, not extended ptrace64* rules,65* Test mode for PTRACE_MODE_READ || PTRACE_MODE_ATTACH66*/6768if (unconfined(tracer) || tracer == tracee)69return 0;70/* log this capability request */71return aa_capable(tracer_task, tracer, CAP_SYS_PTRACE, 1);72}7374/**75* aa_ptrace - do ptrace permission check and auditing76* @tracer: task doing the tracing (NOT NULL)77* @tracee: task being traced (NOT NULL)78* @mode: ptrace mode either PTRACE_MODE_READ || PTRACE_MODE_ATTACH79*80* Returns: %0 else error code if permission denied or error81*/82int aa_ptrace(struct task_struct *tracer, struct task_struct *tracee,83unsigned int mode)84{85/*86* tracer can ptrace tracee when87* - tracer is unconfined ||88* - tracer is in complain mode89* - tracer has rules allowing it to trace tracee currently this is:90* - confined by the same profile ||91* - tracer profile has CAP_SYS_PTRACE92*/9394struct aa_profile *tracer_p;95/* cred released below */96const struct cred *cred = get_task_cred(tracer);97int error = 0;98tracer_p = aa_cred_profile(cred);99100if (!unconfined(tracer_p)) {101/* lcred released below */102const struct cred *lcred = get_task_cred(tracee);103struct aa_profile *tracee_p = aa_cred_profile(lcred);104105error = aa_may_ptrace(tracer, tracer_p, tracee_p, mode);106error = aa_audit_ptrace(tracer_p, tracee_p, error);107108put_cred(lcred);109}110put_cred(cred);111112return error;113}114115116