From c0aefcf2804614d208f2af4a635629a58b7e603a Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sat, 15 Aug 2020 21:01:41 +0100 Subject: [PATCH] Add qemu scripts to debug the null ptr dereference bug --- .gitignore | 4 +- Makefile | 12 ++++++ ksudoku.c | 88 +++++++++++++++++++++++++++++++------------ ksudoku.h | 4 +- simple.c | 3 +- tools/boot_kernel.sh | 13 +++---- tools/build_initrd.sh | 6 +++ tools/create_fs.sh | 19 ++++++---- 8 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 tools/build_initrd.sh diff --git a/.gitignore b/.gitignore index 2efdb2e..3fbd2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -92,4 +92,6 @@ extra_certificates signing_key.priv signing_key.x509 x509.genkey -*.mod \ No newline at end of file +*.mod +root_fs +root.img \ No newline at end of file diff --git a/Makefile b/Makefile index 7582506..bc3ac32 100644 --- a/Makefile +++ b/Makefile @@ -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 *~ diff --git a/ksudoku.c b/ksudoku.c index 1e35d37..a1ddb79 100644 --- a/ksudoku.c +++ b/ksudoku.c @@ -1,4 +1,4 @@ -/* +/** * KSudoku subsystem for Linux kernel to solve sudoku with different * algorithms. * Copyright (C) 2020 Sameer Rahmani @@ -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"); } diff --git a/ksudoku.h b/ksudoku.h index de3cfe7..915c7af 100644 --- a/ksudoku.h +++ b/ksudoku.h @@ -1,4 +1,4 @@ -/* +/** * KSudoku subsystem for Linux kernel to solve sudoku with different * algorithms. * Copyright (C) 2020 Sameer Rahmani @@ -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 diff --git a/simple.c b/simple.c index f3e99da..ed20714 100644 --- a/simple.c +++ b/simple.c @@ -1,4 +1,4 @@ -/* +/** * A simple algorithm to solve sudoku for ksudoku subsystem * Copyright (C) 2020 Sameer Rahmani * @@ -24,6 +24,7 @@ static struct ksudoku *sudoku; + static int simple_sudoku_init(void) { diff --git a/tools/boot_kernel.sh b/tools/boot_kernel.sh index 8f57fa4..87f7c58 100755 --- a/tools/boot_kernel.sh +++ b/tools/boot_kernel.sh @@ -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` diff --git a/tools/build_initrd.sh b/tools/build_initrd.sh new file mode 100644 index 0000000..3ef88e4 --- /dev/null +++ b/tools/build_initrd.sh @@ -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 diff --git a/tools/create_fs.sh b/tools/create_fs.sh index 9da2f03..010bfd9 100755 --- a/tools/create_fs.sh +++ b/tools/create_fs.sh @@ -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