2017-11-17 1 views
1

Ich arbeite an einem Treiber und ich muss UDP-Pakete aus dem Kernel senden. Wenn ich es in einer VM laufe, ist alles in Ordnung und das Paket wird gesendet. Wenn ich es jedoch direkt auf Hardware starte, gibt sock_sendmsg() -1 zurück.Aufruf von sock_sendmsg im Kernel-Modul gibt (-1) Berechtigung verweigert

Ich bin auf Kali Linux mit Kernel-Version 4.13.0-Kali1-AMD64 in beiden Umgebungen ausgeführt. Sie wurden aus Kali Linux 2017.2 erstellt und alle Pakete waren auf dem neuesten Stand.

sockDenied.c:

#include <linux/types.h> 
#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/init.h> 
#include <linux/version.h> 


#include <linux/uaccess.h>  // for get_fs, get_ds, set_fs 
#include <linux/inet.h>   // for in4_pton 
#include <net/sock.h>    // for IPPROTO_IP, SOCK_DGRAM, AF_INET 
#include <linux/net.h>   // for sock_create, sock_alloc_file 


MODULE_AUTHOR("helpme"); 
MODULE_DESCRIPTION("sock_sendmsg recieving permission denied"); 
MODULE_LICENSE("GPL"); 



////////////////////////////////////////////////////////////////////////////// 
//  Global Variables 
////////////////////////////////////////////////////////////////////////////// 

static mm_segment_t _oldfs; 

int my_inet_pton(int af, const char *src, void *dst) { 
    printk("In %s", __FUNCTION__); 
    if (AF_INET == af) { 
     return in4_pton(src, strlen(src), (u8*) dst, '\0', NULL); 
    } else if (AF_INET6 == af) { 
     return in6_pton(src, strlen(src), (u8*) dst, '\0', NULL); 
    } else { 
     printk("uknown af value = %d", af); 
     return -1; 
    } 
} 

////////////////////////////////////////////////////////////////////////////// 
// 
//  Static Functions : UDP 
// 
////////////////////////////////////////////////////////////////////////////// 

static int __init sockdenied_init(void) {  
    int retval = 0; 
    struct msghdr msg = {}; 
    struct iovec iov = {}; 
    char data[2] = {0x01, 0x02}; 
    static struct sockaddr_in txaddr = {}; 

    struct socket* sock = NULL; 
    struct file* fp = NULL; 
    u32 dstip; 

    _oldfs = get_fs(); 
    set_fs(KERNEL_DS); 

    retval = my_inet_pton(AF_INET, "1.1.1.1", &dstip); 

    retval = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_IP, &sock); 
    fp = sock_alloc_file(sock, 0, NULL); 

    txaddr.sin_family = AF_INET; 
    txaddr.sin_addr.s_addr = dstip; 
    txaddr.sin_port = htons(50000); 

    iov.iov_base = data; 
    iov.iov_len = 2; 

    iov_iter_init(&msg.msg_iter, READ, &iov, 1, 1); 

    msg.msg_flags = 0; 
    msg.msg_name = &(txaddr); 
    msg.msg_namelen = sizeof (struct sockaddr_in); 
    msg.msg_control = NULL; 
    msg.msg_controllen = 0; 

    retval = sock_sendmsg(sock, &msg); 

    printk(KERN_INFO"sock_sendmsg returned: %d", retval); 

    sock_release(sock); 

    return 0; 
} 

static void __exit sockdenied_exit(void) { 

    set_fs(_oldfs); 

    return; 
} 

module_init(sockdenied_init); 
module_exit(sockdenied_exit); 

Makefile:

obj-m+=sockDenied.o 
MY_CFLAGS+= -g -DDEBUG 
ccflags-y+= ${MY_CFLAGS} 
CC += ${MY_CFLAGS} 

all: 
    make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules 
    EXTRA_CFLAGS="$(MY_CFLAGS)" 
clean: 
    make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean 

Warum ich recieving -EPERM? Wie kann mein Modul keine Erlaubnis haben, das UDP-Paket zu senden? Warum verhält sich eine VM anders?

+0

Was hat die Funktion 'my_inet_pton' und' sock_create' zurückgegeben? – purplepsycho

+0

sock_create gibt 0 zurück und my_inet_pton gibt 1 zurück – charlesw

Antwort

1

Es stellte sich heraus, dass ich unwissentlich eine Regel für ausgehende IP-Tabellen hatte, die meinen Datenverkehr blockierte. Ich habe die Regel entfernt und das Problem wurde behoben.

Verwandte Themen