Linux filesystem development
 help / color / mirror / Atom feed
* Garbage data while reading via usermode driver?
@ 2020-11-16 11:11 Tetsuo Handa
  2020-11-16 12:23 ` Tetsuo Handa
  2020-11-16 12:35 ` Al Viro
  0 siblings, 2 replies; 4+ messages in thread
From: Tetsuo Handa @ 2020-11-16 11:11 UTC (permalink / raw)
  To: Alexander Viro, Eric W. Biederman, Alexei Starovoitov; +Cc: linux-fsdevel

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 "#!" ?


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-11-16 14:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-16 11:11 Garbage data while reading via usermode driver? Tetsuo Handa
2020-11-16 12:23 ` Tetsuo Handa
2020-11-16 12:35 ` Al Viro
2020-11-16 14:15   ` Tetsuo Handa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox