Linux-Kernel syscall's - Dateien bearbeiten

jkallup

Erfahrenes Mitglied
Hallo,
wollte mal eben ein binary bauen, das nach dem booten eines Linux Kernels
ausgeführt werden soll.
Allerdings scheitert es schon an den kleinen Dingen.
Die Textausgaben stimmen schonmal.
Jetzt möchte ich gerne Dateien schreiben, lesen und anzeigen lassen.
wenn ich den Code unten, mit

gcc -c test01.c -o test01.o

übersetzen will, kommen die merkwürdigsten Meldungen:

test01.c: In function ‘write_file’:
test01.c:30:3: error: unknown type name ‘loff_t’
loff_t pos = 0;
^
test01.c:33:3: error: unknown type name ‘mm_segment_t’
mm_segment_t old_fs = get_fs();
^
test01.c:34:10: error: ‘KERNEL_DS’ undeclared (first use in this function)
set_fs(KERNEL_DS);
^
test01.c:34:10: note: each undeclared identifier is reported only once for each function it appears in
test01.c:36:27: error: ‘O_WRONLY’ undeclared (first use in this function)
fd = sys_open(filename, O_WRONLY|O_CREAT, 0644);
^
test01.c:36:36: error: ‘O_CREAT’ undeclared (first use in this function)
fd = sys_open(filename, O_WRONLY|O_CREAT, 0644);
^
test01.c:38:25: warning: incompatible implicit declaration of built-in function ‘strlen’
sys_write(fd, data, strlen(data));
^
test01.c:39:10: warning: assignment makes pointer from integer without a cast
file = fget(fd);


zweitens: wie kann man verzeichnisse mit kernel funktionen anzeigen lassen?
habe hier einen 4.2.1 linux kernel vorliegen.

Danke, wenn jemand mal Feedback geben kann und sich der Sache mal
an nimmt.


Code:
#define _GNU_SOURCE

#define SYSCALL_EXIT    1
#define SYSCALL_WRITE   4
 
char hello[] = "Hello World!\n";
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
*/

int write_file(char *filename, char *data)
{
  struct file *file;
  loff_t pos = 0;
  int fd;

  mm_segment_t old_fs = get_fs();
  set_fs(KERNEL_DS);

  fd = sys_open(filename, O_WRONLY|O_CREAT, 0644);
  if (fd >= 0) {
    sys_write(fd, data, strlen(data));
    file = fget(fd);
    if (file) {
      vfs_write(file, data, strlen(data), &pos);
      fput(file);
    }
    sys_close(fd);
  }
  else {
    asm("int $0x80" : : "a" (SYSCALL_WRITE), "b" (1), "c" ("plub\n"), "d" (sizeof(5)));
    return -1;
  }
  set_fs(old_fs);
  return 0;
}
 
void _start(void)
{

    // sys_write(1, hello, sizeof(hello))
    asm("int $0x80" : : "a" (SYSCALL_WRITE), "b" (1), "c" (hello), "d"
        (sizeof(hello)));

    // sys_exit(0)
    //asm("int $0x80" : : "a" (SYSCALL_EXIT), "b" (0));

    write_file("/bin/test.txt", "Das sollte hier stehen.");

    for (;;);
}
 
1. Dir fehlen ein Paar includes:

* loff_t ist in <linux/types.h>: http://lxr.free-electrons.com/ident?i=loff_t => http://lxr.free-electrons.com/source/include/linux/types.h#L45
* mm_segment_t ist definiert in <asm/processor.h> oder <asm/segment.h>: http://lxr.free-electrons.com/ident?i=mm_segment_t
* KERNEL_DS ist abhängig von der Ziel-Architektur in <asm/uaccess.h>: http://lxr.free-electrons.com/ident?i=KERNEL_DS
* O_WRONLY und O_CREAT sind in <fcntl.h>: http://lxr.free-electrons.com/ident?i=O_WRONLY ; http://lxr.free-electrons.com/ident?i=O_CREAT
* strlen() ist in <string.h>: http://lxr.free-electrons.com/ident?i=strlen

2. http://stackoverflow.com/a/12921138
 
Hallo,

habe nun zum 10ten mal die Linux Kernel Quellen (4.1.2) gesaugt, entpackt und probiert.
Leider ohne Erfolg.
Ich habe das Ergebins mal hier gepasted:

Code:
#define SYSCALL_EXIT 1
#define SYSCALL_WRITE 4

char hello[] = "Hello World!\n";

//#include <linux/types.h>

#include <linux/module.h>
#include <linux/init.h>

//#include <linux/syscalls.h>
//#include <linux/fs.h>
//#include <linux/fcntl.h>
//#include <linux/vmalloc.h>
#include <linux/mm.h>
//#include <linux/sched.h>

//#include <arch/x86/include/asm/processor.h>
//#include <arch/x86/include/asm/uaccess.h>

//#include <linux/string.h>
//#include <linux/fcntl.h>

//#include <asm/processor.h>

void write_file(char *filename, char *data)
{
struct file *file;
loff_t pos = 0;
int fd;

mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
}
/*
fd = sys_open(filename, O_WRONLY|O_CREAT, 0644);
if (fd >= 0) {
sys_write(fd, data, strlen(data));
file = fget(fd);
if (file) {
vfs_write(file, data, strlen(data), &pos);
fput(file);
}
sys_close(fd);
}
else {
asm("int $0x80" : : "a" (SYSCALL_WRITE), "b" (1), "c" ("plub\n"), "d" (sizeof(5)));
return -1;
}
set_fs(old_fs);
return 0;
}
*/
void _start(void)
{
   // sys_write(1, hello, sizeof(hello))
   asm("int $0x80" : : "a" (SYSCALL_WRITE), "b" (1), "c" (hello), "d" (sizeof(hello)));

   // sys_exit(0)
  //asm("int $0x80" : : "a" (SYSCALL_EXIT), "b" (0));

   // write_file("/bin/test.txt", "Das sollte hier stehen.");

   for (;;);
}

$ gcc -c test01.c -o t.o errors.txt 2>&1

# Ausgabe: errors.txt:

In file included from /usr/src/linux-4.1.2/include/uapi/linux/types.h:4:0,
from /usr/src/linux-4.1.2/include/linux/types.h:5,
from /usr/src/linux-4.1.2/include/linux/list.h:4,
from /usr/src/linux-4.1.2/include/linux/module.h:9,
from test01.c:8:
/usr/include/x86_64-linux-gnu/asm/types.h:4:31: fatal error: asm-generic/types.h: Datei oder Verzeichnis nicht gefunden
#include <asm-generic/types.h>
^
compilation terminated.

ist da irgend ein Trick dabei?
kernel Source futsch?

würde mich auf Antwort freuen
 
Zuletzt bearbeitet von einem Moderator:
ich habe nun mal den neusten stable kernel gesaugt und entpackt.
dann sowas:

Code:
root@debian:/usr/src/linux-4.4.1# make menuconfig
  HOSTCC  scripts/basic/fixdep
scripts/basic/fixdep.c:106:23: fatal error: sys/types.h: Datei oder Verzeichnis nicht gefunden
 #include <sys/types.h>
  ^
compilation terminated.
scripts/Makefile.host:91: recipe for target 'scripts/basic/fixdep' failed
make[1]: *** [scripts/basic/fixdep] Error 1
Makefile:439: recipe for target 'scripts_basic' failed
make: *** [scripts_basic] Error 2

root@debian:/usr/src/linux-4.4.1# dd
 
So, Rückmeldung:

habe jetzt Teile des Systems (header sources) neuinstalliert.
den neuesten Kernel gesaugt und nun geht wieder (fast) alles
musste noch zusätzlich ncurses-dev installieren,
und weitere....

aber alles io nun
 
Zurück