Add qemu scripts to debug the null ptr dereference bug

This commit is contained in:
Sameer Rahmani 2020-08-15 21:01:41 +01:00
parent 8e19822b63
commit c0aefcf280
8 changed files with 106 additions and 43 deletions

4
.gitignore vendored
View File

@ -92,4 +92,6 @@ extra_certificates
signing_key.priv
signing_key.x509
x509.genkey
*.mod
*.mod
root_fs
root.img

View File

@ -6,6 +6,18 @@ build_dir = /lib/modules/$(kernel_version)/build
all:
$(MAKE) -C $(build_dir) M=$(PWD) modules
.PHONY: load
load:
insmod ksudoku.ko
insmod simple.ko
.PHONY: unload
unload:
rmmod simple.ko
rmmod ksudoku.ko
.PHONY: send
send:
@echo "670008010020060000000030000201000006480001700000000009004500000000000300003400802" > /sys/kernel/ksudoku/simple/matrix
clean:
$(MAKE) -C $(build_dir) M=$(PWD) clean
rm *~

View File

@ -1,4 +1,4 @@
/*
/**
* KSudoku subsystem for Linux kernel to solve sudoku with different
* algorithms.
* Copyright (C) 2020 Sameer Rahmani <lxsameer@gnu.org>
@ -117,7 +117,9 @@ static ssize_t matrix_show(struct ksudoku *s,
struct ksudoku_attribute *attr,
char *buf)
{
return sprintf(buf, "%s", s->matrix);
if (s->matrix)
return sprintf(buf, "NULL");
return sprintf(buf, "%s\n", s->matrix);
}
static ssize_t matrix_store(struct ksudoku *s,
@ -126,14 +128,28 @@ static ssize_t matrix_store(struct ksudoku *s,
size_t len)
{
int status = atomic_read(&s->status);
char *p = (char *) buf;
int i, j, retval;
printk(KERN_INFO "len: %ld.\n", len);
if (status == KSUDOKU_BUSY)
return -EBUSY;
if (len != 81)
if (len != 82)
return -EIO;
strncpy(s->matrix, buf, len);
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
char g = (char) *p;
retval = kstrtoint(&g, 10 , &s->matrix_array[i][j]);
if (!retval)
return -EINVAL;
p++;
}
}
return len;
}
@ -189,6 +205,7 @@ struct ksudoku *ksudoku_create_sudoku(const char *name)
}
EXPORT_SYMBOL_GPL(ksudoku_create_sudoku);
/**
* ksudoku_destroy_ksudoku - destroys the given ksudoku instance.
* @sudoku: Pointer to a ksudoku instance to destroy.
@ -199,9 +216,49 @@ void ksudoku_destroy_ksudoku(struct ksudoku *sudoku)
}
EXPORT_SYMBOL_GPL(ksudoku_destroy_ksudoku);
/**
* ksudoku_is_cell_valid - Checks for a valid value in the given cell
* @sudoku: Pointer to a ksudoku instance.
* @v: The value to check
* @row: the row number of the sudoku table
* @col: the column number of the sudoku table
*/
bool ksudoku_is_valid_cell(struct ksudoku *s, int v, int row, int col)
{
int i=0;
int box_row = 3 * (row / 3);
int box_col = 3 * (col / 3);
int row1 = (row + 2) % 3;
int row2 = (row + 4) % 3;
int col1 = (col + 2) % 3;
int col2 = (col + 4) % 3;
/* Check for the value in the given row and column */
for (i = 0; i < 9; i++) {
if (s->matrix_array[i][col] == v)
return false;
if (s->matrix_array[row][i] == v)
return false;
}
/* Check the remaining four spaces in this sector */
if (s->matrix_array[row1 + box_row][col1 + box_col] == v)
return false;
if (s->matrix_array[row2 + box_row][col1 + box_col] == v)
return false;
if (s->matrix_array[row1 + box_row][col2 + box_col] == v)
return false;
if (s->matrix_array[row2 + box_row][col2 + box_col] == v)
return false;
return true;
}
EXPORT_SYMBOL_GPL(ksudoku_is_valid_cell);
static int ksudoku_init(void)
{
printk(KERN_ALERT "Init ksudoku subsystem.\n");
printk(KERN_INFO "Init ksudoku subsystem.\n");
// kernel_kobj is the kobject related to /sys/kernel
ksudoku_set = kset_create_and_add("ksudoku", NULL, kernel_kobj);
@ -209,34 +266,15 @@ static int ksudoku_init(void)
if (!ksudoku_set)
return -ENOMEM;
printk(KERN_ALERT "Ready to register sudokus.\n");
/* sudoku = kmalloc(sizeof (struct ksudoku), GFP_KERNEL); */
/* atomic_set(&sudoku->status, 0); */
/* sudoku->attr_group = &attr_group; */
/* if (!sudoku) */
/* return -ENOMEM; */
/* sudoku->kobj = kobject_create_and_add("ksudoku", kernel_kobj); */
/* if (!&sudoku->kobj) */
/* return -ENOMEM; */
/* sysfs_result = sysfs_create_group(sudoku->kobj, sudoku->attr_group); */
/* if (sysfs_result) */
/* kobject_put(sudoku->kobj); */
printk(KERN_INFO "Ready to register sudokus.\n");
return 0;
}
static void ksudoku_exit(void)
{
//kobject_put(sudoku->kobj);
kset_unregister(ksudoku_set);
printk(KERN_ALERT "Exit ksudoku.\n");
printk(KERN_INFO "Exit ksudoku.\n");
}

View File

@ -1,4 +1,4 @@
/*
/**
* KSudoku subsystem for Linux kernel to solve sudoku with different
* algorithms.
* Copyright (C) 2020 Sameer Rahmani <lxsameer@gnu.org>
@ -33,6 +33,7 @@ struct ksudoku {
struct kobject kobj;
const struct attribute_group *attr_group;
char *matrix;
int matrix_array[9][9];
atomic_t status;
};
#define to_ksudoku(obj) container_of(obj, struct ksudoku, kobj)
@ -50,5 +51,6 @@ struct ksudoku_attribute {
extern struct ksudoku *ksudoku_create_sudoku(const char *name);
extern void ksudoku_destroy_ksudoku(struct ksudoku *sudoku);
extern bool ksudoku_is_valid_cell(struct ksudoku *sudoku, int v, int row, int col);
#endif

View File

@ -1,4 +1,4 @@
/*
/**
* A simple algorithm to solve sudoku for ksudoku subsystem
* Copyright (C) 2020 Sameer Rahmani <lxsameer@gnu.org>
*
@ -24,6 +24,7 @@
static struct ksudoku *sudoku;
static int simple_sudoku_init(void)
{

View File

@ -1,10 +1,9 @@
#! /bin/bash
qemu-system-x86_64 \
-kernel /boot/vmlinuz-`uname -r` \
-nographic \
-append "console=ttyS0 nokaslr init=/bin/bash" \
-initrd `pwd`/ram.disk \
-m 512 \
--enable-kvm \
-cpu host
-kernel /lib/modules/`uname -r`/build/arch/x86/boot/bzImage \
-append "root=/dev/ram init=/init console=ttyS0" \
-initrd `pwd`/initrd.img \
-nographic -smp 1 -cpu host --enable-kvm\
-m 2048 \
-drive file=fat:rw:`pwd`

6
tools/build_initrd.sh Normal file
View File

@ -0,0 +1,6 @@
#! /bin/bash
wget https://www.busybox.net/downloads/busybox-1.31.1.tar.bz2
tar jxvf busybox-1.31.1.tar.bz2
cd busybox-1.31.1

View File

@ -1,12 +1,15 @@
#! /bin/bash
IMG=`pwd`/qemu-image.img
DIR=`pwd`/mounted_fs
IMG=`pwd`/root.img
MOUNT_POINT=`pwd`/mnt
DIR=./root_fs
qemu-img create $IMG 1g
qemu-img create $IMG 1G
sudo mkfs.ext2 $IMG
mkdir $DIR
sudo mount -o loop $IMG $DIR
sudo debootstrap --arch amd64 buster $DIR
sudo umount $DIR
rmdir $DIR
mkdir -p $DIR $MOUNT_POINT
sudo mount -o loop $IMG $MOUNT_POINT
sudo debootstrap stable $DIR http://ftp.de.debian.org/debian
sudo mv $DIR/* $MOUNT_POINT/
sudo umount $MOUNT_POINT
sudo rm -rf $MOUNT_POINT