summaryrefslogtreecommitdiff
path: root/udev/udev_selinux.c
blob: 2e76a7431f34da863c9c67aea72ffd44f2f9bba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2004 Daniel Walsh
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	General Public License for more details.
 * 
 *	You should have received a copy of the GNU General Public License along
 *	with this program; if not, write to the Free Software Foundation, Inc.,
 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <selinux/selinux.h>

#include "udev.h"
#include "udev_selinux.h"

static security_context_t prev_scontext = NULL;

static int is_selinux_running(struct udev *udev)
{
	static int selinux_enabled = -1;

	if (selinux_enabled == -1) 
		selinux_enabled = (is_selinux_enabled() > 0);

	dbg(udev, "selinux=%i\n", selinux_enabled);
	return selinux_enabled;
}

static char *get_media(struct udev *udev, const char *devname, int mode)
{
	FILE *fp;
	char procfile[PATH_MAX];
	char mediabuf[256];
	int size;
	char *media = NULL;

	if (!(mode & S_IFBLK))
		return NULL;

	snprintf(procfile, PATH_MAX, "/proc/ide/%s/media", devname);
	procfile[PATH_MAX-1] = '\0';

	fp = fopen(procfile, "r");
	if (!fp)
		goto out;

	if (fgets(mediabuf, sizeof(mediabuf), fp) == NULL)
		goto close_out;

	size = strlen(mediabuf);
	while (size-- > 0) {
		if (isspace(mediabuf[size])) {
			mediabuf[size] = '\0';
		} else {
			break;
		}
	}

	media = strdup(mediabuf);
	info(udev, "selinux_get_media(%s)='%s'\n", devname, media);

close_out:
	fclose(fp);
out:
	return media;
}

void selinux_setfilecon(struct udev *udev, const char *file, const char *devname, unsigned int mode)
{
	if (is_selinux_running(udev)) {
		security_context_t scontext = NULL;
		char *media;
		int ret = -1;

		if (devname) {
			media = get_media(udev, devname, mode);
			if (media) {
				ret = matchmediacon(media, &scontext);
				free(media);
			}
		}

		if (ret < 0)
			if (matchpathcon(file, mode, &scontext) < 0) {
				err(udev, "matchpathcon(%s) failed\n", file);
				return;
			} 

		if (lsetfilecon(file, scontext) < 0)
			err(udev, "setfilecon %s failed: %s\n", file, strerror(errno));

		freecon(scontext);
	}
}

void selinux_setfscreatecon(struct udev *udev, const char *file, const char *devname, unsigned int mode)
{
	if (is_selinux_running(udev)) {
		security_context_t scontext = NULL;
		char *media;
		int ret = -1;

		if (devname) {
			media = get_media(udev, devname, mode);
			if (media) {
				ret = matchmediacon(media, &scontext);
				free(media);
			}
		}

		if (ret < 0)
			if (matchpathcon(file, mode, &scontext) < 0) {
				err(udev, "matchpathcon(%s) failed\n", file);
				return;
			}

		if (setfscreatecon(scontext) < 0)
			err(udev, "setfscreatecon %s failed: %s\n", file, strerror(errno));

		freecon(scontext);
	}
}

void selinux_resetfscreatecon(struct udev *udev)
{
	if (is_selinux_running(udev)) {
		if (setfscreatecon(prev_scontext) < 0)
			err(udev, "setfscreatecon failed: %s\n", strerror(errno));
	}
}

void selinux_init(struct udev *udev)
{
	/*
	 * record the present security context, for file-creation
	 * restoration creation purposes.
	 */
	if (is_selinux_running(udev)) {
		if (!udev_get_dev_path(udev)[0])
			err(udev, "selinux_init: udev_root not set\n");
		matchpathcon_init_prefix(NULL, udev_get_dev_path(udev));
		if (getfscreatecon(&prev_scontext) < 0) {
			err(udev, "getfscreatecon failed\n");
			prev_scontext = NULL;
		}
	}
}

void selinux_exit(struct udev *udev)
{
	if (is_selinux_running(udev) && prev_scontext) {
		freecon(prev_scontext);
		prev_scontext = NULL;
	}
}