varnish-cache/bin/varnishd/mgt/mgt_jail_unix.c
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 VARNISH_USER
59
#define VARNISH_USER "varnish"
60
#endif
61
62
#ifndef VCACHE_USER
63
#define VCACHE_USER "vcache"
64
#endif
65
66
#ifndef NGID
67
#define NGID 2000
68
#endif
69
70
static int
71 640
vju_getuid(const char *arg)
72
{
73
        struct passwd *pw;
74
75 640
        pw = getpwnam(arg);
76 640
        if (pw != NULL) {
77 600
                vju_user = strdup(arg);
78 600
                AN(vju_user);
79 600
                vju_uid = pw->pw_uid;
80 600
                vju_gid = pw->pw_gid;
81 600
        }
82 640
        endpwent();
83 640
        return (pw == NULL ? -1 : 0);
84
}
85
86
static int
87 640
vju_getwrkuid(const char *arg)
88
{
89
        struct passwd *pw;
90
91 640
        pw = getpwnam(arg);
92 640
        if (pw != NULL) {
93 600
                vju_wrkuser = strdup(arg);
94 600
                AN(vju_wrkuser);
95 600
                vju_wrkuid = pw->pw_uid;
96 600
                vju_wrkgid = pw->pw_gid;
97 600
        }
98 640
        endpwent();
99 640
        return (pw == NULL ? -1 : 0);
100
}
101
102
static int
103 200
vju_getccgid(const char *arg)
104
{
105
        struct group *gr;
106
107 200
        gr = getgrnam(arg);
108 200
        if (gr != NULL) {
109 160
                vju_cc_gid_set = 1;
110 160
                vju_cc_gid = gr->gr_gid;
111 160
        }
112 200
        endgrent();
113 200
        return (gr == NULL ? -1 : 0);
114
}
115
116
/**********************************************************************
117
 */
118
119
// avoid line breaks
120
#define JERR(what, val)                                         \
121
        ARGV_ERR("Unix jail: %s " what " not found.\n", val)
122
123
static int v_matchproto_(jail_init_f)
124 40160
vju_init(char **args)
125
{
126
        const char *val;
127
128 40160
        if (args == NULL) {
129
                /* Autoconfig */
130 39800
                if (geteuid() != 0)
131 39400
                        return (1);
132 400
                if (vju_getuid(VARNISH_USER))
133 0
                        return (1);
134 400
        } else {
135
136 360
                if (geteuid() != 0)
137 0
                        ARGV_ERR("Unix Jail: Must be root.\n");
138
139 760
                for (;*args != NULL; args++) {
140 560
                        val = keyval(*args, "user=");
141 560
                        if (val != NULL) {
142 200
                                if (vju_getuid(val))
143 40
                                        JERR("user", val);
144 160
                                continue;
145
                        }
146 360
                        val = keyval(*args, "workuser=");
147 360
                        if (val != NULL) {
148 120
                                if (vju_getwrkuid(val))
149 40
                                        JERR("user", val);
150 80
                                continue;
151
                        }
152 240
                        val = keyval(*args, "ccgroup=");
153 240
                        if (val != NULL) {
154 200
                                if (vju_getccgid(val))
155 40
                                        JERR("group", val);
156 160
                                continue;
157
                        }
158 40
                        ARGV_ERR("Unix jail: unknown sub-argument '%s'\n",
159
                            *args);
160 0
                }
161
162 200
                if (vju_user == NULL && vju_getuid(VARNISH_USER))
163 0
                        JERR("user", VARNISH_USER);
164
        }
165
166 600
        AN(vju_user);
167
168 600
        vju_mgt_gid = getgid();
169
170 600
        if (vju_wrkuser == NULL && vju_getwrkuid(VCACHE_USER)) {
171 0
                vju_wrkuid = vju_uid;
172 0
                vju_wrkgid = vju_gid;
173 0
        }
174
175 600
        if (vju_wrkuser != NULL && vju_wrkgid != vju_gid)
176 40
                ARGV_ERR("Unix jail: user %s and %s have "
177
                    "different login groups\n", vju_user, vju_wrkuser);
178
179
        /* Do an explicit JAIL_MASTER_LOW */
180 560
        AZ(setegid(vju_gid));
181 560
        AZ(seteuid(vju_uid));
182 560
        return (0);
183 39960
}
184
185
#undef JERR
186
187
static void v_matchproto_(jail_master_f)
188 28840
vju_master(enum jail_master_e jme)
189
{
190 28840
        ASSERT_JAIL_MASTER(jme);
191 28840
        if (jme == JAIL_MASTER_LOW) {
192 13800
                AZ(setegid(vju_gid));
193 13800
                AZ(seteuid(vju_uid));
194 13800
        } else {
195 15040
                AZ(seteuid(0));
196 15040
                AZ(setegid(vju_mgt_gid));
197
        }
198 28840
}
199
200
static void v_matchproto_(jail_subproc_f)
201 1120
vju_subproc(enum jail_subproc_e jse)
202
{
203
        int i;
204
        gid_t gid_list[NGID];
205
206 1120
        ASSERT_JAIL_SUBPROC(jse);
207 1120
        AZ(seteuid(0));
208 2240
        if (vju_wrkuser != NULL &&
209 1120
            (jse == JAIL_SUBPROC_VCLLOAD || jse == JAIL_SUBPROC_WORKER)) {
210 0
                AZ(setgid(vju_wrkgid));
211 0
                AZ(initgroups(vju_wrkuser, vju_wrkgid));
212 0
        } else {
213 1120
                AZ(setgid(vju_gid));
214 1120
                AZ(initgroups(vju_user, vju_gid));
215
        }
216
217 1120
        if (jse == JAIL_SUBPROC_CC && vju_cc_gid_set) {
218
                /* Add the optional extra group for the C-compiler access */
219 160
                i = getgroups(NGID, gid_list);
220 160
                assert(i >= 0);
221 160
                assert(i < NGID - 1);
222 160
                gid_list[i++] = vju_cc_gid;
223 160
                AZ(setgroups(i, gid_list));
224 160
        }
225
226 2240
        if (vju_wrkuser != NULL &&
227 1120
            (jse == JAIL_SUBPROC_VCLLOAD || jse == JAIL_SUBPROC_WORKER)) {
228 0
                AZ(setuid(vju_wrkuid));
229 0
        } else {
230 1120
                AZ(setuid(vju_uid));
231
        }
232 1120
}
233
234
static int v_matchproto_(jail_make_dir_f)
235 1680
vju_make_subdir(const char *dname, const char *what, struct vsb *vsb)
236
{
237
        int e;
238
239 1680
        AN(dname);
240 1680
        AN(what);
241 1680
        AN(vsb);
242 1680
        AZ(seteuid(0));
243
244 1680
        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
        }
250 1680
        AZ(chown(dname, vju_uid, vju_gid));
251 1680
        AZ(seteuid(vju_uid));
252 1680
        return (0);
253 1680
}
254
255
static int v_matchproto_(jail_make_dir_f)
256 560
vju_make_workdir(const char *dname, const char *what, struct vsb *vsb)
257
{
258
259 560
        AN(dname);
260 560
        AZ(what);
261 560
        AN(vsb);
262 560
        AZ(seteuid(0));
263
264 560
        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
        }
269
        //lint -e{570}
270 560
        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
        }
275 560
        AZ(seteuid(vju_uid));
276 560
        return (0);
277 560
}
278
279
static void v_matchproto_(jail_fixfd_f)
280 1800
vju_fixfd(int fd, enum jail_fixfd_e what)
281
{
282
        /* Called under JAIL_MASTER_FILE */
283
284 1800
        switch (what) {
285
        case JAIL_FIXFD_FILE:
286 0
                AZ(fchmod(fd, 0600));
287 0
                AZ(fchown(fd, vju_wrkuid, vju_wrkgid));
288 0
                break;
289
        case JAIL_FIXFD_VSMMGT:
290 560
                AZ(fchmod(fd, 0750));
291 560
                AZ(fchown(fd, vju_uid, vju_gid));
292 560
                break;
293
        case JAIL_FIXFD_VSMWRK:
294
        case JAIL_FIXFD_WRKTMP:
295 1240
                AZ(fchmod(fd, 0750));
296 1240
                AZ(fchown(fd, vju_wrkuid, vju_wrkgid));
297 1240
                break;
298
        default:
299 0
                WRONG("Ain't Fixin'");
300 0
        }
301 1800
}
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
};