File: avc_internal.c

package info (click to toggle)
libselinux 1.32-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,192 kB
  • ctags: 2,205
  • sloc: ansic: 13,005; sh: 195; makefile: 133; python: 103
file content (296 lines) | stat: -rw-r--r-- 6,311 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Callbacks for user-supplied memory allocation, supplemental
 * auditing, and locking routines.
 *
 * Author : Eamon Walsh <ewalsh@epoch.ncsc.mil>
 *
 * Netlink code derived in part from sample code by
 * James Morris <jmorris@redhat.com>.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include "selinux_netlink.h"
#include "avc_internal.h"

#ifndef NETLINK_SELINUX
#define NETLINK_SELINUX 7
#endif

/* callback pointers */
void *(*avc_func_malloc) (size_t) = NULL;
void (*avc_func_free) (void *) = NULL;

void (*avc_func_log) (const char *, ...) = NULL;
void (*avc_func_audit) (void *, security_class_t, char *, size_t) = NULL;

int avc_using_threads = 0;
void *(*avc_func_create_thread) (void (*)(void)) = NULL;
void (*avc_func_stop_thread) (void *) = NULL;

void *(*avc_func_alloc_lock) (void) = NULL;
void (*avc_func_get_lock) (void *) = NULL;
void (*avc_func_release_lock) (void *) = NULL;
void (*avc_func_free_lock) (void *) = NULL;

/* message prefix string and avc enforcing mode */
char avc_prefix[AVC_PREFIX_SIZE] = "uavc";
int avc_enforcing = 1;
int avc_netlink_trouble = 0;

/* netlink socket code */
static int fd;

int avc_netlink_open(int blocking)
{
	int len, rc = 0;
	struct sockaddr_nl addr;

	fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_SELINUX);
	if (fd < 0) {
		rc = fd;
		goto out;
	}

	if (!blocking && fcntl(fd, F_SETFL, O_NONBLOCK)) {
		close(fd);
		rc = -1;
		goto out;
	}

	len = sizeof(addr);

	memset(&addr, 0, len);
	addr.nl_family = AF_NETLINK;
	addr.nl_groups = SELNL_GRP_AVC;

	if (bind(fd, (struct sockaddr *)&addr, len) < 0) {
		close(fd);
		rc = -1;
		goto out;
	}
      out:
	return rc;
}

void avc_netlink_close(void)
{
	close(fd);
}

int avc_netlink_check_nb(void)
{
	int rc;
	struct sockaddr_nl nladdr;
	socklen_t nladdrlen = sizeof nladdr;
	char buf[1024];
	struct nlmsghdr *nlh;

	while (1) {
		rc = recvfrom(fd, buf, sizeof(buf), 0,
			      (struct sockaddr *)&nladdr, &nladdrlen);
		if (rc < 0) {
			if (errno == EINTR)
				continue;
			if (errno != EAGAIN) {
				avc_log("%s:  socket error during read: %d\n",
					avc_prefix, errno);
			} else {
				errno = 0;
				rc = 0;
			}
			goto out;
		}

		if (nladdrlen != sizeof nladdr) {
			avc_log
			    ("%s:  warning: netlink address truncated, len %d?\n",
			     avc_prefix, nladdrlen);
			rc = -1;
			goto out;
		}

		if (nladdr.nl_pid) {
			avc_log
			    ("%s:  warning: received spoofed netlink packet from: %d\n",
			     avc_prefix, nladdr.nl_pid);
			continue;
		}

		if (rc == 0) {
			avc_log("%s:  warning: received EOF on socket\n",
				avc_prefix);
			goto out;
		}

		nlh = (struct nlmsghdr *)buf;

		if (nlh->nlmsg_flags & MSG_TRUNC
		    || nlh->nlmsg_len > (unsigned)rc) {
			avc_log("%s:  warning: incomplete netlink message\n",
				avc_prefix);
			goto out;
		}

		rc = 0;
		switch (nlh->nlmsg_type) {
		case NLMSG_ERROR:{
				struct nlmsgerr *err = NLMSG_DATA(nlh);

				/* Netlink ack */
				if (err->error == 0)
					break;

				errno = -err->error;
				avc_log("%s:  netlink error: %d\n", avc_prefix,
					errno);
				rc = -1;
				goto out;
			}

		case SELNL_MSG_SETENFORCE:{
				struct selnl_msg_setenforce *msg =
				    NLMSG_DATA(nlh);
				avc_log
				    ("%s:  received setenforce notice (enforcing=%d)\n",
				     avc_prefix, msg->val);
				avc_enforcing = msg->val;
				break;
			}

		case SELNL_MSG_POLICYLOAD:{
				struct selnl_msg_policyload *msg =
				    NLMSG_DATA(nlh);
				avc_log
				    ("%s:  received policyload notice (seqno=%d)\n",
				     avc_prefix, msg->seqno);
				rc = avc_ss_reset(msg->seqno);
				if (rc < 0) {
					avc_log
					    ("%s:  cache reset returned %d (errno %d)\n",
					     avc_prefix, rc, errno);
					goto out;
				}
				break;
			}

		default:
			avc_log("%s:  warning: unknown netlink message %d\n",
				avc_prefix, nlh->nlmsg_type);
		}
	}
      out:
	return rc;
}

/* run routine for the netlink listening thread */
void avc_netlink_loop(void)
{
	int ret;
	struct sockaddr_nl nladdr;
	socklen_t nladdrlen = sizeof nladdr;
	char buf[1024];
	struct nlmsghdr *nlh;

	while (1) {
		ret =
		    recvfrom(fd, buf, sizeof(buf), 0,
			     (struct sockaddr *)&nladdr, &nladdrlen);
		if (ret < 0) {
			if (errno == EINTR)
				continue;
			avc_log("%s:  netlink thread: recvfrom: error %d\n",
				avc_prefix, errno);
			goto out;
		}

		if (nladdrlen != sizeof nladdr) {
			avc_log
			    ("%s:  warning: netlink address truncated, len %d?\n",
			     avc_prefix, nladdrlen);
			ret = -1;
			goto out;
		}

		if (nladdr.nl_pid) {
			avc_log
			    ("%s:  warning: received spoofed netlink packet from: %d\n",
			     avc_prefix, nladdr.nl_pid);
			continue;
		}

		if (ret == 0) {
			avc_log("%s:  netlink thread: received EOF on socket\n",
				avc_prefix);
			goto out;
		}

		nlh = (struct nlmsghdr *)buf;

		if (nlh->nlmsg_flags & MSG_TRUNC
		    || nlh->nlmsg_len > (unsigned)ret) {
			avc_log
			    ("%s:  netlink thread: incomplete netlink message\n",
			     avc_prefix);
			goto out;
		}

		switch (nlh->nlmsg_type) {
		case NLMSG_ERROR:{
				struct nlmsgerr *err = NLMSG_DATA(nlh);

				/* Netlink ack */
				if (err->error == 0)
					break;

				avc_log("%s:  netlink thread: msg: error %d\n",
					avc_prefix, -err->error);
				goto out;
			}

		case SELNL_MSG_SETENFORCE:{
				struct selnl_msg_setenforce *msg =
				    NLMSG_DATA(nlh);
				avc_log
				    ("%s:  received setenforce notice (enforcing=%d)\n",
				     avc_prefix, msg->val);
				avc_enforcing = msg->val;
				break;
			}

		case SELNL_MSG_POLICYLOAD:{
				struct selnl_msg_policyload *msg =
				    NLMSG_DATA(nlh);
				avc_log
				    ("%s:  received policyload notice (seqno=%d)\n",
				     avc_prefix, msg->seqno);
				ret = avc_ss_reset(msg->seqno);
				if (ret < 0) {
					avc_log
					    ("%s:  netlink thread: cache reset returned %d (errno %d)\n",
					     avc_prefix, ret, errno);
					goto out;
				}
				break;
			}

		default:
			avc_log
			    ("%s:  netlink thread: warning: unknown msg type %d\n",
			     avc_prefix, nlh->nlmsg_type);
		}
	}
      out:
	close(fd);
	avc_netlink_trouble = 1;
	avc_log("%s:  netlink thread: errors encountered, terminating\n",
		avc_prefix);
}