John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * AppArmor security module |
| 3 | * |
| 4 | * This file contains AppArmor security identifier (secid) manipulation fns |
| 5 | * |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 6 | * Copyright 2009-2017 Canonical Ltd. |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation, version 2 of the |
| 11 | * License. |
| 12 | * |
| 13 | * |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 14 | * AppArmor allocates a unique secid for every label used. If a label |
| 15 | * is replaced it receives the secid of the label it is replacing. |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 16 | */ |
| 17 | |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 18 | #include <linux/errno.h> |
| 19 | #include <linux/err.h> |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 20 | #include <linux/gfp.h> |
Matthew Wilcox | 99cc45e | 2018-05-22 02:32:59 -0700 | [diff] [blame^] | 21 | #include <linux/idr.h> |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/spinlock.h> |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 24 | |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 25 | #include "include/cred.h" |
| 26 | #include "include/lib.h" |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 27 | #include "include/secid.h" |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 28 | #include "include/label.h" |
| 29 | #include "include/policy_ns.h" |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 30 | |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 31 | /* |
| 32 | * secids - do not pin labels with a refcount. They rely on the label |
| 33 | * properly updating/freeing them |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
Matthew Wilcox | 99cc45e | 2018-05-22 02:32:59 -0700 | [diff] [blame^] | 36 | static DEFINE_IDR(aa_secids); |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(secid_lock); |
| 38 | |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 39 | /* |
| 40 | * TODO: allow policy to reserve a secid range? |
| 41 | * TODO: add secid pinning |
| 42 | * TODO: use secid_update in label replace |
| 43 | */ |
| 44 | |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 45 | /** |
| 46 | * aa_secid_update - update a secid mapping to a new label |
| 47 | * @secid: secid to update |
| 48 | * @label: label the secid will now map to |
| 49 | */ |
| 50 | void aa_secid_update(u32 secid, struct aa_label *label) |
| 51 | { |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 52 | unsigned long flags; |
| 53 | |
| 54 | spin_lock_irqsave(&secid_lock, flags); |
Matthew Wilcox | 99cc45e | 2018-05-22 02:32:59 -0700 | [diff] [blame^] | 55 | idr_replace(&aa_secids, label, secid); |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 56 | spin_unlock_irqrestore(&secid_lock, flags); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * see label for inverse aa_label_to_secid |
| 62 | */ |
| 63 | struct aa_label *aa_secid_to_label(u32 secid) |
| 64 | { |
| 65 | struct aa_label *label; |
| 66 | |
| 67 | rcu_read_lock(); |
Matthew Wilcox | 99cc45e | 2018-05-22 02:32:59 -0700 | [diff] [blame^] | 68 | label = idr_find(&aa_secids, secid); |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 69 | rcu_read_unlock(); |
| 70 | |
| 71 | return label; |
| 72 | } |
| 73 | |
| 74 | int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 75 | { |
| 76 | /* TODO: cache secctx and ref count so we don't have to recreate */ |
| 77 | struct aa_label *label = aa_secid_to_label(secid); |
John Johansen | 52e7128e | 2018-05-04 01:57:47 -0700 | [diff] [blame] | 78 | int len; |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 79 | |
| 80 | AA_BUG(!secdata); |
| 81 | AA_BUG(!seclen); |
| 82 | |
| 83 | if (!label) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | if (secdata) |
John Johansen | 52e7128e | 2018-05-04 01:57:47 -0700 | [diff] [blame] | 87 | len = aa_label_asxprint(secdata, root_ns, label, |
| 88 | FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | |
| 89 | FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT, |
| 90 | GFP_ATOMIC); |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 91 | else |
John Johansen | 52e7128e | 2018-05-04 01:57:47 -0700 | [diff] [blame] | 92 | len = aa_label_snxprint(NULL, 0, root_ns, label, |
| 93 | FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | |
| 94 | FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT); |
| 95 | if (len < 0) |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 96 | return -ENOMEM; |
| 97 | |
John Johansen | 52e7128e | 2018-05-04 01:57:47 -0700 | [diff] [blame] | 98 | *seclen = len; |
| 99 | |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 103 | int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) |
| 104 | { |
| 105 | struct aa_label *label; |
| 106 | |
| 107 | label = aa_label_strn_parse(&root_ns->unconfined->label, secdata, |
| 108 | seclen, GFP_KERNEL, false, false); |
| 109 | if (IS_ERR(label)) |
| 110 | return PTR_ERR(label); |
| 111 | *secid = label->secid; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | void apparmor_release_secctx(char *secdata, u32 seclen) |
| 117 | { |
| 118 | kfree(secdata); |
| 119 | } |
| 120 | |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 121 | /** |
| 122 | * aa_alloc_secid - allocate a new secid for a profile |
| 123 | */ |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 124 | u32 aa_alloc_secid(struct aa_label *label, gfp_t gfp) |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 125 | { |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 126 | unsigned long flags; |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 127 | u32 secid; |
| 128 | |
Matthew Wilcox | 99cc45e | 2018-05-22 02:32:59 -0700 | [diff] [blame^] | 129 | idr_preload(gfp); |
| 130 | spin_lock_irqsave(&secid_lock, flags); |
| 131 | secid = idr_alloc(&aa_secids, label, 0, 0, GFP_ATOMIC); |
| 132 | /* XXX: Can return -ENOMEM */ |
| 133 | spin_unlock_irqrestore(&secid_lock, flags); |
| 134 | idr_preload_end(); |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 135 | |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 136 | return secid; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * aa_free_secid - free a secid |
| 141 | * @secid: secid to free |
| 142 | */ |
| 143 | void aa_free_secid(u32 secid) |
| 144 | { |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 145 | unsigned long flags; |
| 146 | |
| 147 | spin_lock_irqsave(&secid_lock, flags); |
Matthew Wilcox | 99cc45e | 2018-05-22 02:32:59 -0700 | [diff] [blame^] | 148 | idr_remove(&aa_secids, secid); |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 149 | spin_unlock_irqrestore(&secid_lock, flags); |
John Johansen | 121d4a9 | 2017-01-16 00:42:17 -0800 | [diff] [blame] | 150 | } |