mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-29 13:04:27 +01:00
kstrto: move doc to README
This commit is contained in:
24
README.adoc
24
README.adoc
@@ -4076,6 +4076,30 @@ The module also shows which handlers are registered for each IRQ, as we have obs
|
|||||||
|
|
||||||
When in text mode, we can also observe interrupt line 4 with handler `ttyS0` increase continuously as IO goes through the UART.
|
When in text mode, we can also observe interrupt line 4 with handler `ttyS0` increase continuously as IO goes through the UART.
|
||||||
|
|
||||||
|
=== Linux kernel utility functions
|
||||||
|
|
||||||
|
==== kstrto
|
||||||
|
|
||||||
|
Convert a string to an integer:
|
||||||
|
|
||||||
|
....
|
||||||
|
/kstrto.sh
|
||||||
|
echo $?
|
||||||
|
....
|
||||||
|
|
||||||
|
Outcome: the test passes:
|
||||||
|
|
||||||
|
....
|
||||||
|
0
|
||||||
|
....
|
||||||
|
|
||||||
|
Sources:
|
||||||
|
|
||||||
|
* link:kernel_module/kstrto.c[]
|
||||||
|
* link:rootfs_overlay/kstrto.sh[]
|
||||||
|
|
||||||
|
Bibliography: https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
|
||||||
|
|
||||||
=== Linux kernel tracing
|
=== Linux kernel tracing
|
||||||
|
|
||||||
Good overviews:
|
Good overviews:
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ Our kernel modules!
|
|||||||
. Misc
|
. Misc
|
||||||
.. link:virt_to_phys.c[]
|
.. link:virt_to_phys.c[]
|
||||||
.. link:netlink.c[]
|
.. link:netlink.c[]
|
||||||
. Utilities
|
|
||||||
.. link:kstrto.c[]
|
|
||||||
. Hardening
|
. Hardening
|
||||||
.. link:strlen_overflow.c[]
|
.. link:strlen_overflow.c[]
|
||||||
. Tracing
|
. Tracing
|
||||||
|
|||||||
@@ -1,36 +1,48 @@
|
|||||||
/*
|
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kstrto */
|
||||||
https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
|
|
||||||
/kstrto.sh
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <linux/debugfs.h>
|
#include <linux/debugfs.h>
|
||||||
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <uapi/linux/stat.h> /* S_IRUSR */
|
#include <linux/seq_file.h>
|
||||||
|
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
|
||||||
|
#include <uapi/linux/stat.h> /* S_IWUSR */
|
||||||
|
|
||||||
static struct dentry *toplevel_file;
|
static struct dentry *toplevel_file;
|
||||||
|
static char read_buf[1024];
|
||||||
|
|
||||||
|
static int show(struct seq_file *m, void *v)
|
||||||
|
{
|
||||||
|
seq_printf(m, read_buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int open(struct inode *inode, struct file *file)
|
||||||
|
{
|
||||||
|
return single_open(file, show, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
|
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
|
||||||
{
|
{
|
||||||
int ret;
|
ssize_t ret;
|
||||||
unsigned long long res;
|
int kstrto_return;
|
||||||
ret = kstrtoull_from_user(buf, len, 10, &res);
|
unsigned long long kstrto_result;
|
||||||
if (ret) {
|
kstrto_return = kstrtoull_from_user(buf, len, 10, &kstrto_result);
|
||||||
|
if (kstrto_return) {
|
||||||
/* Negative error code. */
|
/* Negative error code. */
|
||||||
pr_info("ko = %d\n", ret);
|
ret = kstrto_return;
|
||||||
return ret;
|
|
||||||
} else {
|
} else {
|
||||||
pr_info("ok = %llu\n", res);
|
ret = len;
|
||||||
*off= len;
|
|
||||||
return len;
|
|
||||||
}
|
}
|
||||||
|
snprintf(read_buf, sizeof(read_buf), "%llu", kstrto_result + 1);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct file_operations fops = {
|
static const struct file_operations fops = {
|
||||||
|
.llseek = seq_lseek,
|
||||||
|
.open = open,
|
||||||
.owner = THIS_MODULE,
|
.owner = THIS_MODULE,
|
||||||
|
.read = seq_read,
|
||||||
|
.release = single_release,
|
||||||
.write = write,
|
.write = write,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
f=/sys/kernel/debug/lkmc_kstrto
|
||||||
insmod /kstrto.ko
|
insmod /kstrto.ko
|
||||||
cd /sys/kernel/debug
|
printf 123 > "$f"
|
||||||
echo 1234 > lkmc_kstrto
|
[ "$(cat "$f")" = 124 ]
|
||||||
# 1234
|
echo foobar > "$f" && exit 1
|
||||||
echo foobar > lkmc_kstrto
|
rmmod kstrto
|
||||||
# -22
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ for test in \
|
|||||||
/fops.sh \
|
/fops.sh \
|
||||||
/init_module.sh \
|
/init_module.sh \
|
||||||
/ioctl.sh \
|
/ioctl.sh \
|
||||||
|
/kstrto.sh \
|
||||||
/mmap.sh \
|
/mmap.sh \
|
||||||
/params.sh \
|
/params.sh \
|
||||||
/procfs.sh \
|
/procfs.sh \
|
||||||
|
|||||||
Reference in New Issue
Block a user