blob: 11e66b5bbc4266c7c9b0f4fa47d13afdf709b47f [file] [log] [blame]
John Johansen0ed3b282010-07-29 14:48:05 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor ipc mediation
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
John Johansenb2d09ae2017-06-09 14:22:14 -07007 * Copyright 2009-2017 Canonical Ltd.
John Johansen0ed3b282010-07-29 14:48:05 -07008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
13 */
14
15#include <linux/gfp.h>
16#include <linux/ptrace.h>
17
18#include "include/audit.h"
19#include "include/capability.h"
20#include "include/context.h"
21#include "include/policy.h"
James Morris33f8bf52011-08-29 10:40:54 +100022#include "include/ipc.h"
John Johansen0ed3b282010-07-29 14:48:05 -070023
John Johansen290f4582017-06-09 14:38:35 -070024/**
25 * audit_ptrace_mask - convert mask to permission string
26 * @buffer: buffer to write string to (NOT NULL)
27 * @mask: permission mask to convert
28 */
29static void audit_ptrace_mask(struct audit_buffer *ab, u32 mask)
30{
31 switch (mask) {
32 case MAY_READ:
33 audit_log_string(ab, "read");
34 break;
35 case MAY_WRITE:
36 audit_log_string(ab, "trace");
37 break;
38 case AA_MAY_BE_READ:
39 audit_log_string(ab, "readby");
40 break;
41 case AA_MAY_BE_TRACED:
42 audit_log_string(ab, "tracedby");
43 break;
44 }
45}
46
John Johansen0ed3b282010-07-29 14:48:05 -070047/* call back to audit ptrace fields */
John Johansen637f6882017-06-09 08:14:28 -070048static void audit_ptrace_cb(struct audit_buffer *ab, void *va)
John Johansen0ed3b282010-07-29 14:48:05 -070049{
50 struct common_audit_data *sa = va;
John Johansenb2d09ae2017-06-09 14:22:14 -070051
John Johansen290f4582017-06-09 14:38:35 -070052 if (aad(sa)->request & AA_PTRACE_PERM_MASK) {
53 audit_log_format(ab, " requested_mask=");
54 audit_ptrace_mask(ab, aad(sa)->request);
55
56 if (aad(sa)->denied & AA_PTRACE_PERM_MASK) {
57 audit_log_format(ab, " denied_mask=");
58 audit_ptrace_mask(ab, aad(sa)->denied);
59 }
60 }
John Johansenef88a7a2017-01-16 00:43:02 -080061 audit_log_format(ab, " peer=");
John Johansen637f6882017-06-09 08:14:28 -070062 aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
63 FLAGS_NONE, GFP_ATOMIC);
John Johansen0ed3b282010-07-29 14:48:05 -070064}
65
John Johansen290f4582017-06-09 14:38:35 -070066/* TODO: conditionals */
67static int profile_ptrace_perm(struct aa_profile *profile,
68 struct aa_profile *peer, u32 request,
69 struct common_audit_data *sa)
70{
71 struct aa_perms perms = { };
72
73 /* need because of peer in cross check */
74 if (profile_unconfined(profile) ||
75 !PROFILE_MEDIATES(profile, AA_CLASS_PTRACE))
76 return 0;
77
78 aad(sa)->peer = &peer->label;
79 aa_profile_match_label(profile, &peer->label, AA_CLASS_PTRACE, request,
80 &perms);
81 aa_apply_modes_to_perms(profile, &perms);
82 return aa_check_perms(profile, &perms, request, sa, audit_ptrace_cb);
83}
84
John Johansenb2d09ae2017-06-09 14:22:14 -070085static int cross_ptrace_perm(struct aa_profile *tracer,
86 struct aa_profile *tracee, u32 request,
87 struct common_audit_data *sa)
John Johansen0ed3b282010-07-29 14:48:05 -070088{
John Johansen290f4582017-06-09 14:38:35 -070089 if (PROFILE_MEDIATES(tracer, AA_CLASS_PTRACE))
90 return xcheck(profile_ptrace_perm(tracer, tracee, request, sa),
91 profile_ptrace_perm(tracee, tracer,
92 request << PTRACE_PERM_SHIFT,
93 sa));
John Johansenb2d09ae2017-06-09 14:22:14 -070094 /* policy uses the old style capability check for ptrace */
95 if (profile_unconfined(tracer) || tracer == tracee)
96 return 0;
John Johansen0ed3b282010-07-29 14:48:05 -070097
John Johansenb2d09ae2017-06-09 14:22:14 -070098 aad(sa)->label = &tracer->label;
99 aad(sa)->peer = &tracee->label;
100 aad(sa)->request = 0;
101 aad(sa)->error = aa_capable(&tracer->label, CAP_SYS_PTRACE, 1);
John Johansenef88a7a2017-01-16 00:43:02 -0800102
John Johansenb2d09ae2017-06-09 14:22:14 -0700103 return aa_audit(AUDIT_APPARMOR_AUTO, tracer, sa, audit_ptrace_cb);
John Johansen0ed3b282010-07-29 14:48:05 -0700104}
105
106/**
107 * aa_may_ptrace - test if tracer task can trace the tracee
John Johansenb2d09ae2017-06-09 14:22:14 -0700108 * @tracer: label of the task doing the tracing (NOT NULL)
109 * @tracee: task label to be traced
110 * @request: permission request
John Johansen0ed3b282010-07-29 14:48:05 -0700111 *
112 * Returns: %0 else error code if permission denied or error
113 */
John Johansenb2d09ae2017-06-09 14:22:14 -0700114int aa_may_ptrace(struct aa_label *tracer, struct aa_label *tracee,
115 u32 request)
John Johansen0ed3b282010-07-29 14:48:05 -0700116{
John Johansenb2d09ae2017-06-09 14:22:14 -0700117 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE);
John Johansen0ed3b282010-07-29 14:48:05 -0700118
John Johansenb2d09ae2017-06-09 14:22:14 -0700119 return xcheck_labels_profiles(tracer, tracee, cross_ptrace_perm,
120 request, &sa);
John Johansen0ed3b282010-07-29 14:48:05 -0700121}
122
John Johansen0ed3b282010-07-29 14:48:05 -0700123