Home | History | Annotate | Line # | Download | only in kern
kern_module_vfs.c revision 1.17
      1  1.17  pgoyette /*	$NetBSD: kern_module_vfs.c,v 1.17 2019/01/27 02:08:43 pgoyette Exp $	*/
      2   1.1     pooka 
      3   1.1     pooka /*-
      4   1.1     pooka  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5   1.1     pooka  * All rights reserved.
      6   1.1     pooka  *
      7   1.1     pooka  * This code is derived from software developed for The NetBSD Foundation
      8   1.1     pooka  * by Andrew Doran.
      9   1.1     pooka  *
     10   1.1     pooka  * Redistribution and use in source and binary forms, with or without
     11   1.1     pooka  * modification, are permitted provided that the following conditions
     12   1.1     pooka  * are met:
     13   1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     14   1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     15   1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     pooka  *    documentation and/or other materials provided with the distribution.
     18   1.1     pooka  *
     19   1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     pooka  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     pooka  */
     31   1.1     pooka 
     32   1.1     pooka /*
     33   1.1     pooka  * Kernel module file system interaction.
     34   1.1     pooka  */
     35   1.1     pooka 
     36   1.1     pooka #include <sys/cdefs.h>
     37  1.17  pgoyette __KERNEL_RCSID(0, "$NetBSD: kern_module_vfs.c,v 1.17 2019/01/27 02:08:43 pgoyette Exp $");
     38   1.1     pooka 
     39   1.1     pooka #define _MODULE_INTERNAL
     40   1.1     pooka #include <sys/param.h>
     41   1.1     pooka #include <sys/fcntl.h>
     42   1.1     pooka #include <sys/kmem.h>
     43   1.1     pooka #include <sys/kobj.h>
     44   1.1     pooka #include <sys/module.h>
     45   1.1     pooka #include <sys/namei.h>
     46   1.1     pooka #include <sys/pool.h>
     47   1.1     pooka #include <sys/stat.h>
     48   1.1     pooka #include <sys/vnode.h>
     49   1.1     pooka 
     50   1.1     pooka #include <prop/proplib.h>
     51   1.1     pooka 
     52   1.2     pooka static int	module_load_plist_vfs(const char *, const bool,
     53   1.2     pooka 				      prop_dictionary_t *);
     54   1.5  pgoyette 
     55   1.5  pgoyette void
     56   1.5  pgoyette module_load_vfs_init(void)
     57   1.5  pgoyette {
     58   1.5  pgoyette 	module_load_vfs_vec = module_load_vfs;
     59  1.13     pooka 	aprint_normal("kern.module.path=%s\n", module_base);
     60   1.5  pgoyette }
     61   1.1     pooka 
     62   1.1     pooka int
     63   1.1     pooka module_load_vfs(const char *name, int flags, bool autoload,
     64   1.1     pooka 	module_t *mod, prop_dictionary_t *filedictp)
     65   1.1     pooka {
     66   1.1     pooka 	char *path;
     67   1.1     pooka 	bool nochroot;
     68   1.1     pooka 	int error;
     69   1.8   jnemeth 	prop_bool_t noload;
     70   1.8   jnemeth 	prop_dictionary_t moduledict;
     71   1.1     pooka 
     72   1.1     pooka 	nochroot = false;
     73   1.1     pooka 	error = 0;
     74   1.1     pooka 	path = NULL;
     75   1.8   jnemeth 	moduledict = NULL;
     76   1.1     pooka 	if (filedictp)
     77   1.1     pooka 		*filedictp = NULL;
     78   1.1     pooka 	path = PNBUF_GET();
     79   1.1     pooka 
     80   1.1     pooka 	if (!autoload) {
     81  1.11   mbalmer 		if (strchr(name,  '/') != NULL) {
     82  1.11   mbalmer 			nochroot = false;
     83  1.11   mbalmer 			snprintf(path, MAXPATHLEN, "%s", name);
     84  1.14  pgoyette 			module_print("Loading module from %s", path);
     85  1.11   mbalmer 			error = kobj_load_vfs(&mod->mod_kobj, path, nochroot);
     86  1.11   mbalmer 		} else
     87  1.11   mbalmer 			error = ENOENT;
     88   1.1     pooka 	}
     89   1.1     pooka 	if (autoload || (error == ENOENT)) {
     90  1.11   mbalmer 		if (strchr(name, '/') == NULL) {
     91  1.11   mbalmer 			nochroot = true;
     92  1.11   mbalmer 			snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod",
     93  1.11   mbalmer 			    module_base, name, name);
     94  1.14  pgoyette 			module_print("Loading module from %s", path);
     95  1.11   mbalmer 			error = kobj_load_vfs(&mod->mod_kobj, path, nochroot);
     96  1.11   mbalmer 		} else
     97  1.11   mbalmer 			error = ENOENT;
     98   1.1     pooka 	}
     99   1.1     pooka 	if (error != 0) {
    100   1.1     pooka 		PNBUF_PUT(path);
    101  1.12  christos 		module_print("Cannot %sload kernel object `%s'"
    102  1.12  christos 		    " error=%d", autoload ? "auto" : "", name, error);
    103   1.1     pooka 		return error;
    104   1.1     pooka 	}
    105   1.1     pooka 
    106   1.1     pooka 	/*
    107  1.10   jnemeth 	 * Load and process <module>.plist if it exists.
    108   1.1     pooka 	 */
    109  1.17  pgoyette 	if ((!ISSET(flags, MODCTL_NO_PROP) && filedictp) || autoload) {
    110   1.8   jnemeth 		error = module_load_plist_vfs(path, nochroot, &moduledict);
    111   1.1     pooka 		if (error != 0) {
    112   1.1     pooka 			module_print("plist load returned error %d for `%s'",
    113   1.1     pooka 			    error, path);
    114   1.1     pooka 			if (error != ENOENT)
    115   1.1     pooka 				goto fail;
    116   1.8   jnemeth 		} else if (autoload) {
    117   1.8   jnemeth 			noload = prop_dictionary_get(moduledict, "noautoload");
    118   1.8   jnemeth 			if (noload != NULL && prop_bool_true(noload)) {
    119   1.8   jnemeth 				module_error("autoloading is disallowed for %s",
    120   1.8   jnemeth 				    path);
    121  1.12  christos 				prop_object_release(moduledict);
    122   1.8   jnemeth 				error = EPERM;
    123   1.8   jnemeth 				goto fail;
    124   1.8   jnemeth 			}
    125   1.8   jnemeth 		}
    126   1.8   jnemeth 		if (error == 0) {	/* can get here if error == ENOENT */
    127  1.17  pgoyette 			if (!ISSET(flags, MODCTL_NO_PROP) && filedictp)
    128   1.8   jnemeth 				*filedictp = moduledict;
    129   1.8   jnemeth 			else
    130   1.8   jnemeth 				prop_object_release(moduledict);
    131   1.1     pooka 		}
    132   1.1     pooka 	}
    133   1.1     pooka 
    134   1.1     pooka 	PNBUF_PUT(path);
    135   1.1     pooka 	return 0;
    136   1.1     pooka 
    137   1.1     pooka  fail:
    138   1.1     pooka 	kobj_unload(mod->mod_kobj);
    139   1.1     pooka 	PNBUF_PUT(path);
    140   1.1     pooka 	return error;
    141   1.1     pooka }
    142   1.1     pooka 
    143   1.1     pooka /*
    144   1.2     pooka  * module_load_plist_vfs:
    145   1.1     pooka  *
    146   1.1     pooka  *	Load a plist located in the file system into memory.
    147   1.1     pooka  */
    148   1.1     pooka static int
    149   1.2     pooka module_load_plist_vfs(const char *modpath, const bool nochroot,
    150  1.12  christos     prop_dictionary_t *filedictp)
    151   1.1     pooka {
    152   1.9  dholland 	struct pathbuf *pb;
    153   1.1     pooka 	struct nameidata nd;
    154   1.1     pooka 	struct stat sb;
    155   1.1     pooka 	void *base;
    156   1.1     pooka 	char *proppath;
    157   1.1     pooka 	const size_t plistsize = 8192;
    158   1.1     pooka 	size_t resid;
    159   1.1     pooka 	int error, pathlen;
    160   1.1     pooka 
    161   1.1     pooka 	KASSERT(filedictp != NULL);
    162   1.1     pooka 	base = NULL;
    163   1.1     pooka 
    164   1.1     pooka 	proppath = PNBUF_GET();
    165  1.15      maya 	strlcpy(proppath, modpath, MAXPATHLEN);
    166   1.1     pooka 	pathlen = strlen(proppath);
    167  1.10   jnemeth 	if ((pathlen >= 6) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) {
    168  1.10   jnemeth 		strcpy(&proppath[pathlen - 5], ".plist");
    169  1.10   jnemeth 	} else if (pathlen < MAXPATHLEN - 6) {
    170  1.10   jnemeth 			strcat(proppath, ".plist");
    171   1.1     pooka 	} else {
    172   1.1     pooka 		error = ENOENT;
    173   1.1     pooka 		goto out1;
    174   1.1     pooka 	}
    175   1.1     pooka 
    176   1.9  dholland 	/* XXX this makes an unnecessary extra copy of the path */
    177   1.9  dholland 	pb = pathbuf_create(proppath);
    178   1.9  dholland 	if (pb == NULL) {
    179   1.9  dholland 		error = ENOMEM;
    180   1.9  dholland 		goto out1;
    181   1.9  dholland 	}
    182  1.14  pgoyette 	module_print("Loading plist from %s", proppath);
    183   1.9  dholland 
    184   1.9  dholland 	NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0), pb);
    185   1.1     pooka 
    186   1.4   jnemeth 	error = vn_open(&nd, FREAD, 0);
    187   1.4   jnemeth  	if (error != 0) {
    188   1.9  dholland 	 	goto out2;
    189   1.1     pooka 	}
    190   1.1     pooka 
    191   1.1     pooka 	error = vn_stat(nd.ni_vp, &sb);
    192   1.3  dholland 	if (error != 0) {
    193   1.9  dholland 		goto out3;
    194   1.3  dholland 	}
    195   1.1     pooka 	if (sb.st_size >= (plistsize - 1)) {	/* leave space for term \0 */
    196   1.1     pooka 		error = EFBIG;
    197   1.9  dholland 		goto out3;
    198   1.1     pooka 	}
    199   1.1     pooka 
    200   1.1     pooka 	base = kmem_alloc(plistsize, KM_SLEEP);
    201   1.1     pooka 	error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0,
    202   1.1     pooka 	    UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp);
    203   1.1     pooka 	*((uint8_t *)base + sb.st_size) = '\0';
    204   1.1     pooka 	if (error == 0 && resid != 0) {
    205   1.1     pooka 		error = EFBIG;
    206   1.1     pooka 	}
    207   1.1     pooka 	if (error != 0) {
    208   1.1     pooka 		kmem_free(base, plistsize);
    209   1.1     pooka 		base = NULL;
    210   1.9  dholland 		goto out3;
    211   1.1     pooka 	}
    212   1.1     pooka 
    213   1.1     pooka 	*filedictp = prop_dictionary_internalize(base);
    214   1.1     pooka 	if (*filedictp == NULL) {
    215   1.1     pooka 		error = EINVAL;
    216   1.1     pooka 	}
    217   1.1     pooka 	kmem_free(base, plistsize);
    218   1.1     pooka 	base = NULL;
    219   1.1     pooka 	KASSERT(error == 0);
    220   1.1     pooka 
    221   1.9  dholland out3:
    222   1.7   hannken 	VOP_UNLOCK(nd.ni_vp);
    223   1.1     pooka 	vn_close(nd.ni_vp, FREAD, kauth_cred_get());
    224   1.1     pooka 
    225   1.9  dholland out2:
    226   1.9  dholland 	pathbuf_destroy(pb);
    227   1.9  dholland 
    228   1.1     pooka out1:
    229   1.1     pooka 	PNBUF_PUT(proppath);
    230   1.1     pooka 	return error;
    231   1.1     pooka }
    232