Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common modutils related functions for busybox |
| 3 | * |
| 4 | * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi> |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 7 | */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 8 | #include "modutils.h" |
| 9 | |
Waldemar Brodkorb | 9c083f5 | 2016-12-26 20:07:59 +0100 | [diff] [blame] | 10 | #include <sys/syscall.h> |
| 11 | |
| 12 | #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts) |
| 13 | #if defined(__NR_finit_module) |
| 14 | # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 15 | #endif |
Waldemar Brodkorb | 9c083f5 | 2016-12-26 20:07:59 +0100 | [diff] [blame] | 16 | #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 17 | |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 18 | static module_entry *helper_get_module(module_db *db, const char *module, int create) |
| 19 | { |
| 20 | char modname[MODULE_NAME_LEN]; |
| 21 | struct module_entry *e; |
| 22 | unsigned i, hash; |
| 23 | |
| 24 | filename2modname(module, modname); |
| 25 | |
| 26 | hash = 0; |
| 27 | for (i = 0; modname[i]; i++) |
| 28 | hash = ((hash << 5) + hash) + modname[i]; |
| 29 | hash %= MODULE_HASH_SIZE; |
| 30 | |
| 31 | for (e = db->buckets[hash]; e; e = e->next) |
| 32 | if (strcmp(e->modname, modname) == 0) |
| 33 | return e; |
| 34 | if (!create) |
| 35 | return NULL; |
| 36 | |
| 37 | e = xzalloc(sizeof(*e)); |
| 38 | e->modname = xstrdup(modname); |
| 39 | e->next = db->buckets[hash]; |
| 40 | db->buckets[hash] = e; |
Denys Vlasenko | 196e400 | 2015-11-06 15:50:28 +0100 | [diff] [blame] | 41 | IF_DEPMOD(e->dnext = e->dprev = e;) |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 42 | |
| 43 | return e; |
| 44 | } |
| 45 | module_entry* FAST_FUNC moddb_get(module_db *db, const char *module) |
| 46 | { |
| 47 | return helper_get_module(db, module, 0); |
| 48 | } |
| 49 | module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module) |
| 50 | { |
| 51 | return helper_get_module(db, module, 1); |
| 52 | } |
| 53 | |
| 54 | void FAST_FUNC moddb_free(module_db *db) |
| 55 | { |
| 56 | module_entry *e, *n; |
| 57 | unsigned i; |
| 58 | |
| 59 | for (i = 0; i < MODULE_HASH_SIZE; i++) { |
| 60 | for (e = db->buckets[i]; e; e = n) { |
| 61 | n = e->next; |
| 62 | free(e->name); |
| 63 | free(e->modname); |
| 64 | free(e); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 69 | void FAST_FUNC replace(char *s, char what, char with) |
| 70 | { |
| 71 | while (*s) { |
| 72 | if (what == *s) |
| 73 | *s = with; |
| 74 | ++s; |
| 75 | } |
| 76 | } |
| 77 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 78 | int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim) |
| 79 | { |
| 80 | char *tok; |
| 81 | int len = 0; |
| 82 | |
| 83 | while ((tok = strsep(&string, delim)) != NULL) { |
| 84 | if (tok[0] == '\0') |
| 85 | continue; |
| 86 | llist_add_to_end(llist, xstrdup(tok)); |
| 87 | len += strlen(tok); |
| 88 | } |
| 89 | return len; |
| 90 | } |
| 91 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 92 | char* FAST_FUNC filename2modname(const char *filename, char *modname) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 93 | { |
Denys Vlasenko | cc70b6f | 2015-01-24 22:30:30 +0100 | [diff] [blame] | 94 | char local_modname[MODULE_NAME_LEN]; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 95 | int i; |
Denys Vlasenko | 7885452 | 2015-01-01 19:02:40 +0100 | [diff] [blame] | 96 | const char *from; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 97 | |
| 98 | if (filename == NULL) |
| 99 | return NULL; |
| 100 | if (modname == NULL) |
tarun.kundu | 46c8a90 | 2023-02-02 15:25:03 -0800 | [diff] [blame] | 101 | return NULL; |
| 102 | // modname = local_modname; |
Denys Vlasenko | 7885452 | 2015-01-01 19:02:40 +0100 | [diff] [blame] | 103 | // Disabled since otherwise "modprobe dir/name" would work |
| 104 | // as if it is "modprobe name". It is unclear why |
| 105 | // 'basenamization' was here in the first place. |
| 106 | //from = bb_get_last_path_component_nostrip(filename); |
| 107 | from = filename; |
Denis Vlasenko | 48637e0 | 2009-02-26 12:00:52 +0000 | [diff] [blame] | 108 | for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 109 | modname[i] = (from[i] == '-') ? '_' : from[i]; |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 110 | modname[i] = '\0'; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 111 | |
Denys Vlasenko | cc70b6f | 2015-01-24 22:30:30 +0100 | [diff] [blame] | 112 | if (modname == local_modname) |
| 113 | return xstrdup(modname); |
| 114 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 115 | return modname; |
| 116 | } |
| 117 | |
Kang-Che Sung | b1d6a2c | 2017-01-31 21:09:17 +0800 | [diff] [blame] | 118 | #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 119 | char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 120 | { |
| 121 | char *options; |
| 122 | int optlen; |
| 123 | |
| 124 | options = xzalloc(1); |
| 125 | optlen = 0; |
| 126 | while (*++argv) { |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 127 | const char *fmt; |
| 128 | const char *var; |
| 129 | const char *val; |
| 130 | |
| 131 | var = *argv; |
| 132 | options = xrealloc(options, optlen + 2 + strlen(var) + 2); |
| 133 | fmt = "%.*s%s "; |
| 134 | val = strchrnul(var, '='); |
| 135 | if (quote_spaces) { |
| 136 | /* |
| 137 | * modprobe (module-init-tools version 3.11.1) compat: |
| 138 | * quote only value: |
| 139 | * var="val with spaces", not "var=val with spaces" |
| 140 | * (note: var *name* is not checked for spaces!) |
| 141 | */ |
| 142 | if (*val) { /* has var=val format. skip '=' */ |
| 143 | val++; |
| 144 | if (strchr(val, ' ')) |
| 145 | fmt = "%.*s\"%s\" "; |
| 146 | } |
| 147 | } |
| 148 | optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 149 | } |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 150 | /* Remove trailing space. Disabled */ |
| 151 | /* if (optlen != 0) options[optlen-1] = '\0'; */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 152 | return options; |
| 153 | } |
Kang-Che Sung | b1d6a2c | 2017-01-31 21:09:17 +0800 | [diff] [blame] | 154 | #endif |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 155 | |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 156 | #if ENABLE_FEATURE_INSMOD_TRY_MMAP |
| 157 | void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p) |
| 158 | { |
| 159 | /* We have user reports of failure to load 3MB module |
| 160 | * on a 16MB RAM machine. Apparently even a transient |
| 161 | * memory spike to 6MB during module load |
| 162 | * is too big for that system. */ |
| 163 | void *image; |
| 164 | struct stat st; |
| 165 | int fd; |
| 166 | |
| 167 | fd = xopen(filename, O_RDONLY); |
| 168 | fstat(fd, &st); |
| 169 | image = NULL; |
| 170 | /* st.st_size is off_t, we can't just pass it to mmap */ |
| 171 | if (st.st_size <= *image_size_p) { |
| 172 | size_t image_size = st.st_size; |
Denys Vlasenko | fd3c512 | 2020-12-14 18:25:28 +0100 | [diff] [blame] | 173 | image = mmap_read(fd, image_size); |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 174 | if (image == MAP_FAILED) { |
| 175 | image = NULL; |
| 176 | } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) { |
| 177 | /* No ELF signature. Compressed module? */ |
| 178 | munmap(image, image_size); |
| 179 | image = NULL; |
| 180 | } else { |
| 181 | /* Success. Report the size */ |
| 182 | *image_size_p = image_size; |
| 183 | } |
| 184 | } |
| 185 | close(fd); |
| 186 | return image; |
| 187 | } |
| 188 | #endif |
| 189 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 190 | /* Return: |
| 191 | * 0 on success, |
| 192 | * -errno on open/read error, |
| 193 | * errno on init_module() error |
| 194 | */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 195 | int FAST_FUNC bb_init_module(const char *filename, const char *options) |
| 196 | { |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 197 | size_t image_size; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 198 | char *image; |
Denis Vlasenko | 4144504 | 2009-04-13 20:32:31 +0000 | [diff] [blame] | 199 | int rc; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 200 | bool mmaped; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | 3b5c9b8 | 2009-03-13 23:43:26 +0000 | [diff] [blame] | 202 | if (!options) |
| 203 | options = ""; |
| 204 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 205 | //TODO: audit bb_init_module_24 to match error code convention |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 206 | #if ENABLE_FEATURE_2_4_MODULES |
| 207 | if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) |
| 208 | return bb_init_module_24(filename, options); |
| 209 | #endif |
| 210 | |
Mike Frysinger | 3a45b87 | 2016-09-15 12:16:33 +0200 | [diff] [blame] | 211 | /* |
| 212 | * First we try finit_module if available. Some kernels are configured |
| 213 | * to only allow loading of modules off of secure storage (like a read- |
| 214 | * only rootfs) which needs the finit_module call. If it fails, we fall |
| 215 | * back to normal module loading to support compressed modules. |
| 216 | */ |
| 217 | # ifdef __NR_finit_module |
| 218 | { |
| 219 | int fd = open(filename, O_RDONLY | O_CLOEXEC); |
| 220 | if (fd >= 0) { |
| 221 | rc = finit_module(fd, options, 0) != 0; |
| 222 | close(fd); |
| 223 | if (rc == 0) |
| 224 | return rc; |
| 225 | } |
| 226 | } |
| 227 | # endif |
| 228 | |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 229 | image_size = INT_MAX - 4095; |
| 230 | mmaped = 0; |
| 231 | image = try_to_mmap_module(filename, &image_size); |
| 232 | if (image) { |
| 233 | mmaped = 1; |
| 234 | } else { |
| 235 | errno = ENOMEM; /* may be changed by e.g. open errors below */ |
| 236 | image = xmalloc_open_zipped_read_close(filename, &image_size); |
| 237 | if (!image) |
| 238 | return -errno; |
| 239 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 240 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 241 | errno = 0; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 242 | init_module(image, image_size, options); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 243 | rc = errno; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 244 | if (mmaped) |
| 245 | munmap(image, image_size); |
| 246 | else |
| 247 | free(image); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) |
| 252 | { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 253 | errno = 0; |
| 254 | delete_module(module, flags); |
| 255 | return errno; |
| 256 | } |
| 257 | |
Denys Vlasenko | cd13974 | 2015-10-24 04:17:04 +0200 | [diff] [blame] | 258 | /* Note: not suitable for delete_module() errnos. |
| 259 | * For them, probably only EWOULDBLOCK needs explaining: |
| 260 | * "Other modules depend on us". So far we don't do such |
| 261 | * translation and don't use moderror() for removal errors. |
| 262 | */ |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 263 | const char* FAST_FUNC moderror(int err) |
| 264 | { |
| 265 | switch (err) { |
| 266 | case -1: /* btw: it's -EPERM */ |
| 267 | return "no such module"; |
| 268 | case ENOEXEC: |
| 269 | return "invalid module format"; |
| 270 | case ENOENT: |
| 271 | return "unknown symbol in module, or unknown parameter"; |
| 272 | case ESRCH: |
| 273 | return "module has wrong symbol version"; |
| 274 | case ENOSYS: |
| 275 | return "kernel does not support requested operation"; |
| 276 | } |
| 277 | if (err < 0) /* should always be */ |
| 278 | err = -err; |
| 279 | return strerror(err); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 280 | } |