Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: subr_kobj_vfs.c,v 1.13 2026/01/04 03:19:33 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software developed for The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998-2000 Doug Rabson
     34  * Copyright (c) 2004 Peter Wemm
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56  * SUCH DAMAGE.
     57  */
     58 
     59 /*
     60  * Kernel loader vfs routines.
     61  */
     62 
     63 #include <sys/kobj_impl.h>
     64 #include <sys/sdt.h>
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_modular.h"
     68 #endif
     69 
     70 #ifdef MODULAR
     71 
     72 #include <sys/param.h>
     73 #include <sys/fcntl.h>
     74 #include <sys/module.h>
     75 #include <sys/namei.h>
     76 #include <sys/vnode.h>
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: subr_kobj_vfs.c,v 1.13 2026/01/04 03:19:33 riastradh Exp $");
     80 
     81 static void
     82 kobj_close_vfs(kobj_t ko)
     83 {
     84 
     85 	VOP_UNLOCK(ko->ko_source);
     86 	vn_close(ko->ko_source, FREAD, kauth_cred_get());
     87 }
     88 
     89 /*
     90  * kobj_read:
     91  *
     92  *	Utility function: read from the object.
     93  */
     94 static int
     95 kobj_read_vfs(kobj_t ko, void **basep, size_t size, off_t off,
     96 	bool allocate)
     97 {
     98 	size_t resid;
     99 	void *base;
    100 	int error;
    101 
    102 	KASSERT(ko->ko_source != NULL);
    103 
    104 	if (allocate) {
    105 		base = kmem_alloc(size, KM_SLEEP);
    106 	} else {
    107 		base = *basep;
    108 #ifdef DIAGNOSTIC
    109 		bool ok = false;
    110 		if ((uintptr_t)base >= (uintptr_t)ko->ko_text_address &&
    111 		    (uintptr_t)base + size <=
    112 		    (uintptr_t)ko->ko_text_address + ko->ko_text_size)
    113 			ok = true;
    114 		if ((uintptr_t)base >= (uintptr_t)ko->ko_data_address &&
    115 		    (uintptr_t)base + size <=
    116 		    (uintptr_t)ko->ko_data_address + ko->ko_data_size)
    117 			ok = true;
    118 		if ((uintptr_t)base >= (uintptr_t)ko->ko_rodata_address &&
    119 		    (uintptr_t)base + size <=
    120 		    (uintptr_t)ko->ko_rodata_address + ko->ko_rodata_size)
    121 			ok = true;
    122 		if (!ok)
    123 			panic("kobj_read_vfs: not in a dedicated segment");
    124 #endif
    125 	}
    126 
    127 	error = vn_rdwr(UIO_READ, ko->ko_source, base, size, off,
    128 	    UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid,
    129 	    curlwp);
    130 
    131 	if (error == 0 && resid != 0) {
    132 		error = SET_ERROR(EINVAL);
    133 	}
    134 
    135 	if (allocate && error != 0) {
    136 		kmem_free(base, size);
    137 		base = NULL;
    138 	}
    139 
    140 	if (allocate)
    141 		*basep = base;
    142 
    143 	return error;
    144 }
    145 
    146 /*
    147  * kobj_load_vfs:
    148  *
    149  *	Load an object located in the file system.
    150  */
    151 int
    152 kobj_load_vfs(kobj_t *kop, const char *path, const bool nochroot)
    153 {
    154 	struct pathbuf *pb;
    155 	struct vnode *vp;
    156 	int error;
    157 	kobj_t ko;
    158 
    159 	KASSERT(path != NULL);
    160 	if (strchr(path, '/') == NULL)
    161 		return SET_ERROR(ENOENT);
    162 
    163 	ko = kmem_zalloc(sizeof(*ko), KM_SLEEP);
    164 	pb = pathbuf_create(path);
    165 	if (pb == NULL) {
    166 	 	kmem_free(ko, sizeof(*ko));
    167 		return SET_ERROR(ENOMEM);
    168 	}
    169 
    170 	error = vn_open(NULL, pb, (nochroot ? NOCHROOT : 0), FREAD, 0,
    171 	    &vp, NULL, NULL);
    172 
    173  	if (error != 0) {
    174 		pathbuf_destroy(pb);
    175 	 	kmem_free(ko, sizeof(*ko));
    176 	 	return error;
    177 	}
    178 
    179 	ko->ko_type = KT_VNODE;
    180 	kobj_setname(ko, path);
    181 	ko->ko_source = vp;
    182 	ko->ko_read = kobj_read_vfs;
    183 	ko->ko_close = kobj_close_vfs;
    184 	pathbuf_destroy(pb);
    185 
    186 	*kop = ko;
    187 	return kobj_load(ko);
    188 }
    189 
    190 #else /* MODULAR */
    191 
    192 int
    193 kobj_load_vfs(kobj_t *kop, const char *path, const bool nochroot)
    194 {
    195 
    196 	return SET_ERROR(ENOSYS);
    197 }
    198 
    199 #endif
    200