1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <limits.h>
9 #include "selinux_internal.h"
10 #include "policy.h"
11 #include "mapping.h"
12
security_compute_av_flags_raw(const char * scon,const char * tcon,security_class_t tclass,access_vector_t requested,struct av_decision * avd)13 int security_compute_av_flags_raw(const char * scon,
14 const char * tcon,
15 security_class_t tclass,
16 access_vector_t requested,
17 struct av_decision *avd)
18 {
19 char path[PATH_MAX];
20 char *buf;
21 size_t len;
22 int fd, ret;
23 security_class_t kclass;
24
25 if (!selinux_mnt) {
26 errno = ENOENT;
27 return -1;
28 }
29
30 snprintf(path, sizeof path, "%s/access", selinux_mnt);
31 fd = open(path, O_RDWR | O_CLOEXEC);
32 if (fd < 0)
33 return -1;
34
35 len = selinux_page_size;
36 buf = malloc(len);
37 if (!buf) {
38 ret = -1;
39 goto out;
40 }
41
42 kclass = unmap_class(tclass);
43 snprintf(buf, len, "%s %s %hu %x", scon, tcon,
44 kclass, unmap_perm(tclass, requested));
45
46 ret = write(fd, buf, strlen(buf));
47 if (ret < 0)
48 goto out2;
49
50 memset(buf, 0, len);
51 ret = read(fd, buf, len - 1);
52 if (ret < 0)
53 goto out2;
54
55 ret = sscanf(buf, "%x %x %x %x %u %x",
56 &avd->allowed, &avd->decided,
57 &avd->auditallow, &avd->auditdeny,
58 &avd->seqno, &avd->flags);
59 if (ret < 5) {
60 ret = -1;
61 goto out2;
62 } else if (ret < 6)
63 avd->flags = 0;
64
65 /*
66 * If the tclass could not be mapped to a kernel class at all, the
67 * kernel will have already set avd according to the
68 * handle_unknown flag and we do not need to do anything further.
69 * Otherwise, we must map the permissions within the returned
70 * avd to the userspace permission values.
71 */
72 if (kclass != 0)
73 map_decision(tclass, avd);
74
75 ret = 0;
76 out2:
77 free(buf);
78 out:
79 close(fd);
80 return ret;
81 }
82
hidden_def(security_compute_av_flags_raw)83 hidden_def(security_compute_av_flags_raw)
84
85 int security_compute_av_raw(const char * scon,
86 const char * tcon,
87 security_class_t tclass,
88 access_vector_t requested,
89 struct av_decision *avd)
90 {
91 struct av_decision lavd;
92 int ret;
93
94 ret = security_compute_av_flags_raw(scon, tcon, tclass,
95 requested, &lavd);
96 if (ret == 0) {
97 avd->allowed = lavd.allowed;
98 avd->decided = lavd.decided;
99 avd->auditallow = lavd.auditallow;
100 avd->auditdeny = lavd.auditdeny;
101 avd->seqno = lavd.seqno;
102 /* NOTE:
103 * We should not return avd->flags via the interface
104 * due to the binary compatibility.
105 */
106 }
107 return ret;
108 }
109
hidden_def(security_compute_av_raw)110 hidden_def(security_compute_av_raw)
111
112 int security_compute_av_flags(const char * scon,
113 const char * tcon,
114 security_class_t tclass,
115 access_vector_t requested,
116 struct av_decision *avd)
117 {
118 char * rscon;
119 char * rtcon;
120 int ret;
121
122 if (selinux_trans_to_raw_context(scon, &rscon))
123 return -1;
124 if (selinux_trans_to_raw_context(tcon, &rtcon)) {
125 freecon(rscon);
126 return -1;
127 }
128 ret = security_compute_av_flags_raw(rscon, rtcon, tclass,
129 requested, avd);
130
131 freecon(rscon);
132 freecon(rtcon);
133
134 return ret;
135 }
136
hidden_def(security_compute_av_flags)137 hidden_def(security_compute_av_flags)
138
139 int security_compute_av(const char * scon,
140 const char * tcon,
141 security_class_t tclass,
142 access_vector_t requested, struct av_decision *avd)
143 {
144 struct av_decision lavd;
145 int ret;
146
147 ret = security_compute_av_flags(scon, tcon, tclass,
148 requested, &lavd);
149 if (ret == 0)
150 {
151 avd->allowed = lavd.allowed;
152 avd->decided = lavd.decided;
153 avd->auditallow = lavd.auditallow;
154 avd->auditdeny = lavd.auditdeny;
155 avd->seqno = lavd.seqno;
156 /* NOTE:
157 * We should not return avd->flags via the interface
158 * due to the binary compatibility.
159 */
160 }
161
162 return ret;
163 }
164
165 hidden_def(security_compute_av)
166