Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <sys/types.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <stdio.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include <asm/page.h>
Chris PeBenito 473ea7
#include "selinux_internal.h"
Chris PeBenito 473ea7
#include "policy.h"
Chris PeBenito 473ea7
#include <limits.h>
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int security_compute_av_raw(security_context_t scon,
Chris PeBenito 473ea7
                            security_context_t tcon,
Chris PeBenito 473ea7
                            security_class_t tclass,
Chris PeBenito 473ea7
                            access_vector_t requested,
Chris PeBenito 473ea7
                            struct av_decision *avd)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char path[PATH_MAX];
Chris PeBenito 473ea7
	char *buf;
Chris PeBenito 473ea7
	size_t len;
Chris PeBenito 473ea7
	int fd, ret;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	snprintf(path, sizeof path, "%s/access", selinux_mnt);
Chris PeBenito 473ea7
	fd = open(path, O_RDWR);
Chris PeBenito 473ea7
	if (fd < 0) 
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	len = PAGE_SIZE;
Chris PeBenito 473ea7
	buf = malloc(len);
Chris PeBenito 473ea7
	if (!buf) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	snprintf(buf, len, "%s %s %hu %x", scon, tcon, tclass, requested);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	ret = write(fd, buf, strlen(buf));
Chris PeBenito 473ea7
	if (ret < 0)
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	memset(buf, 0, len);
Chris PeBenito 473ea7
	ret = read(fd, buf, len-1);
Chris PeBenito 473ea7
	if (ret < 0)
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (sscanf(buf, "%x %x %x %x %u", &avd->allowed, 
Chris PeBenito 473ea7
		    &avd->decided, &avd->auditallow, &avd->auditdeny, 
Chris PeBenito 473ea7
		   &avd->seqno) != 5) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	ret = 0;
Chris PeBenito 473ea7
out2:
Chris PeBenito 473ea7
	free(buf);
Chris PeBenito 473ea7
out:
Chris PeBenito 473ea7
	close(fd);
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(security_compute_av_raw)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int security_compute_av(security_context_t scon,
Chris PeBenito 473ea7
                        security_context_t tcon,
Chris PeBenito 473ea7
                        security_class_t tclass,
Chris PeBenito 473ea7
                        access_vector_t requested,
Chris PeBenito 473ea7
                        struct av_decision *avd)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	int ret;
Chris PeBenito 473ea7
	security_context_t rscon = scon;
Chris PeBenito 473ea7
	security_context_t rtcon = tcon;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations) {
Chris PeBenito 473ea7
		if (trans_to_raw_context(scon, &rscon))
Chris PeBenito 473ea7
			return -1;
Chris PeBenito 473ea7
		if (trans_to_raw_context(tcon, &rtcon)) {
Chris PeBenito 473ea7
			freecon(rscon);
Chris PeBenito 473ea7
			return -1;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
 	ret = security_compute_av_raw(rscon, rtcon, tclass, requested, avd);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations) {
Chris PeBenito 473ea7
		freecon(rscon);
Chris PeBenito 473ea7
		freecon(rtcon);
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(security_compute_av)