]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_mib.c
Update to Zstandard 1.4.4
[FreeBSD/FreeBSD.git] / sys / compat / linux / linux_mib.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/sdt.h>
35 #include <sys/systm.h>
36 #include <sys/sysctl.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/jail.h>
41 #include <sys/lock.h>
42 #include <sys/sx.h>
43
44 #include <compat/linux/linux_mib.h>
45 #include <compat/linux/linux_misc.h>
46
47 struct linux_prison {
48         char    pr_osname[LINUX_MAX_UTSNAME];
49         char    pr_osrelease[LINUX_MAX_UTSNAME];
50         int     pr_oss_version;
51         int     pr_osrel;
52 };
53
54 static struct linux_prison lprison0 = {
55         .pr_osname =            "Linux",
56         .pr_osrelease =         LINUX_VERSION_STR,
57         .pr_oss_version =       0x030600,
58         .pr_osrel =             LINUX_VERSION_CODE
59 };
60
61 static unsigned linux_osd_jail_slot;
62
63 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0, "Linux mode");
64
65 static int      linux_set_osname(struct thread *td, char *osname);
66 static int      linux_set_osrelease(struct thread *td, char *osrelease);
67 static int      linux_set_oss_version(struct thread *td, int oss_version);
68
69 static int
70 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
71 {
72         char osname[LINUX_MAX_UTSNAME];
73         int error;
74
75         linux_get_osname(req->td, osname);
76         error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
77         if (error != 0 || req->newptr == NULL)
78                 return (error);
79         error = linux_set_osname(req->td, osname);
80
81         return (error);
82 }
83
84 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
85             CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
86             0, 0, linux_sysctl_osname, "A",
87             "Linux kernel OS name");
88
89 static int
90 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
91 {
92         char osrelease[LINUX_MAX_UTSNAME];
93         int error;
94
95         linux_get_osrelease(req->td, osrelease);
96         error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
97         if (error != 0 || req->newptr == NULL)
98                 return (error);
99         error = linux_set_osrelease(req->td, osrelease);
100
101         return (error);
102 }
103
104 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
105             CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
106             0, 0, linux_sysctl_osrelease, "A",
107             "Linux kernel OS release");
108
109 static int
110 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
111 {
112         int oss_version;
113         int error;
114
115         oss_version = linux_get_oss_version(req->td);
116         error = sysctl_handle_int(oidp, &oss_version, 0, req);
117         if (error != 0 || req->newptr == NULL)
118                 return (error);
119         error = linux_set_oss_version(req->td, oss_version);
120
121         return (error);
122 }
123
124 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
125             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
126             0, 0, linux_sysctl_oss_version, "I",
127             "Linux OSS version");
128
129 /*
130  * Map the osrelease into integer
131  */
132 static int
133 linux_map_osrel(char *osrelease, int *osrel)
134 {
135         char *sep, *eosrelease;
136         int len, v0, v1, v2, v;
137
138         len = strlen(osrelease);
139         eosrelease = osrelease + len;
140         v0 = strtol(osrelease, &sep, 10);
141         if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
142                 return (EINVAL);
143         osrelease = sep + 1;
144         v1 = strtol(osrelease, &sep, 10);
145         if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
146                 return (EINVAL);
147         osrelease = sep + 1;
148         v2 = strtol(osrelease, &sep, 10);
149         if (osrelease == sep ||
150             (sep != eosrelease && (sep + 1 >= eosrelease || *sep != '-')))
151                 return (EINVAL);
152
153         v = LINUX_KERNVER(v0, v1, v2);
154         if (v < LINUX_KERNVER(1, 0, 0))
155                 return (EINVAL);
156
157         if (osrel != NULL)
158                 *osrel = v;
159
160         return (0);
161 }
162
163 /*
164  * Find a prison with Linux info.
165  * Return the Linux info and the (locked) prison.
166  */
167 static struct linux_prison *
168 linux_find_prison(struct prison *spr, struct prison **prp)
169 {
170         struct prison *pr;
171         struct linux_prison *lpr;
172
173         for (pr = spr;; pr = pr->pr_parent) {
174                 mtx_lock(&pr->pr_mtx);
175                 lpr = (pr == &prison0)
176                     ? &lprison0
177                     : osd_jail_get(pr, linux_osd_jail_slot);
178                 if (lpr != NULL)
179                         break;
180                 mtx_unlock(&pr->pr_mtx);
181         }
182         *prp = pr;
183
184         return (lpr);
185 }
186
187 /*
188  * Ensure a prison has its own Linux info.  If lprp is non-null, point it to
189  * the Linux info and lock the prison.
190  */
191 static void
192 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
193 {
194         struct prison *ppr;
195         struct linux_prison *lpr, *nlpr;
196         void **rsv;
197
198         /* If this prison already has Linux info, return that. */
199         lpr = linux_find_prison(pr, &ppr);
200         if (ppr == pr)
201                 goto done;
202         /*
203          * Allocate a new info record.  Then check again, in case something
204          * changed during the allocation.
205          */
206         mtx_unlock(&ppr->pr_mtx);
207         nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
208         rsv = osd_reserve(linux_osd_jail_slot);
209         lpr = linux_find_prison(pr, &ppr);
210         if (ppr == pr) {
211                 free(nlpr, M_PRISON);
212                 osd_free_reserved(rsv);
213                 goto done;
214         }
215         /* Inherit the initial values from the ancestor. */
216         mtx_lock(&pr->pr_mtx);
217         (void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
218         bcopy(lpr, nlpr, sizeof(*lpr));
219         lpr = nlpr;
220         mtx_unlock(&ppr->pr_mtx);
221  done:
222         if (lprp != NULL)
223                 *lprp = lpr;
224         else
225                 mtx_unlock(&pr->pr_mtx);
226 }
227
228 /*
229  * Jail OSD methods for Linux prison data.
230  */
231 static int
232 linux_prison_create(void *obj, void *data)
233 {
234         struct prison *pr = obj;
235         struct vfsoptlist *opts = data;
236         int jsys;
237
238         if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
239             jsys == JAIL_SYS_INHERIT)
240                 return (0);
241         /*
242          * Inherit a prison's initial values from its parent
243          * (different from JAIL_SYS_INHERIT which also inherits changes).
244          */
245         linux_alloc_prison(pr, NULL);
246         return (0);
247 }
248
249 static int
250 linux_prison_check(void *obj __unused, void *data)
251 {
252         struct vfsoptlist *opts = data;
253         char *osname, *osrelease;
254         int error, jsys, len, oss_version;
255
256         /* Check that the parameters are correct. */
257         error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
258         if (error != ENOENT) {
259                 if (error != 0)
260                         return (error);
261                 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
262                         return (EINVAL);
263         }
264         error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
265         if (error != ENOENT) {
266                 if (error != 0)
267                         return (error);
268                 if (len == 0 || osname[len - 1] != '\0')
269                         return (EINVAL);
270                 if (len > LINUX_MAX_UTSNAME) {
271                         vfs_opterror(opts, "linux.osname too long");
272                         return (ENAMETOOLONG);
273                 }
274         }
275         error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
276         if (error != ENOENT) {
277                 if (error != 0)
278                         return (error);
279                 if (len == 0 || osrelease[len - 1] != '\0')
280                         return (EINVAL);
281                 if (len > LINUX_MAX_UTSNAME) {
282                         vfs_opterror(opts, "linux.osrelease too long");
283                         return (ENAMETOOLONG);
284                 }
285                 error = linux_map_osrel(osrelease, NULL);
286                 if (error != 0) {
287                         vfs_opterror(opts, "linux.osrelease format error");
288                         return (error);
289                 }
290         }
291         error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
292             sizeof(oss_version));
293
294         if (error == ENOENT)
295                 error = 0;
296         return (error);
297 }
298
299 static int
300 linux_prison_set(void *obj, void *data)
301 {
302         struct linux_prison *lpr;
303         struct prison *pr = obj;
304         struct vfsoptlist *opts = data;
305         char *osname, *osrelease;
306         int error, gotversion, jsys, len, oss_version;
307
308         /* Set the parameters, which should be correct. */
309         error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
310         if (error == ENOENT)
311                 jsys = -1;
312         error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
313         if (error == ENOENT)
314                 osname = NULL;
315         else
316                 jsys = JAIL_SYS_NEW;
317         error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
318         if (error == ENOENT)
319                 osrelease = NULL;
320         else
321                 jsys = JAIL_SYS_NEW;
322         error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
323             sizeof(oss_version));
324         if (error == ENOENT)
325                 gotversion = 0;
326         else {
327                 gotversion = 1;
328                 jsys = JAIL_SYS_NEW;
329         }
330         switch (jsys) {
331         case JAIL_SYS_INHERIT:
332                 /* "linux=inherit": inherit the parent's Linux info. */
333                 mtx_lock(&pr->pr_mtx);
334                 osd_jail_del(pr, linux_osd_jail_slot);
335                 mtx_unlock(&pr->pr_mtx);
336                 break;
337         case JAIL_SYS_NEW:
338                 /*
339                  * "linux=new" or "linux.*":
340                  * the prison gets its own Linux info.
341                  */
342                 linux_alloc_prison(pr, &lpr);
343                 if (osrelease) {
344                         (void)linux_map_osrel(osrelease, &lpr->pr_osrel);
345                         strlcpy(lpr->pr_osrelease, osrelease,
346                             LINUX_MAX_UTSNAME);
347                 }
348                 if (osname)
349                         strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
350                 if (gotversion)
351                         lpr->pr_oss_version = oss_version;
352                 mtx_unlock(&pr->pr_mtx);
353         }
354
355         return (0);
356 }
357
358 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
359 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
360     "Jail Linux kernel OS name");
361 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
362     "Jail Linux kernel OS release");
363 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
364     "I", "Jail Linux OSS version");
365
366 static int
367 linux_prison_get(void *obj, void *data)
368 {
369         struct linux_prison *lpr;
370         struct prison *ppr;
371         struct prison *pr = obj;
372         struct vfsoptlist *opts = data;
373         int error, i;
374
375         static int version0;
376
377         /* See if this prison is the one with the Linux info. */
378         lpr = linux_find_prison(pr, &ppr);
379         i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
380         error = vfs_setopt(opts, "linux", &i, sizeof(i));
381         if (error != 0 && error != ENOENT)
382                 goto done;
383         if (i) {
384                 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
385                 if (error != 0 && error != ENOENT)
386                         goto done;
387                 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
388                 if (error != 0 && error != ENOENT)
389                         goto done;
390                 error = vfs_setopt(opts, "linux.oss_version",
391                     &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
392                 if (error != 0 && error != ENOENT)
393                         goto done;
394         } else {
395                 /*
396                  * If this prison is inheriting its Linux info, report
397                  * empty/zero parameters.
398                  */
399                 error = vfs_setopts(opts, "linux.osname", "");
400                 if (error != 0 && error != ENOENT)
401                         goto done;
402                 error = vfs_setopts(opts, "linux.osrelease", "");
403                 if (error != 0 && error != ENOENT)
404                         goto done;
405                 error = vfs_setopt(opts, "linux.oss_version", &version0,
406                     sizeof(lpr->pr_oss_version));
407                 if (error != 0 && error != ENOENT)
408                         goto done;
409         }
410         error = 0;
411
412  done:
413         mtx_unlock(&ppr->pr_mtx);
414
415         return (error);
416 }
417
418 static void
419 linux_prison_destructor(void *data)
420 {
421
422         free(data, M_PRISON);
423 }
424
425 void
426 linux_osd_jail_register(void)
427 {
428         struct prison *pr;
429         osd_method_t methods[PR_MAXMETHOD] = {
430             [PR_METHOD_CREATE] =        linux_prison_create,
431             [PR_METHOD_GET] =           linux_prison_get,
432             [PR_METHOD_SET] =           linux_prison_set,
433             [PR_METHOD_CHECK] =         linux_prison_check
434         };
435
436         linux_osd_jail_slot =
437             osd_jail_register(linux_prison_destructor, methods);
438         /* Copy the system Linux info to any current prisons. */
439         sx_slock(&allprison_lock);
440         TAILQ_FOREACH(pr, &allprison, pr_list)
441                 linux_alloc_prison(pr, NULL);
442         sx_sunlock(&allprison_lock);
443 }
444
445 void
446 linux_osd_jail_deregister(void)
447 {
448
449         osd_jail_deregister(linux_osd_jail_slot);
450 }
451
452 void
453 linux_get_osname(struct thread *td, char *dst)
454 {
455         struct prison *pr;
456         struct linux_prison *lpr;
457
458         lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
459         bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
460         mtx_unlock(&pr->pr_mtx);
461 }
462
463 static int
464 linux_set_osname(struct thread *td, char *osname)
465 {
466         struct prison *pr;
467         struct linux_prison *lpr;
468
469         lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
470         strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
471         mtx_unlock(&pr->pr_mtx);
472
473         return (0);
474 }
475
476 void
477 linux_get_osrelease(struct thread *td, char *dst)
478 {
479         struct prison *pr;
480         struct linux_prison *lpr;
481
482         lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
483         bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
484         mtx_unlock(&pr->pr_mtx);
485 }
486
487 int
488 linux_kernver(struct thread *td)
489 {
490         struct prison *pr;
491         struct linux_prison *lpr;
492         int osrel;
493
494         lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
495         osrel = lpr->pr_osrel;
496         mtx_unlock(&pr->pr_mtx);
497
498         return (osrel);
499 }
500
501 static int
502 linux_set_osrelease(struct thread *td, char *osrelease)
503 {
504         struct prison *pr;
505         struct linux_prison *lpr;
506         int error;
507
508         lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
509         error = linux_map_osrel(osrelease, &lpr->pr_osrel);
510         if (error == 0)
511                 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
512         mtx_unlock(&pr->pr_mtx);
513
514         return (error);
515 }
516
517 int
518 linux_get_oss_version(struct thread *td)
519 {
520         struct prison *pr;
521         struct linux_prison *lpr;
522         int version;
523
524         lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
525         version = lpr->pr_oss_version;
526         mtx_unlock(&pr->pr_mtx);
527
528         return (version);
529 }
530
531 static int
532 linux_set_oss_version(struct thread *td, int oss_version)
533 {
534         struct prison *pr;
535         struct linux_prison *lpr;
536
537         lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
538         lpr->pr_oss_version = oss_version;
539         mtx_unlock(&pr->pr_mtx);
540
541         return (0);
542 }