From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 1/2] lib: Fix kernel module detection on BusyBox
Date: Tue, 19 Jan 2021 14:31:21 +0100 [thread overview]
Message-ID: <YAbfKSZhZ35peObS@yuki.lan> (raw)
In-Reply-To: <20210118161308.30771-1-pvorel@suse.cz>
Hi!
> #include <sys/personality.h>
> #include <sys/utsname.h>
> +#include <limits.h>
> +
> #include "test.h"
> #include "tst_kernel.h"
> +#include "old_safe_stdio.h"
>
> static int get_kernel_bits_from_uname(struct utsname *buf)
> {
> @@ -81,20 +85,79 @@ int tst_kernel_bits(void)
> return kernel_bits;
> }
>
> -int tst_check_driver(const char *name)
> +#ifndef __ANDROID__
> +# define MODULES_DIR "/lib/modules"
> +#else
> +# define MODULES_DIR "/system/lib/modules"
> +#endif
> +
> +
> +int tst_search_driver(const char *driver, const char *file)
> {
> + struct stat st;
> + char *path = NULL;
> + char buf[PATH_MAX], module[PATH_MAX], search[PATH_MAX] = "/";
> + FILE *f;
> +
> #ifndef __ANDROID__
> - const char * const argv[] = { "modprobe", "-n", name, NULL };
> - int res = tst_cmd_(NULL, argv, "/dev/null", "/dev/null",
> - TST_CMD_PASS_RETVAL);
> + struct utsname uts;
>
> - /* 255 - it looks like modprobe not available */
> - return (res == 255) ? 0 : res;
> + if (uname(&uts)) {
> + tst_brkm(TBROK | TERRNO, NULL, "uname() failed");
> + return -1;
> + }
> + SAFE_ASPRINTF(NULL, &path, "%s/%s/%s", MODULES_DIR, uts.release, file);
> #else
> - /* Android modprobe may not have '-n', or properly installed
> - * module.*.bin files to determine built-in drivers. Assume
> - * all drivers are available.
> + SAFE_ASPRINTF(NULL, &path, "%s/%s", MODULES_DIR, file);
> +#endif
> +
> + if (stat(path, &st) || !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
> +#ifndef __ANDROID__
> + tst_resm(TWARN, "expected file %s does not exist or not a file", path);
> +#endif
> + return -1;
> + }
> +
> + if (access(path, R_OK)) {
> +#ifndef __ANDROID__
> + tst_resm(TWARN, "file %s cannot be read", path);
> +#endif
> + return -1;
> + }
> +
> + strcat(search, driver);
> + strcat(search, ".ko");
Why not just snprintf() or SAFE_ASPRINTF() here as well?
> + f = SAFE_FOPEN(NULL, path, "r");
> +
> + while (fgets(buf, sizeof(buf), f)) {
> + if (sscanf(buf, "%s", module) != 1)
> + continue;
> +
> + if (strstr(module, search) != NULL) {
And I'm not sure that this is safe either, what about the case that one
module name is a substring of another.
E.g. if we look for "foo.ko" and the file contains "this_is_not_foo.ko"
it will still match here.
Also this seems to be rather distruptive change, so I guess it would be
safer to apply after the release.
--
Cyril Hrubis
chrubis@suse.cz
next prev parent reply other threads:[~2021-01-19 13:31 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-18 16:13 [LTP] [PATCH v2 1/2] lib: Fix kernel module detection on BusyBox Petr Vorel
2021-01-19 7:41 ` Petr Vorel
2021-01-20 11:22 ` Joerg Vehlow
2021-01-20 12:11 ` Petr Vorel
2021-01-19 13:31 ` Cyril Hrubis [this message]
2021-01-19 13:39 ` Petr Vorel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YAbfKSZhZ35peObS@yuki.lan \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.