Code Coverage: vinyl-cache/bin/vinyld/mgt/mgt_jail.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 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
30
 *
31
 */
32
33
#include "config.h"
34
35
#include <fcntl.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
#include <sys/stat.h>
41
//lint -efile(766, sys/statvfs.h)
42
#include <sys/statvfs.h>
43
44
#include "mgt/mgt.h"
45
#include "common/heritage.h"
46
#include "vav.h"
47
48
/**********************************************************************
49
 * A "none" jail implementation which doesn't do anything.
50
 */
51
52
static int v_matchproto_(jail_init_f)
53
vjn_init(char **args)
54 1066
{
55 1066
        if (args != NULL && *args != NULL)
56 0
                ARGV_ERR("-jnone takes no arguments.\n");
57 1066
        return (0);
58 1066
}
59
60
static void v_matchproto_(jail_master_f)
61
vjn_master(enum jail_master_e jme)
62 64998
{
63 64998
        (void)jme;
64 64998
}
65
66
static void v_matchproto_(jail_subproc_f)
67
vjn_subproc(enum jail_subproc_e jse)
68 5229
{
69 5229
        (void)jse;
70 5229
}
71
72
static const struct jail_tech jail_tech_none = {
73
        .magic =        JAIL_TECH_MAGIC,
74
        .name =         "none",
75
        .init =         vjn_init,
76
        .master =       vjn_master,
77
        .subproc =      vjn_subproc,
78
};
79
80
/**********************************************************************/
81
82
static const struct jail_tech *vjt;
83
84
static const struct choice vj_choice[] = {
85
#ifdef HAVE_SETPPRIV
86
        { "solaris",    &jail_tech_solaris },
87
#endif
88
#ifdef __linux__
89
        { "linux",      &jail_tech_linux },
90
#endif
91
        { "unix",       &jail_tech_unix },
92
        { "none",       &jail_tech_none },
93
        { NULL,         NULL },
94
};
95
96
void
97
VJ_Init(const char *j_arg)
98 1099
{
99 1099
        char **av;
100 1099
        int i;
101
102 1099
        if (j_arg != NULL) {
103 11
                av = VAV_Parse(j_arg, NULL, ARGV_COMMA);
104 11
                AN(av);
105 11
                if (av[0] != NULL)
106 0
                        ARGV_ERR("-j argument: %s\n", av[0]);
107 11
                if (av[1] == NULL)
108 0
                        ARGV_ERR("-j argument is empty\n");
109 11
                vjt = MGT_Pick(vj_choice, av[1], "jail");
110 11
                CHECK_OBJ_NOTNULL(vjt, JAIL_TECH_MAGIC);
111 11
                if (vjt->init(av + 2))
112 0
                        ARGV_EXIT;
113 11
                VAV_Free(av);
114 1088
        } else {
115
                /*
116
                 * Go through list of jail technologies until one
117
                 * succeeds, falling back to "none".
118
                 */
119 2154
                for (i = 0; vj_choice[i].name != NULL; i++) {
120 2154
                        vjt = vj_choice[i].ptr;
121 2154
                        CHECK_OBJ_NOTNULL(vjt, JAIL_TECH_MAGIC);
122 2154
                        if (!vjt->init(NULL))
123 1088
                                break;
124 2154
                }
125 1088
        }
126 1099
        VSB_printf(vident, ",-j%s", vjt->name);
127 1099
}
128
129
void
130
VJ_master(enum jail_master_e jme)
131 66158
{
132 66158
        CHECK_OBJ_NOTNULL(vjt, JAIL_TECH_MAGIC);
133 66158
        vjt->master(jme);
134 66158
}
135
136
void
137
VJ_subproc(enum jail_subproc_e jse)
138 5326
{
139 5326
        CHECK_OBJ_NOTNULL(vjt, JAIL_TECH_MAGIC);
140 5326
        vjt->subproc(jse);
141 5326
}
142
143
int
144
VJ_make_workdir(const char *dname, struct vsb *vsb)
145 1049
{
146 1049
        int i;
147
148 1049
        AN(dname);
149 1049
        AN(vsb);
150 1049
        CHECK_OBJ_NOTNULL(vjt, JAIL_TECH_MAGIC);
151
152 1049
        if (vjt->make_workdir != NULL) {
153 23
                i = vjt->make_workdir(dname, NULL, vsb);
154 23
                if (i)
155 0
                        return (i);
156 23
                VJ_master(JAIL_MASTER_FILE);
157 1026
        } else {
158 1026
                VJ_master(JAIL_MASTER_FILE);
159 1026
                if (mkdir(dname, 0755) < 0 && errno != EEXIST) {
160 0
                        VSB_printf(vsb,
161 0
                            "Cannot create working directory '%s': %s\n",
162 0
                            dname, VAS_errtxt(errno));
163 0
                        return (1);
164 0
                }
165 1026
        }
166
167 1049
        if (chdir(dname) < 0) {
168 0
                VSB_printf(vsb, "Cannot change to working directory '%s': %s\n",
169 0
                    dname, VAS_errtxt(errno));
170 0
                return (1);
171 0
        }
172
173 1049
        i = open("_.testfile", O_RDWR|O_CREAT|O_EXCL, 0600);
174 1049
        if (i < 0) {
175 0
                VSB_printf(vsb, "Cannot create test-file in %s (%s)\n"
176 0
                    "Check permissions (or delete old directory)\n",
177 0
                    dname, VAS_errtxt(errno));
178 0
                return (1);
179 0
        }
180
181
#ifdef ST_NOEXEC
182
        struct statvfs vfs[1];
183
184
        /* deliberately ignore fstatvfs errors */
185
        if (! fstatvfs(i, vfs) && vfs->f_flag & ST_NOEXEC) {
186
                closefd(&i);
187
                AZ(unlink("_.testfile"));
188
                VSB_printf(vsb, "Working directory %s (-n argument) "
189
                    "cannot reside on a file system mounted noexec\n", dname);
190
                return (1);
191
        }
192
#endif
193
194 1049
        closefd(&i);
195 1049
        AZ(unlink("_.testfile"));
196 1049
        VJ_master(JAIL_MASTER_LOW);
197 1049
        return (0);
198 1049
}
199
200
int
201
VJ_make_subdir(const char *dname, const char *what, struct vsb *vsb)
202 3734
{
203 3734
        int e;
204
205 3734
        AN(dname);
206 3734
        AN(what);
207 3734
        AN(vsb);
208 3734
        CHECK_OBJ_NOTNULL(vjt, JAIL_TECH_MAGIC);
209 3734
        if (vjt->make_subdir != NULL)
210 72
                return (vjt->make_subdir(dname, what, vsb));
211
212 3662
        VJ_master(JAIL_MASTER_FILE);
213 3662
        if (mkdir(dname, 0755) < 0 && errno != EEXIST) {
214 0
                e = errno;
215 0
                VSB_printf(vsb, "Cannot create %s directory '%s': %s\n",
216 0
                    what, dname, VAS_errtxt(e));
217 0
                return (1);
218 0
        }
219 3662
        VJ_master(JAIL_MASTER_LOW);
220 3662
        return (0);
221 3662
}
222
223
void
224
VJ_unlink(const char *fname, int ignore_enoent)
225 3591
{
226 3591
        VJ_master(JAIL_MASTER_FILE);
227 3591
        if (unlink(fname)) {
228 312
                if (errno != ENOENT || !ignore_enoent)
229 0
                    fprintf(stderr, "Could not delete '%s': %s\n",
230 0
                        fname, strerror(errno));
231 312
        }
232 3591
        VJ_master(JAIL_MASTER_LOW);
233 3591
}
234
235
void
236
VJ_rmdir(const char *dname)
237 335
{
238 335
        VJ_master(JAIL_MASTER_FILE);
239 335
        if (rmdir(dname)) {
240 1
                fprintf(stderr, "Could not rmdir '%s': %s\n",
241 1
                    dname, strerror(errno));
242 1
        }
243 335
        VJ_master(JAIL_MASTER_LOW);
244 335
}
245
246
void
247
VJ_fix_fd(int fd, enum jail_fixfd_e what)
248 3158
{
249
250 3158
        CHECK_OBJ_NOTNULL(vjt, JAIL_TECH_MAGIC);
251 3158
        if (vjt->fixfd != NULL)
252 66
                vjt->fixfd(fd, what);
253 3158
}