linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tiezhu Yang <yangtiezhu@loongson.cn>
To: Luis Chamberlain <mcgrof@kernel.org>,
	Shuah Khan <shuah@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jessica Yu <jeyu@kernel.org>
Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	Markus Elfring <Markus.Elfring@web.de>,
	Xuefeng Li <lixuefeng@loongson.cn>
Subject: [PATCH v4 3/4] kmod: Return directly if module name is empty in request_module()
Date: Tue, 21 Apr 2020 15:05:03 +0800	[thread overview]
Message-ID: <1587452704-1299-4-git-send-email-yangtiezhu@loongson.cn> (raw)
In-Reply-To: <1587452704-1299-1-git-send-email-yangtiezhu@loongson.cn>

If module name is empty, it is better to return directly at the beginning
of request_module() without doing the needless call_modprobe() operation.

(1) In the kernel space

Call trace:

request_module()
      |
      |
__request_module()
      |
      |
call_modprobe()
      |
      |
call_usermodehelper_exec() -- retval = sub_info->retval;
      |
      |
call_usermodehelper_exec_work()
      |
      |
call_usermodehelper_exec_sync() -- sub_info->retval = ret;
      |
      | --> call_usermodehelper_exec_async() --> do_execve()
      |
kernel_wait4(pid, (int __user *)&ret, 0, NULL);

The exit status of child process is wrote to the address of variable
"ret" after call kernel_wait4(), its value is 256 if module name is
empty, then sub_info->retval is assigned to "ret" which is 256, the
function call_usermodehelper_exec() returns 256, call_modprobe() and
__request_module() also return 256.

(2)In the user space

when build and execute the following application, we can see the exit
status is 256.

$ ./system
exit status = 256

$ ./execl
exit status = 256

$ cat system.c
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int status = 0;

	status = system("modprobe '' > /dev/null 2>&1");
	printf("exit status = %d\n", status);

	return status;
}

$ cat execl.c
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
	pid_t pid, w;
	int status;

	pid = fork();
	if (pid == -1) {
		perror("fork");
		exit(EXIT_FAILURE);
	}

	if (pid == 0) {
		execl("/bin/sh", "sh", "-c",
		      "modprobe '' > /dev/null 2>&1", (char *) 0);
	} else {
		w = waitpid(pid, &status, 0);
		if (w == -1) {
			perror("waitpid");
			exit(EXIT_FAILURE);
		}

		printf("exit status = %d\n", status);
		exit(EXIT_SUCCESS);
	}

	return 0;
}

The exit status of child process is wrote to the address of variable
"status" after call waitpid() in the user space that corresponds with
kernel_wait4() in the kernel space.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---

v4:
  - update the commit message with application

v3:
  - no changes

v2:
  - update the commit message to explain the detailed reason

 kernel/kmod.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/kmod.c b/kernel/kmod.c
index 3cd075c..5851444 100644
--- a/kernel/kmod.c
+++ b/kernel/kmod.c
@@ -28,6 +28,8 @@
 
 #include <trace/events/module.h>
 
+#define MODULE_NOT_FOUND 256
+
 /*
  * Assuming:
  *
@@ -144,6 +146,9 @@ int __request_module(bool wait, const char *fmt, ...)
 	if (ret >= MODULE_NAME_LEN)
 		return -ENAMETOOLONG;
 
+	if (strlen(module_name) == 0)
+		return MODULE_NOT_FOUND;
+
 	ret = security_kernel_module_request(module_name);
 	if (ret)
 		return ret;
-- 
2.1.0


  parent reply	other threads:[~2020-04-21  7:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-21  7:05 [PATCH v4 0/4] Fix some issues about kmod Tiezhu Yang
2020-04-21  7:05 ` [PATCH v4 1/4] selftests: kmod: Use variable NAME in kmod_test_0001() Tiezhu Yang
2020-04-21  7:05 ` [PATCH v4 2/4] kmod: Remove redundant "be an" in the comment Tiezhu Yang
2020-04-21  7:05 ` Tiezhu Yang [this message]
2020-04-22  8:32   ` [PATCH v4 3/4] kmod: Return directly if module name is empty in request_module() Luis Chamberlain
2020-04-21  7:05 ` [PATCH v4 4/4] test_kmod: Avoid potential double free in trigger_config_run_type() Tiezhu Yang
2020-05-11 12:59 ` [PATCH v4 0/4] Fix some issues about kmod Tiezhu Yang
2020-05-11 18:28   ` Luis Chamberlain
2020-06-10  2:33     ` Luis Chamberlain

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=1587452704-1299-4-git-send-email-yangtiezhu@loongson.cn \
    --to=yangtiezhu@loongson.cn \
    --cc=Markus.Elfring@web.de \
    --cc=akpm@linux-foundation.org \
    --cc=jeyu@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lixuefeng@loongson.cn \
    --cc=mcgrof@kernel.org \
    --cc=shuah@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).