John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * AppArmor security module |
| 3 | * |
| 4 | * This file contains AppArmor ipc mediation |
| 5 | * |
| 6 | * Copyright (C) 1998-2008 Novell/SUSE |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 7 | * Copyright 2009-2017 Canonical Ltd. |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 8 | * |
| 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 Morris | 33f8bf5 | 2011-08-29 10:40:54 +1000 | [diff] [blame] | 22 | #include "include/ipc.h" |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 23 | |
John Johansen | 290f458 | 2017-06-09 14:38:35 -0700 | [diff] [blame] | 24 | /** |
| 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 | */ |
| 29 | static 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 Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 47 | /* call back to audit ptrace fields */ |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 48 | static void audit_ptrace_cb(struct audit_buffer *ab, void *va) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 49 | { |
| 50 | struct common_audit_data *sa = va; |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 51 | |
John Johansen | 290f458 | 2017-06-09 14:38:35 -0700 | [diff] [blame] | 52 | 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 Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 61 | audit_log_format(ab, " peer="); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 62 | aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer, |
| 63 | FLAGS_NONE, GFP_ATOMIC); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 64 | } |
| 65 | |
John Johansen | 290f458 | 2017-06-09 14:38:35 -0700 | [diff] [blame] | 66 | /* TODO: conditionals */ |
| 67 | static 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 Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 85 | static int cross_ptrace_perm(struct aa_profile *tracer, |
| 86 | struct aa_profile *tracee, u32 request, |
| 87 | struct common_audit_data *sa) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 88 | { |
John Johansen | 290f458 | 2017-06-09 14:38:35 -0700 | [diff] [blame] | 89 | 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 Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 94 | /* policy uses the old style capability check for ptrace */ |
| 95 | if (profile_unconfined(tracer) || tracer == tracee) |
| 96 | return 0; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 97 | |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 98 | 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 Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 102 | |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 103 | return aa_audit(AUDIT_APPARMOR_AUTO, tracer, sa, audit_ptrace_cb); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * aa_may_ptrace - test if tracer task can trace the tracee |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 108 | * @tracer: label of the task doing the tracing (NOT NULL) |
| 109 | * @tracee: task label to be traced |
| 110 | * @request: permission request |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 111 | * |
| 112 | * Returns: %0 else error code if permission denied or error |
| 113 | */ |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 114 | int aa_may_ptrace(struct aa_label *tracer, struct aa_label *tracee, |
| 115 | u32 request) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 116 | { |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 117 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_PTRACE); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 118 | |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 119 | return xcheck_labels_profiles(tracer, tracee, cross_ptrace_perm, |
| 120 | request, &sa); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 121 | } |
| 122 | |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 123 | |