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
83
security_compute_av_raw(const char * scon,const char * tcon,security_class_t tclass,access_vector_t requested,struct av_decision * avd)84 int security_compute_av_raw(const char * scon,
85 const char * tcon,
86 security_class_t tclass,
87 access_vector_t requested,
88 struct av_decision *avd)
89 {
90 struct av_decision lavd;
91 int ret;
92
93 ret = security_compute_av_flags_raw(scon, tcon, tclass,
94 requested, &lavd);
95 if (ret == 0) {
96 avd->allowed = lavd.allowed;
97 avd->decided = lavd.decided;
98 avd->auditallow = lavd.auditallow;
99 avd->auditdeny = lavd.auditdeny;
100 avd->seqno = lavd.seqno;
101 /* NOTE:
102 * We should not return avd->flags via the interface
103 * due to the binary compatibility.
104 */
105 }
106 return ret;
107 }
108
109
security_compute_av_flags(const char * scon,const char * tcon,security_class_t tclass,access_vector_t requested,struct av_decision * avd)110 int security_compute_av_flags(const char * scon,
111 const char * tcon,
112 security_class_t tclass,
113 access_vector_t requested,
114 struct av_decision *avd)
115 {
116 char * rscon;
117 char * rtcon;
118 int ret;
119
120 if (selinux_trans_to_raw_context(scon, &rscon))
121 return -1;
122 if (selinux_trans_to_raw_context(tcon, &rtcon)) {
123 freecon(rscon);
124 return -1;
125 }
126 ret = security_compute_av_flags_raw(rscon, rtcon, tclass,
127 requested, avd);
128
129 freecon(rscon);
130 freecon(rtcon);
131
132 return ret;
133 }
134
135
security_compute_av(const char * scon,const char * tcon,security_class_t tclass,access_vector_t requested,struct av_decision * avd)136 int security_compute_av(const char * scon,
137 const char * tcon,
138 security_class_t tclass,
139 access_vector_t requested, struct av_decision *avd)
140 {
141 struct av_decision lavd;
142 int ret;
143
144 ret = security_compute_av_flags(scon, tcon, tclass,
145 requested, &lavd);
146 if (ret == 0)
147 {
148 avd->allowed = lavd.allowed;
149 avd->decided = lavd.decided;
150 avd->auditallow = lavd.auditallow;
151 avd->auditdeny = lavd.auditdeny;
152 avd->seqno = lavd.seqno;
153 /* NOTE:
154 * We should not return avd->flags via the interface
155 * due to the binary compatibility.
156 */
157 }
158
159 return ret;
160 }
161
162