|
Packit Service |
10cefc |
#include <unistd.h>
|
|
Packit Service |
10cefc |
#include <sys/types.h>
|
|
Packit Service |
10cefc |
#include <fcntl.h>
|
|
Packit Service |
10cefc |
#include <stdlib.h>
|
|
Packit Service |
10cefc |
#include <stdio.h>
|
|
Packit Service |
10cefc |
#include <errno.h>
|
|
Packit Service |
10cefc |
#include <string.h>
|
|
Packit Service |
10cefc |
#include <limits.h>
|
|
Packit Service |
10cefc |
#include <ctype.h>
|
|
Packit Service |
10cefc |
#include "selinux_internal.h"
|
|
Packit Service |
10cefc |
#include "policy.h"
|
|
Packit Service |
10cefc |
#include "mapping.h"
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
static int object_name_encode(const char *objname, char *buffer, size_t buflen)
|
|
Packit Service |
10cefc |
{
|
|
Packit Service |
10cefc |
int code;
|
|
Packit Service |
10cefc |
size_t offset = 0;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
if (buflen - offset < 1)
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
buffer[offset++] = ' ';
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
do {
|
|
Packit Service |
10cefc |
code = *objname++;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
if (isalnum(code) || code == '\0' || code == '-' ||
|
|
Packit Service |
10cefc |
code == '.' || code == '_' || code == '~') {
|
|
Packit Service |
10cefc |
if (buflen - offset < 1)
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
buffer[offset++] = code;
|
|
Packit Service |
10cefc |
} else if (code == ' ') {
|
|
Packit Service |
10cefc |
if (buflen - offset < 1)
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
buffer[offset++] = '+';
|
|
Packit Service |
10cefc |
} else {
|
|
Packit Service |
10cefc |
static const char *table = "0123456789ABCDEF";
|
|
Packit Service |
10cefc |
int l = (code & 0x0f);
|
|
Packit Service |
10cefc |
int h = (code & 0xf0) >> 4;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
if (buflen - offset < 3)
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
buffer[offset++] = '%';
|
|
Packit Service |
10cefc |
buffer[offset++] = table[h];
|
|
Packit Service |
10cefc |
buffer[offset++] = table[l];
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
} while (code != '\0');
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
return 0;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
int security_compute_create_name_raw(const char * scon,
|
|
Packit Service |
10cefc |
const char * tcon,
|
|
Packit Service |
10cefc |
security_class_t tclass,
|
|
Packit Service |
10cefc |
const char *objname,
|
|
Packit Service |
10cefc |
char ** newcon)
|
|
Packit Service |
10cefc |
{
|
|
Packit Service |
10cefc |
char path[PATH_MAX];
|
|
Packit Service |
10cefc |
char *buf;
|
|
Packit Service |
10cefc |
size_t size;
|
|
Packit Service |
10cefc |
int fd, ret, len;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
if (!selinux_mnt) {
|
|
Packit Service |
10cefc |
errno = ENOENT;
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
if ((! scon) || (! tcon)) {
|
|
Packit Service |
10cefc |
errno=EINVAL;
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
snprintf(path, sizeof path, "%s/create", selinux_mnt);
|
|
Packit Service |
10cefc |
fd = open(path, O_RDWR | O_CLOEXEC);
|
|
Packit Service |
10cefc |
if (fd < 0)
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
size = selinux_page_size;
|
|
Packit Service |
10cefc |
buf = malloc(size);
|
|
Packit Service |
10cefc |
if (!buf) {
|
|
Packit Service |
10cefc |
ret = -1;
|
|
Packit Service |
10cefc |
goto out;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
len = snprintf(buf, size, "%s %s %hu",
|
|
Packit Service |
10cefc |
scon, tcon, unmap_class(tclass));
|
|
Packit Service |
10cefc |
if (objname &&
|
|
Packit Service |
10cefc |
object_name_encode(objname, buf + len, size - len) < 0) {
|
|
Packit Service |
10cefc |
errno = ENAMETOOLONG;
|
|
Packit Service |
10cefc |
ret = -1;
|
|
Packit Service |
10cefc |
goto out2;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
ret = write(fd, buf, strlen(buf));
|
|
Packit Service |
10cefc |
if (ret < 0)
|
|
Packit Service |
10cefc |
goto out2;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
memset(buf, 0, size);
|
|
Packit Service |
10cefc |
ret = read(fd, buf, size - 1);
|
|
Packit Service |
10cefc |
if (ret < 0)
|
|
Packit Service |
10cefc |
goto out2;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
*newcon = strdup(buf);
|
|
Packit Service |
10cefc |
if (!(*newcon)) {
|
|
Packit Service |
10cefc |
ret = -1;
|
|
Packit Service |
10cefc |
goto out2;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
ret = 0;
|
|
Packit Service |
10cefc |
out2:
|
|
Packit Service |
10cefc |
free(buf);
|
|
Packit Service |
10cefc |
out:
|
|
Packit Service |
10cefc |
close(fd);
|
|
Packit Service |
10cefc |
return ret;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
hidden_def(security_compute_create_name_raw)
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
int security_compute_create_raw(const char * scon,
|
|
Packit Service |
10cefc |
const char * tcon,
|
|
Packit Service |
10cefc |
security_class_t tclass,
|
|
Packit Service |
10cefc |
char ** newcon)
|
|
Packit Service |
10cefc |
{
|
|
Packit Service |
10cefc |
return security_compute_create_name_raw(scon, tcon, tclass,
|
|
Packit Service |
10cefc |
NULL, newcon);
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
hidden_def(security_compute_create_raw)
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
int security_compute_create_name(const char * scon,
|
|
Packit Service |
10cefc |
const char * tcon,
|
|
Packit Service |
10cefc |
security_class_t tclass,
|
|
Packit Service |
10cefc |
const char *objname,
|
|
Packit Service |
10cefc |
char ** newcon)
|
|
Packit Service |
10cefc |
{
|
|
Packit Service |
10cefc |
int ret;
|
|
Packit Service |
10cefc |
char * rscon;
|
|
Packit Service |
10cefc |
char * rtcon;
|
|
Packit Service |
10cefc |
char * rnewcon;
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
if (selinux_trans_to_raw_context(scon, &rscon))
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
if (selinux_trans_to_raw_context(tcon, &rtcon)) {
|
|
Packit Service |
10cefc |
freecon(rscon);
|
|
Packit Service |
10cefc |
return -1;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
ret = security_compute_create_name_raw(rscon, rtcon, tclass,
|
|
Packit Service |
10cefc |
objname, &rnewcon);
|
|
Packit Service |
10cefc |
freecon(rscon);
|
|
Packit Service |
10cefc |
freecon(rtcon);
|
|
Packit Service |
10cefc |
if (!ret) {
|
|
Packit Service |
10cefc |
ret = selinux_raw_to_trans_context(rnewcon, newcon);
|
|
Packit Service |
10cefc |
freecon(rnewcon);
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
return ret;
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
hidden_def(security_compute_create_name)
|
|
Packit Service |
10cefc |
|
|
Packit Service |
10cefc |
int security_compute_create(const char * scon,
|
|
Packit Service |
10cefc |
const char * tcon,
|
|
Packit Service |
10cefc |
security_class_t tclass,
|
|
Packit Service |
10cefc |
char ** newcon)
|
|
Packit Service |
10cefc |
{
|
|
Packit Service |
10cefc |
return security_compute_create_name(scon, tcon, tclass, NULL, newcon);
|
|
Packit Service |
10cefc |
}
|
|
Packit Service |
10cefc |
hidden_def(security_compute_create)
|