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 80
vju_getuid(const char *arg)
72
{
73
        struct passwd *pw;
74
75 80
        pw = getpwnam(arg);
76 80
        if (pw != NULL) {
77 75
                vju_user = strdup(arg);
78 75
                AN(vju_user);
79 75
                vju_uid = pw->pw_uid;
80 75
                vju_gid = pw->pw_gid;
81 75
        }
82 80
        endpwent();
83 80
        return (pw == NULL ? -1 : 0);
84
}
85
86
static int
87 80
vju_getwrkuid(const char *arg)
88
{
89
        struct passwd *pw;
90
91 80
        pw = getpwnam(arg);
92 80
        if (pw != NULL) {
93 75
                vju_wrkuser = strdup(arg);
94 75
                AN(vju_wrkuser);
95 75
                vju_wrkuid = pw->pw_uid;
96 75
                vju_wrkgid = pw->pw_gid;
97 75
        }
98 80
        endpwent();
99 80
        return (pw == NULL ? -1 : 0);
100
}
101
102
static int
103 25
vju_getccgid(const char *arg)
104
{
105
        struct group *gr;
106
107 25
        gr = getgrnam(arg);
108 25
        if (gr != NULL) {
109 20
                vju_cc_gid_set = 1;
110 20
                vju_cc_gid = gr->gr_gid;
111 20
        }
112 25
        endgrent();
113 25
        return (gr == NULL ? -1 : 0);
114
}
115
116
/**********************************************************************
117
 */
118
119
static int v_matchproto_(jail_init_f)
120 4990
vju_init(char **args)
121
{
122 4990
        if (args == NULL) {
123
                /* Autoconfig */
124 4945
                if (geteuid() != 0)
125 4895
                        return (1);
126 50
                if (vju_getuid(VARNISH_USER))
127 0
                        return (1);
128 50
        } else {
129
130 45
                if (geteuid() != 0)
131 0
                        ARGV_ERR("Unix Jail: Must be root.\n");
132
133 95
                for (;*args != NULL; args++) {
134 70
                        const char * const a_user = "user=";
135 70
                        const size_t l_user = strlen(a_user);
136 70
                        if (!strncmp(*args, a_user, l_user)) {
137 25
                                if (vju_getuid((*args) + l_user))
138 5
                                        ARGV_ERR(
139
                                            "Unix jail: %s user not found.\n",
140
                                            (*args) + 5);
141 20
                                continue;
142
                        }
143 45
                        const char * const a_workuser = "workuser=";
144 45
                        const size_t l_workuser = strlen(a_workuser);
145 45
                        if (!strncmp(*args, a_workuser, l_workuser)) {
146 15
                                if (vju_getwrkuid((*args) + l_workuser))
147 5
                                        ARGV_ERR(
148
                                            "Unix jail: %s user not found.\n",
149
                                            (*args) + 9);
150 10
                                continue;
151
                        }
152 30
                        const char * const a_ccgroup = "ccgroup=";
153 30
                        const size_t l_ccgroup = strlen(a_ccgroup);
154 30
                        if (!strncmp(*args, "ccgroup=", l_ccgroup)) {
155 25
                                if (vju_getccgid((*args) + l_ccgroup))
156 5
                                        ARGV_ERR(
157
                                            "Unix jail: %s group not found.\n",
158
                                            (*args) + 8);
159 20
                                continue;
160
                        }
161 5
                        ARGV_ERR("Unix jail: unknown sub-argument '%s'\n",
162
                            *args);
163 0
                }
164
165 25
                if (vju_user == NULL && vju_getuid(VARNISH_USER))
166 0
                        ARGV_ERR("Unix jail: %s user not found.\n",
167
                            VARNISH_USER);
168
        }
169
170 75
        AN(vju_user);
171
172 75
        vju_mgt_gid = getgid();
173
174 75
        if (vju_wrkuser == NULL && vju_getwrkuid(VCACHE_USER)) {
175 0
                vju_wrkuid = vju_uid;
176 0
                vju_wrkgid = vju_gid;
177 0
        }
178
179 75
        if (vju_wrkuser != NULL && vju_wrkgid != vju_gid)
180 5
                ARGV_ERR("Unix jail: user %s and %s have "
181
                    "different login groups\n", vju_user, vju_wrkuser);
182
183
        /* Do an explicit JAIL_MASTER_LOW */
184 70
        AZ(setegid(vju_gid));
185 70
        AZ(seteuid(vju_uid));
186 70
        return (0);
187 4965
}
188
189
static void v_matchproto_(jail_master_f)
190 3605
vju_master(enum jail_master_e jme)
191
{
192 3605
        ASSERT_JAIL_MASTER(jme);
193 3605
        if (jme == JAIL_MASTER_LOW) {
194 1725
                AZ(setegid(vju_gid));
195 1725
                AZ(seteuid(vju_uid));
196 1725
        } else {
197 1880
                AZ(seteuid(0));
198 1880
                AZ(setegid(vju_mgt_gid));
199
        }
200 3605
}
201
202
static void v_matchproto_(jail_subproc_f)
203 140
vju_subproc(enum jail_subproc_e jse)
204
{
205
        int i;
206
        gid_t gid_list[NGID];
207
208 140
        ASSERT_JAIL_SUBPROC(jse);
209 140
        AZ(seteuid(0));
210 280
        if (vju_wrkuser != NULL &&
211 140
            (jse == JAIL_SUBPROC_VCLLOAD || jse == JAIL_SUBPROC_WORKER)) {
212 0
                AZ(setgid(vju_wrkgid));
213 0
                AZ(initgroups(vju_wrkuser, vju_wrkgid));
214 0
        } else {
215 140
                AZ(setgid(vju_gid));
216 140
                AZ(initgroups(vju_user, vju_gid));
217
        }
218
219 140
        if (jse == JAIL_SUBPROC_CC && vju_cc_gid_set) {
220
                /* Add the optional extra group for the C-compiler access */
221 20
                i = getgroups(NGID, gid_list);
222 20
                assert(i >= 0);
223 20
                assert(i < NGID - 1);
224 20
                gid_list[i++] = vju_cc_gid;
225 20
                AZ(setgroups(i, gid_list));
226 20
        }
227
228 280
        if (vju_wrkuser != NULL &&
229 140
            (jse == JAIL_SUBPROC_VCLLOAD || jse == JAIL_SUBPROC_WORKER)) {
230 0
                AZ(setuid(vju_wrkuid));
231 0
        } else {
232 140
                AZ(setuid(vju_uid));
233
        }
234 140
}
235
236
static int v_matchproto_(jail_make_dir_f)
237 210
vju_make_subdir(const char *dname, const char *what, struct vsb *vsb)
238
{
239
        int e;
240
241 210
        AN(dname);
242 210
        AN(what);
243 210
        AZ(seteuid(0));
244
245 210
        if (mkdir(dname, 0755) < 0 && errno != EEXIST) {
246 0
                e = errno;
247 0
                if (vsb != NULL) {
248 0
                        VSB_printf(vsb,
249
                            "Cannot create %s directory '%s': %s\n",
250 0
                            what, dname, VAS_errtxt(e));
251 0
                } else {
252 0
                        MGT_Complain(C_ERR,
253
                            "Cannot create %s directory '%s': %s",
254 0
                            what, dname, VAS_errtxt(e));
255
                }
256 0
                return (1);
257
        }
258 210
        AZ(chown(dname, vju_uid, vju_gid));
259 210
        AZ(seteuid(vju_uid));
260 210
        return (0);
261 210
}
262
263
static int v_matchproto_(jail_make_dir_f)
264 70
vju_make_workdir(const char *dname, const char *what, struct vsb *vsb)
265
{
266
267 70
        AN(dname);
268 70
        AZ(what);
269 70
        AZ(vsb);
270 70
        AZ(seteuid(0));
271
272 70
        if (mkdir(dname, 0755) < 0 && errno != EEXIST) {
273 0
                MGT_Complain(C_ERR, "Cannot create working directory '%s': %s",
274 0
                    dname, VAS_errtxt(errno));
275 0
                return (1);
276
        }
277
        //lint -e{570}
278 70
        AZ(chown(dname, -1, vju_gid));
279 70
        AZ(seteuid(vju_uid));
280 70
        return (0);
281 70
}
282
283
static void v_matchproto_(jail_fixfd_f)
284 225
vju_fixfd(int fd, enum jail_fixfd_e what)
285
{
286
        /* Called under JAIL_MASTER_FILE */
287
288 225
        switch (what) {
289
        case JAIL_FIXFD_FILE:
290 0
                AZ(fchmod(fd, 0600));
291 0
                AZ(fchown(fd, vju_wrkuid, vju_wrkgid));
292 0
                break;
293
        case JAIL_FIXFD_VSMMGT:
294 70
                AZ(fchmod(fd, 0750));
295 70
                AZ(fchown(fd, vju_uid, vju_gid));
296 70
                break;
297
        case JAIL_FIXFD_VSMWRK:
298
        case JAIL_FIXFD_WRKTMP:
299 155
                AZ(fchmod(fd, 0750));
300 155
                AZ(fchown(fd, vju_wrkuid, vju_wrkgid));
301 155
                break;
302
        default:
303 0
                WRONG("Ain't Fixin'");
304 0
        }
305 225
}
306
307
const struct jail_tech jail_tech_unix = {
308
        .magic =        JAIL_TECH_MAGIC,
309
        .name =         "unix",
310
        .init =         vju_init,
311
        .master =       vju_master,
312
        .make_subdir =  vju_make_subdir,
313
        .make_workdir = vju_make_workdir,
314
        .fixfd =        vju_fixfd,
315
        .subproc =      vju_subproc,
316
};