-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprivsep.c
240 lines (201 loc) · 5.97 KB
/
privsep.c
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
/*
* Copyright (c) 2004 Iwata <[email protected]>
* Copyright (c) 2003 Can Erkin Acar
* Copyright (c) 2003 Anil Madhavapeddy <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <paths.h>
#include <signal.h>
#include <string.h>
#include "hddtemp.h"
int priv_fd = -1;
static volatile pid_t child_pid = -1;
volatile sig_atomic_t gotsig_chld = 0;
static void sig_pass_to_chld(int);
static void sig_chld(int);
static int may_read(int, void *, size_t);
static void must_read(int, void *, size_t);
static void must_write(int, void *, size_t);
int
privsep_init(void)
{
int socks[2], cmd;
struct passwd *pw;
/* Create sockets */
if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, socks) == -1)
err(1, "socketpair() failed");
if ((pw = getpwnam(PRIV_USER)) == NULL)
errx(1, "no such user: " PRIV_USER);
endpwent();
child_pid = fork();
if (child_pid < 0)
errx(1, "fork() failed");
if (!child_pid) {
struct stat stb;
gid_t gidset[1];
if (stat(pw->pw_dir, &stb) == -1)
err(1, "stat");
if (stb.st_uid != 0 || (stb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
err(1, "bad privsep dir permissions");
/* chroot, drop privs and return */
if (chroot(pw->pw_dir) != 0) {
fprintf(stderr, "no such directory: %s falling back to " _PATH_VAREMPTY "\n", pw->pw_dir);
if (chroot(_PATH_VAREMPTY) != 0)
err(1, "unable to chroot");
}
if (chdir("/") != 0)
err(1, "unable to chdir");
gidset[0] = pw->pw_gid;
/* drop to _hddtemp */
if (setgroups(1, gidset) == -1)
err(1, "setgroups() failed");
if (setegid(pw->pw_gid) == -1)
err(1, "setegid() failed");
if (setgid(pw->pw_gid) == -1)
err(1, "setgid() failed");
if (seteuid(pw->pw_uid) == -1)
err(1, "seteuid() failed");
if (setuid(pw->pw_uid) == -1)
err(1, "setuid() failed");
close(socks[0]);
priv_fd = socks[1];
return 0;
}
/* Father */
/* Pass ALRM/TERM/HUP through to child, and accept CHLD */
signal(SIGALRM, sig_pass_to_chld);
signal(SIGTERM, sig_pass_to_chld);
signal(SIGHUP, sig_pass_to_chld);
signal(SIGCHLD, sig_chld);
setproctitle("[priv]");
close(socks[1]);
while (!gotsig_chld) {
int len;
char buf[BUFSIZ];
int temp;
if (may_read(socks[0], &cmd, sizeof(int)))
break;
temp = smart_temperature();
if (strcmp(hdd_db->unit, "C") == 0)
temp = ftoc(temp);
snprintf(buf, BUFSIZ - 1, "|%s|%s|%d|C|", hdd_dev, hdd_model, temp);
len = strlen(buf);
must_write(socks[0], &len, sizeof(int));
must_write(socks[0], buf, len);
}
_exit(0);
}
/* If priv parent gets a TERM or HUP, pass it through to child instead */
static void
sig_pass_to_chld(int sig)
{
int oerrno = errno;
if (child_pid != -1)
kill(child_pid, sig);
errno = oerrno;
}
/* if parent gets a SIGCHLD, it will exit */
/* ARGSUSED */
static void
sig_chld(int sig)
{
gotsig_chld = 1;
}
/* Read all data or return 1 for error. */
static int
may_read(int fd, void *buf, size_t n)
{
char *s = buf;
ssize_t res, pos = 0;
while (n > pos) {
res = read(fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
case 0:
return (1);
default:
pos += res;
}
}
return 0;
}
/* Read data with the assertion that it all must come through, or
* else abort the process. Based on atomicio() from openssh. */
static void
must_read(int fd, void *buf, size_t n)
{
char *s = buf;
ssize_t res, pos = 0;
while (n > pos) {
res = read(fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
case 0:
_exit(0);
default:
pos += res;
}
}
}
/* Write data with the assertion that it all has to be written, or
* else abort the process. Based on atomicio() from openssh. */
static void
must_write(int fd, void *buf, size_t n)
{
char *s = buf;
ssize_t res, pos = 0;
while (n > pos) {
res = write(fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
case 0:
_exit(0);
default:
pos += res;
}
}
}
int
priv_get_temperature(char *buf)
{
int cmd = 1;
int recv_len;
char *recv_buf;
/* wakeup */
must_write(priv_fd, &cmd, sizeof(int));
may_read(priv_fd, &recv_len, sizeof(int));
recv_buf = malloc(recv_len);
may_read(priv_fd, recv_buf, recv_len);
memcpy(buf, recv_buf, recv_len);
free(recv_buf);
return recv_len;
}