# Interacting with Kernel Modules

## Character devices

```c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define KERN_MODULE "/dev/kernel-overflow"
void main()
{
    /*
     * Interacting with this kernel module is easy
     * just treat it like a file
     */

    int fd;
    unsigned long stack_cookie;

    fd = open(KERN_MODULE, O_RDWR);
    if (fd < 0) exit(-1);
    
    close(fd);
    
}
```

```c
unsigned long do_read(int fd)
{
    int bytes_read;
    unsigned long * buf = NULL;
    unsigned long stack_cookie;
    unsigned int cookie_offset = 16;

    buf = malloc(BUF_SIZE);
    if (buf == NULL) exit_and_log("Failed to malloc\n");
---
    memset(buf, '\x00', BUF_SIZE);

    bytes_read = read(fd, buf, BUF_SIZE);

    /*
     * For every 8 bytes read, print them
     */
    for(int i =0; i <(BUF_SIZE / WORD_SIZE);i++)
    {
        printf("buf + 0x%X\t: %lX\n",i*WORD_SIZE, buf[i]);        
    }

    stack_cookie = buf[cookie_offset];
    free(buf);

    return stack_cookie; 
}
```

![](/files/-Ml7BlV48AJYpKAFEzly)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://breaking-bits.gitbook.io/breaking-bits/exploit-development/linux-kernel-exploit-development/interacting-with-kernel-modules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
