Code Coverage: vinyl-cache/bin/vinyld/mgt/mgt_jail_unix.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Jailing processes the UNIX way, using setuid(2) etc.
30
 */
31
32
#include "config.h"
33
34
#include <fcntl.h>
35
#include <grp.h>
36
#include <pwd.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
#include <sys/stat.h>
42
43
#include "mgt/mgt.h"
44
#include "common/heritage.h"
45
46
static gid_t vju_mgt_gid;
47
static uid_t vju_uid;
48
static gid_t vju_gid;
49
static const char *vju_user;
50
51
static uid_t vju_wrkuid;
52
static gid_t vju_wrkgid;
53
static const char *vju_wrkuser;
54
55
static gid_t vju_cc_gid;
56
static int vju_cc_gid_set;
57
58
#ifndef VINYL_USER
59 23
#define VINYL_USER "vinyl"
60
#endif
61
62
#ifndef VCACHE_USER
63 25
#define VCACHE_USER "vcache"
64
#endif
65
66
#ifndef NGID
67 6
#define NGID 2000
68
#endif
69
70
static int
71
vju_getuid(const char *arg)
72 29
{
73 29
        struct passwd *pw;
74
75 29
        pw = getpwnam(arg);
76 29
        if (pw != NULL) {
77 28
                vju_user = strdup(arg);
78 28
                AN(vju_user);
79 28
                vju_uid = pw->pw_uid;
80 28
                vju_gid = pw->pw_gid;
81 28
        }
82 29
        endpwent();
83 29
        return (pw == NULL ? -1 : 0);
84 29
}
85
86
static int
87
vju_getwrkuid(const char *arg)
88 29
{
89 29
        struct passwd *pw;
90
91 29
        pw = getpwnam(arg);
92 29
        if (pw != NULL) {
93 28
                vju_wrkuser = strdup(arg);
94 28
                AN(vju_wrkuser);
95 28
                vju_wrkuid = pw->pw_uid;
96 28
                vju_wrkgid = pw->pw_gid;
97 28
        }
98 29
        endpwent();
99 29
        return (pw == NULL ? -1 : 0);
100 29
}
101
102
static int
103
vju_getccgid(const char *arg)
104 6
{
105 6
        struct group *gr;
106
107 6
        gr = getgrnam(arg);
108 6
        if (gr != NULL) {
109 5
                vju_cc_gid_set = 1;
110 5
                vju_cc_gid = gr->gr_gid;
111 5
        }
112 6
        endgrent();
113 6
        return (gr == NULL ? -1 : 0);
114 6
}
115
116
/**********************************************************************
117
 */
118
119
// avoid line breaks
120
#define JERR(what, val)                                         \
121 3
        ARGV_ERR("Unix jail: %s " what " not found.\n", val)
122
123
static int v_matchproto_(jail_init_f)
124
vju_init(char **args)
125 1098
{
126 1098
        const char *val;
127
128 1098
        if (args == NULL) {
129
                /* Autoconfig */
130 1088
                if (geteuid() != 0)
131 1066
                        return (1);
132 22
                if (vju_getuid(VINYL_USER))
133 0
                        return (1);
134 22
        } else {
135
136 10
                if (geteuid() != 0)
137 0
                        ARGV_ERR("Unix Jail: Must be root.\n");
138
139 23
                for (;*args != NULL; args++) {
140 17
                        val = keyval(*args, "user=");
141 17
                        if (val != NULL) {
142 6
                                if (vju_getuid(val))
143 6
                                        JERR("user", val);
144 5
                                continue;
145 6
                        }
146 11
                        val = keyval(*args, "workuser=");
147 11
                        if (val != NULL) {
148 4
                                if (vju_getwrkuid(val))
149 4
                                        JERR("user", val);
150 3
                                continue;
151 4
                        }
152 7
                        val = keyval(*args, "ccgroup=");
153 7
                        if (val != NULL) {
154 6
                                if (vju_getccgid(val))
155 6
                                        JERR("group", val);
156 5
                                continue;
157 6
                        }
158 1
                        ARGV_ERR("Unix jail: unknown sub-argument '%s'\n",
159 1
                            *args);
160 1
                }
161
162 6
                if (vju_user == NULL && vju_getuid(VINYL_USER))
163 6
                        JERR("user", VINYL_USER);
164 6
        }
165
166 28
        AN(vju_user);
167
168 28
        vju_mgt_gid = getgid();
169
170 28
        if (vju_wrkuser == NULL && vju_getwrkuid(VCACHE_USER)) {
171 0
                vju_wrkuid = vju_uid;
172 0
                vju_wrkgid = vju_gid;
173 0
        }
174
175 28
        if (vju_wrkuser != NULL && vju_wrkgid != vju_gid)
176 1
                ARGV_ERR("Unix jail: user %s and %s have "
177 28
                    "different login groups\n", vju_user, vju_wrkuser);
178
179
        /* Do an explicit JAIL_MASTER_LOW */
180 27
        AZ(setegid(vju_gid));
181 27
        AZ(seteuid(vju_uid));
182 27
        return (0);
183 27
}
184
185
#undef JERR
186
187
static void v_matchproto_(jail_master_f)
188
vju_master(enum jail_master_e jme)
189 1160
{
190 1160
        ASSERT_JAIL_MASTER(jme);
191 1160
        if (jme == JAIL_MASTER_LOW) {
192 559
                AZ(setegid(vju_gid));
193 559
                AZ(seteuid(vju_uid));
194 601
        } else {
195 601
                AZ(seteuid(0));
196 601
                AZ(setegid(vju_mgt_gid));
197 601
        }
198 1160
}
199
200
static void v_matchproto_(jail_subproc_f)
201
vju_subproc(enum jail_subproc_e jse)
202 97
{
203 97
        int i;
204 97
        gid_t gid_list[NGID];
205
206 97
        ASSERT_JAIL_SUBPROC(jse);
207 97
        AZ(seteuid(0));
208 97
        if (vju_wrkuser != NULL &&
209 97
            (jse == JAIL_SUBPROC_VCLLOAD || jse == JAIL_SUBPROC_WORKER)) {
210 45
                AZ(setgid(vju_wrkgid));
211 45
                AZ(initgroups(vju_wrkuser, vju_wrkgid));
212 52
        } else {
213 52
                AZ(setgid(vju_gid));
214 52
                AZ(initgroups(vju_user, vju_gid));
215 52
        }
216
217 97
        if (jse == JAIL_SUBPROC_CC && vju_cc_gid_set) {
218
                /* Add the optional extra group for the C-compiler access */
219 6
                i = getgroups(NGID, gid_list);
220 6
                assert(i >= 0);
221 6
                assert(i < NGID - 1);
222 6
                gid_list[i++] = vju_cc_gid;
223 6
                AZ(setgroups(i, gid_list));
224 6
        }
225
226 97
        if (vju_wrkuser != NULL &&
227 97
            (jse == JAIL_SUBPROC_VCLLOAD || jse == JAIL_SUBPROC_WORKER)) {
228 45
                AZ(setuid(vju_wrkuid));
229 52
        } else {
230 52
                AZ(setuid(vju_uid));
231 52
        }
232 97
}
233
234
static int v_matchproto_(jail_make_dir_f)
235
vju_make_subdir(const char *dname, const char *what, struct vsb *vsb)
236 72
{
237 72
        int e;
238
239 72
        AN(dname);
240 72
        AN(what);
241 72
        AN(vsb);
242 72
        AZ(seteuid(0));
243
244 72
        if (mkdir(dname, 0755) < 0 && errno != EEXIST) {
245 0
                e = errno;
246 0
                VSB_printf(vsb, "Cannot create %s directory '%s': %s\n",
247 0
                    what, dname, VAS_errtxt(e));
248 0
                return (1);
249 0
        }
250 72
        AZ(chown(dname, vju_uid, vju_gid));
251 72
        AZ(seteuid(vju_uid));
252 72
        return (0);
253 72
}
254
255
static int v_matchproto_(jail_make_dir_f)
256
vju_make_workdir(const char *dname, const char *what, struct vsb *vsb)
257 23
{
258
259 23
        AN(dname);
260 23
        AZ(what);
261 23
        AN(vsb);
262 23
        AZ(seteuid(0));
263
264 23
        if (mkdir(dname, 0755) < 0 && errno != EEXIST) {
265 0
                VSB_printf(vsb, "Cannot create working directory '%s': %s",
266 0
                    dname, VAS_errtxt(errno));
267 0
                return (1);
268 0
        }
269
        //lint -e{570}
270 23
        if (chown(dname, -1, vju_gid)) {
271 0
                MGT_Complain(C_ERR, "Cannot change group of working directory '%s': %s",
272 0
                    dname, VAS_errtxt(errno));
273 0
                return (1);
274 0
        }
275 23
        AZ(seteuid(vju_uid));
276 23
        return (0);
277 23
}
278
279
static void v_matchproto_(jail_fixfd_f)
280
vju_fixfd(int fd, enum jail_fixfd_e what)
281 66
{
282
        /* Called under JAIL_MASTER_FILE */
283
284 66
        switch (what) {
285 1
        case JAIL_FIXFD_FILE:
286 1
                AZ(fchmod(fd, 0600));
287 1
                AZ(fchown(fd, vju_wrkuid, vju_wrkgid));
288 1
                break;
289 23
        case JAIL_FIXFD_VSMMGT:
290 23
                AZ(fchmod(fd, 0750));
291 23
                AZ(fchown(fd, vju_uid, vju_gid));
292 23
                break;
293 23
        case JAIL_FIXFD_VSMWRK:
294 42
        case JAIL_FIXFD_WRKTMP:
295 42
                AZ(fchmod(fd, 0750));
296 42
                AZ(fchown(fd, vju_wrkuid, vju_wrkgid));
297 42
                break;
298 42
        default:
299 0
                WRONG("Ain't Fixin'");
300 66
        }
301 66
}
302
303
const struct jail_tech jail_tech_unix = {
304
        .magic =        JAIL_TECH_MAGIC,
305
        .name =         "unix",
306
        .init =         vju_init,
307
        .master =       vju_master,
308
        .make_subdir =  vju_make_subdir,
309
        .make_workdir = vju_make_workdir,
310
        .fixfd =        vju_fixfd,
311
        .subproc =      vju_subproc,
312
};