0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef __AA_CONTEXT_H
0012 #define __AA_CONTEXT_H
0013
0014 #include <linux/cred.h>
0015 #include <linux/slab.h>
0016 #include <linux/sched.h>
0017
0018 #include "label.h"
0019 #include "policy_ns.h"
0020 #include "task.h"
0021
0022 static inline struct aa_label *cred_label(const struct cred *cred)
0023 {
0024 struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred;
0025
0026 AA_BUG(!blob);
0027 return *blob;
0028 }
0029
0030 static inline void set_cred_label(const struct cred *cred,
0031 struct aa_label *label)
0032 {
0033 struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred;
0034
0035 AA_BUG(!blob);
0036 *blob = label;
0037 }
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
0048 {
0049 struct aa_label *label = cred_label(cred);
0050
0051 AA_BUG(!label);
0052 return label;
0053 }
0054
0055
0056
0057
0058
0059
0060
0061 static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
0062 {
0063 return aa_get_newest_label(aa_cred_raw_label(cred));
0064 }
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 static inline struct aa_label *__aa_task_raw_label(struct task_struct *task)
0075 {
0076 return aa_cred_raw_label(__task_cred(task));
0077 }
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 static inline struct aa_label *aa_current_raw_label(void)
0088 {
0089 return aa_cred_raw_label(current_cred());
0090 }
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 static inline struct aa_label *aa_get_current_label(void)
0102 {
0103 struct aa_label *l = aa_current_raw_label();
0104
0105 if (label_is_stale(l))
0106 return aa_get_newest_label(l);
0107 return aa_get_label(l);
0108 }
0109
0110 #define __end_current_label_crit_section(X) end_current_label_crit_section(X)
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 static inline void end_current_label_crit_section(struct aa_label *label)
0121 {
0122 if (label != aa_current_raw_label())
0123 aa_put_label(label);
0124 }
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 static inline struct aa_label *__begin_current_label_crit_section(void)
0139 {
0140 struct aa_label *label = aa_current_raw_label();
0141
0142 if (label_is_stale(label))
0143 label = aa_get_newest_label(label);
0144
0145 return label;
0146 }
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160 static inline struct aa_label *begin_current_label_crit_section(void)
0161 {
0162 struct aa_label *label = aa_current_raw_label();
0163
0164 might_sleep();
0165
0166 if (label_is_stale(label)) {
0167 label = aa_get_newest_label(label);
0168 if (aa_replace_current_label(label) == 0)
0169
0170 aa_put_label(label);
0171 }
0172
0173 return label;
0174 }
0175
0176 static inline struct aa_ns *aa_get_current_ns(void)
0177 {
0178 struct aa_label *label;
0179 struct aa_ns *ns;
0180
0181 label = __begin_current_label_crit_section();
0182 ns = aa_get_ns(labels_ns(label));
0183 __end_current_label_crit_section(label);
0184
0185 return ns;
0186 }
0187
0188 #endif