diff --git a/20160425/.gitignore b/20160425/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..26087ef26d599af6df0c40a10cd275cd7c579c52 --- /dev/null +++ b/20160425/.gitignore @@ -0,0 +1,25 @@ +.chardev-1.ko.cmd +.chardev-1.mod.o.cmd +.chardev-1.o.cmd +.chardev-1write.ko.cmd +.chardev-1write.mod.o.cmd +.chardev-1write.o.cmd +.hellomod-1.ko.cmd +.hellomod-1.mod.o.cmd +.hellomod-1.o.cmd +.tmp_versions/ +Module.symvers +chardev-1.ko +chardev-1.mod.c +chardev-1.mod.o +chardev-1.o +chardev-1write.ko +chardev-1write.mod.c +chardev-1write.mod.o +chardev-1write.o +hellomod-1.ko +hellomod-1.mod.c +hellomod-1.mod.o +hellomod-1.o +modules.order + diff --git a/20160425/Makefile b/20160425/Makefile index 263a0a5effc01e8110f1626a3d9540e646715deb..6075827316530be068f67b43ec8f020019534fdc 100644 --- a/20160425/Makefile +++ b/20160425/Makefile @@ -1,5 +1,6 @@ obj-m += hellomod-1.o obj-m += chardev-1.o +obj-m += chardev-1write.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules diff --git a/20160425/chardev-1write.c b/20160425/chardev-1write.c new file mode 100644 index 0000000000000000000000000000000000000000..bff85170ae5012eb4bf8e82b690dd379e19ad262 --- /dev/null +++ b/20160425/chardev-1write.c @@ -0,0 +1,213 @@ +/* Modified by: Christian Löpke <christian.loepke@hs-bochum.de> + * chardev-1write.c: Creates a char device that says Mr. Anderson or a + * written name how many times he read from the dev + * file. + */ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/fs.h> +#include <asm/uaccess.h> /* for put_user */ + +/* + * Prototypes - this would normally go in a .h file + */ +int init_module(void); +void cleanup_module(void); +static int device_open(struct inode *, struct file *); +static int device_release(struct inode *, struct file *); +static ssize_t device_read(struct file *, char *, size_t, loff_t *); +static ssize_t device_write(struct file *, const char *, size_t, loff_t *); + +#define SUCCESS 0 +#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */ +#define BUF_LEN 80 /* Max length of the message from the device */ +#define USERBUF_LEN 20 /* Max length for user definable name */ +/* + * Global variables are declared as static, so are global within the file. + */ + +static int Major; /* Major number assigned to our device driver */ +static int Device_Open = 0; /* Is device open? + * Used to prevent multiple access to device */ +static char msg[BUF_LEN]; /* The msg the device will give when asked */ +static char *msg_Ptr; /* The current char from msg we are writing */ + +static char msg_username[USERBUF_LEN]; /* User definable name */ +static int told_you_counter = 0; /* Counts the outputs I've made */ + +static struct file_operations fops = { + .read = device_read, + .write = device_write, + .open = device_open, + .release = device_release +}; + +/* + * This function is called when the module is loaded + */ +int init_module(void) +{ + Major = register_chrdev(0, DEVICE_NAME, &fops); + + if (Major < 0) { + printk( KERN_ALERT + "%s: Registering char device failed with %d\n", + DEVICE_NAME, + Major); + return Major; + } + + printk( KERN_INFO "%s: I was assigned major number %d. To talk to\n", + DEVICE_NAME, + Major); + printk( KERN_INFO + "%s: the driver, create a dev file with\n", DEVICE_NAME); + printk( KERN_INFO "%s: 'mknod /dev/%s c %d 0'.\n", + DEVICE_NAME, + DEVICE_NAME, + Major); + printk( KERN_INFO + "%s: Try various minor numbers. Try to cat and echo to\n", + DEVICE_NAME); + printk( KERN_INFO "%s: the device file.\n", DEVICE_NAME); + printk( KERN_INFO + "%s: Remove the device file and module when done.\n", + DEVICE_NAME); + + sprintf(msg_username, "Mr. Anderson"); //Setup initial username. + return SUCCESS; +} + +/* + * This function is called when the module is unloaded + */ +void cleanup_module(void) +{ + /* + * Unregister the device + */ + unregister_chrdev(Major, DEVICE_NAME); +} + +/* + * Methods + */ + +/* + * Called when a process tries to open the device file, like + * "cat /dev/mycharfile" + */ +static int device_open(struct inode *inode, struct file *file) +{ + if (Device_Open) + return -EBUSY; + + Device_Open++; + sprintf(msg, + "I already told you %d times Hello world!, %s.\n", + told_you_counter++, + msg_username); + msg_Ptr = msg; + try_module_get(THIS_MODULE); + + return SUCCESS; /* Device successfully opened */ +} + +/* + * Called when a process closes the device file. + */ +static int device_release(struct inode *inode, struct file *file) +{ + Device_Open--; /* We're now ready for our next caller */ + + /* + * Decrement the usage count, or else once you opened the file, you'll + * never get get rid of the module. + */ + module_put(THIS_MODULE); + + return 0; /* Device sucessfully closed */ +} + +/* + * Called when a process, which already opened the dev file, attempts to + * read from it. + */ +static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ + char *buffer, /* buffer to fill with data */ + size_t length, /* length of the buffer */ + loff_t * offset) +{ + /* + * Number of bytes actually written to the buffer + */ + int bytes_read = 0; + + /* + * If we're at the end of the message, + * return 0 signifying end of file + */ + if (*msg_Ptr == 0) + return 0; + + /* + * Actually put the data into the buffer + */ + while (length && *msg_Ptr) { + + /* + * The buffer is in the user data segment, not the kernel + * segment so "*" assignment won't work. We have to use + * put_user which copies data from the kernel data segment to + * the user data segment. + */ + put_user(*(msg_Ptr++), buffer++); + + length--; + bytes_read++; + } + + /* + * Most read functions return the number of bytes put into the buffer + */ + return bytes_read; +} + +/* + * Called when a process writes to dev file: echo "hi" > /dev/hello + */ +static ssize_t +device_write(struct file *filp, const char *buff, size_t len, loff_t * off) +{ + /* Since nobody wants to read I have to correct the already told you + counter! */ + told_you_counter--; + if(len >= USERBUF_LEN) { + printk( KERN_WARNING + "%s: Sorry, your input is too long. Max %d allowed!\n", + DEVICE_NAME, + USERBUF_LEN); + return -ENOMEM; //Return not enaugh memory + } + else { + int i; + for(i = 0; i < len; i++) {//Save the given string + if(get_user(msg_username[i], buff + i) < 0) { //If returning error code + printk(KERN_WARNING + "%s: This memory does not belong to you!\n", + DEVICE_NAME); + return -EFAULT; //Stop reading from userspace. + } + //msg_username[i] = buff[i]; //Heres was the CRASH + //printk(KERN_INFO "Got %c at %d from %d bytes.\n", buff[i], i, len); + } + for(i = len; i < USERBUF_LEN; i++) //Zeroing the rest of mem + msg_username[i] = 0; + printk(KERN_INFO "%s: Successfully got %d bytes!\n", + DEVICE_NAME, (int)len); + if(msg_username[len-1] == '\n') //Got new line terminator ? + msg_username[len-1] = 0; //Then terminate it! + return len; //Emit successfully stored len bytes + } +} diff --git a/20160425/dmesgCrash.log b/20160425/dmesgCrash.log new file mode 100644 index 0000000000000000000000000000000000000000..2dee7a4aa5ee46eacf9ba5cb541d243b8212a1a2 --- /dev/null +++ b/20160425/dmesgCrash.log @@ -0,0 +1,83 @@ +Mai 08 20:02:22 archtux kernel: BUG: unable to handle kernel paging +request at 0000000000ba9810 +Mai 08 20:02:22 archtux kernel: IP: [<ffffffffa05d6138>] +device_write+0xa8/0xd0 [chardev_1write] +Mai 08 20:02:22 archtux kernel: PGD 251819067 PUD 251b40067 PMD +2518e4067 PTE 800000022be53067 +Mai 08 20:02:22 archtux kernel: Oops: 0001 [#4] PREEMPT SMP +Mai 08 20:02:22 archtux kernel: Modules linked in: chardev_1write(PO) +ctr ccm fuse snd_hda_codec_hdmi snd_hda_codec_realtek +snd_hda_codec_generic joydev mousedev arc4 iwlmvm snd_soc_skl +snd_soc_skl_ipc i915 snd_soc_sst_ipc snd_soc_sst_dsp snd_hda_ext_core +snd_soc_sst_match snd_soc_core snd_compress mac80211 snd_pcm_dmaengine +ac97_bus drm_kms_helper intel_rapl x86_pkg_temp_thermal snd_hda_intel +snd_hda_codec intel_powerclamp coretemp snd_hda_core iwlwifi uvcvideo +videobuf2_vmalloc videobuf2_memops snd_hwdep kvm_intel evdev drm +input_leds snd_pcm videobuf2_v4l2 cfg80211 intel_gtt led_class +syscopyarea sysfillrect snd_timer sysimgblt kvm fb_sys_fops snd +irqbypass mac_hid mei_me videobuf2_core pcspkr i2c_algo_bit shpchp +soundcore mei serio_raw psmouse videodev btusb hci_uart btrtl btbcm +btqca btintel media i2c_i801 thermal +Mai 08 20:02:22 archtux kernel: bluetooth battery video +pinctrl_sunrisepoint pinctrl_intel rfkill tpm_tis i2c_hid hid +intel_lpss_acpi intel_lpss tpm fjes processor acpi_pad button ac +sch_fq_codel ip_tables x_tables sha256_ssse3 sha256_generic hmac drbg +ansi_cprng algif_skcipher af_alg dm_crypt dm_mod sd_mod atkbd libps2 +crct10dif_pclmul crc32_pclmul crc32c_intel ahci ghash_clmulni_intel +libahci libata xhci_pci aesni_intel xhci_hcd aes_x86_64 lrw gf128mul +glue_helper ablk_helper cryptd usbcore scsi_mod usb_common i8042 serio +ext4 crc16 mbcache jbd2 [last unloaded: chardev_1write] +Mai 08 20:02:22 archtux kernel: CPU: 2 PID: 3447 Comm: bash Tainted: P +D O 4.5.2-1-ARCH #1 +Mai 08 20:02:22 archtux kernel: Hardware name: Intel Corporation Skylake +Platform/U931, BIOS 5.11 04/08/2016 +Mai 08 20:02:22 archtux kernel: task: ffff8802671b0000 ti: +ffff8802597e0000 task.ti: ffff8802597e0000 +Mai 08 20:02:22 archtux kernel: RIP: 0010:[<ffffffffa05d6138>] +[<ffffffffa05d6138>] device_write+0xa8/0xd0 [chardev_1write] +Mai 08 20:02:22 archtux kernel: RSP: 0018:ffff8802597e3e10 EFLAGS: +00010246 +Mai 08 20:02:22 archtux kernel: RAX: 0000000000000000 RBX: +0000000000000005 RCX: ffff8802597e3f18 +Mai 08 20:02:22 archtux kernel: RDX: 0000000000000005 RSI: +0000000000ba9810 RDI: ffff880246f4c900 +Mai 08 20:02:22 archtux kernel: RBP: ffff8802597e3e38 R08: +0000000000ba9815 R09: 00007ffffffff000 +Mai 08 20:02:22 archtux kernel: R10: 0000000000000004 R11: +0000000000000246 R12: 0000000000ba9810 +Mai 08 20:02:22 archtux kernel: R13: 0000000000000000 R14: +ffffffffa05d7090 R15: 0000000000000005 +Mai 08 20:02:22 archtux kernel: FS: 00007f1037d52700(0000) +GS:ffff880273d00000(0000) knlGS:0000000000000000 +Mai 08 20:02:22 archtux kernel: CS: 0010 DS: 0000 ES: 0000 CR0: +0000000080050033 +Mai 08 20:02:22 archtux kernel: CR2: 0000000000ba9810 CR3: +0000000268c22000 CR4: 00000000003406e0 +Mai 08 20:02:22 archtux kernel: Stack: +Mai 08 20:02:22 archtux kernel: ffff880246f4c900 ffff8802597e3f18 +0000000000ba9810 ffff8802597e3f18 +Mai 08 20:02:22 archtux kernel: 0000000000000000 ffff8802597e3ec0 +ffffffff811ecde7 00000000354527a1 +Mai 08 20:02:22 archtux kernel: 00000000354527a1 000000000000000a +000000000000000a 0000000000000400 +Mai 08 20:02:22 archtux kernel: Call Trace: +Mai 08 20:02:22 archtux kernel: [<ffffffff811ecde7>] +__vfs_write+0x37/0x100 +Mai 08 20:02:22 archtux kernel: [<ffffffff811edc97>] +vfs_write+0xa7/0x1a0 +Mai 08 20:02:22 archtux kernel: [<ffffffff811eebe5>] +SyS_write+0x55/0xc0 +Mai 08 20:02:22 archtux kernel: [<ffffffff8120ab43>] ? +__close_fd+0xa3/0xd0 +Mai 08 20:02:22 archtux kernel: [<ffffffff815b0b6e>] +entry_SYSCALL_64_fastpath+0x12/0x6d +Mai 08 20:02:22 archtux kernel: Code: 04 72 5d a0 48 c7 c7 58 70 5d a0 +e8 a0 01 b9 e0 48 c7 c0 f4 ff ff ff eb cc 49 89 f4 41 89 d7 31 c0 45 31 +ed 49 c7 c6 90 70 5d a0 <41> 0f be 34 04 44 89 ea 44 89 f9 41 83 c5 01 +4c 89 f7 e8 6e 01 +Mai 08 20:02:22 archtux kernel: RIP [<ffffffffa05d6138>] +device_write+0xa8/0xd0 [chardev_1write] +Mai 08 20:02:22 archtux kernel: RSP <ffff8802597e3e10> +Mai 08 20:02:22 archtux kernel: CR2: 0000000000ba9810 +Mai 08 20:02:22 archtux kernel: ---[ end trace 2e7525e9567b5a55 ]--- +