• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "selinux_internal.h"
9 #include "policy.h"
10 #include <limits.h>
11 
12 #define SELINUX_INITCON_DIR "/initial_contexts/"
13 
security_get_initial_context_raw(const char * name,char ** con)14 int security_get_initial_context_raw(const char * name, char ** con)
15 {
16 	char path[PATH_MAX];
17 	char *buf;
18 	size_t size;
19 	int fd, ret;
20 
21 	if (!selinux_mnt) {
22 		errno = ENOENT;
23 		return -1;
24 	}
25 
26 	snprintf(path, sizeof path, "%s%s%s",
27 		 selinux_mnt, SELINUX_INITCON_DIR, name);
28 	fd = open(path, O_RDONLY | O_CLOEXEC);
29 	if (fd < 0)
30 		return -1;
31 
32 	size = selinux_page_size;
33 	buf = malloc(size);
34 	if (!buf) {
35 		ret = -1;
36 		goto out;
37 	}
38 	memset(buf, 0, size);
39 	ret = read(fd, buf, size - 1);
40 	if (ret < 0)
41 		goto out2;
42 
43 	*con = strdup(buf);
44 	if (!(*con)) {
45 		ret = -1;
46 		goto out2;
47 	}
48 	ret = 0;
49       out2:
50 	free(buf);
51       out:
52 	close(fd);
53 	return ret;
54 }
55 
hidden_def(security_get_initial_context_raw)56 hidden_def(security_get_initial_context_raw)
57 
58 int security_get_initial_context(const char * name, char ** con)
59 {
60 	int ret;
61 	char * rcon;
62 
63 	ret = security_get_initial_context_raw(name, &rcon);
64 	if (!ret) {
65 		ret = selinux_raw_to_trans_context(rcon, con);
66 		freecon(rcon);
67 	}
68 
69 	return ret;
70 }
71 
72 hidden_def(security_get_initial_context)
73