linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	Alexei Starovoitov <ast@kernel.org>
Cc: linux-fsdevel <linux-fsdevel@vger.kernel.org>
Subject: Garbage data while reading via usermode driver?
Date: Mon, 16 Nov 2020 20:11:04 +0900	[thread overview]
Message-ID: <0c98ab7f-8483-bb54-7b8f-3d69ed45f1ff@i-love.sakura.ne.jp> (raw)

Hello.

Below is a loadable kernel module which attempts to read (for example) /proc/interrupts from
kernel using usermode driver interface. What is strange is that the total bytes obtained by
doing "wc -c /proc/interrupts" from userspace's shell and trying to insmod this kernel module
differs; for unknown reason, kernel_read() returns "#!/bin/cat /proc/interrupts\n" (28 bytes)
at the end of input.

---------- Start of sample module ----------
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/usermode_driver.h>

static int __init umd_test_init(void)
{
	static const char program[] = "#!/bin/cat /proc/interrupts\n";
	struct umd_info umd = { .driver_name = "umd_test" };
	static char buffer[512];
	loff_t pos = 0;
	int total = 0;
	int ret;

	if (umd_load_blob(&umd, program, sizeof(program) - 1))
		return -EINVAL;
	ret = fork_usermode_driver(&umd);
	if (ret == 0) {
		memset(buffer, 0, sizeof(buffer));
		while ((ret = kernel_read(umd.pipe_from_umh, buffer, sizeof(buffer) - 1, &pos)) > 0) {
			buffer[ret] = '\0';
			printk("buffer='%s'\n", buffer);
			total += ret;
		}
		printk("ret=%d total=%u\n", ret, total);
	}
	umd_unload_blob(&umd);
	return -EINVAL;
}

module_init(umd_test_init);
MODULE_LICENSE("GPL");
---------- End of sample module ----------

If I don't use "#!" (i.e. replacing

  "#!/bin/cat /proc/kallsyms\n"

with binary data generated by compiling

---------- Start of sample usermode code ----------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	const int fd = open("/proc/interrupts", O_RDONLY);
	char buffer[4096];
	int len;

	while ((len = read(fd, buffer, sizeof(buffer))) > 0 &&
	       write(1, buffer, len) == len);
	return !!len;
}
---------- End of sample usermode code ----------

and converted by ./scripts/bin2c ), the total bytes obtained by doing
"wc -c /proc/interrupts" from userspace's shell and trying to insmod
this kernel module matches (i.e. there is no garbage).

Why there is garbage data if I use "#!" ?


             reply	other threads:[~2020-11-16 12:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-16 11:11 Tetsuo Handa [this message]
2020-11-16 12:23 ` Garbage data while reading via usermode driver? Tetsuo Handa
2020-11-16 12:35 ` Al Viro
2020-11-16 14:15   ` Tetsuo Handa

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=0c98ab7f-8483-bb54-7b8f-3d69ed45f1ff@i-love.sakura.ne.jp \
    --to=penguin-kernel@i-love.sakura.ne.jp \
    --cc=ast@kernel.org \
    --cc=ebiederm@xmission.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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).