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_relabel_raw(security_context_t scon,
Chris PeBenito 473ea7
                                 security_context_t tcon,
Chris PeBenito 473ea7
                                 security_class_t tclass,
Chris PeBenito 473ea7
                                 security_context_t *newcon)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char path[PATH_MAX];
Chris PeBenito 473ea7
	char *buf;
Chris PeBenito 473ea7
	size_t size;
Chris PeBenito 473ea7
	int fd, ret;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	snprintf(path, sizeof path, "%s/relabel", 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
	size = PAGE_SIZE;
Chris PeBenito 473ea7
	buf = malloc(size);
Chris PeBenito 473ea7
	if (!buf) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
	snprintf(buf, size, "%s %s %hu", scon, tcon, tclass);
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, size);
Chris PeBenito 473ea7
	ret = read(fd, buf, size-1);
Chris PeBenito 473ea7
	if (ret < 0)
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	*newcon = strdup(buf);
Chris PeBenito 473ea7
	if (!*newcon) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out2;
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_relabel_raw)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int security_compute_relabel(security_context_t scon,
Chris PeBenito 473ea7
                             security_context_t tcon,
Chris PeBenito 473ea7
                             security_class_t tclass,
Chris PeBenito 473ea7
                             security_context_t *newcon)
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
	security_context_t rnewcon;
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_relabel_raw(rscon, rtcon, tclass, &rnewcon);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations) {
Chris PeBenito 473ea7
		freecon(rscon);
Chris PeBenito 473ea7
		freecon(rtcon);
Chris PeBenito 473ea7
		if (!ret) {
Chris PeBenito 473ea7
			if (raw_to_trans_context(rnewcon, newcon)) {
Chris PeBenito 473ea7
				*newcon = NULL;
Chris PeBenito 473ea7
				ret = -1;
Chris PeBenito 473ea7
			}
Chris PeBenito 473ea7
			freecon(rnewcon);
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	} else if (!ret)
Chris PeBenito 473ea7
		*newcon = rnewcon;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}