aboutsummaryrefslogtreecommitdiffstats
path: root/nldev-handler.c
blob: f4f7eb40d07c7ed17f862250513da0335eedf7c1 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>

#include <linux/netlink.h>

#include "log.h"

#define EVENT_TIMEOUT 2000

void
child(char *runpath)
{
	pid_t pid;

	if (!(pid = fork())) {
		dbg("running %s", runpath);
		if (execlp(runpath, basename(runpath), NULL) < 0)
			edie("execvp");
		exit(0);
	}
	if (pid < 0)
		edie("fork");

	waitpid(pid, NULL, 0);
}


void
usage(void)
{
	die("usage: %s [-dku] [-f subsystem] [-r run]\n", argv0);
}

int main(int argc, char *argv[])
{
	struct pollfd fds;
	int r;
	char *runpath = "/bin/mdev";
	char *subsystem = NULL;

	int showudev, showkernel;

	showudev = 1;
	showkernel = 1;

	ARGBEGIN {
	case 'd':
		dodebug = 1;
		break;
	case 'k':
		showudev = 0;
		break;
	case 'u':
		showkernel = 0;
		break;
	case 'r':
		runpath = EARGF(usage());
		break;
	case 'f':
		subsystem = EARGF(usage());
		break;
	default:
		usage();
	} ARGEND;

	dbg("runpath=%s\n", runpath);

	fds.fd = 0; /* stdin */
	fds.events = POLLIN;
	while ((r = poll(&fds, 1, EVENT_TIMEOUT)) > 0) {
		size_t len;
		int i, slen;
		char *key, *value;
		struct iovec iov;
		char cbuf[CMSG_SPACE(sizeof(struct ucred))];
		char buf[16384];
		struct cmsghdr *chdr;
		struct ucred *cred;
		struct msghdr hdr;
		struct sockaddr_nl cnls;

		clearenv();
		setenv("PATH", "/sbin:/bin", 1);

		if (!(fds.revents & POLLIN))
			continue;

		iov.iov_base = &buf;
		iov.iov_len = sizeof(buf);
		memset(&hdr, 0, sizeof(hdr));
		hdr.msg_iov = &iov;
		hdr.msg_iovlen = 1;
		hdr.msg_control = cbuf;
		hdr.msg_controllen = sizeof(cbuf);
		hdr.msg_name = &cnls;
		hdr.msg_namelen = sizeof(cnls);

		len = recvmsg(fds.fd, &hdr, 0);
		if (len < 0) {
			if (errno == EINTR)
				continue;
			edie("recvmsg");
		}
		if (len < 32 || len >= sizeof(buf))
			continue;

		chdr = CMSG_FIRSTHDR(&hdr);
		if (chdr == NULL || chdr->cmsg_type != SCM_CREDENTIALS)
			continue;

		/*
		 * Don't allow anyone but root to send us messages.
		 *
		 * We will allow users to send us messages, when
		 * udev is enabled. Udev is just a toy you should
		 * only use for testing.
		 */
		cred = (struct ucred *)CMSG_DATA(chdr);
		if (cred->uid != 0 && !showudev)
			continue;

		if (!memcmp(buf, "libudev", 8)) {
			/*
			 * Receiving messages from udev is insecure.
			 */
			if (!showudev)
				continue;
		} else {
			if (!showkernel)
				continue;
			/*
			 * Kernel messages shouldn't come from the
			 * userspace.
			 */
			if (cnls.nl_pid > 0)
				continue;
		}

		for (i = 0; i < len; i += slen + 1) {
			key = buf + i;
			value = strchr(key, '=');
			slen = strlen(buf+i);

			if (!slen || value == NULL)
				continue;
			if (subsystem && !strncmp(key, "SUBSYSTEM=", 10)
					&& !strstr(key+10, subsystem)) {
				dbg("subsystem filter '%s' applied.",
						subsystem);
				break;
			}

			value[0] = '\0';
			value++;

			/*
			 * We generally trust the kernel. But there
			 * might be some udev flaw. (It's >20k sloc!)
			 */
			if (strcmp(key, "PATH")) {
				setenv(key, value, 1);
				dbg("%s = \"%s\"", key, value);
			}
		}
		if (getenv("ACTION") != NULL &&
				getenv("DEVPATH") != NULL &&
				getenv("SUBSYSTEM") != NULL &&
				getenv("SEQNUM") != NULL) {
			child(runpath);
		}

		if (fds.revents & POLLHUP) {
			dbg("parent hung up\n");
			return 0;
		}
	}
	if (r == -1)
		edie("poll");

	if (r == 0)
		dbg("exit due to timeout");

	return 0;
}